Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commited4f54d

Browse files
khiga8keithamus
andcommitted
Remove keyboard event listener,quoteSelection method.
BREAKING CHANGEThe code for handling eventlistening is very trivial, and might vary perconsumer of this library. It is not core functionality, and should beleft to upstream to setup.Removing this reduces the API surface area and complexity from this libraryas it doesn't need to concern itself with event management and keyboardshortcuts.Co-authored-by: Keith Cirkel <keithamus@users.noreply.github.com>
1 parent6c38592 commited4f54d

File tree

2 files changed

+17
-38
lines changed

2 files changed

+17
-38
lines changed

‎README.md‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Quote selection
22

3-
Install a keyboard shortcut <kbd>r</kbd> to append selectedtextto a`<textarea>` as a Markdown quote.
3+
Helpers for quoting selected text, appending thetextinto a`<textarea>` as a Markdown quote.
44

55
##Installation
66

@@ -18,12 +18,18 @@ $ npm install @github/quote-selection
1818
```
1919

2020
```js
21-
import {install}from'@github/quote-selection'
21+
import {install,getSelectionContext,quote}from'@github/quote-selection'
2222

2323
install(document.querySelector('.my-quote-region'))
24+
25+
document.addEventListener('keydown',event=> {
26+
if (event.key=='r') {
27+
quote(getSelectionContext())
28+
}
29+
})
2430
```
2531

26-
This sets up a keyboard event handler so that selecting any text within`.my-quote-region` and pressing <kbd>r</kbd> appends the quoted representation of the selected text into the first applicable`<textarea>` element.
32+
Calling`quote` with`getSelectionContext` will take the currently selected HTML, converts it to markdown, and appends the quoted representation of the selected text into the first applicable`<textarea>` element.
2733

2834
###Preserving Markdown syntax
2935

@@ -33,6 +39,11 @@ install(element, {
3339
copyMarkdown:false,
3440
scopeSelector:'.comment-body'
3541
})
42+
43+
quote(getSelectionContext(), {
44+
quoteMarkdown:true,
45+
scopeSelector:'.comment-body'
46+
})
3647
```
3748

3849
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.
@@ -41,13 +52,13 @@ In `copyMarkdown: true` mode, the browser clipboard copy action is intercepted f
4152

4253
##Events
4354

44-
*`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
45-
*`quote-selection` (bubbles: true, cancelable: true) - fired on the quote region before text is appended to a textarea
55+
-`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
56+
-`quote-selection` (bubbles: true, cancelable: true) - fired on the quote region before text is appended to a textarea
4657

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

4960
```js
50-
region.addEventListener('quote-selection',function(event) {
61+
region.addEventListener('quote-selection',function(event) {
5162
const {selection,selectionText}=event.detail
5263
console.log('Quoted text', selection, selectionText)
5364

‎src/index.ts‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import {extractFragment, insertMarkdownSyntax} from './markdown'
22

33
constcontainers:WeakMap<Element,Options>=newWeakMap()
44

5-
letfirstInstall=true
6-
75
typeOptions={
86
quoteMarkdown:boolean
97
copyMarkdown:boolean
@@ -36,13 +34,6 @@ export function install(container: Element, options?: Partial<Options>) {
3634
options
3735
)
3836
containers.set(container,config)
39-
if(firstInstall){
40-
document.addEventListener('keydown',(e:KeyboardEvent)=>quoteSelection(e,config),{signal:options?.signal})
41-
options?.signal?.addEventListener('abort',()=>{
42-
firstInstall=true
43-
})
44-
firstInstall=false
45-
}
4637
if(config.copyMarkdown){
4738
;(containerasHTMLElement).addEventListener('copy',(e:ClipboardEvent)=>onCopy(e,config),{
4839
signal:options?.signal
@@ -74,18 +65,6 @@ function onCopy(event: ClipboardEvent, options: Partial<Options>) {
7465
selection.addRange(selectionContext.range)
7566
}
7667

77-
functioneventIsNotRelevant(event:KeyboardEvent):boolean{
78-
return(
79-
event.defaultPrevented||
80-
event.key!=='r'||
81-
event.metaKey||
82-
event.altKey||
83-
event.shiftKey||
84-
event.ctrlKey||
85-
(event.targetinstanceofHTMLElement&&isFormField(event.target))
86-
)
87-
}
88-
8968
exportfunctionfindContainer(el:Element):Element|undefined{
9069
letparent:Element|null=el
9170
while((parent=parent.parentElement)){
@@ -103,17 +82,6 @@ export function findTextarea(container: Element): HTMLTextAreaElement | undefine
10382
}
10483
}
10584

106-
exportfunctionquoteSelection(event:KeyboardEvent,options:Partial<Options>):void{
107-
if(eventIsNotRelevant(event))return
108-
109-
constselection=getSelectionContext()
110-
if(!selection)return
111-
112-
if(quote(selection,options)){
113-
event.preventDefault()
114-
}
115-
}
116-
11785
exportfunctionquote(selectionContext:SelectionContext,options:Partial<Options>):boolean{
11886
constquoted=extractQuote(selectionContext,false,options)
11987
if(!quoted)returnfalse

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp