diff --git a/help/SHORTCUTS.md b/help/SHORTCUTS.md index 1d65eb0b..67cb3452 100644 --- a/help/SHORTCUTS.md +++ b/help/SHORTCUTS.md @@ -22,6 +22,7 @@ 当前行下移 | Ctrl + Shift + Down | Cmd + Shift + Down 重复当前行 | Ctrl + Shift + D | Cmd + Shift + D 插入当前日期 | Shift + Alt + D | Shift + Option + D +切换待办状态 | Alt + O | Alt + O 插入当前时间 | Shift + Alt + T | Shift + Option + T 插入文件附件 | Shift + Alt + F | Shift + Option + F 在内置终端里面运行选中内容 | Shift + Alt + R | Shift + Option + R diff --git a/src/renderer/plugins/switch-todo.ts b/src/renderer/plugins/switch-todo.ts index 125ac7c3..ca493a81 100644 --- a/src/renderer/plugins/switch-todo.ts +++ b/src/renderer/plugins/switch-todo.ts @@ -1,10 +1,35 @@ -import dayjs from 'dayjs' -import { getLineContent, replaceLine } from '@fe/services/editor' -import { Plugin } from '@fe/context' +import type { BuildInActionName } from '@fe/types' +import type { Plugin } from '@fe/context' export default { name: 'switch-todo', register: (ctx) => { + const actionName: BuildInActionName = 'plugin.switch-todo.switch' + + ctx.action.registerAction({ + name: actionName, + keys: [ctx.command.Alt, 'o'], + handler: (line?: number, checked?: boolean) => { + if (!line) { + line = ctx.editor.getSelectionInfo().line + } + + const lineText = ctx.editor.getLineContent(line) + + if (typeof checked !== 'boolean') { + checked = !lineText.includes(' [x] ') + } + + const value = checked + ? lineText.replace('[ ]', `[x] ~~${ctx.lib.dayjs().format('YYYY-MM-DD HH:mm')}~~`) + : lineText.replace(/(\[x\] ~~[\d-: ]+~~|\[x\])/, '[ ]') + + if (value !== lineText) { + ctx.editor.replaceLine(line, value) + } + } + }) + ctx.registerHook('ON_VIEW_ELEMENT_CLICK', async (e: MouseEvent) => { const target = e.target as HTMLElement @@ -14,18 +39,10 @@ export default { return true } - function switchTodo (line: number, checked: boolean) { - const lineText = getLineContent(line) - - const value = checked - ? lineText.replace('[ ]', `[x] ~~${dayjs().format('YYYY-MM-DD HH:mm')}~~`) - : lineText.replace(/(\[x\] ~~[\d-: ]+~~|\[x\])/, '[ ]') - - replaceLine(line, value) - } - if (target.tagName === 'INPUT' && target.parentElement!.classList.contains('source-line')) { - switchTodo(parseInt(target.parentElement!.dataset.sourceLine || '0'), (target as HTMLInputElement).checked) + const line = parseInt(target.parentElement!.dataset.sourceLine || '0') + const checked = (target as HTMLInputElement).checked + ctx.action.getActionHandler(actionName)(line, checked) return preventEvent() } diff --git a/src/renderer/types.ts b/src/renderer/types.ts index 2342e0bc..c7957c6f 100644 --- a/src/renderer/types.ts +++ b/src/renderer/types.ts @@ -153,3 +153,4 @@ export type BuildInActionName = 'view.refresh' | 'plugin.status-bar-help.show-plugin' | 'plugin.transform-img-link.all' | 'plugin.transform-img-link.single-by-click' + | 'plugin.switch-todo.switch'