generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
370 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 重新载入后生效。'); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +0,0 @@ | ||
/* Sets all the text color to red! */ | ||
body { | ||
color: red; | ||
} | ||
Oops, something went wrong.