99 Released under the MIT license.
1010 http://www.opensource.org/licenses/mit-license.php
1111
12- Version: v 1.0
13- Date: Aug 26 , 2021
12+ Version: v 1.1. 0
13+ Date: Dec 24 , 2021
1414 */
1515
1616export class FlyTypograf {
@@ -39,34 +39,68 @@ export class FlyTypograf {
3939 "7/8" : `⅞`
4040 }
4141
42- #rules = [
42+ #prepare = [
4343 {
44- // remove multiply space
45- pattern : / [ \u00A0 \u202F ] + / g,
44+ // Remove non-breaking space with simple space
45+ pattern : / \u00A0 | \u00A0 / g,
46+ replace : ` `
47+ } ,
48+ {
49+ // Remove thin non-breaking space with simple space
50+ pattern : / \u202F | \u202F / g,
4651 replace : ` `
4752 } ,
4853 {
49- // Minus sign
50- pattern : / - ( \d ) / g,
51- replace : ` −$1 `
54+ // Remove multiply non-breaking spaces
55+ pattern : / [ \u00A0 \u202F ] + / g,
56+ replace : ` `
5257 } ,
5358 {
54- // Dash sign
55- pattern : / ( ^ | \n | \s | > ) \- ( \s ) / g,
56- replace : `$1—$2`
59+ // Remove dashes and minuses
60+ pattern : / [ − – ] / g,
61+ replace : `-`
62+ }
63+ ]
64+
65+ #process = [
66+ {
67+ // Minus sign
68+ pattern : / (?< = | ^ ) [ — - ] ( \d ) / g,
69+ replace : `−$1`
5770 } ,
5871 {
5972 // Double hyphen
60- pattern : / (?< ! [ - ! ] ) - { 2 } / g,
73+ pattern : / (?< ! [ ! ] ) - - (? ! > ) / g,
6174 replace : ( ) => {
6275 this . #caretPosition--
63- return `— `
76+ return `- `
6477 }
6578 } ,
6679 {
67- // Multiple spaces
68- pattern : / \u00A0 | \u00A0 / g,
69- replace : ` `
80+ // Plus/Minus +/-
81+ pattern : / \+ \/ \- / g,
82+ replace : ( ) => {
83+ this . #caretPosition -= 2
84+ return `±`
85+ }
86+ } ,
87+ {
88+ // Dash sign
89+ pattern : / (?< = | ^ | > | [ ^ - ! а - я ё a - z ] ) - (? = | $ | [ ^ - ] ) / gmi,
90+ replace : `—`
91+ } ,
92+ {
93+ // Non-breaking space with dash sign
94+ pattern : / (?< ! ^ | [ " : ; . ! ? … , ] ) — (? ! - ) / gm,
95+ replace : `\u00A0—`
96+ } ,
97+ {
98+ // Dash sign with non-breaking space
99+ pattern : / ( [ ] + ) — ( [ ] * ?) ( [ a - z а - я ё 0 - 9 ] ) / gmi,
100+ replace : ( str , $1 , $2 , $3 ) => {
101+ this . #caretPosition -= ( $1 . length ? $1 . length - 1 : 0 ) + ( $2 . length ? $2 . length - 1 : 0 )
102+ return ` —\u00A0${ $3 } `
103+ }
70104 } ,
71105 {
72106 // Numerical interval
@@ -129,14 +163,6 @@ export class FlyTypograf {
129163 pattern : / ( \d ) [ x х ] ( \d ) / ig,
130164 replace : `$1×$2`
131165 } ,
132- {
133- // Plus/Minus +/-
134- pattern : / \+ \/ \- / g,
135- replace : ( ) => {
136- this . #caretPosition -= 2
137- return `±`
138- }
139- } ,
140166 {
141167 // Decimals like 1/2
142168 pattern : / \b ( [ 1 2 3 4 5 7 ] \/ [ 2 3 4 5 6 8 ] ) \b / g,
@@ -157,12 +183,12 @@ export class FlyTypograf {
157183 } ,
158184 {
159185 // Open quote
160- pattern : / [ " » ] ( [ a - z 0 - 9 а - я ё … ] ) / ig,
186+ pattern : / [ " » ] ( \S ) / ig,
161187 replace : `${ this . #leftQuote} $1`
162188 } ,
163189 {
164190 // Close quote
165- pattern : / ( [ a - z 0 - 9 а - я ё … ? ! ] ) [ " « ] / ig,
191+ pattern : / ( \S ) [ " « ] / ig,
166192 replace : `$1${ this . #rightQuote} `
167193 } ,
168194 {
@@ -212,9 +238,14 @@ export class FlyTypograf {
212238 }
213239 ] ;
214240
215- constructor ( textElement ) {
241+ constructor ( textElement , preference ) {
216242 this . _element = textElement
217243 this . _isContentEditable = this . _element . contentEditable === true
244+
245+ if ( preference ) {
246+ this . #leftQuote = preference . leftQuote || `«`
247+ this . #rightQuote = preference . rightQuote || `«`
248+ }
218249 }
219250
220251 get result ( ) {
@@ -224,32 +255,24 @@ export class FlyTypograf {
224255 process ( ) {
225256 this . #result = this . _element . value
226257
227- this . _getCaretPosition ( )
258+ this . #applyRules ( this . #prepare )
228259
229- this . #rules . forEach ( ( regex ) => {
230- this . #result = this . #result . replace ( regex . pattern , regex . replace )
231- } )
260+ this . #getCaretPosition ( )
261+
262+ this . #applyRules ( this . #process )
232263
233264 this . _element . value = this . #result
234265
235- this . _setCaretPosition ( this . #caretPosition)
266+ this . #setCaretPosition ( this . #caretPosition)
236267 }
237268
238- _getSelectionText ( ) {
239- if ( window . getSelection ) {
240- return window . getSelection ( )
241- }
242-
243- if ( document . getSelection ) {
244- return document . getSelection ( )
245- }
246-
247- if ( document . selection ) {
248- return document . selection . createRange ( ) . text
249- }
269+ #applyRules ( array ) {
270+ array . forEach ( ( regex ) => {
271+ this . #result = this . #result. replace ( regex . pattern , regex . replace )
272+ } )
250273 }
251274
252- _getCaretPosition ( ) {
275+ #getCaretPosition ( ) {
253276 if ( this . _isContentEditable ) {
254277 this . _element . focus ( )
255278 let _range = document . getSelection ( ) . getRangeAt ( 0 )
@@ -262,7 +285,7 @@ export class FlyTypograf {
262285 this . #caretPosition = this . _element . selectionStart
263286 }
264287
265- _setCaretPosition ( pos ) {
288+ #setCaretPosition ( pos ) {
266289 if ( this . _isContentEditable ) {
267290 this . _element . focus ( )
268291 document . getSelection ( ) . collapse ( this . _element , pos )
0 commit comments