2022-04-25 08:33:38 +00:00
|
|
|
import {
|
|
|
|
autocompletion,
|
|
|
|
completionKeymap,
|
|
|
|
CompletionResult,
|
|
|
|
} from "@codemirror/autocomplete";
|
2022-06-13 16:31:36 +00:00
|
|
|
import { closeBrackets, closeBracketsKeymap } from "@codemirror/autocomplete";
|
2022-04-05 15:02:17 +00:00
|
|
|
import { indentWithTab, standardKeymap } from "@codemirror/commands";
|
2022-06-13 16:31:36 +00:00
|
|
|
import { history, historyKeymap } from "@codemirror/commands";
|
2022-07-04 14:28:22 +00:00
|
|
|
import { syntaxHighlighting } from "@codemirror/language";
|
2022-04-05 15:02:17 +00:00
|
|
|
import { searchKeymap } from "@codemirror/search";
|
2022-06-27 12:14:21 +00:00
|
|
|
import { EditorSelection, EditorState } from "@codemirror/state";
|
2022-03-20 08:56:28 +00:00
|
|
|
import {
|
2022-04-04 13:25:07 +00:00
|
|
|
drawSelection,
|
|
|
|
dropCursor,
|
|
|
|
EditorView,
|
|
|
|
highlightSpecialChars,
|
|
|
|
KeyBinding,
|
|
|
|
keymap,
|
2022-05-17 09:53:17 +00:00
|
|
|
runScopeHandlers,
|
2022-04-04 13:25:07 +00:00
|
|
|
ViewPlugin,
|
2022-04-25 08:33:38 +00:00
|
|
|
ViewUpdate,
|
2022-03-20 08:56:28 +00:00
|
|
|
} from "@codemirror/view";
|
2022-04-05 15:02:17 +00:00
|
|
|
import React, { useEffect, useReducer } from "react";
|
2022-03-20 08:56:28 +00:00
|
|
|
import ReactDOM from "react-dom";
|
2022-04-25 08:33:38 +00:00
|
|
|
import { createSandbox as createIFrameSandbox } from "@plugos/plugos/environments/webworker_sandbox";
|
2022-04-19 14:54:47 +00:00
|
|
|
import { AppEvent, ClickEvent } from "./app_event";
|
2022-04-05 15:02:17 +00:00
|
|
|
import { CommandPalette } from "./components/command_palette";
|
|
|
|
import { PageNavigator } from "./components/page_navigator";
|
|
|
|
import { TopBar } from "./components/top_bar";
|
|
|
|
import { lineWrapper } from "./line_wrapper";
|
2022-04-29 16:54:27 +00:00
|
|
|
import { markdown } from "@silverbulletmd/common/markdown";
|
2022-04-05 15:02:17 +00:00
|
|
|
import { PathPageNavigator } from "./navigator";
|
2022-04-29 16:54:27 +00:00
|
|
|
import buildMarkdown from "@silverbulletmd/common/parser";
|
2022-03-20 08:56:28 +00:00
|
|
|
import reducer from "./reducer";
|
2022-04-05 15:02:17 +00:00
|
|
|
import { smartQuoteKeymap } from "./smart_quotes";
|
2022-04-21 11:57:45 +00:00
|
|
|
import { Space } from "@silverbulletmd/common/spaces/space";
|
2022-03-20 08:56:28 +00:00
|
|
|
import customMarkdownStyle from "./style";
|
2022-04-05 15:02:17 +00:00
|
|
|
import { editorSyscalls } from "./syscalls/editor";
|
2022-04-07 12:04:50 +00:00
|
|
|
import { indexerSyscalls } from "./syscalls";
|
2022-04-05 15:02:17 +00:00
|
|
|
import { spaceSyscalls } from "./syscalls/space";
|
|
|
|
import { Action, AppViewState, initialViewState } from "./types";
|
2022-04-21 11:57:45 +00:00
|
|
|
import { SilverBulletHooks } from "@silverbulletmd/common/manifest";
|
2022-04-25 08:33:38 +00:00
|
|
|
import { safeRun, throttle } from "../common/util";
|
|
|
|
import { System } from "@plugos/plugos/system";
|
|
|
|
import { EventHook } from "@plugos/plugos/hooks/event";
|
2022-04-05 15:02:17 +00:00
|
|
|
import { systemSyscalls } from "./syscalls/system";
|
|
|
|
import { Panel } from "./components/panel";
|
|
|
|
import { CommandHook } from "./hooks/command";
|
|
|
|
import { SlashCommandHook } from "./hooks/slash_command";
|
|
|
|
import { pasteLinkExtension } from "./editor_paste";
|
2022-04-21 11:57:45 +00:00
|
|
|
import { markdownSyscalls } from "@silverbulletmd/common/syscalls/markdown";
|
2022-04-05 15:02:17 +00:00
|
|
|
import { clientStoreSyscalls } from "./syscalls/clientStore";
|
2022-04-29 16:54:27 +00:00
|
|
|
import {
|
|
|
|
loadMarkdownExtensions,
|
|
|
|
MDExt,
|
|
|
|
} from "@silverbulletmd/common/markdown_ext";
|
2022-04-13 12:46:52 +00:00
|
|
|
import { FilterList } from "./components/filter";
|
2022-05-17 09:53:17 +00:00
|
|
|
import { FilterOption, PageMeta } from "@silverbulletmd/common/types";
|
2022-04-21 09:46:33 +00:00
|
|
|
import { syntaxTree } from "@codemirror/language";
|
2022-05-09 12:59:12 +00:00
|
|
|
import sandboxSyscalls from "@plugos/plugos/syscalls/sandbox";
|
2022-07-04 13:07:27 +00:00
|
|
|
import { eventSyscalls } from "@plugos/plugos/syscalls/event";
|
2022-07-12 10:30:15 +00:00
|
|
|
import { storeSyscalls } from "./syscalls/store";
|
2022-08-23 06:12:24 +00:00
|
|
|
import { inlineImagesPlugin } from "./inline_image";
|
2022-04-03 16:12:16 +00:00
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
class PageState {
|
2022-04-26 17:04:36 +00:00
|
|
|
constructor(
|
|
|
|
readonly scrollTop: number,
|
|
|
|
readonly selection: EditorSelection
|
|
|
|
) {}
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 13:21:30 +00:00
|
|
|
const saveInterval = 1000;
|
2022-03-31 12:28:07 +00:00
|
|
|
|
2022-06-14 07:45:22 +00:00
|
|
|
// Monkey patching the languageDataAt, somehow the languageData facet is not set
|
|
|
|
// properly, no idea why
|
|
|
|
// TODO: Remove at some point
|
|
|
|
EditorState.prototype.languageDataAt = function (
|
|
|
|
name: string,
|
|
|
|
pos: number,
|
|
|
|
side = -1
|
|
|
|
) {
|
|
|
|
let values = [];
|
|
|
|
// console.log("Getting language data");
|
|
|
|
// @ts-ignore
|
|
|
|
for (let provider of this.facet(EditorState.languageData)) {
|
|
|
|
let providerResult = provider(this, pos, side);
|
|
|
|
if (!providerResult) {
|
|
|
|
// console.log("Empty provider result");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (let result of providerResult) {
|
|
|
|
if (Object.prototype.hasOwnProperty.call(result, name))
|
|
|
|
values.push(result[name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return values;
|
|
|
|
};
|
|
|
|
|
2022-04-19 14:54:47 +00:00
|
|
|
export class Editor {
|
2022-03-29 10:13:46 +00:00
|
|
|
readonly commandHook: CommandHook;
|
|
|
|
readonly slashCommandHook: SlashCommandHook;
|
2022-03-23 14:41:12 +00:00
|
|
|
openPages = new Map<string, PageState>();
|
2022-03-20 08:56:28 +00:00
|
|
|
editorView?: EditorView;
|
|
|
|
viewState: AppViewState;
|
|
|
|
viewDispatch: React.Dispatch<Action>;
|
2022-04-07 13:21:30 +00:00
|
|
|
space: Space;
|
2022-03-28 13:25:05 +00:00
|
|
|
pageNavigator: PathPageNavigator;
|
2022-03-29 09:21:32 +00:00
|
|
|
eventHook: EventHook;
|
2022-04-03 16:42:12 +00:00
|
|
|
saveTimeout: any;
|
2022-04-04 13:25:07 +00:00
|
|
|
debouncedUpdateEvent = throttle(() => {
|
2022-04-19 14:54:47 +00:00
|
|
|
this.eventHook
|
|
|
|
.dispatchEvent("editor:updated")
|
|
|
|
.catch((e) => console.error("Error dispatching editor:updated event", e));
|
2022-04-04 13:25:07 +00:00
|
|
|
}, 1000);
|
2022-04-03 16:42:12 +00:00
|
|
|
private system = new System<SilverBulletHooks>("client");
|
2022-04-11 18:34:09 +00:00
|
|
|
private mdExtensions: MDExt[] = [];
|
2022-07-22 11:44:28 +00:00
|
|
|
urlPrefix: string;
|
2022-08-02 10:43:39 +00:00
|
|
|
indexPage: string;
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-08-02 10:43:39 +00:00
|
|
|
constructor(
|
|
|
|
space: Space,
|
|
|
|
parent: Element,
|
|
|
|
urlPrefix: string,
|
|
|
|
indexPage: string
|
|
|
|
) {
|
2022-03-20 08:56:28 +00:00
|
|
|
this.space = space;
|
2022-07-22 11:44:28 +00:00
|
|
|
this.urlPrefix = urlPrefix;
|
2022-03-20 08:56:28 +00:00
|
|
|
this.viewState = initialViewState;
|
|
|
|
this.viewDispatch = () => {};
|
2022-08-02 10:43:39 +00:00
|
|
|
this.indexPage = indexPage;
|
2022-03-25 11:03:06 +00:00
|
|
|
|
2022-03-29 09:21:32 +00:00
|
|
|
// Event hook
|
|
|
|
this.eventHook = new EventHook();
|
|
|
|
this.system.addHook(this.eventHook);
|
|
|
|
|
|
|
|
// Command hook
|
|
|
|
this.commandHook = new CommandHook();
|
|
|
|
this.commandHook.on({
|
2022-06-20 16:30:45 +00:00
|
|
|
commandsUpdated: (commandMap) => {
|
2022-03-29 09:21:32 +00:00
|
|
|
this.viewDispatch({
|
|
|
|
type: "update-commands",
|
|
|
|
commands: commandMap,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
this.system.addHook(this.commandHook);
|
|
|
|
|
|
|
|
// Slash command hook
|
|
|
|
this.slashCommandHook = new SlashCommandHook(this);
|
|
|
|
this.system.addHook(this.slashCommandHook);
|
2022-03-25 11:03:06 +00:00
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
this.render(parent);
|
|
|
|
this.editorView = new EditorView({
|
2022-03-31 12:28:07 +00:00
|
|
|
state: this.createEditorState("", ""),
|
2022-07-22 11:44:28 +00:00
|
|
|
parent: document.getElementById("sb-editor")!,
|
2022-03-20 08:56:28 +00:00
|
|
|
});
|
2022-08-02 10:43:39 +00:00
|
|
|
this.pageNavigator = new PathPageNavigator(indexPage, urlPrefix);
|
2022-03-21 14:21:34 +00:00
|
|
|
|
2022-04-11 18:34:09 +00:00
|
|
|
this.system.registerSyscalls(
|
|
|
|
[],
|
2022-07-04 13:07:27 +00:00
|
|
|
eventSyscalls(this.eventHook),
|
2022-05-09 12:59:12 +00:00
|
|
|
editorSyscalls(this),
|
|
|
|
spaceSyscalls(this),
|
|
|
|
indexerSyscalls(this.space),
|
|
|
|
systemSyscalls(this),
|
|
|
|
markdownSyscalls(buildMarkdown(this.mdExtensions)),
|
|
|
|
clientStoreSyscalls(),
|
2022-07-12 10:30:15 +00:00
|
|
|
storeSyscalls(this.space),
|
2022-05-09 12:59:12 +00:00
|
|
|
sandboxSyscalls(this.system)
|
2022-04-11 18:34:09 +00:00
|
|
|
);
|
2022-05-13 12:36:26 +00:00
|
|
|
|
2022-05-17 09:53:17 +00:00
|
|
|
// Make keyboard shortcuts work even when the editor is in read only mode or not focused
|
|
|
|
window.addEventListener("keydown", (ev) => {
|
|
|
|
if (!this.editorView?.hasFocus) {
|
2022-07-19 11:49:54 +00:00
|
|
|
// console.log(
|
|
|
|
// "Window-level keyboard event",
|
|
|
|
// ev
|
|
|
|
// );
|
|
|
|
if ((ev.target as any).classList.contains("cm-textfield")) {
|
|
|
|
// Search & replace feature, ignore this
|
|
|
|
return;
|
|
|
|
}
|
2022-05-17 09:53:17 +00:00
|
|
|
if (runScopeHandlers(this.editorView!, ev, "editor")) {
|
|
|
|
ev.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-07-04 14:28:22 +00:00
|
|
|
|
|
|
|
window.addEventListener("touchstart", (ev) => {
|
|
|
|
// Launch the command palette using a three-finger tap
|
|
|
|
if (ev.touches.length > 2) {
|
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
this.viewDispatch({ type: "show-palette" });
|
|
|
|
}
|
|
|
|
});
|
2022-04-03 16:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get currentPage(): string | undefined {
|
|
|
|
return this.viewState.currentPage;
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
this.focus();
|
|
|
|
|
2022-08-30 08:44:20 +00:00
|
|
|
this.pageNavigator.subscribe(async (pageName, pos: number | string) => {
|
|
|
|
console.log("Now navigating to", pageName, pos);
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
if (!this.editorView) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.loadPage(pageName);
|
2022-03-28 13:25:05 +00:00
|
|
|
if (pos) {
|
2022-08-30 08:44:20 +00:00
|
|
|
if (typeof pos === "string") {
|
|
|
|
console.log("Navigating to anchor", pos);
|
|
|
|
|
|
|
|
// We're going to look up the anchor through a direct page store query...
|
|
|
|
// TODO: This is a bit hacky, but it works for now
|
|
|
|
let posLookup = await this.system.syscallWithContext(
|
|
|
|
// Mock the "core" plug
|
|
|
|
{ plug: { name: "core" } as any },
|
|
|
|
"index.get",
|
|
|
|
[pageName, `a:${pageName}:@${pos}`]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!posLookup) {
|
|
|
|
return this.flashNotification(
|
|
|
|
`Could not find anchor @${pos}`,
|
|
|
|
"error"
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
pos = +posLookup;
|
|
|
|
}
|
|
|
|
}
|
2022-03-28 13:25:05 +00:00
|
|
|
this.editorView.dispatch({
|
|
|
|
selection: { anchor: pos },
|
2022-08-30 08:44:20 +00:00
|
|
|
scrollIntoView: true,
|
2022-03-28 13:25:05 +00:00
|
|
|
});
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
});
|
|
|
|
|
2022-07-22 11:44:28 +00:00
|
|
|
let globalModules: any = await (
|
|
|
|
await fetch(`${this.urlPrefix}/global.plug.json`)
|
|
|
|
).json();
|
2022-06-13 11:06:53 +00:00
|
|
|
|
|
|
|
this.system.on({
|
|
|
|
plugLoaded: (plug) => {
|
|
|
|
safeRun(async () => {
|
|
|
|
for (let [modName, code] of Object.entries(
|
|
|
|
globalModules.dependencies
|
|
|
|
)) {
|
|
|
|
await plug.sandbox.loadDependency(modName, code as string);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
this.space.on({
|
|
|
|
pageChanged: (meta) => {
|
|
|
|
if (this.currentPage === meta.name) {
|
|
|
|
console.log("Page changed on disk, reloading");
|
|
|
|
this.flashNotification("Page changed on disk, reloading");
|
|
|
|
this.reloadPage();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
pageListUpdated: (pages) => {
|
|
|
|
this.viewDispatch({
|
|
|
|
type: "pages-listed",
|
|
|
|
pages: pages,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-06-14 14:55:50 +00:00
|
|
|
await this.reloadPlugs();
|
2022-07-11 07:08:22 +00:00
|
|
|
await this.dispatchAppEvent("editor:init");
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
async save(immediate: boolean = false): Promise<void> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (!this.viewState.unsavedChanges) {
|
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
if (this.saveTimeout) {
|
|
|
|
clearTimeout(this.saveTimeout);
|
|
|
|
}
|
|
|
|
this.saveTimeout = setTimeout(
|
|
|
|
() => {
|
|
|
|
if (this.currentPage) {
|
|
|
|
console.log("Saving page", this.currentPage);
|
|
|
|
this.space
|
|
|
|
.writePage(
|
|
|
|
this.currentPage,
|
|
|
|
this.editorView!.state.sliceDoc(0),
|
|
|
|
true
|
|
|
|
)
|
|
|
|
.then(() => {
|
|
|
|
this.viewDispatch({ type: "page-saved" });
|
|
|
|
resolve();
|
|
|
|
})
|
2022-07-19 15:21:11 +00:00
|
|
|
.catch((e) => {
|
|
|
|
this.flashNotification(
|
|
|
|
"Could not save page, retrying again in 10 seconds",
|
|
|
|
"error"
|
|
|
|
);
|
|
|
|
this.saveTimeout = setTimeout(this.save.bind(this), 10000);
|
|
|
|
reject(e);
|
|
|
|
});
|
2022-03-31 12:28:07 +00:00
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
immediate ? 0 : saveInterval
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-14 11:32:28 +00:00
|
|
|
flashNotification(message: string, type: "info" | "error" = "info") {
|
2022-03-20 08:56:28 +00:00
|
|
|
let id = Math.floor(Math.random() * 1000000);
|
|
|
|
this.viewDispatch({
|
|
|
|
type: "show-notification",
|
|
|
|
notification: {
|
2022-07-14 11:32:28 +00:00
|
|
|
id,
|
|
|
|
type,
|
|
|
|
message,
|
2022-03-20 08:56:28 +00:00
|
|
|
date: new Date(),
|
|
|
|
},
|
|
|
|
});
|
2022-07-14 11:32:28 +00:00
|
|
|
setTimeout(
|
|
|
|
() => {
|
|
|
|
this.viewDispatch({
|
|
|
|
type: "dismiss-notification",
|
|
|
|
id: id,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
type === "info" ? 2000 : 5000
|
|
|
|
);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 12:46:52 +00:00
|
|
|
filterBox(
|
|
|
|
label: string,
|
|
|
|
options: FilterOption[],
|
|
|
|
helpText: string = "",
|
|
|
|
placeHolder: string = ""
|
|
|
|
): Promise<FilterOption | undefined> {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
this.viewDispatch({
|
|
|
|
type: "show-filterbox",
|
2022-04-21 09:46:33 +00:00
|
|
|
label,
|
2022-04-13 12:46:52 +00:00
|
|
|
options,
|
|
|
|
placeHolder,
|
|
|
|
helpText,
|
|
|
|
onSelect: (option) => {
|
|
|
|
this.viewDispatch({ type: "hide-filterbox" });
|
2022-04-19 14:54:47 +00:00
|
|
|
this.focus();
|
2022-04-13 12:46:52 +00:00
|
|
|
resolve(option);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-19 14:54:47 +00:00
|
|
|
async dispatchAppEvent(name: AppEvent, data?: any): Promise<any[]> {
|
2022-03-29 09:21:32 +00:00
|
|
|
return this.eventHook.dispatchEvent(name, data);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
createEditorState(pageName: string, text: string): EditorState {
|
2022-03-20 08:56:28 +00:00
|
|
|
let commandKeyBindings: KeyBinding[] = [];
|
2022-03-29 09:21:32 +00:00
|
|
|
for (let def of this.commandHook.editorCommands.values()) {
|
2022-03-20 08:56:28 +00:00
|
|
|
if (def.command.key) {
|
|
|
|
commandKeyBindings.push({
|
|
|
|
key: def.command.key,
|
|
|
|
mac: def.command.mac,
|
|
|
|
run: (): boolean => {
|
2022-04-21 09:46:33 +00:00
|
|
|
if (def.command.contexts) {
|
|
|
|
let context = this.getContext();
|
|
|
|
if (!context || !def.command.contexts.includes(context)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
Promise.resolve()
|
2022-03-28 13:25:05 +00:00
|
|
|
.then(def.run)
|
|
|
|
.catch((e: any) => {
|
|
|
|
console.error(e);
|
2022-07-14 11:32:28 +00:00
|
|
|
this.flashNotification(
|
|
|
|
`Error running command: ${e.message}`,
|
|
|
|
"error"
|
|
|
|
);
|
2022-07-30 10:57:52 +00:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
// Always be focusing the editor after running a command
|
|
|
|
editor.focus();
|
2022-03-28 13:25:05 +00:00
|
|
|
});
|
2022-03-20 08:56:28 +00:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 12:28:07 +00:00
|
|
|
const editor = this;
|
2022-03-20 08:56:28 +00:00
|
|
|
return EditorState.create({
|
2022-03-31 12:28:07 +00:00
|
|
|
doc: text,
|
2022-03-20 08:56:28 +00:00
|
|
|
extensions: [
|
2022-06-14 07:45:22 +00:00
|
|
|
markdown({
|
|
|
|
base: buildMarkdown(this.mdExtensions),
|
|
|
|
addKeymap: true,
|
|
|
|
}),
|
2022-08-23 06:12:24 +00:00
|
|
|
inlineImagesPlugin(),
|
2022-03-20 08:56:28 +00:00
|
|
|
highlightSpecialChars(),
|
|
|
|
history(),
|
|
|
|
drawSelection(),
|
|
|
|
dropCursor(),
|
2022-06-13 16:31:36 +00:00
|
|
|
syntaxHighlighting(customMarkdownStyle(this.mdExtensions)),
|
2022-03-20 08:56:28 +00:00
|
|
|
autocompletion({
|
|
|
|
override: [
|
2022-04-19 14:54:47 +00:00
|
|
|
this.completer.bind(this),
|
2022-03-29 09:21:32 +00:00
|
|
|
this.slashCommandHook.slashCommandCompleter.bind(
|
|
|
|
this.slashCommandHook
|
|
|
|
),
|
2022-03-20 08:56:28 +00:00
|
|
|
],
|
|
|
|
}),
|
|
|
|
EditorView.lineWrapping,
|
|
|
|
lineWrapper([
|
2022-08-02 12:40:04 +00:00
|
|
|
{ selector: "ATXHeading1", class: "sb-line-h1" },
|
|
|
|
{ selector: "ATXHeading2", class: "sb-line-h2" },
|
|
|
|
{ selector: "ATXHeading3", class: "sb-line-h3" },
|
|
|
|
{ selector: "ListItem", class: "sb-line-li", nesting: true },
|
|
|
|
{ selector: "Blockquote", class: "sb-line-blockquote" },
|
|
|
|
{ selector: "Task", class: "sb-line-task" },
|
|
|
|
{ selector: "CodeBlock", class: "sb-line-code" },
|
|
|
|
{ selector: "FencedCode", class: "sb-line-fenced-code" },
|
|
|
|
{ selector: "Comment", class: "sb-line-comment" },
|
|
|
|
{ selector: "BulletList", class: "sb-line-ul" },
|
|
|
|
{ selector: "OrderedList", class: "sb-line-ol" },
|
|
|
|
{ selector: "TableHeader", class: "sb-line-tbl-header" },
|
2022-03-20 08:56:28 +00:00
|
|
|
]),
|
|
|
|
keymap.of([
|
|
|
|
...smartQuoteKeymap,
|
|
|
|
...closeBracketsKeymap,
|
|
|
|
...standardKeymap,
|
|
|
|
...searchKeymap,
|
|
|
|
...historyKeymap,
|
|
|
|
...completionKeymap,
|
|
|
|
indentWithTab,
|
|
|
|
...commandKeyBindings,
|
|
|
|
{
|
|
|
|
key: "Ctrl-k",
|
|
|
|
mac: "Cmd-k",
|
|
|
|
run: (): boolean => {
|
|
|
|
this.viewDispatch({ type: "start-navigate" });
|
2022-04-26 17:04:36 +00:00
|
|
|
this.space.updatePageList();
|
2022-03-20 08:56:28 +00:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-04-09 20:43:46 +00:00
|
|
|
key: "Ctrl-/",
|
|
|
|
mac: "Cmd-/",
|
2022-03-20 08:56:28 +00:00
|
|
|
run: (): boolean => {
|
2022-04-21 09:46:33 +00:00
|
|
|
let context = this.getContext();
|
2022-03-20 08:56:28 +00:00
|
|
|
this.viewDispatch({
|
|
|
|
type: "show-palette",
|
2022-04-21 09:46:33 +00:00
|
|
|
context,
|
2022-03-20 08:56:28 +00:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
2022-04-05 15:02:17 +00:00
|
|
|
{
|
|
|
|
key: "Ctrl-l",
|
|
|
|
mac: "Cmd-l",
|
|
|
|
run: (): boolean => {
|
|
|
|
this.editorView?.dispatch({
|
|
|
|
effects: [
|
|
|
|
EditorView.scrollIntoView(
|
|
|
|
this.editorView.state.selection.main.anchor,
|
|
|
|
{
|
|
|
|
y: "center",
|
|
|
|
}
|
|
|
|
),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
2022-03-20 08:56:28 +00:00
|
|
|
]),
|
2022-03-30 13:16:22 +00:00
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
EditorView.domEventHandlers({
|
|
|
|
click: (event: MouseEvent, view: EditorView) => {
|
|
|
|
safeRun(async () => {
|
|
|
|
let clickEvent: ClickEvent = {
|
2022-03-28 13:25:05 +00:00
|
|
|
page: pageName,
|
2022-03-20 08:56:28 +00:00
|
|
|
ctrlKey: event.ctrlKey,
|
|
|
|
metaKey: event.metaKey,
|
|
|
|
altKey: event.altKey,
|
|
|
|
pos: view.posAtCoords(event)!,
|
|
|
|
};
|
|
|
|
await this.dispatchAppEvent("page:click", clickEvent);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
}),
|
2022-03-31 12:28:07 +00:00
|
|
|
ViewPlugin.fromClass(
|
|
|
|
class {
|
|
|
|
update(update: ViewUpdate): void {
|
|
|
|
if (update.docChanged) {
|
|
|
|
editor.viewDispatch({ type: "page-changed" });
|
2022-04-04 13:25:07 +00:00
|
|
|
editor.debouncedUpdateEvent();
|
2022-04-19 14:54:47 +00:00
|
|
|
editor.save().catch((e) => console.error("Error saving", e));
|
2022-03-31 12:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
),
|
2022-03-30 13:16:22 +00:00
|
|
|
pasteLinkExtension,
|
2022-06-14 07:45:22 +00:00
|
|
|
closeBrackets(),
|
2022-03-20 08:56:28 +00:00
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:04:36 +00:00
|
|
|
async reloadPlugs() {
|
2022-06-15 15:54:11 +00:00
|
|
|
console.log("Loading plugs");
|
2022-04-26 17:04:36 +00:00
|
|
|
await this.space.updatePageList();
|
|
|
|
await this.system.unloadAll();
|
|
|
|
console.log("(Re)loading plugs");
|
|
|
|
for (let pageInfo of this.space.listPlugs()) {
|
2022-07-11 07:08:22 +00:00
|
|
|
// console.log("Loading plug", pageInfo.name);
|
2022-04-26 17:04:36 +00:00
|
|
|
let { text } = await this.space.readPage(pageInfo.name);
|
|
|
|
await this.system.load(JSON.parse(text), createIFrameSandbox);
|
|
|
|
}
|
|
|
|
this.rebuildEditorState();
|
2022-07-11 07:08:22 +00:00
|
|
|
await this.dispatchAppEvent("plugs:loaded");
|
2022-04-26 17:04:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 15:25:34 +00:00
|
|
|
rebuildEditorState() {
|
|
|
|
const editorView = this.editorView;
|
2022-06-14 14:55:50 +00:00
|
|
|
console.log("Rebuilding editor state");
|
2022-03-31 15:25:34 +00:00
|
|
|
if (editorView && this.currentPage) {
|
2022-06-14 14:55:50 +00:00
|
|
|
console.log("Getting all syntax extensions");
|
2022-04-11 18:34:09 +00:00
|
|
|
this.mdExtensions = loadMarkdownExtensions(this.system);
|
|
|
|
|
|
|
|
// And reload the syscalls to use the new syntax extensions
|
|
|
|
this.system.registerSyscalls(
|
|
|
|
[],
|
|
|
|
markdownSyscalls(buildMarkdown(this.mdExtensions))
|
|
|
|
);
|
|
|
|
|
2022-04-26 17:04:36 +00:00
|
|
|
this.saveState();
|
|
|
|
|
2022-03-31 15:25:34 +00:00
|
|
|
editorView.setState(
|
|
|
|
this.createEditorState(this.currentPage, editorView.state.sliceDoc())
|
|
|
|
);
|
2022-04-20 08:56:43 +00:00
|
|
|
if (editorView.contentDOM) {
|
2022-05-17 09:53:17 +00:00
|
|
|
this.tweakEditorDOM(
|
|
|
|
editorView.contentDOM,
|
|
|
|
this.viewState.perm === "ro"
|
|
|
|
);
|
2022-04-20 08:56:43 +00:00
|
|
|
}
|
2022-04-26 17:04:36 +00:00
|
|
|
|
|
|
|
this.restoreState(this.currentPage);
|
2022-03-31 15:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 14:54:47 +00:00
|
|
|
async completer(): Promise<CompletionResult | null> {
|
|
|
|
let results = await this.dispatchAppEvent("page:complete");
|
|
|
|
let actualResult = null;
|
|
|
|
for (const result of results) {
|
|
|
|
if (result) {
|
|
|
|
if (actualResult) {
|
|
|
|
console.error(
|
|
|
|
"Got completion results from multiple sources, cannot deal with that"
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
actualResult = result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return actualResult;
|
|
|
|
}
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
reloadPage() {
|
|
|
|
console.log("Reloading page");
|
|
|
|
safeRun(async () => {
|
2022-03-31 12:28:07 +00:00
|
|
|
clearTimeout(this.saveTimeout);
|
2022-03-20 08:56:28 +00:00
|
|
|
await this.loadPage(this.currentPage!);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
focus() {
|
|
|
|
this.editorView!.focus();
|
|
|
|
}
|
|
|
|
|
2022-08-30 08:44:20 +00:00
|
|
|
async navigate(name: string, pos?: number | string, replaceState = false) {
|
2022-08-02 10:43:39 +00:00
|
|
|
if (!name) {
|
|
|
|
name = this.indexPage;
|
|
|
|
}
|
2022-08-01 13:06:32 +00:00
|
|
|
await this.pageNavigator.navigate(name, pos, replaceState);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async loadPage(pageName: string) {
|
2022-05-17 13:54:55 +00:00
|
|
|
const loadingDifferentPage = pageName !== this.currentPage;
|
2022-03-20 08:56:28 +00:00
|
|
|
const editorView = this.editorView;
|
|
|
|
if (!editorView) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Persist current page state and nicely close page
|
|
|
|
if (this.currentPage) {
|
2022-04-26 17:04:36 +00:00
|
|
|
this.saveState();
|
2022-03-31 12:28:07 +00:00
|
|
|
this.space.unwatchPage(this.currentPage);
|
|
|
|
await this.save(true);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch next page to open
|
2022-04-06 13:39:20 +00:00
|
|
|
let doc;
|
|
|
|
try {
|
|
|
|
doc = await this.space.readPage(pageName);
|
|
|
|
} catch (e: any) {
|
|
|
|
// Not found, new page
|
|
|
|
console.log("Creating new page", pageName);
|
|
|
|
doc = {
|
|
|
|
text: "",
|
2022-05-17 09:53:17 +00:00
|
|
|
meta: { name: pageName, lastModified: 0, perm: "rw" } as PageMeta,
|
2022-04-06 13:39:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
let editorState = this.createEditorState(pageName, doc.text);
|
2022-03-20 08:56:28 +00:00
|
|
|
editorView.setState(editorState);
|
2022-04-20 08:56:43 +00:00
|
|
|
if (editorView.contentDOM) {
|
2022-05-17 09:53:17 +00:00
|
|
|
this.tweakEditorDOM(editorView.contentDOM, doc.meta.perm === "ro");
|
2022-04-20 08:56:43 +00:00
|
|
|
}
|
2022-04-26 17:04:36 +00:00
|
|
|
this.restoreState(pageName);
|
2022-03-31 12:28:07 +00:00
|
|
|
this.space.watchPage(pageName);
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
this.viewDispatch({
|
|
|
|
type: "page-loaded",
|
2022-05-17 09:53:17 +00:00
|
|
|
meta: doc.meta,
|
2022-03-20 08:56:28 +00:00
|
|
|
});
|
2022-04-04 13:25:07 +00:00
|
|
|
|
2022-05-17 13:54:55 +00:00
|
|
|
if (loadingDifferentPage) {
|
|
|
|
await this.eventHook.dispatchEvent("editor:pageLoaded", pageName);
|
2022-07-18 14:48:36 +00:00
|
|
|
} else {
|
|
|
|
await this.eventHook.dispatchEvent("editor:pageReloaded", pageName);
|
2022-05-17 13:54:55 +00:00
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 09:53:17 +00:00
|
|
|
tweakEditorDOM(contentDOM: HTMLElement, readOnly: boolean) {
|
2022-05-09 08:45:36 +00:00
|
|
|
contentDOM.spellcheck = true;
|
|
|
|
contentDOM.setAttribute("autocorrect", "on");
|
|
|
|
contentDOM.setAttribute("autocapitalize", "on");
|
2022-05-17 09:53:17 +00:00
|
|
|
contentDOM.setAttribute("contenteditable", readOnly ? "false" : "true");
|
2022-05-09 08:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 17:04:36 +00:00
|
|
|
private restoreState(pageName: string) {
|
|
|
|
let pageState = this.openPages.get(pageName);
|
|
|
|
const editorView = this.editorView!;
|
|
|
|
if (pageState) {
|
|
|
|
// Restore state
|
|
|
|
// console.log("Restoring selection state", pageState);
|
2022-07-18 10:10:31 +00:00
|
|
|
editorView.scrollDOM.scrollTop = pageState!.scrollTop;
|
2022-04-26 17:04:36 +00:00
|
|
|
editorView.dispatch({
|
|
|
|
selection: pageState.selection,
|
2022-07-06 10:18:33 +00:00
|
|
|
scrollIntoView: true,
|
2022-04-26 17:04:36 +00:00
|
|
|
});
|
2022-07-18 10:10:31 +00:00
|
|
|
} else {
|
|
|
|
editorView.scrollDOM.scrollTop = 0;
|
2022-04-26 17:04:36 +00:00
|
|
|
}
|
|
|
|
editorView.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
private saveState() {
|
|
|
|
this.openPages.set(
|
|
|
|
this.currentPage!,
|
|
|
|
new PageState(
|
|
|
|
this.editorView!.scrollDOM.scrollTop,
|
|
|
|
this.editorView!.state.selection
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
ViewComponent(): React.ReactElement {
|
|
|
|
const [viewState, dispatch] = useReducer(reducer, initialViewState);
|
|
|
|
this.viewState = viewState;
|
|
|
|
this.viewDispatch = dispatch;
|
|
|
|
|
2022-06-23 15:08:42 +00:00
|
|
|
const editor = this;
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (viewState.currentPage) {
|
|
|
|
document.title = viewState.currentPage;
|
|
|
|
}
|
|
|
|
}, [viewState.currentPage]);
|
|
|
|
|
|
|
|
return (
|
2022-04-04 16:33:13 +00:00
|
|
|
<>
|
2022-03-20 08:56:28 +00:00
|
|
|
{viewState.showPageNavigator && (
|
|
|
|
<PageNavigator
|
|
|
|
allPages={viewState.allPages}
|
|
|
|
currentPage={this.currentPage}
|
|
|
|
onNavigate={(page) => {
|
|
|
|
dispatch({ type: "stop-navigate" });
|
|
|
|
editor.focus();
|
|
|
|
if (page) {
|
|
|
|
safeRun(async () => {
|
2022-03-29 09:21:32 +00:00
|
|
|
await editor.navigate(page);
|
2022-03-20 08:56:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{viewState.showCommandPalette && (
|
|
|
|
<CommandPalette
|
|
|
|
onTrigger={(cmd) => {
|
|
|
|
dispatch({ type: "hide-palette" });
|
2022-06-23 15:08:42 +00:00
|
|
|
editor.focus();
|
2022-03-20 08:56:28 +00:00
|
|
|
if (cmd) {
|
2022-05-16 13:09:36 +00:00
|
|
|
dispatch({ type: "command-run", command: cmd.command.name });
|
2022-07-04 13:07:27 +00:00
|
|
|
cmd
|
|
|
|
.run()
|
|
|
|
.catch((e) => {
|
|
|
|
console.error("Error running command", e.message);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
// Always be focusing the editor after running a command
|
|
|
|
editor.focus();
|
|
|
|
});
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
commands={viewState.commands}
|
2022-05-16 13:09:36 +00:00
|
|
|
recentCommands={viewState.recentCommands}
|
2022-03-20 08:56:28 +00:00
|
|
|
/>
|
|
|
|
)}
|
2022-04-13 12:46:52 +00:00
|
|
|
{viewState.showFilterBox && (
|
|
|
|
<FilterList
|
2022-04-21 09:46:33 +00:00
|
|
|
label={viewState.filterBoxLabel}
|
2022-04-13 12:46:52 +00:00
|
|
|
placeholder={viewState.filterBoxPlaceHolder}
|
|
|
|
options={viewState.filterBoxOptions}
|
|
|
|
allowNew={false}
|
|
|
|
helpText={viewState.filterBoxHelpText}
|
|
|
|
onSelect={viewState.filterBoxOnSelect}
|
|
|
|
/>
|
|
|
|
)}
|
2022-03-20 08:56:28 +00:00
|
|
|
<TopBar
|
|
|
|
pageName={viewState.currentPage}
|
|
|
|
notifications={viewState.notifications}
|
2022-03-31 12:28:07 +00:00
|
|
|
unsavedChanges={viewState.unsavedChanges}
|
2022-03-20 08:56:28 +00:00
|
|
|
onClick={() => {
|
|
|
|
dispatch({ type: "start-navigate" });
|
|
|
|
}}
|
2022-08-26 22:32:40 +00:00
|
|
|
onThemeClick={() => {
|
2022-08-30 07:30:36 +00:00
|
|
|
if (localStorage.theme === "dark") localStorage.theme = "light";
|
|
|
|
else localStorage.theme = "dark";
|
2022-08-26 22:32:40 +00:00
|
|
|
document.documentElement.dataset.theme = localStorage.theme;
|
|
|
|
}}
|
2022-07-04 09:38:16 +00:00
|
|
|
onHomeClick={() => {
|
2022-08-02 10:43:39 +00:00
|
|
|
editor.navigate("");
|
2022-07-04 09:38:16 +00:00
|
|
|
}}
|
2022-06-20 16:30:45 +00:00
|
|
|
onActionClick={() => {
|
|
|
|
dispatch({ type: "show-palette" });
|
|
|
|
}}
|
2022-04-04 16:33:13 +00:00
|
|
|
rhs={
|
|
|
|
!!viewState.showRHS && (
|
|
|
|
<div className="panel" style={{ flex: viewState.showRHS }} />
|
|
|
|
)
|
|
|
|
}
|
|
|
|
lhs={
|
|
|
|
!!viewState.showLHS && (
|
|
|
|
<div className="panel" style={{ flex: viewState.showLHS }} />
|
|
|
|
)
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
/>
|
2022-07-22 11:44:28 +00:00
|
|
|
<div id="sb-main">
|
2022-04-04 16:33:13 +00:00
|
|
|
{!!viewState.showLHS && (
|
2022-05-09 12:59:12 +00:00
|
|
|
<Panel
|
|
|
|
html={viewState.lhsHTML}
|
|
|
|
script={viewState.lhsScript}
|
|
|
|
flex={viewState.showLHS}
|
2022-05-11 18:10:45 +00:00
|
|
|
editor={editor}
|
2022-05-09 12:59:12 +00:00
|
|
|
/>
|
2022-04-04 16:33:13 +00:00
|
|
|
)}
|
2022-07-22 11:44:28 +00:00
|
|
|
<div id="sb-editor" />
|
2022-04-04 16:33:13 +00:00
|
|
|
{!!viewState.showRHS && (
|
2022-05-09 12:59:12 +00:00
|
|
|
<Panel
|
|
|
|
html={viewState.rhsHTML}
|
|
|
|
script={viewState.rhsScript}
|
|
|
|
flex={viewState.showRHS}
|
2022-05-11 18:10:45 +00:00
|
|
|
editor={editor}
|
2022-05-09 12:59:12 +00:00
|
|
|
/>
|
2022-04-04 16:33:13 +00:00
|
|
|
)}
|
|
|
|
</div>
|
2022-04-26 17:04:36 +00:00
|
|
|
{!!viewState.showBHS && (
|
2022-08-02 12:40:04 +00:00
|
|
|
<div className="sb-bhs">
|
2022-05-09 12:59:12 +00:00
|
|
|
<Panel
|
|
|
|
html={viewState.bhsHTML}
|
|
|
|
script={viewState.bhsScript}
|
|
|
|
flex={1}
|
2022-05-11 18:10:45 +00:00
|
|
|
editor={editor}
|
2022-05-09 12:59:12 +00:00
|
|
|
/>
|
2022-04-26 17:04:36 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2022-04-04 16:33:13 +00:00
|
|
|
</>
|
2022-03-20 08:56:28 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-11 07:08:22 +00:00
|
|
|
async runCommandByName(name: string) {
|
|
|
|
const cmd = this.viewState.commands.get(name);
|
|
|
|
if (cmd) {
|
|
|
|
await cmd.run();
|
|
|
|
} else {
|
|
|
|
throw new Error(`Command ${name} not found`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
render(container: ReactDOM.Container) {
|
|
|
|
const ViewComponent = this.ViewComponent.bind(this);
|
|
|
|
ReactDOM.render(<ViewComponent />, container);
|
|
|
|
}
|
2022-04-21 09:46:33 +00:00
|
|
|
|
|
|
|
private getContext(): string | undefined {
|
|
|
|
let state = this.editorView!.state;
|
|
|
|
let selection = state.selection.main;
|
|
|
|
if (selection.empty) {
|
|
|
|
return syntaxTree(state).resolveInner(selection.from).name;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|