aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils.mjs
diff options
context:
space:
mode:
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 )