feat: 切换待办状态支持快捷键

This commit is contained in:
purocean
2021-11-03 15:00:16 +08:00
parent 0c049430b5
commit 51d721488d
3 changed files with 33 additions and 14 deletions
+1
View File
@@ -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
+31 -14
View File
@@ -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()
}
+1
View File
@@ -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'