-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
215 lines (182 loc) · 6.47 KB
/
index.ts
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
import * as fs from "fs";
import * as Path from 'path';
import * as process from 'process';
import { ipcRenderer as ipc } from 'electron';
import { bug } from '../util/bug';
import Signal from '../util/Signal';
import * as Name from '../util/Name';
import Server from '../server';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import Main from '../components/Main';
import { Session, emptySession } from '../components/react-simple-code-editor';
import ghPages from '../publish/ghPages';
import * as Files from './files';
import * as Contents from './contents';
import * as EditName from './editName';
import * as SelectedNote from './selectedNote';
import * as Compiled from './compiled';
import * as Sidebar from './sidebar';
import * as Focus from './focus';
import mkNewNote from './newNote';
const debug = false;
export const mouseSignal: Signal.Writable<{ clientX: number, clientY: number }> =
Signal.cellOk({ clientX: 0, clientY: 0 });
export const selectionSignal: Signal.Writable<Selection | null> =
Signal.cellOk(null);
export const sideBarVisibleCell = Signal.cellOk<boolean>(true);
const toggleSidebarVisible = () => {
sideBarVisibleCell.update(b => !b);
};
export const mainPaneViewCell = Signal.cellOk<'code' | 'display' | 'split'>('split');
const setMainPaneView = (view: 'code' | 'display' | 'split') => {
mainPaneViewCell.setOk(view);
}
export const debugVisibleCell = Signal.cellOk<boolean>(false);
const toggleDebugVisible = () => {
debugVisibleCell.update(b => !b);
}
const deleteNote = () => {
const selected = SelectedNote.selectedNote.get();
if (selected === null) return;
const paths = Files.filesByNameSignal.get().get(selected);
if (!paths) return;
for (const path of paths.keys()) {
Files.filesystem.remove(path);
}
}
export const setNameSignal = Compiled.compiledNoteSignal.map(compiledNote => {
if (compiledNote === null) return (name: string) => {};
else return (name: string) => {
name = Name.normalize(name);
Object.values(compiledNote.files).forEach(file => {
if (!file) return;
const pathParsed = Path.parse(file.path);
const newParsed = { ...pathParsed, base: undefined, dir: Name.dirname(name), name: Name.basename(name) };
const newPath = Path.format(newParsed);
Files.filesystem.rename(file.path, newPath);
});
SelectedNote.setSelected(name);
};
});
export const onNewNoteSignal = mkNewNote({
fsUpdate: Files.filesystem.update,
notes: Files.filesByNameSignal,
focusDir: Sidebar.focusDirCell,
callback: (name: string) => {
SelectedNote.setSelected(name);
EditName.setEditName(name);
Focus.focusTitlebar();
}
});
export const selectedFileSignal =
Signal.join(
Compiled.compiledNoteSignal,
Contents.contents,
).map(([compiledNote, contents]) => {
if (compiledNote) {
const path = Name.pathOfName(compiledNote.name, compiledNote.type);
const file = contents.get(path);
if (file) return file;
}
return null;
});
export const compiledFileSignal = Signal.label('compiledFile',
Signal.join(selectedFileSignal, Compiled.compiledFilesSignal).map(([file, compiledFiles]) => {
if (file) {
const compiledFile = compiledFiles.get(file.path) ?? bug(`expected compiled file for ${file.path}`);
return compiledFile;
}
return null;
})
);
const sessionsCell = Signal.cellOk<Map<string, Session>>(new Map());
export const sessionSignal = Signal.label('session',
Signal.join(selectedFileSignal, sessionsCell).map(([file, sessions]) => {
if (file) {
const session = sessions.get(file.path);
if (session) {
return session;
}
}
return emptySession();
})
);
export const setSessionSignal = Signal.label('setSession',
selectedFileSignal.map(file => {
const noop = (session: Session) => {};
if (!file) return noop;
return (session: Session) => {
sessionsCell.produce(sessions => { sessions.set(file.path, session) });
};
})
);
const server =
new Server(Compiled.compiledNotesSignal);
const nextProblem = () => {
// TODO(jaked)
// const nextIndex = matchingNotes.findIndex(note => note.name === selected) + 1;
// let cont = true;
// for (let i = 0; cont && i < matchingNotes.length; i++) {
// const index = (nextIndex + i) % matchingNotes.length;
// const matchingNote = matchingNotes[index];
// // TODO(jaked) separate selectable content objects in notes?
// if (matchingNote.problems.get() === true) {
// cont = false;
// setSelected(matchingNote.name);
// }
// }
}
const previousProblem = () => {
// TODO(jaked)
// const previousIndex = matchingNotes.findIndex(note => note.name === selected) - 1;
// let cont = true;
// for (let i = matchingNotes.length - 1; cont && i > 0; i--) {
// const index = (previousIndex + i) % matchingNotes.length;
// const matchingNote = matchingNotes[index];
// // TODO(jaked) separate selectable content objects in notes?
// if (matchingNote.problems.get() === true) {
// cont = false;
// setSelected(matchingNote.name);
// }
// }
}
const publishSite = async () => {
const compiledNotes = Compiled.compiledNotesSignal.get();
ghPages(compiledNotes);
}
// TODO(jaked) do we need to remove these somewhere?
ipc.on('focus-search-box', () => Focus.focusSearchbox());
ipc.on('toggle-sidebar-visible', toggleSidebarVisible);
ipc.on('toggle-debug-visible', toggleDebugVisible);
ipc.on('set-main-pane-view', (_, view: 'code' | 'display' | 'split') => {
setMainPaneView(view)
});
ipc.on('history-back', SelectedNote.historyBack);
ipc.on('history-forward', SelectedNote.historyForward);
ipc.on('previous-problem', previousProblem);
ipc.on('next-problem', nextProblem);
ipc.on('delete-note', deleteNote);
ipc.on('publish-site', publishSite);
ipc.on('set-data-dir', (_, path: string) => { Files.setPath(path) });
document.onmousemove = (e: MouseEvent) => {
mouseSignal.setOk({ clientX: e.clientX, clientY: e.clientY });
}
document.onselectionchange = (e: Event) => {
const selection = window.getSelection();
// TODO(jaked)
// need to clone the selection or else the signal doesn't fire
// because it's the same mutable object
// but it doesn't seem to enumerate any properties
// so we explicitly pull out the ones we need
// ???
selectionSignal.setOk(selection ? {
...selection,
isCollapsed: selection.isCollapsed,
focusNode: selection.focusNode
} : null);
}
ReactDOM.render(
React.createElement(Main),
document.getElementById('main')
);