/* THIS IS A GENERATED/BUNDLED FILE BY ESBUILD if you want to view the source, please visit the github repository of this plugin */ var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/main.ts var main_exports = {}; __export(main_exports, { default: () => OpenVSCode }); module.exports = __toCommonJS(main_exports); var import_obsidian2 = require("obsidian"); // src/settings.ts var import_obsidian = require("obsidian"); var DEFAULT_SETTINGS = { ribbonIcon: true, ribbonCommandUsesCode: true, executeTemplate: 'code "{{vaultpath}}" "{{vaultpath}}/{{filepath}}"', openFile: true, workspacePath: "{{vaultpath}}", useUrlInsiders: false }; var OpenVSCodeSettingsTab = class extends import_obsidian.PluginSettingTab { constructor(app, plugin) { super(app, plugin); this.plugin = plugin; } display() { const { containerEl } = this; containerEl.empty(); containerEl.createEl("h3", { text: "General settings" }); new import_obsidian.Setting(containerEl).setName("Display Ribbon Icon").setDesc("Toggle this OFF if you want to hide the Ribbon Icon.").addToggle((toggle) => toggle.setValue(this.plugin.settings.ribbonIcon).onChange((value) => { this.plugin.settings.ribbonIcon = value; this.plugin.saveSettings(); this.plugin.refreshIconRibbon(); })); new import_obsidian.Setting(containerEl).setName("Ribbon opens via `code` command").setDesc(`Toggle this OFF if you'd prefer that the Ribbon Icon opens VSCode via URL.`).addToggle((toggle) => toggle.setValue(this.plugin.settings.ribbonCommandUsesCode).onChange((value) => { this.plugin.settings.ribbonCommandUsesCode = value; this.plugin.saveSettings(); })); containerEl.createEl("h3", { text: "Open via `code` CLI settings" }); new import_obsidian.Setting(containerEl).setName("Template for executing the `code` command").setDesc('You can use the following variables: `{{vaultpath}}` (absolute), `{{filepath}}` (relative). Note that on MacOS, a full path to the VSCode executable is required (generally "/usr/local/bin/code"). Example: `/usr/local/bin/code "{{vaultpath}}" "{{vaultpath}}/{{filepath}}"`').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); })); containerEl.createEl("h3", { text: "Open via `vscode://` URL settings" }); const openViaUrlCaveat = containerEl.createEl("p"); const openViaUrlCaveatEm = openViaUrlCaveat.createEl("em", { text: ` \u26A0\uFE0F This setting is not recommended for Windows users due to UX issues caused by security enhancements in VSCode on Windows. More information: ` }); openViaUrlCaveatEm.appendChild(createEl("a", { text: "Open in VSCode Readme", href: "https://github.com/NomarCub/obsidian-open-vscode/blob/master/README.md#caveats-regarding-the-url-command-for-windows-users" })); openViaUrlCaveatEm.appendText("."); new import_obsidian.Setting(containerEl).setName("Open current file").setDesc("Open the current file rather than the root of the vault.").addToggle((toggle) => toggle.setValue(this.plugin.settings.openFile || DEFAULT_SETTINGS.openFile).onChange((value) => { this.plugin.settings.openFile = value; this.plugin.saveData(this.plugin.settings); })); new import_obsidian.Setting(containerEl).setName("Path to VSCode Workspace").setDesc('Defaults to the {{vaultpath}} template variable. You can set this to an absolute path to a ".code-workspace" file if you prefer to use a Multi Root workspace file: ').setClass("setting-item--vscode-workspacePath").addText((text) => text.setPlaceholder(DEFAULT_SETTINGS.workspacePath).setValue(this.plugin.settings.workspacePath || DEFAULT_SETTINGS.workspacePath).onChange((value) => { value = value.trim(); if (value === "") value = DEFAULT_SETTINGS.workspacePath; this.plugin.settings.workspacePath = value; this.plugin.saveData(this.plugin.settings); })); const workspacePathDescEl = containerEl.querySelector(".setting-item--vscode-workspacePath .setting-item-description"); workspacePathDescEl.appendChild(createEl("a", { href: "https://code.visualstudio.com/docs/editor/workspaces#_multiroot-workspaces", text: "https://code.visualstudio.com/docs/editor/workspaces#_multiroot-workspaces" })); workspacePathDescEl.appendText("."); new import_obsidian.Setting(containerEl).setName("Open VSCode using a `vscode-insiders://` URL").addToggle((toggle) => { toggle.setValue(this.plugin.settings.useUrlInsiders).onChange((value) => { this.plugin.settings.useUrlInsiders = value; this.plugin.saveSettings(); }); }); } }; // src/main.ts var svg = ` `; (0, import_obsidian2.addIcon)("vscode-logo", svg); var DEV = false; var OpenVSCode = class extends import_obsidian2.Plugin { constructor() { super(...arguments); this.refreshIconRibbon = () => { var _a; (_a = this.ribbonIcon) == null ? void 0 : _a.remove(); if (this.settings.ribbonIcon) { this.ribbonIcon = this.addRibbonIcon("vscode-logo", "VSCode", () => { const ribbonCommand = this.settings.ribbonCommandUsesCode ? "openVSCode" : "openVSCodeUrl"; this[ribbonCommand](); }); } }; } async onload() { console.log("Loading " + this.manifest.name + " plugin"); this.addSettingTab(new OpenVSCodeSettingsTab(this.app, this)); await this.loadSettings(); this.refreshIconRibbon(); this.addCommand({ id: "open-vscode", name: "Open as Visual Studio Code workspace", callback: this.openVSCode.bind(this) }); this.addCommand({ id: "open-vscode-via-url", name: "Open as Visual Studio Code workspace using a vscode:// URL", callback: this.openVSCodeUrl.bind(this) }); DEV = this.app.plugins.enabledPlugins.has("hot-reload") && this.app.plugins.plugins["hot-reload"].enabledPlugins.has(this.manifest.id); if (DEV) { this.addCommand({ id: "open-vscode-reload", name: "Reload the plugin in dev", callback: this.reload.bind(this) }); this.addCommand({ id: "open-vscode-reset-settings", name: "Reset plugins settings to default in dev", callback: this.resetSettings.bind(this) }); } } async openVSCode() { var _a; if (!(this.app.vault.adapter instanceof import_obsidian2.FileSystemAdapter)) { return; } const { executeTemplate } = this.settings; const path = this.app.vault.adapter.getBasePath(); const file = this.app.workspace.getActiveFile(); const filePath = (_a = file == null ? void 0 : file.path) != null ? _a : ""; const { exec } = require("child_process"); let command = executeTemplate.trim() === "" ? DEFAULT_SETTINGS.executeTemplate : executeTemplate; command = replaceAll(command, "{{vaultpath}}", path); command = replaceAll(command, "{{filepath}}", filePath); if (DEV) console.log("[openVSCode]", { command }); exec(command, (error, stdout, stderr) => { if (error) { console.error(`[openVSCode] exec error: ${error}`); } }); } async openVSCodeUrl() { var _a; if (!(this.app.vault.adapter instanceof import_obsidian2.FileSystemAdapter)) { return; } const { openFile, useUrlInsiders } = this.settings; const path = this.app.vault.adapter.getBasePath(); const file = this.app.workspace.getActiveFile(); const filePath = (_a = file == null ? void 0 : file.path) != null ? _a : ""; if (DEV) console.log("[open-vscode]", { settings: this.settings, path, filePath }); const protocol = useUrlInsiders ? "vscode-insiders://" : "vscode://"; let url = `${protocol}file/${path}`; if (openFile) { url += `/${filePath}`; const workspacePath = replaceAll(this.settings.workspacePath, "{{vaultpath}}", path); window.open(`vscode://file/${workspacePath}`); setTimeout(() => { if (DEV) console.log("[openVSCode]", { url }); window.open(url); }, 200); } else { if (DEV) console.log("[openVSCode]", { url }); window.open(url); } } async loadSettings() { this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); } async saveSettings() { await this.saveData(this.settings); } async reload() { const id = this.manifest.id; const plugins = this.app.plugins; await plugins.disablePlugin(id); await plugins.enablePlugin(id); console.log("[open-vscode] reloaded", this); } async resetSettings() { console.log("[open-vscode]", { old: this.settings, DEFAULT_SETTINGS }); this.settings = DEFAULT_SETTINGS; await this.saveData(this.settings); } }; function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); } function replaceAll(str, find, replace) { return str.replace(new RegExp(escapeRegExp(find), "g"), replace); }