Skip to content

Commit

Permalink
fix: fix status bar keyboard shortcuts label display
Browse files Browse the repository at this point in the history
  • Loading branch information
purocean committed Aug 5, 2023
1 parent 31206bc commit 9670d26
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 45 deletions.
16 changes: 2 additions & 14 deletions src/renderer/components/KeyboardShortcuts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ import { isMacOS, isOtherOS, isWindows } from '@fe/support/env'
import { useModal } from '@fe/support/ui/modal'
import { getSetting, setSetting } from '@fe/services/setting'
import { getCurrentLanguage, useI18n } from '@fe/services/i18n'
import { whenEditorReady } from '@fe/services/editor'
import { lookupKeybindingKeys, whenEditorReady } from '@fe/services/editor'
import { getLogger } from '@fe/utils'
import type { Command, Keybinding } from '@fe/types'
Expand Down Expand Up @@ -202,19 +202,7 @@ async function refresh () {
} else if (tab.value === 'editor') {
const { editor } = await whenEditorReady()
commands.value = (editor as any).getActions().map((x: any) => {
const keybinding = (editor as any)._standaloneKeybindingService.lookupKeybinding(x.id)
let keys: string[] | null = null
if (keybinding) {
const electronAccelerator = keybinding.getElectronAccelerator()
const userSettingsLabel = keybinding.getUserSettingsLabel()
if (electronAccelerator) {
keys = electronAccelerator.split('+')
} else {
keys = userSettingsLabel?.split(' ')
}
}
const keys = lookupKeybindingKeys(x.id)
return {
type: 'editor' as Tab,
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/plugins/custom-keybindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export default {
when = originalKeybinding.when?.serialize()
const originMonacoKeys = encodeMonacoChords(monaco, originalKeybinding.resolvedKeybinding._chords)
// remove original keybinding
newKeybindings.push({ keybinding: originMonacoKeys, command: null, when, })
newKeybindings.push({ keybinding: originMonacoKeys, command: `-${keybinding.command}`, when })
}

const monacoKeys = resolveKeys(monaco, keybinding.keys)
Expand Down
29 changes: 17 additions & 12 deletions src/renderer/plugins/editor-attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,14 @@ function addDocument (doc: Doc) {
export default {
name: 'editor-attachment',
register: (ctx) => {
const idAddImage = 'plugin.editor.add-image'
const idAddFile = 'plugin.editor.add-file'
const idLinkDoc = 'plugin.editor.link-doc'
const idLinkFile = 'plugin.editor.link-file'

whenEditorReady().then(({ editor, monaco }) => {
editor.addAction({
id: 'plugin.editor.add-image',
id: idAddImage,
contextMenuGroupId: 'modification',
label: t('add-image'),
keybindings: [
Expand All @@ -86,7 +91,7 @@ export default {
run: () => addAttachment(true),
})
editor.addAction({
id: 'plugin.editor.add-file',
id: idAddFile,
contextMenuGroupId: 'modification',
label: t('editor.context-menu.add-attachment'),
keybindings: [
Expand All @@ -95,7 +100,7 @@ export default {
run: () => addAttachment(false),
})
editor.addAction({
id: 'plugin.editor.link-doc',
id: idLinkDoc,
contextMenuGroupId: 'modification',
label: t('editor.context-menu.link-doc'),
keybindings: [
Expand All @@ -104,7 +109,7 @@ export default {
run: () => getActionHandler('filter.choose-document')().then(addDocument),
})
editor.addAction({
id: 'plugin.editor.link-file',
id: idLinkFile,
contextMenuGroupId: 'modification',
label: t('editor.context-menu.link-file'),
keybindings: [
Expand All @@ -117,31 +122,31 @@ export default {
ctx.statusBar.tapMenus(menus => {
menus['status-bar-insert']?.list?.push(
{
id: 'plugin.editor.add-image',
id: idAddImage,
type: 'normal',
title: ctx.i18n.t('add-image'),
subTitle: ctx.command.getKeysLabel([ctx.command.Alt, 'i']),
subTitle: ctx.command.getKeysLabel(ctx.editor.lookupKeybindingKeys(idAddImage) || []),
onClick: () => addAttachment(true),
},
{
id: 'plugin.editor.add-file',
id: idAddFile,
type: 'normal',
title: ctx.i18n.t('editor.context-menu.add-attachment'),
subTitle: ctx.command.getKeysLabel([ctx.command.Alt, 'f']),
subTitle: ctx.command.getKeysLabel(ctx.editor.lookupKeybindingKeys(idAddFile) || []),
onClick: () => addAttachment(false),
},
{
id: 'plugin.editor.link-doc',
id: idLinkDoc,
type: 'normal',
title: ctx.i18n.t('editor.context-menu.link-doc'),
subTitle: ctx.command.getKeysLabel([ctx.command.Alt, 'd']),
subTitle: ctx.command.getKeysLabel(ctx.editor.lookupKeybindingKeys(idLinkDoc) || []),
onClick: () => getActionHandler('filter.choose-document')().then(addDocument),
},
{
id: 'plugin.editor.link-file',
id: idLinkFile,
type: 'normal',
title: ctx.i18n.t('editor.context-menu.link-file'),
subTitle: ctx.command.getKeysLabel([ctx.command.Alt, ctx.command.Shift, 'f']),
subTitle: ctx.command.getKeysLabel(ctx.editor.lookupKeybindingKeys(idLinkFile) || []),
onClick: () => linkFile(),
},
{ type: 'separator' },
Expand Down
59 changes: 41 additions & 18 deletions src/renderer/plugins/editor-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,18 @@ export default {
}
}

const idInsertTime = 'plugin.editor.insert-time'
const idInsertDate = 'plugin.editor.insert-date'
const idRevealLineInPreview = 'plugin.editor.reveal-line-in-preview'
const idForceInsertNewLine = 'plugin.editor.force-insert-new-line'
const idForceInsertIndent = 'plugin.editor.force-insert-indent'

whenEditorReady().then(({ editor, monaco }) => {
const KM = monaco.KeyMod
const KC = monaco.KeyCode

editor.addAction({
id: 'plugin.editor.insert-date',
id: idInsertDate,
label: t('editor.context-menu.insert-date'),
contextMenuGroupId: 'modification',
keybindings: [
Expand All @@ -124,7 +130,7 @@ export default {
})

editor.addAction({
id: 'plugin.editor.insert-time',
id: idInsertTime,
label: t('editor.context-menu.insert-time'),
contextMenuGroupId: 'modification',
keybindings: [
Expand All @@ -134,33 +140,50 @@ export default {
})

editor.addAction({
id: 'plugin.editor.reveal-line-in-preview',
id: idRevealLineInPreview,
label: t('editor.context-menu.reveal-line-in-preview'),
contextMenuGroupId: 'other',
keybindings: [KM.Alt | KC.KeyL],
run: revealLineInPreview
})

editor.addCommand(KM.Alt | KC.Enter, () => {
insert(editor.getModel()!.getEOL())
editor.addAction({
id: idForceInsertNewLine,
label: t('editor.context-menu.force-insert-new-line'),
contextMenuGroupId: 'modification',
keybindings: [KM.Alt | KC.Enter],
run: () => {
insert(editor.getModel()!.getEOL())
}
})

editor.addCommand(KM.Shift | KC.Enter, () => {
insert(getOneIndent())
editor.addAction({
id: idForceInsertIndent,
label: t('editor.context-menu.force-insert-indent'),
contextMenuGroupId: 'modification',
keybindings: [KM.Shift | KC.Enter],
run: () => {
insert(getOneIndent())
}
})

editor.onDidChangeCursorPosition(e => {
processCursorChange(e.source, e.position)
e.secondaryPositions.forEach(processCursorChange.bind(null, e.source))
})

editor.addCommand(KM.chord(KM.CtrlCmd | KC.KeyK, KM.CtrlCmd | KC.KeyU), () => {
editor.getAction('editor.action.transformToUppercase')?.run()
})

editor.addCommand(KM.chord(KM.CtrlCmd | KC.KeyK, KM.CtrlCmd | KC.KeyL), () => {
editor.getAction('editor.action.transformToLowercase')?.run()
})
monaco.editor.addKeybindingRules([
{
command: 'editor.action.transformToUppercase',
keybinding: KM.chord(KM.CtrlCmd | KC.KeyK, KM.CtrlCmd | KC.KeyU),
when: 'editorTextFocus'
},
{
command: 'editor.action.transformToLowercase',
keybinding: KM.chord(KM.CtrlCmd | KC.KeyK, KM.CtrlCmd | KC.KeyL),
when: 'editorTextFocus'
}
])

editor.onDidCompositionStart(() => {
ctx.store.commit('setInComposition', true)
Expand All @@ -174,17 +197,17 @@ export default {
ctx.statusBar.tapMenus(menus => {
menus['status-bar-insert']?.list?.push(
{
id: 'plugin.editor.insert-time',
id: idInsertTime,
type: 'normal',
title: ctx.i18n.t('editor.context-menu.insert-time'),
subTitle: ctx.command.getKeysLabel([ctx.command.Shift, ctx.command.Alt, 't']),
subTitle: ctx.command.getKeysLabel(ctx.editor.lookupKeybindingKeys(idInsertTime) || []),
onClick: insertTime,
},
{
id: 'plugin.editor.insert-date',
id: idInsertDate,
type: 'normal',
title: ctx.i18n.t('editor.context-menu.insert-date'),
subTitle: ctx.command.getKeysLabel([ctx.command.Shift, ctx.command.Alt, 'd']),
subTitle: ctx.command.getKeysLabel(ctx.editor.lookupKeybindingKeys(idInsertDate) || []),
onClick: insertDate,
},
)
Expand Down
1 change: 1 addition & 0 deletions src/renderer/plugins/markdown-code-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ export default {
label: ctx.i18n.t('code-run.run-in-xterm'),
contextMenuGroupId: 'other',
precondition: 'editorHasSelection',
keybindingContext: 'editorHasSelection',
keybindings: [
monaco.KeyMod.Shift | monaco.KeyMod.Alt | monaco.KeyCode.KeyR
],
Expand Down
24 changes: 24 additions & 0 deletions src/renderer/services/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,30 @@ export function whenEditorReady (): Promise<{ editor: typeof editor, monaco: typ
})
}

export function lookupKeybindingKeys (commandId: string): string[] | null {
if (!editor) {
return null
}

const service = (editor as any)._standaloneKeybindingService

const keybinding = service.lookupKeybinding(commandId) || service.lookupKeybinding(`vs.editor.ICodeEditor:1:${commandId}`)

let keys: string[] | null = null

if (keybinding) {
const electronAccelerator = keybinding.getElectronAccelerator()
const userSettingsLabel = keybinding.getUserSettingsLabel()
if (electronAccelerator) {
keys = electronAccelerator.split('+')
} else {
keys = userSettingsLabel?.split(' ')
}
}

return keys
}

/**
* Insert text at current cursor.
* @param text
Expand Down
2 changes: 2 additions & 0 deletions src/share/i18n/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ const data = {
'insert-date': 'Insert Today\'s Date',
'insert-time': 'Insert Current Time',
'reveal-line-in-preview': 'Reveal Line in Preview',
'force-insert-new-line': 'Force Insert New Line',
'force-insert-indent': 'Force Insert Indent',
},
'switch-editor': 'Switch Editor',
'default-editor': 'Default Editor',
Expand Down
2 changes: 2 additions & 0 deletions src/share/i18n/languages/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,8 @@ const data: BaseLanguage = {
'insert-date': '插入当前日期',
'insert-time': '插入当前时间',
'reveal-line-in-preview': '在预览中聚焦',
'force-insert-new-line': '强制插入新行',
'force-insert-indent': '强制插入缩进',
},
'switch-editor': '切换编辑器',
'default-editor': '默认编辑器',
Expand Down

0 comments on commit 9670d26

Please sign in to comment.