Skip to content

Commit

Permalink
Allow an item normalizer to return null
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaUshio committed Dec 12, 2023
1 parent d793814 commit df62880
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(app: App, component: Component, suggestClass: new (...args: any[]) => PopoverSuggest<T> | SuggestModal<T>, itemNormalizer: (item: T) => PreviewInfo): void {
export function registerQuickPreview<T>(app: App, component: Component, suggestClass: new (...args: any[]) => PopoverSuggest<T> | SuggestModal<T>, 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.");
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default class QuickPreviewPlugin extends Plugin {
}));
}

patchSuggester<T>(suggestClass: new (...args: any[]) => Suggester<T>, itemNormalizer: (item: T) => PreviewInfo) {
patchSuggester<T>(suggestClass: new (...args: any[]) => Suggester<T>, itemNormalizer: (item: T) => PreviewInfo | null) {
const prototype = suggestClass.prototype;
const plugin = this;

Expand Down
6 changes: 3 additions & 3 deletions src/popoverManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class PopoverManager<T> extends Component {
popoverHeight: number | null = null;
popoverWidth: number | null = null;

constructor(private plugin: QuickPreviewPlugin, public suggest: PatchedSuggester<T>, private itemNormalizer: (item: T) => PreviewInfo) {
constructor(private plugin: QuickPreviewPlugin, public suggest: PatchedSuggester<T>, private itemNormalizer: (item: T) => PreviewInfo | null) {
super();

if (suggest instanceof PopoverSuggest) this.suggestions = suggest.suggestions;
Expand Down Expand Up @@ -74,8 +74,8 @@ export class PopoverManager<T> 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 } {
Expand Down

0 comments on commit df62880

Please sign in to comment.