Skip to content

Commit fa1ca22

Browse files
authored
Merge pull request #51 from github/remove-hotkeys
Remove hotkey functionality
2 parents 19f5f68 + 9835dc5 commit fa1ca22

File tree

2 files changed

+0
-156
lines changed

2 files changed

+0
-156
lines changed

src/index.ts

-78
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,6 @@ class MarkdownBoldButtonElement extends MarkdownButtonElement {
135135
super()
136136
styles.set(this, {prefix: '**', suffix: '**', trimFirst: true})
137137
}
138-
139-
connectedCallback() {
140-
super.connectedCallback()
141-
this.setAttribute('hotkey', 'b')
142-
}
143138
}
144139

145140
if (!window.customElements.get('md-bold')) {
@@ -152,11 +147,6 @@ class MarkdownItalicButtonElement extends MarkdownButtonElement {
152147
super()
153148
styles.set(this, {prefix: '_', suffix: '_', trimFirst: true})
154149
}
155-
156-
connectedCallback() {
157-
super.connectedCallback()
158-
this.setAttribute('hotkey', 'i')
159-
}
160150
}
161151

162152
if (!window.customElements.get('md-italic')) {
@@ -169,12 +159,6 @@ class MarkdownQuoteButtonElement extends MarkdownButtonElement {
169159
super()
170160
styles.set(this, {prefix: '> ', multiline: true, surroundWithNewlines: true})
171161
}
172-
173-
connectedCallback() {
174-
super.connectedCallback()
175-
this.setAttribute('hotkey', '.')
176-
this.setAttribute('hotkey-requires-shift', 'true')
177-
}
178162
}
179163

180164
if (!window.customElements.get('md-quote')) {
@@ -187,11 +171,6 @@ class MarkdownCodeButtonElement extends MarkdownButtonElement {
187171
super()
188172
styles.set(this, {prefix: '`', suffix: '`', blockPrefix: '```', blockSuffix: '```'})
189173
}
190-
191-
connectedCallback() {
192-
super.connectedCallback()
193-
this.setAttribute('hotkey', 'e')
194-
}
195174
}
196175

197176
if (!window.customElements.get('md-code')) {
@@ -204,11 +183,6 @@ class MarkdownLinkButtonElement extends MarkdownButtonElement {
204183
super()
205184
styles.set(this, {prefix: '[', suffix: '](url)', replaceNext: 'url', scanFor: 'https?://'})
206185
}
207-
208-
connectedCallback() {
209-
super.connectedCallback()
210-
this.setAttribute('hotkey', 'k')
211-
}
212186
}
213187

214188
if (!window.customElements.get('md-link')) {
@@ -233,11 +207,6 @@ class MarkdownUnorderedListButtonElement extends MarkdownButtonElement {
233207
super()
234208
styles.set(this, {prefix: '- ', multiline: true, surroundWithNewlines: true})
235209
}
236-
connectedCallback() {
237-
super.connectedCallback()
238-
this.setAttribute('hotkey', '8')
239-
this.setAttribute('hotkey-requires-shift', 'true')
240-
}
241210
}
242211

243212
if (!window.customElements.get('md-unordered-list')) {
@@ -250,11 +219,6 @@ class MarkdownOrderedListButtonElement extends MarkdownButtonElement {
250219
super()
251220
styles.set(this, {prefix: '1. ', multiline: true, orderedList: true})
252221
}
253-
connectedCallback() {
254-
super.connectedCallback()
255-
this.setAttribute('hotkey', '7')
256-
this.setAttribute('hotkey-requires-shift', 'true')
257-
}
258222
}
259223

260224
if (!window.customElements.get('md-ordered-list')) {
@@ -267,11 +231,6 @@ class MarkdownTaskListButtonElement extends MarkdownButtonElement {
267231
super()
268232
styles.set(this, {prefix: '- [ ] ', multiline: true, surroundWithNewlines: true})
269233
}
270-
271-
connectedCallback() {
272-
super.connectedCallback()
273-
this.setAttribute('hotkey', 'L')
274-
}
275234
}
276235

