Vanessa 2022-10-05 10:12:21 +08:00
parent a87f096fb1
commit c20da40cd7
9 changed files with 32 additions and 26 deletions

View File

@ -4,6 +4,7 @@ import {Model} from "../layout/Model";
import {disabledProtyle} from "../protyle/util/onGet"; import {disabledProtyle} from "../protyle/util/onGet";
import {setPadding} from "../protyle/ui/initUI"; import {setPadding} from "../protyle/ui/initUI";
import {getAllModels} from "../layout/getAll"; import {getAllModels} from "../layout/getAll";
import {countBlockWord} from "../layout/status";
export class Editor extends Model { export class Editor extends Model {
public element: HTMLElement; 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 // 需在 after 回调之前,否则不会聚焦 https://github.com/siyuan-note/siyuan/issues/5303

View File

@ -317,7 +317,7 @@ export const updatePanelByEditor = (protyle?: IProtyle, focus = true, pushBackSt
if (focus) { if (focus) {
if (protyle.toolbar.range) { if (protyle.toolbar.range) {
focusByRange(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")) { if (pushBackStack && protyle.preview.element.classList.contains("fn__none")) {
pushBack(protyle, protyle.toolbar.range); 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")) { if (pushBackStack && protyle.preview.element.classList.contains("fn__none")) {
pushBack(protyle, undefined, protyle.wysiwyg.element.firstElementChild); pushBack(protyle, undefined, protyle.wysiwyg.element.firstElementChild);
} }
// 用于清空状态栏字数统计 countBlockWord([], protyle.block.rootID);
countBlockWord([]);
} }
} }
if (window.siyuan.config.fileTree.alwaysSelectOpenedFile && protyle) { if (window.siyuan.config.fileTree.alwaysSelectOpenedFile && protyle) {

View File

@ -23,7 +23,7 @@ import {showMessage} from "../dialog/message";
import {openFileById, updatePanelByEditor} from "../editor/util"; import {openFileById, updatePanelByEditor} from "../editor/util";
import {scrollCenter} from "../util/highlightById"; import {scrollCenter} from "../util/highlightById";
import {getAllModels} from "./getAll"; import {getAllModels} from "./getAll";
import {countBlockWord} from "./status"; import {clearCounter} from "./status";
import {saveScroll} from "../protyle/scroll/saveScroll"; import {saveScroll} from "../protyle/scroll/saveScroll";
import {Asset} from "../asset"; import {Asset} from "../asset";
import {newFile} from "../util/newFile"; import {newFile} from "../util/newFile";
@ -534,7 +534,6 @@ export class Wnd {
} }
}); });
model.editor.destroy(); model.editor.destroy();
countBlockWord([]);
return; return;
} }
if (model instanceof Search) { if (model instanceof Search) {
@ -552,6 +551,7 @@ export class Wnd {
} }
private removeTabAction = (id: string, closeAll = false, hasSaveScroll = true) => { private removeTabAction = (id: string, closeAll = false, hasSaveScroll = true) => {
clearCounter();
this.children.find((item, index) => { this.children.find((item, index) => {
if (item.id === id) { if (item.id === id) {
if (item.model instanceof Editor && hasSaveScroll) { if (item.model instanceof Editor && hasSaveScroll) {

View File

@ -129,7 +129,7 @@ export const initStatus = () => {
/// #endif /// #endif
}; };
export const countSelectWord = (range: Range) => { export const countSelectWord = (range: Range, rootID?: string) => {
/// #if !MOBILE /// #if !MOBILE
if (document.getElementById("status").classList.contains("fn__none")) { if (document.getElementById("status").classList.contains("fn__none")) {
return; return;
@ -139,13 +139,15 @@ export const countSelectWord = (range: Range) => {
fetchPost("/api/block/getContentWordCount", {"content": range.toString()}, (response) => { fetchPost("/api/block/getContentWordCount", {"content": range.toString()}, (response) => {
renderStatusbarCounter(response.data); renderStatusbarCounter(response.data);
}); });
} else { } else if (rootID) {
document.querySelector("#status .status__counter").innerHTML = ""; fetchPost("/api/block/getTreeStat", {id: rootID}, (response) => {
renderStatusbarCounter(response.data);
});
} }
/// #endif /// #endif
}; };
export const countBlockWord = (ids: string[]) => { export const countBlockWord = (ids: string[], rootID?: string) => {
/// #if !MOBILE /// #if !MOBILE
if (document.getElementById("status").classList.contains("fn__none")) { if (document.getElementById("status").classList.contains("fn__none")) {
return; return;
@ -154,12 +156,18 @@ export const countBlockWord = (ids: string[]) => {
fetchPost("/api/block/getBlocksWordCount", {ids}, (response) => { fetchPost("/api/block/getBlocksWordCount", {ids}, (response) => {
renderStatusbarCounter(response.data); renderStatusbarCounter(response.data);
}); });
} else { } else if (rootID) {
document.querySelector("#status .status__counter").innerHTML = ""; fetchPost("/api/block/getTreeStat", {id: rootID}, (response) => {
renderStatusbarCounter(response.data);
});
} }
/// #endif /// #endif
}; };
export const clearCounter = () => {
document.querySelector("#status .status__counter").innerHTML = "";
}
export const renderStatusbarCounter = (stat: { runeCount: number, wordCount: number, linkCount: number, imageCount: number, refCount: number }) => { export const renderStatusbarCounter = (stat: { runeCount: number, wordCount: number, linkCount: number, imageCount: number, refCount: number }) => {
let html = `<span class="ft__on-surface">${window.siyuan.languages.runeCount}</span>&nbsp;${stat.runeCount}<span class="fn__space"></span> let html = `<span class="ft__on-surface">${window.siyuan.languages.runeCount}</span>&nbsp;${stat.runeCount}<span class="fn__space"></span>
<span class="ft__on-surface">${window.siyuan.languages.wordCount}</span>&nbsp;${stat.wordCount}<span class="fn__space"></span>`; <span class="ft__on-surface">${window.siyuan.languages.wordCount}</span>&nbsp;${stat.wordCount}<span class="fn__space"></span>`;

View File

@ -688,7 +688,7 @@ export class Gutter {
const turnIntoSubmenu: IMenu[] = []; const turnIntoSubmenu: IMenu[] = [];
hideElements(["select"], protyle); hideElements(["select"], protyle);
nodeElement.classList.add("protyle-wysiwyg--select"); 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" // "heading1-6", "list", "ordered-list", "check", "quote", "code", "table", "line", "math", "paragraph"
if (type === "NodeParagraph" && !window.siyuan.config.readonly) { if (type === "NodeParagraph" && !window.siyuan.config.readonly) {
turnIntoSubmenu.push(this.turnsIntoOne({ turnIntoSubmenu.push(this.turnsIntoOne({
@ -1271,7 +1271,7 @@ export class Gutter {
accelerator: window.siyuan.config.keymap.editor.general.insertBefore.custom, accelerator: window.siyuan.config.keymap.editor.general.insertBefore.custom,
click() { click() {
nodeElement.classList.remove("protyle-wysiwyg--select"); nodeElement.classList.remove("protyle-wysiwyg--select");
countBlockWord([]); countBlockWord([], protyle.block.rootID);
insertEmptyBlock(protyle, "beforebegin", id); insertEmptyBlock(protyle, "beforebegin", id);
} }
}).element); }).element);
@ -1281,7 +1281,7 @@ export class Gutter {
accelerator: window.siyuan.config.keymap.editor.general.insertAfter.custom, accelerator: window.siyuan.config.keymap.editor.general.insertAfter.custom,
click() { click() {
nodeElement.classList.remove("protyle-wysiwyg--select"); nodeElement.classList.remove("protyle-wysiwyg--select");
countBlockWord([]); countBlockWord([], protyle.block.rootID);
insertEmptyBlock(protyle, "afterend", id); insertEmptyBlock(protyle, "afterend", id);
} }
}).element); }).element);

View File

@ -59,7 +59,7 @@ export const selectAll = (protyle: IProtyle, nodeElement: Element, range: Range)
range.setStart(cellElement.firstChild, 0); range.setStart(cellElement.firstChild, 0);
range.setEndAfter(cellElement.lastChild); range.setEndAfter(cellElement.lastChild);
protyle.toolbar.render(protyle, range); protyle.toolbar.render(protyle, range);
countSelectWord(range); countSelectWord(range, protyle.block.rootID);
return true; return true;
} }
} }
@ -103,7 +103,7 @@ export const selectAll = (protyle: IProtyle, nodeElement: Element, range: Range)
} }
} }
protyle.toolbar.render(protyle, range); protyle.toolbar.render(protyle, range);
countSelectWord(range); countSelectWord(range, protyle.block.rootID);
return true; return true;
} }
} }
@ -121,7 +121,7 @@ export const selectAll = (protyle: IProtyle, nodeElement: Element, range: Range)
item.classList.add("protyle-wysiwyg--select"); item.classList.add("protyle-wysiwyg--select");
ids.push(item.getAttribute("data-node-id")); ids.push(item.getAttribute("data-node-id"));
}); });
countBlockWord(ids); countBlockWord(ids, protyle.block.rootID);
}; };
export const getEditorRange = (element: Element) => { export const getEditorRange = (element: Element) => {

View File

@ -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) { 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); const nodeElement = hasClosestBlock(range.startContainer);
if (nodeElement) { if (nodeElement) {
if (!nodeElement.classList.contains("protyle-wysiwyg--select")) {
countSelectWord(range);
}
this.setEmptyOutline(protyle, nodeElement); this.setEmptyOutline(protyle, nodeElement);
} }
event.stopPropagation(); event.stopPropagation();

View File

@ -338,7 +338,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select").forEach(item => { protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select").forEach(item => {
ids.push(item.getAttribute("data-node-id")); ids.push(item.getAttribute("data-node-id"));
}); });
countBlockWord(ids); countBlockWord(ids, protyle.block.rootID);
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
return; return;
@ -386,7 +386,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select").forEach(item => { protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select").forEach(item => {
ids.push(item.getAttribute("data-node-id")); ids.push(item.getAttribute("data-node-id"));
}); });
countBlockWord(ids); countBlockWord(ids, protyle.block.rootID);
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
return; return;
@ -680,7 +680,6 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
const selectText = range.toString(); const selectText = range.toString();
// 删除,不可使用 !isCtrl(event),否则软删除回导致 https://github.com/siyuan-note/siyuan/issues/5607 // 删除,不可使用 !isCtrl(event),否则软删除回导致 https://github.com/siyuan-note/siyuan/issues/5607
if (!event.altKey && !event.shiftKey && (event.key === "Backspace" || event.key === "Delete")) { if (!event.altKey && !event.shiftKey && (event.key === "Backspace" || event.key === "Delete")) {
countBlockWord([]);
// https://github.com/siyuan-note/siyuan/issues/5547 // https://github.com/siyuan-note/siyuan/issues/5547
const previousSibling = hasPreviousSibling(range.startContainer) as HTMLElement; const previousSibling = hasPreviousSibling(range.startContainer) as HTMLElement;
if (range.startOffset === 1 && range.startContainer.textContent === Constants.ZWSP && if (range.startOffset === 1 && range.startContainer.textContent === Constants.ZWSP &&
@ -1087,7 +1086,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
protyle.hint.enableEmoji = false; protyle.hint.enableEmoji = false;
} else if (nodeElement.classList.contains("protyle-wysiwyg--select")) { } else if (nodeElement.classList.contains("protyle-wysiwyg--select")) {
hideElements(["select"], protyle); hideElements(["select"], protyle);
countBlockWord([]); countBlockWord([], protyle.block.rootID);
} else if (!window.siyuan.menus.menu.element.classList.contains("fn__none")) { } else if (!window.siyuan.menus.menu.element.classList.contains("fn__none")) {
// 防止 ESC 时选中当前块 // 防止 ESC 时选中当前块
window.siyuan.menus.menu.remove(); window.siyuan.menus.menu.remove();
@ -1097,7 +1096,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
}); });
range.collapse(false); range.collapse(false);
nodeElement.classList.add("protyle-wysiwyg--select"); nodeElement.classList.add("protyle-wysiwyg--select");
countBlockWord([nodeElement.getAttribute("data-node-id")]); countBlockWord([nodeElement.getAttribute("data-node-id")], protyle.block.rootID);
} }
event.preventDefault(); event.preventDefault();
return; return;

View File

@ -16,6 +16,7 @@ import {removeFoldHeading} from "../util/heading";
import {genEmptyElement, genSBElement} from "../../block/util"; import {genEmptyElement, genSBElement} from "../../block/util";
import {hideElements} from "../ui/hideElements"; import {hideElements} from "../ui/hideElements";
import {reloadProtyle} from "../util/reload"; import {reloadProtyle} from "../util/reload";
import {countBlockWord} from "../../layout/status";
const removeTopElement = (updateElement: Element, protyle: IProtyle) => { const removeTopElement = (updateElement: Element, protyle: IProtyle) => {
// 移动到其他文档中,该块需移除 // 移动到其他文档中,该块需移除
@ -71,6 +72,7 @@ const promiseTransaction = () => {
lockFile(protyle.block.rootID); lockFile(protyle.block.rootID);
return; return;
} }
countBlockWord([], protyle.block.rootID);
if (doOperations.length === 1 && (doOperations[0].action === "unfoldHeading" || doOperations[0].action === "foldHeading" || doOperations[0].action === "setAttrs")) { 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"]'); const gutterFoldElement = protyle.gutter.element.querySelector('[data-type="fold"]');
if (gutterFoldElement) { if (gutterFoldElement) {