Skip to content

Commit

Permalink
Add a context menu in reading mode (#33)
Browse files Browse the repository at this point in the history
* add context menu on reading mode

* Addressing comments

* type resolveFileUrl

---------

Co-authored-by: Shyam <[email protected]>
Co-authored-by: Jeremy Valentine <[email protected]>
  • Loading branch information
3 people authored Aug 27, 2023
1 parent d81149b commit eefada6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ webpack.dev.js
main.js.LICENSE.txt

.env
.vscode
39 changes: 38 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
FileSystemAdapter,
FuzzySuggestModal,
MarkdownRenderer,
Menu,
Plugin,
PluginSettingTab,
sanitizeHTMLToDom,
Expand Down Expand Up @@ -59,6 +60,7 @@ declare module "obsidian" {
config: {
theme?: "obsidian" | "moonstone";
};
resolveFileUrl(path: string): TFile;
}
interface Plugin {
_loaded: boolean;
Expand Down Expand Up @@ -317,7 +319,6 @@ class ImageWindowSettingTab extends PluginSettingTab {
await this.parent.saveSettings();
})
);

this.buildWindows(this.containerEl.createDiv());
}
buildWindows(el: HTMLElement) {
Expand Down Expand Up @@ -474,6 +475,42 @@ export default class ImageWindow extends Plugin {
})
);

this.registerDomEvent(document, "contextmenu", (event: MouseEvent) => {
const target = event.target as HTMLElement;
if (target.localName !== "img") return;

const imgPath = (target as HTMLImageElement).currentSrc;
const file = this.app.vault.resolveFileUrl(imgPath);

if (!(file instanceof TFile)) return;
const menu = new Menu();
menu.addItem((item) => {
item.setTitle("Open in new window")
.setIcon("open-elsewhere-glyph")
.onClick(async () => {
this.defaultWindow.loadFile(file);
});
});

for (const [name, record] of Object.entries(
this.settings.windows
)) {
if (name === DEFAULT_WINDOW_NAME) continue;
menu.addItem((item) => {
item.setTitle(`Open in window '${name}'`)
.setIcon("open-elsewhere-glyph")
.onClick(async () => {
const namedWindow = this.windows.get(record.id);
if (namedWindow !== undefined) {
namedWindow.loadFile(file);
}
});
});
}

menu.showAtPosition({ x: event.pageX, y: event.pageY });
});

this.addCommand({
id: "open-image",
name: "Open image in new window",
Expand Down

0 comments on commit eefada6

Please sign in to comment.