Program a notepad editor with good copy and paste features in html
- User
Program a notepad editor with good copy and paste features in html
- Assistant
- User
Can you add to the top to select all text
- Assistant
Program a notepad editor with good copy and paste features in html
Can you add to the top to select all text
Sure! Below is a simple yet functional Notepad-style text editor built with HTML, CSS, and JavaScript. It features a textarea for editing text and includes good copy and paste support via buttons and keyboard shortcuts.
This example supports:
.txt)<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Simple Notepad Editor</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
height: 100vh;
display: flex;
flex-direction: column;
}
.toolbar {
background-color: #eee;
padding: 8px;
display: flex;
gap: 6px;
}
button {
padding: 6px 12px;
font-size: 14px;
cursor: pointer;
}
textarea {
flex-grow: 1;
width: 100%;
border: none;
padding: 12px;
font-family: monospace;
font-size: 16px;
resize: none;
outline: none;
}
</style>
</head>
<body>
<div class="toolbar">
<button id="newBtn" title="New (Ctrl+N)">New</button>
<button id="openBtn" title="Open (Ctrl+O)">Open</button>
<button id="saveBtn" title="Save (Ctrl+S)">Save</button>
<button id="cutBtn" title="Cut (Ctrl+X)">Cut</button>
<button id="copyBtn" title="Copy (Ctrl+C)">Copy</button>
<button id="pasteBtn" title="Paste (Ctrl+V)">Paste</button>
<input type="file" id="fileInput" accept=".txt" style="display:none" />
</div>
<textarea id="editor" spellcheck="false" placeholder="Start typing..."></textarea>
<script>
const editor = document.getElementById('editor');
const newBtn = document.getElementById('newBtn');
const openBtn = document.getElementById('openBtn');
const saveBtn = document.getElementById('saveBtn');
const cutBtn = document.getElementById('cutBtn');
const copyBtn = document.getElementById('copyBtn');
const pasteBtn = document.getElementById('pasteBtn');
const fileInput = document.getElementById('fileInput');
// New File
function newFile() {
if (confirm('Are you sure you want to create a new file? Unsaved changes will be lost.')) {
editor.value = '';
}
}
newBtn.addEventListener('click', newFile);
// Open File
openBtn.addEventListener('click', () => {
fileInput.value = null; // reset input
fileInput.click();
});
fileInput.addEventListener('change', () => {
const file = fileInput.files[0];
if (!file) return;
if (!file.type.match('text.*')) {
alert('Please upload a text file.');
return;
}
const reader = new FileReader();
reader.onload = e => {
editor.value = e.target.result;
};
reader.readAsText(file);
});
// Save File
saveBtn.addEventListener('click', () => {
const text = editor.value;
const blob = new Blob([text], {type: 'text/plain'});
const anchor = document.createElement('a');
anchor.download = 'untitled.txt';
anchor.href = URL.createObjectURL(blob);
anchor.click();
URL.revokeObjectURL(anchor.href);
});
// Copy/Cut/Paste functions using Clipboard API fallback to execCommand
async function copyText() {
try {
if (navigator.clipboard) {
await navigator.clipboard.writeText(editor.value.substring(editor.selectionStart, editor.selectionEnd));
} else {
document.execCommand('copy');
}
} catch (err) {
alert('Copy failed: ' + err);
}
}
async function cutText() {
try {
if (navigator.clipboard) {
const selectedText = editor.value.substring(editor.selectionStart, editor.selectionEnd);
await navigator.clipboard.writeText(selectedText);
const start = editor.selectionStart;
const end = editor.selectionEnd;
editor.value = editor.value.slice(0, start) + editor.value.slice(end);
editor.selectionStart = editor.selectionEnd = start;
} else {
document.execCommand('cut');
}
} catch (err) {
alert('Cut failed: ' + err);
}
}
async function pasteText() {
try {
let text = '';
if (navigator.clipboard) {
text = await navigator.clipboard.readText();
} else {
alert('Paste is not supported by your browser.');
return;
}
const start = editor.selectionStart;
const end = editor.selectionEnd;
editor.value = editor.value.slice(0, start) + text + editor.value.slice(end);
editor.selectionStart = editor.selectionEnd = start + text.length;
editor.focus();
} catch (err) {
alert('Paste failed: ' + err);
}
}
copyBtn.addEventListener('click', copyText);
cutBtn.addEventListener('click', cutText);
pasteBtn.addEventListener('click', pasteText);
// Keyboard shortcuts
window.addEventListener('keydown', function (e) {
if (e.ctrlKey) {
switch (e.key.toLowerCase()) {
case 'n': // Ctrl+N = New
e.preventDefault();
newFile();
break;
case 'o': // Ctrl+O = Open
e.preventDefault();
openBtn.click();
break;
case 's': // Ctrl+S = Save
e.preventDefault();
saveBtn.click();
break;
case 'c': // Ctrl+C
// This works by default, no need to handle unless customizing
break;
case 'x': // Ctrl+X
// This works by default, no need to handle unless customizing
break;
case 'v': // Ctrl+V
// This works by default, no need to handle unless customizing
break;
}
}
});
</script>
</body>
</html>
<textarea> is your text editing area.untitled.txt).execCommand (deprecated but still works in many browsers).Let me know if you'd like me to add any other features!
Certainly! I've added a "Select All" button to the toolbar at the top. Clicking it will select all the text inside the editor.
Here's the updated code snippet with the "Select All" button added:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Simple Notepad Editor</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
height: 100vh;
display: flex;
flex-direction: column;
}
.toolbar {
background-color: #eee;
padding: 8px;
display: flex;
gap: 6px;
flex-wrap: wrap;
}
button {
padding: 6px 12px;
font-size: 14px;
cursor: pointer;
user-select: none;
}
textarea {
flex-grow: 1;
width: 100%;
border: none;
padding: 12px;
font-family: monospace;
font-size: 16px;
resize: none;
outline: none;
}
</style>
</head>
<body>
<div class="toolbar">
<button id="newBtn" title="New (Ctrl+N)">New</button>
<button id="openBtn" title="Open (Ctrl+O)">Open</button>
<button id="saveBtn" title="Save (Ctrl+S)">Save</button>
<button id="selectAllBtn" title="Select All (Ctrl+A)">Select All</button>
<button id="cutBtn" title="Cut (Ctrl+X)">Cut</button>
<button id="copyBtn" title="Copy (Ctrl+C)">Copy</button>
<button id="pasteBtn" title="Paste (Ctrl+V)">Paste</button>
<input type="file" id="fileInput" accept=".txt" style="display:none" />
</div>
<textarea id="editor" spellcheck="false" placeholder="Start typing..."></textarea>
<script>
const editor = document.getElementById('editor');
const newBtn = document.getElementById('newBtn');
const openBtn = document.getElementById('openBtn');
const saveBtn = document.getElementById('saveBtn');
const selectAllBtn = document.getElementById('selectAllBtn');
const cutBtn = document.getElementById('cutBtn');
const copyBtn = document.getElementById('copyBtn');
const pasteBtn = document.getElementById('pasteBtn');
const fileInput = document.getElementById('fileInput');
// New File
function newFile() {
if (confirm('Are you sure you want to create a new file? Unsaved changes will be lost.')) {
editor.value = '';
}
}
newBtn.addEventListener('click', newFile);
// Open File
openBtn.addEventListener('click', () => {
fileInput.value = null; // reset input
fileInput.click();
});
fileInput.addEventListener('change', () => {
const file = fileInput.files[0];
if (!file) return;
if (!file.type.match('text.*')) {
alert('Please upload a text file.');
return;
}
const reader = new FileReader();
reader.onload = e => {
editor.value = e.target.result;
};
reader.readAsText(file);
});
// Save File
saveBtn.addEventListener('click', () => {
const text = editor.value;
const blob = new Blob([text], {type: 'text/plain'});
const anchor = document.createElement('a');
anchor.download = 'untitled.txt';
anchor.href = URL.createObjectURL(blob);
anchor.click();
URL.revokeObjectURL(anchor.href);
});
// Select All
function selectAllText() {
editor.focus();
editor.select();
}
selectAllBtn.addEventListener('click', selectAllText);
// Copy/Cut/Paste handlers using Clipboard API with execCommand fallback
async function copyText() {
try {
const selectedText = editor.value.substring(editor.selectionStart, editor.selectionEnd);
if (!selectedText) return; // nothing selected
if (navigator.clipboard) {
await navigator.clipboard.writeText(selectedText);
} else {
document.execCommand('copy');
}
} catch (err) {
alert('Copy failed: ' + err);
}
}
async function cutText() {
try {
const start = editor.selectionStart;
const end = editor.selectionEnd;
if (start === end) return; // nothing selected to cut
if (navigator.clipboard) {
const selectedText = editor.value.substring(start, end);
await navigator.clipboard.writeText(selectedText);
editor.value = editor.value.slice(0, start) + editor.value.slice(end);
editor.selectionStart = editor.selectionEnd = start;
} else {
document.execCommand('cut');
}
} catch (err) {
alert('Cut failed: ' + err);
}
}
async function pasteText() {
try {
let text = '';
if (navigator.clipboard) {
text = await navigator.clipboard.readText();
} else {
alert('Paste is not supported by your browser.');
return;
}
const start = editor.selectionStart;
const end = editor.selectionEnd;
editor.value = editor.value.slice(0, start) + text + editor.value.slice(end);
editor.selectionStart = editor.selectionEnd = start + text.length;
editor.focus();
} catch (err) {
alert('Paste failed: ' + err);
}
}
copyBtn.addEventListener('click', copyText);
cutBtn.addEventListener('click', cutText);
pasteBtn.addEventListener('click', pasteText);
// Keyboard shortcuts (including Ctrl+A for Select All)
window.addEventListener('keydown', function (e) {
if (e.ctrlKey || e.metaKey) { // also support Cmd key on Mac
switch (e.key.toLowerCase()) {
case 'n': // Ctrl+N = New
e.preventDefault();
newFile();
break;
case 'o': // Ctrl+O = Open
e.preventDefault();
openBtn.click();
break;
case 's': // Ctrl+S = Save
e.preventDefault();
saveBtn.click();
break;
case 'a': // Ctrl+A = Select All
e.preventDefault();
selectAllText();
break;
}
}
});
</script>
</body>
</html>
<button id="selectAllBtn" title="Select All (Ctrl+A)">Select All</button> in the toolbar.selectAllText() function that focuses the editor and selects all its content.Try it out by clicking Select All or pressing Ctrl+A inside the editor.
Let me know if you'd like to add or modify anything else!
Not yet included in a published release.