Skip to content

Commit df9c592

Browse files
authored
Merge pull request #37 from github/td/add-hotkey-to-code-button
Add hotkey to code block button
2 parents 58bba11 + 53b93f2 commit df9c592

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ class MarkdownCodeButtonElement extends MarkdownButtonElement {
181181
super()
182182
styles.set(this, {prefix: '`', suffix: '`', blockPrefix: '```', blockSuffix: '```'})
183183
}
184+
185+
connectedCallback() {
186+
super.connectedCallback()
187+
this.setAttribute('hotkey', 'E')
188+
}
184189
}
185190

186191
if (!window.customElements.get('md-code')) {
@@ -389,7 +394,8 @@ function findHotkey(toolbar: Element, key: string): HTMLElement | null {
389394

390395
function shortcut(toolbar: Element, event: KeyboardEvent) {
391396
if ((event.metaKey && modifierKey === 'Meta') || (event.ctrlKey && modifierKey === 'Control')) {
392-
const button = findHotkey(toolbar, event.key)
397+
const key = event.shiftKey ? event.key.toUpperCase() : event.key
398+
const button = findHotkey(toolbar, key)
393399
if (button) {
394400
button.click()
395401
event.preventDefault()

test/test.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ describe('markdown-toolbar-element', function () {
4141
event.initEvent('keydown', true, true)
4242
event.metaKey = osx
4343
event.ctrlKey = !osx
44-
event.key = hotkey
44+
event.shiftKey = hotkey === hotkey.toUpperCase()
45+
46+
// emulate existing osx browser bug
47+
// https://bugs.webkit.org/show_bug.cgi?id=174782
48+
event.key = osx ? hotkey.toLowerCase() : hotkey
49+
4550
textarea.dispatchEvent(event)
4651
}
4752

@@ -193,6 +198,22 @@ describe('markdown-toolbar-element', function () {
193198
})
194199
})
195200

201+
describe('hotkey case-sensitivity', function () {
202+
it('does not bold selected text when using the uppercased hotkey', function () {
203+
focus()
204+
setVisualValue('The |quick| brown fox jumps over the lazy dog')
205+
pressHotkey('B') // capital `B` instead of lowercase `b`
206+
assert.equal('The |quick| brown fox jumps over the lazy dog', visualValue())
207+
})
208+
209+
it('does not codeblock selected text when using the lowercased hotkey', function () {
210+
focus()
211+
setVisualValue('The |quick| brown fox jumps over the lazy dog')
212+
pressHotkey('e') // lowercase `e` instead of uppercase `E`
213+
assert.equal('The |quick| brown fox jumps over the lazy dog', visualValue())
214+
})
215+
})
216+
196217
describe('bold', function () {
197218
it('bold selected text when you click the bold icon', function () {
198219
setVisualValue('The |quick| brown fox jumps over the lazy dog')
@@ -605,6 +626,13 @@ describe('markdown-toolbar-element', function () {
605626
assert.equal("`|puts 'Hello, world!'|`", visualValue())
606627
})
607628

629+
it('surrounds a line with backticks via hotkey', function () {
630+
focus()
631+
setVisualValue("|puts 'Hello, world!'|")
632+
pressHotkey('E')
633+
assert.equal("`|puts 'Hello, world!'|`", visualValue())
634+
})
635+
608636
it('surrounds multiple lines with triple backticks if you click the code icon', function () {
609637
setVisualValue('|class Greeter\n def hello_world\n "Hello World!"\n end\nend|')
610638
clickToolbar('md-code')

0 commit comments

Comments
 (0)