mirror of
https://github.com/purocean/yn.git
synced 2026-09-24 15:46:02 +08:00
feat: 切换待办状态支持快捷键
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
Reference in New Issue
Block a user