1
0
silverbullet/webapp/src/app.tsx

353 lines
9.1 KiB
TypeScript
Raw Normal View History

2022-02-21 08:32:36 +00:00
import { autocompletion, completionKeymap } from "@codemirror/autocomplete";
import { closeBrackets, closeBracketsKeymap } from "@codemirror/closebrackets";
import { indentWithTab, standardKeymap } from "@codemirror/commands";
import { history, historyKeymap } from "@codemirror/history";
import { indentOnInput } from "@codemirror/language";
import { bracketMatching } from "@codemirror/matchbrackets";
import { searchKeymap } from "@codemirror/search";
2022-02-21 10:27:30 +00:00
import { EditorState, StateField, Transaction } from "@codemirror/state";
2022-02-21 08:32:36 +00:00
import {
drawSelection,
dropCursor,
EditorView,
highlightSpecialChars,
keymap,
} from "@codemirror/view";
2022-02-21 10:27:30 +00:00
import React, { useEffect, useReducer, useRef } from "react";
import ReactDOM from "react-dom";
2022-02-21 08:32:36 +00:00
import * as commands from "./commands";
2022-02-22 13:18:37 +00:00
import { CommandPalette } from "./components/commandpalette";
import { NoteNavigator } from "./components/notenavigator";
2022-02-21 10:27:30 +00:00
import { HttpFileSystem } from "./fs";
2022-02-21 08:32:36 +00:00
import { lineWrapper } from "./lineWrapper";
2022-02-21 10:27:30 +00:00
import { markdown } from "./markdown";
2022-02-21 08:32:36 +00:00
import customMarkDown from "./parser";
2022-02-22 13:18:37 +00:00
import reducer from "./reducer";
2022-02-21 08:32:36 +00:00
import customMarkdownStyle from "./style";
2022-02-22 13:18:37 +00:00
import { Action, AppViewState } from "./types";
2022-02-21 08:32:36 +00:00
2022-02-22 13:18:37 +00:00
import { syntaxTree } from "@codemirror/language";
import * as util from "./util";
2022-02-21 10:27:30 +00:00
2022-02-21 12:25:41 +00:00
const fs = new HttpFileSystem("http://localhost:2222/fs");
2022-02-21 10:27:30 +00:00
2022-02-22 13:18:37 +00:00
const initialViewState: AppViewState = {
2022-02-21 10:27:30 +00:00
currentNote: "",
isSaved: false,
2022-02-22 13:18:37 +00:00
showNoteNavigator: false,
showCommandPalette: false,
2022-02-21 10:27:30 +00:00
allNotes: [],
2022-02-21 08:32:36 +00:00
};
class Editor {
view: EditorView;
currentNote: string;
2022-02-21 10:27:30 +00:00
dispatch: React.Dispatch<Action>;
2022-02-21 08:32:36 +00:00
2022-02-21 10:27:30 +00:00
constructor(
parent: Element,
currentNote: string,
text: string,
dispatch: React.Dispatch<Action>
) {
2022-02-21 08:32:36 +00:00
this.view = new EditorView({
state: this.createEditorState(text),
parent: parent,
});
this.currentNote = currentNote;
2022-02-21 10:27:30 +00:00
this.dispatch = dispatch;
2022-02-21 08:32:36 +00:00
}
2022-02-21 10:27:30 +00:00
createEditorState(text: string): EditorState {
2022-02-22 13:18:37 +00:00
const editor = this;
2022-02-21 08:32:36 +00:00
return EditorState.create({
doc: text,
extensions: [
highlightSpecialChars(),
history(),
drawSelection(),
dropCursor(),
indentOnInput(),
customMarkdownStyle,
bracketMatching(),
closeBrackets(),
autocompletion(),
EditorView.lineWrapping,
lineWrapper([
{ selector: "ATXHeading1", class: "line-h1" },
{ selector: "ATXHeading2", class: "line-h2" },
{ selector: "ListItem", class: "line-li" },
{ selector: "Blockquote", class: "line-blockquote" },
{ selector: "CodeBlock", class: "line-code" },
{ selector: "FencedCode", class: "line-fenced-code" },
]),
keymap.of([
...closeBracketsKeymap,
...standardKeymap,
...searchKeymap,
...historyKeymap,
...completionKeymap,
indentWithTab,
{
key: "Ctrl-b",
mac: "Cmd-b",
run: commands.insertMarker("**"),
},
{
key: "Ctrl-i",
mac: "Cmd-i",
run: commands.insertMarker("_"),
},
{
key: "Ctrl-s",
mac: "Cmd-s",
run: (target: EditorView): boolean => {
Promise.resolve()
.then(async () => {
console.log("Saving");
await this.save();
})
.catch((e) => console.error(e));
return true;
},
},
2022-02-22 13:18:37 +00:00
{
key: "Ctrl-Enter",
mac: "Cmd-Enter",
run: (target): boolean => {
// TODO: Factor this and click handler into one action
let selection = target.state.selection.main;
if (selection.empty) {
let node = syntaxTree(target.state).resolveInner(
selection.from
);
if (node && node.name === "WikiLinkPage") {
let noteName = target.state.sliceDoc(node.from, node.to);
this.navigate(noteName);
return true;
}
}
return false;
},
},
2022-02-21 10:27:30 +00:00
{
key: "Ctrl-p",
mac: "Cmd-p",
run: (target): boolean => {
this.dispatch({ type: "start-navigate" });
return true;
},
},
2022-02-22 13:18:37 +00:00
{
key: "Ctrl-.",
mac: "Cmd-.",
run: (target): boolean => {
this.dispatch({ type: "show-palette" });
return true;
},
},
2022-02-21 08:32:36 +00:00
]),
EditorView.domEventHandlers({
2022-02-21 10:27:30 +00:00
click: this.click.bind(this),
2022-02-21 08:32:36 +00:00
}),
markdown({
base: customMarkDown,
}),
StateField.define({
create: () => null,
2022-02-21 10:27:30 +00:00
update: this.update.bind(this),
2022-02-21 08:32:36 +00:00
}),
],
});
}
2022-02-21 10:27:30 +00:00
update(value: null, transaction: Transaction): null {
if (transaction.docChanged) {
this.dispatch({
2022-02-22 13:18:37 +00:00
type: "note-updated",
2022-02-21 10:27:30 +00:00
});
}
return null;
}
load(name: string, text: string) {
this.currentNote = name;
this.view.setState(this.createEditorState(text));
}
click(event: MouseEvent, view: EditorView) {
if (event.metaKey || event.ctrlKey) {
2022-02-22 13:18:37 +00:00
let coords = view.posAtCoords(event)!;
let node = syntaxTree(view.state).resolveInner(coords);
if (node && node.name === "WikiLinkPage") {
let noteName = view.state.sliceDoc(node.from, node.to);
this.navigate(noteName);
}
2022-02-21 10:27:30 +00:00
return false;
}
}
async save() {
2022-02-22 13:18:37 +00:00
const created = await fs.writeNote(
this.currentNote,
this.view.state.sliceDoc()
);
this.dispatch({ type: "note-saved" });
// If a new note was created, let's refresh the note list
if (created) {
await this.loadNoteList();
}
}
async loadNoteList() {
let notesMeta = await fs.listNotes();
this.dispatch({
type: "notes-listed",
notes: notesMeta,
});
2022-02-21 10:27:30 +00:00
}
focus() {
this.view.focus();
}
2022-02-21 12:25:41 +00:00
navigate(name: string) {
location.hash = encodeURIComponent(name);
}
2022-02-21 08:32:36 +00:00
}
2022-02-22 13:18:37 +00:00
let editor: Editor | null;
function NavigationBar({
2022-02-21 10:27:30 +00:00
currentNote,
2022-02-21 12:25:41 +00:00
onClick,
2022-02-21 10:27:30 +00:00
}: {
currentNote: string;
2022-02-21 12:25:41 +00:00
onClick: () => void;
2022-02-21 10:27:30 +00:00
}) {
2022-02-21 08:32:36 +00:00
return (
<div id="top">
2022-02-21 12:25:41 +00:00
<div className="current-note" onClick={onClick}>
» {currentNote}
</div>
2022-02-21 08:32:36 +00:00
</div>
);
}
2022-02-22 13:18:37 +00:00
function StatusBar({ isSaved }: { isSaved: boolean }) {
let wordCount = 0,
readingTime = 0;
if (editor) {
let text = editor.view.state.sliceDoc();
wordCount = util.countWords(text);
readingTime = util.readingTime(wordCount);
}
return (
<div id="bottom">
{wordCount} words | {readingTime} min | {isSaved ? "Saved" : "Edited"}
</div>
);
}
2022-02-21 10:27:30 +00:00
function AppView() {
2022-02-21 08:32:36 +00:00
const editorRef = useRef<HTMLDivElement>(null);
2022-02-21 10:27:30 +00:00
const [appState, dispatch] = useReducer(reducer, initialViewState);
2022-02-21 08:32:36 +00:00
useEffect(() => {
2022-02-21 10:27:30 +00:00
editor = new Editor(editorRef.current!, "", "", dispatch);
2022-02-21 08:32:36 +00:00
editor.focus();
// @ts-ignore
window.editor = editor;
2022-02-21 12:25:41 +00:00
if (!location.hash) {
editor.navigate("start");
}
2022-02-21 10:27:30 +00:00
}, []);
useEffect(() => {
2022-02-22 13:18:37 +00:00
editor?.loadNoteList();
2022-02-21 08:32:36 +00:00
}, []);
2022-02-22 13:18:37 +00:00
// Auto save
useEffect(() => {
const id = setTimeout(() => {
if (!appState.isSaved) {
editor?.save();
}
}, 2000);
return () => {
clearTimeout(id);
};
}, [appState.isSaved]);
2022-02-21 12:25:41 +00:00
useEffect(() => {
function hashChange() {
const noteName = decodeURIComponent(location.hash.substring(1));
console.log("Now navigating to", noteName);
fs.readNote(noteName)
.then((text) => {
editor!.load(noteName, text);
dispatch({
2022-02-22 13:18:37 +00:00
type: "note-loaded",
2022-02-21 12:25:41 +00:00
name: noteName,
});
})
.catch((e) => {
console.error("Error loading note", e);
});
}
hashChange();
window.addEventListener("hashchange", hashChange);
return () => {
window.removeEventListener("hashchange", hashChange);
};
}, []);
2022-02-21 08:32:36 +00:00
return (
<>
2022-02-22 13:18:37 +00:00
{appState.showNoteNavigator && (
<NoteNavigator
allNotes={appState.allNotes}
onNavigate={(note) => {
dispatch({ type: "stop-navigate" });
editor!.focus();
if (note) {
editor
?.save()
.then(() => {
editor!.navigate(note);
})
.catch((e) => {
alert("Could not save note, not switching");
});
}
}}
/>
)}
{appState.showCommandPalette && (
<CommandPalette
onTrigger={(cmd) => {
dispatch({ type: "hide-palette" });
editor!.focus();
if (cmd) {
console.log("Run", cmd);
}
}}
commands={[{ name: "My command", run: () => {} }]}
/>
)}
<NavigationBar
2022-02-21 10:27:30 +00:00
currentNote={appState.currentNote}
2022-02-21 12:25:41 +00:00
onClick={() => {
dispatch({ type: "start-navigate" });
}}
2022-02-21 10:27:30 +00:00
/>
2022-02-21 08:32:36 +00:00
<div id="editor" ref={editorRef}></div>
2022-02-22 13:18:37 +00:00
<StatusBar isSaved={appState.isSaved} />
2022-02-21 08:32:36 +00:00
</>
);
}
2022-02-21 12:25:41 +00:00
ReactDOM.render(<AppView />, document.getElementById("root"));