Refactoring

This commit is contained in:
Zef Hemel
2022-03-25 12:03:06 +01:00
parent b51730d33f
commit 08e6c3bad8
52 changed files with 796 additions and 326 deletions
+1 -2
View File
@@ -179,7 +179,7 @@ export function collabExtension(
if (this.pushing || !updates.length) return;
this.pushing = true;
let version = getSyncedVersion(this.view.state);
console.log("Updates", updates, "to apply to version", version);
// console.log("Updates", updates, "to apply to version", version);
let success = await callbacks.pushUpdates(pageName, version, updates);
this.pushing = false;
@@ -201,7 +201,6 @@ export function collabExtension(
// Regardless of whether the push failed or new updates came in
// while it was running, try again if there's updates remaining
if (!this.done && sendableUpdates(this.view.state).length) {
// setTimeout(() => this.push(), 100);
this.throttledPush();
}
}
+8 -7
View File
@@ -1,7 +1,6 @@
import { AppViewState, PageMeta } from "../types";
import { Notification } from "../types";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faFileLines } from "@fortawesome/free-solid-svg-icons";
import { Notification } from "../types";
function prettyName(s: string | undefined): string {
if (!s) {
@@ -28,11 +27,13 @@ export function TopBar({
<FontAwesomeIcon icon={faFileLines} />
</span>
<span className="current-page">{prettyName(pageName)}</span>
<div className="status">
{notifications.map((notification) => (
<div key={notification.id}>{notification.message}</div>
))}
</div>
{notifications.length > 0 && (
<div className="status">
{notifications.map((notification) => (
<div key={notification.id}>{notification.message}</div>
))}
</div>
)}
</div>
</div>
);
+17 -17
View File
@@ -37,9 +37,9 @@ import reducer from "./reducer";
import { smartQuoteKeymap } from "./smart_quotes";
import { Space } from "./space";
import customMarkdownStyle from "./style";
import editorSyscalls from "./syscalls/editor.browser";
import indexerSyscalls from "./syscalls/indexer.native";
import spaceSyscalls from "./syscalls/space.native";
import editorSyscalls from "./syscalls/editor";
import indexerSyscalls from "./syscalls/indexer";
import spaceSyscalls from "./syscalls/space";
import {
Action,
AppCommand,
@@ -50,6 +50,8 @@ import {
import { SilverBulletHooks } from "../common/manifest";
import { safeRun } from "./util";
import { System } from "../plugbox/system";
import { EventFeature } from "../plugbox/feature/event";
import { systemSyscalls } from "./syscalls/system";
class PageState {
scrollTop: number;
@@ -71,11 +73,16 @@ export class Editor implements AppEventDispatcher {
space: Space;
navigationResolve?: (val: undefined) => void;
pageNavigator: IPageNavigator;
private eventFeature: EventFeature;
constructor(space: Space, parent: Element) {
this.space = space;
this.viewState = initialViewState;
this.viewDispatch = () => {};
this.eventFeature = new EventFeature();
this.system.addFeature(this.eventFeature);
this.render(parent);
this.editorView = new EditorView({
state: this.createEditorState(
@@ -86,11 +93,10 @@ export class Editor implements AppEventDispatcher {
});
this.pageNavigator = new PathPageNavigator();
this.system.registerSyscalls(
editorSyscalls(this),
spaceSyscalls(this),
indexerSyscalls(this.space)
);
this.system.registerSyscalls("editor", [], editorSyscalls(this));
this.system.registerSyscalls("space", [], spaceSyscalls(this));
this.system.registerSyscalls("indexer", [], indexerSyscalls(this.space));
this.system.registerSyscalls("system", [], systemSyscalls(this.space));
}
async init() {
@@ -129,20 +135,14 @@ export class Editor implements AppEventDispatcher {
},
loadSystem: (systemJSON) => {
safeRun(async () => {
await this.system.replaceAllFromJSON(systemJSON, () =>
createIFrameSandbox(this.system)
);
await this.system.replaceAllFromJSON(systemJSON, createIFrameSandbox);
this.buildAllCommands();
});
},
plugLoaded: (plugName, plug) => {
safeRun(async () => {
console.log("Plug load", plugName);
await this.system.load(
plugName,
plug,
createIFrameSandbox(this.system)
);
await this.system.load(plugName, plug, createIFrameSandbox);
this.buildAllCommands();
});
},
@@ -199,7 +199,7 @@ export class Editor implements AppEventDispatcher {
}
async dispatchAppEvent(name: AppEvent, data?: any): Promise<any[]> {
return this.system.dispatchEvent(name, data);
return this.eventFeature.dispatchEvent(name, data);
}
get currentPage(): string | undefined {
+1 -37
View File
@@ -80,7 +80,7 @@ export class Space extends EventEmitter<SpaceEvents> {
});
}
private wsCall(eventName: string, ...args: any[]): Promise<any> {
public wsCall(eventName: string, ...args: any[]): Promise<any> {
return new Promise((resolve, reject) => {
this.reqId++;
this.socket!.once(`${eventName}Resp${this.reqId}`, (err, result) => {
@@ -160,40 +160,4 @@ export class Space extends EventEmitter<SpaceEvents> {
async getPageMeta(name: string): Promise<PageMeta> {
return this.wsCall("page.getPageMeta", name);
}
async indexSet(pageName: string, key: string, value: any) {
await this.wsCall("index.set", pageName, key, value);
}
async indexBatchSet(pageName: string, kvs: KV[]) {
// TODO: Optimize with batch call
for (let { key, value } of kvs) {
await this.indexSet(pageName, key, value);
}
}
async indexGet(pageName: string, key: string): Promise<any | null> {
return await this.wsCall("index.get", pageName, key);
}
async indexScanPrefixForPage(
pageName: string,
keyPrefix: string
): Promise<{ key: string; value: any }[]> {
return await this.wsCall("index.scanPrefixForPage", pageName, keyPrefix);
}
async indexScanPrefixGlobal(
keyPrefix: string
): Promise<{ key: string; value: any }[]> {
return await this.wsCall("index.scanPrefixGlobal", keyPrefix);
}
async indexDeletePrefixForPage(pageName: string, keyPrefix: string) {
await this.wsCall("index.deletePrefixForPage", keyPrefix);
}
async indexDelete(pageName: string, key: string) {
await this.wsCall("index.delete", pageName, key);
}
}
-1
View File
@@ -129,7 +129,6 @@
.mention {
color: #0330cb;
text-decoration: underline;
}
.tag {
-8
View File
@@ -1,8 +0,0 @@
export default {
"db.put": (key: string, value: any) => {
localStorage.setItem(key, value);
},
"db.get": (key: string) => {
return localStorage.getItem(key);
},
};
@@ -1,7 +1,7 @@
import { Editor } from "../editor";
import { syntaxTree } from "@codemirror/language";
import { Transaction } from "@codemirror/state";
import { PageMeta } from "../types";
import { SysCallMapping } from "../../plugbox/system";
type SyntaxNode = {
name: string;
@@ -26,23 +26,23 @@ function ensureAnchor(expr: any, start: boolean) {
);
}
export default (editor: Editor) => ({
"editor.getCurrentPage": (): string => {
export default (editor: Editor): SysCallMapping => ({
getCurrentPage: (): string => {
return editor.currentPage!;
},
"editor.getText": () => {
getText: () => {
return editor.editorView?.state.sliceDoc();
},
"editor.getCursor": (): number => {
getCursor: (): number => {
return editor.editorView!.state.selection.main.from;
},
"editor.navigate": async (name: string) => {
navigate: async (ctx, name: string) => {
await editor.navigate(name);
},
"editor.openUrl": async (url: string) => {
openUrl: async (ctx, url: string) => {
window.open(url, "_blank")!.focus();
},
"editor.insertAtPos": (text: string, pos: number) => {
insertAtPos: (ctx, text: string, pos: number) => {
editor.editorView!.dispatch({
changes: {
insert: text,
@@ -50,7 +50,7 @@ export default (editor: Editor) => ({
},
});
},
"editor.replaceRange": (from: number, to: number, text: string) => {
replaceRange: (ctx, from: number, to: number, text: string) => {
editor.editorView!.dispatch({
changes: {
insert: text,
@@ -59,14 +59,14 @@ export default (editor: Editor) => ({
},
});
},
"editor.moveCursor": (pos: number) => {
moveCursor: (ctx, pos: number) => {
editor.editorView!.dispatch({
selection: {
anchor: pos,
},
});
},
"editor.insertAtCursor": (text: string) => {
insertAtCursor: (ctx, text: string) => {
let editorView = editor.editorView!;
let from = editorView.state.selection.main.from;
editorView.dispatch({
@@ -79,7 +79,7 @@ export default (editor: Editor) => ({
},
});
},
"editor.getSyntaxNodeUnderCursor": (): SyntaxNode | undefined => {
getSyntaxNodeUnderCursor: (): SyntaxNode | undefined => {
const editorState = editor.editorView!.state;
let selection = editorState.selection.main;
if (selection.empty) {
@@ -94,7 +94,8 @@ export default (editor: Editor) => ({
}
}
},
"editor.matchBefore": (
matchBefore: (
ctx,
regexp: string
): { from: number; to: number; text: string } | null => {
const editorState = editor.editorView!.state;
@@ -112,7 +113,7 @@ export default (editor: Editor) => ({
}
return null;
},
"editor.getSyntaxNodeAtPos": (pos: number): SyntaxNode | undefined => {
getSyntaxNodeAtPos: (ctx, pos: number): SyntaxNode | undefined => {
const editorState = editor.editorView!.state;
let node = syntaxTree(editorState).resolveInner(pos);
if (node) {
@@ -124,10 +125,10 @@ export default (editor: Editor) => ({
};
}
},
"editor.dispatch": (change: Transaction) => {
dispatch: (ctx, change: Transaction) => {
editor.editorView!.dispatch(change);
},
"editor.prompt": (message: string, defaultValue = ""): string | null => {
prompt: (ctx, message: string, defaultValue = ""): string | null => {
return prompt(message, defaultValue);
},
});
-22
View File
@@ -1,22 +0,0 @@
import { Space, KV } from "../space";
export default (space: Space) => ({
"indexer.scanPrefixForPage": async (pageName: string, keyPrefix: string) => {
return await space.indexScanPrefixForPage(pageName, keyPrefix);
},
"indexer.scanPrefixGlobal": async (keyPrefix: string) => {
return await space.indexScanPrefixGlobal(keyPrefix);
},
"indexer.get": async (pageName: string, key: string): Promise<any> => {
return await space.indexGet(pageName, key);
},
"indexer.set": async (pageName: string, key: string, value: any) => {
await space.indexSet(pageName, key, value);
},
"indexer.batchSet": async (pageName: string, kvs: KV[]) => {
await space.indexBatchSet(pageName, kvs);
},
"indexer.delete": async (pageName: string, key: string) => {
await space.indexDelete(pageName, key);
},
});
+17
View File
@@ -0,0 +1,17 @@
import { Space } from "../space";
import { SysCallMapping } from "../../plugbox/system";
import { transportSyscalls } from "../../plugbox/syscall/transport";
export default function indexerSyscalls(space: Space): SysCallMapping {
return transportSyscalls(
[
"scanPrefixForPage",
"scanPrefixGlobal",
"get",
"set",
"batchSet",
"delete",
],
(name, ...args) => space.wsCall(`index.${name}`, ...args)
);
}
@@ -1,21 +1,21 @@
import { Editor } from "../editor";
import { PageMeta } from "../types";
import { SysCallMapping } from "../../plugbox/system";
export default (editor: Editor) => ({
"space.listPages": (): PageMeta[] => {
export default (editor: Editor): SysCallMapping => ({
listPages: (): PageMeta[] => {
return [...editor.viewState.allPages];
},
"space.readPage": async (
readPage: async (
ctx,
name: string
): Promise<{ text: string; meta: PageMeta }> => {
return await editor.space.readPage(name);
},
"space.writePage": async (name: string, text: string): Promise<PageMeta> => {
writePage: async (ctx, name: string, text: string): Promise<PageMeta> => {
return await editor.space.writePage(name, text);
},
"space.deletePage": async (name: string) => {
console.log("Clearing page index", name);
await editor.space.indexDeletePrefixForPage(name, "");
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");
+13
View File
@@ -0,0 +1,13 @@
import { SysCallMapping } from "../../plugbox/system";
import { Space } from "../space";
export function systemSyscalls(space: Space): SysCallMapping {
return {
async invokeFunctionOnServer(ctx, name: string, ...args: any[]) {
if (!ctx.plug) {
throw Error("No plug associated with context");
}
return await space.wsCall("invokeFunction", ctx.plug.name, name, ...args);
},
};
}
-20
View File
@@ -1,20 +0,0 @@
// @ts-ignore
let frameTest = document.getElementById("main-frame");
window.addEventListener("message", async (event) => {
let messageEvent = event as MessageEvent;
let data = messageEvent.data;
if (data.type === "iframe_event") {
// @ts-ignore
window.mainPlug.dispatchEvent(data.data.event, data.data.data);
}
});
export default {
"ui.update": function (doc: any) {
// frameTest.contentWindow.postMessage({
// type: "loadContent",
// doc: doc,
// });
},
};
-12
View File
@@ -1,12 +0,0 @@
{
"include": ["src/**/*"],
"compilerOptions": {
"target": "esnext",
"strict": true,
"moduleResolution": "node",
"module": "ESNext",
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"jsx": "react-jsx"
}
}