-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (96 loc) · 3.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import './styles.css';
import SplitText from '../src/index.js';
document.addEventListener('DOMContentLoaded', () => {
// Get DOM elements
const inputText = document.getElementById('input-text');
const outputText = document.getElementById('output-text');
const originalText = document.getElementById('original-text');
const revertButton = document.getElementById('output-revert');
// Get option checkboxes
const typeLines = document.getElementById('input-type-lines');
const typeWords = document.getElementById('input-type-words');
const typeChars = document.getElementById('input-type-chars');
const noAriaLabel = document.getElementById('input-no-aria-label');
const noBalance = document.getElementById('input-no-balance');
const handleCJT = document.getElementById('input-handle-cjt');
const cssBalance = document.getElementById('input-balance');
const balanceRatio = document.getElementById('input-balance-ratio');
const minLines = document.getElementById('input-min-lines');
const lineThreshold = document.getElementById('input-line-threshold');
// Get font size input
const fontSize = document.getElementById('input-font-size');
let splitInstance = null;
// Helper to get current options
const getOptions = () => {
const types = [];
if (typeLines.checked) types.push('lines');
if (typeWords.checked) types.push('words');
if (typeChars.checked) types.push('chars');
if (cssBalance.checked) {
noBalance.setAttribute('disabled', 'disabled');
balanceRatio.setAttribute('disabled', 'disabled');
} else {
noBalance.removeAttribute('disabled');
balanceRatio.removeAttribute('disabled');
}
return {
type: types.join(','),
noAriaLabel: noAriaLabel.checked,
noBalance: noBalance.checked,
handleCJT: handleCJT.checked,
balanceRatio: Number(balanceRatio.value),
minLines: Number(minLines.value),
lineThreshold: Number(lineThreshold.value),
};
};
// Update split text
const updateSplit = () => {
outputText.style.removeProperty('max-width');
if (cssBalance.checked) {
outputText.style.setProperty('text-wrap', 'balance');
} else {
outputText.style.removeProperty('text-wrap');
}
const text = inputText.value.trim();
if (!text) return;
// Store original text
originalText.innerHTML = text;
// Update output
outputText.innerHTML = text;
// Create new split instance
splitInstance = new SplitText(outputText, getOptions());
};
const updateFontSize = () => {
outputText.style.fontSize = `${fontSize.value}px`;
updateSplit();
};
// Event listeners
inputText.addEventListener('input', updateSplit);
fontSize.addEventListener('input', updateFontSize);
[typeLines, typeWords, typeChars, noAriaLabel, noBalance, handleCJT, cssBalance, balanceRatio, minLines, lineThreshold].forEach((checkbox) => {
checkbox.addEventListener('change', updateSplit);
});
// Revert button
revertButton.addEventListener('click', () => {
outputText.style.removeProperty('max-width');
if (splitInstance) {
splitInstance.revert();
splitInstance = null;
}
});
window.addEventListener('resize', () => {
updateSplit();
});
// Initial text
inputText.value = /* html */ `
<h1>split anything 🐳 🍔 🍕 into words, chars, lines</h1>
<p>Try typing some text to see it split into lines, words, and characters!</p>
<p> Link <a href="https://www.google.com">here</a></p>
<ul>
<li>pizza <b>margherita</b></li>
<li>hamburger</li>
<li>taco</li>
</ul>
`.trim();
updateSplit();
});