Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 64x 41x 41x 64x 1x 1x 1x 1x 1x 64x 23x 23x 23x 23x 23x 23x 64x 64x 23x 23x 23x 23x 23x 23x 23x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 24x 11x 35x 35x 35x 35x 24x 35x 11x 11x 11x 11x 11x 35x 24x 24x 24x 35x 35x 35x 35x 23x 10x 10x 2x 2x 2x 2x 2x 2x 10x 3x 3x 3x 3x 3x 3x 10x 1x 1x 1x 1x 1x 1x 10x 4x 4x 4x 4x 4x 4x 4x 4x 10x 10x 10x 10x 10x 10x 10x 23x 6x 6x 6x 6x 6x 5x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 23x 22x 22x 22x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x | import { ref } from 'vue'; import { AcceptedInputType, ActiveTrigger, DropdownPosition, Item, Trigger, } from './types'; import { getActiveTrigger, getInputSelectionStart, searchItems, setInputValue, } from './util/input'; import { getDropdownPosition } from './util/dropdown'; import { DROPDOWN_HEIGHT, MAX_SEARCH_LENGTH, SELECT_KEYS } from './constants'; export function useSmartSuggest( startTriggers?: Trigger[], startInput?: AcceptedInputType ) { /** * The textarea that is currently active */ let input: AcceptedInputType | undefined = startInput; /** * Current triggers definition */ const triggers = ref<Trigger[]>(startTriggers ?? []); /** * Whether the suggestions should be shown */ const active = ref(false); /** * Current active trigger */ const activeTrigger = ref<ActiveTrigger>(); /** * Current list of suggestions */ const items = ref<Item[]>([]); /** * Current active suggestion */ const activeIndex = ref(0); /** * Current dropdown position */ const dropdownPosition = ref<DropdownPosition>({ toTop: false, top: 0, left: 0, width: 0, height: 0, }); /** * Update the current text area element * @param newInput The new text area element */ function setInputElement(newInput?: AcceptedInputType) { if (newInput === input) { return; } if (input) { input.removeEventListener('blur', onBlur); input.removeEventListener('scroll', onScroll); input.removeEventListener('input', onInput); input.removeEventListener('keydown', onKeyDown); } input = newInput; if (newInput) { newInput.addEventListener('blur', onBlur); newInput.addEventListener('scroll', onScroll); newInput.addEventListener('input', onInput); newInput.addEventListener('keydown', onKeyDown); } } const onBlur = () => (active.value = false); const onScroll = () => input && (dropdownPosition.value = getDropdownPosition(input, DROPDOWN_HEIGHT)); /** * When the text area is updated */ function onInput() { if (!input) { return; } activeIndex.value = 0; const newActiveTrigger = getActiveTrigger( input, triggers.value, MAX_SEARCH_LENGTH ); // Get list of items matching the search items.value = newActiveTrigger ? searchItems(newActiveTrigger.trigger, newActiveTrigger.search) : []; // No active trigger or no result, hide dropdown if ( !newActiveTrigger || (!newActiveTrigger.trigger.showNoResult && items.value.length === 0) ) { active.value = false; return; } // Update dropdown data dropdownPosition.value = getDropdownPosition(input, DROPDOWN_HEIGHT); activeTrigger.value = newActiveTrigger; active.value = true; } /** * Listen for special keys (selection, navigation within items) */ function onKeyDown(event: KeyboardEvent) { if (!input || !active.value) { return; } // Handle up/down keys if (event.key === 'ArrowUp') { event.preventDefault(); event.stopPropagation(); activeIndex.value = (activeIndex.value - 1 + items.value.length) % items.value.length; return; } else if (event.key === 'ArrowDown') { event.preventDefault(); event.stopPropagation(); activeIndex.value = (activeIndex.value + 1) % items.value.length; return; } // Handle force-close key if (event.key === 'Escape') { event.preventDefault(); event.stopPropagation(); active.value = false; return; } // Handle selection key if (SELECT_KEYS.includes(event.key)) { event.preventDefault(); event.stopPropagation(); const activeItem = items.value[activeIndex.value]; if (activeItem) { select(activeItem); } return; } } /** * Add a given item to the input and close the dropdown * * @param item Item to add */ function select(item: Item) { if (input && activeTrigger.value) { const newValue = input.value.substring(0, activeTrigger.value.index) + item.value + (activeTrigger.value.trigger.insertSpaceAfter !== false ? ' ' : '') + input.value.substring(getInputSelectionStart(input)); setInputValue(input, newValue); active.value = false; } } /** * In case the list of triggers is updated */ function setTriggers(newTriggers: Trigger[]) { triggers.value = newTriggers; } return { setInputElement, setTriggers, select, active, items, dropdownPosition, activeIndex, activeTrigger, }; } |