From ce3cc9594d7fcf47bb9ba42434d1c574573a7ad5 Mon Sep 17 00:00:00 2001 From: newtextdoc1111 Date: Thu, 8 May 2025 15:24:45 +0900 Subject: [PATCH] refactor: Move getCurrentTag function to utils.js and update its implementation --- web/js/similar-tags.js | 44 ++---------------------------------------- web/js/utils.js | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/web/js/similar-tags.js b/web/js/similar-tags.js index 557730d..7102f7a 100644 --- a/web/js/similar-tags.js +++ b/web/js/similar-tags.js @@ -5,6 +5,7 @@ import { normalizeTagToInsert, normalizeTagToSearch, isValidTag, + getCurrentTag, getViewportMargin } from './utils.js'; @@ -402,47 +403,6 @@ function findSimilarTags(tag) { return result; } -/** - * Extracts the tag at the current cursor position. - * Handles tags separated by commas or newlines. - * @param {HTMLTextAreaElement} inputElement The textarea element - * @returns {string|null} The tag at cursor or null - */ -function getCurrentTag(inputElement) { - const text = inputElement.value; - const cursorPos = inputElement.selectionStart; - - // Find the start position of the current tag - // Look for the last comma or newline before the cursor - const lastComma = text.lastIndexOf(',', cursorPos - 1); - const lastNewline = text.lastIndexOf('\n', cursorPos - 1); - let startPos = Math.max(lastComma, lastNewline); - startPos = startPos === -1 ? 0 : startPos + 1; // If no separator found, start from the beginning - - // Find the end position of the current tag - // Look for the next comma or newline after the start position (or cursor position if more appropriate) - // We search from startPos to correctly handle cases where the cursor is at the beginning of a tag - let searchEndFrom = Math.max(cursorPos, startPos); - let endPosComma = text.indexOf(',', searchEndFrom); - let endPosNewline = text.indexOf('\n', searchEndFrom); - - // If a separator is not found, treat it as the end of the text - if (endPosComma === -1) endPosComma = text.length; - if (endPosNewline === -1) endPosNewline = text.length; - - // Choose the closer separator as the end position - let endPos = Math.min(endPosComma, endPosNewline); - - // Extract and trim the tag - const tag = text.substring(startPos, endPos).trim(); - - // If no tag found, return null - if (!tag) return null; - - // Process the tag: swap underscores/spaces and unescape parentheses - return normalizeTagToSearch(tag); -} - /** * Function to insert a tag into the textarea. * Appends the selected tag after the current tag. @@ -600,7 +560,7 @@ const similarTagsUI = new SimilarTagsUI(); // Helper function to show similar tags based on cursor position function showSimilarTagsForCurrentPosition(textareaElement) { // Get the tag at current cursor position - const currentTag = getCurrentTag(textareaElement); + const currentTag = normalizeTagToSearch(getCurrentTag(textareaElement)); // If no valid tag or tag is too short, hide the panel if (!isValidTag(currentTag)) { diff --git a/web/js/utils.js b/web/js/utils.js index 86e1ab8..1306644 100644 --- a/web/js/utils.js +++ b/web/js/utils.js @@ -137,6 +137,46 @@ export function isValidTag(tag) { return true; } +/** + * Extracts the tag at the current cursor position. + * Handles tags separated by commas or newlines. + * @param {HTMLTextAreaElement} inputElement The textarea element + * @returns {string|null} The tag at cursor or null + */ +export function getCurrentTag(inputElement) { + const text = inputElement.value; + const cursorPos = inputElement.selectionStart; + + // Find the start position of the current tag + // Look for the last comma or newline before the cursor + const lastComma = text.lastIndexOf(',', cursorPos - 1); + const lastNewline = text.lastIndexOf('\n', cursorPos - 1); + let startPos = Math.max(lastComma, lastNewline); + startPos = startPos === -1 ? 0 : startPos + 1; // If no separator found, start from the beginning + + // Find the end position of the current tag + // Look for the next comma or newline after the start position (or cursor position if more appropriate) + // We search from startPos to correctly handle cases where the cursor is at the beginning of a tag + let searchEndFrom = Math.max(cursorPos, startPos); + let endPosComma = text.indexOf(',', searchEndFrom); + let endPosNewline = text.indexOf('\n', searchEndFrom); + + // If a separator is not found, treat it as the end of the text + if (endPosComma === -1) endPosComma = text.length; + if (endPosNewline === -1) endPosNewline = text.length; + + // Choose the closer separator as the end position + let endPos = Math.min(endPosComma, endPosNewline); + + // Extract and trim the tag + const tag = text.substring(startPos, endPos).trim(); + + // If no tag found, return null + if (!tag) return null; + + // Process the tag: swap underscores/spaces and unescape parentheses + return tag; +} // --- End String Helper Functions ---