Skip to content

Commit

Permalink
Merge pull request #11 from b-yp/dev
Browse files Browse the repository at this point in the history
feat: Added support for shortcut keys and introduced notifications
  • Loading branch information
b-yp committed Jan 16, 2024
2 parents ca91c6d + 83446aa commit 0104226
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@
> - https://eddy.lu/posts/pangu/
> - https://blog.pigfarm.top/2022/10/11/pan-gu-zhi-bai/
- ## 使用

- Block
- 使用斜杠命令,输入:`📄 Pangu Format Block`
- 使用块右键菜单,点击:`📄 Pangu Format Block`
- Page
- 使用斜杠命令,输入:`📄 Pangu Format Page`
- 使用页面右键菜单,点击:`📄 Pangu Format Page`
- 使用快捷键,默认:`shift + alt + f`

- ## 演示

![demo](./loseq-pangu-demo.gif)

- ## 鸣谢

- https://github.com/huacnlee/autocorrect
- 此插件是基于 AutoCorrect 做格式化的
- (pangu.js 不支持 MarkDown)
- 此插件是基于 AutoCorrect 做格式化的 (pangu.js 不支持 MarkDown)

- ## 许可证
- [MIT](https://choosealicense.com/licenses/mit/)
36 changes: 29 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import init, { format } from 'autocorrect-wasm';
import { v4 as uuidv4 } from 'uuid'

import { deepFirstTraversal } from './utils'
import { logseq as PL } from "../package.json";
import { settings } from './settings'
import { logseq as PL } from '../package.json'

const pluginId = PL.id;

Expand All @@ -14,13 +15,15 @@ const formatPage = async (e: IHookEvent) => {
const tree = await logseq.Editor.getPageBlocksTree(e.page)
if (!tree.length) return

deepFirstTraversal(tree, (block) => {
await deepFirstTraversal(tree, (block) => {
if (!block.content) return
formatBlock(block)
formatBlock(false, block)
})

logseq.UI.showMsg('Format page successful 🎉')
}

const formatBlock = async (b?: BlockEntity | IHookEvent) => {
const formatBlock = async (isToast: boolean, b?: BlockEntity | IHookEvent) => {
const block = await logseq.Editor.getBlock(b?.uuid)
if (!block) return

Expand Down Expand Up @@ -57,16 +60,35 @@ const formatBlock = async (b?: BlockEntity | IHookEvent) => {
})

logseq.Editor.updateBlock(block.uuid, formattedContent)

if (!isToast) return

logseq.UI.showMsg('Format block successful 🎉')
}

const main = () => {
console.info(`#${pluginId}: MAIN`);

logseq.Editor.registerSlashCommand('📄 Pangu Format', formatBlock)
logseq.useSettingsSchema(settings);
const shortcutKey = logseq.settings?.['shortcutKey']

logseq.Editor.registerSlashCommand('📄 Pangu Format Block', e => formatBlock(true, e))

logseq.Editor.registerBlockContextMenuItem('📄 Pangu Format Block', e => formatBlock(true, e))

logseq.Editor.registerSlashCommand('📄 Pangu Format Page', formatPage)

logseq.Editor.registerBlockContextMenuItem('📄 Pangu Format', formatBlock)
logseq.App.registerPageMenuItem('📄 Pangu Format Page', formatPage)

logseq.App.registerPageMenuItem('📄 Pangu Format', formatPage)
logseq.App.registerCommandShortcut(
{
binding: shortcutKey,
},
async () => {
const page = await logseq.Editor.getCurrentPage()
formatPage({ page: page?.name })
}
)
}

logseq.ready(main).catch(console.error);
11 changes: 11 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { SettingSchemaDesc } from "@logseq/libs/dist/LSPlugin";

export const settings: SettingSchemaDesc[] = [
{
key: "shortcutKey",
title: "Shortcut keys",
description: "Shortcut key for format page.",
type: "string",
default: "shift+alt+f",
},
];
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { BlockEntity } from "@logseq/libs/dist/LSPlugin";
* @param arr BlockEntity[]
* @param fn (block: BlockEntity) => void
*/
export const deepFirstTraversal = (arr: BlockEntity[], fn: (block: BlockEntity) => void) => {
arr.forEach(obj => {
export const deepFirstTraversal = async (arr: BlockEntity[], fn: (block: BlockEntity) => void) => {
for (const obj of arr) {
// console.log(obj.id); // 输出当前节点的 id
if (obj) {
fn(obj)
}
if (obj.children && obj.children.length > 0) {
deepFirstTraversal(obj.children as BlockEntity[], fn); // 递归遍历子节点
await deepFirstTraversal(obj.children as BlockEntity[], fn); // 递归遍历子节点
}
});
};
}

0 comments on commit 0104226

Please sign in to comment.