🎨 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

@@ -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
});
};