From cd0c1bc399096bb4981a1e9fe14e138cffa63270 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Fri, 3 Mar 2023 23:56:43 +0800 Subject: [PATCH] :sparkles: fix https://github.com/siyuan-note/siyuan/issues/7518 --- app/src/mobile/util/keyboardToolbar.ts | 102 +++++++++++++++---------- 1 file changed, 60 insertions(+), 42 deletions(-) diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.ts index f3af934b8..6fa21bef3 100644 --- a/app/src/mobile/util/keyboardToolbar.ts +++ b/app/src/mobile/util/keyboardToolbar.ts @@ -43,30 +43,66 @@ export const renderKeyboardToolbar = () => { if (getSelection().rangeCount === 0 || window.siyuan.config.editor.readOnly || window.siyuan.config.readonly) { return; } + const dynamicElements = document.querySelectorAll("#keyboardToolbar .keyboard__dynamic"); const range = getSelection().getRangeAt(0); - let html = "" - if (hasClosestByClassName(range.startContainer, "protyle-wysiwyg", true)) { - const protyle = window.siyuan.mobile.editor.protyle; - html = ` - - - - - - - - - - - - -`; - } const selectText = range.toString(); - console.log(range) - document.getElementById("keyboardToolbar").innerHTML = `
-
${html}
-
+ const isProtyle = hasClosestByClassName(range.startContainer, "protyle-wysiwyg", true) + if (selectText || !isProtyle) { + dynamicElements[0].classList.add("fn__none") + } else { + dynamicElements[0].classList.remove("fn__none") + const protyle = window.siyuan.mobile.editor.protyle; + if (protyle.undo.undoStack.length === 0) { + dynamicElements[0].querySelector('[data-type="undo"]').setAttribute("disabled", "disabled"); + } else { + dynamicElements[0].querySelector('[data-type="undo"]').removeAttribute("disabled"); + } + if (protyle.undo.redoStack.length === 0) { + dynamicElements[0].querySelector('[data-type="redo"]').setAttribute("disabled", "disabled"); + } else { + dynamicElements[0].querySelector('[data-type="redo"]').removeAttribute("disabled"); + } + } + if (selectText && isProtyle) { + dynamicElements[1].classList.remove("fn__none") + } else { + dynamicElements[1].classList.add("fn__none") + } + }, 620); // 需等待 range 更新 +}; + +export const hideKeyboardToolbar = () => { + const toolbarElement = document.getElementById("keyboardToolbar"); + toolbarElement.classList.add("fn__none"); +}; + +export const hideKeyboard = () => { + (document.activeElement as HTMLElement).blur(); +}; + +export const initKeyboardToolbar = () => { + document.addEventListener("selectionchange", () => { + renderKeyboardToolbar() + }, false); + + const toolbarElement = document.getElementById("keyboardToolbar"); + toolbarElement.innerHTML = `
+
+ + + + + + + + + + + + + +
+
@@ -89,24 +125,6 @@ export const renderKeyboardToolbar = () => {
`; - }, 620) // 需等待 range 更新 -}; - -export const hideKeyboardToolbar = () => { - const toolbarElement = document.getElementById("keyboardToolbar"); - toolbarElement.classList.add("fn__none"); -}; - -export const hideKeyboard = () => { - (document.activeElement as HTMLElement).blur(); -}; - -export const initKeyboardToolbar = () => { - document.addEventListener("selectionchange", () => { - renderKeyboardToolbar() - }, false); - - const toolbarElement = document.getElementById("keyboardToolbar"); toolbarElement.addEventListener("click", (event) => { const target = event.target as HTMLElement; const buttonElement = hasClosestByMatchTag(target, "BUTTON"); @@ -155,12 +173,12 @@ export const initKeyboardToolbar = () => { } // inline element if (["a", "block-ref", "inline-math", "inline-memo", "text"].includes(type)) { - protyle.toolbar.element.querySelector(`[data-type="${type}"]`).dispatchEvent(new CustomEvent("block-ref" === type ? getEventName() : "click")); + protyle.toolbar.element.querySelector(`[data-type="${type}"]`).dispatchEvent(new CustomEvent("text" === type ? getEventName() : "click")); return true; } if (["strong", "em", "s", "code", "mark", "tag", "u", "sup", "clear", "sub", "kbd"].includes(type)) { protyle.toolbar.setInlineMark(protyle, type, "toolbar"); - return ; + return; } // TODO block element if (type === "up") {