Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,24 @@ export function getGreeting() {
#### Output

> Hello!

## Create Obsidian commands for user scripts

Script commands allow you to run **designated scripts** from the command palette.
These commands can also have a hotkey assigned to them in Obsidians Hotkeys settings.

**How to use:**
- Click the `Add script command` to add a new command settings row
- In the first edit field enter a unique Command ID string
- Must not contain spaces, use hyphens instead (Eg: `execute-js-file-myscriptname`)
- In the second edit field enter the menu text to display in the command palette (Eg: `Execute new note script`)
- This will be displayed in the command palette as `JS Engine: Execute new note script`
- Note the `JS Engine: ` prefix is added by Obsidian itself and can not be removed
- In the third field, a dropdown list, select the script file to execute
- The list contains all js files found in your entire vault
- The button at the end of the settings row with the trash can icon will delete and remove that rows script command
- The button following the `Add script command` will `reload Obsidian`
- Obsidian must be reloaded in order to register any new commands and to remove deleted commands
- It is reccomended to use this button after you are done adding/removing script commands


14 changes: 13 additions & 1 deletion jsEngine/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,19 @@ export default class JsEnginePlugin extends Plugin {
},
});

this.app.workspace.onLayoutReady(() => {
for (let i = 0; i < this.settings.scripts.length; i++) {
if (this.settings.script_ids[i] && this.settings.script_names[i].length && this.settings.scripts[i].length) {
this.addCommand({
id: this.settings.script_ids[i],
name: this.settings.script_names[i],
callback: async () => {
let execution = await this.api.internal.executeFileSimple(this.settings.scripts[i]);
},
});
}
};

this.app.workspace.onLayoutReady(() => {
void this.api.internal.executeStartupScripts();
});

Expand Down
151 changes: 138 additions & 13 deletions jsEngine/settings/Settings.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import type JsEnginePlugin from 'jsEngine/main';
import { StartupScriptsModal } from 'jsEngine/settings/StartupScriptModal';
import type { App } from 'obsidian';
import { PluginSettingTab, Setting } from 'obsidian';
import { PluginSettingTab, Setting, setIcon, TFile } from 'obsidian';

export interface JsEnginePluginSettings {
startupScripts?: string[];
script_ids: string[];
script_names: string[];
scripts: string[];
}

export const JS_ENGINE_DEFAULT_SETTINGS: JsEnginePluginSettings = {
startupScripts: [],
script_ids: [],
script_names: [],
scripts: [],
};

export class JsEnginePluginSettingTab extends PluginSettingTab {
Expand All @@ -20,18 +26,137 @@ export class JsEnginePluginSettingTab extends PluginSettingTab {
}

display(): void {
this.containerEl.empty();
const { containerEl } = this;
containerEl.empty();

if (!this.plugin.settings) {
return;
}
if (!this.plugin.settings) {
return;
}

// this.containerEl.createEl('p', { text: 'Currently Empty, but there will be stuff here later.' });
// Get all vault scripts
let scriptPaths:string[];
let scriptNames:string[];
scriptPaths = [];
scriptNames = [];
let allFiles = this.app.vault.getAllLoadedFiles().filter((file) => file instanceof TFile);
let scriptFiles = allFiles.filter((file) => file.extension === 'js');
scriptPaths = scriptFiles.map(file => file.path);
scriptNames = scriptFiles.map(file => file.name);

new Setting(this.containerEl).setName('Startup scripts').addButton(button => {
button.setButtonText('Manage').onClick(() => {
new StartupScriptsModal(this.plugin).open();
});
});
}
}
// Startup scripts
new Setting(containerEl).setName("Startup Scripts").addButton(button => {
button.setButtonText('Manage').onClick(() => {
new StartupScriptsModal(this.plugin).open();
});
});

// Information heading for script commands
//new Setting(containerEl).setName("Script Commands").setHeading();
const descHeading = document.createDocumentFragment();
descHeading.append(
"Script commands allow you to run ",
descHeading.createEl("strong", { text: "designated scripts " }),
"from the command palette.",
descHeading.createEl("br"),
"These commands can also have a hotkey assigned to them in Obsidians Hotkeys settings.",
descHeading.createEl("br"),
descHeading.createEl("br"),
descHeading.createEl("strong", { text: "How to use: " }),
"In the first edit field enter a unique Command ID (Eg: execute-js-file-myscript)",
descHeading.createEl("br"),
"In the second edit field enter the menu text to display in the command palette (Eg: `Execute new note script`)",
descHeading.createEl("br"),
"This will be displayed in the command palette as ",
descHeading.createEl("strong", { text: "JS Engine: Execute new note script" }),
descHeading.createEl("br"),
"In the third field dropdown select the script file to execute (the list contains all js files found in your vault)",
);
new Setting(containerEl).setName("Script Commands").setDesc(descHeading);

// Add all script commands
// this.plugin.settings.script_ids.forEach((script_ids, index) => {
for (let [index, ids] of this.plugin.settings.script_ids.entries()) {
//console.log('IDs',ids,'Index',index);

const s = new Setting(this.containerEl);

// Script Command ID
s.addText((text) =>
text
.setPlaceholder('Command ID')
.setValue(this.plugin.settings.script_ids[index])
.then(textEl => {
textEl.inputEl.style.width = '215px';
textEl.inputEl.style.marginRight = "10px";
})
.onChange(async (value) => {
this.plugin.settings.script_ids[index] = value;
await this.plugin.saveSettings();
})
);

// Script Menu text
s.addText((text) =>
text
.setPlaceholder('Menu text')
.setValue(this.plugin.settings.script_names[index])
.then(textEl => {
textEl.inputEl.style.width = '215px';
textEl.inputEl.style.marginRight = "10px";
})
.onChange(async (value) => {
this.plugin.settings.script_names[index] = value;
await this.plugin.saveSettings();
})
);

// Script filename
s.addDropdown((cb) => {
cb
for (let i = 0; i < scriptPaths.length; i++) {
cb.addOption(scriptPaths[i], scriptNames[i]);
};
cb.setValue(this.plugin.settings.scripts[index]);
cb.then(dropEl => {
dropEl.selectEl.style.width = '220px';
dropEl.selectEl.style.marginRight = "10px";
})
cb.onChange(async (value) => {
this.plugin.settings.scripts[index] = value;
await this.plugin.saveSettings();
});
});

// Script delete button
s.addButton((el) =>
el
.setButtonText(`Delete `)
.setIcon("trash")
.onClick(async () => {
this.plugin.settings.script_ids.splice(index, 1);
this.plugin.settings.script_names.splice(index, 1);
this.plugin.settings.scripts.splice(index, 1);
await this.plugin.saveSettings();
this.display();
}));
};

// Script add button
new Setting(containerEl).addButton((el) =>
el.setButtonText("Add script command").onClick(() => {
this.plugin.settings.script_ids.push("");
this.plugin.settings.script_names.push("");
this.plugin.settings.scripts.push("");
this.display(); // Update settings display
}),
)
.addExtraButton(button => button
.setIcon('refresh-ccw')
.setTooltip("Reload Obsidian to implement changes")
.onClick(() => {
const app = this.app as any
app.commands.executeCommandById('app:reload');
})
);
};
};
Loading