-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.svelte
More file actions
98 lines (93 loc) · 3.01 KB
/
index.svelte
File metadata and controls
98 lines (93 loc) · 3.01 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
<script lang="ts">
import { current_platform_type, rename_node } from '@/lib/file_system';
import { workspace_root_path } from '@/lib/global_states/index.svelte';
import type { Node } from '@/types';
import { readTextFile, writeTextFile } from '@tauri-apps/plugin-fs';
import { toast } from 'svelte-sonner';
import Editor from './editor/index.svelte';
import MobileToolbar from './editor_toolbar_mobile/index.svelte';
import { editor_view } from './editor_state.svelte';
import { focused_subtree } from '@/components/sidebar_section/file_manager/states.svelte';
let {
filenode = $bindable(),
}: {
filenode: Node | undefined;
} = $props();
let text_content: string | undefined = $state();
let current_file_name: string | undefined = $state();
let is_file_named_changed: boolean = $state(false);
let mobile_toolbar_visible: boolean = $state(false);
$effect(() => {
if (!filenode) return;
readTextFile(filenode.path)
.then((res) => {
text_content = res || '\n';
current_file_name = filenode?.name;
})
.catch((err) => {
toast.error(err);
filenode = undefined;
text_content = undefined;
current_file_name = undefined;
});
});
</script>
<div
class="w-full isolate px-8 flex justify-center flex-1 overflow-auto
{current_platform_type == 'mobile' && 'pt-22'}
"
>
<div class="max-w-180 w-full font-sans" id="text_editor">
<input
type="text"
id="note_file_name_input"
oninput={(e) => {
current_file_name = current_file_name?.replace(
/[^A-Za-z0-9 _.\-()]/g,
''
);
is_file_named_changed = current_file_name != filenode?.name;
}}
onfocusout={async () => {
if (!is_file_named_changed) return;
if (!current_file_name || !filenode || !focused_subtree.data) return;
try {
await rename_node({
node: filenode,
new_name: current_file_name,
parent_subtree: focused_subtree.data,
document_top_tree_uri:
workspace_root_path.data?.document_top_tree_uri ?? null,
});
is_file_named_changed = false;
} catch (e) {
if (e instanceof Error) toast.error(e.message);
}
}}
onkeydown={async (e) => {
if (e.key === 'Enter') {
e.preventDefault();
if (editor_view.data) editor_view.data.focus();
}
}}
bind:value={current_file_name}
class="w-full outline-none b-0 focus:ring-0 mb-16 mt-10 font-semibold text-5xl"
/>
<Editor
{text_content}
write_to_file={(content) => {
if (!filenode) return;
writeTextFile(filenode?.path, content);
}}
on_focus_in={() => {
mobile_toolbar_visible = true;
}}
on_focus_out={() => {
mobile_toolbar_visible = false;
}}
/>
</div>
</div>
{#if current_platform_type == 'mobile' && mobile_toolbar_visible && editor_view.data}
<MobileToolbar editor_view={editor_view.data} />
{/if}