diff --git a/.github/workflows/releases.yml b/.github/workflows/releases.yml index b8b100f..e83c9a4 100644 --- a/.github/workflows/releases.yml +++ b/.github/workflows/releases.yml @@ -55,7 +55,7 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./main.js + asset_path: ./dist/main.js asset_name: main.js asset_content_type: text/javascript - name: Upload manifest.json @@ -67,4 +67,4 @@ jobs: upload_url: ${{ steps.create_release.outputs.upload_url }} asset_path: ./manifest.json asset_name: manifest.json - asset_content_type: application/json \ No newline at end of file + asset_content_type: application/json diff --git a/.gitignore b/.gitignore index e7b7242..cba5115 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ package-lock.json # build main.js -*.js.map \ No newline at end of file +*.js.map +dist diff --git a/main.ts b/main.ts deleted file mode 100644 index 4e55199..0000000 --- a/main.ts +++ /dev/null @@ -1,81 +0,0 @@ -import { App, Plugin, PluginSettingTab, Setting, MarkdownView } from 'obsidian'; -import { format } from './util'; - -export default class Pangu extends Plugin { - format(cm: CodeMirror.Editor): void { - let cursor = cm.getCursor(); - let cursorContent = cm.getRange({ ...cursor, ch: 0 }, cursor); - const { top } = cm.getScrollInfo(); - - cursorContent = format(cursorContent); - let content = cm.getValue().trim(); - content = format(content) - - cm.setValue(content); - cm.scrollTo(null, top); - - // 保持光标格式化后不变 - const newDocLine = cm.getLine(cursor.line); - try { - cursor = { - ...cursor, - ch: newDocLine.indexOf(cursorContent) + cursorContent.length, - }; - } catch (error) { } - - cm.setCursor(cursor); - } - - async onload() { - this.addCommand({ - id: 'pangu-format', - name: '为中英文字符间自动加入空格', - callback: () => { - const activeLeafView = - this.app.workspace.getActiveViewOfType(MarkdownView); - if (activeLeafView) { - // @ts-ignore - this.format(activeLeafView?.sourceMode?.cmEditor); - } - }, - hotkeys: [ - { - modifiers: ['Mod', 'Shift'], - key: 's', - }, - { - modifiers: ['Ctrl', 'Shift'], - key: 's', - }, - ], - }); - - this.addSettingTab(new PanguSettingTab(this.app, this)); - } - - onunload() { - console.log('unloading plugin'); - } - - async loadSettings() { } -} - -class PanguSettingTab extends PluginSettingTab { - plugin: Pangu; - - constructor(app: App, plugin: Pangu) { - super(app, plugin); - this.plugin = plugin; - } - - display(): void { - let { containerEl } = this; - containerEl.empty(); - containerEl.createEl('h2', { text: 'Pangu 使用说明' }); - new Setting(containerEl) - .setName('') - .setDesc( - '默认快捷键为:Mac - Command + Shift + S,Windows - Shift + Ctrl + S。当然,您可以到「设置 - 快捷键」里进行更改。' - ); - } -} diff --git a/rollup.config.js b/rollup.config.js index f7e1b48..b9f0f39 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,15 +1,15 @@ -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: false, - format: 'cjs', - exports: 'default', - }, - external: ['obsidian'], - plugins: [typescript(), nodeResolve({ browser: true }), commonjs()], -}; +import typescript from "@rollup/plugin-typescript"; +import { nodeResolve } from "@rollup/plugin-node-resolve"; +import commonjs from "@rollup/plugin-commonjs"; + +export default { + input: "src/main.ts", + output: { + dir: "dist/", + sourcemap: false, + format: "cjs", + exports: "default", + }, + external: ["obsidian"], + plugins: [typescript(), nodeResolve({ browser: true }), commonjs()], +}; diff --git a/src/main.ts b/src/main.ts new file mode 100644 index 0000000..8d850b3 --- /dev/null +++ b/src/main.ts @@ -0,0 +1,115 @@ +import { App, MarkdownView, Plugin, PluginSettingTab, Setting } from "obsidian"; +import { DEFAULT_SETTINGS, format, IPanGuSetting } from "./util"; + +export default class Pangu extends Plugin { + settings: IPanGuSetting = DEFAULT_SETTINGS; + + format(cm: CodeMirror.Editor): void { + let cursor = cm.getCursor(); + let cursorContent = cm.getRange({ ...cursor, ch: 0 }, cursor); + const { top } = cm.getScrollInfo(); + + cursorContent = format(cursorContent); + let content = cm.getValue().trim(); + content = format(content); + + cm.setValue(content); + cm.scrollTo(null, top); + + // 保持光标格式化后不变 + const newDocLine = cm.getLine(cursor.line); + try { + cursor = { + ...cursor, + ch: newDocLine.indexOf(cursorContent) + cursorContent.length, + }; + } catch (error) {} + + cm.setCursor(cursor); + } + + async onload() { + this.addCommand({ + id: "pangu-format", + name: "为中英文字符间自动加入空格", + callback: () => { + const activeLeafView = + this.app.workspace.getActiveViewOfType(MarkdownView); + if (activeLeafView) { + // @ts-ignore + this.format(activeLeafView?.sourceMode?.cmEditor); + } + }, + hotkeys: [ + { + modifiers: ["Mod", "Shift"], + key: "s", + }, + { + modifiers: ["Ctrl", "Shift"], + key: "s", + }, + ], + }); + + 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); + } +} + +class PanguSettingTab extends PluginSettingTab { + plugin: Pangu; + + constructor(app: App, plugin: Pangu) { + super(app, plugin); + this.plugin = plugin; + } + + display(): void { + let { containerEl } = this; + containerEl.empty(); + containerEl.createEl("h2", { text: "Pangu 使用说明" }); + new Setting(containerEl) + .setName("快速开始") + .setDesc( + "默认快捷键为:Mac - Command + Shift + S,Windows - Shift + Ctrl + S。当然,您可以到「设置 - 快捷键」里进行更改。" + ); + + new Setting(containerEl) + .setName("缩进宽度") + .setDesc("指定格式化时,缩进所占空格数") + .addDropdown((dropdown) => { + dropdown + .addOption("2", "2个空格") + .addOption("4", "4个空格") + .setValue(this.plugin.settings.tabWidth) + .onChange(async (value) => { + this.plugin.settings.tabWidth = value; + await this.plugin.saveSettings(); + }); + }); + + new Setting(containerEl) + .setName("格式化内嵌代码") + .setDesc("指定格式化时,是否格式化文档中的内嵌代码") + .addToggle((toggle) => + toggle + .setValue(this.plugin.settings.embeddedLanguageFormatting) + .onChange(async (value) => { + this.plugin.settings.embeddedLanguageFormatting = value; + await this.plugin.saveSettings(); + }) + ); + } +} diff --git a/src/util.ts b/src/util.ts new file mode 100644 index 0000000..e5e4c74 --- /dev/null +++ b/src/util.ts @@ -0,0 +1,33 @@ +// @ts-ignore +import prettier from "prettier/esm/standalone"; +// @ts-ignore +import markdownParser from "prettier/esm/parser-markdown"; + +export interface IPanGuSetting { + tabWidth: string; + embeddedLanguageFormatting: boolean; +} + +export const DEFAULT_SETTINGS: IPanGuSetting = { + tabWidth: "2", + embeddedLanguageFormatting: true, +}; + +function parseOptions(options: IPanGuSetting): any { + const { tabWidth, embeddedLanguageFormatting } = options; + + return { + tabWidth: +tabWidth, + embeddedLanguageFormatting: embeddedLanguageFormatting ? "auto" : "off", + }; +} +export function format( + content: string, + options: IPanGuSetting = DEFAULT_SETTINGS +): string { + return prettier.format(content, { + parser: "markdown", + plugins: [markdownParser], + ...parseOptions(options), + }); +} diff --git a/util.ts b/util.ts deleted file mode 100644 index 959b214..0000000 --- a/util.ts +++ /dev/null @@ -1,11 +0,0 @@ -// @ts-ignore -import prettier from 'prettier/esm/standalone'; -// @ts-ignore -import markdownParser from 'prettier/esm/parser-markdown'; - -export function format(content: string): string { - return prettier.format(content, { - parser: 'markdown', - plugins: [markdownParser] - }); -} \ No newline at end of file