Install a shortcut to append selected text to a as a Markdown quote.</body></html>
Перейти к файлу
Keith Cirkel 080cfecaa4
upgrade typescript to latest
Co-authored-by: Keith Cirkel <keithamus@users.noreply.github.com>
2021-11-01 09:02:12 +00:00
.github/workflows Replace Babel + Flow with TypeScript 2020-04-10 11:50:58 -06:00
examples Replace Babel + Flow with TypeScript 2020-04-10 11:50:58 -06:00
src Escape specific html tags in P elements when quoting to markdown (#23) 2021-08-04 10:55:16 -07:00
test Escape specific html tags in P elements when quoting to markdown (#23) 2021-08-04 10:55:16 -07:00
.gitignore quoteSelection 2018-07-05 22:34:24 -04:00
.travis.yml quoteSelection 2018-07-05 22:34:24 -04:00
CODEOWNERS Create CODEOWNERS 2020-04-14 13:29:49 +01:00
LICENSE Update development dependencies 2020-04-10 09:21:35 -06:00
README.md Bring back copy Markdown to clipboard functionality 2019-04-16 21:46:35 +02:00
karma.config.js Replace Babel + Flow with TypeScript 2020-04-10 11:50:58 -06:00
package-lock.json upgrade typescript to latest 2021-11-01 09:02:12 +00:00
package.json upgrade typescript to latest 2021-11-01 09:02:12 +00:00
rollup.config.js Replace Babel + Flow with TypeScript 2020-04-10 11:50:58 -06:00
tsconfig.json Replace Babel + Flow with TypeScript 2020-04-10 11:50:58 -06:00

README.md

Quote selection

Install a keyboard shortcut r to append selected text to a <textarea> as a Markdown quote.

Installation

$ npm install @github/quote-selection

Usage

<div class="my-quote-region">
  <p>Text to quote</p>
  <textarea></textarea>
</div>
import {install} from '@github/quote-selection'

install(document.querySelector('.my-quote-region'))

This sets up a keyboard event handler so that selecting any text within .my-quote-region and pressing r appends the quoted representation of the selected text into the first applicable <textarea> element.

Preserving Markdown syntax

install(element, {
  quoteMarkdown: true,
  copyMarkdown: false,
  scopeSelector: '.comment-body'
})

The optional scopeSelector parameter ensures that even if the user selection bleeds outside of the scoped element, the quoted portion will always be contained inside the scope. This is useful to avoid accidentally quoting parts of the UI that might be interspersed between quotable content.

In copyMarkdown: true mode, the browser clipboard copy action is intercepted for user selections within element and the Markdown representation of the content is placed on clipboard under the text/x-gfm MIME-type.

Events

  • quote-selection-markdown (bubbles: true, cancelable: false) - fired on the quote region to optionally inject custom syntax into the fragment element in quoteMarkdown: true mode
  • quote-selection (bubbles: true, cancelable: true) - fired on the quote region before text is appended to a textarea

For example, reveal a textarea so it can be found:

region.addEventListener('quote-selection', function(event) {
  const {selection, selectionText} = event.detail
  console.log('Quoted text', selection, selectionText)

  const textarea = event.target.querySelector('textarea')
  textarea.hidden = false

  // Cancel the quote behavior.
  // event.preventDefault()
})

Development

npm install
npm test

License

Distributed under the MIT license. See LICENSE for details.