2022-02-24 16:24:49 +00:00
|
|
|
import {
|
|
|
|
autocompletion,
|
2022-02-25 14:34:00 +00:00
|
|
|
Completion,
|
2022-02-24 16:24:49 +00:00
|
|
|
CompletionContext,
|
|
|
|
completionKeymap,
|
|
|
|
CompletionResult,
|
|
|
|
} from "@codemirror/autocomplete";
|
2022-02-21 08:32:36 +00:00
|
|
|
import { closeBrackets, closeBracketsKeymap } from "@codemirror/closebrackets";
|
|
|
|
import { indentWithTab, standardKeymap } from "@codemirror/commands";
|
|
|
|
import { history, historyKeymap } from "@codemirror/history";
|
2022-02-24 16:24:49 +00:00
|
|
|
import { indentOnInput, syntaxTree } from "@codemirror/language";
|
2022-02-21 08:32:36 +00:00
|
|
|
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,
|
2022-02-25 14:34:00 +00:00
|
|
|
KeyBinding,
|
2022-02-21 08:32:36 +00:00
|
|
|
keymap,
|
|
|
|
} from "@codemirror/view";
|
2022-02-24 16:24:49 +00:00
|
|
|
import React, { useEffect, useReducer } from "react";
|
2022-02-21 10:27:30 +00:00
|
|
|
import ReactDOM from "react-dom";
|
2022-02-24 16:24:49 +00:00
|
|
|
import coreManifest from "../../plugins/dist/core.plugin.json";
|
2022-02-21 08:32:36 +00:00
|
|
|
import * as commands from "./commands";
|
2022-02-25 10:27:58 +00:00
|
|
|
import { CommandPalette } from "./components/command_palette";
|
2022-02-24 16:24:49 +00:00
|
|
|
import { NavigationBar } from "./components/navigation_bar";
|
2022-02-26 12:26:31 +00:00
|
|
|
import { PageNavigator } from "./components/page_navigator";
|
2022-02-24 16:24:49 +00:00
|
|
|
import { StatusBar } from "./components/status_bar";
|
2022-02-26 12:26:31 +00:00
|
|
|
import { Space } from "./space";
|
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-24 16:24:49 +00:00
|
|
|
import { BrowserSystem } from "./plugins/browser_system";
|
2022-02-25 14:34:00 +00:00
|
|
|
import { Manifest, slashCommandRegexp } from "./plugins/types";
|
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-24 16:24:49 +00:00
|
|
|
import dbSyscalls from "./syscalls/db.localstorage";
|
2022-02-26 11:59:16 +00:00
|
|
|
import { Plugin } from "./plugins/runtime";
|
2022-02-24 16:24:49 +00:00
|
|
|
import editorSyscalls from "./syscalls/editor.browser";
|
2022-02-26 17:02:09 +00:00
|
|
|
import spaceSyscalls from "./syscalls/space.native";
|
2022-02-24 16:24:49 +00:00
|
|
|
import {
|
|
|
|
Action,
|
|
|
|
AppCommand,
|
|
|
|
AppViewState,
|
|
|
|
initialViewState,
|
2022-02-26 12:26:31 +00:00
|
|
|
PageMeta,
|
2022-02-24 16:24:49 +00:00
|
|
|
} from "./types";
|
2022-02-26 12:26:31 +00:00
|
|
|
import { AppEvent, ClickEvent } from "./app_event";
|
2022-02-24 16:24:49 +00:00
|
|
|
import { safeRun } from "./util";
|
2022-02-23 13:09:26 +00:00
|
|
|
|
2022-02-26 12:26:31 +00:00
|
|
|
class PageState {
|
2022-02-23 13:09:26 +00:00
|
|
|
editorState: EditorState;
|
|
|
|
scrollTop: number;
|
2022-02-26 12:26:31 +00:00
|
|
|
meta: PageMeta;
|
2022-02-23 13:09:26 +00:00
|
|
|
|
2022-02-26 12:26:31 +00:00
|
|
|
constructor(editorState: EditorState, scrollTop: number, meta: PageMeta) {
|
2022-02-23 13:09:26 +00:00
|
|
|
this.editorState = editorState;
|
|
|
|
this.scrollTop = scrollTop;
|
2022-02-25 14:34:00 +00:00
|
|
|
this.meta = meta;
|
2022-02-23 13:09:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-22 16:36:24 +00:00
|
|
|
|
2022-02-25 14:34:00 +00:00
|
|
|
const watchInterval = 5000;
|
|
|
|
|
2022-02-24 16:24:49 +00:00
|
|
|
export class Editor {
|
2022-02-23 13:09:26 +00:00
|
|
|
editorView?: EditorView;
|
|
|
|
viewState: AppViewState;
|
|
|
|
viewDispatch: React.Dispatch<Action>;
|
|
|
|
$hashChange?: () => void;
|
2022-02-26 12:26:31 +00:00
|
|
|
openPages: Map<string, PageState>;
|
|
|
|
fs: Space;
|
2022-02-24 16:24:49 +00:00
|
|
|
editorCommands: Map<string, AppCommand>;
|
2022-02-26 11:59:16 +00:00
|
|
|
plugins: Plugin[];
|
2022-02-23 13:09:26 +00:00
|
|
|
|
2022-02-26 12:26:31 +00:00
|
|
|
constructor(fs: Space, parent: Element) {
|
2022-02-24 16:24:49 +00:00
|
|
|
this.editorCommands = new Map();
|
2022-02-26 12:26:31 +00:00
|
|
|
this.openPages = new Map();
|
2022-02-26 11:59:16 +00:00
|
|
|
this.plugins = [];
|
2022-02-23 13:09:26 +00:00
|
|
|
this.fs = fs;
|
|
|
|
this.viewState = initialViewState;
|
|
|
|
this.viewDispatch = () => {};
|
|
|
|
this.render(parent);
|
|
|
|
this.editorView = new EditorView({
|
|
|
|
state: this.createEditorState(""),
|
|
|
|
parent: document.getElementById("editor")!,
|
2022-02-21 08:32:36 +00:00
|
|
|
});
|
2022-02-23 13:09:26 +00:00
|
|
|
this.addListeners();
|
2022-02-26 11:59:16 +00:00
|
|
|
// this.watch();
|
2022-02-24 16:24:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
2022-02-26 12:26:31 +00:00
|
|
|
await this.loadPageList();
|
2022-02-24 16:24:49 +00:00
|
|
|
await this.loadPlugins();
|
2022-02-23 13:09:26 +00:00
|
|
|
this.$hashChange!();
|
2022-02-24 16:24:49 +00:00
|
|
|
this.focus();
|
2022-02-26 12:26:31 +00:00
|
|
|
await this.dispatchAppEvent("app:ready");
|
2022-02-24 16:24:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async loadPlugins() {
|
|
|
|
const system = new BrowserSystem("plugin");
|
2022-02-26 17:02:09 +00:00
|
|
|
system.registerSyscalls(
|
|
|
|
dbSyscalls,
|
|
|
|
editorSyscalls(this),
|
|
|
|
spaceSyscalls(this)
|
|
|
|
);
|
2022-02-24 16:24:49 +00:00
|
|
|
|
|
|
|
await system.bootServiceWorker();
|
|
|
|
console.log("Now loading core plugin");
|
2022-02-26 11:59:16 +00:00
|
|
|
let mainPlugin = await system.load("core", coreManifest);
|
|
|
|
this.plugins.push(mainPlugin);
|
2022-02-24 16:24:49 +00:00
|
|
|
this.editorCommands = new Map<string, AppCommand>();
|
2022-02-26 11:59:16 +00:00
|
|
|
for (let plugin of this.plugins) {
|
|
|
|
this.buildCommands(plugin);
|
|
|
|
}
|
|
|
|
this.viewDispatch({
|
|
|
|
type: "update-commands",
|
|
|
|
commands: this.editorCommands,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private buildCommands(plugin: Plugin) {
|
|
|
|
const cmds = plugin.manifest!.commands;
|
2022-02-24 16:24:49 +00:00
|
|
|
for (let name in cmds) {
|
|
|
|
let cmd = cmds[name];
|
|
|
|
this.editorCommands.set(name, {
|
|
|
|
command: cmd,
|
2022-02-26 12:26:31 +00:00
|
|
|
run: async (arg): Promise<any> => {
|
2022-02-26 11:59:16 +00:00
|
|
|
return await plugin.invoke(cmd.invoke, [arg]);
|
2022-02-24 16:24:49 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2022-02-26 11:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Parallelize?
|
|
|
|
async dispatchAppEvent(name: AppEvent, data?: any) {
|
|
|
|
for (let plugin of this.plugins) {
|
|
|
|
await plugin.dispatchEvent(name, data);
|
|
|
|
}
|
2022-02-23 13:09:26 +00:00
|
|
|
}
|
|
|
|
|
2022-02-26 12:26:31 +00:00
|
|
|
get currentPage(): PageMeta | undefined {
|
|
|
|
return this.viewState.currentPage;
|
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-24 16:24:49 +00:00
|
|
|
let commandKeyBindings: KeyBinding[] = [];
|
|
|
|
for (let def of this.editorCommands.values()) {
|
|
|
|
if (def.command.key) {
|
|
|
|
commandKeyBindings.push({
|
|
|
|
key: def.command.key,
|
|
|
|
mac: def.command.mac,
|
|
|
|
run: (): boolean => {
|
|
|
|
Promise.resolve()
|
|
|
|
.then(async () => {
|
2022-02-26 12:26:31 +00:00
|
|
|
await def.run(null);
|
2022-02-24 16:24:49 +00:00
|
|
|
})
|
|
|
|
.catch((e) => console.error(e));
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-02-21 08:32:36 +00:00
|
|
|
return EditorState.create({
|
|
|
|
doc: text,
|
|
|
|
extensions: [
|
|
|
|
highlightSpecialChars(),
|
|
|
|
history(),
|
|
|
|
drawSelection(),
|
|
|
|
dropCursor(),
|
|
|
|
indentOnInput(),
|
|
|
|
customMarkdownStyle,
|
|
|
|
bracketMatching(),
|
|
|
|
closeBrackets(),
|
2022-02-22 16:36:24 +00:00
|
|
|
autocompletion({
|
2022-02-25 14:34:00 +00:00
|
|
|
override: [
|
2022-02-26 12:26:31 +00:00
|
|
|
this.pageCompleter.bind(this),
|
2022-02-25 14:34:00 +00:00
|
|
|
this.commandCompleter.bind(this),
|
|
|
|
],
|
2022-02-22 16:36:24 +00:00
|
|
|
}),
|
2022-02-21 08:32:36 +00:00
|
|
|
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,
|
2022-02-24 16:24:49 +00:00
|
|
|
...commandKeyBindings,
|
2022-02-21 08:32:36 +00:00
|
|
|
{
|
|
|
|
key: "Ctrl-b",
|
|
|
|
mac: "Cmd-b",
|
|
|
|
run: commands.insertMarker("**"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "Ctrl-i",
|
|
|
|
mac: "Cmd-i",
|
|
|
|
run: commands.insertMarker("_"),
|
|
|
|
},
|
|
|
|
{
|
2022-02-26 17:02:09 +00:00
|
|
|
key: "Ctrl-p",
|
|
|
|
mac: "Cmd-p",
|
2022-02-25 14:34:00 +00:00
|
|
|
run: (): boolean => {
|
|
|
|
window.open(location.href, "_blank")!.focus();
|
2022-02-21 08:32:36 +00:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
2022-02-21 10:27:30 +00:00
|
|
|
{
|
2022-02-26 17:02:09 +00:00
|
|
|
key: "Ctrl-e",
|
|
|
|
mac: "Cmd-e",
|
2022-02-21 10:27:30 +00:00
|
|
|
run: (target): boolean => {
|
2022-02-23 13:09:26 +00:00
|
|
|
this.viewDispatch({ type: "start-navigate" });
|
2022-02-21 10:27:30 +00:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
2022-02-22 13:18:37 +00:00
|
|
|
{
|
|
|
|
key: "Ctrl-.",
|
|
|
|
mac: "Cmd-.",
|
|
|
|
run: (target): boolean => {
|
2022-02-25 14:34:00 +00:00
|
|
|
this.viewDispatch({
|
|
|
|
type: "show-palette",
|
|
|
|
});
|
2022-02-22 13:18:37 +00:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
2022-02-21 08:32:36 +00:00
|
|
|
]),
|
|
|
|
EditorView.domEventHandlers({
|
2022-02-26 12:26:31 +00:00
|
|
|
click: (event: MouseEvent, view: EditorView) => {
|
|
|
|
safeRun(async () => {
|
|
|
|
let clickEvent: ClickEvent = {
|
|
|
|
ctrlKey: event.ctrlKey,
|
|
|
|
metaKey: event.metaKey,
|
|
|
|
altKey: event.altKey,
|
|
|
|
pos: view.posAtCoords(event)!,
|
|
|
|
};
|
|
|
|
await this.dispatchAppEvent("page:click", clickEvent);
|
|
|
|
});
|
|
|
|
},
|
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
|
|
|
|
2022-02-26 12:26:31 +00:00
|
|
|
pageCompleter(ctx: CompletionContext): CompletionResult | null {
|
2022-02-25 14:34:00 +00:00
|
|
|
let prefix = ctx.matchBefore(/\[\[[\w\s]*/);
|
2022-02-22 16:36:24 +00:00
|
|
|
if (!prefix) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
from: prefix.from + 2,
|
2022-02-26 12:26:31 +00:00
|
|
|
options: this.viewState.allPages.map((pageMeta) => ({
|
|
|
|
label: pageMeta.name,
|
|
|
|
type: "page",
|
2022-02-22 16:36:24 +00:00
|
|
|
})),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-25 14:34:00 +00:00
|
|
|
commandCompleter(ctx: CompletionContext): CompletionResult | null {
|
|
|
|
let prefix = ctx.matchBefore(slashCommandRegexp);
|
|
|
|
if (!prefix) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let options: Completion[] = [];
|
|
|
|
for (let [name, def] of this.viewState.commands) {
|
|
|
|
if (!def.command.slashCommand) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
options.push({
|
|
|
|
label: def.command.slashCommand,
|
|
|
|
detail: name,
|
|
|
|
apply: () => {
|
|
|
|
this.editorView?.dispatch({
|
|
|
|
changes: {
|
|
|
|
from: prefix!.from,
|
|
|
|
to: ctx.pos,
|
|
|
|
insert: "",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
safeRun(async () => {
|
2022-02-26 12:26:31 +00:00
|
|
|
def.run(null);
|
2022-02-25 14:34:00 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
from: prefix.from + 1,
|
|
|
|
options: options,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-21 10:27:30 +00:00
|
|
|
update(value: null, transaction: Transaction): null {
|
|
|
|
if (transaction.docChanged) {
|
2022-02-23 13:09:26 +00:00
|
|
|
this.viewDispatch({
|
2022-02-26 12:26:31 +00:00
|
|
|
type: "page-updated",
|
2022-02-21 10:27:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
click(event: MouseEvent, view: EditorView) {
|
2022-02-26 12:26:31 +00:00
|
|
|
// if (event.metaKey || event.ctrlKey) {
|
|
|
|
// let coords = view.posAtCoords(event)!;
|
|
|
|
// let node = syntaxTree(view.state).resolveInner(coords);
|
|
|
|
// if (node && node.name === "WikiLinkPage") {
|
|
|
|
// let pageName = view.state.sliceDoc(node.from, node.to);
|
|
|
|
// this.navigate(pageName);
|
|
|
|
// }
|
|
|
|
// if (node && node.name === "TaskMarker") {
|
|
|
|
// let checkBoxText = view.state.sliceDoc(node.from, node.to);
|
|
|
|
// if (checkBoxText === "[x]" || checkBoxText === "[X]") {
|
|
|
|
// view.dispatch({
|
|
|
|
// changes: { from: node.from, to: node.to, insert: "[ ]" },
|
|
|
|
// });
|
|
|
|
// } else {
|
|
|
|
// view.dispatch({
|
|
|
|
// changes: { from: node.from, to: node.to, insert: "[x]" },
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return false;
|
|
|
|
// }
|
2022-02-21 10:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async save() {
|
2022-02-23 13:09:26 +00:00
|
|
|
const editorState = this.editorView!.state;
|
|
|
|
|
2022-02-26 12:26:31 +00:00
|
|
|
if (!this.currentPage) {
|
2022-02-23 13:09:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Write to file system
|
2022-02-26 12:26:31 +00:00
|
|
|
let pageMeta = await this.fs.writePage(
|
|
|
|
this.currentPage.name,
|
2022-02-23 13:09:26 +00:00
|
|
|
editorState.sliceDoc()
|
2022-02-22 13:18:37 +00:00
|
|
|
);
|
2022-02-23 13:09:26 +00:00
|
|
|
|
2022-02-26 12:26:31 +00:00
|
|
|
// Update in open page cache
|
|
|
|
this.openPages.set(
|
|
|
|
this.currentPage.name,
|
|
|
|
new PageState(editorState, this.editorView!.scrollDOM.scrollTop, pageMeta)
|
2022-02-23 13:09:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Dispatch update to view
|
2022-02-26 12:26:31 +00:00
|
|
|
this.viewDispatch({ type: "page-saved", meta: pageMeta });
|
2022-02-23 13:09:26 +00:00
|
|
|
|
2022-02-26 12:26:31 +00:00
|
|
|
// If a new page was created, let's refresh the page list
|
|
|
|
if (pageMeta.created) {
|
|
|
|
await this.loadPageList();
|
2022-02-22 13:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-26 12:26:31 +00:00
|
|
|
async loadPageList() {
|
|
|
|
let pagesMeta = await this.fs.listPages();
|
2022-02-23 13:09:26 +00:00
|
|
|
this.viewDispatch({
|
2022-02-26 12:26:31 +00:00
|
|
|
type: "pages-listed",
|
|
|
|
pages: pagesMeta,
|
2022-02-22 13:18:37 +00:00
|
|
|
});
|
2022-02-21 10:27:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 14:34:00 +00:00
|
|
|
watch() {
|
|
|
|
setInterval(() => {
|
|
|
|
safeRun(async () => {
|
2022-02-26 12:26:31 +00:00
|
|
|
if (!this.currentPage) {
|
2022-02-25 14:34:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-26 12:26:31 +00:00
|
|
|
const currentPageName = this.currentPage.name;
|
|
|
|
let newPageMeta = await this.fs.getPageMeta(currentPageName);
|
2022-02-25 14:34:00 +00:00
|
|
|
if (
|
2022-02-26 12:26:31 +00:00
|
|
|
this.currentPage.lastModified.getTime() <
|
|
|
|
newPageMeta.lastModified.getTime()
|
2022-02-25 14:34:00 +00:00
|
|
|
) {
|
|
|
|
console.log("File changed on disk, reloading");
|
2022-02-26 12:26:31 +00:00
|
|
|
let pageData = await this.fs.readPage(currentPageName);
|
|
|
|
this.openPages.set(
|
|
|
|
newPageMeta.name,
|
|
|
|
new PageState(this.createEditorState(pageData.text), 0, newPageMeta)
|
2022-02-25 14:34:00 +00:00
|
|
|
);
|
2022-02-26 12:26:31 +00:00
|
|
|
await this.loadPage(currentPageName);
|
2022-02-25 14:34:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}, watchInterval);
|
|
|
|
}
|
|
|
|
|
2022-02-21 10:27:30 +00:00
|
|
|
focus() {
|
2022-02-23 13:09:26 +00:00
|
|
|
this.editorView!.focus();
|
2022-02-21 10:27:30 +00:00
|
|
|
}
|
2022-02-21 12:25:41 +00:00
|
|
|
|
2022-02-23 13:09:26 +00:00
|
|
|
async navigate(name: string) {
|
2022-02-21 12:25:41 +00:00
|
|
|
location.hash = encodeURIComponent(name);
|
|
|
|
}
|
2022-02-21 08:32:36 +00:00
|
|
|
|
2022-02-23 13:09:26 +00:00
|
|
|
hashChange() {
|
|
|
|
Promise.resolve()
|
|
|
|
.then(async () => {
|
|
|
|
await this.save();
|
2022-02-26 12:26:31 +00:00
|
|
|
const pageName = decodeURIComponent(location.hash.substring(1));
|
|
|
|
console.log("Now navigating to", pageName);
|
2022-02-23 13:09:26 +00:00
|
|
|
|
|
|
|
if (!this.editorView) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-26 12:26:31 +00:00
|
|
|
await this.loadPage(pageName);
|
2022-02-23 13:09:26 +00:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
}
|
2022-02-21 08:32:36 +00:00
|
|
|
|
2022-02-26 12:26:31 +00:00
|
|
|
async loadPage(pageName: string) {
|
|
|
|
let pageState = this.openPages.get(pageName);
|
|
|
|
if (!pageState) {
|
|
|
|
let pageData = await this.fs.readPage(pageName);
|
|
|
|
pageState = new PageState(
|
|
|
|
this.createEditorState(pageData.text),
|
2022-02-25 14:34:00 +00:00
|
|
|
0,
|
2022-02-26 12:26:31 +00:00
|
|
|
pageData.meta
|
2022-02-25 14:34:00 +00:00
|
|
|
);
|
2022-02-26 12:26:31 +00:00
|
|
|
this.openPages.set(pageName, pageState!);
|
2022-02-25 14:34:00 +00:00
|
|
|
}
|
2022-02-26 12:26:31 +00:00
|
|
|
this.editorView!.setState(pageState!.editorState);
|
|
|
|
this.editorView!.scrollDOM.scrollTop = pageState!.scrollTop;
|
2022-02-25 14:34:00 +00:00
|
|
|
|
|
|
|
this.viewDispatch({
|
2022-02-26 12:26:31 +00:00
|
|
|
type: "page-loaded",
|
|
|
|
meta: pageState.meta,
|
2022-02-25 14:34:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-23 13:09:26 +00:00
|
|
|
addListeners() {
|
|
|
|
this.$hashChange = this.hashChange.bind(this);
|
|
|
|
window.addEventListener("hashchange", this.$hashChange);
|
2022-02-22 13:18:37 +00:00
|
|
|
}
|
2022-02-21 10:27:30 +00:00
|
|
|
|
2022-02-23 13:09:26 +00:00
|
|
|
dispose() {
|
|
|
|
if (this.$hashChange) {
|
|
|
|
window.removeEventListener("hashchange", this.$hashChange);
|
2022-02-21 12:25:41 +00:00
|
|
|
}
|
2022-02-23 13:09:26 +00:00
|
|
|
}
|
2022-02-21 10:27:30 +00:00
|
|
|
|
2022-02-23 13:09:26 +00:00
|
|
|
ViewComponent(): React.ReactElement {
|
|
|
|
const [viewState, dispatch] = useReducer(reducer, initialViewState);
|
|
|
|
this.viewState = viewState;
|
|
|
|
this.viewDispatch = dispatch;
|
2022-02-21 08:32:36 +00:00
|
|
|
|
2022-02-23 13:09:26 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!location.hash) {
|
|
|
|
this.navigate("start");
|
2022-02-22 13:18:37 +00:00
|
|
|
}
|
2022-02-23 13:09:26 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
// Auto save
|
|
|
|
useEffect(() => {
|
|
|
|
const id = setTimeout(() => {
|
|
|
|
if (!viewState.isSaved) {
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
}, 2000);
|
|
|
|
return () => {
|
|
|
|
clearTimeout(id);
|
|
|
|
};
|
|
|
|
}, [viewState.isSaved]);
|
|
|
|
|
|
|
|
let editor = this;
|
|
|
|
|
2022-02-25 10:27:58 +00:00
|
|
|
useEffect(() => {
|
2022-02-26 12:26:31 +00:00
|
|
|
if (viewState.currentPage) {
|
|
|
|
document.title = viewState.currentPage.name;
|
2022-02-25 10:27:58 +00:00
|
|
|
}
|
2022-02-26 12:26:31 +00:00
|
|
|
}, [viewState.currentPage]);
|
2022-02-23 13:09:26 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-02-26 12:26:31 +00:00
|
|
|
{viewState.showPageNavigator && (
|
|
|
|
<PageNavigator
|
|
|
|
allPages={viewState.allPages}
|
|
|
|
onNavigate={(page) => {
|
2022-02-23 13:09:26 +00:00
|
|
|
dispatch({ type: "stop-navigate" });
|
|
|
|
editor!.focus();
|
2022-02-26 12:26:31 +00:00
|
|
|
if (page) {
|
2022-02-23 13:09:26 +00:00
|
|
|
editor
|
|
|
|
?.save()
|
|
|
|
.then(() => {
|
2022-02-26 12:26:31 +00:00
|
|
|
editor!.navigate(page);
|
2022-02-23 13:09:26 +00:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
2022-02-26 12:26:31 +00:00
|
|
|
alert("Could not save page, not switching");
|
2022-02-23 13:09:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{viewState.showCommandPalette && (
|
|
|
|
<CommandPalette
|
|
|
|
onTrigger={(cmd) => {
|
|
|
|
dispatch({ type: "hide-palette" });
|
|
|
|
editor!.focus();
|
|
|
|
if (cmd) {
|
2022-02-24 16:24:49 +00:00
|
|
|
safeRun(async () => {
|
2022-02-26 12:26:31 +00:00
|
|
|
let result = await cmd.run(null);
|
2022-02-24 16:24:49 +00:00
|
|
|
console.log("Result of command", result);
|
|
|
|
});
|
2022-02-23 13:09:26 +00:00
|
|
|
}
|
|
|
|
}}
|
2022-02-24 16:24:49 +00:00
|
|
|
commands={viewState.commands}
|
2022-02-23 13:09:26 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<NavigationBar
|
2022-02-26 12:26:31 +00:00
|
|
|
currentPage={viewState.currentPage}
|
2022-02-23 13:09:26 +00:00
|
|
|
onClick={() => {
|
|
|
|
dispatch({ type: "start-navigate" });
|
2022-02-22 13:18:37 +00:00
|
|
|
}}
|
|
|
|
/>
|
2022-02-23 13:09:26 +00:00
|
|
|
<div id="editor"></div>
|
|
|
|
<StatusBar isSaved={viewState.isSaved} editorView={this.editorView} />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render(container: ReactDOM.Container) {
|
|
|
|
const ViewComponent = this.ViewComponent.bind(this);
|
|
|
|
ReactDOM.render(<ViewComponent />, container);
|
|
|
|
}
|
2022-02-21 08:32:36 +00:00
|
|
|
}
|