From cb1f2a87bd8694b8dfdbe8e0c14fb7ee0da841f3 Mon Sep 17 00:00:00 2001 From: Eltrac Date: Mon, 15 Sep 2025 11:28:38 +0800 Subject: refactor: lightbox ID generation and shadow management Replaces the async SHA-256 based unique ID generation with a synchronous hashCode method in Utility. Refactors LightBox to manage Shadow instances in a local cache instead of querying the DOM, and removes the static find method from Shadow. Also extracts image link wrapping logic into a helper method for clarity. --- src/utility.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'src/utility.js') diff --git a/src/utility.js b/src/utility.js index 790443d..f2ee3fb 100644 --- a/src/utility.js +++ b/src/utility.js @@ -1,12 +1,11 @@ -const utility = { - generateUniqueId: (str) => { - const encoder = new TextEncoder(); - const data = encoder.encode(str); - const hashBuffer = crypto.subtle.digest('SHA-256', data); // SHA-256 hash - const hashArray = Array.from(new Uint8Array(hashBuffer)); // Convert buffer to array - const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join(''); - return hashHex; // Return the hex string +export default class Utility { + static hashCode(str) { + let hash = 0; + for (let i = 0, len = str.length; i < len; i++) { + let chr = str.charCodeAt(i); + hash = (hash << 5) - hash + chr; + hash |= 0; // Convert to 32bit integer + } + return (hash>0) ? hash : -hash; } } - -export default utility; -- cgit v1.2.3