From f173f200ea0b8629cace1b8b3d9440d6113983eb Mon Sep 17 00:00:00 2001 From: Matthew Sessions Date: Tue, 15 Dec 2020 17:57:39 -0500 Subject: [PATCH] Initial commit --- .gitignore | 11 +++++++ README.md | 45 ++++++++++++++++++++++++++ main.ts | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ manifest.json | 10 ++++++ package.json | 23 ++++++++++++++ rollup.config.js | 19 +++++++++++ styles.css | 4 +++ tsconfig.json | 22 +++++++++++++ versions.json | 4 +++ 9 files changed, 221 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 main.ts create mode 100644 manifest.json create mode 100644 package.json create mode 100644 rollup.config.js create mode 100644 styles.css create mode 100644 tsconfig.json create mode 100644 versions.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e7b7242 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +# Intellij +*.iml +.idea + +# npm +node_modules +package-lock.json + +# build +main.js +*.js.map \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..da25203 --- /dev/null +++ b/README.md @@ -0,0 +1,45 @@ +## Obsidian Sample Plugin + +This is a sample plugin for Obsidian (https://obsidian.md). + +This project uses Typescript to provide type checking and documentation. +The repo depends on the latest plugin API (obsidian.d.ts) in Typescript Definition format, which contains TSDoc comments describing what it does. + +**Note:** The Obsidian API is still in early alpha and is subject to change at any time! + +This sample plugin demonstrates some of the basic functionality the plugin API can do. +- Changes the default font color to red using `styles.css`. +- Adds a ribbon icon, which shows a Notice when clicked. +- Adds a command "Open Sample Modal" which opens a Modal. +- Adds a plugin setting tab to the settings page. +- Registers a global click event and output 'click' to the console. +- Registers a global interval which logs 'setInterval' to the console. + + +### Releasing new releases + +- Update your `manifest.json` with your new version number, such as `1.0.1`, and the minimum Obsidian version required for your latest release. +- Update your `versions.json` file with `"new-plugin-version": "minimum-obsidian-version"` so older versions of Obsidian can download an older version of your plugin that's compatible. +- Create new GitHub release using your new version number as the "Tag version". Use the exact version number, don't include a prefix `v`. See here for an example: https://github.com/obsidianmd/obsidian-sample-plugin/releases +- Upload the files `manifest.json`, `main.js`, `styles.css` as binary attachments. +- Publish the release. + +### Adding your plugin to the community plugin list + +- Publish an initial version. +- Make sure you have a `README.md` file in the root of your repo. +- Make a pull request at https://github.com/obsidianmd/obsidian-releases to add your plugin. + +### How to use + +- Clone this repo. +- `npm i` or `yarn` to install dependencies +- `npm run dev` to start compilation in watch mode. + +### Manually installing the plugin + +- Copy over `main.js`, `styles.css`, `manifest.json` to your vault `VaultFolder/.obsidian/plugins/your-plugin-id/`. + +### API Documentation + +See https://github.com/obsidianmd/obsidian-api diff --git a/main.ts b/main.ts new file mode 100644 index 0000000..e17ffef --- /dev/null +++ b/main.ts @@ -0,0 +1,83 @@ +import { App, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian'; + +export default class MyPlugin extends Plugin { + onload() { + console.log('loading plugin'); + + this.addRibbonIcon('dice', 'Sample Plugin', () => { + new Notice('This is a notice!'); + }); + + this.addStatusBarItem().setText('Status Bar Text'); + + this.addCommand({ + id: 'open-sample-modal', + name: 'Open Sample Modal', + // callback: () => { + // console.log('Simple Callback'); + // }, + checkCallback: (checking: boolean) => { + let leaf = this.app.workspace.activeLeaf; + if (leaf) { + if (!checking) { + new SampleModal(this.app).open(); + } + return true; + } + return false; + } + }); + + this.addSettingTab(new SampleSettingTab(this.app, this)); + + this.registerEvent(this.app.on('codemirror', (cm: CodeMirror.Editor) => { + console.log('codemirror', cm); + })); + + this.registerDomEvent(document, 'click', (evt: MouseEvent) => { + console.log('click', evt); + }); + + this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000)); + } + + onunload() { + console.log('unloading plugin'); + } +} + +class SampleModal extends Modal { + constructor(app: App) { + super(app); + } + + onOpen() { + let {contentEl} = this; + contentEl.setText('Woah!'); + } + + onClose() { + let {contentEl} = this; + contentEl.empty(); + } +} + +class SampleSettingTab extends PluginSettingTab { + display(): void { + let {containerEl} = this; + + containerEl.empty(); + + containerEl.createEl('h2', {text: 'Settings for my awesome plugin.'}); + + new Setting(containerEl) + .setName('Setting #1') + .setDesc('It\'s a secret') + .addText(text => text.setPlaceholder('Enter your secret') + .setValue('') + .onChange((value) => { + console.log('Secret: ' + value); + })); + + } +} diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..4ca4889 --- /dev/null +++ b/manifest.json @@ -0,0 +1,10 @@ +{ + "id": "obsidian-sample-plugin", + "name": "Sample Plugin", + "version": "1.0.1", + "minAppVersion": "0.9.12", + "description": "This is a sample plugin for Obsidian. This plugin demonstrates some of the capabilities of the Obsidian API.", + "author": "Obsidian", + "authorUrl": "https://obsidian.md/about", + "isDesktopOnly": false +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..c0573df --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "obsidian-sample-plugin", + "version": "0.9.7", + "description": "This is a sample plugin for Obsidian (https://obsidian.md)", + "main": "main.js", + "scripts": { + "dev": "rollup --config rollup.config.js -w", + "build": "rollup --config rollup.config.js" + }, + "keywords": [], + "author": "", + "license": "MIT", + "devDependencies": { + "@rollup/plugin-commonjs": "^15.1.0", + "@rollup/plugin-node-resolve": "^9.0.0", + "@rollup/plugin-typescript": "^6.0.0", + "@types/node": "^14.14.2", + "obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master", + "rollup": "^2.32.1", + "tslib": "^2.0.3", + "typescript": "^4.0.3" + } +} diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..4f6107e --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,19 @@ +import typescript from '@rollup/plugin-typescript'; +import {nodeResolve} from '@rollup/plugin-node-resolve'; +import commonjs from '@rollup/plugin-commonjs'; + +export default { + input: 'main.ts', + output: { + dir: '.', + sourcemap: 'inline', + format: 'cjs', + exports: 'default' + }, + external: ['obsidian'], + plugins: [ + typescript(), + nodeResolve({browser: true}), + commonjs(), + ] +}; \ No newline at end of file diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..cfd0fd7 --- /dev/null +++ b/styles.css @@ -0,0 +1,4 @@ +/* Sets all the text color to red! */ +body { + color: red; +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..d148fe0 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "inlineSourceMap": true, + "inlineSources": true, + "module": "ESNext", + "target": "es5", + "allowJs": true, + "noImplicitAny": true, + "moduleResolution": "node", + "importHelpers": true, + "lib": [ + "dom", + "es5", + "scripthost", + "es2015" + ] + }, + "include": [ + "**/*.ts" + ] +} diff --git a/versions.json b/versions.json new file mode 100644 index 0000000..ba14785 --- /dev/null +++ b/versions.json @@ -0,0 +1,4 @@ +{ + "1.0.1": "0.9.12", + "1.0.0": "0.9.7" +}