编辑调整、对比模块

This commit is contained in:
zhongluofeng committed 2026-10-16 13:18:32 +08:00
1 parent c9e61f38c5
commit 6cfa74c901
76 files changed
+9514 -626

No files matched your search

+74 -35
View File
@@ -36,15 +36,27 @@ import {
} from '../editor/core'
import { editorBackground } from '../editor/themes'
import { createLogger } from '@/lib/logger'
import type { LanguageLoader } from '../editor/languages'
import type { Extension } from '@codemirror/state'
const logger = createLogger('editor')
const props = defineProps<{
/** 应用当前是否深色(跟随应用主题时用于取深/浅变体) */
/**
* 编辑器**窗口**当前是否深色(由 EditorWindow 按「跟随主应用主题 / 配色方案」推导)。
* 用于:跟随应用时按它取深浅变体,配色方案为「跟随系统」时按它取 VS Code 深/浅。
*/
isDark: boolean
/** 是否启用 Live Preview */
livePreview: boolean
/**
* 绑定的标签 id(并排副面板用)。
*
* 缺省(主面板)= 绑定 store 的**扁平活动文档**;给出 id 时 = 绑定该标签**自己的缓冲**
* (读 `tab.content`、写 `tab.content` 并置脏、保存走 `saveTab`)。副面板永远绑定非活动
* 标签,因此不会与主面板争抢扁平 state。
*/
tabId?: string
}>()
const emit = defineEmits<{
@@ -57,6 +69,29 @@ const emit = defineEmits<{
const store = useEditorStore()
const hostRef = ref<HTMLDivElement | null>(null)
/** 是否为「绑定某个标签」的副面板(否则为主面板,读扁平 state)。 */
const isPane = computed(() => !!props.tabId)
/** 副面板绑定的标签(主面板为 null)。 */
const boundTab = computed(() =>
props.tabId ? store.tabs.find(t => t.id === props.tabId) ?? null : null
)
/** 需要读取的文档属性:主面板取扁平 state,副面板取标签缓冲。 */
const paneContent = computed(() => (isPane.value ? boundTab.value?.content ?? '' : store.content))
const paneLarge = computed(() =>
isPane.value ? boundTab.value?.fileDoc?.large ?? false : store.fileDoc?.large ?? false
)
const paneKind = computed(() => (isPane.value ? boundTab.value?.kind ?? 'none' : store.kind))
const paneRelPath = computed(() =>
isPane.value ? boundTab.value?.relPath ?? '' : store.currentRelPath
)
const paneRevision = computed(() =>
isPane.value ? boundTab.value?.docRevision ?? 0 : store.docRevision
)
const paneLanguage = computed<LanguageLoader>(() =>
isPane.value ? boundTab.value?.language ?? (() => Promise.resolve(null)) : store.language
)
/**
* 编辑器外壳底色:跟随当前**实际生效**的主题。
*
@@ -78,7 +113,8 @@ let languageSeq = 0
function currentConfig(): EditorConfig {
const s = store.settings
const large = store.fileDoc?.large ?? false
const large = paneLarge.value
const note = paneKind.value === 'note'
return {
livePreview: props.livePreview,
showLineNumbers: s.showLineNumbers,
@@ -95,11 +131,11 @@ function currentConfig(): EditorConfig {
language: () => null,
largeFile: large,
autocomplete: s.autocomplete,
wikilink: s.wikilink && store.kind === 'note',
wikilink: s.wikilink && note,
onWikilink: target => emit('wikilink', target),
// 图片预览只在库内笔记里有意义(库外文件没有库根上下文)
currentRelPath: store.kind === 'note' ? store.currentRelPath : '',
images: store.kind === 'note'
currentRelPath: note ? paneRelPath.value : '',
images: note
}
}
@@ -109,16 +145,21 @@ function scheduleAutosave(): void {
if (autosaveTimer) clearTimeout(autosaveTimer)
autosaveTimer = setTimeout(() => {
autosaveTimer = null
void store.saveCurrent().catch(() => undefined)
void persist().catch(() => undefined)
}, ms)
}
/** 落盘:主面板存活动文档,副面板存它绑定的标签。 */
function persist(): Promise<void> {
return isPane.value && props.tabId ? store.saveTab(props.tabId) : store.saveCurrent()
}
function flushAutosave(): void {
if (autosaveTimer) {
clearTimeout(autosaveTimer)
autosaveTimer = null
}
void store.saveCurrent().catch(() => undefined)
void persist().catch(() => undefined)
}
/** 加载并应用当前文档的语言扩展。 */
@@ -126,7 +167,7 @@ async function applyLanguage(seq: number): Promise<void> {
if (!view) return
let extension: Extension | null = null
try {
extension = await store.language()
extension = await paneLanguage.value()
} catch (e) {
logger.warn(`语言扩展加载失败:${String(e)}`)
}
@@ -140,17 +181,21 @@ onMounted(() => {
view = new EditorView({
parent: hostRef.value,
state: createEditorState({
doc: store.content,
doc: paneContent.value,
config: currentConfig(),
onDocChanged: doc => {
if (applyingDoc) return
store.setContent(doc)
if (isPane.value && props.tabId) store.setTabContent(props.tabId, doc)
else store.setContent(doc)
scheduleAutosave()
},
onSelectionChanged: (line, column) => emit('cursor-moved', line, column)
// 副面板的光标不该覆盖状态栏(状态栏跟随活动文档)
onSelectionChanged: (line, column) => {
if (!isPane.value) emit('cursor-moved', line, column)
}
})
})
appliedRevision = store.docRevision
appliedRevision = paneRevision.value
void applyLanguage(++languageSeq)
})
@@ -160,20 +205,20 @@ onMounted(() => {
* 用 `docRevision` 而不是路径:重载同一篇时路径没变,但内容必须被换掉。
*/
watch(
() => store.docRevision,
() => {
paneRevision,
revision => {
if (!view) return
if (autosaveTimer) {
clearTimeout(autosaveTimer)
autosaveTimer = null
}
if (appliedRevision === store.docRevision) return
appliedRevision = store.docRevision
if (appliedRevision === revision) return
appliedRevision = revision
// 目标光标行要在 replaceDoc **之前**取:替换会派发 selection 变化,
// updateListener 随即把 store.cursorLine 覆盖成 1,之后再读就只剩第 1 行了。
const restoreLine = store.cursorLine
const restoreLine = isPane.value ? 1 : store.cursorLine
applyingDoc = true
replaceDoc(view, store.content)
replaceDoc(view, paneContent.value)
// 标签切换 / 会话恢复:按 store.cursorLine 复位光标(默认第 1 行不动)
if (restoreLine > 1) {
const line = Math.min(restoreLine, view.state.doc.lines)
@@ -186,12 +231,9 @@ watch(
)
/** 语言变化(异步加载完成 / 切换到另一种语言)。 */
watch(
() => store.language,
() => {
void applyLanguage(++languageSeq)
}
)
watch(paneLanguage, () => {
void applyLanguage(++languageSeq)
})
/**
* 切换笔记:Live Preview 的图片预览按「相对当前笔记所在目录」解析目标,
@@ -200,21 +242,18 @@ watch(
* 只动 mode 与 wikilink 两个 compartment:语言由 `applyLanguage` 单独负责,
* 在这里一并 reconfigure 会把刚异步加载好的语言重置掉。
*/
watch(
() => store.currentRelPath,
() => {
if (!view) return
try {
reconfigureEditor(view, currentConfig())
} catch (e) {
logger.error(`切换笔记后重配置失败:${String(e)}`)
}
watch(paneRelPath, () => {
if (!view) return
try {
reconfigureEditor(view, currentConfig())
} catch (e) {
logger.error(`切换笔记后重配置失败:${String(e)}`)
}
)
})
/** 设置 / 深浅 / 视图模式 / 文档类型变化:热重配置,不重建编辑器。 */
watch(
() => [store.settings, props.isDark, props.livePreview, store.fileDoc?.large],
() => [store.settings, props.isDark, props.livePreview, paneLarge.value],
() => {
if (!view) return
try {