Skip to content

Commit

Permalink
feat: execute optional command (after creating file from template)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgurney committed Dec 21, 2024
1 parent a467628 commit 8f0f191
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/Adapters/TemplaterAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export default class TemplaterAdapter extends Adapter {
description: "",
parameters: [
{ parameter: 'sourceFile', label: t('adapter.templater.create-sourcefile'), description: t('adapter.templater.create-sourcefile-description'), type: SettingType.File, required: true },
{ parameter: 'outputFile', label: t('adapter.templater.create-outputfile'), description: t('adapter.templater.create-outputfile-description'), type: SettingType.Text, required: false }
{ parameter: 'outputFile', label: t('adapter.templater.create-outputfile'), description: t('adapter.templater.create-outputfile-description'), type: SettingType.Text, required: false },
{ parameter: 'postCommand', label: t('adapter.postCommand'), description: t('adapter.postCommand-description'), type: SettingType.Command, required: false }
]
},
{
Expand Down Expand Up @@ -97,6 +98,15 @@ export default class TemplaterAdapter extends Adapter {
break;
}

if (config.postCommand) {
try {
await this.noteToolbar?.app.commands.executeCommandById(config.postCommand);
}
catch (error) {
throw new Error(error);
}
}

return result;
}

Expand Down Expand Up @@ -126,6 +136,7 @@ export default class TemplaterAdapter extends Adapter {
/**
* Calls create_new_note_from_template.
* @param filename
* @param outputFile
*/
async createFrom(filename: string, outputFile?: string): Promise<void> {

Expand All @@ -143,7 +154,6 @@ export default class TemplaterAdapter extends Adapter {
try {
if (templateFile) {
await this.adapterApi.create_new_note_from_template(templateFile, outputFolder, outputFilename);
await this.noteToolbar?.app.commands.executeCommandById('file-explorer:reveal-active-file');
}
else {
throw new Error(t('adapter.error.file-not-found', { filename: filename }));
Expand Down
4 changes: 3 additions & 1 deletion src/I18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"args-description": "Arguments accepted by your script function, in comma-separated 'name: value' format.",
"outputcontainer": "Output callout ID (optional)",
"outputcontainer-description": "Add a note-toolbar-output callout with a unique meta field to your note to put text output.",
"postCommand": "Command (optional)",
"postCommand-description": "Command to run after execution.",
"dataview": {
"error-general": "Dataview error: Check console for more details.\n```\n{{error}}\n```",
"dvjs-function": "Evaluate Dataview JS expression",
Expand Down Expand Up @@ -46,7 +48,7 @@
"create-sourcefile": "Template",
"create-sourcefile-description": "Template file to create a new file from.",
"create-sourcefile-error-required": "Error: A template file is required",
"create-outputfile": "(Optional) Output filename",
"create-outputfile": "Output filename (optional)",
"create-outputfile-description": "Name of the file (without file extension) and folder(s) to create, from the provided template.",
"eval-function": "Execute Templater command",
"eval-expr": "Templater command",
Expand Down
1 change: 1 addition & 0 deletions src/Settings/NoteToolbarSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ export interface ScriptConfig {
sourceArgs?: string;
outputContainer?: string;
outputFile?: string;
postCommand?: string;
};

/******************************************************************************
Expand Down
28 changes: 25 additions & 3 deletions src/Settings/UI/Modals/ToolbarSettingsModal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { App, ButtonComponent, Command, DropdownComponent, Menu, MenuItem, Modal, Notice, Platform, Setting, TFile, TFolder, debounce, getIcon, normalizePath, setIcon, setTooltip } from 'obsidian';
import { arraymove, debugLog, getElementPosition, removeComponentVisibility, addComponentVisibility, moveElement, getUUID, importArgs, getCommandIdByName } from 'Utils/Utils';
import { arraymove, debugLog, getElementPosition, removeComponentVisibility, addComponentVisibility, moveElement, getUUID, importArgs, getCommandIdByName, getCommandNameById } from 'Utils/Utils';
import { emptyMessageFr, learnMoreFr, createToolbarPreviewFr, displayHelpSection, showWhatsNewIfNeeded, pluginLinkFr } from "../Utils/SettingsUIUtils";
import NoteToolbarPlugin from 'main';
import { DEFAULT_STYLE_OPTIONS, ItemType, MOBILE_STYLE_OPTIONS, POSITION_OPTIONS, PositionType, DEFAULT_STYLE_DISCLAIMERS, ToolbarItemSettings, ToolbarSettings, MOBILE_STYLE_DISCLAIMERS, LINK_OPTIONS, ComponentType, t, DEFAULT_ITEM_VISIBILITY_SETTINGS, COMMAND_DOES_NOT_EXIST, ScriptConfig, SettingType, SettingFieldItemMap } from 'Settings/NoteToolbarSettings';
Expand Down Expand Up @@ -1124,6 +1124,28 @@ export default class ToolbarSettingsModal extends Modal {
let initialValue = config[param.parameter as keyof ScriptConfig];
let setting;
switch (param.type) {
case SettingType.Command:
setting = new Setting(fieldDiv)
.setClass("note-toolbar-setting-item-field-link")
.addSearch((cb) => {
new CommandSuggester(this.app, cb.inputEl, async (command) => {
this.updateItemComponentStatus(command.id, param.type, cb.inputEl.parentElement);
config[param.parameter as keyof ScriptConfig] = command.id;
cb.inputEl.value = command.name;
await this.plugin.settingsManager.save();
});
cb.setPlaceholder(param.label)
.setValue(initialValue ? (getCommandNameById(this.plugin, initialValue) || '') : '')
.onChange(debounce(async (commandName) => {
const commandId = commandName ? getCommandIdByName(this.plugin, commandName) : '';
const isValid = this.updateItemComponentStatus(commandId, param.type, cb.inputEl.parentElement);
config[param.parameter as keyof ScriptConfig] = isValid && commandId ? commandId : '';
await this.plugin.settingsManager.save();
this.renderPreview(toolbarItem); // to make sure error state is refreshed
}, 500));
this.updateItemComponentStatus(initialValue ? initialValue : '', param.type, cb.inputEl.parentElement);
});
break;
case SettingType.File:
setting = new Setting(fieldDiv)
.setClass("note-toolbar-setting-item-field-link")
Expand Down Expand Up @@ -1157,7 +1179,7 @@ export default class ToolbarSettingsModal extends Modal {
cb.setPlaceholder(param.label)
.setValue(initialValue ? initialValue : '')
.onChange(
debounce(async (value) => {
debounce(async (value: string) => {
let isValid = this.updateItemComponentStatus(value, param.type, cb.inputEl.parentElement);
config[param.parameter as keyof ScriptConfig] = isValid ? value : '';
this.toolbar.updated = new Date().toISOString();
Expand All @@ -1175,7 +1197,7 @@ export default class ToolbarSettingsModal extends Modal {
cb.setPlaceholder(param.label)
.setValue(initialValue ? initialValue : '')
.onChange(
debounce(async (value) => {
debounce(async (value: string) => {
config[param.parameter as keyof ScriptConfig] = value;
this.toolbar.updated = new Date().toISOString();
await this.plugin.settingsManager.save();
Expand Down

0 comments on commit 8f0f191

Please sign in to comment.