-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
370 lines (298 loc) · 11.6 KB
/
Copy pathcode.js
File metadata and controls
370 lines (298 loc) · 11.6 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
let historial = [];
let redoStack = [];
const maxHistorial = 20;
function guardarHistorial() {
const area = document.getElementById("areaTexto");
if (!area) return;
const actual = area.value;
const ultimo = historial[historial.length - 1];
if (actual !== ultimo) {
historial.push(actual);
redoStack = [];
if (historial.length > maxHistorial) {
historial.shift();
}
}
}
function deshacer() {
const area = document.getElementById("areaTexto");
const msg = document.getElementById("msg");
if (historial.length === 0) {
msg.textContent = "⛔ No hay cambios para deshacer";
return;
}
const estadoAnterior = historial.pop(); // este es el estado que queremos restaurar
redoStack.push(area.value); // guarda el estado actual para rehacer
area.value = estadoAnterior;
msg.textContent = "↩️ Se ha deshecho el último cambio";
}
function rehacer() {
const area = document.getElementById("areaTexto");
const msg = document.getElementById("msg");
if (redoStack.length === 0) {
msg.textContent = "⛔ No hay cambios para rehacer";
return;
}
const estadoRehecho = redoStack.pop();
historial.push(area.value); // guarda el estado actual antes de rehacer
area.value = estadoRehecho;
msg.textContent = "🔁 Se ha rehecho el cambio";
}
function F8textoChulo() {
const area = document.getElementById("areaTexto");
area.classList.remove("fuente-fija", "roboto-mono", "fira-code", "source-code");
area.classList.add("titulo");
document.getElementById("msg").textContent = "🎨 Cool style applied";
}
function aplicarFuenteFija() {
const area = document.getElementById("areaTexto");
area.classList.remove("titulo", "roboto-mono", "fira-code", "source-code");
area.classList.add("fuente-fija");
document.getElementById("msg").textContent = "🔤 Fixed-width font applied";
}
function cargarArchivoTexto() {
const input = document.getElementById("archivoTexto");
const area = document.getElementById("areaTexto");
const msg = document.getElementById("msg");
if (!input.files || input.files.length === 0) {
msg.textContent = "⚠️ No se ha seleccionado ningún archivo";
return;
}
const archivo = input.files[0];
const lector = new FileReader();
lector.onload = function(e) {
area.value = e.target.result;
guardarHistorial();
msg.textContent = "✅ Archivo cargado correctamente";
};
lector.onerror = function() {
msg.textContent = "❌ Error al leer el archivo";
};
lector.readAsText(archivo, "UTF-8");
}
function guardarTextoComoArchivo() {
const area = document.getElementById("areaTexto");
if (!area) return;
const contenido = area.value;
const ahora = new Date();
const año = ahora.getFullYear();
const mes = String(ahora.getMonth() + 1).padStart(2, '0');
const día = String(ahora.getDate()).padStart(2, '0');
const hora = String(ahora.getHours()).padStart(2, '0');
const minuto = String(ahora.getMinutes()).padStart(2, '0');
const nombreArchivo = `CleanSubs_${año}${mes}${día}_${hora}${minuto}.txt`;
const blob = new Blob([contenido], { type: 'text/plain' });
const enlace = document.createElement('a');
enlace.href = URL.createObjectURL(blob);
enlace.download = nombreArchivo;
document.body.appendChild(enlace);
enlace.click();
document.body.removeChild(enlace);
}
function copiaPortapapeles() {
const area = document.getElementById("areaTexto");
const texto = area.value;
navigator.clipboard.writeText(texto)
.then(() => {
document.getElementById("msg").textContent = "✅ Texto copiado al portapapeles";
})
.catch(err => {
document.getElementById("msg").textContent = "❌ Error al copiar: " + err;
});
}
function quitarLineasEnBlanco() {
guardarHistorial();
const area = document.getElementById("areaTexto");
if (!area) return;
const textoOriginal = area.value;
const lineas = textoOriginal.split('\n');
const lineasFiltradas = lineas.filter(linea => linea.trim() !== '');
area.value = lineasFiltradas.join('\n');
document.getElementById('msg').textContent = `Líneas en blanco eliminadas`;
}
function limpiarTexto() {
guardarHistorial();
const area = document.getElementById("areaTexto");
const msg = document.getElementById("msg");
const textoOriginal = area.value;
const regexNumeros = /^\d+$/gm;
const regexHoras = /^\d{2}:\d{2}:\d{2},\d{3}\s-->\s\d{2}:\d{2}:\d{2},\d{3}$/gm;
let textoModificado = textoOriginal
.replace(regexNumeros, '')
.replace(regexHoras, '')
.replace(/^\s*\n/gm, '');
area.value = textoModificado;
msg.textContent = "🧹 Se han eliminado líneas numéricas y líneas con hora";
}
function quitarSaltosDeLinea() {
guardarHistorial();
const area = document.getElementById("areaTexto");
if (!area) return;
// Elimina saltos de línea sin añadir espacios extra
const textoSinSaltos = area.value.replace(/\s*\r?\n\s*/g, ' ').trim();
area.value = textoSinSaltos;
document.getElementById('msg').textContent = `Saltos de línea eliminados`;
}
function quitarEspacios() {
guardarHistorial();
const textarea = document.querySelector('.cuadro-texto textarea');
const textoSinEspacios = textarea.value.replace(/[ \t]+/g, '');
textarea.value = textoSinEspacios;
const caracteresSinEspacios = textoSinEspacios.replace(/\s/g, '').length;
document.getElementById('msg').textContent =
`Caracteres sin espacios (saltos conservados): ${caracteresSinEspacios}`;
}
function borrarTexto() {
guardarHistorial();
const area = document.getElementById("areaTexto");
const input = document.getElementById("textoABorrar");
const msg = document.getElementById("msg");
const textoOriginal = area.value;
const textoABorrar = input.value;
if (!textoABorrar) {
msg.textContent = "⚠️ Ingresa el texto que deseas borrar";
return;
}
const regex = new RegExp(textoABorrar, 'gi');
const textoModificado = textoOriginal.replace(regex, '');
area.value = textoModificado;
msg.textContent = `🧹 Se han borrado todas las incidencias de "${textoABorrar}"`;
}
function aumentarTamaño() {
const textarea = document.querySelector('.cuadro-texto textarea');
let tamañoActual = parseInt(window.getComputedStyle(textarea).fontSize);
if (tamañoActual < 100) {
textarea.style.fontSize = (tamañoActual + 5) + 'px';
document.getElementById('msg').textContent = 'Font: ' + textarea.style.fontSize;
}
}
function disminuirTamaño() {
const textarea = document.querySelector('.cuadro-texto textarea');
let tamañoActual = parseInt(window.getComputedStyle(textarea).fontSize);
if (tamañoActual > 20) {
textarea.style.fontSize = (tamañoActual - 5) + 'px';
document.getElementById('msg').textContent = 'Font: ' + textarea.style.fontSize;
}
}
function contarLineas() {
const texto = document.querySelector('.cuadro-texto textarea').value;
const lineas = texto.split(/\r\n|\r|\n/).length;
document.getElementById('msg').textContent = `Líneas: ${lineas}`;
}
function contarCaracteresConEspacios() {
const texto = document.querySelector('.cuadro-texto textarea').value;
const total = texto.length;
document.getElementById('msg').textContent = `Caracteres (con espacios): ${total}`;
}
function contarCaracteresSinEspacios() {
const texto = document.querySelector('.cuadro-texto textarea').value;
const total = texto.replace(/\s/g, '').length;
document.getElementById('msg').textContent = `Caracteres (sin espacios): ${total}`;
}
function toggleHerramientasExtra() {
const extra = document.getElementById("herramientasExtra");
const msg = document.getElementById("msg");
if (extra.style.display === "none") {
extra.style.display = "flex"; // o "block" según tu diseño
msg.textContent = "🧰 Herramientas adicionales activadas";
} else {
extra.style.display = "none";
msg.textContent = "🧰 Herramientas adicionales ocultas";
}
}
function ajustaTiempoSubt() {
const area = document.getElementById("areaTexto");
const input = document.getElementById("timeSubt");
const msg = document.getElementById("msg");
const texto = area.value;
const desplazamiento = input.value.trim();
// Validar formato
const regexFormato = /^([+-])(\d{2}):(\d{2}):(\d{2}),(\d{3})$/;
const match = desplazamiento.match(regexFormato);
if (!match) {
msg.textContent = "⚠️ Formato inválido. Usa +00:00:01,500 o -00:00:02,000";
return;
}
// Guardar estado actual antes de modificar
guardarHistorial();
const signo = match[1] === "+" ? 1 : -1;
const horas = parseInt(match[2]);
const minutos = parseInt(match[3]);
const segundos = parseInt(match[4]);
const milisegundos = parseInt(match[5]);
const totalMs = signo * (
horas * 3600000 +
minutos * 60000 +
segundos * 1000 +
milisegundos
);
// Expresión para encontrar líneas de tiempo
const regexTiempo = /(\d{2}):(\d{2}):(\d{2}),(\d{3})\s-->\s(\d{2}):(\d{2}):(\d{2}),(\d{3})/g;
const textoModificado = texto.replace(regexTiempo, (match, h1, m1, s1, ms1, h2, m2, s2, ms2) => {
const t1 = convertirATiempoMs(h1, m1, s1, ms1) + totalMs;
const t2 = convertirATiempoMs(h2, m2, s2, ms2) + totalMs;
return `${formatearTiempo(t1)} --> ${formatearTiempo(t2)}`;
});
area.value = textoModificado;
msg.textContent = `⏱️ Tiempos ajustados en ${desplazamiento}`;
}
// Convierte tiempo a milisegundos
function convertirATiempoMs(h, m, s, ms) {
return (
parseInt(h) * 3600000 +
parseInt(m) * 60000 +
parseInt(s) * 1000 +
parseInt(ms)
);
}
// Convierte milisegundos a formato SRT
function formatearTiempo(ms) {
if (ms < 0) ms = 0; // Evitar tiempos negativos
const h = String(Math.floor(ms / 3600000)).padStart(2, '0');
ms %= 3600000;
const m = String(Math.floor(ms / 60000)).padStart(2, '0');
ms %= 60000;
const s = String(Math.floor(ms / 1000)).padStart(2, '0');
const msFinal = String(ms % 1000).padStart(3, '0');
return `${h}:${m}:${s},${msFinal}`;
}
function addNumbering() {
guardarHistorial();
const area = document.getElementById("areaTexto");
const formatoInput = document.getElementById("formatoNumeracion");
const msg = document.getElementById("msg");
if (!area || !formatoInput) {
msg.textContent = "⚠️ Área de texto o campo de formato no encontrado";
return;
}
const formato = formatoInput.value.trim();
const textoOriginal = area.value;
const lineas = textoOriginal.split(/\r?\n/);
let textoNumerado = "";
if (/^9+$/.test(formato)) {
// Formato numérico con ceros a la izquierda
const longitud = formato.length;
textoNumerado = lineas.map((linea, index) => {
const numero = String(index + 1).padStart(longitud, '0');
return `${numero} ${linea}`;
}).join('\n');
msg.textContent = `🔢 Numeración aplicada con ${longitud} dígitos`;
} else {
// Formato como viñeta personalizada
textoNumerado = lineas.map(linea => `${formato} ${linea}`).join('\n');
msg.textContent = `🔸 Viñeta "${formato}" aplicada a cada línea`;
}
area.value = textoNumerado;
}
window.addEventListener("DOMContentLoaded", () => {
const area = document.getElementById("areaTexto");
if (area && area.value.trim() !== "") {
guardarHistorial();
}
document.getElementById("areaTexto").focus();
});
function limpiarTodo() {
const area = document.getElementById("areaTexto");
area.value = "";
area.focus();
}