diff --git a/docs/ExcalidrawScriptsEngine.md b/docs/ExcalidrawScriptsEngine.md index a2cfd1b..6399303 100644 --- a/docs/ExcalidrawScriptsEngine.md +++ b/docs/ExcalidrawScriptsEngine.md @@ -20,8 +20,13 @@ This will allow you to assign hotkeys to your favorite scripts just like to any ## Script development An Excalidraw script will automatically receive two objects: - `ea`: The Script Enginge will initialize the `ea` object including setting the active view to the View from which the script was called. -- `utils`: There is currently only a single function published on `utils` - - `inputPrompt: (header: string, placeholder?: string, value?: string)`. You need to await the result of inputPrompt. See the example below for details. +- `utils`: I have borrowed functions exposed on utils from [QuickAdd](https://github.com/chhoumann/quickadd/blob/master/docs/QuickAddAPI.md), though currently not all QuickAdd utility functions are implemented in Excalidraw. As of now, these are the available functions. See the example below for details. + - `inputPrompt: (header: string, placeholder?: string, value?: string)` + - Opens a prompt that asks for an input. Returns a string with the input. + - You need to await the result of inputPrompt. + - `suggester: (displayItems: string[], actualItems: string[])` + - Opens a suggester. Displays the displayItems, but you map these the other values with actualItems. Returns the selected value. + - You need to await the result of suggester. --------- diff --git a/images/scripts-split-lines.jpg b/images/scripts-split-lines.jpg new file mode 100644 index 0000000..01766ba Binary files /dev/null and b/images/scripts-split-lines.jpg differ diff --git a/manifest.json b/manifest.json index b0bae38..6b26782 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-excalidraw-plugin", "name": "Excalidraw", - "version": "1.5.0", + "version": "1.5.1", "minAppVersion": "0.12.16", "description": "An Obsidian plugin to edit and view Excalidraw drawings", "author": "Zsolt Viczian", diff --git a/package.json b/package.json index 384c80e..23914ef 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "author": "", "license": "MIT", "dependencies": { - "@zsviczian/excalidraw": "0.10.0-obsidian-18", + "@zsviczian/excalidraw": "0.10.0-obsidian-19", "monkey-around": "^2.2.0", "react": "^17.0.2", "react-dom": "^17.0.2", diff --git a/src/ExcalidrawAutomate.ts b/src/ExcalidrawAutomate.ts index 0cbd1a4..5d4a407 100644 --- a/src/ExcalidrawAutomate.ts +++ b/src/ExcalidrawAutomate.ts @@ -1028,7 +1028,7 @@ export async function initExcalidrawAutomate( appState: st, commitToHistory: true, }); - this.targetView.save(); + //this.targetView.save(); return true; }, getViewSelectedElement(): any { diff --git a/src/ExcalidrawView.ts b/src/ExcalidrawView.ts index ce58f3b..a40289d 100644 --- a/src/ExcalidrawView.ts +++ b/src/ExcalidrawView.ts @@ -1427,7 +1427,7 @@ export default class ExcalidrawView extends TextFileView { if ( document.fullscreenEnabled && document.fullscreenElement == this.contentEl && - e.keyCode == 27 + e.keyCode === 27 ) { document.exitFullscreen(); this.zoomToFit(); @@ -1822,7 +1822,7 @@ export default class ExcalidrawView extends TextFileView { await this.save(false); //this callback function will only be invoked if quick parse fails, i.e. there is a transclusion in the raw text //thus I only check if TextMode.parsed, text is always != with parseResult - if (this.textMode == TextMode.parsed) { + if (this.textMode === TextMode.parsed) { this.excalidrawAPI.history.clear(); } this.setupAutosaveTimer(); @@ -1831,7 +1831,7 @@ export default class ExcalidrawView extends TextFileView { if (parseResult) { //there were no transclusions in the raw text, quick parse was successful this.setupAutosaveTimer(); - if (this.textMode == TextMode.raw) { + if (this.textMode === TextMode.raw) { return; } //text is displayed in raw, no need to clear the history, undo will not create problems if (text == parseResult) { @@ -1843,7 +1843,7 @@ export default class ExcalidrawView extends TextFileView { return; } this.setupAutosaveTimer(); - if (this.textMode == TextMode.parsed) { + if (this.textMode === TextMode.parsed) { return this.excalidrawData.getParsedText(textElement.id); } }, diff --git a/src/Prompt.ts b/src/Prompt.ts index 248a590..9890b12 100644 --- a/src/Prompt.ts +++ b/src/Prompt.ts @@ -1,4 +1,4 @@ -import { App, ButtonComponent, Modal, TextComponent } from "obsidian"; +import { App, ButtonComponent, Modal, TextComponent,FuzzyMatch, FuzzySuggestModal } from "obsidian"; export class Prompt extends Modal { private promptEl: HTMLInputElement; @@ -55,7 +55,7 @@ export class Prompt extends Modal { } } -export default class GenericInputPrompt extends Modal { +export class GenericInputPrompt extends Modal { public waitForClose: Promise; private resolvePromise: (input: string) => void; @@ -203,3 +203,50 @@ export default class GenericInputPrompt extends Modal { this.removeInputListener(); } } + +export class GenericSuggester extends FuzzySuggestModal{ + private resolvePromise: (value: string) => void; + private rejectPromise: (reason?: any) => void; + public promise: Promise; + private resolved: boolean; + + public static Suggest(app: App, displayItems: string[], items: string[]) { + const newSuggester = new GenericSuggester(app, displayItems, items); + return newSuggester.promise; + } + + public constructor(app: App, private displayItems: string[], private items: string[]) { + super(app); + + this.promise = new Promise( + (resolve, reject) => {(this.resolvePromise = resolve); (this.rejectPromise = reject)} + ); + + this.open(); + } + + getItemText(item: string): string { + return this.displayItems[this.items.indexOf(item)]; + } + + getItems(): string[] { + return this.items; + } + + selectSuggestion(value: FuzzyMatch, evt: MouseEvent | KeyboardEvent) { + this.resolved = true; + super.selectSuggestion(value, evt); + } + + onChooseItem(item: string, evt: MouseEvent | KeyboardEvent): void { + this.resolved = true; + this.resolvePromise(item); + } + + onClose() { + super.onClose(); + + if (!this.resolved) + this.rejectPromise("no input given."); + } +} \ No newline at end of file diff --git a/src/Scripts.ts b/src/Scripts.ts index 5407066..c23aee8 100644 --- a/src/Scripts.ts +++ b/src/Scripts.ts @@ -2,7 +2,7 @@ import { App, TAbstractFile, TFile } from "obsidian"; import { VIEW_TYPE_EXCALIDRAW } from "./constants"; import ExcalidrawView from "./ExcalidrawView"; import ExcalidrawPlugin from "./main"; -import GenericInputPrompt from "./Prompt"; +import { GenericInputPrompt, GenericSuggester} from "./Prompt"; import { splitFolderAndFilename } from "./Utils"; export class ScriptEngine { @@ -135,6 +135,8 @@ export class ScriptEngine { return await new AsyncFunction("ea", "utils", script)(this.plugin.ea, { inputPrompt: (header: string, placeholder?: string, value?: string) => ScriptEngine.inputPrompt(this.plugin.app, header, placeholder, value), + suggester: (displayItems: string[], items: string[]) => + ScriptEngine.suggester(this.plugin.app, displayItems, items), }); } @@ -150,4 +152,17 @@ export class ScriptEngine { return undefined; } } + + public static async suggester( + app: App, + displayItems: string[], + items: string[] + ) { + try { + return await GenericSuggester.Suggest(app, displayItems,items); + } catch { + return undefined; + } + } + } diff --git a/versions.json b/versions.json index f15e0a9..b5baad3 100644 --- a/versions.json +++ b/versions.json @@ -1,4 +1,4 @@ { - "1.5.0": "0.12.16", + "1.5.1": "0.12.16", "1.4.2": "0.11.13" } diff --git a/yarn.lock b/yarn.lock index 4de1078..ad045a1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1202,10 +1202,10 @@ "@typescript-eslint/types" "5.6.0" "eslint-visitor-keys" "^3.0.0" -"@zsviczian/excalidraw@0.10.0-obsidian-18": - "integrity" "sha512-hnlDZUVVOMSgIoKRTu6gGe6mQVg4ggExWqB/DyaWJkv9DoFigMUYvPYA8xz3t1hWqtRHKTWOyA1CiFJFK/Zt8w==" - "resolved" "https://registry.npmjs.org/@zsviczian/excalidraw/-/excalidraw-0.10.0-obsidian-18.tgz" - "version" "0.10.0-obsidian-18" +"@zsviczian/excalidraw@0.10.0-obsidian-19": + "integrity" "sha512-sARr3HG4PaFNhvX3ekcFW8fPH9ixKLzs3bZ5tnUeEmQBDfgDV5FMaKMNRHieqcPBGfG5nquIM4oXFAf4ms1RXQ==" + "resolved" "https://registry.npmjs.org/@zsviczian/excalidraw/-/excalidraw-0.10.0-obsidian-19.tgz" + "version" "0.10.0-obsidian-19" dependencies: "dotenv" "10.0.0"