Skip to content

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

:art: fix https://github.com/siyuan-note/siyuan/issues/8196

Vanessa0df92e59fa52
diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts
index e00e3c7fc1..1e00d4017d 100644
--- a/app/src/protyle/util/editorCommonEvent.ts
+++ b/app/src/protyle/util/editorCommonEvent.ts
@@ -1,4 +1,4 @@
-import {focusBlock, focusByRange} from "./selection";
+import {focusBlock, focusByRange, getRangeByPoint} from "./selection";
import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName, hasClosestByTag} from "./hasClosest";
import {Constants} from "../../constants";
import {paste} from "./paste";
@@ -740,7 +740,7 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
const gutterTypes = gutterType.replace(Constants.SIYUAN_DROP_GUTTER, "").split(Constants.ZWSP);
const selectedIds = gutterTypes[2].split(",");
if (event.altKey) {
- focusByRange(document.caretRangeFromPoint(event.clientX, event.clientY));
+ focusByRange(getRangeByPoint(event.clientX, event.clientY));
let html = "";
for (let i = 0; i < selectedIds.length; i++) {
const response = await fetchSyncPost("/api/block/getRefText", {id: selectedIds[i]});
@@ -748,7 +748,7 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
}
insertHTML(html, protyle);
} else if (event.shiftKey) {
- focusByRange(document.caretRangeFromPoint(event.clientX, event.clientY));
+ focusByRange(getRangeByPoint(event.clientX, event.clientY));
let html = "";
selectedIds.forEach(item => {
html += `{{select * from blocks where id='${item}'}}\n`;
@@ -866,7 +866,7 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
targetElement.classList.remove("dragover__bottom", "dragover__top");
} else if (!window.siyuan.dragElement && (event.dataTransfer.types[0] === "Files" || event.dataTransfer.types.includes("text/html"))) {
// 外部文件拖入编辑器中或者编辑器内选中文字拖拽
- focusByRange(document.caretRangeFromPoint(event.clientX, event.clientY));
+ focusByRange(getRangeByPoint(event.clientX, event.clientY));
if (event.dataTransfer.types[0] === "Files" && !isBrowser()) {
const files: string[] = [];
for (let i = 0; i < event.dataTransfer.files.length; i++) {
diff --git a/app/src/protyle/util/selection.ts b/app/src/protyle/util/selection.ts
index 1fb0bb825a..4033490af0 100644
--- a/app/src/protyle/util/selection.ts
+++ b/app/src/protyle/util/selection.ts
@@ -125,6 +125,17 @@ export const selectAll = (protyle: IProtyle, nodeElement: Element, range: Range)
countBlockWord(ids, protyle.block.rootID);
};
+// https://github.com/siyuan-note/siyuan/issues/8196
+export const getRangeByPoint = (x: number, y: number) => {
+ const range = document.caretRangeFromPoint(x, y);
+ const imgElement = hasClosestByAttribute(range.startContainer, "data-type", "img")
+ if (imgElement) {
+ range.setStart(imgElement.nextSibling, 0);
+ range.collapse();
+ }
+ return range;
+}
+
export const getEditorRange = (element: Element) => {
let range: Range;
if (getSelection().rangeCount > 0) {