Skip to content

Commit a07e31c

Browse files
committed
(frontend) seperate note editor code from core logic
1 parent 3cc3e18 commit a07e31c

File tree

3 files changed

+46
-67
lines changed

3 files changed

+46
-67
lines changed

frontend/src/components/note.tsx

+24-15
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { Accessor, Component, Match, Show, Switch, createSignal } from "solid-js";
2-
import { Note as NoteDetails } from "../core/types";
1+
import { Accessor, Component, Match, Switch, createSignal, untrack } from "solid-js";
32
import NoteViewPlain from "./note/view_plain";
4-
import NoteEdit from "./note/edit";
53
import NoteViewRendered from "./note/view_rendered";
64
import Icon from "./icon";
75
import { copyToClipboard } from "../core/helpers";
86
import { ToastType, useToast } from "../contexts/ToastProvider";
7+
import { SetStoreFunction, Store } from "solid-js/store";
8+
import Editor, { EditorState } from "./editor/editor";
9+
10+
const AUTO_SAVE_TIMEOUT = 2400;
911

1012
export enum NoteMode {
1113
RENDERED = "rendered",
@@ -16,10 +18,12 @@ export enum NoteMode {
1618
type NoteProps = {
1719
mode: NoteMode,
1820
setMode: (mode: NoteMode) => any,
19-
noteDetails: NoteDetails,
20-
content: Accessor<string | undefined>,
21+
content: Accessor<string>,
2122
setContent: (content: string) => any,
2223
isEditAllowed: boolean,
24+
state: Store<EditorState>
25+
setState: SetStoreFunction<EditorState>
26+
onSave: (content: string) => any
2327
}
2428

2529
const Note: Component<NoteProps> = (props) => {
@@ -86,16 +90,21 @@ const Note: Component<NoteProps> = (props) => {
8690
</label>
8791
</div>
8892
</div>
89-
<Show when={props.content()}>
90-
{content => <Switch fallback={<NoteViewRendered content={content} />}>
91-
<Match when={props.mode === NoteMode.PLAIN}>
92-
<NoteViewPlain content={content} />
93-
</Match>
94-
<Match when={props.mode === NoteMode.EDIT}>
95-
<NoteEdit note={props.noteDetails} content={content} onChange={props.setContent} isFullscreen={isFullscreen} />
96-
</Match>
97-
</Switch>}
98-
</Show>
93+
<Switch fallback={<NoteViewRendered content={props.content} />}>
94+
<Match when={props.mode === NoteMode.PLAIN}>
95+
<NoteViewPlain content={props.content} />
96+
</Match>
97+
<Match when={props.mode === NoteMode.EDIT}>
98+
<Editor
99+
content={untrack(props.content)}
100+
autoSaveTimeout={AUTO_SAVE_TIMEOUT}
101+
onSave={props.onSave}
102+
state={props.state}
103+
setState={props.setState}
104+
isFullscreen={isFullscreen}
105+
/>
106+
</Match>
107+
</Switch>
99108
</div>
100109
)
101110
}

frontend/src/components/note/edit.tsx

-50
This file was deleted.

frontend/src/routes/[username]/[...path].tsx

+22-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import StorageHandler from '../../core/storage';
1919
import AssetsModal from '../../components/modals/assets';
2020
import { StringSource, copyToClipboard, download } from '../../core/helpers';
2121
import PrintNoteModal from '../../components/modals/print_note';
22+
import { createStore } from 'solid-js/store';
23+
import { EditorState } from '../../components/editor/editor';
2224

2325
const Shelf: Component = () => {
2426
const params = useParams()
@@ -37,6 +39,11 @@ const Shelf: Component = () => {
3739
return stored
3840
}
3941

42+
const [state, setState] = createStore<EditorState>({
43+
saving: false,
44+
unsaved: false,
45+
})
46+
4047
const slugParts: () => Breadcrumb = () => {
4148
return {
4249
username: params.username,
@@ -187,6 +194,17 @@ const Shelf: Component = () => {
187194
})
188195
}
189196

197+
const saveNote = async (noteId: string, content: string) => {
198+
setState({ saving: true })
199+
let result = await api().updateNoteContent(noteId, content)
200+
setState({ saving: false })
201+
if (result instanceof ApiError) pushToast(apiErrorIntoToast(result, "saving note"))
202+
else {
203+
setState({ unsaved: false })
204+
setNoteContent(content)
205+
}
206+
}
207+
190208
return (
191209
<div class="flex flex-col gap-4">
192210
<div class="flex gap-4 flex-col sm:flex-row">
@@ -298,10 +316,12 @@ const Shelf: Component = () => {
298316
if (mode === NoteMode.RENDERED) { setNoteModeSetting(null) }
299317
else { setNoteModeSetting(mode) }
300318
}}
301-
noteDetails={note()}
302-
content={noteContent}
319+
content={() => noteContent() || ""}
303320
setContent={setNoteContent}
304321
isEditAllowed={allowNoteCreate() || false}
322+
state={state}
323+
setState={setState}
324+
onSave={(content) => saveNote(note().id, content)}
305325
/>}
306326
</Show>
307327
</Show>

0 commit comments

Comments
 (0)