Skip to content

Commit eefada6

Browse files
matthewturkyamshyvalentine195
authored
Add a context menu in reading mode (#33)
* add context menu on reading mode * Addressing comments * type resolveFileUrl --------- Co-authored-by: Shyam <[email protected]> Co-authored-by: Jeremy Valentine <[email protected]>
1 parent d81149b commit eefada6

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ webpack.dev.js
2121
main.js.LICENSE.txt
2222

2323
.env
24+
.vscode

src/main.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
FileSystemAdapter,
88
FuzzySuggestModal,
99
MarkdownRenderer,
10+
Menu,
1011
Plugin,
1112
PluginSettingTab,
1213
sanitizeHTMLToDom,
@@ -59,6 +60,7 @@ declare module "obsidian" {
5960
config: {
6061
theme?: "obsidian" | "moonstone";
6162
};
63+
resolveFileUrl(path: string): TFile;
6264
}
6365
interface Plugin {
6466
_loaded: boolean;
@@ -317,7 +319,6 @@ class ImageWindowSettingTab extends PluginSettingTab {
317319
await this.parent.saveSettings();
318320
})
319321
);
320-
321322
this.buildWindows(this.containerEl.createDiv());
322323
}
323324
buildWindows(el: HTMLElement) {
@@ -474,6 +475,42 @@ export default class ImageWindow extends Plugin {
474475
})
475476
);
476477

478+
this.registerDomEvent(document, "contextmenu", (event: MouseEvent) => {
479+
const target = event.target as HTMLElement;
480+
if (target.localName !== "img") return;
481+
482+
const imgPath = (target as HTMLImageElement).currentSrc;
483+
const file = this.app.vault.resolveFileUrl(imgPath);
484+
485+
if (!(file instanceof TFile)) return;
486+
const menu = new Menu();
487+
menu.addItem((item) => {
488+
item.setTitle("Open in new window")
489+
.setIcon("open-elsewhere-glyph")
490+
.onClick(async () => {
491+
this.defaultWindow.loadFile(file);
492+
});
493+
});
494+
495+
for (const [name, record] of Object.entries(
496+
this.settings.windows
497+
)) {
498+
if (name === DEFAULT_WINDOW_NAME) continue;
499+
menu.addItem((item) => {
500+
item.setTitle(`Open in window '${name}'`)
501+
.setIcon("open-elsewhere-glyph")
502+
.onClick(async () => {
503+
const namedWindow = this.windows.get(record.id);
504+
if (namedWindow !== undefined) {
505+
namedWindow.loadFile(file);
506+
}
507+
});
508+
});
509+
}
510+
511+
menu.showAtPosition({ x: event.pageX, y: event.pageY });
512+
});
513+
477514
this.addCommand({
478515
id: "open-image",
479516
name: "Open image in new window",

0 commit comments

Comments
 (0)