Skip to content

Commit d8370f3

Browse files
authored
Merge pull request #36 from mattmikolay/strikethrough
Add support for strikethrough
2 parents 7bbdf32 + 3b1f01d commit d8370f3

File tree

3 files changed

+85
-11
lines changed

3 files changed

+85
-11
lines changed

examples/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<md-task-list class="btn btn-sm">task-list</md-task-list>
2121
<md-mention class="btn btn-sm">mention</md-mention>
2222
<md-ref class="btn btn-sm">ref</md-ref>
23+
<md-strikethrough class="btn btn-sm">strikethrough</md-strikethrough>
2324
</markdown-toolbar>
2425
<textarea rows="6" class="mt-3 d-block width-full" id="textarea"></textarea>
2526
</div>
@@ -41,6 +42,7 @@
4142
<md-task-list class="btn btn-sm">task-list</md-task-list>
4243
<md-mention class="btn btn-sm">mention</md-mention>
4344
<md-ref class="btn btn-sm">ref</md-ref>
45+
<md-strikethrough class="btn btn-sm">strikethrough</md-strikethrough>
4446
</markdown-toolbar>
4547
<textarea rows="6" class="mt-3 d-block width-full" id="textarea2"></textarea>
4648
</div>

src/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ declare global {
1313
MarkdownTaskListButtonElement: typeof MarkdownTaskListButtonElement
1414
MarkdownMentionButtonElement: typeof MarkdownMentionButtonElement
1515
MarkdownRefButtonElement: typeof MarkdownRefButtonElement
16+
MarkdownStrikethroughButtonElement: typeof MarkdownStrikethroughButtonElement
1617
}
1718
interface HTMLElementTagNameMap {
1819
'markdown-toolbar': MarkdownToolbarElement
@@ -28,6 +29,7 @@ declare global {
2829
'md-task-list': MarkdownTaskListButtonElement
2930
'md-mention': MarkdownMentionButtonElement
3031
'md-ref': MarkdownRefButtonElement
32+
'md-strikethrough': MarkdownStrikethroughButtonElement
3133
}
3234
}
3335

