File size: 6,301 Bytes
6426ece |
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
<!-- references are:
https://github.com/codemirror/search
https://codemirror.net/docs/ref/#search
-->
<script lang="ts">
import type { EditorView } from '@codemirror/view';
import { onMount, createEventDispatcher } from 'svelte';
import {
SearchQuery,
findPrevious,
findNext,
setSearchQuery,
replaceNext,
replaceAll
} from '@codemirror/search';
import IconCaretV2 from '../Icons/IconCaretV2.svelte';
import IconArrowLeft from '../Icons/IconArrowLeft.svelte';
import IconCross from '../Icons/IconCross.svelte';
import IconReplace from '../Icons/IconReplace.svelte';
import IconReplaceAll from '../Icons/IconReplaceAll.svelte';
export let view: EditorView;
let el: HTMLDivElement;
let searchTxtEl: HTMLInputElement;
let searchTxt = '';
let replaceTxt = '';
let isCaseSensitive = false;
let isRegexp = false;
let isWholeWord = false;
let isReplacePanelOpen = false;
const dispatch = createEventDispatcher<{ close: void }>();
$: query = new SearchQuery({
search: searchTxt,
caseSensitive: isCaseSensitive,
wholeWord: isWholeWord,
regexp: isRegexp,
replace: replaceTxt
});
$: query, search();
// positionSearchPanel is no longer needed since we use CSS for top-right positioning.
function destroyDefaultPanel() {
// hack to prevent default CodeMirror search box appearing
// when you use {findNext, etc...} from @codemirror/search, it tries to automatically create default CodeMirror search box
const el = document.querySelector('.codemirror-wrapper .cm-search');
el?.parentElement?.removeChild(el);
}
function getSelectedText(editorView: EditorView) {
const state = editorView.state;
const selection = state.selection;
const selectedText = selection.ranges
.map((range) => state.doc.sliceString(range.from, range.to))
.join('\n');
return selectedText;
}
function search() {
destroyDefaultPanel();
view?.dispatch({ effects: setSearchQuery.of(query) });
if (searchTxt && view) {
findPrevious(view);
findNext(view);
} else {
reset();
}
}
function reset() {
// send and empty query
view?.dispatch({
effects: setSearchQuery.of(
new SearchQuery({
search: ''
})
)
});
}
function onKeyDownWindow(e: KeyboardEvent) {
const { ctrlKey, metaKey, key, shiftKey } = e;
const cmdKey = metaKey || ctrlKey;
const isOpenShortcut = key === 'f3' || (cmdKey && key === 'f');
const isNextOrPrevShortcut = cmdKey && key === 'g';
const isCloseShortcut = key === 'Escape' || key === 'Esc';
if (isOpenShortcut) {
e.preventDefault();
searchTxt = getSelectedText(view);
searchTxtEl.focus();
} else if (isNextOrPrevShortcut) {
e.preventDefault();
shiftKey ? findPrevious(view) : findNext(view);
} else if (isCloseShortcut) {
dispatch('close');
}
}
function onKeyDownEl(e: KeyboardEvent) {
const { key } = e;
const isNextShortcut = key === 'Enter';
if (isNextShortcut) {
e.preventDefault();
findNext(view);
}
}
onMount(() => {
searchTxt = getSelectedText(view);
searchTxtEl.focus();
// Switch from absolute to fixed and preserve position
if (el) {
const rect = el.getBoundingClientRect();
el.style.position = 'fixed';
el.style.top = rect.top + 'px';
el.style.left = rect.left + 'px';
el.style.right = 'auto'; // Remove right if present
el.classList.remove('absolute');
el.classList.add('fixed');
}
return reset;
});
</script>
<svelte:window on:keydown={onKeyDownWindow} />
<div
bind:this={el}
class="absolute top-0 right-0 z-20 rounded-sm border border-gray-500 bg-white dark:bg-gray-900 dark:text-white"
on:keydown={onKeyDownEl}
>
<div class="flex border-b border-gray-500">
<button
type="button"
class="border-r border-gray-500 px-0.5"
on:click={() => (isReplacePanelOpen = !isReplacePanelOpen)}
>
<IconCaretV2 classNames="h-full {isReplacePanelOpen ? '' : '-rotate-90'}" />
</button>
<div class="my-1">
<div class="flex items-center">
<div class="flex w-[250px] items-center bg-gray-100 dark:bg-gray-800">
<input
type="text"
class="w-full border-0 bg-transparent py-1 pr-[4.3rem] pl-2 text-sm"
bind:value={searchTxt}
bind:this={searchTxtEl}
/>
<!-- buttons: case sensetive, full word, regex -->
<div
class="-ml-[4.3rem] flex w-16 items-center justify-between gap-0.5 font-mono select-none"
>
<button
type="button"
title="Match Case"
class="rounded-sm px-0.5 text-sm {isCaseSensitive ? 'bg-black text-white' : ''}"
on:click={() => (isCaseSensitive = !isCaseSensitive)}
>
Aa
</button>
<button
type="button"
title="Match Whole Word"
class="rounded-sm px-0.5 text-sm underline {isWholeWord ? 'bg-black text-white' : ''}"
on:click={() => (isWholeWord = !isWholeWord)}
>
ab
</button>
<button
type="button"
title="Use Regular Expression"
class="rounded-sm px-0.5 text-sm {isRegexp ? 'bg-black text-white' : ''}"
on:click={() => (isRegexp = !isRegexp)}
>
re
</button>
</div>
</div>
<!-- buttons: find next, find previous, close search panel -->
<div class="mx-2 flex items-center gap-0.5 select-none">
<button type="button" title="Next Match" on:click={() => findNext(view)}>
<IconArrowLeft classNames="-rotate-90" />
</button>
<button type="button" title="Previous Match" on:click={() => findPrevious(view)}>
<IconArrowLeft classNames="rotate-90" />
</button>
<button type="button" title="Close" on:click={() => dispatch('close')}>
<IconCross />
</button>
</div>
</div>
{#if isReplacePanelOpen}
<div class="mt-1 flex items-center">
<input
type="text"
bind:value={replaceTxt}
class="w-[250px] border-0 bg-gray-100 py-1 pl-2 text-sm dark:bg-gray-800"
/>
<div class="ml-1 flex items-center gap-0.5 select-none">
<button type="button" title="Replace" on:click={() => replaceNext(view)}>
<IconReplace classNames="text-base" />
</button>
<button type="button" title="Replace All" on:click={() => replaceAll(view)}>
<IconReplaceAll classNames="text-base" />
</button>
</div>
</div>
{/if}
</div>
</div>
</div>
|