This commit is contained in:
Zsolt Viczian
2021-12-12 13:44:36 +01:00
parent f4a045b476
commit c39ff3f3e2
10 changed files with 84 additions and 17 deletions

View File

@@ -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.
---------

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

View File

@@ -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",

View File

@@ -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",

View File

@@ -1028,7 +1028,7 @@ export async function initExcalidrawAutomate(
appState: st,
commitToHistory: true,
});
this.targetView.save();
//this.targetView.save();
return true;
},
getViewSelectedElement(): any {

View File

@@ -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);
}
},

View File

@@ -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<string>;
private resolvePromise: (input: string) => void;
@@ -203,3 +203,50 @@ export default class GenericInputPrompt extends Modal {
this.removeInputListener();
}
}
export class GenericSuggester extends FuzzySuggestModal<string>{
private resolvePromise: (value: string) => void;
private rejectPromise: (reason?: any) => void;
public promise: Promise<string>;
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<string>(
(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<string>, 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.");
}
}

View File

@@ -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;
}
}
}

View File

@@ -1,4 +1,4 @@
{
"1.5.0": "0.12.16",
"1.5.1": "0.12.16",
"1.4.2": "0.11.13"
}

View File

@@ -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"