diff --git a/app/src/mobile/index.ts b/app/src/mobile/index.ts index 3421fc873..36984306f 100644 --- a/app/src/mobile/index.ts +++ b/app/src/mobile/index.ts @@ -2,12 +2,7 @@ import {addScript, addScriptSync} from "../protyle/util/addScript"; import {Constants} from "../constants"; import {onMessage} from "./util/onMessage"; import {genUUID} from "../util/genID"; -import { - hasClosestBlock, - hasClosestByAttribute, - hasClosestByClassName, - hasTopClosestByClassName -} from "../protyle/util/hasClosest"; +import {hasClosestBlock, hasClosestByAttribute, hasTopClosestByClassName} from "../protyle/util/hasClosest"; import {Model} from "../layout/Model"; import "../assets/scss/mobile.scss"; import {Menus} from "../menus"; @@ -37,7 +32,7 @@ import {correctHotkey} from "../boot/globalEvent/commonHotkey"; import {processIOSPurchaseResponse} from "../util/iOSPurchase"; import {updateControlAlt} from "../protyle/util/hotKey"; import {nbsp2space} from "../protyle/util/normalizeText"; -import {callMobileAppShowKeyboard} from "./util/mobileAppUtil"; +import {callMobileAppShowKeyboard, canInput} from "./util/mobileAppUtil"; class App { public plugins: import("../plugin").Plugin[] = []; @@ -102,16 +97,7 @@ class App { }, Constants.TIMEOUT_TRANSITION); } if (window.JSAndroid && window.JSAndroid.showKeyboard || window.JSHarmony && window.JSHarmony.showKeyboard) { - const wysisygElement = hasClosestByClassName(event.target, "protyle-wysiwyg", true); - let editElement: HTMLElement; - if ((event.target.tagName === "TEXTAREA" || - (event.target.tagName === "INPUT" && ["email", "number", "password", "search", "tel", "text", "url", "", null].includes(event.target.getAttribute("type")))) && - event.target.getAttribute("readonly") !== "readonly") { - editElement = event.target; - } else if (wysisygElement && wysisygElement.getAttribute("data-readonly") === "false") { - editElement = hasClosestByAttribute(event.target, "contenteditable", "true") as HTMLElement; - } - if (editElement) { + if (canInput(event.target)) { callMobileAppShowKeyboard(); } } @@ -126,16 +112,8 @@ class App { } catch (e) { console.error("Error in focus event:", e); } - const wysisygElement = hasClosestByClassName(this, "protyle-wysiwyg", true); - if ((this.tagName === "TEXTAREA" || - (this.tagName === "INPUT" && ["email", "number", "password", "search", "tel", "text", "url", "", null].includes(this.getAttribute("type")))) && - this.getAttribute("readonly") !== "readonly") { + if (canInput(this)) { callMobileAppShowKeyboard(); - } else if (wysisygElement && wysisygElement.getAttribute("data-readonly") === "false") { - const editElement = hasClosestByAttribute(this, "contenteditable", "true") as HTMLElement; - if (editElement) { - callMobileAppShowKeyboard(); - } } }; } diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.ts index b063a1b4e..c4e48c8da 100644 --- a/app/src/mobile/util/keyboardToolbar.ts +++ b/app/src/mobile/util/keyboardToolbar.ts @@ -14,7 +14,7 @@ import {hideElements} from "../../protyle/ui/hideElements"; import {softEnter} from "../../protyle/wysiwyg/enter"; import {isInAndroid, isInEdge, isInHarmony} from "../../protyle/util/compatibility"; import {tabCodeBlock} from "../../protyle/wysiwyg/codeBlock"; -import {callMobileAppShowKeyboard} from "./mobileAppUtil"; +import {callMobileAppShowKeyboard, canInput} from "./mobileAppUtil"; let renderKeyboardToolbarTimeout: number; let showUtil = false; @@ -327,24 +327,7 @@ const hideKeyboardToolbarUtil = () => { const renderKeyboardToolbar = () => { clearTimeout(renderKeyboardToolbarTimeout); renderKeyboardToolbarTimeout = window.setTimeout(() => { - if (getSelection().rangeCount === 0 || - window.siyuan.config.readonly || - document.getElementById("toolbarName").getAttribute("readonly") === "readonly" || - !document.activeElement || ( - document.activeElement && - !["INPUT", "TEXTAREA"].includes(document.activeElement.tagName) && - !document.activeElement.classList.contains("protyle-wysiwyg") && - document.activeElement.getAttribute("contenteditable") !== "true" - )) { - hideKeyboardToolbar(); - return; - } - // 编辑器设置界面点击空白或关闭,焦点不知何故会飘移到编辑器上 - if (document.activeElement && - !["INPUT", "TEXTAREA"].includes(document.activeElement.tagName) && ( - document.getElementById("menu").style.transform === "translateX(0px)" || - document.getElementById("model").style.transform === "translateY(0px)" - )) { + if (!canInput(document.activeElement)) { hideKeyboardToolbar(); return; } diff --git a/app/src/mobile/util/mobileAppUtil.ts b/app/src/mobile/util/mobileAppUtil.ts index 4b1e95dc9..ed791281d 100644 --- a/app/src/mobile/util/mobileAppUtil.ts +++ b/app/src/mobile/util/mobileAppUtil.ts @@ -1,3 +1,5 @@ +import {hasClosestByAttribute, hasClosestByClassName} from "../../protyle/util/hasClosest"; + export const callMobileAppShowKeyboard = () => { if (window.JSAndroid && window.JSAndroid.showKeyboard) { window.JSAndroid.showKeyboard(); @@ -5,3 +7,21 @@ export const callMobileAppShowKeyboard = () => { window.JSHarmony.showKeyboard(); } }; + + +export const canInput = (element: Element) => { + if (!element || element.nodeType !== 1) { + return false; + } + if (( + element.tagName === "TEXTAREA" || + (element.tagName === "INPUT" && ["email", "number", "password", "search", "tel", "text", "url", "", null].includes(element.getAttribute("type"))) + ) && element.getAttribute("readonly") !== "readonly") { + return element + } + const wysisygElement = hasClosestByClassName(element, "protyle-wysiwyg", true); + if (wysisygElement && wysisygElement.getAttribute("data-readonly") === "false") { + return hasClosestByAttribute(element, "contenteditable", "true") + } + return false; +} \ No newline at end of file