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,
pos?: string | number,
replaceState = false,
newWindow = false,
): Promise<void> {
return syscall("editor.navigate", name, pos, replaceState);
return syscall("editor.navigate", name, pos, replaceState, newWindow);
}
export function reloadPage(): Promise<void> {

View File

@ -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) {

View File

@ -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);
}

View File

@ -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();