diff --git a/.gitignore b/.gitignore index 2fd7c5e..9ea81ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,14 +1,14 @@ -# Intellij -*.iml -.idea - -# npm -node_modules -package-lock.json - -# build -main.js -*.js.map - -# obsidian -data.json +# Intellij +*.iml +.idea + +# npm +node_modules +package-lock.json + +# build +main.js +*.js.map + +# obsidian +data.json diff --git a/README.md b/README.md index b6659df..ff01494 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -## Obsidian JavaScript Init - -Run JavaScript when Obsidian loads, or any other time. - -### Manually installing the plugin - -- Copy over `main.js` and `manifest.json` to your vault `VaultFolder/.obsidian/plugins/obsidian-javascript-init/`. +## Obsidian JavaScript Init + +Run JavaScript when Obsidian loads, or any other time. + +### Manually installing the plugin + +- Copy over `main.js` and `manifest.json` to your vault `VaultFolder/.obsidian/plugins/obsidian-javascript-init/`. diff --git a/main.ts b/main.ts index 76f9d55..b3d4a3c 100644 --- a/main.ts +++ b/main.ts @@ -1,98 +1,98 @@ -import { App, Notice, Plugin, PluginSettingTab, Setting } from "obsidian"; - -interface JavaScriptInitPluginSettings { - code: string; -} - -const DEFAULT_SETTINGS: JavaScriptInitPluginSettings = { - code: "", -}; - -export default class JavaScriptInitPlugin extends Plugin { - settings: JavaScriptInitPluginSettings; - - runCode(code = this.settings.code) { - const source = String(code); - const appendedScript = document.createElement("script"); - appendedScript.textContent = source; - (document.head || document.documentElement).appendChild(appendedScript); - } - - async onload() { - console.log("Loading Custom JavaScript plugin ..."); - - await this.loadSettings(); - - this.addRibbonIcon("dice", "Custom JavaScript Plugin", () => { - this.runCode(); - }); - - this.addCommand({ - id: "run-custom-javascript", - name: "Run Custom JavaScript", - callback: () => { - this.runCode(); - }, - }); - - this.addSettingTab(new JavaScriptInitSettingTab(this.app, this)); - - console.log(this.settings); - this - .runCode - // `document.addEventListener("DOMContentLoaded", () => { ${this.settings.code}; });` - (); - } - - onunload() { - console.log("Unloading Custom JavaScript plugin ..."); - } - - async loadSettings() { - this.settings = Object.assign( - {}, - DEFAULT_SETTINGS, - await this.loadData() - ); - } - - async saveSettings() { - await this.saveData(this.settings); - } -} - -class JavaScriptInitSettingTab extends PluginSettingTab { - plugin: JavaScriptInitPlugin; - - constructor(app: App, plugin: JavaScriptInitPlugin) { - super(app, plugin); - this.plugin = plugin; - } - - display(): void { - let { containerEl } = this; - - containerEl.empty(); - - containerEl.createEl("h2", { text: "Custom JavaScript Settings." }); - - new Setting(containerEl) - .setName("Code") - .setDesc("Custom code to execute on startup or on demand.") - .addTextArea((text) => { - const resultant = text - - .setPlaceholder("Sweet code here ...") - .setValue(this.plugin.settings.code || "") - .onChange(async (value) => { - this.plugin.settings.code = value; - await this.plugin.saveSettings(); - }); - - resultant.inputEl.style.fontFamily = "monospace"; - resultant.inputEl.style.width = "100%"; - - return resultant; - }); - } -} +import { App, Plugin, PluginSettingTab, Setting } from "obsidian"; + +type JavaScriptInitPluginSettings = { + code: string; +}; + +const DEFAULT_SETTINGS: JavaScriptInitPluginSettings = { + code: "", +}; + +export default class JavaScriptInitPlugin extends Plugin { + settings: JavaScriptInitPluginSettings; + + runCode(code = this.settings.code) { + const source = String(code); + const appendedScript = document.createElement("script"); + appendedScript.textContent = source; + (document.head || document.documentElement).appendChild(appendedScript); + } + + async onload() { + console.log("Loading JavaScript Init plugin ..."); + + await this.loadSettings(); + + this.addRibbonIcon("dice", "Run Init JavaScript", () => { + this.runCode(); + }); + + this.addCommand({ + id: "run-init-javascript", + name: "Run Init JavaScript", + callback: () => { + this.runCode(); + }, + }); + + this.addSettingTab(new JavaScriptInitSettingTab(this.app, this)); + + console.log(this.settings); + this + .runCode + // `document.addEventListener("DOMContentLoaded", () => { ${this.settings.code}; });` + (); + } + + onunload() { + console.log("Unloading JavaScript Init plugin ..."); + } + + async loadSettings() { + this.settings = Object.assign( + {}, + DEFAULT_SETTINGS, + await this.loadData() + ); + } + + async saveSettings() { + await this.saveData(this.settings); + } +} + +class JavaScriptInitSettingTab extends PluginSettingTab { + plugin: JavaScriptInitPlugin; + + constructor(app: App, plugin: JavaScriptInitPlugin) { + super(app, plugin); + this.plugin = plugin; + } + + display(): void { + let { containerEl } = this; + + containerEl.empty(); + + containerEl.createEl("h2", { text: "JavaScript Init Settings." }); + + new Setting(containerEl) + .setName("Code") + .setDesc("Custom code to execute on startup or on demand.") + .addTextArea((text) => { + const resultant = text + + .setPlaceholder("Sweet code here ...") + .setValue(this.plugin.settings.code || "") + .onChange(async (value) => { + this.plugin.settings.code = value; + await this.plugin.saveSettings(); + }); + + resultant.inputEl.style.fontFamily = "monospace"; + resultant.inputEl.style.width = "100%"; + + return resultant; + }); + } +} diff --git a/manifest.json b/manifest.json index cba3788..b17dc16 100644 --- a/manifest.json +++ b/manifest.json @@ -1,10 +1,10 @@ -{ - "id": "obsidian-javascript-init", - "name": "JavaScript Init", - "version": "1.0.0", - "minAppVersion": "0.9.12", - "description": "Run JavaScript when Obsidian loads, or any other time.", - "author": "ryanpcmcquen", - "authorUrl": "https://github.com/ryanpcmcquen", - "isDesktopOnly": false -} +{ + "id": "obsidian-javascript-init", + "name": "JavaScript Init", + "version": "1.0.0", + "minAppVersion": "0.9.12", + "description": "Run JavaScript when Obsidian loads, or any other time.", + "author": "ryanpcmcquen", + "authorUrl": "https://github.com/ryanpcmcquen", + "isDesktopOnly": false +} diff --git a/package.json b/package.json index d42e8ed..369ba88 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "keywords": [], "author": "", "license": "MPL-2.0", + "repository": "https://github.com/ryanpcmcquen/obsidian-javascript-init", "devDependencies": { "@rollup/plugin-commonjs": "^18.0.0", "@rollup/plugin-node-resolve": "^11.2.1", diff --git a/rollup.config.js b/rollup.config.js index 4c6541b..c47e576 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,25 +1,25 @@ -import typescript from "@rollup/plugin-typescript"; -import { nodeResolve } from "@rollup/plugin-node-resolve"; -import commonjs from "@rollup/plugin-commonjs"; - -const isProd = process.env.BUILD === "production"; - -const banner = `/* -THIS IS A GENERATED/BUNDLED FILE BY ROLLUP -if you want to view the source visit the plugins github repository -*/ -`; - -export default { - input: "main.ts", - output: { - dir: ".", - sourcemap: "inline", - sourcemapExcludeSources: isProd, - format: "cjs", - exports: "default", - banner, - }, - external: ["obsidian"], - plugins: [typescript(), nodeResolve({ browser: true }), commonjs()], -}; +import typescript from "@rollup/plugin-typescript"; +import { nodeResolve } from "@rollup/plugin-node-resolve"; +import commonjs from "@rollup/plugin-commonjs"; + +const isProd = process.env.BUILD === "production"; + +const banner = `/* +THIS IS A GENERATED/BUNDLED FILE BY ROLLUP +if you want to view the source visit the plugins github repository +*/ +`; + +export default { + input: "main.ts", + output: { + dir: ".", + sourcemap: "inline", + sourcemapExcludeSources: isProd, + format: "cjs", + exports: "default", + banner, + }, + external: ["obsidian"], + plugins: [typescript(), nodeResolve({ browser: true }), commonjs()], +}; diff --git a/tsconfig.json b/tsconfig.json index d5020dd..4666d8e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,15 +1,15 @@ -{ - "compilerOptions": { - "baseUrl": ".", - "inlineSourceMap": true, - "inlineSources": true, - "module": "ESNext", - "target": "es6", - "allowJs": true, - "noImplicitAny": true, - "moduleResolution": "node", - "importHelpers": true, - "lib": ["dom", "es5", "scripthost", "es2015"] - }, - "include": ["**/*.ts"] -} +{ + "compilerOptions": { + "baseUrl": ".", + "inlineSourceMap": true, + "inlineSources": true, + "module": "ESNext", + "target": "es6", + "allowJs": true, + "noImplicitAny": true, + "moduleResolution": "node", + "importHelpers": true, + "lib": ["dom", "es5", "scripthost", "es2015"] + }, + "include": ["**/*.ts"] +} diff --git a/versions.json b/versions.json index f5edab4..f0f8553 100644 --- a/versions.json +++ b/versions.json @@ -1,3 +1,3 @@ -{ - "1.0.0": "0.9.12" -} +{ + "1.0.0": "0.9.12" +}