|
| 1 | +import { Modal, Setting } from "obsidian"; |
| 2 | +import ExcalidrawPlugin from "src/core/main"; |
| 3 | +import { t } from "src/lang/helpers"; |
| 4 | +import { fragWithHTML } from "src/utils/utils"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Lists every script that has ever called `ExcalidrawAutomate. |
| 8 | + * registerAutostart()` (i.e. the user ran it at least once), each with an |
| 9 | + * editable Autostart / Manual start only / Ask every time control (reusing |
| 10 | + * the exact same labels as the permission prompt's buttons, so the two |
| 11 | + * surfaces speak one consistent vocabulary), and a warning if the script's |
| 12 | + * most recent autostart run failed (`settings.autostartScriptFailures`, |
| 13 | + * purely informational - it never changes the script's setting on its |
| 14 | + * own). A "Manual start only" (deny) decision is not a dead end: it can be |
| 15 | + * switched straight back to Autostart or Ask every time from here, without |
| 16 | + * needing to re-run the script to get prompted again. |
| 17 | + * |
| 18 | + * Rendered as a bordered table (header row + one row per script), reusing |
| 19 | + * the AI provider table's existing `.excalidraw-ai-provider-table` CSS (see |
| 20 | + * `renderAISettings()` in `settings.ts`) rather than adding near-duplicate |
| 21 | + * rules to `styles.css`, and rather than a foldable `<details>` section, |
| 22 | + * since this is a short, flat list rather than a large collapsible |
| 23 | + * settings group. Rendered both inline in Settings (under the Startup |
| 24 | + * script setting, in the ExcalidrawAutomate section) and inside |
| 25 | + * `AutostartScriptsModal` for the Command Palette entry point, matching |
| 26 | + * how `EmbeddalbeMDFileCustomDataSettingsComponent` is reused across |
| 27 | + * contexts. |
| 28 | + */ |
| 29 | +export class AutostartScriptsSettingsComponent { |
| 30 | + constructor( |
| 31 | + private contentEl: HTMLElement, |
| 32 | + private plugin: ExcalidrawPlugin, |
| 33 | + ) {} |
| 34 | + |
| 35 | + render(): void { |
| 36 | + this.contentEl.empty(); |
| 37 | + this.contentEl.addClass("excalidraw-ai-provider-table"); |
| 38 | + |
| 39 | + const autostartScripts = this.plugin.settings.autostartScripts; |
| 40 | + const scriptNames = Object.keys(autostartScripts).sort(); |
| 41 | + |
| 42 | + const headerSetting = new Setting(this.contentEl) |
| 43 | + .setName(t("AUTOSTART_SCRIPTS_HEAD")) |
| 44 | + .setDesc(fragWithHTML(t("AUTOSTART_SCRIPTS_DESC"))); |
| 45 | + headerSetting.settingEl.addClass("excalidraw-ai-provider-table__header"); |
| 46 | + |
| 47 | + if (scriptNames.length === 0) { |
| 48 | + const emptyRow = new Setting(this.contentEl).setDesc( |
| 49 | + t("AUTOSTART_SCRIPTS_EMPTY"), |
| 50 | + ); |
| 51 | + emptyRow.settingEl.addClass("excalidraw-ai-provider-table__row"); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + scriptNames.forEach((scriptName) => { |
| 56 | + const rowSetting = new Setting(this.contentEl) |
| 57 | + .setName(scriptName) |
| 58 | + .addDropdown((dropdown) => |
| 59 | + dropdown |
| 60 | + .addOption("allow", t("AUTOSTART_SCRIPT_ALLOW")) |
| 61 | + .addOption("deny", t("AUTOSTART_SCRIPT_DENY")) |
| 62 | + .addOption("unknown", t("AUTOSTART_SCRIPT_ASK_LATER")) |
| 63 | + .setValue(autostartScripts[scriptName]) |
| 64 | + .onChange(async (value) => { |
| 65 | + autostartScripts[scriptName] = value as |
| 66 | + | "allow" |
| 67 | + | "deny" |
| 68 | + | "unknown"; |
| 69 | + await this.plugin.saveSettings(); |
| 70 | + }), |
| 71 | + ); |
| 72 | + rowSetting.settingEl.addClass("excalidraw-ai-provider-table__row"); |
| 73 | + |
| 74 | + if (this.plugin.settings.autostartScriptFailures[scriptName]) { |
| 75 | + rowSetting.setDesc( |
| 76 | + fragWithHTML( |
| 77 | + `<span style="color: var(--text-error);">${t("AUTOSTART_SCRIPT_FAILED_WARNING")}</span>`, |
| 78 | + ), |
| 79 | + ); |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +export class AutostartScriptsModal extends Modal { |
| 86 | + constructor(private plugin: ExcalidrawPlugin) { |
| 87 | + super(plugin.app); |
| 88 | + } |
| 89 | + |
| 90 | + onOpen(): void { |
| 91 | + this.titleEl.setText(t("AUTOSTART_SCRIPTS_HEAD")); |
| 92 | + new AutostartScriptsSettingsComponent( |
| 93 | + this.contentEl, |
| 94 | + this.plugin, |
| 95 | + ).render(); |
| 96 | + |
| 97 | + new Setting(this.contentEl).addButton((button) => |
| 98 | + button.setButtonText(t("PROMPT_BUTTON_CLOSE")).onClick(() => this.close()), |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + onClose(): void { |
| 103 | + this.contentEl.empty(); |
| 104 | + } |
| 105 | +} |
0 commit comments