Skip to content

Commit

Permalink
fix: remove all trace of text input suggest
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Oct 19, 2023
1 parent b924ba7 commit 68cace2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 264 deletions.
217 changes: 0 additions & 217 deletions src/suggesters/suggest.ts

This file was deleted.

9 changes: 4 additions & 5 deletions src/suggesters/suggestArray.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { App } from "obsidian";
import { TextInputSuggest } from "./suggest";
import { AbstractInputSuggest, App } from "obsidian";

export class ArraySuggest extends TextInputSuggest<string> {
export class ArraySuggest extends AbstractInputSuggest<string> {
content: Set<string>;

constructor(app: App, input: HTMLInputElement, content: Set<string>) {
super(app, input);
constructor(app: App, private inputEl: HTMLInputElement, content: Set<string>) {
super(app, inputEl);
this.content = content;
}

Expand Down
9 changes: 3 additions & 6 deletions src/suggesters/suggestFile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
// Credits go to Liam's Periodic Notes Plugin: https://github.com/liamcain/obsidian-periodic-notes

import { App, TAbstractFile, TFile } from "obsidian";
import { TextInputSuggest } from "./suggest";
import { AbstractInputSuggest, App, TAbstractFile, TFile } from "obsidian";
import { get_tfiles_from_folder } from "../utils/files";
import { tryCatch } from "../utils/Error";

Expand All @@ -13,9 +10,9 @@ export interface FileStrategy {
selectSuggestion(file: TFile): string;
}

export class FileSuggest extends TextInputSuggest<TFile> {
export class FileSuggest extends AbstractInputSuggest<TFile> {
constructor(
protected app: App,
public app: App,
public inputEl: HTMLInputElement,
private strategy: FileStrategy,
private folder: string,
Expand Down
71 changes: 35 additions & 36 deletions src/suggesters/suggestFromDataview.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Credits go to Liam's Periodic Notes Plugin: https://github.com/liamcain/obsidian-periodic-notes
import { App, TAbstractFile, TFolder } from "obsidian";
import { TextInputSuggest } from "./suggest";
import { AbstractInputSuggest, App } from "obsidian";
import { ModalFormError, tryCatch } from "src/utils/Error";
import { log_error } from "src/utils/Log";

Expand All @@ -9,42 +8,42 @@ import { log_error } from "src/utils/Log";
* It requires the dataview plugin to be installed and enabled.
* For now, we are not very strict with the checks and just throw errors
*/
export class DataviewSuggest extends TextInputSuggest<string> {
sandboxedQuery: (dv: any, pages: any) => string[]
export class DataviewSuggest extends AbstractInputSuggest<string> {
sandboxedQuery: (dv: any, pages: any) => string[]

constructor(
public inputEl: HTMLInputElement,
dvQuery: string,
protected app: App,
) {
super(app, inputEl);
this.sandboxedQuery = tryCatch(
() => eval(`(function sandboxedQuery(dv, pages) { return ${dvQuery} })`),
"Invalid dataview query"
)
}
constructor(
public inputEl: HTMLInputElement,
dvQuery: string,
public app: App,
) {
super(app, inputEl);
this.sandboxedQuery = tryCatch(
() => eval(`(function sandboxedQuery(dv, pages) { return ${dvQuery} })`),
"Invalid dataview query"
)
}

getSuggestions(inputStr: string): string[] {
const dv = this.app.plugins.plugins.dataview?.api
if (!dv) {
log_error(new ModalFormError("Dataview plugin is not enabled"))
return [];
}
const result = this.sandboxedQuery(dv, dv.pages)
if (!Array.isArray(result)) {
log_error(new ModalFormError("The dataview query did not return an array"))
return [];
}
return result.filter(r => r.toLowerCase().includes(inputStr.toLowerCase()))
}
getSuggestions(inputStr: string): string[] {
const dv = this.app.plugins.plugins.dataview?.api
if (!dv) {
log_error(new ModalFormError("Dataview plugin is not enabled"))
return [];
}
const result = this.sandboxedQuery(dv, dv.pages)
if (!Array.isArray(result)) {
log_error(new ModalFormError("The dataview query did not return an array"))
return [];
}
return result.filter(r => r.toLowerCase().includes(inputStr.toLowerCase()))
}

renderSuggestion(option: string, el: HTMLElement): void {
el.setText(option);
}
renderSuggestion(option: string, el: HTMLElement): void {
el.setText(option);
}

selectSuggestion(option: string): void {
this.inputEl.value = option;
this.inputEl.trigger("input");
this.close();
}
selectSuggestion(option: string): void {
this.inputEl.value = option;
this.inputEl.trigger("input");
this.close();
}
}

0 comments on commit 68cace2

Please sign in to comment.