-
Notifications
You must be signed in to change notification settings - Fork 0
/
PresentationPreferencesEditor.tsx
182 lines (177 loc) · 6.09 KB
/
PresentationPreferencesEditor.tsx
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
import {useDebouncedCallback} from 'use-debounce';
import {useRef} from 'react';
import {type Note} from '../../functions/src/presentation';
import NoteEditor from './NoteEditor';
import SaveIndicator from './SaveIndicator';
import Button from './toolbar/Button';
import {exportNotes, importNotes} from './slides/parse-notes';
export type NotesSaveState = 'dirty' | 'saved' | 'saving';
export default function PresentationPreferencesEditor({
saveState,
onSave,
notes,
setNotes,
title,
setTitle,
onDirty,
pages,
}: {
readonly saveState: NotesSaveState;
readonly onSave: () => void;
readonly notes: Note[];
readonly title: string;
readonly setTitle: (nextTitle: string) => void;
readonly setNotes: (updateNotes: (currentNotes: Note[]) => Note[]) => void;
readonly onDirty: () => void;
readonly pages: string[];
}) {
const debouncedSaveNotes = useDebouncedCallback(() => {
onSave();
}, 3000);
const fileReference = useRef<HTMLInputElement>(null);
return (
<div className="w-full max-w-screen-md mx-auto flex flex-col text-base gap-4">
<div className="grid grid-cols-[6fr_1fr_1fr] lt-sm:(grid-cols-2) gap-4">
<label className="flex flex-row gap-2 items-center lt-sm:col-span-2">
Title:
<input
placeholder="Give your presentation a name..."
className="input w-full max-w-screen-sm self-center h-full"
value={title}
onChange={(event) => {
onDirty();
setTitle(event.target.value);
debouncedSaveNotes();
}}
/>
</label>
<Button
border
icon="i-tabler-package-import"
label="import"
title="Import notes"
onClick={() => {
fileReference.current?.click?.();
}}
/>
<input
ref={fileReference}
type="file"
className="hidden"
accept="text/markdown,.md"
onChange={async (event) => {
try {
if ((event.target.files?.length ?? 0) < 1) {
return;
}
const markdown = await event.target.files![0].text();
const importedNotes = importNotes(markdown, pages.length);
console.log('imported notes', importedNotes);
onDirty();
setNotes(() => importedNotes);
debouncedSaveNotes();
} finally {
// Clear the input so that even if the same file is chosen, it will be re-parsed
event.target.value = '';
}
}}
/>
<Button
border
icon="i-tabler-package-export"
label="export"
title="Export notes"
onClick={() => {
const markdown = exportNotes(notes);
const blob = new Blob([markdown], {type: 'text/markdown'});
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.download = `${title || 'notes'}.md`;
link.href = url;
link.click();
URL.revokeObjectURL(url);
}}
/>
</div>
<div id="speaker-notes">Speaker notes:</div>
<ul className="flex flex-col w-full" aria-labelledby="speaker-notes">
{notes.map((note, noteIndex) => (
<NoteEditor
key={note.pageIndices.toString()}
pageIndices={note.pageIndices}
markdown={note.markdown}
handleFoldUp={() => {
onDirty();
setNotes((currentNotes) => {
// Add the current index to the previous note
const previousNote: Note = {
...currentNotes[noteIndex - 1],
pageIndices: [
...currentNotes[noteIndex - 1].pageIndices,
...note.pageIndices,
],
};
const nextNotes = [
// Copy up to the previous note
...currentNotes.slice(0, Math.max(noteIndex - 1, 0)),
// Add the previous note
previousNote,
// Skip the current note and copy everything else
...currentNotes.slice(noteIndex + 1),
];
return nextNotes;
});
debouncedSaveNotes();
}}
handleFoldDown={() => {
onDirty();
setNotes((currentNotes) => {
const lastIndexInNote =
currentNotes[noteIndex].pageIndices.at(-1)!;
// Remove the last index from the current note
const currentNote: Note = {
...currentNotes[noteIndex],
pageIndices: currentNotes[noteIndex].pageIndices.slice(
0,
-1,
) as [number, ...number[]],
};
const nextNote: Note = {
pageIndices: [lastIndexInNote],
markdown: '',
};
const nextNotes = [
// Copy up to the current note
...currentNotes.slice(0, noteIndex),
// Add the current note
currentNote,
// Insert the next note
nextNote,
// Copy the rest
...currentNotes.slice(noteIndex + 1),
];
console.log('next notes', nextNotes);
return nextNotes;
});
debouncedSaveNotes();
}}
pages={pages}
onMarkdownChange={(markdown) => {
onDirty();
setNotes((currentNotes) => {
const nextNotes = Array.from(currentNotes);
nextNotes[noteIndex] = {...currentNotes[noteIndex], markdown};
return nextNotes;
});
debouncedSaveNotes();
}}
onMarkdownDirty={() => {
onDirty();
}}
/>
))}
</ul>
<SaveIndicator saveState={saveState} />
</div>
);
}