generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #207 from danielo515/feat/multi-select-note-improved
feat: multi select notes uses the new notes input (better UI and search)
- Loading branch information
Showing
5 changed files
with
132 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/suggesters/MultiSuggest.ts → src/suggesters/StringSuggest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { A, pipe } from "@std"; | ||
import { absurd } from "fp-ts/function"; | ||
import { App } from "obsidian"; | ||
import { multiselect, inputTag } from "src/core/InputDefinitionSchema"; | ||
import { executeSandboxedDvQuery, sandboxedDvQuery } from "src/suggesters/SafeDataviewQuery"; | ||
import { StringSuggest } from "src/suggesters/StringSuggest"; | ||
import { FileSuggest } from "src/suggesters/suggestFile"; | ||
import { Writable } from "svelte/store"; | ||
|
||
export interface MultiSelectModel { | ||
createInput(element: HTMLInputElement): void; | ||
removeValue(value: string): void; | ||
} | ||
|
||
export function MultiSelectModel( | ||
fieldInput: multiselect, | ||
app: App, | ||
values: Writable<string[]>, | ||
): MultiSelectModel { | ||
const source = fieldInput.source; | ||
const removeValue = (value: string) => | ||
values.update((xs) => | ||
pipe( | ||
xs, | ||
A.filter((x) => x !== value), | ||
), | ||
); | ||
switch (source) { | ||
case "dataview": | ||
case "fixed": { | ||
const remainingOptions = new Set( | ||
source === "fixed" | ||
? fieldInput.multi_select_options | ||
: executeSandboxedDvQuery(sandboxedDvQuery(fieldInput.query), app), | ||
); | ||
return { | ||
createInput(element: HTMLInputElement) { | ||
new StringSuggest( | ||
element, | ||
remainingOptions, | ||
(selected) => { | ||
remainingOptions.delete(selected); | ||
values.update((x) => [...x, selected]); | ||
}, | ||
app, | ||
fieldInput.allowUnknownValues, | ||
); | ||
}, | ||
removeValue(value: string) { | ||
remainingOptions.add(value); | ||
removeValue(value); | ||
}, | ||
}; | ||
} | ||
case "notes": { | ||
return { | ||
createInput(element: HTMLInputElement) { | ||
new FileSuggest( | ||
app, | ||
element, | ||
{ | ||
renderSuggestion(file) { | ||
return file.basename; | ||
}, | ||
selectSuggestion(file) { | ||
values.update((x) => [...x, file.basename]); | ||
return ""; | ||
}, | ||
}, | ||
fieldInput.folder, | ||
); | ||
}, | ||
removeValue, | ||
}; | ||
} | ||
default: | ||
return absurd(source); | ||
} | ||
} | ||
|
||
export function MultiSelectTags( | ||
fieldInput: inputTag, | ||
app: App, | ||
values: Writable<string[]>, | ||
): MultiSelectModel { | ||
const remainingOptions = new Set( | ||
Object.keys(app.metadataCache.getTags()).map( | ||
(tag) => tag.slice(1) /** remove the leading # */, | ||
), | ||
); | ||
return { | ||
createInput(element: HTMLInputElement) { | ||
new StringSuggest( | ||
element, | ||
remainingOptions, | ||
(selected) => { | ||
remainingOptions.delete(selected); | ||
values.update((x) => [...x, selected]); | ||
}, | ||
app, | ||
false, | ||
); | ||
}, | ||
removeValue(value: string) { | ||
remainingOptions.add(value); | ||
values.update((x) => | ||
pipe( | ||
x, | ||
A.filter((x) => x !== value), | ||
), | ||
); | ||
}, | ||
}; | ||
} |