Skip to content

Commit 6c23d45

Browse files
committed
Initial commit
0 parents commit 6c23d45

File tree

9 files changed

+276
-0
lines changed

9 files changed

+276
-0
lines changed

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Intellij
2+
*.iml
3+
.idea
4+
5+
# npm
6+
node_modules
7+
package-lock.json
8+
9+
# build
10+
main.js
11+
*.js.map
12+
13+
# obsidian
14+
data.json

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## Obsidian Sample Plugin
2+
3+
This is a sample plugin for Obsidian (https://obsidian.md).
4+
5+
This project uses Typescript to provide type checking and documentation.
6+
The repo depends on the latest plugin API (obsidian.d.ts) in Typescript Definition format, which contains TSDoc comments describing what it does.
7+
8+
**Note:** The Obsidian API is still in early alpha and is subject to change at any time!
9+
10+
This sample plugin demonstrates some of the basic functionality the plugin API can do.
11+
- Changes the default font color to red using `styles.css`.
12+
- Adds a ribbon icon, which shows a Notice when clicked.
13+
- Adds a command "Open Sample Modal" which opens a Modal.
14+
- Adds a plugin setting tab to the settings page.
15+
- Registers a global click event and output 'click' to the console.
16+
- Registers a global interval which logs 'setInterval' to the console.
17+
18+
### First time developing plugins?
19+
20+
Quick starting guide for new plugin devs:
21+
22+
- Make a copy of this repo as a template with the "Use this template" button (login to GitHub if you don't see it).
23+
- Clone your repo to a local development folder. For convenience, you can place this folder in your `.obsidian/plugins/your-plugin-name` folder.
24+
- Install NodeJS, then run `npm i` in the command line under your repo folder.
25+
- Run `npm run dev` to compile your plugin from `main.ts` to `main.js`.
26+
- Make changes to `main.ts` (or create new `.ts` files). Those changes should be automatically compiled into `main.js`.
27+
- Reload Obsidian to load the new version of your plugin.
28+
- Enable plugin in settings window.
29+
- For updates to the Obsidian API run `npm update` in the command line under your repo folder.
30+
31+
### Releasing new releases
32+
33+
- Update your `manifest.json` with your new version number, such as `1.0.1`, and the minimum Obsidian version required for your latest release.
34+
- 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.
35+
- 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
36+
- Upload the files `manifest.json`, `main.js`, `styles.css` as binary attachments.
37+
- Publish the release.
38+
39+
### Adding your plugin to the community plugin list
40+
41+
- Publish an initial version.
42+
- Make sure you have a `README.md` file in the root of your repo.
43+
- Make a pull request at https://github.com/obsidianmd/obsidian-releases to add your plugin.
44+
45+
### How to use
46+
47+
- Clone this repo.
48+
- `npm i` or `yarn` to install dependencies
49+
- `npm run dev` to start compilation in watch mode.
50+
51+
### Manually installing the plugin
52+
53+
- Copy over `main.js`, `styles.css`, `manifest.json` to your vault `VaultFolder/.obsidian/plugins/your-plugin-id/`.
54+
55+
### API Documentation
56+
57+
See https://github.com/obsidianmd/obsidian-api

main.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { App, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
2+
3+
interface MyPluginSettings {
4+
mySetting: string;
5+
}
6+
7+
const DEFAULT_SETTINGS: MyPluginSettings = {
8+
mySetting: 'default'
9+
}
10+
11+
export default class MyPlugin extends Plugin {
12+
settings: MyPluginSettings;
13+
14+
async onload() {
15+
console.log('loading plugin');
16+
17+
await this.loadSettings();
18+
19+
this.addRibbonIcon('dice', 'Sample Plugin', () => {
20+
new Notice('This is a notice!');
21+
});
22+
23+
this.addStatusBarItem().setText('Status Bar Text');
24+
25+
this.addCommand({
26+
id: 'open-sample-modal',
27+
name: 'Open Sample Modal',
28+
// callback: () => {
29+
// console.log('Simple Callback');
30+
// },
31+
checkCallback: (checking: boolean) => {
32+
let leaf = this.app.workspace.activeLeaf;
33+
if (leaf) {
34+
if (!checking) {
35+
new SampleModal(this.app).open();
36+
}
37+
return true;
38+
}
39+
return false;
40+
}
41+
});
42+
43+
this.addSettingTab(new SampleSettingTab(this.app, this));
44+
45+
this.registerCodeMirror((cm: CodeMirror.Editor) => {
46+
console.log('codemirror', cm);
47+
});
48+
49+
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
50+
console.log('click', evt);
51+
});
52+
53+
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
54+
}
55+
56+
onunload() {
57+
console.log('unloading plugin');
58+
}
59+
60+
async loadSettings() {
61+
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
62+
}
63+
64+
async saveSettings() {
65+
await this.saveData(this.settings);
66+
}
67+
}
68+
69+
class SampleModal extends Modal {
70+
constructor(app: App) {
71+
super(app);
72+
}
73+
74+
onOpen() {
75+
let {contentEl} = this;
76+
contentEl.setText('Woah!');
77+
}
78+
79+
onClose() {
80+
let {contentEl} = this;
81+
contentEl.empty();
82+
}
83+
}
84+
85+
class SampleSettingTab extends PluginSettingTab {
86+
plugin: MyPlugin;
87+
88+
constructor(app: App, plugin: MyPlugin) {
89+
super(app, plugin);
90+
this.plugin = plugin;
91+
}
92+
93+
display(): void {
94+
let {containerEl} = this;
95+
96+
containerEl.empty();
97+
98+
containerEl.createEl('h2', {text: 'Settings for my awesome plugin.'});
99+
100+
new Setting(containerEl)
101+
.setName('Setting #1')
102+
.setDesc('It\'s a secret')
103+
.addText(text => text
104+
.setPlaceholder('Enter your secret')
105+
.setValue('')
106+
.onChange(async (value) => {
107+
console.log('Secret: ' + value);
108+
this.plugin.settings.mySetting = value;
109+
await this.plugin.saveSettings();
110+
}));
111+
}
112+
}

manifest.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"id": "obsidian-sample-plugin",
3+
"name": "Sample Plugin",
4+
"version": "1.0.1",
5+
"minAppVersion": "0.9.12",
6+
"description": "This is a sample plugin for Obsidian. This plugin demonstrates some of the capabilities of the Obsidian API.",
7+
"author": "Obsidian",
8+
"authorUrl": "https://obsidian.md/about",
9+
"isDesktopOnly": false
10+
}

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "obsidian-sample-plugin",
3+
"version": "0.12.0",
4+
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
5+
"main": "main.js",
6+
"scripts": {
7+
"dev": "rollup --config rollup.config.js -w",
8+
"build": "rollup --config rollup.config.js --environment BUILD:production"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "MIT",
13+
"devDependencies": {
14+
"@rollup/plugin-commonjs": "^18.0.0",
15+
"@rollup/plugin-node-resolve": "^11.2.1",
16+
"@rollup/plugin-typescript": "^8.2.1",
17+
"@types/node": "^14.14.37",
18+
"obsidian": "^0.12.0",
19+
"rollup": "^2.32.1",
20+
"tslib": "^2.2.0",
21+
"typescript": "^4.2.4"
22+
}
23+
}

