2026-04-04 20:05:40 +08:00
|
|
|
<template>
|
2026-04-04 23:56:18 +08:00
|
|
|
<div class="doc-block-crepe" :class="{ collapsed: collapsedState }">
|
2026-04-04 20:05:40 +08:00
|
|
|
<div class="doc-header">
|
2026-04-04 23:56:18 +08:00
|
|
|
<div class="doc-accent"></div>
|
2026-04-04 20:05:40 +08:00
|
|
|
<div class="doc-icon">
|
|
|
|
|
<svg v-if="docType === 'pdf'" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
|
|
|
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/>
|
|
|
|
|
<polyline points="14 2 14 8 20 8"/>
|
2026-04-04 23:56:18 +08:00
|
|
|
<path d="M8 13h5"/>
|
|
|
|
|
<path d="M8 17h8"/>
|
2026-04-04 20:05:40 +08:00
|
|
|
</svg>
|
2026-04-04 23:56:18 +08:00
|
|
|
<svg v-else-if="docType === 'docx'" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
2026-04-04 20:05:40 +08:00
|
|
|
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/>
|
|
|
|
|
<polyline points="14 2 14 8 20 8"/>
|
2026-04-04 23:56:18 +08:00
|
|
|
<path d="m8 13 2 4 2-4 2 4 2-4"/>
|
2026-04-04 20:05:40 +08:00
|
|
|
</svg>
|
2026-04-04 23:56:18 +08:00
|
|
|
<svg v-else-if="docType === 'pptx'" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
|
|
|
<rect x="3" y="4" width="18" height="12" rx="2"/>
|
|
|
|
|
<path d="M8 20h8"/>
|
|
|
|
|
<path d="M12 16v4"/>
|
|
|
|
|
<path d="M9 8h3a2 2 0 0 1 0 4H9z"/>
|
2026-04-04 20:05:40 +08:00
|
|
|
</svg>
|
|
|
|
|
<svg v-else width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
|
|
|
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/>
|
|
|
|
|
<polyline points="14 2 14 8 20 8"/>
|
2026-04-04 23:56:18 +08:00
|
|
|
<path d="M8 13h8"/>
|
|
|
|
|
<path d="M8 17h5"/>
|
2026-04-04 20:05:40 +08:00
|
|
|
</svg>
|
|
|
|
|
</div>
|
2026-04-04 23:56:18 +08:00
|
|
|
<div class="doc-meta">
|
|
|
|
|
<div class="doc-name">{{ docName }}</div>
|
|
|
|
|
<div class="doc-subline">{{ typeLabel }} · {{ displayTime }}</div>
|
|
|
|
|
</div>
|
2026-04-04 20:05:40 +08:00
|
|
|
<div class="doc-actions">
|
2026-04-04 23:56:18 +08:00
|
|
|
<button type="button" class="action-btn" :title="collapsedState ? '展开文件' : '折叠文件'" @click="toggleCollapse">
|
|
|
|
|
<svg v-if="collapsedState" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
2026-04-04 20:05:40 +08:00
|
|
|
<polyline points="9 18 15 12 9 6"/>
|
|
|
|
|
</svg>
|
|
|
|
|
<svg v-else width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
|
|
|
<polyline points="6 9 12 15 18 9"/>
|
|
|
|
|
</svg>
|
|
|
|
|
</button>
|
2026-04-04 23:56:18 +08:00
|
|
|
<button type="button" class="action-btn action-btn-danger" title="删除文件" @click="props.onDelete?.()">
|
|
|
|
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
|
|
|
<path d="M3 6h18"/>
|
|
|
|
|
<path d="M8 6V4h8v2"/>
|
|
|
|
|
<path d="M19 6l-1 14H6L5 6"/>
|
|
|
|
|
<path d="M10 11v6"/>
|
|
|
|
|
<path d="M14 11v6"/>
|
|
|
|
|
</svg>
|
|
|
|
|
</button>
|
2026-04-04 20:05:40 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-04-04 23:56:18 +08:00
|
|
|
<div v-show="!collapsedState" class="doc-editor">
|
2026-04-04 20:05:40 +08:00
|
|
|
<div ref="editorRoot" class="inner-crepe"></div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup>
|
2026-04-04 23:56:18 +08:00
|
|
|
import { computed, onMounted, onUnmounted, ref, watch } from 'vue'
|
|
|
|
|
import { replaceAll } from '@milkdown/kit/utils'
|
2026-04-04 20:05:40 +08:00
|
|
|
import { Crepe } from '@milkdown/crepe'
|
2026-04-04 23:56:18 +08:00
|
|
|
import { editorViewCtx } from '@milkdown/kit/core'
|
|
|
|
|
import { copilotPlugin, copilotConfigCtx, copilotGhostMark, setCopilotEnabled, clearGhostSuggestion } from '../plugins/copilotPlugin'
|
2026-04-04 20:05:40 +08:00
|
|
|
import { fetchSuggestion } from '../utils/api.js'
|
|
|
|
|
|
|
|
|
|
const props = defineProps({
|
2026-04-04 23:56:18 +08:00
|
|
|
docType: { type: String, default: 'txt' },
|
2026-04-04 20:05:40 +08:00
|
|
|
docName: { type: String, default: 'document.txt' },
|
|
|
|
|
uploadTime: { type: String, default: '' },
|
2026-04-04 23:56:18 +08:00
|
|
|
content: { type: String, default: '' },
|
|
|
|
|
collapsed: { type: Boolean, default: false },
|
|
|
|
|
resolveSuggestionRequest: { type: Function, default: null },
|
|
|
|
|
onUpdateContent: { type: Function, default: null },
|
|
|
|
|
onUpdateCollapsed: { type: Function, default: null },
|
|
|
|
|
onDelete: { type: Function, default: null },
|
2026-04-04 20:05:40 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const editorRoot = ref(null)
|
2026-04-04 23:56:18 +08:00
|
|
|
const collapsedState = ref(Boolean(props.collapsed))
|
|
|
|
|
const currentContent = ref(props.content || '')
|
2026-04-04 20:05:40 +08:00
|
|
|
let crepe = null
|
2026-04-04 23:56:18 +08:00
|
|
|
let syncTimer = null
|
|
|
|
|
let applyingExternalContent = false
|
2026-04-04 20:05:40 +08:00
|
|
|
|
2026-04-04 23:56:18 +08:00
|
|
|
const displayTime = computed(() => {
|
|
|
|
|
if (!props.uploadTime) return '刚上传'
|
|
|
|
|
const date = new Date(props.uploadTime)
|
|
|
|
|
if (Number.isNaN(date.getTime())) return '刚上传'
|
|
|
|
|
return date.toLocaleString('zh-CN', { hour12: false })
|
|
|
|
|
})
|
2026-04-04 20:05:40 +08:00
|
|
|
|
2026-04-04 23:56:18 +08:00
|
|
|
const typeLabel = computed(() => {
|
|
|
|
|
if (props.docType === 'docx') return 'DOCX'
|
|
|
|
|
if (props.docType === 'pptx') return 'PPTX'
|
|
|
|
|
if (props.docType === 'pdf') return 'PDF'
|
|
|
|
|
return 'TXT'
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const toggleCollapse = () => {
|
|
|
|
|
collapsedState.value = !collapsedState.value
|
|
|
|
|
props.onUpdateCollapsed?.(collapsedState.value)
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const syncContent = () => {
|
|
|
|
|
if (!crepe) return
|
2026-04-04 23:56:18 +08:00
|
|
|
if (syncTimer) clearTimeout(syncTimer)
|
|
|
|
|
syncTimer = setTimeout(async () => {
|
|
|
|
|
if (!crepe || applyingExternalContent) return
|
2026-04-04 20:05:40 +08:00
|
|
|
const markdown = await crepe.getMarkdown()
|
2026-04-04 23:56:18 +08:00
|
|
|
currentContent.value = markdown
|
|
|
|
|
props.onUpdateContent?.(markdown)
|
2026-04-04 20:05:40 +08:00
|
|
|
}, 120)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 23:56:18 +08:00
|
|
|
const syncExternalContent = async (nextValue) => {
|
|
|
|
|
if (!crepe) {
|
|
|
|
|
currentContent.value = nextValue || ''
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if ((nextValue || '') === currentContent.value) return
|
|
|
|
|
applyingExternalContent = true
|
|
|
|
|
try {
|
|
|
|
|
crepe.editor.action(replaceAll(nextValue || ''))
|
|
|
|
|
currentContent.value = nextValue || ''
|
|
|
|
|
} finally {
|
|
|
|
|
applyingExternalContent = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
watch(() => props.content, (nextValue) => {
|
|
|
|
|
void syncExternalContent(nextValue || '')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
watch(() => props.collapsed, (nextValue) => {
|
|
|
|
|
collapsedState.value = Boolean(nextValue)
|
|
|
|
|
})
|
|
|
|
|
|
2026-04-04 20:05:40 +08:00
|
|
|
onMounted(async () => {
|
|
|
|
|
if (!editorRoot.value) return
|
|
|
|
|
crepe = new Crepe({
|
|
|
|
|
root: editorRoot.value,
|
2026-04-04 23:56:18 +08:00
|
|
|
defaultValue: props.content || '',
|
2026-04-04 20:05:40 +08:00
|
|
|
features: {
|
|
|
|
|
[Crepe.Feature.Latex]: true,
|
|
|
|
|
[Crepe.Feature.ImageBlock]: true,
|
|
|
|
|
[Crepe.Feature.Table]: true,
|
|
|
|
|
[Crepe.Feature.ListCheck]: true,
|
|
|
|
|
},
|
2026-04-04 23:56:18 +08:00
|
|
|
config: {
|
|
|
|
|
showLineNumber: false,
|
|
|
|
|
},
|
2026-04-04 20:05:40 +08:00
|
|
|
})
|
2026-04-04 23:56:18 +08:00
|
|
|
|
|
|
|
|
crepe.editor.config((ctx) => {
|
2026-04-04 20:05:40 +08:00
|
|
|
ctx.set(copilotConfigCtx.key, {
|
2026-04-04 23:56:18 +08:00
|
|
|
fetchSuggestion: async (prefix, suffix, languageId, signal) => {
|
|
|
|
|
const payload = props.resolveSuggestionRequest
|
|
|
|
|
? await props.resolveSuggestionRequest({ prefix, suffix, languageId })
|
|
|
|
|
: { prefix, suffix, languageId, blocked: false }
|
|
|
|
|
if (payload?.blocked) return ''
|
|
|
|
|
return fetchSuggestion(payload?.prefix ?? prefix, payload?.suffix ?? suffix, payload?.languageId ?? languageId, signal)
|
|
|
|
|
},
|
|
|
|
|
debounceMs: 900,
|
2026-04-04 20:05:40 +08:00
|
|
|
})
|
|
|
|
|
})
|
2026-04-04 23:56:18 +08:00
|
|
|
|
|
|
|
|
crepe.editor.use(copilotConfigCtx)
|
|
|
|
|
crepe.editor.use(copilotGhostMark)
|
2026-04-04 20:05:40 +08:00
|
|
|
crepe.editor.use(copilotPlugin)
|
2026-04-04 23:56:18 +08:00
|
|
|
|
2026-04-04 20:05:40 +08:00
|
|
|
await crepe.create()
|
2026-04-04 23:56:18 +08:00
|
|
|
|
|
|
|
|
crepe.on((listener) => {
|
2026-04-04 20:05:40 +08:00
|
|
|
listener.updated(() => {
|
|
|
|
|
syncContent()
|
|
|
|
|
})
|
|
|
|
|
})
|
2026-04-04 23:56:18 +08:00
|
|
|
|
|
|
|
|
crepe.editor.action((ctx) => {
|
2026-04-04 20:05:40 +08:00
|
|
|
const view = ctx.get(editorViewCtx)
|
|
|
|
|
setCopilotEnabled(view, true)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
2026-04-04 23:56:18 +08:00
|
|
|
if (syncTimer) {
|
|
|
|
|
clearTimeout(syncTimer)
|
|
|
|
|
syncTimer = null
|
|
|
|
|
}
|
2026-04-04 20:05:40 +08:00
|
|
|
if (crepe) {
|
2026-04-04 23:56:18 +08:00
|
|
|
crepe.editor.action((ctx) => {
|
|
|
|
|
const view = ctx.get(editorViewCtx)
|
|
|
|
|
clearGhostSuggestion(view)
|
|
|
|
|
})
|
2026-04-04 20:05:40 +08:00
|
|
|
crepe.destroy()
|
|
|
|
|
crepe = null
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
.doc-block-crepe {
|
2026-04-04 23:56:18 +08:00
|
|
|
position: relative;
|
|
|
|
|
margin: 14px 0;
|
|
|
|
|
border: 1px solid color-mix(in srgb, var(--panel-border) 72%, transparent);
|
|
|
|
|
border-radius: 18px;
|
2026-04-04 20:05:40 +08:00
|
|
|
overflow: hidden;
|
2026-04-04 23:56:18 +08:00
|
|
|
background: linear-gradient(180deg, color-mix(in srgb, var(--panel-bg) 82%, transparent) 0%, color-mix(in srgb, var(--crepe-color-surface-low) 88%, transparent) 100%);
|
|
|
|
|
box-shadow: 0 18px 38px rgba(15, 23, 42, 0.08), inset 0 1px 0 rgba(255, 255, 255, 0.08);
|
|
|
|
|
backdrop-filter: blur(14px);
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.doc-header {
|
2026-04-04 23:56:18 +08:00
|
|
|
display: grid;
|
|
|
|
|
grid-template-columns: 4px 24px minmax(0, 1fr) auto;
|
2026-04-04 20:05:40 +08:00
|
|
|
align-items: center;
|
2026-04-04 23:56:18 +08:00
|
|
|
gap: 12px;
|
|
|
|
|
padding: 12px 14px;
|
|
|
|
|
background: linear-gradient(135deg, color-mix(in srgb, var(--btn-bg) 76%, transparent) 0%, color-mix(in srgb, var(--crepe-color-surface) 78%, transparent) 100%);
|
|
|
|
|
border-bottom: 1px solid color-mix(in srgb, var(--panel-border) 76%, transparent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.doc-accent {
|
|
|
|
|
width: 4px;
|
|
|
|
|
height: 36px;
|
|
|
|
|
border-radius: 999px;
|
|
|
|
|
background: linear-gradient(180deg, #4f8cff 0%, #7dc1ff 100%);
|
|
|
|
|
box-shadow: 0 0 18px rgba(79, 140, 255, 0.32);
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.doc-icon {
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: center;
|
2026-04-04 23:56:18 +08:00
|
|
|
color: var(--btn-fg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.doc-meta {
|
|
|
|
|
min-width: 0;
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.doc-name {
|
|
|
|
|
font-size: 14px;
|
2026-04-04 23:56:18 +08:00
|
|
|
font-weight: 600;
|
|
|
|
|
color: var(--app-text);
|
|
|
|
|
white-space: nowrap;
|
2026-04-04 20:05:40 +08:00
|
|
|
overflow: hidden;
|
|
|
|
|
text-overflow: ellipsis;
|
2026-04-04 23:56:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.doc-subline {
|
|
|
|
|
margin-top: 2px;
|
|
|
|
|
font-size: 11px;
|
|
|
|
|
color: var(--muted-text);
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.doc-actions {
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
2026-04-04 23:56:18 +08:00
|
|
|
gap: 6px;
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.action-btn {
|
2026-04-04 23:56:18 +08:00
|
|
|
width: 30px;
|
|
|
|
|
height: 30px;
|
|
|
|
|
border: 1px solid color-mix(in srgb, var(--panel-border) 72%, transparent);
|
|
|
|
|
border-radius: 10px;
|
|
|
|
|
background: color-mix(in srgb, var(--btn-bg) 84%, transparent);
|
|
|
|
|
color: var(--btn-fg);
|
|
|
|
|
cursor: pointer;
|
2026-04-04 20:05:40 +08:00
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: center;
|
2026-04-04 23:56:18 +08:00
|
|
|
transition: transform 0.14s ease, background-color 0.14s ease, border-color 0.14s ease;
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.action-btn:hover {
|
2026-04-04 23:56:18 +08:00
|
|
|
transform: translateY(-1px);
|
|
|
|
|
background: var(--btn-hover-bg);
|
|
|
|
|
border-color: var(--btn-hover-bg);
|
|
|
|
|
color: var(--btn-hover-fg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.action-btn-danger:hover {
|
|
|
|
|
background: rgba(220, 38, 38, 0.12);
|
|
|
|
|
border-color: rgba(220, 38, 38, 0.22);
|
|
|
|
|
color: #dc2626;
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.doc-editor {
|
2026-04-04 23:56:18 +08:00
|
|
|
padding: 10px 12px 12px;
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.inner-crepe {
|
2026-04-04 23:56:18 +08:00
|
|
|
border-radius: 14px;
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
background: color-mix(in srgb, var(--crepe-color-background) 78%, transparent);
|
|
|
|
|
border: 1px solid color-mix(in srgb, var(--panel-border) 68%, transparent);
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.inner-crepe :deep(.milkdown) {
|
|
|
|
|
background: transparent !important;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 23:56:18 +08:00
|
|
|
.inner-crepe :deep(.milkdown__main),
|
|
|
|
|
.inner-crepe :deep(.milkdown__editor) {
|
|
|
|
|
margin: 0 !important;
|
|
|
|
|
padding: 0 !important;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 20:05:40 +08:00
|
|
|
.inner-crepe :deep(.ProseMirror) {
|
2026-04-04 23:56:18 +08:00
|
|
|
min-height: 92px;
|
|
|
|
|
padding: 10px 12px 14px !important;
|
|
|
|
|
font-size: 14px !important;
|
|
|
|
|
line-height: 1.7;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.inner-crepe :deep(.ProseMirror h1),
|
|
|
|
|
.inner-crepe :deep(.ProseMirror h2),
|
|
|
|
|
.inner-crepe :deep(.ProseMirror h3),
|
|
|
|
|
.inner-crepe :deep(.ProseMirror p),
|
|
|
|
|
.inner-crepe :deep(.ProseMirror li),
|
|
|
|
|
.inner-crepe :deep(.ProseMirror blockquote),
|
|
|
|
|
.inner-crepe :deep(.ProseMirror code) {
|
|
|
|
|
font-size: inherit;
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
</style>
|