1
0

Editor refact: rename Editor to Client

This commit is contained in:
Zef Hemel 2023-07-14 16:56:20 +02:00
parent f3936f9b2f
commit 16842a1c4b
20 changed files with 47 additions and 49 deletions

View File

@ -1,16 +1,14 @@
import { safeRun } from "../common/util.ts"; import { safeRun } from "../common/util.ts";
import { Editor } from "./editor.ts"; import { Client } from "./client.ts";
safeRun(async () => { safeRun(async () => {
console.log("Booting"); console.log("Booting SilverBullet...");
const editor = new Editor( const client = new Client(
document.getElementById("sb-root")!, document.getElementById("sb-root")!,
); );
await client.init();
window.editor = editor; window.client = client;
await editor.init();
}); });
if (navigator.serviceWorker) { if (navigator.serviceWorker) {

View File

@ -4,7 +4,6 @@ import {
CompletionResult, CompletionResult,
EditorView, EditorView,
gitIgnoreCompiler, gitIgnoreCompiler,
runScopeHandlers,
syntaxTree, syntaxTree,
} from "../common/deps.ts"; } from "../common/deps.ts";
import { Space } from "./space.ts"; import { Space } from "./space.ts";
@ -44,12 +43,12 @@ declare global {
silverBulletConfig: { silverBulletConfig: {
spaceFolderPath: string; spaceFolderPath: string;
}; };
editor: Editor; client: Client;
} }
} }
// TODO: Oh my god, need to refactor this // TODO: Oh my god, need to refactor this
export class Editor { export class Client {
editorView?: EditorView; editorView?: EditorView;
pageNavigator?: PathPageNavigator; pageNavigator?: PathPageNavigator;
@ -293,7 +292,7 @@ export class Editor {
this.ui.viewDispatch({ type: "sync-change", synced: true }); this.ui.viewDispatch({ type: "sync-change", synced: true });
}); });
this.eventHook.addLocalListener("sync:error", (name) => { this.eventHook.addLocalListener("sync:error", (_name) => {
this.ui.viewDispatch({ type: "sync-change", synced: false }); this.ui.viewDispatch({ type: "sync-change", synced: false });
}); });
this.eventHook.addLocalListener("sync:conflict", (name) => { this.eventHook.addLocalListener("sync:conflict", (name) => {
@ -316,7 +315,7 @@ export class Editor {
try { try {
settingsText = (await this.space.readPage("SETTINGS")).text; settingsText = (await this.space.readPage("SETTINGS")).text;
} catch (e: any) { } catch {
console.log("No SETTINGS page, falling back to default"); console.log("No SETTINGS page, falling back to default");
settingsText = "```yaml\nindexPage: index\n```\n"; settingsText = "```yaml\nindexPage: index\n```\n";
} }

View File

@ -10,7 +10,7 @@ import assetSyscalls from "../plugos/syscalls/asset.ts";
import { eventSyscalls } from "../plugos/syscalls/event.ts"; import { eventSyscalls } from "../plugos/syscalls/event.ts";
import { storeSyscalls } from "../plugos/syscalls/store.dexie_browser.ts"; import { storeSyscalls } from "../plugos/syscalls/store.dexie_browser.ts";
import { SysCallMapping, System } from "../plugos/system.ts"; import { SysCallMapping, System } from "../plugos/system.ts";
import type { Editor } from "./editor.ts"; import type { Client } from "./client.ts";
import { CodeWidgetHook } from "./hooks/code_widget.ts"; import { CodeWidgetHook } from "./hooks/code_widget.ts";
import { CommandHook } from "./hooks/command.ts"; import { CommandHook } from "./hooks/command.ts";
import { SlashCommandHook } from "./hooks/slash_command.ts"; import { SlashCommandHook } from "./hooks/slash_command.ts";
@ -42,7 +42,7 @@ export class ClientSystem {
mdExtensions: MDExt[] = []; mdExtensions: MDExt[] = [];
constructor( constructor(
private editor: Editor, private editor: Client,
private kvStore: DexieKVStore, private kvStore: DexieKVStore,
private dbPrefix: string, private dbPrefix: string,
private eventHook: EventHook, private eventHook: EventHook,

View File

@ -6,7 +6,7 @@ import {
syntaxTree, syntaxTree,
WidgetType, WidgetType,
} from "../deps.ts"; } from "../deps.ts";
import { Editor } from "../editor.ts"; import { Client } from "../client.ts";
import { decoratorStateField, isCursorInRange } from "./util.ts"; import { decoratorStateField, isCursorInRange } from "./util.ts";
type AdmonitionType = "note" | "warning"; type AdmonitionType = "note" | "warning";
@ -97,7 +97,7 @@ function extractAdmonitionFields(rawText: string): AdmonitionFields | null {
return null; return null;
} }
export function admonitionPlugin(editor: Editor) { export function admonitionPlugin(editor: Client) {
return decoratorStateField((state: EditorState) => { return decoratorStateField((state: EditorState) => {
const widgets: any[] = []; const widgets: any[] = [];

View File

@ -1,6 +1,6 @@
import type { ClickEvent } from "../../plug-api/app_event.ts"; import type { ClickEvent } from "../../plug-api/app_event.ts";
import type { Extension } from "../deps.ts"; import type { Extension } from "../deps.ts";
import type { Editor } from "../editor.ts"; import type { Client } from "../client.ts";
import { blockquotePlugin } from "./block_quote.ts"; import { blockquotePlugin } from "./block_quote.ts";
import { admonitionPlugin } from "./admonition.ts"; import { admonitionPlugin } from "./admonition.ts";
import { directivePlugin } from "./directive.ts"; import { directivePlugin } from "./directive.ts";
@ -14,7 +14,7 @@ import { cleanWikiLinkPlugin } from "./wiki_link.ts";
import { cleanCommandLinkPlugin } from "./command_link.ts"; import { cleanCommandLinkPlugin } from "./command_link.ts";
import { fencedCodePlugin } from "./fenced_code.ts"; import { fencedCodePlugin } from "./fenced_code.ts";
export function cleanModePlugins(editor: Editor) { export function cleanModePlugins(editor: Client) {
return [ return [
linkPlugin(), linkPlugin(),
directivePlugin(), directivePlugin(),

View File

@ -1,7 +1,7 @@
import { commandLinkRegex } from "../../common/markdown_parser/parser.ts"; import { commandLinkRegex } from "../../common/markdown_parser/parser.ts";
import { ClickEvent } from "$sb/app_event.ts"; import { ClickEvent } from "$sb/app_event.ts";
import { Decoration, syntaxTree } from "../deps.ts"; import { Decoration, syntaxTree } from "../deps.ts";
import { Editor } from "../editor.ts"; import { Client } from "../client.ts";
import { import {
ButtonWidget, ButtonWidget,
decoratorStateField, decoratorStateField,
@ -12,7 +12,7 @@ import {
/** /**
* Plugin to hide path prefix when the cursor is not inside. * Plugin to hide path prefix when the cursor is not inside.
*/ */
export function cleanCommandLinkPlugin(editor: Editor) { export function cleanCommandLinkPlugin(editor: Client) {
return decoratorStateField((state) => { return decoratorStateField((state) => {
const widgets: any[] = []; const widgets: any[] = [];
// let parentRange: [number, number]; // let parentRange: [number, number];

View File

@ -1,6 +1,6 @@
import { EditorView, syntaxTree, ViewPlugin, ViewUpdate } from "../deps.ts"; import { EditorView, syntaxTree, ViewPlugin, ViewUpdate } from "../deps.ts";
import { maximumAttachmentSize } from "../../common/types.ts"; import { maximumAttachmentSize } from "../../common/types.ts";
import { Editor } from "../editor.ts"; import { Client } from "../client.ts";
// We use turndown to convert HTML to Markdown // We use turndown to convert HTML to Markdown
import TurndownService from "https://cdn.skypack.dev/turndown@7.1.1"; import TurndownService from "https://cdn.skypack.dev/turndown@7.1.1";
@ -80,7 +80,7 @@ export const pasteLinkExtension = ViewPlugin.fromClass(
}, },
); );
export function attachmentExtension(editor: Editor) { export function attachmentExtension(editor: Client) {
let shiftDown = false; let shiftDown = false;
return EditorView.domEventHandlers({ return EditorView.domEventHandlers({
dragover: (event) => { dragover: (event) => {

View File

@ -1,7 +1,7 @@
import { WidgetContent } from "../../plug-api/app_event.ts"; import { WidgetContent } from "../../plug-api/app_event.ts";
import { panelHtml } from "../components/panel.tsx"; import { panelHtml } from "../components/panel.tsx";
import { Decoration, EditorState, syntaxTree, WidgetType } from "../deps.ts"; import { Decoration, EditorState, syntaxTree, WidgetType } from "../deps.ts";
import type { Editor } from "../editor.ts"; import type { Client } from "../client.ts";
import { CodeWidgetCallback } from "../hooks/code_widget.ts"; import { CodeWidgetCallback } from "../hooks/code_widget.ts";
import { import {
decoratorStateField, decoratorStateField,
@ -13,7 +13,7 @@ class IFrameWidget extends WidgetType {
constructor( constructor(
readonly from: number, readonly from: number,
readonly to: number, readonly to: number,
readonly editor: Editor, readonly editor: Client,
readonly bodyText: string, readonly bodyText: string,
readonly codeWidgetCallback: CodeWidgetCallback, readonly codeWidgetCallback: CodeWidgetCallback,
) { ) {
@ -94,7 +94,7 @@ class IFrameWidget extends WidgetType {
} }
} }
export function fencedCodePlugin(editor: Editor) { export function fencedCodePlugin(editor: Client) {
return decoratorStateField((state: EditorState) => { return decoratorStateField((state: EditorState) => {
const widgets: any[] = []; const widgets: any[] = [];
syntaxTree(state).iterate({ syntaxTree(state).iterate({

View File

@ -8,7 +8,7 @@ import {
import { decoratorStateField } from "./util.ts"; import { decoratorStateField } from "./util.ts";
import type { Space } from "../space.ts"; import type { Space } from "../space.ts";
import type { Editor } from "../editor.ts"; import type { Client } from "../client.ts";
class InlineImageWidget extends WidgetType { class InlineImageWidget extends WidgetType {
constructor( constructor(
@ -53,7 +53,7 @@ class InlineImageWidget extends WidgetType {
} }
} }
export function inlineImagesPlugin(editor: Editor) { export function inlineImagesPlugin(editor: Client) {
return decoratorStateField((state: EditorState) => { return decoratorStateField((state: EditorState) => {
const widgets: Range<Decoration>[] = []; const widgets: Range<Decoration>[] = [];
const imageRegex = /!\[(?<title>[^\]]*)\]\((?<url>.+)\)/; const imageRegex = /!\[(?<title>[^\]]*)\]\((?<url>.+)\)/;

View File

@ -8,12 +8,12 @@ import {
import { renderMarkdownToHtml } from "../../plugs/markdown/markdown_render.ts"; import { renderMarkdownToHtml } from "../../plugs/markdown/markdown_render.ts";
import { ParseTree } from "$sb/lib/tree.ts"; import { ParseTree } from "$sb/lib/tree.ts";
import { lezerToParseTree } from "../../common/markdown_parser/parse_tree.ts"; import { lezerToParseTree } from "../../common/markdown_parser/parse_tree.ts";
import type { Editor } from "../editor.ts"; import type { Client } from "../client.ts";
class TableViewWidget extends WidgetType { class TableViewWidget extends WidgetType {
constructor( constructor(
readonly pos: number, readonly pos: number,
readonly editor: Editor, readonly editor: Client,
readonly t: ParseTree, readonly t: ParseTree,
) { ) {
super(); super();
@ -48,7 +48,7 @@ class TableViewWidget extends WidgetType {
} }
} }
export function tablePlugin(editor: Editor) { export function tablePlugin(editor: Client) {
return decoratorStateField((state: EditorState) => { return decoratorStateField((state: EditorState) => {
const widgets: any[] = []; const widgets: any[] = [];
syntaxTree(state).iterate({ syntaxTree(state).iterate({

View File

@ -1,7 +1,7 @@
import { pageLinkRegex } from "../../common/markdown_parser/parser.ts"; import { pageLinkRegex } from "../../common/markdown_parser/parser.ts";
import { ClickEvent } from "../../plug-api/app_event.ts"; import { ClickEvent } from "../../plug-api/app_event.ts";
import { Decoration, syntaxTree } from "../deps.ts"; import { Decoration, syntaxTree } from "../deps.ts";
import { Editor } from "../editor.ts"; import { Client } from "../client.ts";
import { import {
decoratorStateField, decoratorStateField,
invisibleDecoration, invisibleDecoration,
@ -12,7 +12,7 @@ import {
/** /**
* Plugin to hide path prefix when the cursor is not inside. * Plugin to hide path prefix when the cursor is not inside.
*/ */
export function cleanWikiLinkPlugin(editor: Editor) { export function cleanWikiLinkPlugin(editor: Client) {
return decoratorStateField((state) => { return decoratorStateField((state) => {
const widgets: any[] = []; const widgets: any[] = [];
// let parentRange: [number, number]; // let parentRange: [number, number];

View File

@ -1,5 +1,5 @@
import { useEffect, useRef } from "../deps.ts"; import { useEffect, useRef } from "../deps.ts";
import { Editor } from "../editor.ts"; import { Client } from "../client.ts";
import { PanelConfig } from "../types.ts"; import { PanelConfig } from "../types.ts";
export const panelHtml = `<!DOCTYPE html> export const panelHtml = `<!DOCTYPE html>
@ -98,7 +98,7 @@ export function Panel({
editor, editor,
}: { }: {
config: PanelConfig; config: PanelConfig;
editor: Editor; editor: Client;
}) { }) {
const iFrameRef = useRef<HTMLIFrameElement>(null); const iFrameRef = useRef<HTMLIFrameElement>(null);
useEffect(() => { useEffect(() => {

View File

@ -49,7 +49,7 @@ import {
xmlLanguage, xmlLanguage,
yamlLanguage, yamlLanguage,
} from "../common/deps.ts"; } from "../common/deps.ts";
import { Editor } from "./editor.ts"; import { Client } from "./client.ts";
import { vim } from "./deps.ts"; import { vim } from "./deps.ts";
import { inlineImagesPlugin } from "./cm_plugins/inline_image.ts"; import { inlineImagesPlugin } from "./cm_plugins/inline_image.ts";
import { cleanModePlugins } from "./cm_plugins/clean.ts"; import { cleanModePlugins } from "./cm_plugins/clean.ts";
@ -63,7 +63,7 @@ import {
} from "./cm_plugins/editor_paste.ts"; } from "./cm_plugins/editor_paste.ts";
export function createEditorState( export function createEditorState(
editor: Editor, editor: Client,
pageName: string, pageName: string,
text: string, text: string,
readOnly: boolean, readOnly: boolean,

View File

@ -15,7 +15,7 @@ import {
useEffect, useEffect,
useReducer, useReducer,
} from "./deps.ts"; } from "./deps.ts";
import type { Editor } from "./editor.ts"; import type { Client } from "./client.ts";
import { Panel } from "./components/panel.tsx"; import { Panel } from "./components/panel.tsx";
import { h } from "./deps.ts"; import { h } from "./deps.ts";
@ -23,7 +23,7 @@ export class MainUI {
viewState: AppViewState = initialViewState; viewState: AppViewState = initialViewState;
viewDispatch: (action: Action) => void = () => {}; viewDispatch: (action: Action) => void = () => {};
constructor(private editor: Editor) { constructor(private editor: Client) {
// Make keyboard shortcuts work even when the editor is in read only mode or not focused // Make keyboard shortcuts work even when the editor is in read only mode or not focused
globalThis.addEventListener("keydown", (ev) => { globalThis.addEventListener("keydown", (ev) => {
if (!editor.editorView?.hasFocus) { if (!editor.editorView?.hasFocus) {

View File

@ -2,7 +2,7 @@ import { Hook, Manifest } from "../../plugos/types.ts";
import { System } from "../../plugos/system.ts"; import { System } from "../../plugos/system.ts";
import { Completion, CompletionContext, CompletionResult } from "../deps.ts"; import { Completion, CompletionContext, CompletionResult } from "../deps.ts";
import { safeRun } from "../../common/util.ts"; import { safeRun } from "../../common/util.ts";
import { Editor } from "../editor.ts"; import { Client } from "../client.ts";
import { syntaxTree } from "../deps.ts"; import { syntaxTree } from "../deps.ts";
export type SlashCommandDef = { export type SlashCommandDef = {
@ -24,9 +24,9 @@ const slashCommandRegexp = /([^\w:]|^)\/[\w\-]*/;
export class SlashCommandHook implements Hook<SlashCommandHookT> { export class SlashCommandHook implements Hook<SlashCommandHookT> {
slashCommands = new Map<string, AppSlashCommand>(); slashCommands = new Map<string, AppSlashCommand>();
private editor: Editor; private editor: Client;
constructor(editor: Editor) { constructor(editor: Client) {
this.editor = editor; this.editor = editor;
} }

View File

@ -1,4 +1,4 @@
import { Editor } from "../editor.ts"; import { Client } from "../client.ts";
import { import {
EditorView, EditorView,
foldAll, foldAll,
@ -13,7 +13,7 @@ import {
import { SysCallMapping } from "../../plugos/system.ts"; import { SysCallMapping } from "../../plugos/system.ts";
import type { FilterOption } from "../types.ts"; import type { FilterOption } from "../types.ts";
export function editorSyscalls(editor: Editor): SysCallMapping { export function editorSyscalls(editor: Client): SysCallMapping {
const syscalls: SysCallMapping = { const syscalls: SysCallMapping = {
"editor.getCurrentPage": (): string => { "editor.getCurrentPage": (): string => {
return editor.currentPage!; return editor.currentPage!;

View File

@ -1,8 +1,8 @@
import { Editor } from "../editor.ts"; import { Client } from "../client.ts";
import { SysCallMapping } from "../../plugos/system.ts"; import { SysCallMapping } from "../../plugos/system.ts";
import { AttachmentMeta, PageMeta } from "../types.ts"; import { AttachmentMeta, PageMeta } from "../types.ts";
export function spaceSyscalls(editor: Editor): SysCallMapping { export function spaceSyscalls(editor: Client): SysCallMapping {
return { return {
"space.listPages": (): Promise<PageMeta[]> => { "space.listPages": (): Promise<PageMeta[]> => {
return editor.space.fetchPageList(); return editor.space.fetchPageList();

View File

@ -1,7 +1,7 @@
import { SysCallMapping } from "../../plugos/system.ts"; import { SysCallMapping } from "../../plugos/system.ts";
import type { Editor } from "../editor.ts"; import type { Client } from "../client.ts";
export function syncSyscalls(editor: Editor): SysCallMapping { export function syncSyscalls(editor: Client): SysCallMapping {
return { return {
"sync.isSyncing": (): Promise<boolean> => { "sync.isSyncing": (): Promise<boolean> => {
return editor.syncService.isSyncing(); return editor.syncService.isSyncing();

View File

@ -1,10 +1,10 @@
import type { Plug } from "../../plugos/plug.ts"; import type { Plug } from "../../plugos/plug.ts";
import { SysCallMapping, System } from "../../plugos/system.ts"; import { SysCallMapping, System } from "../../plugos/system.ts";
import type { Editor } from "../editor.ts"; import type { Client } from "../client.ts";
import { CommandDef } from "../hooks/command.ts"; import { CommandDef } from "../hooks/command.ts";
export function systemSyscalls( export function systemSyscalls(
editor: Editor, editor: Client,
system: System<any>, system: System<any>,
): SysCallMapping { ): SysCallMapping {
return { return {

View File

@ -9,6 +9,7 @@ release.
* **Bug fix**: Renaming of pages now works again on iOS * **Bug fix**: Renaming of pages now works again on iOS
* Backlinks (as queried via the `link` data source) now contains richer data, namely `inDirective` (if the link appears in the context of a directive) and `alias` (if the backlink has an alias name). This also fixes not updating page references inside directives. This introduced a backwards incompatible data. To update your indexes, please run {[Space: Reindex]} on your clients (once). * Backlinks (as queried via the `link` data source) now contains richer data, namely `inDirective` (if the link appears in the context of a directive) and `alias` (if the backlink has an alias name). This also fixes not updating page references inside directives. This introduced a backwards incompatible data. To update your indexes, please run {[Space: Reindex]} on your clients (once).
* Added {[Debug: Reset Client]} command that flushes the local databases and caches (and service worker) for debugging purposes. * Added {[Debug: Reset Client]} command that flushes the local databases and caches (and service worker) for debugging purposes.
* Big internal code refactor
--- ---