Skip to content

Commit 89c6c60

Browse files
authored
Merge pull request #4 from Spearance/1.1.0
1.1.0
2 parents dd1aa97 + da53173 commit 89c6c60

File tree

5 files changed

+81
-50
lines changed

5 files changed

+81
-50
lines changed

FlyTypograf.js

Lines changed: 69 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
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

1616
export 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([123457]\/[234568])\b/g,
@@ -157,12 +183,12 @@ export class FlyTypograf {
157183
},
158184
{
159185
// Open quote
160-
pattern: /["»]([a-z0-9а-яё])/ig,
186+
pattern: /["»](\S)/ig,
161187
replace: `${this.#leftQuote}$1`
162188
},
163189
{
164190
// Close quote
165-
pattern: /([a-z0-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)

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) Evgeniy Lepeshkin, (https://spearance.ru)
3+
Copyright (c) 2021 Evgeniy Lepeshkin, (https://spearance.ru)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

example/example-min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ textarea.addEventListener(`keyup`, () => {
88
highLight.innerHTML = `<p>${textarea.value
99
.replace(/\u00a0/g, `<span class="hlg">&nbsp\;</span>`)
1010
11-
.replace(/([©®×«»½¼¾])/g, `<span class="hlb">$1</span>`)
11+
.replace(/([©®×«»½¼¾±])/g, `<span class="hlb">$1</span>`)
1212
1313
.replace(/\n{2,}/g, "</p><p>")
1414

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "FlyTypograf.js",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Афтотипографика текста в полях <input> и <textarea> на лету",
55
"main": "FlyTypograf.js",
66
"repository": {
@@ -12,6 +12,14 @@
1212
"email": "mail@spearance.ru",
1313
"url": "https://spearance.ru"
1414
},
15+
"keywords": [
16+
"correct",
17+
"text",
18+
"typograf",
19+
"autotypograf",
20+
"typograph",
21+
"page-proofs"
22+
],
1523
"license": "MIT",
1624
"bugs": {
1725
"url": "https://github.com/Spearance/FlyTypograf.js/issues"

0 commit comments

Comments
 (0)