-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextEditor.html
More file actions
92 lines (82 loc) · 2.41 KB
/
TextEditor.html
File metadata and controls
92 lines (82 loc) · 2.41 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<title>Text Editor</title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#textInput {
width: 100%;
height: 100%;
box-sizing: border-box;
padding-bottom: 60px; /* make room for buttons */
font-family: monospace;
font-size: 14px;
}
#controls {
position: fixed;
bottom: 10px;
left: 0;
display: flex;
justify-content: center;
gap: 10px;
background: rgba(128, 128, 128, 0.3);
padding: 10px;
box-shadow: 0 -2px 5px rgba(0,0,0,0.1);
}
</style>
</head>
<body>
<textarea id="textInput">Text Content</textarea>
<div id="controls">
<button id="save-text-btn">Save</button>
<input type="file" id="textLoader" accept=".txt,text/*">
</div>
<script>
const textInput = document.getElementById('textInput');
document.getElementById('textLoader').addEventListener('change', function (e) {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function (event) {
textInput.value = event.target.result;
};
reader.readAsText(file);
});
document.getElementById('save-text-btn').addEventListener('click', async () => {
let filename = 'mytext.txt';
if ('showSaveFilePicker' in window) {
try {
const opts = {
suggestedName: filename,
types: [{
description: 'text file',
accept: { 'text/plain': ['.txt'] }
}]
};
const handle = await window.showSaveFilePicker(opts);
const writable = await handle.createWritable();
await writable.write(textInput.value);
await writable.close();
} catch (err) {
if (err.name !== 'AbortError') {
alert('Error saving file: ' + err.message);
}
}
} else {
// fallback for unsupported browsers
const blob = new Blob([textInput.value], { type: 'text/plain' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = filename;
a.click();
}
});
</script>
</body>
</html>