@@ -16,24 +16,38 @@ const shouldBeClosingQuote = (text, indexCharBefore) => text[indexCharBefore] &&
16
16
17
17
const replaceQuote = ( range , index , quoteType ) => {
18
18
const startContainer = range . startContainer
19
+ if ( ! startContainer . nodeValue ) {
20
+ return
21
+ }
19
22
const textNode = document . createTextNode ( `${ startContainer . nodeValue . substring ( 0 , index ) } ${ quoteType } ${ startContainer . nodeValue . substring ( index + 1 ) } ` )
20
- range . startContainer . replaceWith ( textNode )
23
+ startContainer . replaceWith ( textNode )
21
24
return textNode
22
25
}
23
26
27
+ // TODO: Fix ‹Didn’t› case -> only works with timeout
28
+ const hasCharAfter = ( textArr , offset ) => {
29
+ console . log ( 'textArr :>> ' , textArr )
30
+ console . log ( 'offset :>> ' , offset )
31
+ return false
32
+ }
33
+
24
34
const hasSingleOpeningQuote = ( textArr , offset , singleOpeningQuote ) => {
25
35
if ( offset <= 0 ) {
26
36
return false
27
37
}
28
38
for ( let i = offset - 1 ; i >= 0 ; i -- ) {
29
39
if ( isSingleQuote ( textArr [ i ] ) && ( ! isApostrophe ( singleOpeningQuote ) && ! isApostrophe ( textArr [ i ] ) ) ) {
30
40
return textArr [ i ] === singleOpeningQuote
41
+ // TODO: keep on looking for an unclosed single opening quote -> need to save single opening and closing quotes
42
+ // if (textArr[i] === singleOpeningQuote) {
43
+ // return true
44
+ // }
31
45
}
32
46
}
33
47
return false
34
48
}
35
49
36
- export const applySmartQuotes = ( range , config , char , target ) => {
50
+ export const applySmartQuotes = ( range , config , char , target , carretOffset ) => {
37
51
const isCharSingleQuote = isSingleQuote ( char )
38
52
const isCharDoubleQuote = isDoubleQuote ( char )
39
53
@@ -47,14 +61,16 @@ export const applySmartQuotes = (range, config, char, target) => {
47
61
let newTextNode
48
62
49
63
if ( shouldBeClosingQuote ( textArr , offset - 2 ) ) {
50
- // Don't transform single-quote if there is no respective single-opening-quote
51
- if ( isCharSingleQuote && ! hasSingleOpeningQuote ( textArr , offset , singleQuotes [ 0 ] ) ) {
52
- return
64
+ if ( isCharSingleQuote ) {
65
+ // Don't transform apostrophes
66
+ if ( hasCharAfter ( textArr , offset ) ) {
67
+ return
68
+ }
69
+ // Don't transform single-quote if there is no respective single-opening-quote
70
+ if ( ! hasSingleOpeningQuote ( textArr , offset , singleQuotes [ 0 ] ) ) {
71
+ return
72
+ }
53
73
}
54
- // TODO: Fix ‹Didn’t› case -> only works with timeout
55
- // if (isCharSingleQuote && hasTextAfter(target, offset)) {
56
- // return
57
- // }
58
74
const closingQuote = isCharSingleQuote ? singleQuotes [ 1 ] : quotes [ 1 ]
59
75
newTextNode = replaceQuote ( range , offset - 1 , closingQuote )
60
76
} else if ( shouldBeOpeningQuote ( textArr , offset - 2 ) ) {
@@ -69,5 +85,7 @@ export const applySmartQuotes = (range, config, char, target) => {
69
85
// Resets the cursor
70
86
const window = target . ownerDocument . defaultView
71
87
const selection = window . getSelection ( )
72
- selection . collapse ( newTextNode , offset )
88
+ selection . collapse ( newTextNode , carretOffset ?? offset )
73
89
}
90
+
91
+ // TODO: fix special case when quickly typing two quotes after each other
0 commit comments