diff --git a/app/src/editor/index.ts b/app/src/editor/index.ts index f6fc3d17d..9088038f4 100644 --- a/app/src/editor/index.ts +++ b/app/src/editor/index.ts @@ -4,6 +4,7 @@ import {Model} from "../layout/Model"; import {disabledProtyle} from "../protyle/util/onGet"; import {setPadding} from "../protyle/ui/initUI"; import {getAllModels} from "../layout/getAll"; +import {countBlockWord} from "../layout/status"; export class Editor extends Model { public element: HTMLElement; @@ -60,6 +61,7 @@ export class Editor extends Model { } }); } + countBlockWord([], editor.protyle.block.rootID); }, }); // 需在 after 回调之前,否则不会聚焦 https://github.com/siyuan-note/siyuan/issues/5303 diff --git a/app/src/editor/util.ts b/app/src/editor/util.ts index 9fe17d9ef..adf9a851d 100644 --- a/app/src/editor/util.ts +++ b/app/src/editor/util.ts @@ -317,7 +317,7 @@ export const updatePanelByEditor = (protyle?: IProtyle, focus = true, pushBackSt if (focus) { if (protyle.toolbar.range) { focusByRange(protyle.toolbar.range); - countSelectWord(protyle.toolbar.range); + countSelectWord(protyle.toolbar.range, protyle.block.rootID); if (pushBackStack && protyle.preview.element.classList.contains("fn__none")) { pushBack(protyle, protyle.toolbar.range); } @@ -326,8 +326,7 @@ export const updatePanelByEditor = (protyle?: IProtyle, focus = true, pushBackSt if (pushBackStack && protyle.preview.element.classList.contains("fn__none")) { pushBack(protyle, undefined, protyle.wysiwyg.element.firstElementChild); } - // 用于清空状态栏字数统计 - countBlockWord([]); + countBlockWord([], protyle.block.rootID); } } if (window.siyuan.config.fileTree.alwaysSelectOpenedFile && protyle) { diff --git a/app/src/layout/Wnd.ts b/app/src/layout/Wnd.ts index a1bf969f1..ed1348c1a 100644 --- a/app/src/layout/Wnd.ts +++ b/app/src/layout/Wnd.ts @@ -23,7 +23,7 @@ import {showMessage} from "../dialog/message"; import {openFileById, updatePanelByEditor} from "../editor/util"; import {scrollCenter} from "../util/highlightById"; import {getAllModels} from "./getAll"; -import {countBlockWord} from "./status"; +import {clearCounter} from "./status"; import {saveScroll} from "../protyle/scroll/saveScroll"; import {Asset} from "../asset"; import {newFile} from "../util/newFile"; @@ -534,7 +534,6 @@ export class Wnd { } }); model.editor.destroy(); - countBlockWord([]); return; } if (model instanceof Search) { @@ -552,6 +551,7 @@ export class Wnd { } private removeTabAction = (id: string, closeAll = false, hasSaveScroll = true) => { + clearCounter(); this.children.find((item, index) => { if (item.id === id) { if (item.model instanceof Editor && hasSaveScroll) { diff --git a/app/src/layout/status.ts b/app/src/layout/status.ts index db3003249..cb2bc6ef7 100644 --- a/app/src/layout/status.ts +++ b/app/src/layout/status.ts @@ -129,7 +129,7 @@ export const initStatus = () => { /// #endif }; -export const countSelectWord = (range: Range) => { +export const countSelectWord = (range: Range, rootID?: string) => { /// #if !MOBILE if (document.getElementById("status").classList.contains("fn__none")) { return; @@ -139,13 +139,15 @@ export const countSelectWord = (range: Range) => { fetchPost("/api/block/getContentWordCount", {"content": range.toString()}, (response) => { renderStatusbarCounter(response.data); }); - } else { - document.querySelector("#status .status__counter").innerHTML = ""; + } else if (rootID) { + fetchPost("/api/block/getTreeStat", {id: rootID}, (response) => { + renderStatusbarCounter(response.data); + }); } /// #endif }; -export const countBlockWord = (ids: string[]) => { +export const countBlockWord = (ids: string[], rootID?: string) => { /// #if !MOBILE if (document.getElementById("status").classList.contains("fn__none")) { return; @@ -154,12 +156,18 @@ export const countBlockWord = (ids: string[]) => { fetchPost("/api/block/getBlocksWordCount", {ids}, (response) => { renderStatusbarCounter(response.data); }); - } else { - document.querySelector("#status .status__counter").innerHTML = ""; + } else if (rootID) { + fetchPost("/api/block/getTreeStat", {id: rootID}, (response) => { + renderStatusbarCounter(response.data); + }); } /// #endif }; +export const clearCounter = () => { + document.querySelector("#status .status__counter").innerHTML = ""; +} + export const renderStatusbarCounter = (stat: { runeCount: number, wordCount: number, linkCount: number, imageCount: number, refCount: number }) => { let html = `${window.siyuan.languages.runeCount} ${stat.runeCount} ${window.siyuan.languages.wordCount} ${stat.wordCount}`; diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts index bd324d8b5..f8baf6a86 100644 --- a/app/src/protyle/gutter/index.ts +++ b/app/src/protyle/gutter/index.ts @@ -688,7 +688,7 @@ export class Gutter { const turnIntoSubmenu: IMenu[] = []; hideElements(["select"], protyle); nodeElement.classList.add("protyle-wysiwyg--select"); - countBlockWord([id]); + countBlockWord([id], protyle.block.rootID); // "heading1-6", "list", "ordered-list", "check", "quote", "code", "table", "line", "math", "paragraph" if (type === "NodeParagraph" && !window.siyuan.config.readonly) { turnIntoSubmenu.push(this.turnsIntoOne({ @@ -1271,7 +1271,7 @@ export class Gutter { accelerator: window.siyuan.config.keymap.editor.general.insertBefore.custom, click() { nodeElement.classList.remove("protyle-wysiwyg--select"); - countBlockWord([]); + countBlockWord([], protyle.block.rootID); insertEmptyBlock(protyle, "beforebegin", id); } }).element); @@ -1281,7 +1281,7 @@ export class Gutter { accelerator: window.siyuan.config.keymap.editor.general.insertAfter.custom, click() { nodeElement.classList.remove("protyle-wysiwyg--select"); - countBlockWord([]); + countBlockWord([], protyle.block.rootID); insertEmptyBlock(protyle, "afterend", id); } }).element); diff --git a/app/src/protyle/util/selection.ts b/app/src/protyle/util/selection.ts index 96ab26990..3901b3398 100644 --- a/app/src/protyle/util/selection.ts +++ b/app/src/protyle/util/selection.ts @@ -59,7 +59,7 @@ export const selectAll = (protyle: IProtyle, nodeElement: Element, range: Range) range.setStart(cellElement.firstChild, 0); range.setEndAfter(cellElement.lastChild); protyle.toolbar.render(protyle, range); - countSelectWord(range); + countSelectWord(range, protyle.block.rootID); return true; } } @@ -103,7 +103,7 @@ export const selectAll = (protyle: IProtyle, nodeElement: Element, range: Range) } } protyle.toolbar.render(protyle, range); - countSelectWord(range); + countSelectWord(range, protyle.block.rootID); return true; } } @@ -121,7 +121,7 @@ export const selectAll = (protyle: IProtyle, nodeElement: Element, range: Range) item.classList.add("protyle-wysiwyg--select"); ids.push(item.getAttribute("data-node-id")); }); - countBlockWord(ids); + countBlockWord(ids, protyle.block.rootID); }; export const getEditorRange = (element: Element) => { diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts index 5e5bbf4eb..370ed8cfd 100644 --- a/app/src/protyle/wysiwyg/index.ts +++ b/app/src/protyle/wysiwyg/index.ts @@ -1378,12 +1378,8 @@ export class WYSIWYG { } if (event.eventPhase !== 3 && !event.shiftKey && (event.key.indexOf("Arrow") > -1 || event.key === "Home" || event.key === "End" || event.key === "PageUp" || event.key === "PageDown") && !event.isComposing) { - // 不可编辑的块处理 const nodeElement = hasClosestBlock(range.startContainer); if (nodeElement) { - if (!nodeElement.classList.contains("protyle-wysiwyg--select")) { - countSelectWord(range); - } this.setEmptyOutline(protyle, nodeElement); } event.stopPropagation(); diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts index f1f266fff..88c3d571d 100644 --- a/app/src/protyle/wysiwyg/keydown.ts +++ b/app/src/protyle/wysiwyg/keydown.ts @@ -338,7 +338,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select").forEach(item => { ids.push(item.getAttribute("data-node-id")); }); - countBlockWord(ids); + countBlockWord(ids, protyle.block.rootID); event.stopPropagation(); event.preventDefault(); return; @@ -386,7 +386,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select").forEach(item => { ids.push(item.getAttribute("data-node-id")); }); - countBlockWord(ids); + countBlockWord(ids, protyle.block.rootID); event.stopPropagation(); event.preventDefault(); return; @@ -680,7 +680,6 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { const selectText = range.toString(); // 删除,不可使用 !isCtrl(event),否则软删除回导致 https://github.com/siyuan-note/siyuan/issues/5607 if (!event.altKey && !event.shiftKey && (event.key === "Backspace" || event.key === "Delete")) { - countBlockWord([]); // https://github.com/siyuan-note/siyuan/issues/5547 const previousSibling = hasPreviousSibling(range.startContainer) as HTMLElement; if (range.startOffset === 1 && range.startContainer.textContent === Constants.ZWSP && @@ -1087,7 +1086,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { protyle.hint.enableEmoji = false; } else if (nodeElement.classList.contains("protyle-wysiwyg--select")) { hideElements(["select"], protyle); - countBlockWord([]); + countBlockWord([], protyle.block.rootID); } else if (!window.siyuan.menus.menu.element.classList.contains("fn__none")) { // 防止 ESC 时选中当前块 window.siyuan.menus.menu.remove(); @@ -1097,7 +1096,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { }); range.collapse(false); nodeElement.classList.add("protyle-wysiwyg--select"); - countBlockWord([nodeElement.getAttribute("data-node-id")]); + countBlockWord([nodeElement.getAttribute("data-node-id")], protyle.block.rootID); } event.preventDefault(); return; diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts index 6b14a5369..0bceb3d4f 100644 --- a/app/src/protyle/wysiwyg/transaction.ts +++ b/app/src/protyle/wysiwyg/transaction.ts @@ -16,6 +16,7 @@ import {removeFoldHeading} from "../util/heading"; import {genEmptyElement, genSBElement} from "../../block/util"; import {hideElements} from "../ui/hideElements"; import {reloadProtyle} from "../util/reload"; +import {countBlockWord} from "../../layout/status"; const removeTopElement = (updateElement: Element, protyle: IProtyle) => { // 移动到其他文档中,该块需移除 @@ -71,6 +72,7 @@ const promiseTransaction = () => { lockFile(protyle.block.rootID); return; } + countBlockWord([], protyle.block.rootID); if (doOperations.length === 1 && (doOperations[0].action === "unfoldHeading" || doOperations[0].action === "foldHeading" || doOperations[0].action === "setAttrs")) { const gutterFoldElement = protyle.gutter.element.querySelector('[data-type="fold"]'); if (gutterFoldElement) {