From 7ffcd83c44de71fb5495544498dfbe6c07cd8e96 Mon Sep 17 00:00:00 2001 From: Lennart Ochel Date: Thu, 8 Jan 2026 16:15:57 +0100 Subject: [PATCH] Support |^| to set cursor position in virtual pages (#1764) --- client/client.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/client/client.ts b/client/client.ts index 647635f4..a99c6174 100644 --- a/client/client.ts +++ b/client/client.ts @@ -1054,6 +1054,7 @@ export class Client { // Fetch next page to open let doc; + let markerIndex = -1; try { doc = await this.space.readPage(pageName); } catch (e: any) { @@ -1102,6 +1103,13 @@ export class Client { if (results.length === 1) { doc.text = results[0].text; doc.meta.perm = results[0].perm; + // check for |^| and remove it; record position to place cursor later + const cursorMarker = "|^|"; + const idx = doc.text.indexOf(cursorMarker); + if (idx !== -1) { + markerIndex = idx; + doc.text = doc.text.slice(0, idx) + doc.text.slice(idx + cursorMarker.length); + } } else if (results.length > 1) { console.error( "Multiple responses for editor:pageCreating event, this is not supported", @@ -1186,6 +1194,22 @@ export class Client { // We don't really care if this fails. } } + + // If a cursor marker was found for a newly-created page, place the + // cursor there now (after navigateWithinPage so it doesn't get + // overwritten by default positioning). + if (markerIndex !== -1) { + try { + const pos = Math.max(0, Math.min(markerIndex, this.editorView.state.doc.length)); + this.editorView.dispatch({ + selection: { anchor: pos }, + effects: [EditorView.scrollIntoView(pos, { y: "center" })], + }); + this.editorView.focus(); + } catch (e) { + console.error("Failed to set cursor at cursor marker:", e); + } + } } isDocumentEditor(): this is { documentEditor: DocumentEditor } & this {