diff --git a/src/index.ts b/src/index.ts index 181189b..0561507 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,9 +19,9 @@ export function isPluginEnabled(app: App): boolean { * @param app * @param component A component that manages the lifecycle of the suggester's quick preview feature. Typically this is your plugin instance. Unload this component to disable quick preview for the suggester. * @param suggestClass A suggester class to be patched. `PopoverSuggest` (e.g. `EditorSuggest` & `AbstractInputSuggest`) or `SuggestModal` are supported. - * @param itemNormalizer A function that converts an item of the suggester to a `SuggestItem` object. + * @param itemNormalizer A function that specifies how the preview for an suggestion item should be triggered. Return null when you don't want to show a quick preview for the item. */ -export function registerQuickPreview(app: App, component: Component, suggestClass: new (...args: any[]) => PopoverSuggest | SuggestModal, itemNormalizer: (item: T) => PreviewInfo): void { +export function registerQuickPreview(app: App, component: Component, suggestClass: new (...args: any[]) => PopoverSuggest | SuggestModal, itemNormalizer: (item: T) => PreviewInfo | null): void { app.workspace.onLayoutReady(() => { const plugin = app.plugins.getPlugin("quick-preview") as QuickPreviewPlugin | undefined; if (!plugin) throw Error("Quick Preview API: Quick Preview is not enabled."); diff --git a/src/main.ts b/src/main.ts index c967630..ad1f35a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -88,7 +88,7 @@ export default class QuickPreviewPlugin extends Plugin { })); } - patchSuggester(suggestClass: new (...args: any[]) => Suggester, itemNormalizer: (item: T) => PreviewInfo) { + patchSuggester(suggestClass: new (...args: any[]) => Suggester, itemNormalizer: (item: T) => PreviewInfo | null) { const prototype = suggestClass.prototype; const plugin = this; diff --git a/src/popoverManager.ts b/src/popoverManager.ts index ada5ed8..53034ca 100644 --- a/src/popoverManager.ts +++ b/src/popoverManager.ts @@ -16,7 +16,7 @@ export class PopoverManager extends Component { popoverHeight: number | null = null; popoverWidth: number | null = null; - constructor(private plugin: QuickPreviewPlugin, public suggest: PatchedSuggester, private itemNormalizer: (item: T) => PreviewInfo) { + constructor(private plugin: QuickPreviewPlugin, public suggest: PatchedSuggester, private itemNormalizer: (item: T) => PreviewInfo | null) { super(); if (suggest instanceof PopoverSuggest) this.suggestions = suggest.suggestions; @@ -74,8 +74,8 @@ export class PopoverManager extends Component { this.currentHoverParent = new QuickPreviewHoverParent(this.suggest); - const info: PreviewInfo = this.itemNormalizer(item); - this.plugin.onLinkHover(this.currentHoverParent, null, info.linktext, info.sourcePath, { scroll: info.line }); + const info = this.itemNormalizer(item); + if (info) this.plugin.onLinkHover(this.currentHoverParent, null, info.linktext, info.sourcePath, { scroll: info.line }); }; getShownPos(): { x: number, y: number } {