Vanessa 2025-10-13 22:50:55 +08:00
parent 4019ec3def
commit bd6cf65266
2 changed files with 23 additions and 11 deletions

View file

@ -542,11 +542,12 @@ export const initKeyboardToolbar = () => {
const slashBtnElement = hasClosestByClassName(event.target as HTMLElement, "keyboard__slash-item"); const slashBtnElement = hasClosestByClassName(event.target as HTMLElement, "keyboard__slash-item");
if (slashBtnElement && !slashBtnElement.getAttribute("data-type")) { if (slashBtnElement && !slashBtnElement.getAttribute("data-type")) {
const dataValue = decodeURIComponent(slashBtnElement.getAttribute("data-value")); const dataValue = decodeURIComponent(slashBtnElement.getAttribute("data-value"));
protyle.hint.fill(dataValue, protyle, false); // 点击后 range 会改变 if (dataValue === Constants.ZWSP + 3) {
if (dataValue !== Constants.ZWSP + 3) { return;
event.preventDefault();
event.stopPropagation();
} }
protyle.hint.fill(dataValue, protyle, false); // 点击后 range 会改变
event.preventDefault();
event.stopPropagation();
if (slashBtnElement.getAttribute("data-focus") === "true") { if (slashBtnElement.getAttribute("data-focus") === "true") {
focusByRange(protyle.toolbar.range); focusByRange(protyle.toolbar.range);
} }

View file

@ -5,7 +5,7 @@ import {
hasPreviousSibling, hasPreviousSibling,
isNotEditBlock isNotEditBlock
} from "../wysiwyg/getBlock"; } from "../wysiwyg/getBlock";
import {hasClosestByAttribute, hasClosestByTag} from "./hasClosest"; import {hasClosestBlock, hasClosestByAttribute, hasClosestByTag} from "./hasClosest";
import {countBlockWord, countSelectWord} from "../../layout/status"; import {countBlockWord, countSelectWord} from "../../layout/status";
import {hideElements} from "../ui/hideElements"; import {hideElements} from "../ui/hideElements";
import {genRenderFrame} from "../render/util"; import {genRenderFrame} from "../render/util";
@ -140,12 +140,23 @@ export const getEditorRange = (element: Element): Range => {
if (getSelection().rangeCount > 0) { if (getSelection().rangeCount > 0) {
range = getSelection().getRangeAt(0); range = getSelection().getRangeAt(0);
if (element === range.startContainer || element.contains(range.startContainer)) { if (element === range.startContainer || element.contains(range.startContainer)) {
// 有时候点击编辑器头部需要矫正到第一个块中 if (range.toString() === "" && range.startContainer.nodeType === 1) {
if (range.toString() === "" && range.startContainer.nodeType === 1 && range.startOffset === 0 && // 有时候点击编辑器头部需要矫正到第一个块中
(range.startContainer as HTMLElement).classList.contains("protyle-wysiwyg")) { if (range.startOffset === 0 && (range.startContainer as HTMLElement).classList.contains("protyle-wysiwyg")) {
const focusRange = focusBlock(range.startContainer.firstChild as Element); const focusRange = focusBlock(range.startContainer.firstChild as Element);
if (focusRange) { if (focusRange) {
return focusRange; return focusRange;
}
}
// 移动端获取有偏差 https://github.com/siyuan-note/siyuan/issues/15998
if (getContenteditableElement(range.startContainer as Element)) {
const blockElement = hasClosestBlock(range.startContainer);
if (blockElement) {
const focusRange = focusBlock(blockElement);
if (focusRange) {
return focusRange;
}
}
} }
} }
return range; return range;