From 24c4fe6d2fa8c1824129535502d4f619fcf0f912 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Wed, 23 Oct 2024 12:45:40 +0800 Subject: 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 --- src/utils.mjs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'src/utils.mjs') diff --git a/src/utils.mjs b/src/utils.mjs index 9149ac2..dbb3881 100644 --- a/src/utils.mjs +++ b/src/utils.mjs @@ -88,6 +88,25 @@ export function throttle (func, delay) { } } +/** + * debounce. + * + * @param {Function} func + * @param {Number} delay - milliseconds + */ +export function debounce (func, delay = 1000) { + let timer = null + + return function (...args) { + const context = this + + clearTimeout(timer) + timer = setTimeout(() => { + func.apply(context, args) + }, delay) + } +} + /** * shiftByWindow. make sure HTMLElement inside viewport * @@ -154,7 +173,7 @@ export const replaceTextNodes = ( const nodeIterator = document.createNodeIterator( rootNode, window.NodeFilter.SHOW_TEXT, - node => node.textContent.match(pattern) && !node.parentElement.closest('code') + node => node.textContent.match(pattern) && !node.parentElement.closest('pre,code,a') ? window.NodeFilter.FILTER_ACCEPT : window.NodeFilter.FILTER_REJECT, ) -- cgit v1.2.3-70-g09d2