Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: Improve photo insertion positioning in nested lists on mobile https://github.com/siyuan-note/siyuan/issues/18690

Daniel031f48e702f7
diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.ts
index 5a2a160ebc..2fa8e4e896 100644
--- a/app/src/protyle/hint/index.ts
+++ b/app/src/protyle/hint/index.ts
@@ -44,7 +44,8 @@ import {
unicode2Emoji,
} from "../../emoji";
import {blockRender} from "../render/blockRender";
-import {uploadFiles} from "../upload";
+import {getUploadInsertRange, uploadFiles} from "../upload";
+import {createUploadInsertPosition} from "../upload/insertPosition";
/// #if !MOBILE
import {openFileById} from "../../editor/util";
/// #endif
@@ -344,17 +345,34 @@ export class Hint {
public bindUploadEvent(protyle: IProtyle, element: HTMLElement) {
element.querySelectorAll('input[type="file"]').forEach(item => {
- item.addEventListener("change", (event: InputEvent & { target: HTMLInputElement }) => {
- if (event.target.files.length === 0) {
- return;
+ const captureInsertPosition = () => {
+ let range = protyle.toolbar.range;
+ if (!range || !protyle.wysiwyg.element.contains(range.startContainer) ||
+ !protyle.wysiwyg.element.contains(range.endContainer)) {
+ range = getEditorRange(protyle.wysiwyg.element);
}
- const range = getEditorRange(protyle.wysiwyg.element);
+ range = range.cloneRange();
if (this.lastIndex > -1) {
range.setStart(range.startContainer, this.lastIndex);
}
+ return createUploadInsertPosition(range,
+ getUndoFocusContext(protyle.wysiwyg.element, range, true));
+ };
+ let insertPosition = captureInsertPosition();
+ item.addEventListener("click", () => {
+ insertPosition = captureInsertPosition();
+ });
+ item.addEventListener("change", (event: InputEvent & { target: HTMLInputElement }) => {
+ if (event.target.files.length === 0) {
+ return;
+ }
+ const range = getUploadInsertRange(protyle, insertPosition);
range.deleteContents();
+ range.collapse(true);
uploadFiles(protyle, event.target.files, event.target, undefined, undefined, {
htmlAsIframe: event.target.dataset.uploadMode === "html-iframe",
+ insertPosition: createUploadInsertPosition(range,
+ getUndoFocusContext(protyle.wysiwyg.element, range, true)),
});
hideElements(["hint", "toolbar"], protyle);
});
diff --git a/app/src/protyle/upload/index.ts b/app/src/protyle/upload/index.ts
index 7b44f56092..5be2051223 100644
--- a/app/src/protyle/upload/index.ts
+++ b/app/src/protyle/upload/index.ts
@@ -4,7 +4,7 @@ import {Constants} from "../../constants";
import {destroy} from "../util/destroy";
import {escapeHtml} from "../../util/escape";
import {fetchPost} from "../../util/fetch";
-import {getEditorRange} from "../util/selection";
+import {getEditorRange, restoreFocusContext} from "../util/selection";
import {pathPosix} from "../../util/pathName";
import {genAssetHTML} from "../../asset/renderAssets";
import {hasClosestBlock, hasClosestByClassName} from "../util/hasClosest";
@@ -17,6 +17,8 @@ import {transaction} from "../wysiwyg/transaction";
import * as dayjs from "dayjs";
import {getAVSelectedCells} from "../render/av/selectionState";
import {getAVSelectedTableCells} from "../render/av/virtualScroll";
+import {isUploadInsertPositionAvailable} from "./insertPosition";
+import type {IUploadInsertPosition} from "./insertPosition";
interface FileWithPath extends File {
path: string;
@@ -24,6 +26,7 @@ interface FileWithPath extends File {
export interface IUploadInsertOptions {
htmlAsIframe?: boolean;
+ insertPosition?: IUploadInsertPosition;
}
export class Upload {
@@ -116,7 +119,7 @@ const genUploadedLabel = async (responseText: string, protyle: IProtyle, options
showMessage(errorTip);
}
let insertBlock = true;
- const range = getEditorRange(protyle.wysiwyg.element);
+ const range = getUploadInsertRange(protyle, options?.insertPosition);
if (range.toString() === "" && range.startContainer.nodeType === 3 && protyle.toolbar.getCurrentType(range).length > 0) {
// 防止链接插入其他元素中 https://ld246.com/article/1676003478664
range.setEndAfter(range.startContainer.parentElement);
@@ -265,13 +268,28 @@ const genUploadedLabel = async (responseText: string, protyle: IProtyle, options
return;
}
// 避免插入代码块中,其次因为都要独立成块 https://github.com/siyuan-note/siyuan/issues/7607
- insertHTML(successFileText, protyle, insertBlock);
+ if (options?.insertPosition) {
+ protyle.toolbar.range = range;
+ insertHTML(successFileText, protyle, insertBlock, true);
+ } else {
+ insertHTML(successFileText, protyle, insertBlock);
+ }
// 粘贴图片后定位不准确 https://github.com/siyuan-note/siyuan/issues/13336
setTimeout(() => {
scrollCenter(protyle, undefined, "nearest", "smooth");
}, hasImage ? 0 : Constants.TIMEOUT_LOAD);
};
+export const getUploadInsertRange = (protyle: IProtyle, position?: IUploadInsertPosition) => {
+ if (isUploadInsertPositionAvailable(protyle.wysiwyg.element, position)) {
+ return position.range.cloneRange();
+ }
+ if (position?.context && restoreFocusContext(protyle, position.context)) {
+ return getEditorRange(protyle.wysiwyg.element).cloneRange();
+ }
+ return getEditorRange(protyle.wysiwyg.element);
+};
+
export const uploadLocalFiles = (files: ILocalFiles[], protyle: IProtyle, isUpload: boolean,
options?: IUploadInsertOptions) => {
let msg = "";
diff --git a/app/src/protyle/upload/insertPosition.test.ts b/app/src/protyle/upload/insertPosition.test.ts
new file mode 100644
index 0000000000..e05bf2727b
--- /dev/null
+++ b/app/src/protyle/upload/insertPosition.test.ts
@@ -0,0 +1,68 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {createUploadInsertPosition, isUploadInsertPositionAvailable} from "./insertPosition";
+
+class TestElement {
+ public parent?: TestElement;
+ public children: TestElement[] = [];
+
+ append(...children: TestElement[]) {
+ children.forEach(child => {
+ child.parent = this;
+ this.children.push(child);
+ });
+ return this;
+ }
+
+ contains(node: TestElement): boolean {
+ return node === this || this.children.some(child => child.contains(node));
+ }
+}
+
+const createRange = (startContainer: TestElement, endContainer = startContainer) => {
+ const range = {
+ startContainer: startContainer as unknown as Node,
+ endContainer: endContainer as unknown as Node,
+ cloneRange() {
+ return createRange(this.startContainer as unknown as TestElement,
+ this.endContainer as unknown as TestElement);
+ },
+ } as unknown as Range;
+ return range;
+};
+
+describe("upload insert position", () => {
+ it("keeps an independent range snapshot", () => {
+ const source = new TestElement();
+ const range = createRange(source);
+ const position = createUploadInsertPosition(range, {undoFocusId: "target"});
+
+ assert.notEqual(position.range, range);
+ assert.equal(position.startContainer, source);
+ assert.deepEqual(position.context, {undoFocusId: "target"});
+ });
+
+ it("uses the live range while its original boundaries remain in the editor", () => {
+ const start = new TestElement();
+ const end = new TestElement();
+ const editor = new TestElement().append(start, end);
+ const position = createUploadInsertPosition(createRange(start, end));
+
+ assert.equal(isUploadInsertPositionAvailable(editor as unknown as Element, position), true);
+ });
+
+ it("rejects a rehomed range after its original boundary is replaced", () => {
+ const original = new TestElement();
+ const replacement = new TestElement();
+ const editor = new TestElement().append(original);
+ const position = createUploadInsertPosition(createRange(original));
+ editor.children = [replacement];
+ replacement.parent = editor;
+ (position.range as unknown as {startContainer: Node; endContainer: Node}).startContainer =
+ editor as unknown as Node;
+ (position.range as unknown as {startContainer: Node; endContainer: Node}).endContainer =
+ editor as unknown as Node;
+
+ assert.equal(isUploadInsertPositionAvailable(editor as unknown as Element, position), false);
+ });
+});
diff --git a/app/src/protyle/upload/insertPosition.ts b/app/src/protyle/upload/insertPosition.ts
new file mode 100644
index 0000000000..9705aec269
--- /dev/null
+++ b/app/src/protyle/upload/insertPosition.ts
@@ -0,0 +1,24 @@
+export interface IUploadInsertPosition {
+ range: Range;
+ startContainer: Node;
+ endContainer: Node;
+ context?: Record<string, string>;
+}
+
+export const createUploadInsertPosition = (range: Range, context?: Record<string, string>): IUploadInsertPosition => {
+ const insertRange = range.cloneRange();
+ return {
+ range: insertRange,
+ startContainer: insertRange.startContainer,
+ endContainer: insertRange.endContainer,
+ context,
+ };
+};
+
+export const isUploadInsertPositionAvailable = (editorElement: Element, position?: IUploadInsertPosition) => {
+ return !!position &&
+ editorElement.contains(position.startContainer) &&
+ editorElement.contains(position.endContainer) &&
+ editorElement.contains(position.range.startContainer) &&
+ editorElement.contains(position.range.endContainer);
+};