mirror of
https://github.com/john-okeefe/foliate-js.git
synced 2026-09-09 11:29:14 -04:00
Textwalker with optional filter func parameter (#77)
This commit is contained in:
@@ -109,7 +109,7 @@ export const search = (strs, query, options) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const searchMatcher = (textWalker, opts) => {
|
export const searchMatcher = (textWalker, opts) => {
|
||||||
const { defaultLocale, matchCase, matchDiacritics, matchWholeWords } = opts
|
const { defaultLocale, matchCase, matchDiacritics, matchWholeWords, acceptNode } = opts
|
||||||
return function* (doc, query) {
|
return function* (doc, query) {
|
||||||
const iter = textWalker(doc, function* (strs, makeRange) {
|
const iter = textWalker(doc, function* (strs, makeRange) {
|
||||||
for (const result of search(strs, query, {
|
for (const result of search(strs, query, {
|
||||||
@@ -124,7 +124,7 @@ export const searchMatcher = (textWalker, opts) => {
|
|||||||
result.range = makeRange(startIndex, startOffset, endIndex, endOffset)
|
result.range = makeRange(startIndex, startOffset, endIndex, endOffset)
|
||||||
yield result
|
yield result
|
||||||
}
|
}
|
||||||
})
|
}, acceptNode)
|
||||||
for (const result of iter) yield result
|
for (const result of iter) yield result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -27,9 +27,9 @@ const acceptNode = node => {
|
|||||||
return NodeFilter.FILTER_ACCEPT
|
return NodeFilter.FILTER_ACCEPT
|
||||||
}
|
}
|
||||||
|
|
||||||
export const textWalker = function* (x, func) {
|
export const textWalker = function* (x, func, filterFunc) {
|
||||||
const root = x.commonAncestorContainer ?? x.body ?? x
|
const root = x.commonAncestorContainer ?? x.body ?? x
|
||||||
const walker = document.createTreeWalker(root, filter, { acceptNode })
|
const walker = document.createTreeWalker(root, filter, { acceptNode: filterFunc || acceptNode })
|
||||||
const walk = x.commonAncestorContainer ? walkRange : walkDocument
|
const walk = x.commonAncestorContainer ? walkRange : walkDocument
|
||||||
const nodes = walk(x, walker)
|
const nodes = walk(x, walker)
|
||||||
const strs = nodes.map(node => node.nodeValue)
|
const strs = nodes.map(node => node.nodeValue)
|
||||||
|
|||||||
Reference in New Issue
Block a user