MCPcopy Create free account
hub / github.com/Coneja-Chibi/CarrotKernel / initializeKeywordSelector

Function initializeKeywordSelector

chunk-visualizer.js:614–770  ·  view source on GitHub ↗
(chunk)

Source from the content-addressed store, hash-verified

612// =============================================================================
613
614function initializeKeywordSelector(chunk) {
615 const hash = chunk.hash;
616 const $select = $(`.carrot-chunk-keywords[data-hash="${hash}"]`);
617 const $textarea = $(`.carrot-chunk-keywords-plaintext[data-hash="${hash}"]`);
618 const $switchBtn = $(`.carrot-switch-input-type-icon[data-hash="${hash}"]`);
619
620 if (!$select.length) return;
621
622 const isPlaintext = extension_settings[extensionName].keyword_input_plaintext || false;
623
624 const systemKeywords = ensureArrayValue(chunk.systemKeywords);
625 const customKeywords = ensureArrayValue(chunk.customKeywords);
626 const allKeywords = [...new Set([...systemKeywords, ...customKeywords])];
627 const customKeywordSet = new Set(customKeywords.map(normalizeKeywordClient));
628 const disabledSet = new Set(ensureArrayValue(chunk.disabledKeywords).map(normalizeKeywordClient));
629
630 if (!chunk.customWeights) chunk.customWeights = {};
631
632 const getWeight = (keyword) => {
633 const normalized = normalizeKeywordClient(keyword);
634 const defaultPriority = fullsheetAPI.getKeywordPriority ? fullsheetAPI.getKeywordPriority(keyword) : 20;
635 const customWeight = chunk.customWeights[normalized];
636 return customWeight !== undefined
637 ? customWeight
638 : (customKeywordSet.has(normalized) ? CUSTOM_KEYWORD_PRIORITY : defaultPriority);
639 };
640
641 if (!isPlaintext) {
642 // Initialize select2
643 $select.select2({
644 tags: true,
645 tokenSeparators: [','],
646 placeholder: $select.attr('placeholder'),
647 width: '100%',
648 templateResult: function(item) {
649 const content = $('<span>').addClass('item').text(item.text);
650 const isRegex = isValidRegex(item.text);
651 if (isRegex) {
652 content.html(highlightRegex(item.text));
653 content.addClass('regex_item').prepend($('<span>').addClass('regex_icon').text('•*').attr('title', 'Regex'));
654 }
655 return content;
656 },
657 templateSelection: function(item) {
658 const keyword = item.text;
659 const isRegex = isValidRegex(keyword);
660 const normalized = normalizeKeywordClient(keyword);
661 const weight = getWeight(keyword);
662
663 if (isRegex) {
664 const $regexTag = $('<span>').addClass('item').addClass('regex_item');
665 $regexTag.prepend($('<span>').addClass('regex_icon').text('•*'));
666 $regexTag.append(' ').append($(highlightRegex(keyword)));
667 const $weight = createWeightBadge(keyword, hash, weight);
668 $regexTag.append(' [').append($weight).append(']');
669 return $regexTag;
670 }
671

Callers 1

renderChunksFunction · 0.85

Calls 9

normalizeKeywordClientFunction · 0.85
createWeightBadgeFunction · 0.85
showMethod · 0.80
uiMethod · 0.80
ensureArrayValueFunction · 0.70
isValidRegexFunction · 0.70
getWeightFunction · 0.70
renderChunkCardFunction · 0.70

Tested by

no test coverage detected