Skip to content

Commit

Permalink
Merge pull request #37 from github/td/add-hotkey-to-code-button
Browse files Browse the repository at this point in the history
Add hotkey to code block button
  • Loading branch information
keithamus authored Mar 22, 2021
2 parents 58bba11 + 53b93f2 commit df9c592
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ class MarkdownCodeButtonElement extends MarkdownButtonElement {
super()
styles.set(this, {prefix: '`', suffix: '`', blockPrefix: '```', blockSuffix: '```'})
}

connectedCallback() {
super.connectedCallback()
this.setAttribute('hotkey', 'E')
}
}

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

function shortcut(toolbar: Element, event: KeyboardEvent) {
if ((event.metaKey && modifierKey === 'Meta') || (event.ctrlKey && modifierKey === 'Control')) {
const button = findHotkey(toolbar, event.key)
const key = event.shiftKey ? event.key.toUpperCase() : event.key
const button = findHotkey(toolbar, key)
if (button) {
button.click()
event.preventDefault()
Expand Down
30 changes: 29 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ describe('markdown-toolbar-element', function () {
event.initEvent('keydown', true, true)
event.metaKey = osx
event.ctrlKey = !osx
event.key = hotkey
event.shiftKey = hotkey === hotkey.toUpperCase()

// emulate existing osx browser bug
// https://bugs.webkit.org/show_bug.cgi?id=174782
event.key = osx ? hotkey.toLowerCase() : hotkey

textarea.dispatchEvent(event)
}

Expand Down Expand Up @@ -193,6 +198,22 @@ describe('markdown-toolbar-element', function () {
})
})

describe('hotkey case-sensitivity', function () {
it('does not bold selected text when using the uppercased hotkey', function () {
focus()
setVisualValue('The |quick| brown fox jumps over the lazy dog')
pressHotkey('B') // capital `B` instead of lowercase `b`
assert.equal('The |quick| brown fox jumps over the lazy dog', visualValue())
})

it('does not codeblock selected text when using the lowercased hotkey', function () {
focus()
setVisualValue('The |quick| brown fox jumps over the lazy dog')
pressHotkey('e') // lowercase `e` instead of uppercase `E`
assert.equal('The |quick| brown fox jumps over the lazy dog', visualValue())
})
})

describe('bold', function () {
it('bold selected text when you click the bold icon', function () {
setVisualValue('The |quick| brown fox jumps over the lazy dog')
Expand Down Expand Up @@ -605,6 +626,13 @@ describe('markdown-toolbar-element', function () {
assert.equal("`|puts 'Hello, world!'|`", visualValue())
})

it('surrounds a line with backticks via hotkey', function () {
focus()
setVisualValue("|puts 'Hello, world!'|")
pressHotkey('E')
assert.equal("`|puts 'Hello, world!'|`", visualValue())
})

it('surrounds multiple lines with triple backticks if you click the code icon', function () {
setVisualValue('|class Greeter\n def hello_world\n "Hello World!"\n end\nend|')
clickToolbar('md-code')
Expand Down

0 comments on commit df9c592

Please sign in to comment.