aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils.mjs
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2024-10-23 12:45:40 +0800
committerHsieh Chin Fan <pham@topo.tw>2024-10-23 16:50:30 +0800
commit24c4fe6d2fa8c1824129535502d4f619fcf0f912 (patch)
treecfec74036861ca3e63e6c50ca9e36abdb463895e /src/utils.mjs
parent1e1ad1a1f8dc738d1f716646ceed30174ee76331 (diff)
refactor: set observers for charactorData/childList separately
* this patches e774e55, make code easier to understanded * use data attribute to initialize content in container * add limitation on method replaceNode(), to prevent GeoLinks generated in pre/code/a element
Diffstat (limited to 'src/utils.mjs')
-rw-r--r--src/utils.mjs21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/utils.mjs b/src/utils.mjs
index 9149ac2..dbb3881 100644
--- a/src/utils.mjs
+++ b/src/utils.mjs
@@ -89,6 +89,25 @@ export function throttle (func, delay) {
89} 89}
90 90
91/** 91/**
92 * debounce.
93 *
94 * @param {Function} func
95 * @param {Number} delay - milliseconds
96 */
97export function debounce (func, delay = 1000) {
98 let timer = null
99
100 return function (...args) {
101 const context = this
102
103 clearTimeout(timer)
104 timer = setTimeout(() => {
105 func.apply(context, args)
106 }, delay)
107 }
108}
109
110/**
92 * shiftByWindow. make sure HTMLElement inside viewport 111 * shiftByWindow. make sure HTMLElement inside viewport
93 * 112 *
94 * @param {HTMLElement} element 113 * @param {HTMLElement} element
@@ -154,7 +173,7 @@ export const replaceTextNodes = (
154 const nodeIterator = document.createNodeIterator( 173 const nodeIterator = document.createNodeIterator(
155 rootNode, 174 rootNode,
156 window.NodeFilter.SHOW_TEXT, 175 window.NodeFilter.SHOW_TEXT,
157 node => node.textContent.match(pattern) && !node.parentElement.closest('code') 176 node => node.textContent.match(pattern) && !node.parentElement.closest('pre,code,a')
158 ? window.NodeFilter.FILTER_ACCEPT 177 ? window.NodeFilter.FILTER_ACCEPT
159 : window.NodeFilter.FILTER_REJECT, 178 : window.NodeFilter.FILTER_REJECT,
160 ) 179 )