|
| 1 | +import './styles.css'; |
| 2 | +import SplitText from '../src/index.js'; |
| 3 | + |
| 4 | +document.addEventListener('DOMContentLoaded', () => { |
| 5 | + // Get DOM elements |
| 6 | + const inputText = document.getElementById('input-text'); |
| 7 | + const outputText = document.getElementById('output-text'); |
| 8 | + const originalText = document.getElementById('original-text'); |
| 9 | + const revertButton = document.getElementById('output-revert'); |
| 10 | + |
| 11 | + // Get option checkboxes |
| 12 | + const typeLines = document.getElementById('input-type-lines'); |
| 13 | + const typeWords = document.getElementById('input-type-words'); |
| 14 | + const typeChars = document.getElementById('input-type-chars'); |
| 15 | + const noAriaLabel = document.getElementById('input-no-aria-label'); |
| 16 | + const noBalance = document.getElementById('input-no-balance'); |
| 17 | + const handleCJK = document.getElementById('input-handle-cjk'); |
| 18 | + const cssBalance = document.getElementById('input-balance'); |
| 19 | + // Get font size input |
| 20 | + const fontSize = document.getElementById('input-font-size'); |
| 21 | + |
| 22 | + let splitInstance = null; |
| 23 | + |
| 24 | + // Helper to get current options |
| 25 | + const getOptions = () => { |
| 26 | + const types = []; |
| 27 | + if (typeLines.checked) types.push('lines'); |
| 28 | + if (typeWords.checked) types.push('words'); |
| 29 | + if (typeChars.checked) types.push('chars'); |
| 30 | + |
| 31 | + return { |
| 32 | + type: types.join(','), |
| 33 | + noAriaLabel: noAriaLabel.checked, |
| 34 | + noBalance: noBalance.checked, |
| 35 | + handleCJK: handleCJK.checked, |
| 36 | + }; |
| 37 | + }; |
| 38 | + |
| 39 | + // Update split text |
| 40 | + const updateSplit = () => { |
| 41 | + outputText.style.removeProperty('max-width'); |
| 42 | + |
| 43 | + if (cssBalance.checked) { |
| 44 | + outputText.style.setProperty('text-wrap', 'balance'); |
| 45 | + } else { |
| 46 | + outputText.style.removeProperty('text-wrap'); |
| 47 | + } |
| 48 | + |
| 49 | + const text = inputText.value.trim(); |
| 50 | + if (!text) return; |
| 51 | + |
| 52 | + // Store original text |
| 53 | + originalText.innerHTML = text; |
| 54 | + |
| 55 | + // Update output |
| 56 | + outputText.innerHTML = text; |
| 57 | + |
| 58 | + // Create new split instance |
| 59 | + splitInstance = new SplitText(outputText, getOptions()); |
| 60 | + }; |
| 61 | + |
| 62 | + const updateFontSize = () => { |
| 63 | + outputText.style.fontSize = `${fontSize.value}px`; |
| 64 | + updateSplit(); |
| 65 | + }; |
| 66 | + |
| 67 | + // Event listeners |
| 68 | + inputText.addEventListener('input', updateSplit); |
| 69 | + fontSize.addEventListener('input', updateFontSize); |
| 70 | + [typeLines, typeWords, typeChars, noAriaLabel, noBalance, handleCJK, cssBalance].forEach((checkbox) => { |
| 71 | + checkbox.addEventListener('change', updateSplit); |
| 72 | + }); |
| 73 | + |
| 74 | + // Revert button |
| 75 | + revertButton.addEventListener('click', () => { |
| 76 | + outputText.style.removeProperty('max-width'); |
| 77 | + |
| 78 | + if (splitInstance) { |
| 79 | + splitInstance.revert(); |
| 80 | + splitInstance = null; |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + window.addEventListener('resize', () => { |
| 85 | + updateSplit(); |
| 86 | + }); |
| 87 | + |
| 88 | + // Initial text |
| 89 | + inputText.value = [/*html*/ `<h1>split anything 🐳 🍔 🍕 into words, chars, lines</h1>`, /*html*/ `<p>Try typing some text here to see it split into lines, words, and characters!</p>`].join('\n'); |
| 90 | + |
| 91 | + updateSplit(); |
| 92 | +}); |
0 commit comments