1
0

Cmd/Ctrl-click now opens page in new window

This commit is contained in:
Zef Hemel 2022-10-29 09:23:12 +02:00
parent 540af411a0
commit bef2c4c62b
4 changed files with 26 additions and 8 deletions

View File

@ -33,8 +33,9 @@ export function navigate(
name: string, name: string,
pos?: string | number, pos?: string | number,
replaceState = false, replaceState = false,
newWindow = false,
): Promise<void> { ): Promise<void> {
return syscall("editor.navigate", name, pos, replaceState); return syscall("editor.navigate", name, pos, replaceState, newWindow);
} }
export function reloadPage(): Promise<void> { export function reloadPage(): Promise<void> {

View File

@ -10,7 +10,10 @@ function patchUrl(url: string): string {
return url; return url;
} }
async function actionClickOrActionEnter(mdTree: ParseTree | null) { async function actionClickOrActionEnter(
mdTree: ParseTree | null,
inNewWindow = false,
) {
if (!mdTree) { if (!mdTree) {
return; return;
} }
@ -28,7 +31,7 @@ async function actionClickOrActionEnter(mdTree: ParseTree | null) {
if (!pageLink) { if (!pageLink) {
pageLink = await editor.getCurrentPage(); pageLink = await editor.getCurrentPage();
} }
await editor.navigate(pageLink, pos); await editor.navigate(pageLink, pos, false, inNewWindow);
break; break;
} }
case "URL": case "URL":
@ -61,13 +64,13 @@ export async function linkNavigate() {
} }
export async function clickNavigate(event: ClickEvent) { export async function clickNavigate(event: ClickEvent) {
// Navigate by default, don't navigate when Ctrl or Cmd is held // Navigate by default, don't navigate when Alt is held
if (event.ctrlKey || event.metaKey) { if (event.altKey) {
return; return;
} }
const mdTree = await markdown.parseMarkdown(await editor.getText()); const mdTree = await markdown.parseMarkdown(await editor.getText());
const newNode = nodeAtPos(mdTree, event.pos); const newNode = nodeAtPos(mdTree, event.pos);
await actionClickOrActionEnter(newNode); await actionClickOrActionEnter(newNode, event.ctrlKey || event.metaKey);
} }
export async function navigateCommand(cmdDef: any) { export async function navigateCommand(cmdDef: any) {

View File

@ -604,10 +604,23 @@ export class Editor {
this.editorView!.focus(); 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) { if (!name) {
name = this.indexPage; 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); await this.pageNavigator.navigate(name, pos, replaceState);
} }

View File

@ -48,8 +48,9 @@ export function editorSyscalls(editor: Editor): SysCallMapping {
name: string, name: string,
pos: number | string, pos: number | string,
replaceState = false, replaceState = false,
newWindow = false,
) => { ) => {
await editor.navigate(name, pos, replaceState); await editor.navigate(name, pos, replaceState, newWindow);
}, },
"editor.reloadPage": async () => { "editor.reloadPage": async () => {
await editor.reloadPage(); await editor.reloadPage();