vault backup: 2024-04-15 11:54:03
This commit is contained in:
240
.obsidian/plugins/open-vscode/main.js
vendored
240
.obsidian/plugins/open-vscode/main.js
vendored
@@ -3,235 +3,17 @@ 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: `
|
||||
var f=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var P=(a,l)=>{for(var e in l)f(a,e,{get:l[e],enumerable:!0})},T=(a,l,e,i)=>{if(l&&typeof l=="object"||typeof l=="function")for(let s of V(l))!x.call(a,s)&&s!==e&&f(a,s,{get:()=>l[s],enumerable:!(i=S(l,s))||i.enumerable});return a};var k=a=>T(f({},"__esModule",{value:!0}),a);var E={};P(E,{default:()=>v});module.exports=k(E);var c=require("obsidian");var r=require("obsidian"),p={ribbonIcon:!0,ribbonCommandUsesCode:!0,executeTemplate:'code "{{vaultpath}}" "{{vaultpath}}/{{filepath}}"',openFile:!0,workspacePath:"{{vaultpath}}",useUrlInsiders:!1},m=class extends r.PluginSettingTab{constructor(e,i){super(e,i);this.plugin=i}display(){let{containerEl:e}=this;e.empty(),e.createEl("h3",{text:"General settings"}),new r.Setting(e).setName("Display Ribbon Icon").setDesc("Toggle this OFF if you want to hide the Ribbon Icon.").addToggle(n=>n.setValue(this.plugin.settings.ribbonIcon).onChange(t=>{this.plugin.settings.ribbonIcon=t,this.plugin.saveSettings(),this.plugin.refreshIconRibbon()})),new r.Setting(e).setName("Ribbon opens via `code` command").setDesc("Toggle this OFF if you'd prefer that the Ribbon Icon opens VSCode via URL.").addToggle(n=>n.setValue(this.plugin.settings.ribbonCommandUsesCode).onChange(t=>{this.plugin.settings.ribbonCommandUsesCode=t,this.plugin.saveSettings()})),e.createEl("h3",{text:"Open via `code` CLI settings"}),new r.Setting(e).setName("Template for executing the `code` command").setDesc('You can use the following variables: `{{vaultpath}}` (absolute), `{{filepath}}` (relative), `{{folderpath}}` (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(n=>n.setPlaceholder(p.executeTemplate).setValue(this.plugin.settings.executeTemplate||p.executeTemplate).onChange(t=>{t=t.trim(),t===""&&(t=p.executeTemplate),this.plugin.settings.executeTemplate=t,this.plugin.saveData(this.plugin.settings)})),e.createEl("h3",{text:"Open via `vscode://` URL settings"});let s=e.createEl("p").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 = `
|
||||
<path
|
||||
fill="currentColor"
|
||||
d="M 96.457031 10.777344 L 75.875 0.875 C 73.492188 -0.273438 70.640625 0.210938 68.769531 2.082031 L 29.355469 38.042969 L 12.1875 25.007812 C 10.589844 23.796875 8.355469 23.894531 6.871094 25.246094 L 1.363281 30.253906 C 0.496094 31.042969 0 32.160156 0 33.335938 C -0.00390625 34.507812 0.492188 35.625 1.359375 36.417969 L 16.246094 50 L 1.359375 63.582031 C 0.492188 64.375 -0.00390625 65.492188 0 66.664062 C 0 67.839844 0.496094 68.957031 1.363281 69.746094 L 6.875 74.75 C 8.359375 76.101562 10.59375 76.199219 12.191406 74.988281 L 29.359375 61.953125 L 68.773438 97.914062 C 70.644531 99.785156 73.492188 100.269531 75.875 99.121094 L 96.464844 89.214844 C 98.628906 88.171875 100 85.984375 100 83.582031 L 100 16.414062 C 100 14.011719 98.625 11.820312 96.457031 10.777344 Z M 75.015625 72.699219 L 45.109375 50 L 75.015625 27.300781 Z M 75.015625 72.699219"
|
||||
/>
|
||||
`;
|
||||
(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);
|
||||
}
|
||||
`});s.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"})),s.appendText("."),new r.Setting(e).setName("Open current file").setDesc("Open the current file rather than the root of the vault.").addToggle(n=>n.setValue(this.plugin.settings.openFile||p.openFile).onChange(t=>{this.plugin.settings.openFile=t,this.plugin.saveData(this.plugin.settings)})),new r.Setting(e).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(n=>n.setPlaceholder(p.workspacePath).setValue(this.plugin.settings.workspacePath||p.workspacePath).onChange(t=>{t=t.trim(),t===""&&(t=p.workspacePath),this.plugin.settings.workspacePath=t,this.plugin.saveData(this.plugin.settings)}));let d=e.querySelector(".setting-item--vscode-workspacePath .setting-item-description");d.appendChild(createEl("a",{href:"https://code.visualstudio.com/docs/editor/workspaces#_multiroot-workspaces",text:"https://code.visualstudio.com/docs/editor/workspaces#_multiroot-workspaces"})),d.appendText("."),new r.Setting(e).setName("Open VSCode using a `vscode-insiders://` URL").addToggle(n=>{n.setValue(this.plugin.settings.useUrlInsiders).onChange(t=>{this.plugin.settings.useUrlInsiders=t,this.plugin.saveSettings()})})}};var I=`
|
||||
<svg role="img" viewBox="0 0 24 24"
|
||||
xmlns="http://www.w3.org/2000/svg">
|
||||
<title>Visual Studio Code</title>
|
||||
<path
|
||||
fill="currentColor"
|
||||
d="M23.15 2.587L18.21.21a1.494 1.494 0 0 0-1.705.29l-9.46 8.63-4.12-3.128a.999.999 0 0 0-1.276.057L.327 7.261A1 1 0 0 0 .326 8.74L3.899 12 .326 15.26a1 1 0 0 0 .001 1.479L1.65 17.94a.999.999 0 0 0 1.276.057l4.12-3.128 9.46 8.63a1.492 1.492 0 0 0 1.704.29l4.942-2.377A1.5 1.5 0 0 0 24 20.06V3.939a1.5 1.5 0 0 0-.85-1.352zm-5.146 14.861L10.826 12l7.178-5.448v10.896z"
|
||||
/>
|
||||
</svg>
|
||||
`;(0,c.addIcon)("vscode-logo",I);var h=!1,v=class extends c.Plugin{constructor(){super(...arguments);this.refreshIconRibbon=()=>{var e;(e=this.ribbonIcon)==null||e.remove(),this.settings.ribbonIcon&&(this.ribbonIcon=this.addRibbonIcon("vscode-logo","VSCode",()=>{let i=this.settings.ribbonCommandUsesCode?"openVSCode":"openVSCodeUrl";this[i]()}))}}async onload(){var e;console.log("Loading "+this.manifest.name+" plugin"),this.addSettingTab(new m(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)}),h=this.app.plugins.enabledPlugins.has("hot-reload")&&((e=this.app.plugins.plugins["hot-reload"])==null?void 0:e.enabledPlugins.has(this.manifest.id)),h&&(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 g,u,C;if(!(this.app.vault.adapter instanceof c.FileSystemAdapter))return;let{executeTemplate:e}=this.settings,i=this.app.vault.adapter.getBasePath(),s=this.app.workspace.getActiveFile(),d=(g=s==null?void 0:s.path)!=null?g:"",n=(C=(u=s==null?void 0:s.parent)==null?void 0:u.path)!=null?C:"",{exec:t}=require("child_process"),o=e.trim()===""?p.executeTemplate:e;o=b(o,"{{vaultpath}}",i),o=b(o,"{{filepath}}",d),o=b(o,"{{folderpath}}",n),h&&console.log("[openVSCode]",{command:o}),t(o,(w,y,R)=>{w&&console.error(`[openVSCode] exec error: ${w}`)})}async openVSCodeUrl(){var g;if(!(this.app.vault.adapter instanceof c.FileSystemAdapter))return;let{openFile:e,useUrlInsiders:i}=this.settings,s=this.app.vault.adapter.getBasePath(),d=this.app.workspace.getActiveFile(),n=(g=d==null?void 0:d.path)!=null?g:"";h&&console.log("[open-vscode]",{settings:this.settings,path:s,filePath:n});let o=`${i?"vscode-insiders://":"vscode://"}file/${s}`;if(e){o+=`/${n}`;let u=b(this.settings.workspacePath,"{{vaultpath}}",s);window.open(`vscode://file/${u}`),setTimeout(()=>{h&&console.log("[openVSCode]",{url:o}),window.open(o)},200)}else h&&console.log("[openVSCode]",{url:o}),window.open(o)}async loadSettings(){this.settings=Object.assign({},p,await this.loadData())}async saveSettings(){await this.saveData(this.settings)}async reload(){let e=this.manifest.id,i=this.app.plugins;await i.disablePlugin(e),await i.enablePlugin(e),console.log("[open-vscode] reloaded",this)}async resetSettings(){console.log("[open-vscode]",{old:this.settings,DEFAULT_SETTINGS:p}),this.settings=p,await this.saveData(this.settings)}};function U(a){return a.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function b(a,l,e){return a.replace(new RegExp(U(l),"g"),e)}
|
||||
|
Reference in New Issue
Block a user