Skip to content

Commit

Permalink
按照视觉上的呈现来获取文本
Browse files Browse the repository at this point in the history
  • Loading branch information
TCOTC committed Mar 26, 2024
1 parent caab664 commit 8f969c7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "siyuan-plugin-hsr-mdzz2048-fork",
"author": "TCOTC",
"url": "https://github.com/TCOTC/siyuan-plugin-hsr-mdzz2048-fork",
"version": "0.0.9",
"version": "0.0.10",
"minAppVersion": "2.9.8",
"backends": ["windows", "linux", "darwin"],
"frontends": ["desktop", "browser-desktop"],
Expand Down
26 changes: 25 additions & 1 deletion src/Search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function highlightHitResult(value: string) {
const str = value.trim().toLowerCase()
if (!str) return
/*
// 查找所有文本节点是否包含搜索词
const ranges = allTextNodes
.map((el) => {
Expand All @@ -116,7 +117,30 @@ function highlightHitResult(value: string) {
return range
})
})
*/
// 替换为:
// 查找所有文本节点是否包含搜索词,并创建对应的 Range 对象
let ranges = [];
allTextNodes.forEach((node) => {
const textContent = node.textContent.toLowerCase();
let startIndex = 0;
while ((startIndex = textContent.indexOf(str, startIndex)) !== -1) {
const range = document.createRange();
try {
range.setStart(node, startIndex);
range.setEnd(node, startIndex + str.length);
ranges.push(range);
} catch (error) {
console.error("Error setting range in node:", node, error);
}
startIndex += str.length;
}
});
// 创建高亮对象
const searchResultsHighlight = new Highlight(...ranges.flat())
resultCount.value = ranges.flat().length
Expand Down

0 comments on commit 8f969c7

Please sign in to comment.