Skip to content

Commit

Permalink
use templatable command instead of URL
Browse files Browse the repository at this point in the history
  • Loading branch information
NomarCub committed Aug 7, 2021
1 parent 4960751 commit d6c09fb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ The icons work with light and dark mode.
You can also use it as a command and assign hotkeys to it. You can disable the ribbon button in settings.
![command](https://user-images.githubusercontent.com/5298006/125869408-d39d870b-ab4f-42d0-b915-b6abc1e617d5.png)

By default the plugin uses `child_process` to launch VSCode with the `code` command, but the previous method using URLs can still be enabled.

You can template the command opening VSCode however you like with its [provided command line arguments](https://code.visualstudio.com/docs/editor/command-line). This way you can technically launch any command you set, so take caution. Potential use cases include opening workspaces with `.code-workspace` files (e.g. for Dendron), opening specific files, folders, etc.

## Compatibility

The plugin was tested in Obsidian v0.11.13 and subsequent versions, but probably works with older versions.
Expand All @@ -24,7 +28,7 @@ You can also manually copy from releases to your `.obsidian/plugins/open-vscode`

## Issues

VSCode can't open a workspace without a further confirmation dialog (that you just can hit enter on) for security reasons. See [this issue](https://github.com/microsoft/vscode/issues/95670) for more infomation.
Using the URL method for opening, VSCode can't open a workspace without a further confirmation dialog (that you just can hit enter on) for security reasons. See [this issue](https://github.com/microsoft/vscode/issues/95670) for more infomation.

## Credits

Expand Down
49 changes: 41 additions & 8 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
addIcon, FileSystemAdapter, Plugin,
PluginSettingTab, App, Setting
addIcon, App, FileSystemAdapter, Plugin,
PluginSettingTab, Setting
} from 'obsidian';

let svg = `
const svg = `
<path
fill="currentColor"
d="M 96.453125 10.773438 L 75.882812 0.878906 C 73.488281 -0.277344 70.640625 0.210938 68.769531 2.082031 L 29.367188 38.035156 L 12.195312 25.011719 C 10.601562 23.789062 8.351562 23.890625 6.871094 25.242188 L 1.371094 30.253906 C -0.449219 31.898438 -0.449219 34.761719 1.355469 36.40625 L 16.25 49.996094 L 1.355469 63.585938 C -0.449219 65.230469 -0.449219 68.097656 1.371094 69.742188 L 6.871094 74.753906 C 8.367188 76.101562 10.601562 76.203125 12.195312 74.980469 L 29.367188 61.945312 L 68.789062 97.914062 C 70.644531 99.785156 73.492188 100.273438 75.882812 99.117188 L 96.476562 89.203125 C 98.640625 88.164062 100.007812 85.980469 100.007812 83.570312 L 100.007812 16.398438 C 100.007812 14.007812 98.621094 11.808594 96.460938 10.769531 Z M 75.015625 72.707031 L 45.101562 50 L 75.015625 27.292969 Z M 75.015625 72.707031"
Expand Down Expand Up @@ -32,9 +32,17 @@ export default class OpenVSCode extends Plugin {
return;
}

let path = this.app.vault.adapter.getBasePath();
let url = "vscode://file/" + path;
window.open(url, "_blank");
const path = this.app.vault.adapter.getBasePath();
if (this.settings.useURL) {
const url = "vscode://file/" + path;
window.open(url, "_blank");
}
else {
const { exec } = require("child_process");
const template = this.settings.executeTemplate.trim() === "" ? DEFAULT_SETTINGS.executeTemplate : this.settings.executeTemplate;
const command = template.replace("{{vaultpath}}", path);
exec(command);
}
}

async loadSettings() {
Expand All @@ -57,10 +65,14 @@ export default class OpenVSCode extends Plugin {

interface OpenVSCodeSettings {
ribbonIcon: boolean,
useURL: boolean,
executeTemplate: string,
}

const DEFAULT_SETTINGS: OpenVSCodeSettings = {
ribbonIcon: true,
useURL: false,
executeTemplate: "code {{vaultpath}}",
}

class OpenVSCodeSettingsTab extends PluginSettingTab {
Expand All @@ -73,7 +85,7 @@ class OpenVSCodeSettingsTab extends PluginSettingTab {
}

display(): void {
let { containerEl } = this;
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("h2", { text: "Settings" });

Expand All @@ -87,7 +99,28 @@ class OpenVSCodeSettingsTab extends PluginSettingTab {
this.plugin.saveSettings();
this.plugin.refreshIconRibbon();
})
)
);
new Setting(containerEl)
.setName('Use URL')
.setDesc('Open VSCode using a `vscode://` URL instead of executing the `code` command.')
.addToggle((toggle) => toggle
.setValue(this.plugin.settings.useURL)
.onChange((value) => {
this.plugin.settings.useURL = value;
this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName('Default template for executing the `code` command')
.setDesc('You can use the following placeholders: {{vaultpath}}')
.addText(text => text.setPlaceholder(DEFAULT_SETTINGS.executeTemplate)
.setValue(this.plugin.settings.executeTemplate || DEFAULT_SETTINGS.executeTemplate)
.onChange((value) => {
value = value.trim();
if (value === "") value = DEFAULT_SETTINGS.executeTemplate;
this.plugin.settings.executeTemplate = value;
this.plugin.saveData(this.plugin.settings);
}));
}

}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "open-vscode",
"name": "Open vault in VSCode",
"version": "1.0.2",
"version": "1.1.0",
"minAppVersion": "0.11.13",
"description": "Ribbon button and command to open vault as a Visual Studio Code workspace",
"author": "NomarCub",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "open-vscode",
"version": "1.0.2",
"version": "1.1.0",
"description": "Open vault in Visual Studio Code ribbon button and command for Obsidian (https://obsidian.md)",
"main": "main.js",
"scripts": {
Expand Down

0 comments on commit d6c09fb

Please sign in to comment.