Skip to content

Commit

Permalink
release: 0.6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaUshio committed Jun 6, 2024
1 parent 7027c18 commit fb719cf
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 50 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
39 changes: 8 additions & 31 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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: '' };
Expand All @@ -52,7 +55,6 @@ export default class QuickPreviewPlugin extends Plugin {
return info;
}
);
this.patchCanvasSuggest();
});
}

Expand Down Expand Up @@ -104,11 +106,10 @@ export default class QuickPreviewPlugin extends Plugin {

const uninstaller = around(prototype, {
open(old) {
return function () {
return function (this: PatchedSuggester<T>) {
old.call(this);
const self = this as PatchedSuggester<T>;
if (!self.popoverManager) self.popoverManager = new PopoverManager<T>(plugin, self, itemNormalizer);
self.popoverManager.load();
if (!this.popoverManager) this.popoverManager = new PopoverManager<T>(plugin, this, itemNormalizer);
this.popoverManager.load();
}
},
close(old) {
Expand All @@ -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);
}
}
33 changes: 21 additions & 12 deletions src/popoverManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ export class PopoverManager<T> 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 {
Expand All @@ -33,12 +41,12 @@ export class PopoverManager<T> 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();
});

Expand Down Expand Up @@ -75,12 +83,13 @@ export class PopoverManager<T> 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 } {
Expand All @@ -100,11 +109,11 @@ export class PopoverManager<T> 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 } {
Expand All @@ -119,7 +128,7 @@ export class PopoverManager<T> 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 };
Expand All @@ -129,11 +138,11 @@ export class PopoverManager<T> 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');
}
}

0 comments on commit fb719cf

Please sign in to comment.