| | |
| | | {{ label }} <input v-model.number="margins[side]" type="number" min="0" max="80" step="0.1" |
| | | :aria-label="`${label}页边距(毫米)`" :disabled="readonly || !ready || applyingMargins" /> |
| | | </label> |
| | | <el-button size="small" :loading="applyingMargins" :disabled="readonly || !ready" @click="applyMargins">应用页边距</el-button> |
| | | <el-button size="small" :loading="applyingMargins" :disabled="readonly || !ready || applyingScript" @click="applyMargins">应用页边距</el-button> |
| | | <div class="script-settings" role="group" aria-label="上下标设置"> |
| | | <span>选中文字后设置:</span> |
| | | <el-button v-for="option in scriptOptions" :key="option.value" size="small" |
| | | :disabled="readonly || !ready || applyingMargins || applyingScript" |
| | | :aria-label="option.label" :title="option.tip" |
| | | @pointerdown.prevent="rememberScriptSelection" @mousedown.prevent |
| | | @click="applyScript(option.value)">{{ option.label }}</el-button> |
| | | </div> |
| | | </div> |
| | | <div ref="toolbar" class="word-toolbar" :inert="readonly || undefined"></div> |
| | | <div class="word-canvas" :inert="readonly || undefined"> |
| | |
| | | const editor = shallowRef(null) |
| | | const ready = ref(false) |
| | | const applyingMargins = ref(false) |
| | | const applyingScript = ref(false) |
| | | const scriptOptions = [ |
| | | { value: 'superscript', label: '上标 x²', tip: '将选中文字设为上标,例如 10⁻³ 中的 -3' }, |
| | | { value: 'subscript', label: '下标 x₂', tip: '将选中文字设为下标,例如 H₂O 中的 2' }, |
| | | { value: 'baseline', label: '恢复正常', tip: '取消选中文字的上标或下标' } |
| | | ] |
| | | const error = ref('') |
| | | const margins = ref({ top: 25.4, bottom: 25.4, left: 25.4, right: 25.4 }) |
| | | const marginLabels = { top: '上', bottom: '下', left: '左', right: '右' } |
| | |
| | | let toolbarObserver |
| | | let fontSelectionTarget = false |
| | | let fontSelectionKnown = false |
| | | let scriptSelection = null |
| | | |
| | | function rememberScriptSelection() { |
| | | // 在按钮禁用或焦点变化之前向 Worker 读取选区,避免点击后选区被清空。 |
| | | scriptSelection = editor.value?.activeEditor.doc.selection.current().catch(() => null) |
| | | } |
| | | |
| | | function rememberFontSelection(event) { |
| | | if (!event.target?.closest?.('[data-item="btn-fontFamily"]')) return |
| | |
| | | |
| | | async function openDocument() { |
| | | const current = ++generation |
| | | scriptSelection = null |
| | | clearTimeout(readyTimer) |
| | | editor.value?.destroy() |
| | | editor.value = null |
| | |
| | | } |
| | | |
| | | async function applyMargins() { |
| | | if (!ready.value || props.readonly || applyingMargins.value) return |
| | | if (!ready.value || props.readonly || applyingMargins.value || applyingScript.value) return |
| | | applyingMargins.value = true |
| | | try { |
| | | const sections = editor.value.activeEditor.doc.sections |
| | |
| | | finally { applyingMargins.value = false } |
| | | } |
| | | |
| | | async function applyScript(vertAlign) { |
| | | if (!ready.value || props.readonly || applyingMargins.value || applyingScript.value) return |
| | | const current = generation |
| | | const superdoc = editor.value |
| | | const selectionPromise = scriptSelection || superdoc.activeEditor.doc.selection.current() |
| | | scriptSelection = null |
| | | applyingScript.value = true |
| | | try { |
| | | // 读取实际选区,避免 UI 快照尚未更新时修改到之前选中的文字。 |
| | | const selection = await selectionPromise |
| | | if (current !== generation || props.readonly) return |
| | | if (!selection || selection.empty || !selection.selectionTarget) { |
| | | ElMessage.info('请先选中需要设置上下标的文字,例如 10-3 中的 -3') |
| | | return |
| | | } |
| | | const receipt = await superdoc.activeEditor.doc.format.apply({ |
| | | target: selection.selectionTarget, |
| | | // 清除原有手动升降位置,让 Word 按上下标规则排版。 |
| | | inline: { vertAlign, position: null } |
| | | }) |
| | | if (current !== generation) return |
| | | if (!receipt.success && receipt.failure?.code !== 'NO_OP') { |
| | | throw new Error(receipt.failure?.message || '设置上下标失败') |
| | | } |
| | | emit('change') |
| | | ElMessage.success('文字格式已设置,点击保存后生效') |
| | | } catch (cause) { |
| | | if (current === generation) ElMessage.error(cause.message || '设置上下标失败') |
| | | } finally { applyingScript.value = false } |
| | | } |
| | | |
| | | async function exportDocx() { |
| | | if (!ready.value || error.value || !editor.value) throw new Error('Word 尚未加载完成,不能保存') |
| | | if (applyingMargins.value) throw new Error('页边距正在应用,请稍候再保存') |
| | | if (applyingScript.value) throw new Error('上下标正在设置,请稍候再保存') |
| | | const blob = await editor.value.export({ exportType: ['docx'], triggerDownload: false }) |
| | | if (!(blob instanceof Blob) || !blob.size) throw new Error('Word 导出失败') |
| | | return blob |
| | |
| | | .page-settings { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; padding: 8px; background: #fff; } |
| | | .page-settings label { display: flex; align-items: center; gap: 4px; } |
| | | .page-settings input { width: 70px; padding: 5px; border: 1px solid #dcdfe6; border-radius: 4px; } |
| | | .script-settings { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; } |
| | | .script-settings :deep(.el-button + .el-button) { margin-left: 0; } |
| | | .word-toolbar { flex-shrink: 0; } |
| | | .word-canvas { flex: 1; min-height: 0; overflow: auto; background: #f3f4f6; } |
| | | /* SuperDoc 滚动时会把文档容器缩到纸张宽度,导致整页重新贴到左边。 */ |