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

Commita1e818f

Browse files
committed
RemovegetSelectionContext() argument, addquoteElement.
We can remove `getSelectionContext` entirely, by inlining it into the`extractQuote` body, however it was useful to pass in a custom selectioncontext for the use case of selecting the contents of an element.By introducing `quoteElement` as an option we can supply the single usecase for creating custom `SelectionContext`s, which also can replacesome code we have upstream.
1 parent5e36fa5 commita1e818f

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

‎README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ $ npm install @github/quote-selection
1818
```
1919

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

2323
document.addEventListener('keydown',event=> {
2424
if (event.key=='r') {
25-
constquote=extractQuote(getSelectionContext(),{containerSelector:'.my-quote-region'})
25+
constquote=extractQuote({containerSelector:'.my-quote-region'})
2626
if (quote) {
2727
insertQuote(quote.selectionText,document.querySelector('textarea'))
2828
}
@@ -37,7 +37,7 @@ document.addEventListener('keydown', event => {
3737
###Preserving Markdown syntax
3838

3939
```js
40-
extractQuote(getSelectionContext(),{
40+
extractQuote({
4141
quoteMarkdown:true,
4242
scopeSelector:'.comment-body',
4343
containerSelector:'.my-quote-region'

‎src/index.ts‎

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,31 @@ type Options = {
44
quoteMarkdown:boolean
55
scopeSelector:string
66
containerSelector:string
7+
quoteElement:Element
78
}
89

9-
interfaceSelectionContext{
10-
text:string
11-
range:Range
10+
typeQuote={
11+
container:Element
12+
selectionText:string
1213
}
1314

14-
exportfunctiongetSelectionContext():SelectionContext|null{
15+
exportfunctionextractQuote(options:Partial<Options>):Quote|undefined{
1516
constselection=window.getSelection()
16-
if(!selection)returnnull
17+
if(!selection)return
18+
if(options?.quoteElement){
19+
selection.removeAllRanges()
20+
selection.selectAllChildren(options.quoteElement)
21+
}
22+
letrange
1723
try{
18-
return{text:selection.toString(),range:selection.getRangeAt(0)}
24+
range=selection.getRangeAt(0)
1925
}catch{
20-
returnnull
26+
return
2127
}
22-
}
23-
24-
typeQuote={
25-
container:Element
26-
selectionText:string
27-
}
28-
29-
exportfunctionextractQuote(selectionContext:SelectionContext,options:Partial<Options>):Quote|undefined{
30-
letselectionText=selectionContext.text.trim()
28+
letselectionText=selection.toString().trim()
3129
if(!selectionText)return
3230

33-
letfocusNode:Node|null=selectionContext.range.startContainer
31+
letfocusNode:Node|null=range.startContainer
3432
if(!focusNode)return
3533

3634
if(focusNode.nodeType!==Node.ELEMENT_NODE)focusNode=focusNode.parentNode
@@ -43,12 +41,12 @@ export function extractQuote(selectionContext: SelectionContext, options: Partia
4341

4442
if(options?.quoteMarkdown){
4543
try{
46-
constfragment=extractFragment(selectionContext.range,options.scopeSelector??'')
44+
constfragment=extractFragment(range,options.scopeSelector??'')
4745
container.dispatchEvent(
4846
newCustomEvent('quote-selection-markdown',{
4947
bubbles:true,
5048
cancelable:false,
51-
detail:{fragment,range:selectionContext.range}
49+
detail:{fragment, range}
5250
})
5351
)
5452
insertMarkdownSyntax(fragment)

‎test/test.js‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import{getSelectionContext,extractQuote,insertQuote}from'../dist/index.js'
1+
import{extractQuote,insertQuote}from'../dist/index.js'
22

33
functioncreateSelection(selection,el){
44
constrange=document.createRange()
@@ -39,7 +39,7 @@ describe('quote-selection', function () {
3939
textarea.addEventListener('change',function(){
4040
changeCount++
4141
})
42-
constquote=extractQuote(getSelectionContext(),{containerSelector:'[data-quote], [data-nested-quote]'})
42+
constquote=extractQuote({containerSelector:'[data-quote], [data-nested-quote]'})
4343
insertQuote(quote.selectionText,textarea)
4444

4545
assert.equal(textarea.value,'Has text\n\n> Test Quotable text, bold.\n\n')
@@ -55,7 +55,7 @@ describe('quote-selection', function () {
5555

5656
textarea.hidden=false
5757

58-
constquote=extractQuote(getSelectionContext(),{containerSelector:'[data-quote], [data-nested-quote]'})
58+
constquote=extractQuote({containerSelector:'[data-quote], [data-nested-quote]'})
5959
insertQuote(quote.selectionText,textarea)
6060

6161
assert.equal(outerTextarea.value,'Has text')
@@ -67,7 +67,7 @@ describe('quote-selection', function () {
6767
constselection=window.getSelection()
6868
window.getSelection=()=>createSelection(selection,el)
6969

70-
constquote=extractQuote(getSelectionContext(),{containerSelector:'[data-quote], [data-nested-quote]'})
70+
constquote=extractQuote({containerSelector:'[data-quote], [data-nested-quote]'})
7171

7272
assert.equal(quote,undefined)
7373
})
@@ -98,14 +98,12 @@ describe('quote-selection', function () {
9898
})
9999

100100
it('preserves formatting',function(){
101-
constrange=document.createRange()
102-
range.selectNodeContents(document.querySelector('.comment-body').parentNode)
103-
constquote=extractQuote({text:'whatever', range},{
101+
constquote=extractQuote({
104102
quoteMarkdown:true,
105103
scopeSelector:'.comment-body',
106-
containerSelector:'[data-quote]'
104+
containerSelector:'[data-quote]',
105+
quoteElement:document.querySelector('.comment-body')
107106
})
108-
assert.ok(quote)
109107
consttextarea=document.querySelector('textarea')
110108
insertQuote(quote.selectionText,textarea)
111109

@@ -136,14 +134,12 @@ describe('quote-selection', function () {
136134
fragment.querySelector('img[alt]').replaceWith(':emoji:')
137135
})
138136

139-
constrange=document.createRange()
140-
range.selectNodeContents(document.querySelector('.comment-body').parentNode)
141-
constquote=extractQuote({text:'whatever', range},{
137+
constquote=extractQuote({
142138
quoteMarkdown:true,
143139
scopeSelector:'.comment-body',
144140
containerSelector:'[data-quote]',
141+
quoteElement:document.querySelector('.comment-body')
145142
})
146-
assert.ok(quote)
147143

148144
consttextarea=document.querySelector('textarea')
149145
insertQuote(quote.selectionText,textarea)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp