Vanessa 2025-01-17 17:59:14 +08:00
parent 0636b2e1fb
commit c3342935d6
2 changed files with 23 additions and 7 deletions

View file

@ -20,7 +20,6 @@ export const searchMarkRender = (protyle: IProtyle, keys: string[], hlId?: strin
});
}
// 准备一个数组来保存所有文本节点
const textNodes: Node[] = [];
const textNodesSize: number[] = [];
@ -75,6 +74,20 @@ export const searchMarkRender = (protyle: IProtyle, keys: string[], hlId?: strin
}
});
// 没有匹配到关键字,但是有高亮块时,需将其添加进去
if (!isSetHL && hlBlockElement) {
const startIndex = text.indexOf(hlBlockElement.textContent);
if (startIndex > -1) {
const range = new Range();
let currentNodeIndex = 0;
while (textNodes.length && textNodesSize[currentNodeIndex] <= startIndex) {
currentNodeIndex++;
}
range.setStart(textNodes[currentNodeIndex], 0);
rangeIndexes.push({range, startIndex, isCurrent: true});
}
}
rangeIndexes.sort((b, a) => {
if (a.startIndex > b.startIndex) {
return -1;

View file

@ -1019,17 +1019,21 @@ const renderNextSearchMark = (options: {
if (options.edit.protyle.highlight.rangeIndex >= options.edit.protyle.highlight.ranges.length) {
options.edit.protyle.highlight.rangeIndex = 0;
}
let rangeTop;
let currentRange: Range;
options.edit.protyle.highlight.ranges.forEach((item, index) => {
if (options.edit.protyle.highlight.rangeIndex === index) {
options.edit.protyle.highlight.markHL.add(item);
rangeTop = item.getBoundingClientRect().top;
currentRange = item
} else {
options.edit.protyle.highlight.mark.add(item);
}
});
if (typeof rangeTop === "number") {
options.edit.protyle.contentElement.scrollTop = options.edit.protyle.contentElement.scrollTop + rangeTop - contentRect.top - contentRect.height / 2;
if (currentRange) {
if (!currentRange.toString()) {
highlightById(options.edit.protyle, options.id);
} else {
options.edit.protyle.contentElement.scrollTop = options.edit.protyle.contentElement.scrollTop + currentRange.getBoundingClientRect().top - contentRect.top - contentRect.height / 2;
}
}
return;
}
@ -1102,8 +1106,7 @@ export const getArticle = (options: {
if (isSupportCSSHL()) {
searchMarkRender(options.edit.protyle, getResponse.data.keywords, options.id, () => {
if (options.edit.protyle.highlight.ranges.length > 0 && options.edit.protyle.highlight.ranges[options.edit.protyle.highlight.rangeIndex]) {
const blockElement = hasClosestBlock(options.edit.protyle.highlight.ranges[options.edit.protyle.highlight.rangeIndex].startContainer);
if (blockElement && blockElement.getAttribute("data-node-id") !== options.id) {
if (!options.edit.protyle.highlight.ranges[options.edit.protyle.highlight.rangeIndex].toString()) {
highlightById(options.edit.protyle, options.id);
} else {
options.edit.protyle.contentElement.scrollTop = options.edit.protyle.contentElement.scrollTop + options.edit.protyle.highlight.ranges[options.edit.protyle.highlight.rangeIndex].getBoundingClientRect().top - contentRect.top - contentRect.height / 2;