Skip to content

Commit

Permalink
fix: make hotkey configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
彧瑾 committed May 11, 2021
1 parent 0f246e7 commit 6a4889e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 46 deletions.
62 changes: 18 additions & 44 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import { App, Modal, Notice, Plugin, PluginSettingTab, Setting, MarkdownView } from 'obsidian';
import formatUtil from './formatUtil';

interface MyPluginSettings {
autoSpacing: boolean;
}

const DEFAULT_SETTINGS: MyPluginSettings = {
autoSpacing: true,
};

export default class MyPlugin extends Plugin {
settings: MyPluginSettings;

export default class Pangu extends Plugin {
format(cm: CodeMirror.Editor): void {
let cursor = cm.getCursor();
let cursorContent = cm.getRange({ ...cursor, ch: 0 }, cursor);
Expand All @@ -37,8 +27,6 @@ export default class MyPlugin extends Plugin {
}

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

this.addCommand({
id: 'pangu-format',
name: '为中英文字符间自动加入空格',
Expand All @@ -48,54 +36,40 @@ export default class MyPlugin extends Plugin {
this.format(activeLeafView.sourceMode.cmEditor);
}
},
hotkeys: [
{
modifiers: ['Mod', 'Shift'],
key: 's',
},
{
modifiers: ['Ctrl', 'Shift'],
key: 's',
},
],
});

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

this.addSettingTab(new SampleSettingTab(this.app, this));
this.addSettingTab(new PanguSettingTab(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);
}
async loadSettings() {}
}

class SampleSettingTab extends PluginSettingTab {
plugin: MyPlugin;
class PanguSettingTab extends PluginSettingTab {
plugin: Pangu;

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

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

new Setting(containerEl)
.setName('开启快捷键')
.setDesc('Mac: Command + Shift + S,Windows: Shift + Ctrl + S')
.addToggle((toggle: { setValue: (arg0: boolean) => void, onChange: (arg0: (value: any) => Promise<void>) => void }) => {
toggle.setValue(this.plugin.settings.autoSpacing);
toggle.onChange(async (value: boolean) => {
this.plugin.settings.autoSpacing = value;
await this.plugin.saveSettings();
new Notice('按 Command + R 或 F5 重新载入后生效。');
});
});
containerEl.createEl('h2', { text: 'Pangu 使用说明' });
new Setting(containerEl).setName('').setDesc('默认快捷键为:Mac - Command + Shift + S,Windows - Shift + Ctrl + S。当然,您可以到「设置 - 快捷键」里进行更改。');
}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-pangu",
"name": "盘古",
"version": "1.2.3",
"version": "1.2.4",
"minAppVersion": "0.9.12",
"description": "自动为中英文之间插入空格,排版强迫者的福音。",
"author": "Natumsol",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-pangu",
"version": "1.2.3",
"version": "1.2.4",
"description": "A small plugin aims to add space between Chinese Characters and English Alphabet, and it is a boon for typographically compulsive people. ",
"main": "main.js",
"scripts": {
Expand Down

0 comments on commit 6a4889e

Please sign in to comment.