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
|
|
|
|
|
|
|
const fs = new HttpFileSystem("http://localhost:2222/fs");
|
|
|
|
|
2022-02-21 10:27:30 +00:00
|
|
|
type NoteMeta = {
|
|
|
|
name: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type AppViewState = {
|
2022-02-21 08:32:36 +00:00
|
|
|
currentNote: string;
|
2022-02-21 10:27:30 +00:00
|
|
|
isSaved: boolean;
|
|
|
|
isFiltering: boolean;
|
|
|
|
allNotes: NoteMeta[];
|
|
|
|
};
|
|
|
|
|
|
|
|
const initialViewState = {
|
|
|
|
currentNote: "",
|
|
|
|
isSaved: false,
|
|
|
|
isFiltering: false,
|
|
|
|
allNotes: [],
|
2022-02-21 08:32:36 +00:00
|
|
|
};
|
|
|
|
|
2022-02-21 10:27:30 +00:00
|
|
|
type Action =
|
|
|
|
| { type: "loaded"; name: string }
|
|
|
|
| { type: "saved" }
|
|
|
|
| { type: "start-navigate" }
|
|
|
|
| { type: "stop-navigate" }
|
|
|
|
| { type: "updated" }
|
|
|
|
| { type: "notes-list"; notes: NoteMeta[] };
|
|
|
|
|
|
|
|
function reducer(state: AppViewState, action: Action): AppViewState {
|
|
|
|
switch (action.type) {
|
|
|
|
case "loaded":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
currentNote: action.name,
|
|
|
|
isSaved: true,
|
|
|
|
};
|
|
|
|
case "saved":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isSaved: true,
|
|
|
|
};
|
|
|
|
case "updated":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isSaved: false,
|
|
|
|
};
|
|
|
|
case "start-navigate":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isFiltering: true,
|
|
|
|
};
|
|
|
|
case "stop-navigate":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isFiltering: false,
|
|
|
|
};
|
|
|
|
case "notes-list":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
allNotes: action.notes,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
console.log("Something changed");
|
|
|
|
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 08:32:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 10:27:30 +00:00
|
|
|
function TopBar({
|
|
|
|
currentNote,
|
|
|
|
isSaved,
|
|
|
|
isFiltering,
|
|
|
|
allNotes,
|
|
|
|
onNavigate,
|
|
|
|
}: {
|
|
|
|
currentNote: string;
|
|
|
|
isSaved: boolean;
|
|
|
|
isFiltering: boolean;
|
|
|
|
allNotes: NoteMeta[];
|
|
|
|
onNavigate: (note: string) => void;
|
|
|
|
}) {
|
2022-02-21 08:32:36 +00:00
|
|
|
return (
|
|
|
|
<div id="top">
|
2022-02-21 10:27:30 +00:00
|
|
|
<span className="current-note">{currentNote}</span>
|
|
|
|
{isSaved ? "" : "*"}
|
|
|
|
|
|
|
|
{isFiltering ? (
|
|
|
|
<FilterList
|
|
|
|
initialText=""
|
|
|
|
options={allNotes}
|
|
|
|
onSelect={(opt) => {
|
|
|
|
console.log("Selected", opt);
|
|
|
|
onNavigate(opt.name);
|
|
|
|
}}
|
|
|
|
></FilterList>
|
|
|
|
) : null}
|
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;
|
|
|
|
fs.readNote("start").then((text) => {
|
2022-02-21 10:27:30 +00:00
|
|
|
editor!.load("start", text);
|
|
|
|
dispatch({
|
|
|
|
type: "loaded",
|
|
|
|
name: "start",
|
|
|
|
});
|
2022-02-21 08:32:36 +00:00
|
|
|
});
|
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
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-02-21 10:27:30 +00:00
|
|
|
<TopBar
|
|
|
|
currentNote={appState.currentNote}
|
|
|
|
isSaved={appState.isSaved}
|
|
|
|
isFiltering={appState.isFiltering}
|
|
|
|
allNotes={appState.allNotes}
|
|
|
|
onNavigate={(note) => {
|
|
|
|
dispatch({ type: "stop-navigate" });
|
|
|
|
editor!.focus();
|
|
|
|
fs.readNote(note).then((text) => {
|
|
|
|
editor!.load(note, text);
|
|
|
|
dispatch({
|
|
|
|
type: "loaded",
|
|
|
|
name: note,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
2022-02-21 08:32:36 +00:00
|
|
|
<div id="editor" ref={editorRef}></div>
|
|
|
|
<div id="bottom">Bottom</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-21 10:27:30 +00:00
|
|
|
ReactDOM.render(<AppView />, document.body);
|