1
0
silverbullet/webapp/src/editor.tsx

532 lines
14 KiB
TypeScript
Raw Normal View History

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-03-09 11:25:42 +00:00
import { debounce } from "lodash";
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";
import { RealtimeSpace } 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-03-09 11:25:42 +00:00
import { EditorSelection } from "@codemirror/state";
import { Cursor } from "./cursorEffect";
2022-03-07 12:34:25 +00:00
2022-02-26 12:26:31 +00:00
class PageState {
2022-02-23 13:09:26 +00:00
scrollTop: number;
2022-03-09 11:25:42 +00:00
selection: EditorSelection;
2022-02-23 13:09:26 +00:00
2022-03-09 11:25:42 +00:00
constructor(scrollTop: number, selection: EditorSelection) {
2022-02-23 13:09:26 +00:00
this.scrollTop = scrollTop;
2022-03-09 11:25:42 +00:00
this.selection = selection;
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>;
space: RealtimeSpace;
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-03-09 11:25:42 +00:00
indexCurrentPageDebounced: () => any;
2022-02-23 13:09:26 +00:00
constructor(space: RealtimeSpace, 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({
state: this.createEditorState(
"",
new Document(Text.of([""]), 0, new Map<string, Cursor>())
),
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-09 11:25:42 +00:00
this.indexCurrentPageDebounced = debounce(this.indexCurrentPage, 2000);
2022-02-24 16:24:49 +00:00
}
async init() {
// 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) => {
console.log("Now navigating to", pageName);
if (!this.editorView) {
return;
}
2022-03-09 11:25:42 +00:00
if (this.currentPage) {
let pageState = this.openPages.get(this.currentPage)!;
if (pageState) {
pageState.selection = this.editorView!.state.selection;
pageState.scrollTop = this.editorView!.scrollDOM.scrollTop;
}
2022-03-09 11:25:42 +00:00
this.space.closePage(this.currentPage);
}
2022-02-28 13:35:51 +00:00
await this.loadPage(pageName);
});
this.space.on({
connect: () => {
if (this.currentPage) {
console.log("Connected to socket, fetch fresh?");
this.reloadPage();
}
},
pageChanged: (meta) => {
if (this.currentPage === meta.name) {
console.log("page changed on disk, reloading");
this.reloadPage();
}
},
pageListUpdated: (pages) => {
this.viewDispatch({
type: "pages-listed",
pages: pages,
});
},
2022-03-09 11:25:42 +00:00
});
2022-02-28 13:35:51 +00:00
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-03-09 11:25:42 +00:00
get currentPage(): string | undefined {
2022-02-26 12:26:31 +00:00
return this.viewState.currentPage;
2022-02-21 08:32:36 +00:00
}
2022-03-09 11:25:42 +00:00
createEditorState(pageName: string, 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(
2022-03-09 11:25:42 +00:00
pageName,
this.space.socket.id,
doc,
2022-03-07 12:34:25 +00:00
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-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-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-09 11:25:42 +00:00
reloadPage() {
console.log("Reloading page");
this.loadPage(this.currentPage!);
}
2022-03-07 12:34:25 +00:00
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-03-09 11:25:42 +00:00
this.indexCurrentPageDebounced();
2022-02-21 10:27:30 +00:00
}
return null;
}
2022-03-09 11:25:42 +00:00
private async indexCurrentPage() {
if (this.currentPage) {
console.log("Indexing page", this.currentPage);
await this.indexer.indexPage(
this,
this.currentPage,
this.editorView!.state.sliceDoc(),
true
);
2022-02-22 13:18:37 +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-09 11:25:42 +00:00
async loadPage(pageName: string) {
let doc = await this.space.openPage(pageName);
let editorState = this.createEditorState(pageName, doc);
2022-02-26 12:26:31 +00:00
let pageState = this.openPages.get(pageName);
2022-03-09 11:25:42 +00:00
const editorView = this.editorView;
if (!editorView) {
return;
}
editorView.setState(editorState);
2022-02-26 12:26:31 +00:00
if (!pageState) {
2022-03-09 11:25:42 +00:00
pageState = new PageState(0, editorState.selection);
2022-02-26 12:26:31 +00:00
this.openPages.set(pageName, pageState!);
editorView.dispatch({
selection: { anchor: 0 },
});
2022-03-09 11:25:42 +00:00
} else {
// Restore state
console.log("Restoring selection state");
editorView.dispatch({
selection: pageState.selection,
});
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",
2022-03-09 11:25:42 +00:00
name: pageName,
2022-02-25 14:34:00 +00:00
});
2022-02-21 10:27:30 +00:00
2022-03-09 11:25:42 +00:00
// let indexerPageMeta = await this.indexer.getPageIndexPageMeta(pageName);
// if (
// (indexerPageMeta &&
// doc.meta.lastModified.getTime() !==
// indexerPageMeta.lastModified.getTime()) ||
// !indexerPageMeta
// ) {
await this.indexCurrentPage();
// }
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
let editor = this;
2022-02-25 10:27:58 +00:00
useEffect(() => {
2022-02-26 12:26:31 +00:00
if (viewState.currentPage) {
2022-03-09 11:25:42 +00:00
document.title = viewState.currentPage;
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>
2022-03-09 11:25:42 +00:00
<StatusBar editorView={this.editorView} />
2022-02-23 13:09:26 +00:00
</>
);
}
render(container: ReactDOM.Container) {
const ViewComponent = this.ViewComponent.bind(this);
ReactDOM.render(<ViewComponent />, container);
}
2022-02-21 08:32:36 +00:00
}