Skip to content

Commit 3df9dd0

Browse files
authored
Merge pull request #46 from github/new-keyboard-shortcuts
New keyboard shortcuts for quotes, bullets and numbered lists
2 parents 731b52a + 08ede8f commit 3df9dd0

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

Diff for: src/index.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ class MarkdownQuoteButtonElement extends MarkdownButtonElement {
169169
super()
170170
styles.set(this, {prefix: '> ', multiline: true, surroundWithNewlines: true})
171171
}
172+
173+
connectedCallback() {
174+
super.connectedCallback()
175+
this.setAttribute('hotkey', '.')
176+
}
172177
}
173178

174179
if (!window.customElements.get('md-quote')) {
@@ -227,6 +232,11 @@ class MarkdownUnorderedListButtonElement extends MarkdownButtonElement {
227232
super()
228233
styles.set(this, {prefix: '- ', multiline: true, surroundWithNewlines: true})
229234
}
235+
connectedCallback() {
236+
super.connectedCallback()
237+
this.setAttribute('hotkey', '8')
238+
this.setAttribute('hotkey-requires-shift', 'true')
239+
}
230240
}
231241

232242
if (!window.customElements.get('md-unordered-list')) {
@@ -239,6 +249,11 @@ class MarkdownOrderedListButtonElement extends MarkdownButtonElement {
239249
super()
240250
styles.set(this, {prefix: '1. ', multiline: true, orderedList: true})
241251
}
252+
connectedCallback() {
253+
super.connectedCallback()
254+
this.setAttribute('hotkey', '9')
255+
this.setAttribute('hotkey-requires-shift', 'true')
256+
}
242257
}
243258

244259
if (!window.customElements.get('md-ordered-list')) {
@@ -382,10 +397,13 @@ function focusKeydown(event: KeyboardEvent) {
382397
}
383398

384399
const shortcutListeners = new WeakMap()
400+
function elementHotkeyRequiresShift(element: Element): boolean {
401+
return element.hasAttribute('hotkey-requires-shift') && element.getAttribute('hotkey-requires-shift') !== 'false'
402+
}
385403

386-
function findHotkey(toolbar: Element, key: string): HTMLElement | null {
404+
function findHotkey(toolbar: Element, key: string, shiftPressed: boolean): HTMLElement | null {
387405
for (const el of toolbar.querySelectorAll<HTMLElement>('[hotkey]')) {
388-
if (el.getAttribute('hotkey') === key) {
406+
if (el.getAttribute('hotkey') === key && (!elementHotkeyRequiresShift(el) || shiftPressed)) {
389407
return el
390408
}
391409
}
@@ -395,7 +413,7 @@ function findHotkey(toolbar: Element, key: string): HTMLElement | null {
395413
function shortcut(toolbar: Element, event: KeyboardEvent) {
396414
if ((event.metaKey && modifierKey === 'Meta') || (event.ctrlKey && modifierKey === 'Control')) {
397415
const key = event.shiftKey ? event.key.toUpperCase() : event.key
398-
const button = findHotkey(toolbar, key)
416+
const button = findHotkey(toolbar, key, event.shiftKey)
399417
if (button) {
400418
button.click()
401419
event.preventDefault()

Diff for: test/test.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ describe('markdown-toolbar-element', function () {
3434
textarea.dispatchEvent(event)
3535
}
3636

37-
function pressHotkey(hotkey) {
37+
function pressHotkey(hotkey, explicitShiftKey = false) {
3838
const textarea = document.querySelector('textarea')
3939
const osx = navigator.userAgent.indexOf('Macintosh') !== -1
4040
const event = document.createEvent('Event')
4141
event.initEvent('keydown', true, true)
4242
event.metaKey = osx
4343
event.ctrlKey = !osx
44-
event.shiftKey = hotkey === hotkey.toUpperCase()
44+
event.shiftKey = (hotkey === hotkey.toUpperCase() && hotkey !== hotkey.toLowerCase()) || explicitShiftKey
4545

4646
// emulate existing osx browser bug
4747
// https://bugs.webkit.org/show_bug.cgi?id=174782
@@ -468,6 +468,13 @@ describe('markdown-toolbar-element', function () {
468468
assert.equal('> |', visualValue())
469469
})
470470

471+
it('inserts selected quoted sample via hotkey', function () {
472+
focus()
473+
setVisualValue('')
474+
pressHotkey('.')
475+
assert.equal('> |', visualValue())
476+
})
477+
471478
it('quotes the selected text when you click the quote icon', function () {
472479
setVisualValue('|Butts|\n\nThe quick brown fox jumps over the lazy dog')
473480
clickToolbar('md-quote')
@@ -549,6 +556,22 @@ describe('markdown-toolbar-element', function () {
549556
assert.equal('One\n\n|- Two\n- Three|\n', visualValue())
550557
})
551558

559+
it('turns multiple lines into unordered list via hotkey, requiring shift', function () {
560+
setVisualValue('One\n|Two\nThree|\n')
561+
pressHotkey('8', false)
562+
assert.equal('One\n|Two\nThree|\n', visualValue())
563+
pressHotkey('8', true)
564+
assert.equal('One\n\n|- Two\n- Three|\n', visualValue())
565+
})
566+
567+
it('turns multiple lines into ordered list via hotkey, requiring shift', function () {
568+
setVisualValue('One\n|Two\nThree|\n')
569+
pressHotkey('9', false)
570+
assert.equal('One\n|Two\nThree|\n', visualValue())
571+
pressHotkey('9', true)
572+
assert.equal('One\n\n|1. Two\n2. Three|\n', visualValue())
573+
})
574+
552575
it('prefixes newlines when a list is created on the last line', function () {
553576
setVisualValue("Here's a list:|One|")
554577
clickToolbar('md-unordered-list')

0 commit comments

Comments
 (0)