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

Use the connectedCallback function to set button styles instead of the constructor #55

Merged
merged 5 commits into from
Jan 5, 2022
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
58 changes: 27 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,22 @@ class MarkdownButtonElement extends HTMLElement {
}

class MarkdownHeaderButtonElement extends MarkdownButtonElement {
constructor() {
super()

connectedCallback() {
const level = parseInt(this.getAttribute('level') || '3', 10)
this.#setLevelStyle(level)
}

static get observedAttributes() {
return ['level']
}

attributeChangedCallback(name: string, oldValue: string, newValue: string) {
if (name !== 'level') return
const level = parseInt(newValue || '3', 10)
this.#setLevelStyle(level)
}

#setLevelStyle(level: number) {
if (level < 1 || level > 6) {
return
}
Expand All @@ -132,8 +144,7 @@ if (!window.customElements.get('md-header')) {
}

class MarkdownBoldButtonElement extends MarkdownButtonElement {
constructor() {
super()
connectedCallback() {
styles.set(this, {prefix: '**', suffix: '**', trimFirst: true})
}
}
Expand All @@ -144,8 +155,7 @@ if (!window.customElements.get('md-bold')) {
}

class MarkdownItalicButtonElement extends MarkdownButtonElement {
constructor() {
super()
connectedCallback() {
styles.set(this, {prefix: '_', suffix: '_', trimFirst: true})
}
}
Expand All @@ -156,8 +166,7 @@ if (!window.customElements.get('md-italic')) {
}

class MarkdownQuoteButtonElement extends MarkdownButtonElement {
constructor() {
super()
connectedCallback() {
styles.set(this, {prefix: '> ', multiline: true, surroundWithNewlines: true})
}
}
Expand All @@ -168,8 +177,7 @@ if (!window.customElements.get('md-quote')) {
}

class MarkdownCodeButtonElement extends MarkdownButtonElement {
constructor() {
super()
connectedCallback() {
styles.set(this, {prefix: '`', suffix: '`', blockPrefix: '```', blockSuffix: '```'})
}
}
Expand All @@ -180,8 +188,7 @@ if (!window.customElements.get('md-code')) {
}

class MarkdownLinkButtonElement extends MarkdownButtonElement {
constructor() {
super()
connectedCallback() {
styles.set(this, {prefix: '[', suffix: '](url)', replaceNext: 'url', scanFor: 'https?://'})
}
}
Expand All @@ -192,8 +199,7 @@ if (!window.customElements.get('md-link')) {
}

class MarkdownImageButtonElement extends MarkdownButtonElement {
constructor() {
super()
connectedCallback() {
styles.set(this, {prefix: '![', suffix: '](url)', replaceNext: 'url', scanFor: 'https?://'})
}
}
Expand All @@ -204,8 +210,7 @@ if (!window.customElements.get('md-image')) {
}

class MarkdownUnorderedListButtonElement extends MarkdownButtonElement {
constructor() {
super()
connectedCallback() {
styles.set(this, {prefix: '- ', multiline: true, unorderedList: true})
}
}
Expand All @@ -216,8 +221,7 @@ if (!window.customElements.get('md-unordered-list')) {
}

class MarkdownOrderedListButtonElement extends MarkdownButtonElement {
constructor() {
super()
connectedCallback() {
styles.set(this, {prefix: '1. ', multiline: true, orderedList: true})
}
}
Expand All @@ -228,8 +232,7 @@ if (!window.customElements.get('md-ordered-list')) {
}

class MarkdownTaskListButtonElement extends MarkdownButtonElement {
constructor() {
super()
connectedCallback() {
styles.set(this, {prefix: '- [ ] ', multiline: true, surroundWithNewlines: true})
}
}
Expand All @@ -240,8 +243,7 @@ if (!window.customElements.get('md-task-list')) {
}

class MarkdownMentionButtonElement extends MarkdownButtonElement {
constructor() {
super()
connectedCallback() {
styles.set(this, {prefix: '@', prefixSpace: true})
}
}
Expand All @@ -252,8 +254,7 @@ if (!window.customElements.get('md-mention')) {
}

class MarkdownRefButtonElement extends MarkdownButtonElement {
constructor() {
super()
connectedCallback() {
styles.set(this, {prefix: '#', prefixSpace: true})
}
}
Expand All @@ -264,8 +265,7 @@ if (!window.customElements.get('md-ref')) {
}

class MarkdownStrikethroughButtonElement extends MarkdownButtonElement {
constructor() {
super()
connectedCallback() {
styles.set(this, {prefix: '~~', suffix: '~~', trimFirst: true})
}
}
Expand All @@ -276,10 +276,6 @@ if (!window.customElements.get('md-strikethrough')) {
}

class MarkdownToolbarElement extends HTMLElement {
constructor() {
super()
}

connectedCallback(): void {
if (!this.hasAttribute('role')) {
this.setAttribute('role', 'toolbar')
Expand Down
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,18 @@ describe('markdown-toolbar-element', function () {
})

describe('header', function () {
it('sets the level correctly even when dynamically created', function () {
const headerElement = document.createElement('md-header')
headerElement.setAttribute('level', '2')
headerElement.textContent = 'h2'
const toolbar = document.querySelector('markdown-toolbar')
toolbar.appendChild(headerElement)

setVisualValue('|title|')
clickToolbar('md-header[level="2"]')
assert.equal('## |title|', visualValue())
})

it('inserts header syntax with cursor in description', function () {
setVisualValue('|title|')
clickToolbar('md-header')
Expand All @@ -848,6 +860,15 @@ describe('markdown-toolbar-element', function () {
clickToolbar('md-header[level="10"]')
assert.equal('|title|', visualValue())
})

it('dynamically changes header level based on the level attribute', function () {
setVisualValue('|title|')
const headerButton = document.querySelector('md-header[level="1"]')
headerButton.setAttribute('level', '2')
headerButton.click()

assert.equal('## |title|', visualValue())
})
})
})
})