rollup.config.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import typescript from '@rollup/plugin-typescript';
2+
import {nodeResolve} from '@rollup/plugin-node-resolve';
3+
import commonjs from '@rollup/plugin-commonjs';
4+
5+
const isProd = (process.env.BUILD === 'production');
6+
7+
const banner =
8+
`/*
9+
THIS IS A GENERATED/BUNDLED FILE BY ROLLUP
10+
if you want to view the source visit the plugins github repository
11+
*/
12+
`;
13+
14+
export default {
15+
input: 'main.ts',
16+
output: {
17+
dir: '.',
18+
sourcemap: 'inline',
19+
sourcemapExcludeSources: isProd,
20+
format: 'cjs',
21+
exports: 'default',
22+
banner,
23+
},
24+
external: ['obsidian'],
25+
plugins: [
26+
typescript(),
27+
nodeResolve({browser: true}),
28+
commonjs(),
29+
]
30+
};

styles.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Sets all the text color to red! */
2+
body {
3+
color: red;
4+
}

tsconfig.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"inlineSourceMap": true,
5+
"inlineSources": true,
6+
"module": "ESNext",
7+
"target": "es6",
8+
"allowJs": true,
9+
"noImplicitAny": true,
10+
"moduleResolution": "node",
11+
"importHelpers": true,
12+
"lib": [
13+
"dom",
14+
"es5",
15+
"scripthost",
16+
"es2015"
17+
]
18+
},
19+
"include": [
20+
"**/*.ts"
21+
]
22+
}

versions.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"1.0.1": "0.9.12",
3+
"1.0.0": "0.9.7"
4+
}

0 commit comments

Comments
 (0)