Skip to content

Commit a46c91c

Browse files
committed
feat: add more shortcuts
1 parent af8af8f commit a46c91c

File tree

3 files changed

+57
-45
lines changed

3 files changed

+57
-45
lines changed

packages/bytemd/src/editor.svelte

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import useYamlFrontmatter from 'codemirror-ssr/mode/yaml-frontmatter/yaml-frontmatter';
3535
import useVim from 'codemirror-ssr/keymap/vim';
3636
import useEmacs from 'codemirror-ssr/keymap/emacs';
37+
// import useLint from 'codemirror-ssr/addon/lint/lint';
3738
3839
export let value: EditorProps['value'] = '';
3940
export let plugins: NonNullable<EditorProps['plugins']> = [];
@@ -161,12 +162,17 @@
161162
useYamlFrontmatter(codemirror);
162163
useVim(codemirror);
163164
useEmacs(codemirror);
165+
// useLint(codemirror);
166+
// codemirror.registerHelper('lint', 'yaml-frontmatter', (text: string) => {
167+
// debugger;
168+
// });
164169
165170
editor = codemirror.fromTextArea(textarea, {
166171
mode: 'yaml-frontmatter',
167172
lineWrapping: true,
168173
tabSize: 8, // keep consistent with preview: https://developer.mozilla.org/en-US/docs/Web/CSS/tab-size#formal_definition
169174
placeholder,
175+
// lint: true,
170176
...editorConfig,
171177
});
172178

packages/bytemd/src/editor.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,13 @@ export function findStartIndex(num: number, nums: number[]) {
120120
return startIndex;
121121
}
122122

123-
const getShortcutWithPrefix = (key: string) => {
124-
if (typeof navigator !== 'undefined' && /Mac/.test(navigator.platform)) {
125-
return 'Cmd-' + key;
126-
} else {
127-
return 'Ctrl-' + key;
128-
}
123+
const getShortcutWithPrefix = (key: string, shift = false) => {
124+
const shiftPrefix = shift ? 'Shift-' : '';
125+
const CmdPrefix =
126+
typeof navigator !== 'undefined' && /Mac/.test(navigator.platform)
127+
? 'Cmd-'
128+
: 'Ctrl-';
129+
return shiftPrefix + CmdPrefix + key;
129130
};
130131

131132
export function getBuiltinActions(
@@ -165,11 +166,11 @@ export function getBuiltinActions(
165166
cheatsheet: `**${locale.action.boldText}**`,
166167
handler: {
167168
type: 'action',
169+
shortcut: getShortcutWithPrefix('B'),
168170
click({ wrapText, editor }) {
169171
wrapText('**');
170172
editor.focus();
171173
},
172-
shortcut: getShortcutWithPrefix('B'),
173174
},
174175
},
175176
{
@@ -178,11 +179,11 @@ export function getBuiltinActions(
178179
cheatsheet: `*${locale.action.italicText}*`,
179180
handler: {
180181
type: 'action',
182+
shortcut: getShortcutWithPrefix('I'),
181183
click({ wrapText, editor }) {
182184
wrapText('*');
183185
editor.focus();
184186
},
185-
shortcut: getShortcutWithPrefix('I'),
186187
},
187188
},
188189
{
@@ -203,6 +204,7 @@ export function getBuiltinActions(
203204
cheatsheet: `[${locale.action.linkText}](url)`,
204205
handler: {
205206
type: 'action',
207+
shortcut: getShortcutWithPrefix('K'),
206208
click({ editor, wrapText }) {
207209
wrapText('[', '](url)');
208210
const cursor = editor.getCursor();
@@ -212,7 +214,6 @@ export function getBuiltinActions(
212214
);
213215
editor.focus();
214216
},
215-
shortcut: getShortcutWithPrefix('K'),
216217
},
217218
},
218219
{
@@ -222,6 +223,7 @@ export function getBuiltinActions(
222223
handler: uploadImages
223224
? {
224225
type: 'action',
226+
shortcut: getShortcutWithPrefix('I', true),
225227
async click({ appendBlock, selectFiles, editor }) {
226228
const fileList = await selectFiles({
227229
accept: 'image/*',
@@ -252,6 +254,7 @@ export function getBuiltinActions(
252254
cheatsheet: '`' + locale.action.codeText + '`',
253255
handler: {
254256
type: 'action',
257+
shortcut: getShortcutWithPrefix('K', true),
255258
click({ wrapText, editor }) {
256259
wrapText('`');
257260
editor.focus();
@@ -264,6 +267,7 @@ export function getBuiltinActions(
264267
cheatsheet: '```' + locale.action.codeLang + '↵',
265268
handler: {
266269
type: 'action',
270+
shortcut: getShortcutWithPrefix('C', true),
267271
click({ editor, appendBlock }) {
268272
const { line } = appendBlock('```js\n```');
269273
editor.setSelection({ line, ch: 3 }, { line, ch: 5 });
@@ -277,6 +281,7 @@ export function getBuiltinActions(
277281
cheatsheet: `- ${locale.action.ulItem}`,
278282
handler: {
279283
type: 'action',
284+
shortcut: getShortcutWithPrefix('U', true),
280285
click({ replaceLines, editor }) {
281286
replaceLines((line) => '- ' + line);
282287
editor.focus();
@@ -289,6 +294,7 @@ export function getBuiltinActions(
289294
cheatsheet: `1. ${locale.action.olItem}`,
290295
handler: {
291296
type: 'action',
297+
shortcut: getShortcutWithPrefix('O', true),
292298
click({ replaceLines, editor }) {
293299
replaceLines((line, i) => `${i + 1}. ${line}`);
294300
editor.focus();

packages/bytemd/src/toolbar.svelte

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,42 +33,42 @@
3333
$: previewActive = activeTab === 'preview';
3434
3535
$: rightActions = [
36-
{
37-
title: 'Key binding',
38-
icon: icons.keyboard,
39-
handler: {
40-
type: 'dropdown',
41-
actions: [
42-
{
43-
title: 'Normal',
44-
handler: {
45-
type: 'action',
46-
click() {
47-
dispatch('key', 'default');
48-
},
49-
},
50-
},
51-
{
52-
title: 'Vim',
53-
handler: {
54-
type: 'action',
55-
click() {
56-
dispatch('key', 'vim');
57-
},
58-
},
59-
},
60-
{
61-
title: 'Emacs',
62-
handler: {
63-
type: 'action',
64-
click() {
65-
dispatch('key', 'emacs');
66-
},
67-
},
68-
},
69-
],
70-
},
71-
},
36+
// {
37+
// title: 'Key binding',
38+
// icon: icons.keyboard,
39+
// handler: {
40+
// type: 'dropdown',
41+
// actions: [
42+
// {
43+
// title: 'Normal',
44+
// handler: {
45+
// type: 'action',
46+
// click() {
47+
// dispatch('key', 'default');
48+
// },
49+
// },
50+
// },
51+
// {
52+
// title: 'Vim',
53+
// handler: {
54+
// type: 'action',
55+
// click() {
56+
// dispatch('key', 'vim');
57+
// },
58+
// },
59+
// },
60+
// {
61+
// title: 'Emacs',
62+
// handler: {
63+
// type: 'action',
64+
// click() {
65+
// dispatch('key', 'emacs');
66+
// },
67+
// },
68+
// },
69+
// ],
70+
// },
71+
// },
7272
{
7373
title: tocActive ? locale.toolbar.closeToc : locale.toolbar.toc,
7474
icon: icons.toc,

0 commit comments

Comments
 (0)