Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hotkey to code block button #37

Merged
merged 5 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Comment on lines +397 to +398
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this change is needed. I can revert this block locally and the tests still run green.

The case-sensitivity is already backed in since we do strict equality of the key pressed and the hotkey attribute.

Copy link
Contributor Author

@aaron-tdouble aaron-tdouble Mar 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@koddsson I must be doing something wrong because this test (correctly) fails for me on main.

Copy link
Contributor Author

@aaron-tdouble aaron-tdouble Mar 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does it do for you on the example page?

If I cmd-b or cmd-B (cmd-shift-b), it bolds the text. I would expect it only to respond to a lowercase b.

This is true for me on Chrome, Safari, and Firefox. Also true for someone that used Microsoft Edge browser on Mac.

Copy link
Contributor Author

@aaron-tdouble aaron-tdouble Mar 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@koddsson Wait. Scratch my first comment above. I was testing the wrong thing. Let me look into that in the morning, but my second comment ☝️ still stands. I'm curious how this behaves for you.

I agree with you that it should work as written. All the docs I read say that key will always return the actual key being pressed (as in, the uppercase B or lowercase b), but when I inspect the event in the debugger, I never see an uppercase letter for key.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does it do for you on the example page?

If I cmd-b or cmd-B (cmd-shift-b), it bolds the text. I would expect it only to respond to a lowercase b.

+B is a global shortcut on my mac that launches a application 😄

But yeah if I turn that off then I can bold with both. This is unexpected but apparently, if you are holding the meta key down KeyboardEvent.key will always be lowercase.

I made a little site to test out the keydown handler here.

Getting the tests to fail when this block is removed would be good though 👍🏻

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah... this is a specific edge case with macos reporting lower case keys for specifically cmd+shift. See https://bugs.webkit.org/show_bug.cgi?id=174782 & https://bugs.chromium.org/p/chromium/issues/detail?id=747358

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good finds and information, @koddsson and @keithamus. I'll work on getting a breaking test on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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