277236
if (!window.customElements.get('md-task-list')) {
@@ -315,8 +274,6 @@ if (!window.customElements.get('md-strikethrough')) {
315274
window.customElements.define('md-strikethrough', MarkdownStrikethroughButtonElement)
316275
}
317276

318-
const modifierKey = navigator.userAgent.match(/Macintosh/) ? 'Meta' : 'Control'
319-
320277
class MarkdownToolbarElement extends HTMLElement {
321278
constructor() {
322279
super()
@@ -327,21 +284,11 @@ class MarkdownToolbarElement extends HTMLElement {
327284
this.setAttribute('role', 'toolbar')
328285
}
329286
this.addEventListener('keydown', focusKeydown)
330-
const fn = shortcut.bind(null, this)
331-
if (this.field) {
332-
this.field.addEventListener('keydown', fn)
333-
shortcutListeners.set(this, fn)
334-
}
335287
this.setAttribute('tabindex', '0')
336288
this.addEventListener('focus', onToolbarFocus, {once: true})
337289
}
338290

339291
disconnectedCallback(): void {
340-
const fn = shortcutListeners.get(this)
341-
if (fn && this.field) {
342-
this.field.removeEventListener('keydown', fn)
343-
shortcutListeners.delete(this)
344-
}
345292
this.removeEventListener('keydown', focusKeydown)
346293
}
347294

@@ -397,31 +344,6 @@ function focusKeydown(event: KeyboardEvent) {
397344
buttons[n].focus()
398345
}
399346

400-
const shortcutListeners = new WeakMap()
401-
function elementHotkeyRequiresShift(element: Element): boolean {
402-
return element.hasAttribute('hotkey-requires-shift') && element.getAttribute('hotkey-requires-shift') !== 'false'
403-
}
404-
405-
function findHotkey(toolbar: Element, key: string, shiftPressed: boolean): HTMLElement | null {
406-
for (const el of toolbar.querySelectorAll<HTMLElement>('[hotkey]')) {
407-
if (el.getAttribute('hotkey') === key && (!elementHotkeyRequiresShift(el) || shiftPressed)) {
408-
return el
409-
}
410-
}
411-
return null
412-
}
413-
414-
function shortcut(toolbar: Element, event: KeyboardEvent) {
415-
if ((event.metaKey && modifierKey === 'Meta') || (event.ctrlKey && modifierKey === 'Control')) {
416-
const key = event.shiftKey ? event.key.toUpperCase() : event.key
417-
const button = findHotkey(toolbar, key, event.shiftKey)
418-
if (button) {
419-
button.click()
420-
event.preventDefault()
421-
}
422-
}
423-
}
424-
425347
if (!window.customElements.get('markdown-toolbar')) {
426348
window.MarkdownToolbarElement = MarkdownToolbarElement
427349
window.customElements.define('markdown-toolbar', MarkdownToolbarElement)

test/test.js

-78
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,6 @@ describe('markdown-toolbar-element', function () {
2727
})
2828

2929
describe('after tree insertion', function () {
30-
function focus() {
31-
const textarea = document.querySelector('textarea')
32-
const event = document.createEvent('Event')
33-
event.initEvent('focus', false, true)
34-
textarea.dispatchEvent(event)
35-
}
36-
37-
function pressHotkey(hotkey, explicitShiftKey = false) {
38-
const textarea = document.querySelector('textarea')
39-
const osx = navigator.userAgent.indexOf('Macintosh') !== -1
40-
const event = document.createEvent('Event')
41-
event.initEvent('keydown', true, true)
42-
event.metaKey = osx
43-
event.ctrlKey = !osx
44-
event.shiftKey = (hotkey === hotkey.toUpperCase() && hotkey !== hotkey.toLowerCase()) || explicitShiftKey
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-
50-
textarea.dispatchEvent(event)
51-
}
52-
5330
function clickToolbar(selector) {
5431
const toolbar = document.querySelector('markdown-toolbar')
5532
toolbar.querySelector(selector).click()
@@ -198,29 +175,13 @@ describe('markdown-toolbar-element', function () {
198175
})
199176
})
200177

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-
210178
describe('bold', function () {
211179
it('bold selected text when you click the bold icon', function () {
212180
setVisualValue('The |quick| brown fox jumps over the lazy dog')
213181
clickToolbar('md-bold')
214182
assert.equal('The **|quick|** brown fox jumps over the lazy dog', visualValue())
215183
})
216184

217-
it('bolds selected text with hotkey', function () {
218-
focus()
219-
setVisualValue('The |quick| brown fox jumps over the lazy dog')
220-
pressHotkey('b')
221-
assert.equal('The **|quick|** brown fox jumps over the lazy dog', visualValue())
222-
})
223-
224185
it('bold empty selection and textarea inserts ** with cursor ready to type inside', function () {
225186
setVisualValue('|')
226187
clickToolbar('md-bold')
@@ -349,13 +310,6 @@ describe('markdown-toolbar-element', function () {
349310
assert.equal('The _|quick|_ brown fox jumps over the lazy dog', visualValue())
350311
})
351312

352-
it('italicizes selected text with hotkey', function () {
353-
focus()
354-
setVisualValue('The |quick| brown fox jumps over the lazy dog')
355-
pressHotkey('i')
356-
assert.equal('The _|quick|_ brown fox jumps over the lazy dog', visualValue())
357-
})
358-
359313
it('italicize when there is leading whitespace in selection', function () {
360314
setVisualValue('| \nHello world|')
361315
clickToolbar('md-italic')
@@ -468,15 +422,6 @@ describe('markdown-toolbar-element', function () {
468422
assert.equal('> |', visualValue())
469423
})
470424

471-
it('inserts selected quoted sample via hotkey, requiring shift', function () {
472-
focus()
473-
setVisualValue('')
474-
pressHotkey('.', false)
475-
assert.equal('|', visualValue())
476-
pressHotkey('.', true)
477-
assert.equal('> |', visualValue())
478-
})
479-
480425
it('quotes the selected text when you click the quote icon', function () {
481426
setVisualValue('|Butts|\n\nThe quick brown fox jumps over the lazy dog')
482427
clickToolbar('md-quote')
@@ -558,22 +503,6 @@ describe('markdown-toolbar-element', function () {
558503
assert.equal('One\n\n|- Two\n- Three|\n', visualValue())
559504
})
560505

561-
it('turns multiple lines into unordered list via hotkey, requiring shift', function () {
562-
setVisualValue('One\n|Two\nThree|\n')
563-
pressHotkey('8', false)
564-
assert.equal('One\n|Two\nThree|\n', visualValue())
565-
pressHotkey('8', true)
566-
assert.equal('One\n\n|- Two\n- Three|\n', visualValue())
567-
})
568-
569-
it('turns multiple lines into ordered list via hotkey, requiring shift', function () {
570-
setVisualValue('One\n|Two\nThree|\n')
571-
pressHotkey('7', false)
572-
assert.equal('One\n|Two\nThree|\n', visualValue())
573-
pressHotkey('7', true)
574-
assert.equal('One\n\n|1. Two\n2. Three|\n', visualValue())
575-
})
576-
577506
it('prefixes newlines when a list is created on the last line', function () {
578507
setVisualValue("Here's a list:|One|")
579508
clickToolbar('md-unordered-list')
@@ -644,13 +573,6 @@ describe('markdown-toolbar-element', function () {
644573
assert.equal("`|puts 'Hello, world!'|`", visualValue())
645574
})
646575

647-
it('surrounds a line with backticks via hotkey', function () {
648-
focus()
649-
setVisualValue("|puts 'Hello, world!'|")
650-
pressHotkey('e')
651-
assert.equal("`|puts 'Hello, world!'|`", visualValue())
652-
})
653-
654576
it('surrounds multiple lines with triple backticks if you click the code icon', function () {
655577
setVisualValue('|class Greeter\n def hello_world\n "Hello World!"\n end\nend|')
656578
clickToolbar('md-code')

0 commit comments

Comments
 (0)