Skip to content

Commit ff909fa

Browse files
committed
🎨 #15805
1 parent bfe50d9 commit ff909fa

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

app/src/protyle/hint/index.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -559,16 +559,12 @@ ${genHintItemHTML(item)}
559559
}, response => {
560560
// https://github.com/siyuan-note/siyuan/issues/10133
561561
protyle.toolbar.range = range;
562-
protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
562+
const refElement = protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
563563
type: "id",
564564
color: `${response.data}${Constants.ZWSP}${refIsS ? "s" : "d"}${Constants.ZWSP}${(refIsS ? fileNames[0] : realFileName).substring(0, window.siyuan.config.editor.blockRefDynamicAnchorTextMaxLen)}`
565565
});
566-
if (protyle.toolbar.range.endContainer.nodeType === 1 &&
567-
protyle.toolbar.range.endContainer.childNodes[protyle.toolbar.range.endOffset]) {
568-
const refElement = hasPreviousSibling(protyle.toolbar.range.endContainer.childNodes[protyle.toolbar.range.endOffset]) as HTMLElement;
569-
if (refElement && refElement.nodeType === 1 && refElement.getAttribute("data-type") === "block-ref") {
570-
setLastNodeRange(refElement as HTMLElement, protyle.toolbar.range, false);
571-
}
566+
if (refElement[0]) {
567+
protyle.toolbar.range.setEnd(refElement[0].lastChild, refElement[0].lastChild.textContent.length);
572568
}
573569
protyle.toolbar.range.collapse(false);
574570
});
@@ -600,16 +596,12 @@ ${genHintItemHTML(item)}
600596
tempElement.innerText = dynamicTexts[1];
601597
}
602598
}
603-
protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
599+
const refElement = protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
604600
type: "id",
605601
color: `${tempElement.getAttribute("data-id")}${Constants.ZWSP}${tempElement.getAttribute("data-subtype")}${Constants.ZWSP}${tempElement.textContent}`
606602
});
607-
if (protyle.toolbar.range.endContainer.nodeType === 1 &&
608-
protyle.toolbar.range.endContainer.childNodes[protyle.toolbar.range.endOffset]) {
609-
const refElement = hasPreviousSibling(protyle.toolbar.range.endContainer.childNodes[protyle.toolbar.range.endOffset]) as HTMLElement;
610-
if (refElement && refElement.nodeType === 1 && refElement.getAttribute("data-type") === "block-ref") {
611-
setLastNodeRange(refElement as HTMLElement, protyle.toolbar.range, false);
612-
}
603+
if (refElement[0]) {
604+
protyle.toolbar.range.setEnd(refElement[0].lastChild, refElement[0].lastChild.textContent.length);
613605
}
614606
protyle.toolbar.range.collapse(false);
615607
return;

app/src/protyle/util/paste.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,13 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
386386
}
387387

388388
if (types.includes("block-ref")) {
389-
protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
389+
const refElement = protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
390390
type: "id",
391391
color: `${linkElement.dataset.id}${Constants.ZWSP}s${Constants.ZWSP}${range.toString()}`
392392
});
393+
if (refElement[0]) {
394+
protyle.toolbar.range.selectNodeContents(refElement[0]);
395+
}
393396
return;
394397
}
395398
if (types.includes("a")) {
@@ -508,12 +511,12 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
508511
}
509512
if (linkElement) {
510513
const selectText = range.toString();
511-
const aElements = protyle.toolbar.setInlineMark(protyle, "a", "range", {
514+
const aElements = protyle.toolbar.setInlineMark(protyle, "a", "range", {
512515
type: "a",
513516
color: `${linkElement.getAttribute("href")}${Constants.ZWSP}${selectText || linkElement.textContent}`
514517
});
515518
if (!selectText) {
516-
if(aElements[0].lastChild) {
519+
if (aElements[0].lastChild) {
517520
// https://github.com/siyuan-note/siyuan/issues/15801
518521
range.setEnd(aElements[0].lastChild, aElements[0].lastChild.textContent.length);
519522
}
@@ -546,11 +549,14 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
546549
if (range.toString() !== "") {
547550
const firstLine = textPlain.split("\n")[0];
548551
if (isDynamicRef(textPlain)) {
549-
protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
552+
const refElement = protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
550553
type: "id",
551554
// range 不能 escape,否则 https://github.com/siyuan-note/siyuan/issues/8359
552555
color: `${textPlain.substring(2, 22 + 2)}${Constants.ZWSP}s${Constants.ZWSP}${range.toString()}`
553556
});
557+
if (refElement[0]) {
558+
protyle.toolbar.range.selectNodeContents(refElement[0]);
559+
}
554560
return;
555561
} else if (isFileAnnotation(firstLine)) {
556562
protyle.toolbar.setInlineMark(protyle, "file-annotation-ref", "range", {

app/src/util/newFile.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,21 +249,27 @@ export const newFileBySelect = (protyle: IProtyle, selectText: string, nodeEleme
249249
}, (idResponse) => {
250250
const refText = newFileName.substring(0, window.siyuan.config.editor.blockRefDynamicAnchorTextMaxLen);
251251
if (idResponse.data && idResponse.data.length > 0) {
252-
protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
252+
const refElement = protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
253253
type: "id",
254254
color: `${idResponse.data[0]}${Constants.ZWSP}d${Constants.ZWSP}${refText}`
255255
});
256+
if (refElement[0]) {
257+
protyle.toolbar.range.selectNodeContents(refElement[0]);
258+
}
256259
} else {
257260
fetchPost("/api/filetree/createDocWithMd", {
258261
notebook: targetNotebookId,
259262
path: hPath,
260263
parentID: protyle.notebookId === targetNotebookId ? protyle.block.rootID : "",
261264
markdown: ""
262265
}, response => {
263-
protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
266+
const refElement = protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
264267
type: "id",
265268
color: `${response.data}${Constants.ZWSP}d${Constants.ZWSP}${refText}`
266269
});
270+
if (refElement[0]) {
271+
protyle.toolbar.range.selectNodeContents(refElement[0]);
272+
}
267273
});
268274
}
269275
hideElements(["toolbar"], protyle);

0 commit comments

Comments
 (0)