Skip to content

Commit

Permalink
fix(md-editor): 快捷键兼容macOS键盘
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatZPP committed Dec 10, 2024
1 parent 69a8967 commit b8710c9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import cloneDeep from 'lodash/cloneDeep';
import { computed, nextTick, onMounted, reactive, Ref, ref, SetupContext, toRefs, watch, onBeforeUnmount } from 'vue';
import { debounce } from '../../../shared/utils';
import { EditorMdProps, Mode } from '../editor-md-types';
import { DEFAULT_TOOLBARS } from '../toolbar-config';
import { ALT_KEY, DEFAULT_TOOLBARS } from '../toolbar-config';
import { parseHTMLStringToDomList } from '../utils';
import { refreshEditorCursor, _enforceMaxLength } from './helper';
import { throttle } from 'lodash';
Expand Down Expand Up @@ -289,8 +289,8 @@ export function useEditorMd(props: EditorMdProps, ctx: SetupContext) {
const tempToolbars = { ...toolbars, ...customToolbars?.value };
for (const key of Object.keys(tempToolbars)) {
const toolbarItem = tempToolbars[key];
if (toolbarItem.shortKey && flatToolbarConfig.includes(toolbarItem.id)) {
shortKeys[toolbarItem.shortKey.replace(/\+/g, '-')] = toolbarItem.handler?.bind(null, editorIns, toolbarItem.params);
if (toolbarItem.shortKeyWithCode && flatToolbarConfig.includes(toolbarItem.id)) {
shortKeys[toolbarItem.shortKeyWithCode.replace(/\+/g, '-')] = toolbarItem.handler?.bind(null, editorIns, toolbarItem.params);
}
}

Expand Down Expand Up @@ -322,13 +322,17 @@ export function useEditorMd(props: EditorMdProps, ctx: SetupContext) {
if (e.ctrlKey) {
keyCombination += 'Ctrl-';
}
if (e.metaKey) {
keyCombination += '⌘-';
}
if (e.altKey) {
keyCombination += 'Alt-';
keyCombination += `${ALT_KEY}-`;
}
if (e.shiftKey) {
keyCombination += 'Shift-';
}
keyCombination += e.key.toUpperCase();

keyCombination += e.keyCode;
if (shortKeys[keyCombination] && typeof shortKeys[keyCombination] === 'function') {
e.preventDefault();
shortKeys[keyCombination]();
Expand Down
52 changes: 36 additions & 16 deletions packages/devui-vue/devui/editor-md/src/toolbar-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface IToolbarItemConfig {
template?: any;
component?: any;
shortKey?: string;
shortKeyWithCode?: string;
params?: { [key: string]: any };
handler?(editor?: any, params?: any): void;
}
Expand Down Expand Up @@ -277,93 +278,107 @@ class ToolBarHandler {
static color = (): void => {};
}

export const CTRL_KEY = navigator?.platform?.indexOf('Mac') !== -1 ? '⌘' : 'Ctrl';
export const ALT_KEY = navigator?.platform?.indexOf('Mac') !== -1 ? '⌥' : 'Alt';

export const DEFAULT_TOOLBARS: Record<string, IToolbarItemConfig> = {
undo: {
id: 'undo',
name: 'undo',
type: 'button',
icon: UNDO_ICON,
shortKey: 'Ctrl+Z',
shortKey: `${CTRL_KEY}+Z`,
shortKeyWithCode: `${CTRL_KEY}+90`,
handler: ToolBarHandler.undo,
},
redo: {
id: 'redo',
name: 'redo',
type: 'button',
icon: REDO_ICON,
shortKey: 'Ctrl+Y',
shortKey: `${CTRL_KEY}+Y`,
shortKeyWithCode: `${CTRL_KEY}+89`,
handler: ToolBarHandler.redo,
},
bold: {
id: 'bold',
name: 'bold',
type: 'button',
icon: BOLD_ICON,
shortKey: 'Ctrl+B',
shortKey: `${CTRL_KEY}+B`,
shortKeyWithCode: `${CTRL_KEY}+66`,
handler: ToolBarHandler.bold,
},
italic: {
id: 'italic',
name: 'italic',
type: 'button',
icon: ITALIC_ICON,
shortKey: 'Ctrl+I',
shortKey: `${CTRL_KEY}+I`,
shortKeyWithCode: `${CTRL_KEY}+73`,
handler: ToolBarHandler.italic,
},
strike: {
id: 'strike',
name: 'strike',
type: 'button',
icon: STRIKE_ICON,
shortKey: 'Ctrl+D',
shortKey: `${CTRL_KEY}+D`,
shortKeyWithCode: `${CTRL_KEY}+68`,
handler: ToolBarHandler.strike,
},
h1: {
id: 'h1',
name: 'h1',
type: 'button',
icon: H1_ICON,
shortKey: 'Ctrl+1',
shortKey: `${CTRL_KEY}+1`,
shortKeyWithCode: `${CTRL_KEY}+49`,
handler: ToolBarHandler.h1,
},
h2: {
id: 'h2',
name: 'h2',
type: 'button',
icon: H2_ICON,
shortKey: 'Ctrl+2',
shortKey: `${CTRL_KEY}+2`,
shortKeyWithCode: `${CTRL_KEY}+50`,
handler: ToolBarHandler.h2,
},
ul: {
id: 'ul',
name: 'unorderedlist',
type: 'button',
icon: LIST_UNORDERED_ICON,
shortKey: 'Ctrl+U',
shortKey: `${CTRL_KEY}+U`,
shortKeyWithCode: `${CTRL_KEY}+85`,
handler: ToolBarHandler.ul,
},
ol: {
id: 'ol',
name: 'orderedlist',
type: 'button',
icon: LIST_ORDERED_ICON,
shortKey: 'Ctrl+O',
shortKey: `${CTRL_KEY}+O`,
shortKeyWithCode: `${CTRL_KEY}+79`,
handler: ToolBarHandler.ol,
},
checklist: {
id: 'checklist',
name: 'checklist',
type: 'button',
icon: LIST_CHECK_ICON,
shortKey: 'Ctrl+Alt+C',
shortKey: `${CTRL_KEY}+${ALT_KEY}+C`,
shortKeyWithCode: `${CTRL_KEY}+${ALT_KEY}+67`,
handler: ToolBarHandler.checkList,
},
underline: {
id: 'underline',
name: 'underline',
type: 'button',
icon: UNDERLINE_ICON,
shortKey: 'Ctrl+R',
shortKey: `${CTRL_KEY}+R`,
shortKeyWithCode: `${CTRL_KEY}+82`,
handler: ToolBarHandler.underline,
},
font: {
Expand All @@ -379,15 +394,17 @@ export const DEFAULT_TOOLBARS: Record<string, IToolbarItemConfig> = {
name: 'link',
type: 'button',
icon: LINK_ICON,
shortKey: 'Ctrl+L',
shortKey: `${CTRL_KEY}+L`,
shortKeyWithCode: `${CTRL_KEY}+76`,
handler: ToolBarHandler.link,
},
image: {
id: 'image',
name: 'image',
type: 'button',
icon: IMAGE_ICON,
shortKey: 'Ctrl+G',
shortKey: `${CTRL_KEY}+G`,
shortKeyWithCode: `${CTRL_KEY}+71`,
params: { imageUploadToServer: false },
handler: ToolBarHandler.image,
},
Expand All @@ -397,23 +414,26 @@ export const DEFAULT_TOOLBARS: Record<string, IToolbarItemConfig> = {
type: 'button',
icon: FILE_ICON,
params: {},
shortKey: 'Ctrl+F',
shortKey: `${CTRL_KEY}+F`,
shortKeyWithCode: `${CTRL_KEY}+70`,
handler: ToolBarHandler.file,
},
code: {
id: 'code',
name: 'code',
type: 'button',
icon: CODE_ICON,
shortKey: 'Ctrl+K',
shortKey: `${CTRL_KEY}+K`,
shortKeyWithCode: `${CTRL_KEY}+75`,
handler: ToolBarHandler.code,
},
table: {
id: 'table',
name: 'table',
type: 'button',
icon: TABLE_ICON,
shortKey: 'Ctrl+Alt+T',
shortKey: `${CTRL_KEY}+${ALT_KEY}+T`,
shortKeyWithCode: `${CTRL_KEY}+${ALT_KEY}+84`,
handler: ToolBarHandler.table,
},
fullscreen: {
Expand Down

0 comments on commit b8710c9

Please sign in to comment.