You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

630 lines
19 KiB

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>六步法编辑器</title>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
background: #fafafa;
}
#app {
width: 100%;
height: 100vh;
}
.step-editor-container {
display: flex;
flex-direction: column;
height: 100vh;
background: #fafafa;
}
.editor-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 32px;
height: 56px;
background: #fff;
border-bottom: 1px solid #e8e8e8;
}
.header-left {
display: flex;
align-items: center;
gap: 16px;
}
.back-btn {
padding: 8px 12px;
font-size: 14px;
color: #666;
}
.back-btn:hover {
color: #409EFF;
}
.step-title {
font-size: 16px;
font-weight: 500;
color: #333;
}
.save-btn {
padding: 8px 24px;
font-size: 14px;
border-radius: 4px;
}
.editor-toolbar {
display: flex;
align-items: center;
gap: 0;
padding: 0 32px;
height: 44px;
background: #fff;
border-bottom: 1px solid #e8e8e8;
}
.toolbar-group {
display: flex;
align-items: center;
gap: 4px;
}
.toolbar-btn {
width: 36px;
height: 36px;
padding: 0;
border-radius: 4px;
color: #666;
font-size: 16px;
transition: all 0.2s;
}
.toolbar-btn:hover {
background: #f5f5f5;
color: #409EFF;
}
.format-icon {
font-size: 16px;
font-weight: 700;
font-family: Arial, sans-serif;
font-style: normal;
}
.format-icon.italic {
font-style: italic;
}
.format-icon.underline {
text-decoration: underline;
}
.format-icon.strike {
text-decoration: line-through;
}
.toolbar-divider {
width: 1px;
height: 24px;
background: #e8e8e8;
margin: 0 16px;
}
.word-count {
font-size: 12px;
color: #999;
padding: 0 12px;
min-width: 60px;
text-align: right;
}
.editor-content {
flex: 1;
overflow: hidden;
padding: 32px;
display: flex;
justify-content: center;
}
.editor-area {
width: 100%;
max-width: 840px;
height: 100%;
background: #fff;
border-radius: 8px;
padding: 40px;
overflow-y: auto;
outline: none;
font-size: 16px;
line-height: 1.8;
color: #333;
}
.editor-area:empty:before {
content: attr(data-placeholder);
color: #c0c4cc;
pointer-events: none;
}
.editor-area p {
margin: 16px 0;
}
.editor-area img {
max-width: 100%;
height: auto;
margin: 10px 0;
}
.editor-area a {
color: #409EFF;
text-decoration: underline;
}
.editor-area ul,
.editor-area ol {
margin: 16px 0;
padding-left: 32px;
}
.editor-area li {
margin: 8px 0;
}
</style>
</head>
<body>
<div id="app">
<div class="step-editor-container">
<div class="editor-header">
<div class="header-left">
<el-button type="text" icon="el-icon-back" @click="goBack" class="back-btn">
关闭
</el-button>
<span class="step-title">{{ stepTitle }}</span>
</div>
<div class="header-right">
<el-button type="primary" @click="saveContent" class="save-btn">
<i class="el-icon-check"></i>
保存
</el-button>
</div>
</div>
<div class="editor-toolbar">
<div class="toolbar-group">
<el-tooltip content="撤销" placement="top">
<el-button type="text" @click="formatText('undo')" class="toolbar-btn">
<i class="el-icon-refresh-left"></i>
</el-button>
</el-tooltip>
<el-tooltip content="重做" placement="top">
<el-button type="text" @click="formatText('redo')" class="toolbar-btn">
<i class="el-icon-refresh-right"></i>
</el-button>
</el-tooltip>
</div>
<div class="toolbar-divider"></div>
<div class="toolbar-group">
<el-tooltip content="导入文档" placement="top">
<el-button type="text" @click="importDocument" class="toolbar-btn">
<i class="el-icon-document"></i>
</el-button>
</el-tooltip>
<el-tooltip content="导入图片" placement="top">
<el-button type="text" @click="importImage" class="toolbar-btn">
<i class="el-icon-picture"></i>
</el-button>
</el-tooltip>
<el-tooltip content="插入链接" placement="top">
<el-button type="text" @click="insertLink" class="toolbar-btn">
<i class="el-icon-link"></i>
</el-button>
</el-tooltip>
</div>
<div class="toolbar-divider"></div>
<div class="toolbar-group">
<el-tooltip content="加粗" placement="top">
<el-button type="text" @click="formatText('bold')" class="toolbar-btn">
<span class="format-icon">B</span>
</el-button>
</el-tooltip>
<el-tooltip content="斜体" placement="top">
<el-button type="text" @click="formatText('italic')" class="toolbar-btn">
<span class="format-icon italic">I</span>
</el-button>
</el-tooltip>
<el-tooltip content="下划线" placement="top">
<el-button type="text" @click="formatText('underline')" class="toolbar-btn">
<span class="format-icon underline">U</span>
</el-button>
</el-tooltip>
<el-tooltip content="删除线" placement="top">
<el-button type="text" @click="formatText('strikeThrough')" class="toolbar-btn">
<span class="format-icon strike">S</span>
</el-button>
</el-tooltip>
</div>
<div class="toolbar-divider"></div>
<div class="toolbar-group">
<el-tooltip content="上标" placement="top">
<el-button type="text" @click="formatText('superscript')" class="toolbar-btn">
<span class="format-icon"></span>
</el-button>
</el-tooltip>
<el-tooltip content="下标" placement="top">
<el-button type="text" @click="formatText('subscript')" class="toolbar-btn">
<span class="format-icon">x₂</span>
</el-button>
</el-tooltip>
</div>
<div class="toolbar-divider"></div>
<div class="toolbar-group">
<el-tooltip content="左对齐" placement="top">
<el-button type="text" @click="formatText('justifyLeft')" class="toolbar-btn">
<i class="el-icon-s-unfold"></i>
</el-button>
</el-tooltip>
<el-tooltip content="居中对齐" placement="top">
<el-button type="text" @click="formatText('justifyCenter')" class="toolbar-btn">
<i class="el-icon-s-grid"></i>
</el-button>
</el-tooltip>
<el-tooltip content="右对齐" placement="top">
<el-button type="text" @click="formatText('justifyRight')" class="toolbar-btn">
<i class="el-icon-s-fold"></i>
</el-button>
</el-tooltip>
<el-tooltip content="两端对齐" placement="top">
<el-button type="text" @click="formatText('justifyFull')" class="toolbar-btn">
<i class="el-icon-menu"></i>
</el-button>
</el-tooltip>
</div>
<div class="toolbar-divider"></div>
<div class="toolbar-group">
<el-tooltip content="无序列表" placement="top">
<el-button type="text" @click="formatText('insertUnorderedList')" class="toolbar-btn">
<i class="el-icon-plus"></i>
</el-button>
</el-tooltip>
<el-tooltip content="有序列表" placement="top">
<el-button type="text" @click="formatText('insertOrderedList')" class="toolbar-btn">
<i class="el-icon-sort"></i>
</el-button>
</el-tooltip>
<el-tooltip content="减少缩进" placement="top">
<el-button type="text" @click="formatText('outdent')" class="toolbar-btn">
<i class="el-icon-back"></i>
</el-button>
</el-tooltip>
<el-tooltip content="增加缩进" placement="top">
<el-button type="text" @click="formatText('indent')" class="toolbar-btn">
<i class="el-icon-right"></i>
</el-button>
</el-tooltip>
</div>
<div class="toolbar-divider"></div>
<div class="toolbar-group">
<el-tooltip content="全屏" placement="top">
<el-button type="text" @click="toggleFullscreen" class="toolbar-btn">
<i :class="isFullscreen ? 'el-icon-crop' : 'el-icon-full-screen'"></i>
</el-button>
</el-tooltip>
<div class="word-count">
{{ wordCount }} 字
</div>
</div>
</div>
<div class="editor-content">
<div
ref="editor"
class="editor-area"
contenteditable="true"
:data-placeholder="placeholder"
@input="onInput"
@keydown="onKeydown"
@focus="onFocus"
@blur="onBlur"
>
</div>
</div>
<input
ref="fileInput"
type="file"
style="display: none"
@change="handleFileChange"
/>
</div>
</div>
<script src="https://unpkg.com/vue@2.6.12/dist/vue.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="https://unpkg.com/mammoth@1.6.0/mammoth.browser.min.js"></script>
<script>
new Vue({
el: '#app',
data() {
return {
stepTitle: '',
stepIndex: 0,
content: '',
placeholder: '在此输入内容...',
uploadType: '',
savedContent: {},
wordCount: 0,
isFullscreen: false
}
},
created() {
const urlParams = new URLSearchParams(window.location.search)
this.stepTitle = urlParams.get('title') || '编辑器'
this.stepIndex = parseInt(urlParams.get('index')) || 0
this.loadSavedContent()
},
methods: {
goBack() {
window.close()
},
formatText(command) {
document.execCommand(command, false, null)
this.$refs.editor.focus()
},
importDocument() {
this.uploadType = 'document'
this.$refs.fileInput.accept = '.txt,.md,.docx'
this.$refs.fileInput.click()
},
importImage() {
this.uploadType = 'image'
this.$refs.fileInput.accept = 'image/*'
this.$refs.fileInput.click()
},
handleFileChange(event) {
const file = event.target.files[0]
if (!file) return
if (this.uploadType === 'document') {
this.handleDocumentUpload(file)
} else if (this.uploadType === 'image') {
this.handleImageUpload(file)
}
event.target.value = ''
},
handleDocumentUpload(file) {
const fileName = file.name.toLowerCase()
const fileExt = fileName.substring(fileName.lastIndexOf('.'))
if (fileExt === '.docx') {
this.handleDocxUpload(file)
} else if (fileExt === '.txt' || fileExt === '.md') {
this.handleTextFileUpload(file)
} else {
this.$message.warning('不支持的文件格式,请使用 .txt、.md 或 .docx 文件')
}
},
handleDocxUpload(file) {
this.$message.warning('正在解析 Word 文档...')
const reader = new FileReader()
reader.onload = (e) => {
try {
const arrayBuffer = e.target.result
this.parseDocx(arrayBuffer)
} catch (error) {
console.error('解析 Word 文档失败:', error)
this.$message.error('解析 Word 文档失败,请将文档另存为 .txt 或 .md 格式后重试')
}
}
reader.readAsArrayBuffer(file)
},
async parseDocx(arrayBuffer) {
try {
const result = await mammoth.extractRawText({ arrayBuffer: arrayBuffer })
const text = result.value
this.insertText(text)
this.$message.success('Word 文档导入成功')
} catch (error) {
console.error('mammoth.js 解析失败:', error)
this.$message.error('解析 Word 文档失败')
}
},
handleTextFileUpload(file) {
const reader = new FileReader()
reader.onload = (e) => {
let text = e.target.result
if (!text || text.trim() === '') {
this.$message.warning('文档内容为空')
return
}
if (this.isGarbled(text)) {
this.tryGBKEncoding(file)
} else {
this.insertText(text)
this.$message.success('文档导入成功')
}
}
reader.readAsText(file, 'UTF-8')
},
isGarbled(text) {
const garbageChars = text.match(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g)
return garbageChars && garbageChars.length > text.length * 0.3
},
tryGBKEncoding(file) {
const reader = new FileReader()
reader.onload = (e) => {
const text = e.target.result
if (this.isGarbled(text)) {
this.$message.error('文档编码无法识别,请确保文档使用 UTF-8 或 GBK 编码')
} else {
this.insertText(text)
this.$message.success('文档导入成功')
}
}
reader.readAsText(file, 'GBK')
},
handleImageUpload(file) {
const reader = new FileReader()
reader.onload = (e) => {
const img = `<img src="${e.target.result}" style="max-width: 100%; height: auto; margin: 10px 0;" />`
this.insertHTML(img)
this.$message.success('图片导入成功')
}
reader.readAsDataURL(file)
},
insertLink() {
this.$prompt('请输入链接地址', '插入链接', {
confirmButtonText: '确定',
cancelButtonText: '取消'
}).then(({ value }) => {
if (value) {
const link = `<a href="${value}" target="_blank" style="color: #409EFF; text-decoration: underline;">${value}</a>`
this.insertHTML(link)
}
})
},
insertText(text) {
const editor = this.$refs.editor
const selection = window.getSelection()
if (selection.rangeCount > 0) {
const range = selection.getRangeAt(0)
const textNode = document.createTextNode(text)
range.insertNode(textNode)
} else {
if (editor.innerHTML.trim() === '') {
editor.innerHTML = `<p>${text}</p>`
} else {
editor.innerHTML += `<p>${text}</p>`
}
}
this.$nextTick(() => {
this.updateWordCount()
})
},
insertHTML(html) {
const editor = this.$refs.editor
const selection = window.getSelection()
if (selection.rangeCount > 0) {
const range = selection.getRangeAt(0)
const div = document.createElement('div')
div.innerHTML = html
range.insertNode(div)
} else {
if (editor.innerHTML.trim() === '') {
editor.innerHTML = `<p>${html}</p>`
} else {
editor.innerHTML += `<p>${html}</p>`
}
}
this.$nextTick(() => {
this.updateWordCount()
})
},
onInput() {
this.content = this.$refs.editor.innerHTML
this.updateWordCount()
},
updateWordCount() {
const text = this.$refs.editor.innerText || ''
this.wordCount = text.replace(/\s+/g, '').length
},
toggleFullscreen() {
this.isFullscreen = !this.isFullscreen
const container = document.querySelector('.step-editor-container')
if (this.isFullscreen) {
if (container.requestFullscreen) {
container.requestFullscreen()
} else if (container.webkitRequestFullscreen) {
container.webkitRequestFullscreen()
} else if (container.msRequestFullscreen) {
container.msRequestFullscreen()
}
} else {
if (document.exitFullscreen) {
document.exitFullscreen()
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen()
} else if (document.msExitFullscreen) {
document.msExitFullscreen()
}
}
},
onFocus() {
const editor = this.$refs.editor
if (editor.innerText.trim() === '' && editor.innerHTML.trim() === '') {
editor.innerHTML = ''
}
},
onBlur() {
const editor = this.$refs.editor
if (editor.innerHTML.trim() === '' || editor.innerText.trim() === '') {
editor.innerHTML = ''
}
},
onKeydown(event) {
if (event.ctrlKey || event.metaKey) {
if (event.key === 's') {
event.preventDefault()
this.saveContent()
}
}
},
saveContent() {
this.content = this.$refs.editor.innerHTML
const savedData = JSON.parse(localStorage.getItem('stepEditorContent') || '{}')
savedData[`step_${this.stepIndex}`] = {
content: this.content,
updateTime: new Date().toISOString()
}
localStorage.setItem('stepEditorContent', JSON.stringify(savedData))
this.$message.success('保存成功')
},
loadSavedContent() {
const savedData = JSON.parse(localStorage.getItem('stepEditorContent') || '{}')
const stepData = savedData[`step_${this.stepIndex}`]
if (stepData && stepData.content) {
this.$nextTick(() => {
this.$refs.editor.innerHTML = stepData.content
this.content = stepData.content
this.updateWordCount()
})
}
}
}
})
</script>
</body>
</html>