<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Advanced Text Indenter Pro</title>
<style>
:root {
--bg-primary: #1a1a1a;
--bg-secondary: #2a2a2a;
--bg-tertiary: #333333;
--text-primary: #e0e0e0;
--text-secondary: #b0b0b0;
--accent: #007acc;
--accent-hover: #005a99;
--accent-active: #004477;
--success: #28a745;
--danger: #dc3545;
--warning: #ffc107;
--border: #444;
}
* {
box-sizing: border-box;
}
body {
background-color: var(--bg-primary);
color: var(--text-primary);
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
flex-direction: column;
}
header {
background-color: var(--bg-secondary);
padding: 15px 20px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
position: sticky;
top: 0;
z-index: 100;
}
.header-content {
max-width: 1400px;
margin: 0 auto;
display: flex;
justify-content: space-between;
align-items: center;
}
h1 {
margin: 0;
font-size: 1.5rem;
color: #ffffff;
}
.header-controls {
display: flex;
gap: 10px;
align-items: center;
}
main {
flex: 1;
max-width: 1400px;
width: 100%;
margin: 0 auto;
padding: 20px;
}
.controls-panel {
background-color: var(--bg-secondary);
padding: 20px;
border-radius: 8px;
margin-bottom: 20px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
}
.controls-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
margin-bottom: 15px;
}
.control-group {
display: flex;
flex-direction: column;
gap: 8px;
}
.control-group label {
font-weight: 500;
color: var(--text-secondary);
font-size: 0.9rem;
}
.control-row {
display: flex;
gap: 10px;
align-items: center;
}
.editor-container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
height: 600px;
}
@media (max-width: 968px) {
.editor-container {
grid-template-columns: 1fr;
height: auto;
}
.editor-section {
height: 400px !important;
}
}
.editor-section {
background-color: var(--bg-secondary);
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
display: flex;
flex-direction: column;
overflow: hidden;
}
.editor-header {
background-color: var(--bg-tertiary);
padding: 10px 15px;
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid var(--border);
}
.editor-title {
font-weight: 600;
display: flex;
align-items: center;
gap: 10px;
}
.editor-actions {
display: flex;
gap: 5px;
}
.editor-wrapper {
flex: 1;
position: relative;
overflow: hidden;
}
.line-numbers {
position: absolute;
left: 0;
top: 0;
bottom: 0;
width: 50px;
background-color: var(--bg-tertiary);
color: var(--text-secondary);
padding: 10px 5px;
font-family: 'Courier New', Courier, monospace;
font-size: 14px;
line-height: 1.5;
text-align: right;
overflow: hidden;
user-select: none;
}
textarea {
width: 100%;
height: 100%;
background-color: var(--bg-primary);
color: var(--text-primary);
border: none;
padding: 10px;
padding-left: 60px;
font-family: 'Courier New', Courier, monospace;
font-size: 14px;
line-height: 1.5;
resize: none;
outline: none;
}
textarea.no-line-numbers {
padding-left: 10px;
}
.indent-preview {
position: absolute;
left: 60px;
top: 10px;
pointer-events: none;
font-family: 'Courier New', Courier, monospace;
font-size: 14px;
line-height: 1.5;
color: rgba(0, 122, 204, 0.3);
white-space: pre;
overflow: hidden;
max-width: calc(100% - 70px);
}
button {
background-color: var(--accent);
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
font-weight: 500;
transition: background-color 0.2s;
}
button:hover {
background-color: var(--accent-hover);
}
button:active {
background-color: var(--accent-active);
}
button.small {
padding: 5px 10px;
font-size: 12px;
}
button.success {
background-color: var(--success);
}
button.danger {
background-color: var(--danger);
}
button.secondary {
background-color: var(--bg-tertiary);
}
button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
select, input[type="number"], input[type="text"] {
background-color: var(--bg-tertiary);
color: var(--text-primary);
border: 1px solid var(--border);
padding: 6px 10px;
border-radius: 4px;
font-size: 14px;
outline: none;
}
select:focus, input:focus {
border-color: var(--accent);
}
input[type="range"] {
width: 100%;
-webkit-appearance: none;
appearance: none;
height: 6px;
background: var(--border);
border-radius: 3px;
outline: none;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 18px;
height: 18px;
background: var(--accent);
border-radius: 50%;
cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
width: 18px;
height: 18px;
background: var(--accent);
border-radius: 50%;
cursor: pointer;
border: none;
}
input[type="checkbox"] {
width: 16px;
height: 16px;
cursor: pointer;
}
.stats-bar {
background-color: var(--bg-tertiary);
padding: 8px 15px;
display: flex;
gap: 20px;
font-size: 12px;
color: var(--text-secondary);
border-top: 1px solid var(--border);
}
.stat-item {
display: flex;
gap: 5px;
}
.stat-label {
font-weight: 500;
}
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.7);
z-index: 1000;
justify-content: center;
align-items: center;
}
.modal-content {
background-color: var(--bg-secondary);
padding: 30px;
border-radius: 8px;
max-width: 500px;
width: 90%;
max-height: 80vh;
overflow-y: auto;
}
.modal-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
}
.modal-header h2 {
margin: 0;
}
.shortcuts-grid {
display: grid;
gap: 10px;
}
.shortcut-item {
display: flex;
justify-content: space-between;
padding: 8px;
background-color: var(--bg-tertiary);
border-radius: 4px;
}
.shortcut-key {
font-family: monospace;
background-color: var(--bg-primary);
padding: 2px 6px;
border-radius: 3px;
font-size: 12px;
}
.toast {
position: fixed;
bottom: 20px;
right: 20px;
background-color: var(--success);
color: white;
padding: 12px 20px;
border-radius: 4px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
opacity: 0;
transform: translateY(20px);
transition: opacity 0.3s, transform 0.3s;
z-index: 1000;
}
.toast.show {
opacity: 1;
transform: translateY(0);
}
.preset-buttons {
display: flex;
gap: 5px;
flex-wrap: wrap;
}
.preset-button {
padding: 4px 8px;
font-size: 12px;
background-color: var(--bg-tertiary);
}
.history-controls {
display: flex;
gap: 5px;
}
.tab-container {
display: flex;
gap: 5px;
padding: 10px 15px;
background-color: var(--bg-tertiary);
border-bottom: 1px solid var(--border);
overflow-x: auto;
}
.tab {
padding: 5px 15px;
background-color: var(--bg-secondary);
border-radius: 4px 4px 0 0;
cursor: pointer;
white-space: nowrap;
position: relative;
}
.tab.active {
background-color: var(--accent);
}
.tab-close {
margin-left: 10px;
cursor: pointer;
opacity: 0.7;
}
.tab-close:hover {
opacity: 1;
}
</style>
</head>
<body>
<header>
<div class="header-content">
<h1>🚀 Advanced Text Indenter Pro</h1>
<div class="header-controls">
<button class="small secondary" onclick="toggleTheme()">🌓 Theme</button>
<button class="small secondary" onclick="showShortcuts()">⌨️ Shortcuts</button>
<button class="small secondary" onclick="showAbout()">ℹ️ About</button>
</div>
</div>
</header>
<main>
<div class="controls-panel">
<div class="controls-grid">
<div class="control-group">
<label>Indentation Type</label>
<select id="indentType" onchange="updateIndentation()">
<option value="tab">Tabs</option>
<option value="space">Spaces</option>
<option value="custom">Custom Character</option>
</select>
<input type="text" id="customChar" placeholder="Custom character" style="display:none" onkeyup="updateIndentation()">
</div>
<div class="control-group">
<label>Indentation Amount: <span id="indentValue">1</span></label>
<input type="range" id="indentSlider" min="0" max="10" value="1" oninput="updateIndentValue()">
<div class="preset-buttons">
<button class="preset-button" onclick="setIndent(0)">Remove</button>
<button class="preset-button" onclick="setIndent(1)">1</button>
<button class="preset-button" onclick="setIndent(2)">2</button>
<button class="preset-button" onclick="setIndent(4)">4</button>
<button class="preset-button" onclick="setIndent(8)">8</button>
</div>
</div>
<div class="control-group">
<label>Indentation Mode</label>
<select id="indentMode" onchange="updateIndentation()">
<option value="all">All Lines</option>
<option value="non-empty">Non-empty Lines Only</option>
<option value="first">First Line Only</option>
<option value="except-first">All Except First</option>
<option value="alternate">Alternate Lines</option>
<option value="smart">Smart (Code-like)</option>
</select>
</div>
<div class="control-group">
<label>Space Count (for spaces)</label>
<input type="number" id="spaceCount" min="1" max="10" value="4" onchange="updateIndentation()">
</div>
<div class="control-group">
<label>Options</label>
<div class="control-row">
<input type="checkbox" id="showLineNumbers" checked onchange="toggleLineNumbers()">
<label for="showLineNumbers">Line Numbers</label>
</div>
<div class="control-row">
<input type="checkbox" id="preserveExisting" onchange="updateIndentation()">
<label for="preserveExisting">Preserve Existing Indentation</label>
</div>
<div class="control-row">
<input type="checkbox" id="trimLines" onchange="updateIndentation()">
<label for="trimLines">Trim Line Endings</label>
</div>
</div>
<div class="control-group">
<label>Quick Actions</label>
<div class="control-row">
<button onclick="removeAllIndentation()">Remove All Indents</button>
<button onclick="normalizeIndentation()">Normalize</button>
</div>
<div class="control-row">
<button onclick="convertTabsToSpaces()">Tabs → Spaces</button>
<button onclick="convertSpacesToTabs()">Spaces → Tabs</button>
</div>
</div>
</div>
<div class="history-controls">
<button onclick="undo()" id="undoBtn" disabled>↶ Undo</button>
<button onclick="redo()" id="redoBtn" disabled>↷ Redo</button>
<button onclick="clearAll()" class="danger">Clear All</button>
<button onclick="swapTexts()">⇄ Swap</button>
<button onclick="addNewTab()">+ New Tab</button>
</div>
</div>
<div class="tab-container" id="tabContainer">
<div class="tab active" data-tab="0" onclick="switchTab(0)">
Tab 1
<span class="tab-close" onclick="closeTab(event, 0)">×</span>
</div>
</div>
<div class="editor-container">
<div class="editor-section">
<div class="editor-header">
<div class="editor-title">
<span>📝 Input Text</span>
<span id="inputStats" style="font-size: 12px; color: var(--text-secondary);"></span>
</div>
<div class="editor-actions">
<button class="small" onclick="pasteFromClipboard()">📋 Paste</button>
<button class="small" onclick="loadFile()">📁 Load</button>
<button class="small" onclick="clearInput()">🗑️ Clear</button>
</div>
</div>
<div class="editor-wrapper">
<div class="line-numbers" id="inputLineNumbers"></div>
<textarea id="inputText" placeholder="Paste or type your text here..." oninput="handleInput()" onscroll="syncScroll('input')"></textarea>
</div>
<div class="stats-bar">
<div class="stat-item">
<span class="stat-label">Lines:</span>
<span id="inputLines">0</span>
</div>
<div class="stat-item">
<span class="stat-label">Words:</span>
<span id="inputWords">0</span>
</div>
<div class="stat-item">
<span class="stat-label">Characters:</span>
<span id="inputChars">0</span>
</div>
</div>
</div>
<div class="editor-section">
<div class="editor-header">
<div class="editor-title">
<span>✨ Output Text</span>
<span id="outputStats" style="font-size: 12px; color: var(--text-secondary);"></span>
</div>
<div class="editor-actions">
<button class="small success" onclick="copyToClipboard()">📋 Copy</button>
<button class="small" onclick="downloadText()">💾 Save</button>
<button class="small" onclick="previewHTML()">👁️ Preview</button>
</div>
</div>
<div class="editor-wrapper">
<div class="line-numbers" id="outputLineNumbers"></div>
<div class="indent-preview" id="indentPreview"></div>
<textarea id="outputText" readonly placeholder="Indented text will appear here..." onscroll="syncScroll('output')"></textarea>
</div>
<div class="stats-bar">
<div class="stat-item">
<span class="stat-label">Lines:</span>
<span id="outputLines">0</span>
</div>
<div class="stat-item">
<span class="stat-label">Words:</span>
<span id="outputWords">0</span>
</div>
<div class="stat-item">
<span class="stat-label">Characters:</span>
<span id="outputChars">0</span>
</div>
</div>
</div>
</div>
</main>
<div class="modal" id="shortcutsModal">
<div class="modal-content">
<div class="modal-header">
<h2>Keyboard Shortcuts</h2>
<button onclick="closeModal('shortcutsModal')">×</button>
</div>
<div class="shortcuts-grid">
<div class="shortcut-item">
<span>Process/Update Indentation</span>
<span class="shortcut-key">Ctrl + Enter</span>
</div>
<div class="shortcut-item">
<span>Copy Output</span>
<span class="shortcut-key">Ctrl + Shift + C</span>
</div>
<div class="shortcut-item">
<span>Clear All</span>
<span class="shortcut-key">Ctrl + Shift + X</span>
</div>
<div class="shortcut-item">
<span>Undo</span>
<span class="shortcut-key">Ctrl + Z</span>
</div>
<div class="shortcut-item">
<span>Redo</span>
<span class="shortcut-key">Ctrl + Y</span>
</div>
<div class="shortcut-item">
<span>Increase Indent</span>
<span class="shortcut-key">Ctrl + ]</span>
</div>
<div class="shortcut-item">
<span>Decrease Indent</span>
<span class="shortcut-key">Ctrl + [</span>
</div>
<div class="shortcut-item">
<span>Toggle Theme</span>
<span class="shortcut-key">Ctrl + Shift + T</span>
</div>
<div class="shortcut-item">
<span>New Tab</span>
<span class="shortcut-key">Ctrl + T</span>
</div>
<div class="shortcut-item">
<span>Close Tab</span>
<span class="shortcut-key">Ctrl + W</span>
</div>
</div>
</div>
</div>
<div class="modal" id="aboutModal">
<div class="modal-content">
<div class="modal-header">
<h2>About Text Indenter Pro</h2>
<button onclick="closeModal('aboutModal')">×</button>
</div>
<p>A powerful text indentation tool with advanced features including:</p>
<ul>
<li>Multiple indentation types (tabs, spaces, custom)</li>
<li>Various indentation modes</li>
<li>Line numbers and statistics</li>
<li>Undo/Redo functionality</li>
<li>Multiple tabs support</li>
<li>Import/Export capabilities</li>
<li>Keyboard shortcuts</li>
<li>Theme switching</li>
<li>And much more!</li>
</ul>
<p>Version 2.0 - Enhanced Edition</p>
</div>
</div>
<div class="toast" id="toast"></div>
<input type="file" id="fileInput" style="display: none;" accept="text/*">
<script>
// State management
let history = [];
let historyIndex = -1;
let currentTab = 0;
let tabs = [{ input: '', output: '' }];
let isDarkMode = true;
// Initialize
document.addEventListener('DOMContentLoaded', function() {
updateIndentation();
updateStats();
updateLineNumbers();
setupKeyboardShortcuts();
loadFromLocalStorage();
});
// Core indentation function
function updateIndentation() {
const inputText = document.getElementById('inputText');
const outputText = document.getElementById('outputText');
const indentType = document.getElementById('indentType').value;
const indentLevel = parseInt(document.getElementById('indentSlider').value);
const indentMode = document.getElementById('indentMode').value;
const spaceCount = parseInt(document.getElementById('spaceCount').value);
const preserveExisting = document.getElementById('preserveExisting').checked;
const trimLines = document.getElementById('trimLines').checked;
let indentChar;
if (indentType === 'tab') {
indentChar = '\t';
} else if (indentType === 'space') {
indentChar = ' '.repeat(spaceCount);
} else {
indentChar = document.getElementById('customChar').value || '>';
}
const indent = indentChar.repeat(indentLevel);
const lines = inputText.value.split('\n');
let indentedLines = [];
lines.forEach((line, index) => {
let processedLine = trimLines ? line.trimEnd() : line;
if (!preserveExisting && indentLevel > 0) {
processedLine = processedLine.trimStart();
}
let shouldIndent = false;
switch (indentMode) {
case 'all':
shouldIndent = true;
break;
case 'non-empty':
shouldIndent = processedLine.trim() !== '';
break;
case 'first':
shouldIndent = index === 0;
break;
case 'except-first':
shouldIndent = index > 0;
break;
case 'alternate':
shouldIndent = index % 2 === 0;
break;
case 'smart':
shouldIndent = processedLine.trim() !== '' && !processedLine.trim().startsWith('}') && !processedLine.trim().startsWith(')');
break;
}
if (shouldIndent && (indentLevel > 0 || preserveExisting)) {
indentedLines.push(indent + processedLine);
} else if (indentLevel === 0 && !preserveExisting) {
indentedLines.push(processedLine.trimStart());
} else {
indentedLines.push(processedLine);
}
});
outputText.value = indentedLines.join('\n');
// Save to current tab
tabs[currentTab].input = inputText.value;
tabs[currentTab].output = outputText.value;
updateStats();
updateLineNumbers();
updateIndentPreview();
saveToLocalStorage();
}
function updateIndentValue() {
const slider = document.getElementById('indentSlider');
document.getElementById('indentValue').textContent = slider.value;
updateIndentation();
}
function setIndent(value) {
document.getElementById('indentSlider').value = value;
updateIndentValue();
}
function handleInput() {
addToHistory();
updateIndentation();
}
// History management
function addToHistory() {
const currentState = document.getElementById('inputText').value;
if (historyIndex < history.length - 1) {
history = history.slice(0, historyIndex + 1);
}
history.push(currentState);
historyIndex++;
// Limit history size
if (history.length > 50) {
history.shift();
historyIndex--;
}
updateHistoryButtons();
}
function undo() {
if (historyIndex > 0) {
historyIndex--;
document.getElementById('inputText').value = history[historyIndex];
updateIndentation();
updateHistoryButtons();
}
}
function redo() {
if (historyIndex < history.length - 1) {
historyIndex++;
document.getElementById('inputText').value = history[historyIndex];
updateIndentation();
updateHistoryButtons();
}
}
function updateHistoryButtons() {
document.getElementById('undoBtn').disabled = historyIndex <= 0;
document.getElementById('redoBtn').disabled = historyIndex >= history.length - 1;
}
// Tab management
function addNewTab() {
const newIndex = tabs.length;
tabs.push({ input: '', output: '' });
const tabContainer = document.getElementById('tabContainer');
const newTab = document.createElement('div');
newTab.className = 'tab';
newTab.setAttribute('data-tab', newIndex);
newTab.onclick = () => switchTab(newIndex);
newTab.innerHTML = `
Tab ${newIndex + 1}
<span class="tab-close" onclick="closeTab(event, ${newIndex})">×</span>
`;
tabContainer.appendChild(newTab);
switchTab(newIndex);
}
function switchTab(index) {
// Save current tab state
tabs[currentTab].input = document.getElementById('inputText').value;
tabs[currentTab].output = document.getElementById('outputText').value;
// Switch to new tab
currentTab = index;
document.getElementById('inputText').value = tabs[index].input;
document.getElementById('outputText').value = tabs[index].output;
// Update UI
document.querySelectorAll('.tab').forEach((tab, i) => {
tab.classList.toggle('active', i === index);
});
updateIndentation();
history = [tabs[index].input];
historyIndex = 0;
updateHistoryButtons();
}
function closeTab(event, index) {
event.stopPropagation();
if (tabs.length === 1) {
showToast('Cannot close the last tab');
return;
}
tabs.splice(index, 1);
document.querySelector(`[data-tab="${index}"]`).remove();
// Update tab indices
document.querySelectorAll('.tab').forEach((tab, i) => {
tab.setAttribute('data-tab', i);
tab.onclick = () => switchTab(i);
tab.innerHTML = `
Tab ${i + 1}
<span class="tab-close" onclick="closeTab(event, ${i})">×</span>
`;
});
if (currentTab >= tabs.length) {
currentTab = tabs.length - 1;
}
switchTab(currentTab);
}
// UI Functions
function toggleLineNumbers() {
const show = document.getElementById('showLineNumbers').checked;
document.querySelectorAll('.line-numbers').forEach(el => {
el.style.display = show ? 'block' : 'none';
});
document.querySelectorAll('textarea').forEach(el => {
el.classList.toggle('no-line-numbers', !show);
});
updateLineNumbers();
}
function updateLineNumbers() {
if (!document.getElementById('showLineNumbers').checked) return;
const inputLines = document.getElementById('inputText').value.split('\n').length;
const outputLines = document.getElementById('outputText').value.split('\n').length;
document.getElementById('inputLineNumbers').textContent =
Array.from({length: inputLines}, (_, i) => i + 1).join('\n');
document.getElementById('outputLineNumbers').textContent =
Array.from({length: outputLines}, (_, i) => i + 1).join('\n');
}
function updateStats() {
const inputText = document.getElementById('inputText').value;
const outputText = document.getElementById('outputText').value;
// Input stats
const inputLines = inputText.split('\n').length;
const inputWords = inputText.trim() ? inputText.trim().split(/\s+/).length : 0;
const inputChars = inputText.length;
document.getElementById('inputLines').textContent = inputLines;
document.getElementById('inputWords').textContent = inputWords;
document.getElementById('inputChars').textContent = inputChars;
// Output stats
const outputLines = outputText.split('\n').length;
const outputWords = outputText.trim() ? outputText.trim().split(/\s+/).length : 0;
const outputChars = outputText.length;
document.getElementById('outputLines').textContent = outputLines;
document.getElementById('outputWords').textContent = outputWords;
document.getElementById('outputChars').textContent = outputChars;
}
function updateIndentPreview() {
const indentType = document.getElementById('indentType').value;
const indentLevel = parseInt(document.getElementById('indentSlider').value);
const spaceCount = parseInt(document.getElementById('spaceCount').value);
let indentChar;
if (indentType === 'tab') {
indentChar = '→';
} else if (indentType === 'space') {
indentChar = '·'.repeat(spaceCount);
} else {
indentChar = document.getElementById('customChar').value || '>';
}
const preview = indentChar.repeat(indentLevel);
document.getElementById('indentPreview').textContent = preview;
}
function syncScroll(source) {
const inputText = document.getElementById('inputText');
const outputText = document.getElementById('outputText');
const inputLineNumbers = document.getElementById('inputLineNumbers');
const outputLineNumbers = document.getElementById('outputLineNumbers');
if (source === 'input') {
inputLineNumbers.scrollTop = inputText.scrollTop;
} else {
outputLineNumbers.scrollTop = outputText.scrollTop;
}
}
// Action functions
function copyToClipboard() {
const outputText = document.getElementById('outputText');
outputText.select();
document.execCommand('copy');
showToast('Copied to clipboard!');
}
function clearAll() {
if (confirm('Clear all text? This cannot be undone.')) {
document.getElementById('inputText').value = '';
document.getElementById('outputText').value = '';
history = [''];
historyIndex = 0;
updateHistoryButtons();
updateStats();
updateLineNumbers();
showToast('Cleared all text');
}
}
function clearInput() {
document.getElementById('inputText').value = '';
updateIndentation();
showToast('Input cleared');
}
function swapTexts() {
const inputText = document.getElementById('inputText');
const outputText = document.getElementById('outputText');
const temp = inputText.value;
inputText.value = outputText.value;
addToHistory();
updateIndentation();
showToast('Texts swapped');
}
function removeAllIndentation() {
const inputText = document.getElementById('inputText');
inputText.value = inputText.value.split('\n').map(line => line.trimStart()).join('\n');
addToHistory();
updateIndentation();
showToast('Removed all indentation');
}
function normalizeIndentation() {
const inputText = document.getElementById('inputText');
const lines = inputText.value.split('\n');
const minIndent = Math.min(...lines
.filter(line => line.trim())
.map(line => line.match(/^\s*/)[0].length));
inputText.value = lines.map(line =>
line.trim() ? line.substring(minIndent) : line
).join('\n');
addToHistory();
updateIndentation();
showToast('Normalized indentation');
}
function convertTabsToSpaces() {
const inputText = document.getElementById('inputText');
const spaceCount = parseInt(document.getElementById('spaceCount').value);
inputText.value = inputText.value.replace(/\t/g, ' '.repeat(spaceCount));
addToHistory();
updateIndentation();
showToast('Converted tabs to spaces');
}
function convertSpacesToTabs() {
const inputText = document.getElementById('inputText');
const spaceCount = parseInt(document.getElementById('spaceCount').value);
const spacePattern = new RegExp(' '.repeat(spaceCount), 'g');
inputText.value = inputText.value.replace(spacePattern, '\t');
addToHistory();
updateIndentation();
showToast('Converted spaces to tabs');
}
// File operations
function loadFile() {
document.getElementById('fileInput').click();
}
document.getElementById('fileInput').addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
document.getElementById('inputText').value = e.target.result;
addToHistory();
updateIndentation();
showToast(`Loaded ${file.name}`);
};
reader.readAsText(file);
}
});
function downloadText() {
const outputText = document.getElementById('outputText').value;
const blob = new Blob([outputText], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `indented_text_${new Date().getTime()}.txt`;
a.click();
URL.revokeObjectURL(url);
showToast('File downloaded');
}
function pasteFromClipboard() {
navigator.clipboard.readText().then(text => {
document.getElementById('inputText').value = text;
addToHistory();
updateIndentation();
showToast('Pasted from clipboard');
}).catch(() => {
showToast('Failed to paste from clipboard');
});
}
function previewHTML() {
const outputText = document.getElementById('outputText').value;
const previewWindow = window.open('', '_blank');
previewWindow.document.write(`
<html>
<head>
<title>Preview</title>
<style>
body {
font-family: monospace;
white-space: pre;
padding: 20px;
background-color: ${isDarkMode ? '#1a1a1a' : '#ffffff'};
color: ${isDarkMode ? '#e0e0e0' : '#000000'};
}
</style>
</head>
<body>${outputText.replace(/</g, '<').replace(/>/g, '>')}</body>
</html>
`);
}
// Theme management
function toggleTheme() {
isDarkMode = !isDarkMode;
document.documentElement.style.setProperty('--bg-primary', isDarkMode ? '#1a1a1a' : '#ffffff');
document.documentElement.style.setProperty('--bg-secondary', isDarkMode ? '#2a2a2a' : '#f5f5f5');
document.documentElement.style.setProperty('--bg-tertiary', isDarkMode ? '#333333' : '#e0e0e0');
document.documentElement.style.setProperty('--text-primary', isDarkMode ? '#e0e0e0' : '#000000');
document.documentElement.style.setProperty('--text-secondary', isDarkMode ? '#b0b0b0' : '#666666');
document.documentElement.style.setProperty('--border', isDarkMode ? '#444' : '#ccc');
saveToLocalStorage();
showToast(`Switched to ${isDarkMode ? 'dark' : 'light'} mode`);
}
// Modal functions
function showShortcuts() {
document.getElementById('shortcutsModal').style.display = 'flex';
}
function showAbout() {
document.getElementById('aboutModal').style.display = 'flex';
}
function closeModal(modalId) {
document.getElementById(modalId).style.display = 'none';
}
// Toast notification
function showToast(message) {
const toast = document.getElementById('toast');
toast.textContent = message;
toast.classList.add('show');
setTimeout(() => {
toast.classList.remove('show');
}, 3000);
}
// Keyboard shortcuts
function setupKeyboardShortcuts() {
document.addEventListener('keydown', function(e) {
// Ctrl + Enter: Process
if (e.ctrlKey && e.key === 'Enter') {
e.preventDefault();
updateIndentation();
}
// Ctrl + Shift + C: Copy
else if (e.ctrlKey && e.shiftKey && e.key === 'C') {
e.preventDefault();
copyToClipboard();
}
// Ctrl + Shift + X: Clear all
else if (e.ctrlKey && e.shiftKey && e.key === 'X') {
e.preventDefault();
clearAll();
}
// Ctrl + Z: Undo
else if (e.ctrlKey && !e.shiftKey && e.key === 'z') {
e.preventDefault();
undo();
}
// Ctrl + Y: Redo
else if (e.ctrlKey && e.key === 'y') {
e.preventDefault();
redo();
}
// Ctrl + ]: Increase indent
else if (e.ctrlKey && e.key === ']') {
e.preventDefault();
const slider = document.getElementById('indentSlider');
slider.value = Math.min(10, parseInt(slider.value) + 1);
updateIndentValue();
}
// Ctrl + [: Decrease indent
else if (e.ctrlKey && e.key === '[') {
e.preventDefault();
const slider = document.getElementById('indentSlider');
slider.value = Math.max(0, parseInt(slider.value) - 1);
updateIndentValue();
}
// Ctrl + Shift + T: Toggle theme
else if (e.ctrlKey && e.shiftKey && e.key === 'T') {
e.preventDefault();
toggleTheme();
}
// Ctrl + T: New tab
else if (e.ctrlKey && !e.shiftKey && e.key === 't') {
e.preventDefault();
addNewTab();
}
// Ctrl + W: Close tab
else if (e.ctrlKey && e.key === 'w') {
e.preventDefault();
closeTab(e, currentTab);
}
});
}
// Local storage
function saveToLocalStorage() {
const data = {
tabs: tabs,
currentTab: currentTab,
settings: {
indentType: document.getElementById('indentType').value,
indentLevel: document.getElementById('indentSlider').value,
indentMode: document.getElementById('indentMode').value,
spaceCount: document.getElementById('spaceCount').value,
showLineNumbers: document.getElementById('showLineNumbers').checked,
preserveExisting: document.getElementById('preserveExisting').checked,
trimLines: document.getElementById('trimLines').checked,
isDarkMode: isDarkMode
}
};
localStorage.setItem('textIndenterPro', JSON.stringify(data));
}
function loadFromLocalStorage() {
const saved = localStorage.getItem('textIndenterPro');
if (saved) {
const data = JSON.parse(saved);
// Load settings
if (data.settings) {
document.getElementById('indentType').value = data.settings.indentType || 'tab';
document.getElementById('indentSlider').value = data.settings.indentLevel || 1;
document.getElementById('indentMode').value = data.settings.indentMode || 'all';
document.getElementById('spaceCount').value = data.settings.spaceCount || 4;
document.getElementById('showLineNumbers').checked = data.settings.showLineNumbers !== false;
document.getElementById('preserveExisting').checked = data.settings.preserveExisting || false;
document.getElementById('trimLines').checked = data.settings.trimLines || false;
if (data.settings.isDarkMode === false) {
toggleTheme();
}
}
// Load tabs
if (data.tabs && data.tabs.length > 0) {
tabs = data.tabs;
currentTab = data.currentTab || 0;
// Recreate tab UI
const tabContainer = document.getElementById('tabContainer');
tabContainer.innerHTML = '';
tabs.forEach((tab, i) => {
const tabEl = document.createElement('div');
tabEl.className = 'tab' + (i === currentTab ? ' active' : '');
tabEl.setAttribute('data-tab', i);
tabEl.onclick = () => switchTab(i);
tabEl.innerHTML = `
Tab ${i + 1}
<span class="tab-close" onclick="closeTab(event, ${i})">×</span>
`;
tabContainer.appendChild(tabEl);
});
// Load current tab content
document.getElementById('inputText').value = tabs[currentTab].input || '';
document.getElementById('outputText').value = tabs[currentTab].output || '';
}
updateIndentValue();
toggleLineNumbers();
}
}
// UI updates
document.getElementById('indentType').addEventListener('change', function() {
const customChar = document.getElementById('customChar');
customChar.style.display = this.value === 'custom' ? 'block' : 'none';
updateIndentation();
});
// Close modals when clicking outside
window.onclick = function(event) {
if (event.target.classList.contains('modal')) {
event.target.style.display = 'none';
}
}
</script>
</body>
</html>