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";
|
|
|
|
import { bracketMatching } from "@codemirror/matchbrackets";
|
|
|
|
import { searchKeymap } from "@codemirror/search";
|
2022-03-07 12:34:25 +00:00
|
|
|
import { EditorState, StateField, Transaction, Text } 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-03-03 09:35:32 +00:00
|
|
|
|
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-03-04 10:21:11 +00:00
|
|
|
import coreManifest from "./generated/core.plug.json";
|
2022-03-04 11:09:25 +00:00
|
|
|
|
2022-03-03 09:35:32 +00:00
|
|
|
// @ts-ignore
|
|
|
|
window.coreManifest = coreManifest;
|
|
|
|
import { AppEvent, AppEventDispatcher, ClickEvent } from "./app_event";
|
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-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-03-03 09:35:32 +00:00
|
|
|
import { TopBar } from "./components/top_bar";
|
|
|
|
import { Indexer } from "./indexer";
|
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-03-03 09:35:32 +00:00
|
|
|
import { IPageNavigator, PathPageNavigator } from "./navigator";
|
2022-02-21 08:32:36 +00:00
|
|
|
import customMarkDown from "./parser";
|
2022-03-07 09:21:02 +00:00
|
|
|
import { System } from "../../plugbox/src/runtime";
|
2022-03-04 11:17:44 +00:00
|
|
|
import { Plug } from "../../plugbox/src/runtime";
|
2022-03-04 11:09:25 +00:00
|
|
|
import { slashCommandRegexp } from "./types";
|
2022-03-04 10:21:11 +00:00
|
|
|
|
2022-02-22 13:18:37 +00:00
|
|
|
import reducer from "./reducer";
|
2022-03-03 09:35:32 +00:00
|
|
|
import { smartQuoteKeymap } from "./smart_quotes";
|
2022-03-06 09:27:36 +00:00
|
|
|
import { HttpRemoteSpace } from "./space";
|
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";
|
|
|
|
import editorSyscalls from "./syscalls/editor.browser";
|
2022-02-28 13:35:51 +00:00
|
|
|
import indexerSyscalls from "./syscalls/indexer.native";
|
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-03-04 11:09:25 +00:00
|
|
|
NuggetHook,
|
2022-02-26 12:26:31 +00:00
|
|
|
PageMeta,
|
2022-02-24 16:24:49 +00:00
|
|
|
} from "./types";
|
|
|
|
import { safeRun } from "./util";
|
2022-02-23 13:09:26 +00:00
|
|
|
|
2022-03-07 12:34:25 +00:00
|
|
|
import { collabExtension } from "./collab";
|
|
|
|
|
|
|
|
import { Document } from "./collab";
|
|
|
|
|
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-28 13:35:51 +00:00
|
|
|
export class Editor implements AppEventDispatcher {
|
2022-02-23 13:09:26 +00:00
|
|
|
editorView?: EditorView;
|
|
|
|
viewState: AppViewState;
|
|
|
|
viewDispatch: React.Dispatch<Action>;
|
2022-02-26 12:26:31 +00:00
|
|
|
openPages: Map<string, PageState>;
|
2022-03-06 09:27:36 +00:00
|
|
|
space: HttpRemoteSpace;
|
2022-02-24 16:24:49 +00:00
|
|
|
editorCommands: Map<string, AppCommand>;
|
2022-03-04 11:17:44 +00:00
|
|
|
plugs: Plug<NuggetHook>[];
|
2022-02-28 13:35:51 +00:00
|
|
|
indexer: Indexer;
|
|
|
|
navigationResolve?: (val: undefined) => void;
|
|
|
|
pageNavigator: IPageNavigator;
|
2022-02-23 13:09:26 +00:00
|
|
|
|
2022-03-06 09:27:36 +00:00
|
|
|
constructor(space: HttpRemoteSpace, 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-03-04 11:17:44 +00:00
|
|
|
this.plugs = [];
|
2022-02-28 13:35:51 +00:00
|
|
|
this.space = space;
|
2022-02-23 13:09:26 +00:00
|
|
|
this.viewState = initialViewState;
|
|
|
|
this.viewDispatch = () => {};
|
|
|
|
this.render(parent);
|
|
|
|
this.editorView = new EditorView({
|
2022-03-07 12:34:25 +00:00
|
|
|
state: this.createEditorState(
|
|
|
|
new Document(Text.of([""]), {
|
|
|
|
name: "",
|
|
|
|
lastModified: new Date(),
|
|
|
|
version: 0,
|
|
|
|
})
|
|
|
|
),
|
2022-02-23 13:09:26 +00:00
|
|
|
parent: document.getElementById("editor")!,
|
2022-02-21 08:32:36 +00:00
|
|
|
});
|
2022-02-28 13:35:51 +00:00
|
|
|
this.pageNavigator = new PathPageNavigator();
|
|
|
|
this.indexer = new Indexer("page-index", space);
|
2022-03-07 12:34:25 +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-03-04 11:17:44 +00:00
|
|
|
await this.loadPlugs();
|
2022-02-24 16:24:49 +00:00
|
|
|
this.focus();
|
2022-02-28 13:35:51 +00:00
|
|
|
|
|
|
|
this.pageNavigator.subscribe(async (pageName) => {
|
|
|
|
await this.save();
|
|
|
|
console.log("Now navigating to", pageName);
|
|
|
|
|
|
|
|
if (!this.editorView) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.loadPage(pageName);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.pageNavigator.getCurrentPage() === "") {
|
|
|
|
this.pageNavigator.navigate("start");
|
|
|
|
}
|
2022-02-24 16:24:49 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 11:17:44 +00:00
|
|
|
async loadPlugs() {
|
2022-03-07 09:21:02 +00:00
|
|
|
const system = new System<NuggetHook>();
|
2022-02-26 17:02:09 +00:00
|
|
|
system.registerSyscalls(
|
|
|
|
dbSyscalls,
|
|
|
|
editorSyscalls(this),
|
2022-02-28 13:35:51 +00:00
|
|
|
spaceSyscalls(this),
|
|
|
|
indexerSyscalls(this.indexer)
|
2022-02-26 17:02:09 +00:00
|
|
|
);
|
2022-02-24 16:24:49 +00:00
|
|
|
|
2022-03-04 11:17:44 +00:00
|
|
|
console.log("Now loading core plug");
|
|
|
|
let mainPlug = await system.load("core", coreManifest);
|
|
|
|
this.plugs.push(mainPlug);
|
2022-02-24 16:24:49 +00:00
|
|
|
this.editorCommands = new Map<string, AppCommand>();
|
2022-03-04 11:17:44 +00:00
|
|
|
for (let plug of this.plugs) {
|
|
|
|
this.buildCommands(plug);
|
2022-02-26 11:59:16 +00:00
|
|
|
}
|
|
|
|
this.viewDispatch({
|
|
|
|
type: "update-commands",
|
|
|
|
commands: this.editorCommands,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-04 11:17:44 +00:00
|
|
|
private buildCommands(plug: Plug<NuggetHook>) {
|
|
|
|
const cmds = plug.manifest!.hooks.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-03-04 11:17:44 +00:00
|
|
|
return await plug.invoke(cmd.invoke, [arg]);
|
2022-02-24 16:24:49 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2022-02-26 11:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Parallelize?
|
2022-02-27 09:17:43 +00:00
|
|
|
async dispatchAppEvent(name: AppEvent, data?: any): Promise<any[]> {
|
|
|
|
let results: any[] = [];
|
2022-03-04 11:17:44 +00:00
|
|
|
for (let plug of this.plugs) {
|
|
|
|
let plugResults = await plug.dispatchEvent(name, data);
|
|
|
|
if (plugResults) {
|
|
|
|
for (let result of plugResults) {
|
2022-02-27 09:17:43 +00:00
|
|
|
results.push(result);
|
|
|
|
}
|
|
|
|
}
|
2022-02-26 11:59:16 +00:00
|
|
|
}
|
2022-02-27 09:17:43 +00:00
|
|
|
return results;
|
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-03-07 12:34:25 +00:00
|
|
|
createEditorState(doc: Document): 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({
|
2022-03-07 12:34:25 +00:00
|
|
|
doc: doc.text,
|
2022-02-21 08:32:36 +00:00
|
|
|
extensions: [
|
|
|
|
highlightSpecialChars(),
|
|
|
|
history(),
|
|
|
|
drawSelection(),
|
|
|
|
dropCursor(),
|
2022-03-03 09:35:32 +00:00
|
|
|
// indentOnInput(),
|
2022-02-21 08:32:36 +00:00
|
|
|
customMarkdownStyle,
|
|
|
|
bracketMatching(),
|
|
|
|
closeBrackets(),
|
2022-03-07 12:34:25 +00:00
|
|
|
collabExtension(
|
|
|
|
doc.meta.name,
|
|
|
|
doc.meta.version!,
|
|
|
|
this.space,
|
|
|
|
this.reloadPage.bind(this)
|
|
|
|
),
|
2022-02-22 16:36:24 +00:00
|
|
|
autocompletion({
|
2022-02-25 14:34:00 +00:00
|
|
|
override: [
|
2022-03-04 11:17:44 +00:00
|
|
|
this.plugCompleter.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" },
|
2022-03-03 09:35:32 +00:00
|
|
|
{ selector: "ATXHeading3", class: "line-h3" },
|
|
|
|
{ selector: "ListItem", class: "line-li", nesting: true },
|
2022-02-21 08:32:36 +00:00
|
|
|
{ selector: "Blockquote", class: "line-blockquote" },
|
2022-03-04 16:04:26 +00:00
|
|
|
{ selector: "Task", class: "line-task" },
|
2022-02-21 08:32:36 +00:00
|
|
|
{ selector: "CodeBlock", class: "line-code" },
|
|
|
|
{ selector: "FencedCode", class: "line-fenced-code" },
|
2022-03-03 09:35:32 +00:00
|
|
|
{ selector: "Comment", class: "line-comment" },
|
2022-03-04 16:04:26 +00:00
|
|
|
{ selector: "BulletList", class: "line-ul" },
|
|
|
|
{ selector: "OrderedList", class: "line-ol" },
|
2022-02-21 08:32:36 +00:00
|
|
|
]),
|
|
|
|
keymap.of([
|
2022-03-01 15:57:20 +00:00
|
|
|
...smartQuoteKeymap,
|
2022-02-21 08:32:36 +00:00
|
|
|
...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-03-01 15:57:20 +00:00
|
|
|
key: "Ctrl-k",
|
|
|
|
mac: "Cmd-k",
|
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-27 09:17:43 +00:00
|
|
|
{
|
|
|
|
key: "Ctrl-s",
|
|
|
|
mac: "Cmd-s",
|
|
|
|
run: (target): boolean => {
|
|
|
|
this.save();
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
2022-02-22 13:18:37 +00:00
|
|
|
{
|
|
|
|
key: "Ctrl-.",
|
|
|
|
mac: "Cmd-.",
|
|
|
|
run: (target): boolean => {
|
2022-03-04 09:26:41 +00:00
|
|
|
console.log("YO");
|
|
|
|
|
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-03-04 16:04:26 +00:00
|
|
|
// focus: (event: FocusEvent, view: EditorView) => {
|
|
|
|
// console.log("Got focus");
|
|
|
|
// document.body.classList.add("keyboard");
|
|
|
|
// },
|
|
|
|
// blur: (event: FocusEvent, view: EditorView) => {
|
|
|
|
// console.log("Lost focus");
|
|
|
|
// document.body.classList.remove("keyboard");
|
|
|
|
// },
|
|
|
|
// focusout: (event: FocusEvent, view: EditorView) => {
|
|
|
|
// window.scrollTo(0, 0);
|
|
|
|
// },
|
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-03-07 12:34:25 +00:00
|
|
|
reloadPage() {}
|
|
|
|
|
2022-03-04 11:17:44 +00:00
|
|
|
async plugCompleter(
|
2022-02-27 09:17:43 +00:00
|
|
|
ctx: CompletionContext
|
|
|
|
): Promise<CompletionResult | null> {
|
|
|
|
let allCompletionResults = await this.dispatchAppEvent("editor:complete");
|
|
|
|
if (allCompletionResults.length === 1) {
|
|
|
|
return allCompletionResults[0];
|
|
|
|
} else if (allCompletionResults.length > 1) {
|
|
|
|
console.error(
|
|
|
|
"Got completion results from multiple sources, cannot deal with that",
|
|
|
|
allCompletionResults
|
|
|
|
);
|
2022-02-22 16:36:24 +00:00
|
|
|
}
|
2022-02-27 09:17:43 +00:00
|
|
|
return null;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-02-28 13:35:51 +00:00
|
|
|
|
|
|
|
if (this.viewState.isSaved) {
|
|
|
|
console.log("Page not modified, skipping saving");
|
|
|
|
return;
|
|
|
|
}
|
2022-03-01 15:57:20 +00:00
|
|
|
// Write to the space
|
|
|
|
const pageName = this.currentPage.name;
|
|
|
|
const text = editorState.sliceDoc();
|
|
|
|
let pageMeta = await this.space.writePage(pageName, text);
|
2022-02-23 13:09:26 +00:00
|
|
|
|
2022-02-26 12:26:31 +00:00
|
|
|
// Update in open page cache
|
|
|
|
this.openPages.set(
|
2022-03-01 15:57:20 +00:00
|
|
|
pageName,
|
2022-02-26 12:26:31 +00:00
|
|
|
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-28 13:35:51 +00:00
|
|
|
|
|
|
|
// Reindex page
|
|
|
|
await this.indexPage(text, pageMeta);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async indexPage(text: string, pageMeta: PageMeta) {
|
|
|
|
console.log("Indexing page", pageMeta.name);
|
|
|
|
this.indexer.indexPage(this, pageMeta, text, true);
|
2022-02-22 13:18:37 +00:00
|
|
|
}
|
|
|
|
|
2022-02-26 12:26:31 +00:00
|
|
|
async loadPageList() {
|
2022-02-28 13:35:51 +00:00
|
|
|
let pagesMeta = await this.space.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-28 13:35:51 +00:00
|
|
|
if (this.currentPage && this.viewState.isSaved) {
|
|
|
|
await this.checkForNewVersion(this.currentPage);
|
2022-02-25 14:34:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}, watchInterval);
|
|
|
|
}
|
|
|
|
|
2022-02-28 13:35:51 +00:00
|
|
|
async checkForNewVersion(cachedMeta: PageMeta) {
|
|
|
|
const currentPageName = cachedMeta.name;
|
|
|
|
let newPageMeta = await this.space.getPageMeta(currentPageName);
|
|
|
|
if (
|
|
|
|
cachedMeta.lastModified.getTime() !== newPageMeta.lastModified.getTime()
|
|
|
|
) {
|
|
|
|
console.log("File changed on disk, reloading");
|
2022-03-07 12:34:25 +00:00
|
|
|
let doc = await this.space.openPage(currentPageName);
|
2022-02-28 13:35:51 +00:00
|
|
|
this.openPages.set(
|
2022-03-01 15:57:20 +00:00
|
|
|
currentPageName,
|
2022-03-07 12:34:25 +00:00
|
|
|
new PageState(this.createEditorState(doc), 0, doc.meta)
|
2022-02-28 13:35:51 +00:00
|
|
|
);
|
2022-03-01 15:57:20 +00:00
|
|
|
await this.loadPage(currentPageName, false);
|
2022-02-28 13:35:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-01 15:57:20 +00:00
|
|
|
navigate(name: string) {
|
|
|
|
this.pageNavigator.navigate(name);
|
2022-02-23 13:09:26 +00:00
|
|
|
}
|
2022-02-21 08:32:36 +00:00
|
|
|
|
2022-03-01 15:57:20 +00:00
|
|
|
async loadPage(pageName: string, checkNewVersion: boolean = true) {
|
2022-02-26 12:26:31 +00:00
|
|
|
let pageState = this.openPages.get(pageName);
|
|
|
|
if (!pageState) {
|
2022-03-07 12:34:25 +00:00
|
|
|
let doc = await this.space.openPage(pageName);
|
|
|
|
pageState = new PageState(this.createEditorState(doc), 0, doc.meta);
|
2022-02-26 12:26:31 +00:00
|
|
|
this.openPages.set(pageName, pageState!);
|
2022-03-01 15:57:20 +00:00
|
|
|
// Freshly loaded, no need to check for a new version either way
|
|
|
|
checkNewVersion = false;
|
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-21 10:27:30 +00:00
|
|
|
|
2022-02-28 13:35:51 +00:00
|
|
|
let indexerPageMeta = await this.indexer.getPageIndexPageMeta(pageName);
|
|
|
|
if (
|
|
|
|
(indexerPageMeta &&
|
|
|
|
pageState.meta.lastModified.getTime() !==
|
|
|
|
indexerPageMeta.lastModified.getTime()) ||
|
|
|
|
!indexerPageMeta
|
|
|
|
) {
|
|
|
|
await this.indexPage(pageState.editorState.sliceDoc(), pageState.meta);
|
2022-02-21 12:25:41 +00:00
|
|
|
}
|
2022-03-01 15:57:20 +00:00
|
|
|
|
|
|
|
if (checkNewVersion) {
|
|
|
|
// Loaded page from in-memory cache, let's async see if this page hasn't been updated
|
|
|
|
this.checkForNewVersion(pageState.meta).catch((e) => {
|
|
|
|
console.error("Failed to check for new version");
|
|
|
|
});
|
|
|
|
}
|
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
|
|
|
// 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}
|
2022-02-28 13:35:51 +00:00
|
|
|
currentPage={this.currentPage}
|
2022-02-26 12:26:31 +00:00
|
|
|
onNavigate={(page) => {
|
2022-02-23 13:09:26 +00:00
|
|
|
dispatch({ type: "stop-navigate" });
|
2022-02-28 13:35:51 +00:00
|
|
|
editor.focus();
|
2022-02-26 12:26:31 +00:00
|
|
|
if (page) {
|
2022-02-28 13:35:51 +00:00
|
|
|
safeRun(async () => {
|
|
|
|
editor.navigate(page);
|
|
|
|
});
|
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
|
|
|
/>
|
|
|
|
)}
|
2022-03-03 09:35:32 +00:00
|
|
|
<TopBar
|
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
|
|
|
}
|