An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Correct iOS touch caret placement and double-tap selection https://github.com/siyuan-note/siyuan/issues/20168
Vanessaaa89fffcfe55
diff --git a/app/src/mobile/util/columnResizeTouch.test.ts b/app/src/mobile/util/columnResizeTouch.test.tsindex 059cfd92f3..41ce993ad5 100644--- a/app/src/mobile/util/columnResizeTouch.test.ts+++ b/app/src/mobile/util/columnResizeTouch.test.ts@@ -139,17 +139,27 @@ const setup = (resize = true) => {isInAndroid: () => false, ipcRenderer: {on() {}}, stopScrollAnimation() {},}, ...globals});bridge.initTouchDragBridge();- const dispatch = (type: string, x: number, eventTarget = target) => {+ const dispatch = (type: string, x: number, eventTarget = target, properties = {}) => {now += 50;const point = {target: eventTarget, clientX: x, clientY: 200, radiusX: 1};const event = {target: eventTarget, touches: [point], changedTouches: [point], defaultPrevented: false,- preventDefault() { this.defaultPrevented = true; }, stopImmediatePropagation() {}};+ preventDefault() { this.defaultPrevented = true; }, stopImmediatePropagation() {}, ...properties};handlers.get(type)?.forEach(listener => listener(event));return event;};- return {actions, left, right, target, child, scroll, dispatch, width: () => width};+ return {actions, left, right, target, child, scroll, dispatch, width: () => width,+ advance: (duration: number) => now += duration};};+test("delayed short taps do not become global long-press menus while actual long presses are consumed", () => {+ const fixture = setup(false);+ fixture.dispatch("touchstart", 180, fixture.target, {timeStamp: 100});+ fixture.advance(600);+ assert.equal(fixture.dispatch("touchend", 180, fixture.target, {timeStamp: 150}).defaultPrevented, false);+ fixture.dispatch("touchstart", 180, fixture.target, {timeStamp: 200});+ assert.equal(fixture.dispatch("touchend", 180, fixture.target, {timeStamp: 800}).defaultPrevented, true);+});+for (const x of [120, 240]) {test(`database width drag to ${x}px keeps both sidebars closed while the resize bridge runs`, () => {const fixture = setup();diff --git a/app/src/mobile/util/touch.ts b/app/src/mobile/util/touch.tsindex 52c84cf25b..4a90245b6e 100644--- a/app/src/mobile/util/touch.ts+++ b/app/src/mobile/util/touch.ts@@ -41,6 +41,7 @@ let clientY: number;let xDiff: number;let yDiff: number;let time: number;+let touchStartTimeStamp: number;let firstDirection: MobileSwipeDirection;let firstXY: "x" | "y";let lastClientX: number; // 和起始方向不一致时,记录最后一次的 clientX@@ -180,6 +181,8 @@ export const handleTouchEnd = (event: TouchEvent) => {}const target = event.target as HTMLElement;const currentTime = Date.now();+ const touchDuration = Number.isFinite(event.timeStamp) && Number.isFinite(touchStartTimeStamp) ?+ event.timeStamp - touchStartTimeStamp : currentTime - time;const editor = getCurrentEditor();if (!isInHarmony() && !isInAndroid()) {handleTouchUp();@@ -212,7 +215,7 @@ export const handleTouchEnd = (event: TouchEvent) => {event.stopImmediatePropagation();event.preventDefault();}- } else if (currentTime - time > Constants.TIMEOUT_LONGPRESS) {+ } else if (touchDuration > Constants.TIMEOUT_LONGPRESS) {// 长按:多选已在按住满阈值时触发,此处取消定时器避免重复触发if (isIPhone() && !isChromeBrowser() && !window.siyuan.touchDragActive) {target.dispatchEvent(new MouseEvent("contextmenu", {@@ -374,6 +377,7 @@ export const handleTouchStart = (event: TouchEvent) => {updateSidebarSwipeState();resetTouchGesture();time = Date.now();+ touchStartTimeStamp = event.timeStamp;longPressBlockElement = undefined;longPressTouchRange = undefined;const target = event.touches[0].target as HTMLElement;diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.tsindex bddf1ae15d..e5bec46679 100644--- a/app/src/protyle/wysiwyg/index.ts+++ b/app/src/protyle/wysiwyg/index.ts@@ -11,6 +11,7 @@ import {repairHiddenTabSelection} from "../util/tabsSelection";import {isTabTextBoundary} from "./tabsBoundary";import {captureCompositionText} from "./compositionCaret";import {isCommittedTextInput} from "./compositionInput";+import {bindIOSTouchCaret} from "./touchCaret";import {isDirectMathClick} from "../util/mathClick";import {beforePaste,@@ -414,6 +415,7 @@ export class WYSIWYG {private disposeEmbedToolbarVisibility?: () => void;private disposeHeadingFoldIndicators?: () => void;private disposeBoundedBlockDragSelect?: () => void;+ private disposeTouchCaret?: () => void;private scheduleInput(callback: () => void | Promise<void>, delay = 0, replace = true) {if (replace && this.inputTimeout) {@@ -556,6 +558,11 @@ export class WYSIWYG {if (isAndroid() || isIOSDevice()) {this.disposeBoundedBlockDragSelect = bindBoundedBlockDragSelect(protyle, this.element);}+ if (isIOSDevice()) {+ this.disposeTouchCaret = bindIOSTouchCaret(this.element, () => !protyle.disabled &&+ !protyle.toolbar.isMultiSelectMode() && !window.siyuan.touchDragActive &&+ !getBlockSelectionModeElement(this.element));+ }/// #if !MOBILEbindTouchNavigation(this.element, (target, point) => {if (!protyle.toolbar.isMultiSelectMode() && !window.siyuan.touchDragActive) {@@ -588,6 +595,7 @@ export class WYSIWYG {this.disposeEmbedToolbarVisibility?.();this.disposeSpellcheckFocus?.();this.disposeBoundedBlockDragSelect?.();+ this.disposeTouchCaret?.();this.largeListVirtualizer?.destroy();this.largeTableVirtualizer?.destroy();}diff --git a/app/src/protyle/wysiwyg/touchCaret.ts b/app/src/protyle/wysiwyg/touchCaret.tsnew file mode 100644index 0000000000..2173f2cd55--- /dev/null+++ b/app/src/protyle/wysiwyg/touchCaret.ts@@ -0,0 +1,245 @@+import {Constants} from "../../constants";+import {focusByRange} from "../util/selection";+import {isCommittedTextInput} from "./compositionInput";+import {bindTouchNavigation} from "./touchNavigation";++const TOUCH_CARET_EXCLUDED = "input, textarea, select, button, a, img, audio, video, iframe, " ++ ".protyle-action, .protyle-attr, .table, .av, .protyle-custom, [data-prevent-swipe], " ++ "[data-type~='block-ref'], [data-type~='virtual-block-ref'], [data-type~='file-annotation-ref'], " ++ "[data-type~='a'], [data-type~='tag'], [data-type~='inline-memo'], [data-type~='inline-math']";++const getTouchCharacterRange = (range: Range, point: {x: number, y: number}) => {+ const node = range.startContainer;+ if (node.nodeType !== Node.TEXT_NODE) {+ return;+ }+ for (const candidate of [range.startOffset, range.startOffset - 1]) {+ if (candidate < 0 || candidate >= node.textContent.length) {+ continue;+ }+ const start = candidate > 0 && /[\uDC00-\uDFFF]/.test(node.textContent[candidate]) ? candidate - 1 : candidate;+ const end = start + (node.textContent.codePointAt(start) > 0xffff ? 2 : 1);+ const character = range.cloneRange();+ character.setStart(node, start);+ character.setEnd(node, end);+ if (character.toString().trim() && Array.from(character.getClientRects()).some(rect =>+ point.x >= rect.left && point.x <= rect.right && point.y >= rect.top && point.y <= rect.bottom)) {+ return character;+ }+ }+};++export const bindIOSTouchCaret = (element: HTMLElement, canPlaceCaret: () => boolean) => {+ const ownerDocument = element.ownerDocument;+ const ownerWindow = ownerDocument.defaultView;+ let frame: number;+ let composing = false;+ let touchCaret: {editable: HTMLElement, range: Range, character?: Range, startedUnfocused: boolean, time: number};+ let previousTap: typeof touchCaret & {time: number, x: number, y: number, count: number, word?: Range | false};+ let pendingTap: typeof previousTap;+ const cancelFrame = () => {+ if (frame !== undefined) {+ ownerWindow.cancelAnimationFrame(frame);+ frame = undefined;+ }+ };+ const cancel = () => {+ cancelFrame();+ pendingTap = undefined;+ };+ const schedule = () => {+ cancelFrame();+ const tap = pendingTap;+ // 等待原生点击更新折叠选区后校正位置,不阻止点击、键盘唤起或系统文字选择手势。+ frame = ownerWindow.requestAnimationFrame(() => {+ frame = undefined;+ const selection = ownerDocument.getSelection();+ const activeElement = ownerDocument.activeElement;+ if (composing || !canPlaceCaret() || !tap.range.startContainer.isConnected ||+ !tap.editable.isContentEditable || !element.contains(activeElement) ||+ (!tap.editable.contains(activeElement) && !activeElement.contains(tap.editable)) ||+ !selection?.isCollapsed || selection.rangeCount === 0 ||+ !element.contains(selection.anchorNode)) {+ return;+ }+ if (tap.count === 1) {+ if (selection.anchorNode !== tap.range.startContainer || selection.anchorOffset !== tap.range.startOffset) {+ focusByRange(tap.range);+ }+ return;+ }+ if (tap.word === false || !tap.character?.startContainer.isConnected) {+ return;+ }+ if (tap.word) {+ focusByRange(tap.word);+ return;+ }+ const word = tap.character.cloneRange() as Range & {expand?: (unit: string) => void};+ if (typeof word.expand === "function") {+ word.expand("word");+ } else {+ // 使用浏览器的分词粒度,兼容未提供 Range.expand 的实现。+ const saved = selection.getRangeAt(0).cloneRange();+ selection.collapse(word.endContainer, word.endOffset);+ selection.modify("move", "backward", "word");+ selection.modify("extend", "forward", "word");+ const expanded = selection.getRangeAt(0);+ word.setStart(expanded.startContainer, expanded.startOffset);+ word.setEnd(expanded.endContainer, expanded.endOffset);+ focusByRange(saved);+ }+ if (word.startContainer.nodeType === Node.TEXT_NODE) {+ const leadingSpace = word.startContainer.textContent.substring(word.startOffset).match(/^\s+/u);+ if (leadingSpace) {+ word.setStart(word.startContainer, word.startOffset + leadingSpace[0].length);+ }+ }+ if (word.endContainer.nodeType === Node.TEXT_NODE) {+ const trailingSpace = word.endContainer.textContent.substring(0, word.endOffset).match(/\s+$/u);+ if (trailingSpace) {+ word.setEnd(word.endContainer, word.endOffset - trailingSpace[0].length);+ }+ }+ if (!word.collapsed && word.toString().trim() && tap.editable.contains(word.startContainer) &&+ tap.editable.contains(word.endContainer) &&+ !Array.from(tap.editable.querySelectorAll(TOUCH_CARET_EXCLUDED + ", [contenteditable='false']"))+ .some(item => word.intersectsNode(item))) {+ tap.word = word;+ focusByRange(word);+ } else {+ tap.word = false;+ }+ });+ };+ const selectionChange = () => {+ const tap = pendingTap;+ const selection = ownerDocument.getSelection();+ if (frame !== undefined || !tap || tap.word === false || Date.now() - tap.time >= Constants.TIMEOUT_LONGPRESS ||+ !selection?.isCollapsed || selection.rangeCount === 0) {+ return;+ }+ // 原生手势可能在后续事件中再次吸附到词边界,仅在本轮短按内校正迟到的折叠选区。+ if (tap.count === 2 || selection.anchorNode !== tap.range.startContainer ||+ selection.anchorOffset !== tap.range.startOffset) {+ schedule();+ }+ };+ const click = (event: MouseEvent) => {+ if (pendingTap && !composing && canPlaceCaret() && !event.defaultPrevented && event.detail === 1 &&+ Date.now() - pendingTap.time < Constants.TIMEOUT_LONGPRESS &&+ Math.hypot(event.clientX - pendingTap.x, event.clientY - pendingTap.y) < Constants.SIZE_DRAG_THRESHOLD) {+ if (event.target === element && pendingTap.startedUnfocused) {+ // 布局变化使合成点击落到非编辑宿主时,保留最初命中的正文焦点和选区。+ event.preventDefault();+ pendingTap.editable.focus({preventScroll: true});+ const selection = ownerDocument.getSelection();+ if (!selection?.rangeCount || selection.isCollapsed) {+ focusByRange(pendingTap.word || pendingTap.range);+ }+ }+ // 原生点击晚于首帧到达时,沿用本轮触摸命中的字符位置。+ schedule();+ }+ };+ const focusOut = (event: FocusEvent) => {+ if (event.relatedTarget && !element.contains(event.relatedTarget as Node)) {+ cancel();+ }+ };+ const compositionStart = () => {+ composing = true;+ cancel();+ };+ const compositionEnd = () => {+ composing = false;+ };+ const input = (event: InputEvent) => {+ cancel();+ if (isCommittedTextInput(event)) {+ composing = false;+ }+ };+ const touchStart = (event: TouchEvent) => {+ cancel();+ touchCaret = undefined;+ const target = event.target as HTMLElement;+ if (event.defaultPrevented || event.touches.length !== 1 || composing || !canPlaceCaret() ||+ target.closest(TOUCH_CARET_EXCLUDED)) {+ return;+ }+ const point = {x: event.touches[0].clientX, y: event.touches[0].clientY};+ if (previousTap?.count === 1 && previousTap.startedUnfocused &&+ Date.now() - previousTap.time < Constants.TIMEOUT_LONGPRESS &&+ Math.hypot(point.x - previousTap.x, point.y - previousTap.y) < Constants.SIZE_DRAG_THRESHOLD * 4 &&+ previousTap.range.startContainer.isConnected && element.contains(previousTap.editable)) {+ // 键盘唤起后第二次点击可能命中正文空白,双击仍以第一次按下时的字符为目标。+ touchCaret = {...previousTap, time: Date.now()};+ return;+ }+ const editable = target.closest<HTMLElement>("[contenteditable]");+ if (!editable || editable.getAttribute("contenteditable") !== "true" || !element.contains(editable)) {+ return;+ }+ const range = ownerDocument.caretRangeFromPoint(point.x, point.y);+ const rangeElement = range && (range.startContainer.nodeType === Node.ELEMENT_NODE ?+ range.startContainer as Element : range.startContainer.parentElement);+ if (!rangeElement || rangeElement.closest("[contenteditable]") !== editable ||+ !editable.contains(range.endContainer)) {+ return;+ }+ range.collapse(true);+ // 在键盘唤起和视口调整前记录字符位置,避免用变化后的页面布局再次命中坐标。+ const activeElement = ownerDocument.activeElement;+ const startedUnfocused = !element.contains(activeElement) ||+ (!editable.contains(activeElement) && !activeElement.contains(editable));+ touchCaret = {editable, range, character: getTouchCharacterRange(range, point), startedUnfocused, time: Date.now()};+ };+ const disposeNavigation = bindTouchNavigation(element, (_target, point) => {+ cancel();+ const caret = touchCaret;+ touchCaret = undefined;+ if (!caret || composing || !canPlaceCaret() || !caret.range.startContainer.isConnected ||+ !element.contains(caret.editable)) {+ return;+ }+ const time = Date.now();+ const repeated = previousTap && previousTap.editable === caret.editable &&+ caret.time - previousTap.time < Constants.TIMEOUT_LONGPRESS &&+ Math.hypot(point.x - previousTap.x, point.y - previousTap.y) < Constants.SIZE_DRAG_THRESHOLD * 4;+ previousTap = {...(repeated ? previousTap : caret), time, ...point, count: repeated ? previousTap.count + 1 : 1};+ // 三击及更多次连续点击保留系统的整段选择行为。+ if (previousTap.count > 2) {+ return;+ }+ const activeElement = ownerDocument.activeElement;+ if (!element.contains(activeElement) ||+ (!caret.editable.contains(activeElement) && !activeElement.contains(caret.editable))) {+ caret.editable.focus({preventScroll: true});+ focusByRange(previousTap.range);+ }+ pendingTap = previousTap;+ schedule();+ }, () => !!touchCaret);+ const cancelEvents = ["pointerdown", "touchcancel", "beforeinput", "keydown", "scroll"];+ cancelEvents.forEach(type => element.addEventListener(type, cancel, {capture: true, passive: true}));+ element.addEventListener("touchstart", touchStart, {capture: true, passive: true});+ element.addEventListener("compositionstart", compositionStart);+ element.addEventListener("compositionend", compositionEnd);+ element.addEventListener("input", input);+ element.addEventListener("click", click);+ element.addEventListener("focusout", focusOut);+ ownerDocument.addEventListener("selectionchange", selectionChange);+ return () => {+ cancel();+ disposeNavigation();+ cancelEvents.forEach(type => element.removeEventListener(type, cancel, true));+ element.removeEventListener("touchstart", touchStart, true);+ element.removeEventListener("compositionstart", compositionStart);+ element.removeEventListener("compositionend", compositionEnd);+ element.removeEventListener("input", input);+ element.removeEventListener("click", click);+ element.removeEventListener("focusout", focusOut);+ ownerDocument.removeEventListener("selectionchange", selectionChange);+ };+};diff --git a/app/src/protyle/wysiwyg/touchNavigation.test.ts b/app/src/protyle/wysiwyg/touchNavigation.test.tsindex 52f564c86e..1ce1beb40d 100644--- a/app/src/protyle/wysiwyg/touchNavigation.test.ts+++ b/app/src/protyle/wysiwyg/touchNavigation.test.ts@@ -8,7 +8,7 @@ const compiled = transpileModule(readFileSync("src/protyle/wysiwyg/touchNavigaticompilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},}).outputText;-const fixture = (handled = false) => {+const fixture = (handled = false, canTapTarget?: (target: unknown) => boolean) => {const exports: any = {};let time = 0;const targets: unknown[] = [];@@ -25,7 +25,7 @@ const fixture = (handled = false) => {targets.push(target);points.push(point);return handled;- });+ }, canTapTarget);const target = {closest: () => element};const send = (type: string, options: any = {}) => {const touch = {identifier: 1, clientX: 100, clientY: 100, ...options.point};@@ -85,3 +85,26 @@ test("multi-touch and gestures handled by other controls do not record navigatiof.send("touchend", {defaultPrevented: true});assert.deepEqual(f.targets, []);});++test("delayed event delivery uses touch timestamps while actual long presses stay native", () => {+ const f = fixture();+ f.send("touchstart", {timeStamp: 100});+ f.advance();+ f.send("touchend", {timeStamp: 150});+ assert.equal(f.targets.length, 1);+ f.send("touchstart", {timeStamp: 200});+ f.send("touchend", {timeStamp: 700});+ assert.equal(f.targets.length, 1);+});++test("noneditable targets are tracked only when the caller explicitly accepts them", () => {+ const accepted = fixture(false, () => true);+ accepted.target.closest = () => null;+ accepted.send("touchstart");+ accepted.send("touchend");+ assert.equal(accepted.targets.length, 1);+ const rejected = fixture(false, () => false);+ rejected.send("touchstart");+ rejected.send("touchend");+ assert.deepEqual(rejected.targets, []);+});diff --git a/app/src/protyle/wysiwyg/touchNavigation.ts b/app/src/protyle/wysiwyg/touchNavigation.tsindex 0dc9a3bbd8..fc2750fe57 100644--- a/app/src/protyle/wysiwyg/touchNavigation.ts+++ b/app/src/protyle/wysiwyg/touchNavigation.ts@@ -1,33 +1,36 @@import {Constants} from "../../constants";export const bindTouchNavigation = (element: HTMLElement,- onTap: (target: HTMLElement, point: {x: number, y: number}) => boolean | void) => {+ onTap: (target: HTMLElement, point: {x: number, y: number}) => boolean | void,+ canTapTarget = (target: HTMLElement) => !!target.closest('[contenteditable="true"]')) => {let start: {id: number, x: number, y: number, time: number, target: HTMLElement} | undefined;- element.addEventListener("touchstart", (event: TouchEvent) => {+ // 触摸时长使用事件时间戳,不把键盘动画造成的事件投递延迟计入长按。+ const eventTime = (event: TouchEvent) => Number.isFinite(event.timeStamp) ? event.timeStamp : Date.now();+ const touchStart = (event: TouchEvent) => {start = undefined;const target = event.target as HTMLElement;if (event.defaultPrevented || event.touches.length !== 1 ||- !target.closest('[contenteditable="true"]')) {+ !canTapTarget(target)) {return;}const touch = event.touches[0];- start = {id: touch.identifier, x: touch.clientX, y: touch.clientY, time: Date.now(), target};- }, {passive: true});- element.addEventListener("touchmove", (event: TouchEvent) => {+ start = {id: touch.identifier, x: touch.clientX, y: touch.clientY, time: eventTime(event), target};+ };+ const touchMove = (event: TouchEvent) => {const touch = start && Array.from(event.touches).find(item => item.identifier === start.id);if (!touch || event.touches.length !== 1 ||Math.abs(touch.clientX - start.x) >= Constants.SIZE_DRAG_THRESHOLD ||Math.abs(touch.clientY - start.y) >= Constants.SIZE_DRAG_THRESHOLD) {start = undefined;}- }, {passive: true});- element.addEventListener("touchend", (event: TouchEvent) => {+ };+ const touchEnd = (event: TouchEvent) => {const tap = start;start = undefined;const touch = tap && Array.from(event.changedTouches).find(item => item.identifier === tap.id);// 原生触摸不一定合成点击事件,短按结束时直接记录;滚动、长按和多指手势不进入导航历史。if (!touch || event.defaultPrevented || event.touches.length > 0 ||- Date.now() - tap.time >= Constants.TIMEOUT_LONGPRESS ||+ eventTime(event) - tap.time >= Constants.TIMEOUT_LONGPRESS ||Math.abs(touch.clientX - tap.x) >= Constants.SIZE_DRAG_THRESHOLD ||Math.abs(touch.clientY - tap.y) >= Constants.SIZE_DRAG_THRESHOLD) {return;@@ -36,8 +39,19 @@ export const bindTouchNavigation = (element: HTMLElement,// 已处理链接导航,阻止浏览器随后合成点击而重复打开。event.preventDefault();}- }, {passive: false});- element.addEventListener("touchcancel", () => {+ };+ const touchCancel = () => {start = undefined;- }, {passive: true});+ };+ element.addEventListener("touchstart", touchStart, {passive: true});+ element.addEventListener("touchmove", touchMove, {passive: true});+ element.addEventListener("touchend", touchEnd, {passive: false});+ element.addEventListener("touchcancel", touchCancel, {passive: true});+ return () => {+ start = undefined;+ element.removeEventListener("touchstart", touchStart);+ element.removeEventListener("touchmove", touchMove);+ element.removeEventListener("touchend", touchEnd);+ element.removeEventListener("touchcancel", touchCancel);+ };};diff --git a/app/tests/iosTouchCaret.test.js b/app/tests/iosTouchCaret.test.jsnew file mode 100644index 0000000000..47eed937ec--- /dev/null+++ b/app/tests/iosTouchCaret.test.js@@ -0,0 +1,415 @@+const assert = require("node:assert/strict");+const {readFileSync, mkdtempSync, rmSync} = require("node:fs");+const path = require("node:path");+const os = require("node:os");++const rendererSource = () => {+ const ts = require("typescript");+ const read = name => readFileSync(path.join(__dirname, "../src/protyle", `${name}.ts`), "utf8");+ const selection = ts.createSourceFile("selection.ts", read("util/selection"), ts.ScriptTarget.Latest, true);+ const focus = selection.statements.find(statement => ts.isVariableStatement(statement) &&+ statement.declarationList.declarations.some(declaration => declaration.name.getText(selection) === "focusByRange"));+ assert.ok(focus);+ const source = "const Constants = {SIZE_DRAG_THRESHOLD: 5, TIMEOUT_LONGPRESS: 460};\n" ++ "const revealTabsForTarget = () => {};\n" + focus.getText(selection) + "\n" ++ ["wysiwyg/compositionInput", "wysiwyg/touchNavigation", "wysiwyg/touchCaret"].map(name => {+ const parsed = ts.createSourceFile(name + ".ts", read(name), ts.ScriptTarget.Latest, true);+ return parsed.statements.filter(statement => !ts.isImportDeclaration(statement))+ .map(statement => statement.getText(parsed)).join("\n");+ }).join("\n");+ return ts.transpileModule(source, {compilerOptions: {+ module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2021,+ }}).outputText;+};++const runCases = async () => {+ const check = require("node:assert/strict");+ const {bindIOSTouchCaret} = window.touchCaret;+ const selection = document.getSelection();+ let time = 1000;+ const originalNow = Date.now;+ Date.now = () => time;+ const nextFrame = () => new Promise(resolve => requestAnimationFrame(resolve));+ const errors = [];+ const onError = event => errors.push(event.message);+ window.addEventListener("error", onError);+ let cases = 0;+ const fixture = (focused = true) => {+ document.body.innerHTML = `<div class="protyle-wysiwyg" contenteditable="false" style="font: 20px monospace">+ <div data-node-id="one"><div contenteditable="true">97997974949464649</div></div>+ <div data-node-id="two"><div contenteditable="true">点击定位文字</div></div>+ </div><input id="outside">`;+ const root = document.querySelector(".protyle-wysiwyg");+ const editable = root.querySelector('[contenteditable="true"]');+ const text = editable.firstChild;+ let enabled = true;+ const dispose = bindIOSTouchCaret(root, () => enabled);+ const place = (offset, node = text, end = offset) => {+ const range = document.createRange();+ range.setStart(node, offset);+ range.setEnd(node, end);+ selection.removeAllRanges();+ selection.addRange(range);+ };+ const pointAt = (offset, node = text) => {+ const range = document.createRange();+ range.setStart(node, offset);+ range.setEnd(node, offset + 1);+ const rect = range.getBoundingClientRect();+ return {clientX: rect.left + 0.1, clientY: (rect.top + rect.bottom) / 2};+ };+ const send = (type, point, target = editable, options = {}) => {+ const touch = new Touch({identifier: 1, target, ...point});+ const event = new TouchEvent(type, {bubbles: true, cancelable: true,+ touches: type === "touchend" || type === "touchcancel" ? [] : [touch], changedTouches: [touch], ...options});+ Object.defineProperty(event, "timeStamp", {value: time});+ target.dispatchEvent(event);+ return event;+ };+ const tap = (offset, target = editable, node = text) => {+ const point = pointAt(offset, node);+ send("touchstart", point, target);+ return send("touchend", point, target);+ };+ const caret = (offset, node = text) => {+ check.equal(selection.isCollapsed, true);+ check.equal(selection.anchorNode, node);+ check.equal(selection.anchorOffset, offset);+ };+ if (focused) {+ editable.focus();+ place(text.length);+ } else {+ selection.removeAllRanges();+ }+ return {root, editable, text, place, pointAt, send, tap, caret, dispose,+ disable: () => { enabled = false; }};+ };+ const run = async (callback, focused = true) => {+ time += 1000;+ const f = fixture(focused);+ try {+ await callback(f);+ cases++;+ } finally {+ f.dispose();+ }+ };+ try {+ await run(async f => {+ check.equal(selection.rangeCount, 0);+ f.tap(4);+ check.equal(document.activeElement, f.editable);+ f.caret(4);+ await nextFrame();+ f.caret(4);+ time += 100;+ f.tap(4);+ await nextFrame();+ check.equal(selection.toString(), f.text.textContent);+ }, false);+ await run(async f => {+ f.disable();+ f.tap(4);+ await nextFrame();+ check.equal(document.activeElement, document.body);+ check.equal(selection.rangeCount, 0);+ }, false);+ await run(async f => {+ const point = f.pointAt(4);+ f.send("touchstart", point);+ f.root.style.marginTop = "100px";+ f.send("touchend", point);+ await nextFrame();+ f.caret(4);+ }, false);+ await run(async f => {+ const point = f.pointAt(4);+ f.send("touchstart", point);+ f.root.style.paddingTop = "100px";+ f.send("touchend", point);+ await nextFrame();+ time += 100;+ f.send("touchstart", point, f.root);+ f.send("touchend", point, f.root);+ await nextFrame();+ check.equal(selection.toString(), f.text.textContent);+ }, false);+ await run(async f => {+ const point = f.pointAt(4);+ f.send("touchstart", point);+ f.root.style.paddingTop = "100px";+ f.send("touchend", point);+ await nextFrame();+ f.editable.blur();+ selection.removeAllRanges();+ const event = new MouseEvent("click", {bubbles: true, cancelable: true, detail: 1, ...point});+ f.root.dispatchEvent(event);+ check.equal(event.defaultPrevented, true);+ await nextFrame();+ check.equal(document.activeElement, f.editable);+ f.caret(4);+ }, false);+ await run(async f => {+ f.text.textContent = "alpha beta";+ const character = document.createRange();+ character.setStart(f.text, 4);+ character.setEnd(f.text, 5);+ const rect = character.getBoundingClientRect();+ const point = {clientX: rect.right - 0.1, clientY: (rect.top + rect.bottom) / 2};+ f.send("touchstart", point);+ f.send("touchend", point);+ await nextFrame();+ time += 100;+ f.send("touchstart", point);+ f.send("touchend", point);+ await nextFrame();+ check.equal(selection.toString(), "alpha");+ });+ await run(async f => {+ f.tap(4);+ await nextFrame();+ let changed = new Promise(resolve => document.addEventListener("selectionchange", resolve, {once: true}));+ f.place(f.text.length);+ await changed;+ await nextFrame();+ f.caret(4);+ time += 100;+ f.tap(4);+ await nextFrame();+ changed = new Promise(resolve => document.addEventListener("selectionchange", resolve, {once: true}));+ f.place(f.text.length);+ await changed;+ await nextFrame();+ check.equal(selection.toString(), f.text.textContent);+ });+ await run(async f => {+ f.editable.innerHTML = "hel<b>lo</b> world";+ const target = f.editable.querySelector("b");+ const text = target.firstChild;+ f.tap(1, target, text);+ await nextFrame();+ time += 100;+ f.tap(1, target, text);+ await nextFrame();+ check.equal(selection.toString(), "hello");+ });+ await run(async f => {+ const editable = f.root.querySelectorAll('[contenteditable="true"]')[1];+ const text = editable.firstChild;+ f.tap(2, editable, text);+ await nextFrame();+ time += 100;+ f.tap(2, editable, text);+ await nextFrame();+ check.ok(selection.toString().includes("定"));+ check.ok(editable.contains(selection.anchorNode));+ check.ok(editable.contains(selection.focusNode));+ }, false);+ await run(async f => {+ f.text.textContent = "a 😀 b";+ f.tap(2);+ await nextFrame();+ time += 100;+ f.tap(2);+ await nextFrame();+ check.equal(selection.toString(), "😀");+ });+ await run(async f => {+ f.text.textContent = "hel";+ f.editable.insertAdjacentHTML("beforeend", "<span data-type='a'>lo</span>");+ f.tap(1);+ await nextFrame();+ time += 100;+ f.tap(1);+ await nextFrame();+ check.equal(selection.isCollapsed, true);+ });+ await run(async f => {+ check.equal(f.tap(4).defaultPrevented, false);+ f.place(f.text.length);+ await nextFrame();+ f.caret(4);+ // 快速点击不同字符时,不能将第二次定位误判为双击。+ time += 100;+ f.tap(11);+ f.place(f.text.length);+ await nextFrame();+ f.caret(11);+ });+ await run(async f => {+ const editable = f.root.querySelectorAll('[contenteditable="true"]')[1];+ const text = editable.firstChild;+ f.tap(2, editable, text);+ editable.focus();+ f.place(text.length, text);+ await nextFrame();+ f.caret(2, text);+ });+ await run(async f => {+ f.root.setAttribute("contenteditable", "true");+ f.root.focus();+ f.tap(4);+ f.place(f.text.length);+ await nextFrame();+ f.caret(4);+ });+ await run(async f => {+ const point = f.pointAt(4);+ f.tap(4);+ await nextFrame();+ f.place(f.text.length);+ time += 100;+ f.editable.dispatchEvent(new MouseEvent("click", {bubbles: true, detail: 1, ...point}));+ await nextFrame();+ f.caret(4);+ });+ await run(async f => {+ f.tap(4);+ await nextFrame();+ time += 100;+ f.tap(4);+ f.place(0, f.text, f.text.length);+ await nextFrame();+ check.equal(selection.toString(), f.text.textContent);+ });+ await run(async f => {+ f.tap(4);+ f.place(2, f.text, 6);+ await nextFrame();+ check.equal(selection.toString(), f.text.textContent.slice(2, 6));+ });+ for (const kind of ["longpress", "scroll", "cancel", "multitouch"]) {+ await run(async f => {+ const point = f.pointAt(4);+ f.send("touchstart", point);+ if (kind === "longpress") {+ time += 500;+ } else if (kind === "scroll") {+ f.send("touchmove", {...point, clientY: point.clientY + 10});+ } else if (kind === "cancel") {+ f.send("touchcancel", point);+ } else {+ const touch = new Touch({identifier: 2, target: f.editable, ...point});+ f.send("touchstart", point, f.editable, {touches: [touch, touch]});+ }+ f.send("touchend", point);+ await nextFrame();+ f.caret(f.text.length);+ });+ }+ for (const kind of ["disabled", "removed", "outside", "disposed", "beforeinput", "keydown", "scroll"]) {+ await run(async f => {+ f.tap(4);+ if (kind === "disabled") {+ f.disable();+ } else if (kind === "removed") {+ f.editable.remove();+ } else if (kind === "outside") {+ document.getElementById("outside").focus();+ } else if (kind === "disposed") {+ f.dispose();+ } else {+ f.editable.dispatchEvent(new Event(kind, {bubbles: true}));+ }+ const node = selection.anchorNode;+ const offset = selection.anchorOffset;+ await nextFrame();+ check.equal(selection.anchorNode, node);+ check.equal(selection.anchorOffset, offset);+ });+ }+ await run(async f => {+ f.editable.dispatchEvent(new CompositionEvent("compositionstart", {bubbles: true}));+ f.tap(4);+ await nextFrame();+ f.caret(f.text.length);+ f.editable.dispatchEvent(new InputEvent("input", {bubbles: true, isComposing: false, inputType: "insertText"}));+ time += 1000;+ f.tap(4);+ await nextFrame();+ f.caret(4);+ });+ for (const markup of ["<a href='#'>link</a>", "<span data-type='block-ref'>reference</span>",+ "<span data-type='inline-math'>math</span>", "<span contenteditable='false'>readonly</span>"]) {+ await run(async f => {+ f.editable.insertAdjacentHTML("beforeend", markup);+ const target = f.editable.lastElementChild;+ const point = f.pointAt(4);+ f.send("touchstart", point, target);+ f.send("touchend", point, target);+ await nextFrame();+ f.caret(f.text.length);+ });+ }+ await run(async f => {+ const caretRangeFromPoint = document.caretRangeFromPoint;+ try {+ document.caretRangeFromPoint = () => null;+ f.tap(4);+ await nextFrame();+ f.caret(f.text.length);+ } finally {+ document.caretRangeFromPoint = caretRangeFromPoint;+ }+ });+ await run(async f => {+ const text = f.root.querySelectorAll('[contenteditable="true"]')[1].firstChild;+ const point = f.pointAt(2, text);+ f.send("touchstart", point);+ f.send("touchend", point);+ await nextFrame();+ f.caret(f.text.length);+ });+ await nextFrame();+ check.deepEqual(errors, []);+ return cases;+ } finally {+ Date.now = originalNow;+ window.removeEventListener("error", onError);+ }+};++const runElectron = async () => {+ const {app, BrowserWindow} = require("electron");+ app.setPath("userData", process.argv[2]);+ app.commandLine.appendSwitch("disable-gpu");+ await app.whenReady();+ const win = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true, contextIsolation: false}});+ let exitCode = 0;+ try {+ await win.loadURL("data:text/html,<html><body></body></html>");+ await win.webContents.executeJavaScript(`window.touchCaret = {};+ new Function("exports", ${JSON.stringify(rendererSource())})(window.touchCaret);`);+ const cases = await win.webContents.executeJavaScript(`(${runCases.toString()})()`);+ console.log(`iOS touch caret: ${cases} DOM cases passed`);+ } catch (error) {+ console.error(error);+ exitCode = 1;+ } finally {+ win.destroy();+ app.exit(exitCode);+ }+};++if (process.versions.electron && process.type === "browser") {+ runElectron().catch(error => { console.error(error); require("electron").app.exit(1); });+} else {+ require("node:test").test("places iOS short-tap carets at character coordinates and preserves native selection gestures", {+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,+ timeout: 45000,+ }, async () => {+ const profile = mkdtempSync(path.join(os.tmpdir(), "siyuan-ios-touch-caret-"));+ const env = {...process.env};+ delete env.ELECTRON_RUN_AS_NODE;+ try {+ const {stdout} = await require("node:util").promisify(require("node:child_process").execFile)(+ require("electron"), [__filename, profile], {env, windowsHide: true, timeout: 40000});+ assert.match(stdout, /iOS touch caret: 35 DOM cases passed/);+ } finally {+ assert.equal(path.dirname(path.resolve(profile)), path.resolve(os.tmpdir()));+ assert.ok(path.basename(profile).startsWith("siyuan-ios-touch-caret-"));+ rmSync(profile, {recursive: true, force: true, maxRetries: 5, retryDelay: 100});+ }+ });+}