♻️ Refactor the Markdown preview theme application logic
This commit is contained in:
@@ -35,17 +35,16 @@ onMounted(async () => {
|
||||
|
||||
// onBeforeUnmount(() => {
|
||||
// editorStore.clearAllEditors();
|
||||
//
|
||||
// });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="editor-container">
|
||||
<div ref="editorElement" class="editor"></div>
|
||||
<Toolbar/>
|
||||
<transition name="loading-fade">
|
||||
<LoadingScreen v-if="editorStore.isLoading && enableLoadingAnimation" text="VOIDRAFT"/>
|
||||
</transition>
|
||||
<div ref="editorElement" class="editor"></div>
|
||||
<Toolbar/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
* Markdown 预览扩展主入口
|
||||
*/
|
||||
import { EditorView } from "@codemirror/view";
|
||||
import { Compartment } from "@codemirror/state";
|
||||
import { useThemeStore } from "@/stores/themeStore";
|
||||
import { usePanelStore } from "@/stores/panelStore";
|
||||
import { useDocumentStore } from "@/stores/documentStore";
|
||||
@@ -52,11 +53,30 @@ export function toggleMarkdownPreview(view: EditorView): boolean {
|
||||
/**
|
||||
* 导出 Markdown 预览扩展
|
||||
*/
|
||||
export function markdownPreviewExtension() {
|
||||
const previewThemeCompartment = new Compartment();
|
||||
|
||||
const buildPreviewTheme = () => {
|
||||
const themeStore = useThemeStore();
|
||||
const colors = themeStore.currentColors;
|
||||
|
||||
const theme = colors ? createMarkdownPreviewTheme(colors) : EditorView.baseTheme({});
|
||||
|
||||
return [previewPanelState, previewPanelPlugin, theme];
|
||||
return colors ? createMarkdownPreviewTheme(colors) : EditorView.baseTheme({});
|
||||
};
|
||||
|
||||
export function markdownPreviewExtension() {
|
||||
return [
|
||||
previewPanelState,
|
||||
previewPanelPlugin,
|
||||
previewThemeCompartment.of(buildPreviewTheme())
|
||||
];
|
||||
}
|
||||
|
||||
export function updateMarkdownPreviewTheme(view: EditorView): void {
|
||||
if (!view?.dispatch) return;
|
||||
|
||||
try {
|
||||
view.dispatch({
|
||||
effects: previewThemeCompartment.reconfigure(buildPreviewTheme())
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Failed to update markdown preview theme", error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ export class MarkdownPreviewPanel {
|
||||
private readonly resizeHandle: HTMLDivElement;
|
||||
private readonly content: HTMLDivElement;
|
||||
private view: EditorView;
|
||||
private themeUnwatch?: () => void;
|
||||
private themeUnwatchers: Array<() => void> = [];
|
||||
private lastRenderedContent: string = "";
|
||||
private readonly debouncedUpdate: ReturnType<typeof createDebounce>;
|
||||
private isDestroyed: boolean = false; // 标记面板是否已销毁
|
||||
@@ -38,11 +38,22 @@ export class MarkdownPreviewPanel {
|
||||
|
||||
// 监听主题变化
|
||||
const themeStore = useThemeStore();
|
||||
this.themeUnwatch = watch(() => themeStore.isDarkMode, (isDark) => {
|
||||
const newTheme = isDark ? "dark" : "default";
|
||||
updateMermaidTheme(newTheme);
|
||||
this.lastRenderedContent = ""; // 清空缓存,强制重新渲染
|
||||
});
|
||||
this.themeUnwatchers.push(
|
||||
watch(() => themeStore.isDarkMode, (isDark) => {
|
||||
const newTheme = isDark ? "dark" : "default";
|
||||
updateMermaidTheme(newTheme);
|
||||
this.resetPreviewContent();
|
||||
})
|
||||
);
|
||||
this.themeUnwatchers.push(
|
||||
watch(
|
||||
() => themeStore.currentColors,
|
||||
() => {
|
||||
this.resetPreviewContent();
|
||||
},
|
||||
{ deep: true }
|
||||
)
|
||||
);
|
||||
|
||||
// 创建 DOM 结构
|
||||
this.dom = document.createElement("div");
|
||||
@@ -315,6 +326,16 @@ export class MarkdownPreviewPanel {
|
||||
});
|
||||
}
|
||||
|
||||
private resetPreviewContent(): void {
|
||||
if (this.isDestroyed) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.md = createMarkdownRenderer();
|
||||
this.lastRenderedContent = "";
|
||||
this.updateContentInternal();
|
||||
}
|
||||
|
||||
/**
|
||||
* 响应编辑器更新
|
||||
*/
|
||||
@@ -339,9 +360,14 @@ export class MarkdownPreviewPanel {
|
||||
if (this.debouncedUpdate) {
|
||||
this.debouncedUpdate.cancel();
|
||||
}
|
||||
|
||||
|
||||
// 清空缓存
|
||||
this.lastRenderedContent = "";
|
||||
|
||||
if (this.themeUnwatchers.length) {
|
||||
this.themeUnwatchers.forEach(unwatch => unwatch());
|
||||
this.themeUnwatchers = [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user