🎨 Updated

This commit is contained in:
2025-06-22 12:08:50 +08:00
parent 77287bccfa
commit 35c89e086e
7 changed files with 47 additions and 26 deletions

View File

@@ -9,7 +9,6 @@ import {createBasicSetup} from '@/views/editor/extensions/basicSetup';
import {
createStatsUpdateExtension,
getTabExtensions,
updateStats,
updateTabConfig,
createAutoSavePlugin,
createSaveShortcutPlugin,
@@ -22,7 +21,7 @@ import { useThemeStore } from './themeStore';
import { useI18n } from 'vue-i18n';
import { SystemThemeType } from '@/../bindings/voidraft/internal/models/models';
import { DocumentService } from '@/../bindings/voidraft/internal/services';
import {ensureSyntaxTree } from "@codemirror/language"
export interface DocumentStats {
lines: number;
characters: number;
@@ -199,15 +198,15 @@ export const useEditorStore = defineStore('editor', () => {
// 将编辑器实例保存到store
setEditorView(view);
isEditorInitialized.value = true;
// 确保编辑器已渲染后再滚动到底部
scrollToBottom(view);
ensureSyntaxTree(view.state, view.state.doc.length, 5000)
// 应用初始字体大小
applyFontSize();
// 立即更新统计信息
updateStats(view, updateDocumentStats);
};
// 重新配置编辑器

View File

@@ -1,6 +1,8 @@
import {Extension} from '@codemirror/state';
import {EditorView} from '@codemirror/view';
import {DocumentStats} from '@/stores/editorStore';
import {getActiveNoteBlock} from '@/views/editor/extensions/codeblock/state';
// 更新编辑器文档统计信息
export const updateStats = (
view: EditorView,
@@ -9,22 +11,44 @@ export const updateStats = (
if (!view) return;
const state = view.state;
const doc = state.doc;
const text = doc.toString();
// 计算选中的字符数
// 获取当前光标所在的代码块
const activeBlock = getActiveNoteBlock(state as any);
if (!activeBlock) {
// 如果没有活动块,显示空统计
updateDocumentStats({
lines: 0,
characters: 0,
selectedCharacters: 0
});
return;
}
// 获取当前块的内容范围
const blockContent = state.doc.sliceString(activeBlock.content.from, activeBlock.content.to);
// 计算块内容的行数
const blockLines = blockContent.split('\n').length;
// 计算选中的字符数(只统计在当前块内的选中内容)
let selectedChars = 0;
const selections = state.selection;
if (selections) {
for (let i = 0; i < selections.ranges.length; i++) {
const range = selections.ranges[i];
selectedChars += range.to - range.from;
// 计算选中范围与当前块内容范围的交集
const selectionStart = Math.max(range.from, activeBlock.content.from);
const selectionEnd = Math.min(range.to, activeBlock.content.to);
if (selectionStart < selectionEnd) {
selectedChars += selectionEnd - selectionStart;
}
}
}
updateDocumentStats({
lines: doc.lines,
characters: text.length,
lines: blockLines,
characters: blockContent.length,
selectedCharacters: selectedChars
});
};