Skip to content

Commit df795a6

Browse files
committed
feat: implement hasCharAfter for edge case apostrophe in word
1 parent 157cb46 commit df795a6

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

src/smartQuotes.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ export const shouldApplySmartQuotes = (config, target) => {
88
const isDoubleQuote = (char) => /^[«»"]$/.test(char)
99
const isSingleQuote = (char) => /^[']$/.test(char)
1010
const isApostrophe = (char) => /^[']$/.test(char)
11+
const isWhitespace = (char) => /^\s$/.test(char)
1112

12-
// TODO: Test with: '>', ' ', no space & all kinds of dashes dash
13-
// edge case: applied tooltip quotes and then inserted single quote after space
1413
const shouldBeOpeningQuote = (text, indexCharBefore) => indexCharBefore < 0 || /\s|[>\-]/.test(text[indexCharBefore])
15-
const shouldBeClosingQuote = (text, indexCharBefore) => text[indexCharBefore] && !/\s/.test(text[indexCharBefore])
14+
const shouldBeClosingQuote = (text, indexCharBefore) => !!text[indexCharBefore] && !isWhitespace(text[indexCharBefore])
15+
const hasCharAfter = (textArr, indexCharAfter) => !!textArr[indexCharAfter] && !isWhitespace(textArr[indexCharAfter])
1616

1717
const replaceQuote = (range, index, quoteType) => {
1818
const startContainer = range.startContainer
@@ -24,24 +24,13 @@ const replaceQuote = (range, index, quoteType) => {
2424
return textNode
2525
}
2626

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-
3427
const hasSingleOpeningQuote = (textArr, offset, singleOpeningQuote) => {
3528
if (offset <= 0) {
3629
return false
3730
}
3831
for (let i = offset - 1; i >= 0; i--) {
3932
if (isSingleQuote(textArr[i]) && (!isApostrophe(singleOpeningQuote) && !isApostrophe(textArr[i]))) {
4033
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-
// }
4534
}
4635
}
4736
return false
@@ -82,10 +71,9 @@ export const applySmartQuotes = (range, config, char, target, carretOffset) => {
8271
return
8372
}
8473

85-
// Resets the cursor
74+
// Resets the cursor to the currentPosition after applying the smart-quote
8675
const window = target.ownerDocument.defaultView
8776
const selection = window.getSelection()
8877
selection.collapse(newTextNode, carretOffset ?? offset)
8978
}
9079

91-
// TODO: fix special case when quickly typing two quotes after each other

0 commit comments

Comments
 (0)