Rewrote some navigation stuff based on new parser

This commit is contained in:
Zef Hemel
2022-04-03 18:42:12 +02:00
parent 16bf0d866d
commit 07453d638b
21 changed files with 206 additions and 235 deletions
+19 -22
View File
@@ -6,14 +6,14 @@ import {bracketMatching} from "@codemirror/matchbrackets";
import {searchKeymap} from "@codemirror/search";
import {EditorSelection, EditorState} from "@codemirror/state";
import {
drawSelection,
dropCursor,
EditorView,
highlightSpecialChars,
KeyBinding,
keymap,
ViewPlugin,
ViewUpdate,
drawSelection,
dropCursor,
EditorView,
highlightSpecialChars,
KeyBinding,
keymap,
ViewPlugin,
ViewUpdate,
} from "@codemirror/view";
import React, {useEffect, useReducer} from "react";
import ReactDOM from "react-dom";
@@ -47,7 +47,6 @@ import {CompleterHook} from "./hooks/completer";
import {pasteLinkExtension} from "./editor_paste";
import {markdownSyscalls} from "../common/syscalls/markdown";
class PageState {
scrollTop: number;
selection: EditorSelection;
@@ -61,11 +60,9 @@ class PageState {
const saveInterval = 2000;
export class Editor implements AppEventDispatcher {
private system = new System<SilverBulletHooks>("client");
readonly commandHook: CommandHook;
readonly slashCommandHook: SlashCommandHook;
readonly completerHook: CompleterHook;
openPages = new Map<string, PageState>();
editorView?: EditorView;
viewState: AppViewState;
@@ -73,6 +70,8 @@ export class Editor implements AppEventDispatcher {
space: Space;
pageNavigator: PathPageNavigator;
eventHook: EventHook;
saveTimeout: any;
private system = new System<SilverBulletHooks>("client");
constructor(space: Space, parent: Element) {
this.space = space;
@@ -110,11 +109,15 @@ export class Editor implements AppEventDispatcher {
});
this.pageNavigator = new PathPageNavigator();
this.system.registerSyscalls("editor", [], editorSyscalls(this));
this.system.registerSyscalls("space", [], spaceSyscalls(this));
this.system.registerSyscalls("index", [], indexerSyscalls(this.space));
this.system.registerSyscalls("system", [], systemSyscalls(this.space));
this.system.registerSyscalls("markdown", [], markdownSyscalls());
this.system.registerSyscalls([], editorSyscalls(this));
this.system.registerSyscalls([], spaceSyscalls(this));
this.system.registerSyscalls([], indexerSyscalls(this.space));
this.system.registerSyscalls([], systemSyscalls(this.space));
this.system.registerSyscalls([], markdownSyscalls());
}
get currentPage(): string | undefined {
return this.viewState.currentPage;
}
async init() {
@@ -180,8 +183,6 @@ export class Editor implements AppEventDispatcher {
}
}
saveTimeout: any;
async save(immediate: boolean = false): Promise<void> {
return new Promise((resolve, reject) => {
if (!this.viewState.unsavedChanges) {
@@ -236,10 +237,6 @@ export class Editor implements AppEventDispatcher {
return this.eventHook.dispatchEvent(name, data);
}
get currentPage(): string | undefined {
return this.viewState.currentPage;
}
createEditorState(pageName: string, text: string): EditorState {
let commandKeyBindings: KeyBinding[] = [];
for (let def of this.commandHook.editorCommands.values()) {
+23 -19
View File
@@ -28,37 +28,37 @@ function ensureAnchor(expr: any, start: boolean) {
export function editorSyscalls(editor: Editor): SysCallMapping {
return {
getCurrentPage: (): string => {
"editor.getCurrentPage": (): string => {
return editor.currentPage!;
},
getText: () => {
"editor.getText": () => {
return editor.editorView?.state.sliceDoc();
},
getCursor: (): number => {
"editor.getCursor": (): number => {
return editor.editorView!.state.selection.main.from;
},
save: async () => {
"editor.save": async () => {
return editor.save(true);
},
navigate: async (ctx, name: string, pos: number) => {
"editor.navigate": async (ctx, name: string, pos: number) => {
await editor.navigate(name, pos);
},
reloadPage: async (ctx) => {
"editor.reloadPage": async (ctx) => {
await editor.reloadPage();
},
openUrl: async (ctx, url: string) => {
"editor.openUrl": async (ctx, url: string) => {
window.open(url, "_blank")!.focus();
},
flashNotification: (ctx, message: string) => {
"editor.flashNotification": (ctx, message: string) => {
editor.flashNotification(message);
},
showRhs: (ctx, html: string) => {
"editor.showRhs": (ctx, html: string) => {
editor.viewDispatch({
type: "show-rhs",
html: html,
});
},
insertAtPos: (ctx, text: string, pos: number) => {
"editor.insertAtPos": (ctx, text: string, pos: number) => {
editor.editorView!.dispatch({
changes: {
insert: text,
@@ -66,7 +66,7 @@ export function editorSyscalls(editor: Editor): SysCallMapping {
},
});
},
replaceRange: (ctx, from: number, to: number, text: string) => {
"editor.replaceRange": (ctx, from: number, to: number, text: string) => {
editor.editorView!.dispatch({
changes: {
insert: text,
@@ -75,14 +75,14 @@ export function editorSyscalls(editor: Editor): SysCallMapping {
},
});
},
moveCursor: (ctx, pos: number) => {
"editor.moveCursor": (ctx, pos: number) => {
editor.editorView!.dispatch({
selection: {
anchor: pos,
},
});
},
insertAtCursor: (ctx, text: string) => {
"editor.insertAtCursor": (ctx, text: string) => {
let editorView = editor.editorView!;
let from = editorView.state.selection.main.from;
editorView.dispatch({
@@ -95,7 +95,7 @@ export function editorSyscalls(editor: Editor): SysCallMapping {
},
});
},
getSyntaxNodeUnderCursor: (): SyntaxNode | undefined => {
"editor.getSyntaxNodeUnderCursor": (): SyntaxNode | undefined => {
const editorState = editor.editorView!.state;
let selection = editorState.selection.main;
if (selection.empty) {
@@ -110,13 +110,13 @@ export function editorSyscalls(editor: Editor): SysCallMapping {
}
}
},
getLineUnderCursor: (): string => {
"editor.getLineUnderCursor": (): string => {
const editorState = editor.editorView!.state;
let selection = editorState.selection.main;
let line = editorState.doc.lineAt(selection.from);
return editorState.sliceDoc(line.from, line.to);
},
matchBefore: (
"editor.matchBefore": (
ctx,
regexp: string
): { from: number; to: number; text: string } | null => {
@@ -135,7 +135,7 @@ export function editorSyscalls(editor: Editor): SysCallMapping {
}
return null;
},
getSyntaxNodeAtPos: (ctx, pos: number): SyntaxNode | undefined => {
"editor.getSyntaxNodeAtPos": (ctx, pos: number): SyntaxNode | undefined => {
const editorState = editor.editorView!.state;
let node = syntaxTree(editorState).resolveInner(pos);
if (node) {
@@ -147,10 +147,14 @@ export function editorSyscalls(editor: Editor): SysCallMapping {
};
}
},
dispatch: (ctx, change: Transaction) => {
"editor.dispatch": (ctx, change: Transaction) => {
editor.editorView!.dispatch(change);
},
prompt: (ctx, message: string, defaultValue = ""): string | null => {
"editor.prompt": (
ctx,
message: string,
defaultValue = ""
): string | null => {
return prompt(message, defaultValue);
},
};
+7 -7
View File
@@ -5,13 +5,13 @@ import {transportSyscalls} from "../../plugos/syscalls/transport";
export function indexerSyscalls(space: Space): SysCallMapping {
return transportSyscalls(
[
"scanPrefixForPage",
"scanPrefixGlobal",
"get",
"set",
"batchSet",
"delete",
"index.scanPrefixForPage",
"index.scanPrefixGlobal",
"index.get",
"index.set",
"index.batchSet",
"index.delete",
],
(ctx, name, ...args) => space.remoteSyscall(ctx.plug, `index.${name}`, args)
(ctx, name, ...args) => space.remoteSyscall(ctx.plug, name, args)
);
}
+8 -4
View File
@@ -4,19 +4,23 @@ import {PageMeta} from "../../common/types";
export function spaceSyscalls(editor: Editor): SysCallMapping {
return {
listPages: async (): Promise<PageMeta[]> => {
"space.listPages": async (): Promise<PageMeta[]> => {
return [...(await editor.space.listPages())];
},
readPage: async (
"space.readPage": async (
ctx,
name: string
): Promise<{ text: string; meta: PageMeta }> => {
return await editor.space.readPage(name);
},
writePage: async (ctx, name: string, text: string): Promise<PageMeta> => {
"space.writePage": async (
ctx,
name: string,
text: string
): Promise<PageMeta> => {
return await editor.space.writePage(name, text);
},
deletePage: async (ctx, name: string) => {
"space.deletePage": async (ctx, name: string) => {
// If we're deleting the current page, navigate to the start page
if (editor.currentPage === name) {
await editor.navigate("start");
+7 -3
View File
@@ -1,9 +1,13 @@
import { SysCallMapping } from "../../plugos/system";
import { Space } from "../space";
import {SysCallMapping} from "../../plugos/system";
import {Space} from "../space";
export function systemSyscalls(space: Space): SysCallMapping {
return {
async invokeFunctionOnServer(ctx, name: string, ...args: any[]) {
"system.invokeFunctionOnServer": async (
ctx,
name: string,
...args: any[]
) => {
if (!ctx.plug) {
throw Error("No plug associated with context");
}