Vanessa 2024-12-07 23:23:59 +08:00
parent 88445d5a6c
commit 2e9c797a13
6 changed files with 29 additions and 82 deletions

View file

@ -28,6 +28,9 @@ export const searchMarkRender = (protyle: IProtyle, keys: string[], isHL: boolea
const rangeIndexes: { range: Range, startIndex: number }[] = [];
keys.forEach(key => {
if (!key) {
return;
}
let startIndex = 0;
let endIndex = 0;
let currentNodeIndex = 0;
@ -76,72 +79,6 @@ export const searchMarkRender = (protyle: IProtyle, keys: string[], isHL: boolea
}, protyle.wysiwyg.element.querySelector(".hljs") ? Constants.TIMEOUT_TRANSITION : 0);
};
export const searchTextMarkRender = (protyle: IProtyle, keys: string[], element: HTMLElement,) => {
if (!isSupportCSSHL()) {
return;
}
protyle.highlight.markHL.clear();
protyle.highlight.mark.clear();
// 准备一个数组来保存所有文本节点
const textNodes: Node[] = [];
const textNodesSize: number[] = [];
let currentSize = 0;
const treeWalker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT);
let currentNode = treeWalker.nextNode();
while (currentNode) {
textNodes.push(currentNode);
currentSize += currentNode.textContent.length
textNodesSize.push(currentSize);
currentNode = treeWalker.nextNode();
}
const text = element.textContent;
const rangeIndexes: { range: Range, startIndex: number }[] = [];
keys.forEach(key => {
let startIndex = 0;
let endIndex = 0;
let currentNodeIndex = 0;
while ((startIndex = text.indexOf(key, startIndex)) !== -1) {
const range = new Range();
endIndex = startIndex + key.length;
try {
while (currentNodeIndex < textNodes.length && textNodesSize[currentNodeIndex] <= startIndex) {
currentNodeIndex++;
}
let currentTextNode = textNodes[currentNodeIndex];
range.setStart(currentTextNode, startIndex - (currentNodeIndex ? textNodesSize[currentNodeIndex - 1] : 0));
while (currentNodeIndex < textNodes.length && textNodesSize[currentNodeIndex] < endIndex) {
currentNodeIndex++
}
currentTextNode = textNodes[currentNodeIndex]
range.setEnd(currentTextNode, endIndex - (currentNodeIndex ? textNodesSize[currentNodeIndex - 1] : 0));
rangeIndexes.push({range, startIndex});
} catch (e) {
console.error("searchMarkRender error:", e);
}
startIndex = endIndex;
}
})
rangeIndexes.sort((b, a) => {
if (a.startIndex > b.startIndex) {
return -1
} else {
return 0
}
}).forEach((item, index) => {
protyle.highlight.mark.add(item.range);
});
CSS.highlights.set("search-mark-" + protyle.highlight.styleElement.dataset.uuid, protyle.highlight.mark);
}
export const isSupportCSSHL = () => {
return !!(CSS && CSS.highlights);
}

View file

@ -53,7 +53,7 @@ export const getDocByScroll = (options: {
protyle: IProtyle,
scrollAttr?: IScrollAttr,
mergedOptions?: IProtyleOptions,
cb?: () => void
cb?: (keys: string[]) => void
focus?: boolean,
updateReadonly?: boolean
}) => {
@ -89,7 +89,9 @@ export const getDocByScroll = (options: {
protyle: options.protyle,
action: actions,
scrollAttr: options.scrollAttr,
afterCB: options.cb,
afterCB: options.cb ? () => {
options.cb(response.data.keywords);
} : undefined,
updateReadonly: options.updateReadonly
});
});
@ -100,7 +102,9 @@ export const getDocByScroll = (options: {
protyle: options.protyle,
action: actions,
scrollAttr: options.scrollAttr,
afterCB: options.cb,
afterCB: options.cb ? () => {
options.cb(response.data.keywords);
} : undefined,
updateReadonly: options.updateReadonly
});
}
@ -121,7 +125,9 @@ export const getDocByScroll = (options: {
protyle: options.protyle,
action: actions,
scrollAttr: options.scrollAttr,
afterCB: options.cb,
afterCB: options.cb ? () => {
options.cb(response.data.keywords);
} : undefined,
updateReadonly: options.updateReadonly
});
});

View file

@ -37,15 +37,16 @@ export const reloadProtyle = (protyle: IProtyle, focus: boolean, updateReadonly?
const tabElement = hasClosestByClassName(protyle.element, "sy__backlink");
if (tabElement) {
const inputsElement = tabElement.querySelectorAll(".b3-text-field") as NodeListOf<HTMLInputElement>;
const keyword = isMention ? inputsElement[1].value : inputsElement[0].value
fetchPost(isMention ? "/api/ref/getBackmentionDoc" : "/api/ref/getBacklinkDoc", {
defID: protyle.element.getAttribute("data-defid"),
refTreeID: protyle.block.rootID,
highlight: !isSupportCSSHL(),
keyword: isMention ? inputsElement[1].value : inputsElement[0].value,
keyword,
}, response => {
protyle.options.backlinkData = isMention ? response.data.backmentions : response.data.backlinks;
renderBacklink(protyle, protyle.options.backlinkData);
searchMarkRender(protyle, ["TODO"], false);
searchMarkRender(protyle, keyword.split(" "), false);
});
}
} else {
@ -55,9 +56,9 @@ export const reloadProtyle = (protyle: IProtyle, focus: boolean, updateReadonly?
focus,
scrollAttr: saveScroll(protyle, true) as IScrollAttr,
updateReadonly,
cb() {
cb(keys) {
if (protyle.query?.key) {
searchMarkRender(protyle, ["TODO"], true);
searchMarkRender(protyle, keys, true);
}
}
});