diff --git a/src/index.ts b/src/index.ts index b90b506..4e8776f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,10 @@ export class Quote { return startElement.closest(selector) } + get active(): boolean { + return (this.selection?.rangeCount || 0) > 0 + } + get range(): Range { if (!this.selection || !this.selection.rangeCount) return new Range() return this.selection.getRangeAt(0) diff --git a/test/test.js b/test/test.js index 570ffe5..091b1f5 100644 --- a/test/test.js +++ b/test/test.js @@ -40,6 +40,7 @@ describe('quote-selection', function () { changeCount++ }) const quote = new Quote() + assert.ok(quote.active) assert.ok(quote.closest('[data-quote], [data-nested-quote]')) quote.insert(textarea) @@ -57,6 +58,7 @@ describe('quote-selection', function () { textarea.hidden = false const quote = new Quote() + assert.ok(quote.active) assert.ok(quote.closest('[data-quote], [data-nested-quote]')) quote.insert(textarea) @@ -71,8 +73,15 @@ describe('quote-selection', function () { const quote = new Quote() + assert.ok(quote.active) assert.equal(quote.closest('[data-quote], [data-nested-quote]'), null) }) + + it('is not active if nothing is selected', function () { + window.getSelection().removeAllRanges() + const quote = new Quote() + assert.notOk(quote.active) + }) }) describe('with markdown enabled', function () { @@ -132,6 +141,7 @@ describe('quote-selection', function () { fragment.querySelector('img[alt]').replaceWith(':emoji:') }) quote.select(document.querySelector('.comment-body')) + assert.ok(quote.active) assert.ok(quote.closest('[data-quote]')) const textarea = document.querySelector('textarea')