From 8750b5dcacbfa4bb0bf38af190b41fbc1fdf5d81 Mon Sep 17 00:00:00 2001 From: IceSandwich Date: Mon, 14 Jul 2025 14:48:20 +0800 Subject: [PATCH] fix indents --- web/js/autocomplete.js | 144 ++++++++++++++++++++--------------------- web/js/data.js | 70 ++++++++++---------- web/js/settings.js | 2 +- web/js/utils.js | 4 +- 4 files changed, 110 insertions(+), 110 deletions(-) diff --git a/web/js/autocomplete.js b/web/js/autocomplete.js index e4d8402..998ceef 100644 --- a/web/js/autocomplete.js +++ b/web/js/autocomplete.js @@ -103,85 +103,85 @@ function searchCompletionCandidates(textareaElement) { const sources = getEnabledTagSourceInPriorityOrder(); for (const source of sources) { - if (autoCompleteData[source].use_fuse && autoCompleteData[source].fuse) { - const searchResults = autoCompleteData[source].fuse.search(partialTag, { - limit: settingValues.maxSuggestions, - suggest: false, - cache: true, - }); - if (settingValues._logprocessingTime) { - const endTime = performance.now(); - const duration = endTime - startTime; - console.debug(`[Autocomplete-Plus] Fast Search for "${partialTag}" in ${source} took ${duration.toFixed(2)}ms. Found ${searchResults.length} candidates.`); - } - let result = []; - searchResults.sort((a, b) => { - if (matchWord(autoCompleteData[source].sortedTags[b].tag, queryVariations).matched) { - return 999999999999; - } - if (matchWord(autoCompleteData[source].sortedTags[a].tag, queryVariations).matched) { - return -999999999999; - } - return autoCompleteData[source].sortedTags[b].count - autoCompleteData[source].sortedTags[a].count; - }).forEach(seachResult => { - result.push(autoCompleteData[source].sortedTags[seachResult]); - }); - return result; - } else { - // Search in sortedTags (already sorted by count) - for (const tagData of autoCompleteData[source].sortedTags) { - let matched = false; - let isExactMatch = false; - let matchedAlias = null; + if (autoCompleteData[source].use_fuse && autoCompleteData[source].fuse) { + const searchResults = autoCompleteData[source].fuse.search(partialTag, { + limit: settingValues.maxSuggestions, + suggest: false, + cache: true, + }); + if (settingValues._logprocessingTime) { + const endTime = performance.now(); + const duration = endTime - startTime; + console.debug(`[Autocomplete-Plus] Fast Search for "${partialTag}" in ${source} took ${duration.toFixed(2)}ms. Found ${searchResults.length} candidates.`); + } + let result = []; + searchResults.sort((a, b) => { + if (matchWord(autoCompleteData[source].sortedTags[b].tag, queryVariations).matched) { + return 999999999999; + } + if (matchWord(autoCompleteData[source].sortedTags[a].tag, queryVariations).matched) { + return -999999999999; + } + return autoCompleteData[source].sortedTags[b].count - autoCompleteData[source].sortedTags[a].count; + }).forEach(seachResult => { + result.push(autoCompleteData[source].sortedTags[seachResult]); + }); + return result; + } else { + // Search in sortedTags (already sorted by count) + for (const tagData of autoCompleteData[source].sortedTags) { + let matched = false; + let isExactMatch = false; + let matchedAlias = null; - // Check primary tag against all variations for exact/partial match - const tagMatch = matchWord(tagData.tag, queryVariations); - matched = tagMatch.matched; - isExactMatch = tagMatch.isExactMatch; + // Check primary tag against all variations for exact/partial match + const tagMatch = matchWord(tagData.tag, queryVariations); + matched = tagMatch.matched; + isExactMatch = tagMatch.isExactMatch; - // If primary tag didn't match, check aliases against all variations - if (!matched && tagData.alias && Array.isArray(tagData.alias) && tagData.alias.length > 0) { - for (const alias of tagData.alias) { - const lowerAlias = alias.toLowerCase(); - const aliasMatch = matchWord(lowerAlias, queryVariations); - if (aliasMatch.matched) { - matched = true; - isExactMatch = aliasMatch.isExactMatch; - matchedAlias = alias; - break; - } - } - } + // If primary tag didn't match, check aliases against all variations + if (!matched && tagData.alias && Array.isArray(tagData.alias) && tagData.alias.length > 0) { + for (const alias of tagData.alias) { + const lowerAlias = alias.toLowerCase(); + const aliasMatch = matchWord(lowerAlias, queryVariations); + if (aliasMatch.matched) { + matched = true; + isExactMatch = aliasMatch.isExactMatch; + matchedAlias = alias; + break; + } + } + } - const tagSetKey = tagData.tag; + const tagSetKey = tagData.tag; - // Add candidate if matched and not already added - if (matched && !addedTags.has(tagSetKey)) { - // Add to exact matches or partial matches based on match type - if (isExactMatch) { - exactMatches.push(tagData); - } else { - partialMatches.push(tagData); - } + // Add candidate if matched and not already added + if (matched && !addedTags.has(tagSetKey)) { + // Add to exact matches or partial matches based on match type + if (isExactMatch) { + exactMatches.push(tagData); + } else { + partialMatches.push(tagData); + } - addedTags.add(tagSetKey); + addedTags.add(tagSetKey); - // Check if we've reached the maximum suggestions limit combining both arrays - if (exactMatches.length + partialMatches.length >= settingValues.maxSuggestions) { - // Return the combined results, prioritizing exact matches - const result = [...exactMatches, ...partialMatches].slice(0, settingValues.maxSuggestions); + // Check if we've reached the maximum suggestions limit combining both arrays + if (exactMatches.length + partialMatches.length >= settingValues.maxSuggestions) { + // Return the combined results, prioritizing exact matches + const result = [...exactMatches, ...partialMatches].slice(0, settingValues.maxSuggestions); - if (settingValues._logprocessingTime) { - const endTime = performance.now(); - const duration = endTime - startTime; - console.debug(`[Autocomplete-Plus] Search for "${partialTag}" took ${duration.toFixed(2)}ms. Found ${result.length} candidates (max reached).`); - } + if (settingValues._logprocessingTime) { + const endTime = performance.now(); + const duration = endTime - startTime; + console.debug(`[Autocomplete-Plus] Search for "${partialTag}" took ${duration.toFixed(2)}ms. Found ${result.length} candidates (max reached).`); + } - return result; // Early exit - } - } - } - } + return result; // Early exit + } + } + } + } } // Combine results, with exact matches first @@ -208,7 +208,7 @@ function getCurrentPartialTag(inputElement) { // Find the last newline or comma before the cursor const lastNewLine = text.lastIndexOf('\n', cursorPos - 1); const lastComma = text.lastIndexOf(',', cursorPos - 1); - const lastPeriod = text.lastIndexOf('.', cursorPos - 1); + const lastPeriod = text.lastIndexOf('.', cursorPos - 1); // Get the position of the last separator (newline or comma) before cursor const lastSeparator = Math.max(lastNewLine, lastComma, lastPeriod); diff --git a/web/js/data.js b/web/js/data.js index 46e419a..42e7376 100644 --- a/web/js/data.js +++ b/web/js/data.js @@ -64,11 +64,11 @@ export class TagData { class AutocompleteData { constructor() { - /** @type {Index} */ - this.fuse = null; + /** @type {Index} */ + this.fuse = null; - /** @type {boolean} */ - this.use_fuse = false; + /** @type {boolean} */ + this.use_fuse = false; /** @type {TagData[]} */ this.sortedTags = []; @@ -189,39 +189,39 @@ async function loadTags(csvUrl, siteName) { } }); - autoCompleteData[siteName].fuse = new Index({ - tokenize: "forward", - }); + autoCompleteData[siteName].fuse = new Index({ + tokenize: "forward", + }); - let startIdx = 0; - const startTime = performance.now(); - function processChunkTasks() { - const chunkSize = 1000; - const end = Math.min(startIdx + chunkSize, autoCompleteData[siteName].sortedTags.length); - for (; startIdx < end; startIdx++) { - const tagData = autoCompleteData[siteName].sortedTags[startIdx]; - let allTags = []; - allTags.push(tagData.tag); - if (tagData.alias && Array.isArray(tagData.alias)) { - tagData.alias.forEach(alias => { - allTags.push(alias); - }); - } - allTags = [...new Set(allTags)]; - autoCompleteData[siteName].fuse.add(startIdx, allTags.join(',')); - } + let startIdx = 0; + const startTime = performance.now(); + function processChunkTasks() { + const chunkSize = 1000; + const end = Math.min(startIdx + chunkSize, autoCompleteData[siteName].sortedTags.length); + for (; startIdx < end; startIdx++) { + const tagData = autoCompleteData[siteName].sortedTags[startIdx]; + let allTags = []; + allTags.push(tagData.tag); + if (tagData.alias && Array.isArray(tagData.alias)) { + tagData.alias.forEach(alias => { + allTags.push(alias); + }); + } + allTags = [...new Set(allTags)]; + autoCompleteData[siteName].fuse.add(startIdx, allTags.join(',')); + } - if (startIdx < autoCompleteData[siteName].sortedTags.length) { - setTimeout(processChunkTasks, 0); - // console.log(`[Autocomplete-Plus] Current porcess: ${startIdx}`); - } else { - const endTime = performance.now(); - const duration = endTime - startTime; - autoCompleteData[siteName].use_fuse = true; - console.debug(`[Autocomplete-Plus] Building ${autoCompleteData[siteName].sortedTags.length} index for ${siteName} took ${duration.toFixed(2)}ms.`); - } - } - processChunkTasks(); + if (startIdx < autoCompleteData[siteName].sortedTags.length) { + setTimeout(processChunkTasks, 0); + // console.log(`[Autocomplete-Plus] Current porcess: ${startIdx}`); + } else { + const endTime = performance.now(); + const duration = endTime - startTime; + autoCompleteData[siteName].use_fuse = true; + console.debug(`[Autocomplete-Plus] Building ${autoCompleteData[siteName].sortedTags.length} index for ${siteName} took ${duration.toFixed(2)}ms.`); + } + } + processChunkTasks(); } catch (error) { console.error(`[Autocomplete-Plus] Failed to fetch or process tags from ${csvUrl}:`, error); } diff --git a/web/js/settings.js b/web/js/settings.js index 3155288..6b93e98 100644 --- a/web/js/settings.js +++ b/web/js/settings.js @@ -21,4 +21,4 @@ export const settingValues = { // Debugging settings (use internally) _hideWhenOutofFocus: true, // Hide UI when the input is out of focus _logprocessingTime: false, // Log processing time for debugging -} +} \ No newline at end of file diff --git a/web/js/utils.js b/web/js/utils.js index 2a9e3de..2c816ba 100644 --- a/web/js/utils.js +++ b/web/js/utils.js @@ -349,11 +349,11 @@ export function findAllTagPositions(text) { // Find the end of this tag (next comma or newline) let endPosComma = text.indexOf(',', startPos); let endPosNewline = text.indexOf('\n', startPos); - let endPosPeriod = text.indexOf('.', startPos); + let endPosPeriod = text.indexOf('.', startPos); if (endPosComma === -1) endPosComma = text.length; if (endPosNewline === -1) endPosNewline = text.length; - if (endPosPeriod === -1) endPosPeriod = text.length; + if (endPosPeriod === -1) endPosPeriod = text.length; const endPos = Math.min(endPosComma, endPosNewline, endPosPeriod); const tagText = text.substring(startPos, endPos);