Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
4.1 kB
37
Indexable
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Text Converter Tool</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            width: 80%;
            max-width: 600px;
            text-align: center; /* Center align text and buttons */
        }

        textarea {
            width: 100%;
            height: 150px;
            padding: 10px;
            box-sizing: border-box;
            margin-bottom: 10px;
            resize: none;
        }

        button {
            padding: 10px 20px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }

        button:hover {
            background-color: #0056b3;
        }

        .copy-button {
            background-color: #28a745;
        }

        .copy-button:hover {
            background-color: #218838;
        }

        .paste-button {
            background-color: #17a2b8;
        }

        .paste-button:hover {
            background-color: #117a8b;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Titlecase Text Converter Tool</h1>
        <div class="text-box">
            <textarea id="text-box" placeholder="Enter your text here..."></textarea>
            <button onclick="convertToTitleCase()">Convert to Title Case</button>
            <button class="copy-button" onclick="copyToClipboard()">Copy</button>
            <button class="paste-button" onclick="pasteFromClipboard()">Paste</button>
        </div>
    </div>
    <script>
        function convertToTitleCase() {
            const textBox = document.getElementById('text-box');
            const text = textBox.value;

            // Define common words that are usually lowercase in title case
            const lowerCaseWords = ['a', 'an', 'and', 'as', 'at', 'but', 'by', 'for', 'if', 'in', 'of', 'on', 'or', 'the', 'to', 'up', 'yet'];

            const words = text.split(' ');
            const titleCasedText = words.map((word, index) => {
                // Always capitalize the first word and any word after punctuation
                if (index === 0 || /[.!?]$/.test(words[index - 1])) {
                    return capitalize(word);
                }
                // Capitalize words not in the lowercase list
                if (!lowerCaseWords.includes(word.toLowerCase())) {
                    return capitalize(word);
                }
                // Return lowercase for common words
                return word.toLowerCase();
            }).join(' ');

            textBox.value = titleCasedText;
        }

        function capitalize(word) {
            return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
        }

        function copyToClipboard() {
            const textBox = document.getElementById('text-box');
            textBox.select();
            textBox.setSelectionRange(0, 99999); // For mobile devices
            document.execCommand('copy');
            alert('Copied to clipboard!');
        }

        function pasteFromClipboard() {
            const textBox = document.getElementById('text-box');
            navigator.clipboard.readText().then(text => {
                textBox.value = text;
            }).catch(err => {
                console.error('Failed to read clipboard contents: ', err);
            });
        }
    </script>
</body>
</html>
Leave a Comment