From a6af94b9e3e08a8e097b4c496d3ba79f4653b48e Mon Sep 17 00:00:00 2001 From: MrMugame <40832361+MrMugame@users.noreply.github.com> Date: Tue, 25 Feb 2025 20:02:59 +0100 Subject: [PATCH] Remove `editor.setText` syscall from client (#1263) --- web/client.ts | 14 ++++++++++++-- web/syscalls/editor.ts | 11 +---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/web/client.ts b/web/client.ts index c16ca061..e92d43c7 100644 --- a/web/client.ts +++ b/web/client.ts @@ -5,6 +5,7 @@ import type { import type { Compartment, EditorState } from "@codemirror/state"; import { EditorView } from "@codemirror/view"; import { indentUnit, syntaxTree } from "@codemirror/language"; +import { isolateHistory } from "@codemirror/commands"; import { compile as gitIgnoreCompiler } from "gitignore-parser"; import type { SyntaxNode } from "@lezer/common"; import { Space } from "../common/space.ts"; @@ -74,7 +75,7 @@ import { lezerToParseTree } from "$common/markdown_parser/parse_tree.ts"; import { findNodeMatching } from "@silverbulletmd/silverbullet/lib/tree"; import type { AspiringPageObject } from "../plugs/index/page_links.ts"; import type { Config, ConfigContainer } from "../type/config.ts"; -import { editor } from "@silverbulletmd/silverbullet/syscalls"; +import { diffAndPrepareChanges } from "./cm_util.ts"; const frontMatterRegex = /^---\n(([^\n]|\n)*?)---\n/; @@ -1157,7 +1158,7 @@ export class Client implements ConfigContainer { this.space.watchPage(pageName); } else { // Just apply minimal patches so that the cursor is preserved - await editor.setText(doc.text, true); + this.setEditorText(doc.text, true); } // Note: these events are dispatched asynchronously deliberately (not waiting for results) @@ -1190,6 +1191,15 @@ export class Client implements ConfigContainer { contentDOM.setAttribute("autocapitalize", "on"); } + setEditorText(newText: string, shouldIsolateHistory = false) { + const currentText = this.editorView.state.sliceDoc(); + const allChanges = diffAndPrepareChanges(currentText, newText); + client.editorView.dispatch({ + changes: allChanges, + annotations: shouldIsolateHistory ? isolateHistory.of("full") : undefined, + }); + } + async loadCustomStyles() { const spaceStyles = await this.clientSystem.queryObjects( "space-style", diff --git a/web/syscalls/editor.ts b/web/syscalls/editor.ts index 36e819a6..3fbfd008 100644 --- a/web/syscalls/editor.ts +++ b/web/syscalls/editor.ts @@ -8,7 +8,6 @@ import { } from "@codemirror/language"; import { deleteLine, - isolateHistory, moveLineDown, moveLineUp, redo, @@ -22,7 +21,6 @@ import type { FilterOption } from "@silverbulletmd/silverbullet/type/client"; import type { PageMeta, UploadFile } from "../../plug-api/types.ts"; import type { PageRef } from "@silverbulletmd/silverbullet/lib/page_ref"; import { openSearchPanel } from "@codemirror/search"; -import { diffAndPrepareChanges } from "../cm_util.ts"; export function editorSyscalls(client: Client): SysCallMapping { const syscalls: SysCallMapping = { @@ -36,14 +34,7 @@ export function editorSyscalls(client: Client): SysCallMapping { return client.editorView.state.sliceDoc(); }, "editor.setText": (_ctx, newText: string, shouldIsolateHistory = false) => { - const currentText = client.editorView.state.sliceDoc(); - const allChanges = diffAndPrepareChanges(currentText, newText); - client.editorView.dispatch({ - changes: allChanges, - annotations: shouldIsolateHistory - ? isolateHistory.of("full") - : undefined, - }); + client.setEditorText(newText, shouldIsolateHistory); }, "editor.getCursor": (): number => { return client.editorView.state.selection.main.from;