|
9 | 9 | */ |
10 | 10 | function updateWordCount(element) { |
11 | 11 | const currentCount = parseInt(element.innerText.match(/\d+/)?.[0], 10) || 0; |
12 | | - const limit = parseInt( |
13 | | - element.closest("[data-word-limit]").dataset.wordLimit, |
14 | | - 10 |
15 | | - ); |
| 12 | + const fieldset = element.closest("[data-word-limit]"); |
| 13 | + const limit = parseInt(fieldset.dataset.wordLimit, 10); |
16 | 14 | const warningThreshold = limit * WARNING_THRESHOLD; |
| 15 | + const textCharacters = fieldset.dataset.wordCountCharacters || "characters"; |
| 16 | + const textOutOf = fieldset.dataset.wordCountOutOf || "out of"; |
| 17 | + const textClose = fieldset.dataset.wordCountClose || "(Close to the limit)"; |
| 18 | + const textOver = fieldset.dataset.wordCountOver || "(Over the limit)"; |
17 | 19 |
|
18 | 20 | /** |
19 | 21 | * Clear warning states and classes |
|
23 | 25 | element.classList.remove("word-count-warning", "word-count-warning-2"); |
24 | 26 | } |
25 | 27 |
|
26 | | - if (element.textContent.includes("characters")) { |
| 28 | + if (element.textContent.includes(textCharacters)) { |
27 | 29 | clearWarnings(); |
28 | 30 | return; |
29 | 31 | } |
30 | 32 |
|
31 | | - element.dataset.afterWordCount = ` out of ${limit}`; |
| 33 | + element.dataset.afterWordCount = ` ${textOutOf} ${limit}`; |
32 | 34 |
|
33 | 35 | if (currentCount <= warningThreshold) { |
34 | 36 | clearWarnings(); |
35 | 37 | } else if (currentCount <= limit) { |
36 | | - element.dataset.afterWordCount += " (Close to the limit)"; |
| 38 | + element.dataset.afterWordCount += ` ${textClose}`; |
37 | 39 | element.classList.remove("word-count-warning-2"); |
38 | 40 | element.classList.add("word-count-warning"); |
39 | 41 | } else { |
40 | | - element.dataset.afterWordCount += " (Over the limit)"; |
| 42 | + element.dataset.afterWordCount += ` ${textOver}`; |
41 | 43 | element.classList.add("word-count-warning-2"); |
42 | 44 | } |
43 | 45 | } |
|
0 commit comments