2026-04-04 20:05:40 +08:00
|
|
|
<template>
|
2026-04-05 10:16:16 +08:00
|
|
|
<section class="doc-card" :class="{ 'is-collapsed': collapsedState }">
|
|
|
|
|
<header class="doc-card__header">
|
|
|
|
|
<div class="doc-card__badge">{{ typeLabel }}</div>
|
|
|
|
|
<div class="doc-card__meta">
|
|
|
|
|
<div class="doc-card__name">{{ docName }}</div>
|
|
|
|
|
<div class="doc-card__time">{{ displayTime }}</div>
|
2026-04-04 20:05:40 +08:00
|
|
|
</div>
|
2026-04-05 10:16:16 +08:00
|
|
|
<div class="doc-card__actions">
|
|
|
|
|
<button type="button" class="doc-card__btn" :title="collapsedState ? '展开文件' : '折叠文件'" @click="toggleCollapse">
|
2026-04-04 23:56:18 +08:00
|
|
|
<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-05 10:16:16 +08:00
|
|
|
<button type="button" class="doc-card__btn doc-card__btn--danger" title="删除文件" @click="props.onDelete?.()">
|
2026-04-04 23:56:18 +08:00
|
|
|
<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>
|
2026-04-05 10:16:16 +08:00
|
|
|
</header>
|
|
|
|
|
<div v-show="!collapsedState" class="doc-card__body">
|
|
|
|
|
<div ref="editorRoot" class="doc-card__editor"></div>
|
2026-04-04 20:05:40 +08:00
|
|
|
</div>
|
2026-04-05 10:16:16 +08:00
|
|
|
</section>
|
2026-04-04 20:05:40 +08:00
|
|
|
</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
|
2026-04-05 10:16:16 +08:00
|
|
|
let syncingExternal = 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'
|
|
|
|
|
})
|
|
|
|
|
|
2026-04-05 10:16:16 +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 23:56:18 +08:00
|
|
|
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 () => {
|
2026-04-05 10:16:16 +08:00
|
|
|
if (!crepe || syncingExternal) 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) => {
|
2026-04-05 10:16:16 +08:00
|
|
|
const value = nextValue || ''
|
2026-04-04 23:56:18 +08:00
|
|
|
if (!crepe) {
|
2026-04-05 10:16:16 +08:00
|
|
|
currentContent.value = value
|
2026-04-04 23:56:18 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-04-05 10:16:16 +08:00
|
|
|
if (value === currentContent.value) return
|
|
|
|
|
syncingExternal = true
|
2026-04-04 23:56:18 +08:00
|
|
|
try {
|
2026-04-05 10:16:16 +08:00
|
|
|
crepe.editor.action(replaceAll(value))
|
|
|
|
|
currentContent.value = value
|
2026-04-04 23:56:18 +08:00
|
|
|
} finally {
|
2026-04-05 10:16:16 +08:00
|
|
|
syncingExternal = false
|
2026-04-04 23:56:18 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
watch(() => props.content, (nextValue) => {
|
2026-04-05 10:16:16 +08:00
|
|
|
void syncExternalContent(nextValue)
|
2026-04-04 23:56:18 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
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>
|
2026-04-05 10:16:16 +08:00
|
|
|
.doc-card {
|
|
|
|
|
width: 100%;
|
|
|
|
|
max-width: 100%;
|
|
|
|
|
margin: 8px 0;
|
|
|
|
|
border-radius: 12px;
|
|
|
|
|
border: 1px solid rgba(59, 130, 246, 0.12);
|
2026-04-05 23:22:00 +08:00
|
|
|
background: rgba(255, 255, 255, 0.8);
|
2026-04-05 10:16:16 +08:00
|
|
|
box-shadow: 0 2px 8px rgba(59, 130, 246, 0.06), 0 1px 3px rgba(0, 0, 0, 0.04);
|
2026-04-04 20:05:40 +08:00
|
|
|
overflow: hidden;
|
2026-04-05 10:16:16 +08:00
|
|
|
backdrop-filter: blur(10px);
|
|
|
|
|
position: relative;
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 23:22:00 +08:00
|
|
|
:root[data-theme='dark'] .doc-card {
|
|
|
|
|
background: rgba(26, 30, 39, 0.8);
|
|
|
|
|
border-color: rgba(96, 165, 250, 0.15);
|
|
|
|
|
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3), 0 1px 3px rgba(0, 0, 0, 0.2);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 10:16:16 +08:00
|
|
|
.doc-card__header {
|
2026-04-04 23:56:18 +08:00
|
|
|
display: grid;
|
2026-04-05 10:16:16 +08:00
|
|
|
grid-template-columns: auto minmax(0, 1fr) auto;
|
|
|
|
|
gap: 10px;
|
2026-04-04 20:05:40 +08:00
|
|
|
align-items: center;
|
2026-04-05 10:16:16 +08:00
|
|
|
padding: 8px 12px;
|
|
|
|
|
border-bottom: 1px solid rgba(59, 130, 246, 0.1);
|
2026-04-05 23:22:00 +08:00
|
|
|
background: rgba(255, 255, 255, 0.8);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
:root[data-theme='dark'] .doc-card__header {
|
|
|
|
|
background: rgba(26, 30, 39, 0.8);
|
|
|
|
|
border-bottom-color: rgba(96, 165, 250, 0.15);
|
2026-04-04 23:56:18 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 10:16:16 +08:00
|
|
|
.doc-card__badge {
|
|
|
|
|
min-width: 48px;
|
|
|
|
|
padding: 4px 10px;
|
2026-04-04 23:56:18 +08:00
|
|
|
border-radius: 999px;
|
2026-04-05 10:16:16 +08:00
|
|
|
background: linear-gradient(135deg, #3b82f6 0%, #60a5fa 100%);
|
|
|
|
|
color: #fff;
|
|
|
|
|
font-size: 10px;
|
|
|
|
|
font-weight: 600;
|
|
|
|
|
letter-spacing: 0.08em;
|
|
|
|
|
text-align: center;
|
|
|
|
|
box-shadow: 0 2px 6px rgba(59, 130, 246, 0.2);
|
2026-04-04 23:56:18 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 10:16:16 +08:00
|
|
|
.doc-card__meta {
|
2026-04-04 23:56:18 +08:00
|
|
|
min-width: 0;
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 10:16:16 +08:00
|
|
|
.doc-card__name {
|
|
|
|
|
color: #1e293b;
|
|
|
|
|
font-size: 13px;
|
2026-04-04 23:56:18 +08:00
|
|
|
font-weight: 600;
|
|
|
|
|
white-space: nowrap;
|
2026-04-04 20:05:40 +08:00
|
|
|
overflow: hidden;
|
|
|
|
|
text-overflow: ellipsis;
|
2026-04-04 23:56:18 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 23:22:00 +08:00
|
|
|
:root[data-theme='dark'] .doc-card__name {
|
|
|
|
|
color: #e5e7eb;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 10:16:16 +08:00
|
|
|
.doc-card__time {
|
2026-04-04 23:56:18 +08:00
|
|
|
margin-top: 2px;
|
2026-04-05 10:16:16 +08:00
|
|
|
color: #64748b;
|
|
|
|
|
font-size: 10px;
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 23:22:00 +08:00
|
|
|
:root[data-theme='dark'] .doc-card__time {
|
|
|
|
|
color: #aeb6c5;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 10:16:16 +08:00
|
|
|
.doc-card__actions {
|
2026-04-04 20:05:40 +08:00
|
|
|
display: flex;
|
2026-04-05 10:16:16 +08:00
|
|
|
gap: 4px;
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 10:16:16 +08:00
|
|
|
.doc-card__btn {
|
|
|
|
|
width: 26px;
|
|
|
|
|
height: 26px;
|
|
|
|
|
border: 1px solid rgba(59, 130, 246, 0.12);
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
background: rgba(255, 255, 255, 0.5);
|
|
|
|
|
color: #64748b;
|
2026-04-04 20:05:40 +08:00
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: center;
|
2026-04-05 10:16:16 +08:00
|
|
|
cursor: pointer;
|
|
|
|
|
transition: all 0.15s ease;
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 23:22:00 +08:00
|
|
|
:root[data-theme='dark'] .doc-card__btn {
|
|
|
|
|
background: rgba(34, 40, 52, 0.5);
|
|
|
|
|
border-color: rgba(96, 165, 250, 0.15);
|
|
|
|
|
color: #aeb6c5;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 10:16:16 +08:00
|
|
|
.doc-card__btn:hover {
|
|
|
|
|
background: rgba(59, 130, 246, 0.1);
|
|
|
|
|
border-color: rgba(59, 130, 246, 0.25);
|
|
|
|
|
color: #3b82f6;
|
2026-04-04 23:56:18 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 10:16:16 +08:00
|
|
|
.doc-card__btn--danger:hover {
|
|
|
|
|
background: rgba(239, 68, 68, 0.1);
|
|
|
|
|
border-color: rgba(239, 68, 68, 0.2);
|
|
|
|
|
color: #ef4444;
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 10:16:16 +08:00
|
|
|
.doc-card__body {
|
|
|
|
|
padding: 8px 10px;
|
2026-04-05 23:22:00 +08:00
|
|
|
background: rgba(248, 250, 252, 0.8);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
:root[data-theme='dark'] .doc-card__body {
|
|
|
|
|
background: rgba(18, 22, 30, 0.8);
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 10:16:16 +08:00
|
|
|
.doc-card__editor {
|
|
|
|
|
min-height: 48px;
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
border: 1px solid rgba(59, 130, 246, 0.08);
|
2026-04-05 23:22:00 +08:00
|
|
|
background: rgba(255, 255, 255, 0.8);
|
2026-04-04 23:56:18 +08:00
|
|
|
overflow: hidden;
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 23:22:00 +08:00
|
|
|
:root[data-theme='dark'] .doc-card__editor {
|
|
|
|
|
background: rgba(26, 30, 39, 0.8);
|
|
|
|
|
border-color: rgba(96, 165, 250, 0.12);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 10:16:16 +08:00
|
|
|
.doc-card__editor :deep(.milkdown) {
|
2026-04-04 20:05:40 +08:00
|
|
|
background: transparent !important;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 10:16:16 +08:00
|
|
|
.doc-card__editor :deep(.milkdown__main),
|
|
|
|
|
.doc-card__editor :deep(.milkdown__editor) {
|
2026-04-04 23:56:18 +08:00
|
|
|
margin: 0 !important;
|
|
|
|
|
padding: 0 !important;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 10:16:16 +08:00
|
|
|
.doc-card__editor :deep(.ProseMirror) {
|
2026-04-05 13:42:29 +08:00
|
|
|
min-height: 0;
|
2026-04-05 10:16:16 +08:00
|
|
|
padding: 10px 12px 12px !important;
|
|
|
|
|
font-size: 13px !important;
|
|
|
|
|
line-height: 1.6;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.doc-card__editor :deep(.ProseMirror > *:last-child) {
|
|
|
|
|
margin-bottom: 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.doc-card__editor :deep(.ProseMirror p:first-child) {
|
|
|
|
|
margin-top: 0;
|
2026-04-04 23:56:18 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 10:16:16 +08:00
|
|
|
.doc-card__editor :deep(.milkdown__toolbar),
|
|
|
|
|
.doc-card__editor :deep(.milkdown__menu),
|
|
|
|
|
.doc-card__editor :deep(.milkdown__statusbar),
|
|
|
|
|
.doc-card__editor :deep(.milkdown-slate-toolbar),
|
|
|
|
|
.doc-card__editor :deep(.milkdown-bubble-menu) {
|
|
|
|
|
display: none !important;
|
2026-04-04 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
</style>
|
2026-04-05 10:16:16 +08:00
|
|
|
|