📱 输入键盘

Signed-off-by: Daniel <845765@qq.com>
This commit is contained in:
Daniel 2026-02-10 20:39:45 +08:00
parent 830320096e
commit 621ca5d273
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 26 additions and 45 deletions

View file

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

View file

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

View file

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