@@ -44,7 +46,8 @@ const buttonSelectors = [
4446
'md-ordered-list',
4547
'md-task-list',
4648
'md-mention',
47-
'md-ref'
49+
'md-ref',
50+
'md-strikethrough'
4851
]
4952
function getButtons(toolbar: Element): HTMLElement[] {
5053
const els = []
@@ -279,6 +282,18 @@ if (!window.customElements.get('md-ref')) {
279282
window.customElements.define('md-ref', MarkdownRefButtonElement)
280283
}
281284

285+
class MarkdownStrikethroughButtonElement extends MarkdownButtonElement {
286+
constructor() {
287+
super()
288+
styles.set(this, {prefix: '~~', suffix: '~~', trimFirst: true})
289+
}
290+
}
291+
292+
if (!window.customElements.get('md-strikethrough')) {
293+
window.MarkdownStrikethroughButtonElement = MarkdownStrikethroughButtonElement
294+
window.customElements.define('md-strikethrough', MarkdownStrikethroughButtonElement)
295+
}
296+
282297
const modifierKey = navigator.userAgent.match(/Macintosh/) ? 'Meta' : 'Control'
283298

284299
class MarkdownToolbarElement extends HTMLElement {

test/test.js

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ describe('markdown-toolbar-element', function () {
9292
<md-header level="10">h1</md-header>
9393
<div data-md-button>Other button</div>
9494
<md-italic>italic</md-italic>
95+
<md-strikethrough>strikethrough</md-strikethrough>
9596
<md-quote>quote</md-quote>
9697
<md-code>code</md-code>
9798
<md-link>link</md-link>
@@ -135,47 +136,47 @@ describe('markdown-toolbar-element', function () {
135136
it('moves focus to next button when ArrowRight is pressed', function () {
136137
focusFirstButton()
137138
pushKeyOnFocussedButton('ArrowRight')
138-
assert.equal(getElementsWithTabindex(-1).length, 14)
139+
assert.equal(getElementsWithTabindex(-1).length, 15)
139140
assert.deepEqual(getElementsWithTabindex(0), [document.querySelector('md-header')])
140141
assert.deepEqual(getElementsWithTabindex(0), [document.activeElement])
141142
pushKeyOnFocussedButton('ArrowRight')
142-
assert.equal(getElementsWithTabindex(-1).length, 14)
143+
assert.equal(getElementsWithTabindex(-1).length, 15)
143144
assert.deepEqual(getElementsWithTabindex(0), [document.querySelector('md-header[level="1"]')])
144145
assert.deepEqual(getElementsWithTabindex(0), [document.activeElement])
145146
pushKeyOnFocussedButton('ArrowRight')
146-
assert.equal(getElementsWithTabindex(-1).length, 14)
147+
assert.equal(getElementsWithTabindex(-1).length, 15)
147148
assert.deepEqual(getElementsWithTabindex(0), [document.querySelector('md-header[level="10"]')])
148149
assert.deepEqual(getElementsWithTabindex(0), [document.activeElement])
149150
})
150151

151152
it('cycles focus round to last element from first when ArrowLeft is pressed', function () {
152153
focusFirstButton()
153154
pushKeyOnFocussedButton('ArrowLeft')
154-
assert.equal(getElementsWithTabindex(-1).length, 14)
155+
assert.equal(getElementsWithTabindex(-1).length, 15)
155156
assert.deepEqual(getElementsWithTabindex(0), [document.querySelector('md-ref')])
156157
assert.deepEqual(getElementsWithTabindex(0), [document.activeElement])
157158
pushKeyOnFocussedButton('ArrowLeft')
158-
assert.equal(getElementsWithTabindex(-1).length, 14)
159+
assert.equal(getElementsWithTabindex(-1).length, 15)
159160
assert.deepEqual(getElementsWithTabindex(0), [document.querySelector('md-mention')])
160161
assert.deepEqual(getElementsWithTabindex(0), [document.activeElement])
161162
})
162163

163164
it('focussed first/last button when Home/End key is pressed', function () {
164165
focusFirstButton()
165166
pushKeyOnFocussedButton('End')
166-
assert.equal(getElementsWithTabindex(-1).length, 14)
167+
assert.equal(getElementsWithTabindex(-1).length, 15)
167168
assert.deepEqual(getElementsWithTabindex(0), [document.querySelector('md-ref')])
168169
assert.deepEqual(getElementsWithTabindex(0), [document.activeElement])
169170
pushKeyOnFocussedButton('End')
170-
assert.equal(getElementsWithTabindex(-1).length, 14)
171+
assert.equal(getElementsWithTabindex(-1).length, 15)
171172
assert.deepEqual(getElementsWithTabindex(0), [document.querySelector('md-ref')])
172173
assert.deepEqual(getElementsWithTabindex(0), [document.activeElement])
173174
pushKeyOnFocussedButton('Home')
174-
assert.equal(getElementsWithTabindex(-1).length, 14)
175+
assert.equal(getElementsWithTabindex(-1).length, 15)
175176
assert.deepEqual(getElementsWithTabindex(0), [document.querySelector('md-bold')])
176177
assert.deepEqual(getElementsWithTabindex(0), [document.activeElement])
177178
pushKeyOnFocussedButton('Home')
178-
assert.equal(getElementsWithTabindex(-1).length, 14)
179+
assert.equal(getElementsWithTabindex(-1).length, 15)
179180
assert.deepEqual(getElementsWithTabindex(0), [document.querySelector('md-bold')])
180181
assert.deepEqual(getElementsWithTabindex(0), [document.activeElement])
181182
})
@@ -186,7 +187,7 @@ describe('markdown-toolbar-element', function () {
186187
pushKeyOnFocussedButton('ArrowRight')
187188
pushKeyOnFocussedButton('ArrowRight')
188189
pushKeyOnFocussedButton('ArrowRight')
189-
assert.equal(getElementsWithTabindex(-1).length, 14)
190+
assert.equal(getElementsWithTabindex(-1).length, 15)
190191
assert.deepEqual(getElementsWithTabindex(0), [document.querySelector('div[data-md-button]')])
191192
assert.deepEqual(getElementsWithTabindex(0), [document.activeElement])
192193
})
@@ -390,6 +391,62 @@ describe('markdown-toolbar-element', function () {
390391
})
391392
})
392393

394+
describe('strikethrough', function () {
395+
it('strikes through selected text when you click the strikethrough icon', function () {
396+
setVisualValue('The |quick| brown fox jumps over the lazy dog')
397+
clickToolbar('md-strikethrough')
398+
assert.equal('The ~~|quick|~~ brown fox jumps over the lazy dog', visualValue())
399+
})
400+
401+
it('strikes through when there is leading whitespace in selection', function () {
402+
setVisualValue('| \nHello world|')
403+
clickToolbar('md-strikethrough')
404+
assert.equal(' \n~~|Hello world|~~', visualValue())
405+
})
406+
407+
it('strikes through when there is trailing whitespace in selection', function () {
408+
setVisualValue('|Hello world\n \t|')
409+
clickToolbar('md-strikethrough')
410+
assert.equal('~~|Hello world|~~\n \t', visualValue())
411+
})
412+
413+
it('strikes through empty selection and textarea inserts ~~ with cursor ready to type inside', function () {
414+
setVisualValue('|')
415+
clickToolbar('md-strikethrough')
416+
assert.equal('~~|~~', visualValue())
417+
})
418+
419+
it('strikes through empty selection with previous text inserts ~~ with cursor ready to type inside', function () {
420+
setVisualValue('The |')
421+
clickToolbar('md-strikethrough')
422+
assert.equal('The ~~|~~', visualValue())
423+
})
424+
425+
it('strikes through selected word when cursor is at the start of the word', function () {
426+
setVisualValue('The |quick brown fox jumps over the lazy dog')
427+
clickToolbar('md-strikethrough')
428+
assert.equal('The ~~|quick~~ brown fox jumps over the lazy dog', visualValue())
429+
})
430+
431+
it('strikes through selected word when cursor is in the middle of the word', function () {
432+
setVisualValue('The qui|ck brown fox jumps over the lazy dog')
433+
clickToolbar('md-strikethrough')
434+
assert.equal('The ~~qui|ck~~ brown fox jumps over the lazy dog', visualValue())
435+
})
436+
437+
it('strikes through selected word when cursor is at the end of the word', function () {
438+
setVisualValue('The quick| brown fox jumps over the lazy dog')
439+
clickToolbar('md-strikethrough')
440+
assert.equal('The ~~quick|~~ brown fox jumps over the lazy dog', visualValue())
441+
})
442+
443+
it('un-strikes through selected struck-through text when you click the strikethrough icon', function () {
444+
setVisualValue('The ~~|quick|~~ brown fox jumps over the lazy dog')
445+
clickToolbar('md-strikethrough')
446+
assert.equal('The |quick| brown fox jumps over the lazy dog', visualValue())
447+
})
448+
})
449+
393450
describe('quote level', function () {
394451
it('inserts selected quoted sample if you click the quote icon', function () {
395452
setVisualValue('')

0 commit comments

Comments
 (0)