Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
Natumsol committed Mar 6, 2021
1 parent e9f42d5 commit 71df45c
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 139 deletions.
39 changes: 7 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,13 @@
## Obsidian Sample Plugin
## Obsidian Pangu Plugin

This is a sample plugin for Obsidian (https://obsidian.md).
A small plugin aims to add space between Chinese Characters and English Alphabet, powered by [pangu.js](https://github.com/vinta/pangu.js).

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.
For Example:

```diff
- 大多数人在20到30岁就已经过完自己的一生;一过了这个年龄段,他们就变成自己的影子。
+ 大多数人在 20 到 30 岁就已经过完自己的一生;一过了这个年龄段,他们就变成自己的影子。
```
### How to use

- Clone this repo.
Expand All @@ -40,6 +18,3 @@ This sample plugin demonstrates some of the basic functionality the plugin API c

- 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
180 changes: 82 additions & 98 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,112 +1,96 @@
import { App, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { App, Modal, Notice, Plugin, PluginSettingTab, Setting, MarkdownView } from 'obsidian';
import { spacing } from 'pangu';

interface MyPluginSettings {
mySetting: string;
autoSpacing: boolean;
}

const DEFAULT_SETTINGS: MyPluginSettings = {
mySetting: 'default'
}
autoSpacing: true,
};

export default class MyPlugin extends Plugin {
settings: MyPluginSettings;

async onload() {
console.log('loading plugin');

await this.loadSettings();

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.registerCodeMirror((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');
}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

async saveSettings() {
await this.saveData(this.settings);
}
settings: MyPluginSettings;

format(cm: CodeMirror.Editor): void {
const cursor = cm.getCursor();
cm.setValue(spacing(cm.getValue()));
cm.setCursor(cursor);
}

async onload() {
await this.loadSettings();

this.addCommand({
id: 'pangu-format',
name: '为中英文字符间自动加入空格',
callback: () => {
const activeLeafView = this.app.workspace.activeLeaf.view;
if (activeLeafView instanceof MarkdownView) {
this.format(activeLeafView.sourceMode.cmEditor);
}
},
});

this.registerCodeMirror(cm => {
this.settings.autoSpacing &&
cm.addKeyMap({
'Ctrl-S': this.format,
'Cmd-S': this.format,
});
});

this.addSettingTab(new SampleSettingTab(this.app, this));
}

onunload() {
console.log('unloading plugin');
}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

async saveSettings() {
await this.saveData(this.settings);
}
}

class SampleModal extends Modal {
constructor(app: App) {
super(app);
}

onOpen() {
let {contentEl} = this;
contentEl.setText('Woah!');
}

onClose() {
let {contentEl} = this;
contentEl.empty();
}
constructor(app: App) {
super(app);
}

onOpen() {
let { contentEl } = this;
contentEl.setText('Woah!');
}

onClose() {
let { contentEl } = this;
contentEl.empty();
}
}

class SampleSettingTab extends PluginSettingTab {
plugin: MyPlugin;

constructor(app: App, plugin: MyPlugin) {
super(app, plugin);
this.plugin = plugin;
}

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(async (value) => {
console.log('Secret: ' + value);
this.plugin.settings.mySetting = value;
await this.plugin.saveSettings();
}));
}
plugin: MyPlugin;

constructor(app: App, plugin: MyPlugin) {
super(app, plugin);
this.plugin = plugin;
}

display(): void {
let { containerEl } = this;
containerEl.empty();

new Setting(containerEl).setName('自动格式化').addToggle(toggle => {
toggle.setValue(this.plugin.settings.autoSpacing);
toggle.onChange(async value => {
this.plugin.settings.autoSpacing = value;
await this.plugin.saveSettings();
new Notice('按 CMD + R 或 F5 重新载入后生效。');
});
});
}
}
10 changes: 5 additions & 5 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"id": "obsidian-sample-plugin",
"name": "Sample Plugin",
"id": "obsidian-pangu-plugin",
"name": "盘古",
"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",
"description": "自动为中英文之间插入空格,排版强迫者的福音。",
"author": "Natumsol",
"authorUrl": "https://github.com/natumsol",
"isDesktopOnly": false
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
"@rollup/plugin-node-resolve": "^9.0.0",
"@rollup/plugin-typescript": "^6.0.0",
"@types/node": "^14.14.2",
"@types/pangu": "^3.3.0",
"obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master",
"rollup": "^2.32.1",
"tslib": "^2.0.3",
"typescript": "^4.0.3"
},
"dependencies": {
"pangu": "^4.0.7"
}
}
4 changes: 0 additions & 4 deletions styles.css
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
/* Sets all the text color to red! */
body {
color: red;
}
Loading

0 comments on commit 71df45c

Please sign in to comment.