forked from Tsuzat/Edra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchAndReplace.svelte
More file actions
114 lines (100 loc) · 3.28 KB
/
SearchAndReplace.svelte
File metadata and controls
114 lines (100 loc) · 3.28 KB
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
<script lang="ts">
import type { Editor } from '@tiptap/core';
import ArrowLeft from 'lucide-svelte/icons/arrow-left';
import ArrowRight from 'lucide-svelte/icons/arrow-right';
import CaseSensitive from 'lucide-svelte/icons/case-sensitive';
import Replace from 'lucide-svelte/icons/replace';
import ReplaceAll from 'lucide-svelte/icons/replace-all';
import Search from 'lucide-svelte/icons/search';
interface Props {
editor: Editor;
show: boolean;
}
let { editor, show = $bindable(false) }: Props = $props();
let searchText = $state('');
let replaceText = $state('');
let caseSensitive = $state(false);
let searchIndex = $derived(editor.storage?.searchAndReplace?.resultIndex);
let searchCount = $derived(editor.storage?.searchAndReplace?.results.length);
function updateSearchTerm(clearIndex: boolean = false) {
if (clearIndex) editor.commands.resetIndex();
editor.commands.setSearchTerm(searchText);
editor.commands.setReplaceTerm(replaceText);
editor.commands.setCaseSensitive(caseSensitive);
}
function goToSelection() {
const { results, resultIndex } = editor.storage.searchAndReplace;
const position = results[resultIndex];
if (!position) return;
editor.commands.setTextSelection(position);
const { node } = editor.view.domAtPos(editor.state.selection.anchor);
if (node instanceof HTMLElement) node.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
function replace() {
editor.commands.replace();
goToSelection();
}
const next = () => {
editor.commands.nextSearchResult();
goToSelection();
};
const previous = () => {
editor.commands.previousSearchResult();
goToSelection();
};
const clear = () => {
searchText = '';
replaceText = '';
caseSensitive = false;
editor.commands.resetIndex();
};
const replaceAll = () => editor.commands.replaceAll();
</script>
<div class="edra-search-and-replace">
<button
class="edra-command-button"
onclick={() => {
show = !show;
clear();
updateSearchTerm();
}}
title={show ? 'Go Back' : 'Search and Replace'}
>
{#if show}
<ArrowLeft class="edra-toolbar-icon" />
{:else}
<Search class="edra-toolbar-icon" />
{/if}
</button>
{#if show}
<div class="edra-search-and-replace-content">
<input placeholder="Search..." bind:value={searchText} oninput={() => updateSearchTerm()} />
<span>{searchCount > 0 ? searchIndex + 1 : 0}/{searchCount}</span>
<button
class="edra-command-button"
class:active={caseSensitive}
onclick={() => {
caseSensitive = !caseSensitive;
updateSearchTerm();
}}
title="Case Sensitive"
>
<CaseSensitive class="edra-toolbar-icon" />
</button>
<button class="edra-command-button" onclick={previous} title="Previous">
<ArrowLeft class="edra-toolbar-icon" />
</button>
<button class="edra-command-button" onclick={next} title="Next">
<ArrowRight class="edra-toolbar-icon" />
</button>
<span class="separator"></span>
<input placeholder="Replace..." bind:value={replaceText} oninput={() => updateSearchTerm()} />
<button class="edra-command-button" onclick={replace} title="Replace">
<Replace class="edra-toolbar-icon" />
</button>
<button class="edra-command-button" onclick={replaceAll} title="Replace All">
<ReplaceAll class="edra-toolbar-icon" />
</button>
</div>
{/if}
</div>