WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit f090933

Browse files
committed
refactor to Map
1 parent 9b85e5d commit f090933

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

web_src/js/features/comp/EditorMarkdown.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -196,25 +196,23 @@ function handleNewline(textarea: HTMLTextAreaElement, e: KeyboardEvent) {
196196

197197
// Keys that act as dead keys will not work because the spec dictates that such keys are
198198
// emitted as `Dead` in e.key instead of the actual key.
199-
const pairs: Record<string, string> = {
200-
"'": "'",
201-
'"': '"',
202-
'`': '`',
203-
'(': ')',
204-
'[': ']',
205-
'{': '}',
206-
'<': '>',
207-
};
199+
const pairs = new Map<string, string>([
200+
["'", "'"],
201+
['"', '"'],
202+
['`', '`'],
203+
['(', ')'],
204+
['[', ']'],
205+
['{', '}'],
206+
['<', '>'],
207+
]);
208208

209209
function handlePairCharacter(textarea: HTMLTextAreaElement, e: KeyboardEvent): void {
210210
const selStart = textarea.selectionStart;
211211
const selEnd = textarea.selectionEnd;
212212
if (selEnd === selStart) return; // do not process when no selection
213213
e.preventDefault();
214-
const openChar = e.key;
215-
const closeChar = pairs[e.key];
216214
const inner = textarea.value.substring(selStart, selEnd);
217-
replaceTextareaSelection(textarea, `${openChar}${inner}${closeChar}`);
215+
replaceTextareaSelection(textarea, `${e.key}${inner}${pairs.get(e.key)}`);
218216
textarea.setSelectionRange(selStart + 1, selEnd + 1);
219217
}
220218

@@ -231,7 +229,7 @@ export function initTextareaMarkdown(textarea: HTMLTextAreaElement) {
231229
} else if (e.key === 'Enter' && !e.shiftKey && !e.ctrlKey && !e.metaKey && !e.altKey) {
232230
// use Enter to insert a new line with the same indention and prefix
233231
handleNewline(textarea, e);
234-
} else if (Object.keys(pairs).includes(e.key)) {
232+
} else if (pairs.has(e.key)) {
235233
handlePairCharacter(textarea, e);
236234
}
237235
});

0 commit comments

Comments
 (0)