1
0
silverbullet/webapp/src/app.tsx

283 lines
7.0 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-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";
import customMarkdownStyle from "./style";
2022-02-21 10:27:30 +00:00
import { FilterList } from "./components/filter";
2022-02-21 08:32:36 +00:00
2022-02-21 12:25:41 +00:00
import { NoteMeta, AppViewState, Action } from "./types";
import reducer from "./reducer";
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
const initialViewState = {
currentNote: "",
isSaved: false,
isFiltering: false,
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-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-21 10:27:30 +00:00
{
key: "Ctrl-p",
mac: "Cmd-p",
run: (target): boolean => {
this.dispatch({ type: "start-navigate" });
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({
type: "updated",
});
}
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) {
console.log("Navigate click");
let coords = view.posAtCoords(event);
console.log("Coords", view.state.doc.sliceString(coords!, coords! + 1));
return false;
}
}
async save() {
await fs.writeNote(this.currentNote, this.view.state.sliceDoc());
this.dispatch({ type: "saved" });
}
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-21 10:27:30 +00:00
function TopBar({
currentNote,
isSaved,
isFiltering,
allNotes,
onNavigate,
2022-02-21 12:25:41 +00:00
onClick,
2022-02-21 10:27:30 +00:00
}: {
currentNote: string;
isSaved: boolean;
isFiltering: boolean;
allNotes: NoteMeta[];
2022-02-21 12:25:41 +00:00
onNavigate: (note: string | undefined) => void;
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}
{isSaved ? "" : "*"}
</div>
2022-02-21 10:27:30 +00:00
2022-02-21 12:25:41 +00:00
{isFiltering && (
2022-02-21 10:27:30 +00:00
<FilterList
initialText=""
options={allNotes}
onSelect={(opt) => {
console.log("Selected", opt);
2022-02-21 12:25:41 +00:00
onNavigate(opt?.name);
2022-02-21 10:27:30 +00:00
}}
></FilterList>
2022-02-21 12:25:41 +00:00
)}
2022-02-21 08:32:36 +00:00
</div>
);
}
2022-02-21 10:27:30 +00:00
let editor: Editor | null;
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(() => {
fs.listNotes()
.then((notes) => {
dispatch({
type: "notes-list",
notes: notes,
});
})
.catch((e) => console.error(e));
2022-02-21 08:32:36 +00:00
}, []);
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({
type: "loaded",
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-21 10:27:30 +00:00
<TopBar
currentNote={appState.currentNote}
isSaved={appState.isSaved}
isFiltering={appState.isFiltering}
allNotes={appState.allNotes}
2022-02-21 12:25:41 +00:00
onClick={() => {
dispatch({ type: "start-navigate" });
}}
2022-02-21 10:27:30 +00:00
onNavigate={(note) => {
dispatch({ type: "stop-navigate" });
editor!.focus();
2022-02-21 12:25:41 +00:00
if (note) {
editor!.navigate(note);
}
2022-02-21 10:27:30 +00:00
}}
/>
2022-02-21 08:32:36 +00:00
<div id="editor" ref={editorRef}></div>
<div id="bottom">Bottom</div>
</>
);
}
2022-02-21 12:25:41 +00:00
ReactDOM.render(<AppView />, document.getElementById("root"));