From fb719cfe0dd7937186a7ee46e5cfa55a69b0f3c5 Mon Sep 17 00:00:00 2001 From: RyotaUshio Date: Thu, 6 Jun 2024 15:59:56 +0900 Subject: [PATCH] release: 0.6.1 --- README.md | 3 --- manifest.json | 2 +- package-lock.json | 4 ++-- package.json | 2 +- src/main.ts | 39 ++++++++------------------------------- src/popoverManager.ts | 33 +++++++++++++++++++++------------ 6 files changed, 33 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 3dfcbde..bb6382f 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,5 @@ # Obsidian Quick Preview -> [!warning] -> This plugin is currently not compatible with Obsidian v1.6. I will release the necessary fix shortly. - This [Obsidian.md](https://obsidian.md) plugin adds a ***quick preview*** functionality to - [Link suggestions](https://help.obsidian.md/Linking+notes+and+files/Internal+links), - [Quick switcher](https://help.obsidian.md/Plugins/Quick+switcher), diff --git a/manifest.json b/manifest.json index ba41414..cd6753e 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "quick-preview", "name": "Quick Preview", - "version": "0.6.0", + "version": "0.6.1", "minAppVersion": "1.3.5", "description": "Quickly preview a suggestion before selecting it in link suggestions & quick switcher.", "author": "Ryota Ushio", diff --git a/package-lock.json b/package-lock.json index a2d22e4..ceee06d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "obsidian-quick-preview", - "version": "0.6.0", + "version": "0.6.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "obsidian-quick-preview", - "version": "0.6.0", + "version": "0.6.1", "license": "MIT", "dependencies": { "monkey-around": "^2.3.0" diff --git a/package.json b/package.json index aa215f4..631253e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-quick-preview", - "version": "0.6.0", + "version": "0.6.1", "description": "An Obsidian.md plugin to quickly preview a suggestion before selecting it in link suggestions & quick swicher.", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/main.ts b/src/main.ts index 9529586..4b3f48e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,4 @@ -import { stripHeadingForLink, SuggestModal } from 'obsidian'; +import { stripHeadingForLink } from 'obsidian'; import { HoverParent, Keymap, Plugin, UserEvent } from 'obsidian'; import { around } from 'monkey-around'; @@ -40,8 +40,11 @@ export default class QuickPreviewPlugin extends Plugin { else if (item.type === 'block') info.line = item.node.position.start.line; return info; }); + const quickSwitcherConstructor = this.app.internalPlugins.getPluginById('switcher').instance.QuickSwitcherModal; + // Superclass of the Quick Switcher modal, canvas's file suggester modal, etc + const generalFileSuggestConstructor = Object.getPrototypeOf(quickSwitcherConstructor); this.patchSuggester( - this.app.internalPlugins.getPluginById('switcher').instance.QuickSwitcherModal, + generalFileSuggestConstructor, (item: QuickSwitcherItem | QuickSwitcherPlusHeadingItem | QuickSwitcherPlusSymbolItem | QuickSwitcherPlusFileBookmarkItem): PreviewInfo | null => { if (!item.file) return null; const info: PreviewInfo = { linktext: item.file.path, sourcePath: '' }; @@ -52,7 +55,6 @@ export default class QuickPreviewPlugin extends Plugin { return info; } ); - this.patchCanvasSuggest(); }); } @@ -104,11 +106,10 @@ export default class QuickPreviewPlugin extends Plugin { const uninstaller = around(prototype, { open(old) { - return function () { + return function (this: PatchedSuggester) { old.call(this); - const self = this as PatchedSuggester; - if (!self.popoverManager) self.popoverManager = new PopoverManager(plugin, self, itemNormalizer); - self.popoverManager.load(); + if (!this.popoverManager) this.popoverManager = new PopoverManager(plugin, this, itemNormalizer); + this.popoverManager.load(); } }, close(old) { @@ -124,28 +125,4 @@ export default class QuickPreviewPlugin extends Plugin { return uninstaller; } - - patchCanvasSuggest() { - const plugin = this; - - const uninstaller = around(SuggestModal.prototype, { - setInstructions(old) { - return function (...args: any[]) { - old.call(this, ...args); - const proto = Object.getPrototypeOf(this); - - if (this.hasOwnProperty('canvas') && proto.hasOwnProperty('showMarkdownAndCanvas') && proto.hasOwnProperty('showAttachments')) { - plugin.patchSuggester(this.constructor, (item: QuickSwitcherItem): PreviewInfo | null => { - if (!item.file) return null; - return { linktext: item.file.path, sourcePath: '' } - }); - - uninstaller(); - } - } - } - }); - - this.register(uninstaller); - } } diff --git a/src/popoverManager.ts b/src/popoverManager.ts index 36297e3..7ae0ee4 100644 --- a/src/popoverManager.ts +++ b/src/popoverManager.ts @@ -23,8 +23,16 @@ export class PopoverManager extends Component { else this.suggestions = suggest.chooser; } + get doc() { + return this.suggestions.containerEl.doc; + } + + get win() { + return this.doc.win; + } + onload() { - this.registerDomEvent(window, 'keydown', (event) => { + this.registerDomEvent(this.win, 'keydown', (event) => { if (this.suggest.isOpen && Keymap.isModifier(event, this.plugin.settings.modifier)) { if (this.currentOpenHoverParent) this.hide(); else { @@ -33,12 +41,12 @@ export class PopoverManager extends Component { } } }); - this.registerDomEvent(window, 'keyup', (event: KeyboardEvent) => { + this.registerDomEvent(this.win, 'keyup', (event: KeyboardEvent) => { if (event.key === this.plugin.settings.modifier) this.hide(); }); // This is a workaround for the problem that the keyup event is not fired when command key is released on macOS. // cf.) https://blog.bitsrc.io/keyup-event-and-cmd-problem-88f4038c5ed2 - this.registerDomEvent(window, 'mousemove', (event: MouseEvent) => { + this.registerDomEvent(this.win, 'mousemove', (event: MouseEvent) => { if (!Keymap.isModifier(event, this.plugin.settings.modifier)) this.hide(); }); @@ -75,12 +83,13 @@ export class PopoverManager extends Component { spawnPreview(item: T, lazyHide: boolean = false, event: UserEvent | null = null) { this.hide(lazyHide); - if (event instanceof MouseEvent || event instanceof PointerEvent) this.lastEvent = event; + if (event && (event.instanceOf(MouseEvent) || event.instanceOf(PointerEvent))) this.lastEvent = event; this.currentHoverParent = new QuickPreviewHoverParent(this.suggest); const info = this.itemNormalizer(item); - if (info) this.plugin.onLinkHover(this.currentHoverParent, null, info.linktext, info.sourcePath, { scroll: info.line }); + // From Obsidian v1.6, passing null as targetEl is no longer allowed. + if (info) this.plugin.onLinkHover(this.currentHoverParent, this.doc.body, info.linktext, info.sourcePath, { scroll: info.line }); } getShownPos(): { x: number, y: number } { @@ -100,11 +109,11 @@ export class PopoverManager extends Component { if (position === 'Top left') { return { x: 0, y: 0 }; } else if (position === 'Top right') { - return { x: window.innerWidth, y: 0 }; + return { x: this.win.innerWidth, y: 0 }; } else if (position === 'Bottom left') { - return { x: 0, y: window.innerHeight }; + return { x: 0, y: this.win.innerHeight }; } - return { x: window.innerWidth, y: window.innerHeight }; + return { x: this.win.innerWidth, y: this.win.innerHeight }; } getShownPosAuto(): { x: number, y: number } { @@ -119,7 +128,7 @@ export class PopoverManager extends Component { // show the popover next to the suggestion box if possible let offsetX = width * 0.1; let offsetY = height * 0.1; - if (right - offsetX + this.popoverWidth < window.innerWidth) return { x: right - offsetX, y: top + offsetY }; + if (right - offsetX + this.popoverWidth < this.win.innerWidth) return { x: right - offsetX, y: top + offsetY }; offsetX = width * 0.03; offsetY = height * 0.05; if (left > this.popoverWidth + offsetX) return { x: left - this.popoverWidth - offsetX, y: top + offsetY }; @@ -129,11 +138,11 @@ export class PopoverManager extends Component { const x = (left + right) * 0.5; const y = (top + bottom) * 0.5; - if (x >= window.innerWidth * 0.6) { // not a typo. suggestion text tends to be on the left side. avoid covering it - if (y >= window.innerHeight * 0.5) return this.getShownPosCorner('Top left'); + if (x >= this.win.innerWidth * 0.6) { // not a typo. suggestion text tends to be on the left side. avoid covering it + if (y >= this.win.innerHeight * 0.5) return this.getShownPosCorner('Top left'); return this.getShownPosCorner('Bottom left'); } - if (y >= window.innerHeight * 0.5) return this.getShownPosCorner('Top right'); + if (y >= this.win.innerHeight * 0.5) return this.getShownPosCorner('Top right'); return this.getShownPosCorner('Bottom right'); } }