From bef2c4c62bcb358def1948230dc8787ea5b52174 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sat, 29 Oct 2022 09:23:12 +0200 Subject: [PATCH] Cmd/Ctrl-click now opens page in new window --- plug-api/silverbullet-syscall/editor.ts | 3 ++- plugs/core/navigate.ts | 13 ++++++++----- web/editor.tsx | 15 ++++++++++++++- web/syscalls/editor.ts | 3 ++- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/plug-api/silverbullet-syscall/editor.ts b/plug-api/silverbullet-syscall/editor.ts index ebbb117..76396ba 100644 --- a/plug-api/silverbullet-syscall/editor.ts +++ b/plug-api/silverbullet-syscall/editor.ts @@ -33,8 +33,9 @@ export function navigate( name: string, pos?: string | number, replaceState = false, + newWindow = false, ): Promise { - return syscall("editor.navigate", name, pos, replaceState); + return syscall("editor.navigate", name, pos, replaceState, newWindow); } export function reloadPage(): Promise { diff --git a/plugs/core/navigate.ts b/plugs/core/navigate.ts index 8eb32b0..7d945c3 100644 --- a/plugs/core/navigate.ts +++ b/plugs/core/navigate.ts @@ -10,7 +10,10 @@ function patchUrl(url: string): string { return url; } -async function actionClickOrActionEnter(mdTree: ParseTree | null) { +async function actionClickOrActionEnter( + mdTree: ParseTree | null, + inNewWindow = false, +) { if (!mdTree) { return; } @@ -28,7 +31,7 @@ async function actionClickOrActionEnter(mdTree: ParseTree | null) { if (!pageLink) { pageLink = await editor.getCurrentPage(); } - await editor.navigate(pageLink, pos); + await editor.navigate(pageLink, pos, false, inNewWindow); break; } case "URL": @@ -61,13 +64,13 @@ export async function linkNavigate() { } export async function clickNavigate(event: ClickEvent) { - // Navigate by default, don't navigate when Ctrl or Cmd is held - if (event.ctrlKey || event.metaKey) { + // Navigate by default, don't navigate when Alt is held + if (event.altKey) { return; } const mdTree = await markdown.parseMarkdown(await editor.getText()); const newNode = nodeAtPos(mdTree, event.pos); - await actionClickOrActionEnter(newNode); + await actionClickOrActionEnter(newNode, event.ctrlKey || event.metaKey); } export async function navigateCommand(cmdDef: any) { diff --git a/web/editor.tsx b/web/editor.tsx index 30ab14b..6ed73f3 100644 --- a/web/editor.tsx +++ b/web/editor.tsx @@ -604,10 +604,23 @@ export class Editor { this.editorView!.focus(); } - async navigate(name: string, pos?: number | string, replaceState = false) { + async navigate( + name: string, + pos?: number | string, + replaceState = false, + newWindow = false, + ) { if (!name) { name = this.indexPage; } + + if (newWindow) { + const win = window.open(`${location.origin}/${name}`, "_blank"); + if (win) { + win.focus(); + } + return; + } await this.pageNavigator.navigate(name, pos, replaceState); } diff --git a/web/syscalls/editor.ts b/web/syscalls/editor.ts index 728db1c..39927a2 100644 --- a/web/syscalls/editor.ts +++ b/web/syscalls/editor.ts @@ -48,8 +48,9 @@ export function editorSyscalls(editor: Editor): SysCallMapping { name: string, pos: number | string, replaceState = false, + newWindow = false, ) => { - await editor.navigate(name, pos, replaceState); + await editor.navigate(name, pos, replaceState, newWindow); }, "editor.reloadPage": async () => { await editor.reloadPage();