kym / index.html
Mslammm's picture
اعطيني مربع نص نكتب داخله عندما نضغط عليه فيه امكانيات التكبير والتصغير وكل ما يلزمة لتحديدة على الصفحة والكتابة عليه بكل اريحية - Initial Deployment
26986ca verified
Raw History Blame Contribute Delete
7.89 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Text Box</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/feather-icons"></script>
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
<style>
.text-box {
position: absolute;
min-width: 200px;
min-height: 100px;
border: 2px dashed #4F46E5;
border-radius: 8px;
padding: 16px;
background-color: rgba(255, 255, 255, 0.9);
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
resize: both;
overflow: auto;
cursor: move;
}
.text-box:focus {
outline: none;
border: 2px solid #4F46E5;
}
.controls {
position: absolute;
top: -30px;
right: 0;
display: flex;
gap: 4px;
background: white;
padding: 4px;
border-radius: 4px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.control-btn {
width: 24px;
height: 24px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 4px;
background: #E5E7EB;
cursor: pointer;
transition: all 0.2s;
}
.control-btn:hover {
background: #D1D5DB;
}
.control-btn i {
width: 16px;
height: 16px;
}
</style>
</head>
<body class="bg-gray-100 min-h-screen p-4">
<div class="container mx-auto">
<h1 class="text-3xl font-bold text-indigo-600 mb-6">Interactive Text Box</h1>
<p class="text-gray-600 mb-8">Click anywhere on the page to create a new editable text box. Drag, resize, and edit as needed.</p>
<div id="workspace" class="relative min-h-[500px] bg-white rounded-lg shadow-md p-4 border-2 border-dashed border-gray-300">
<!-- Text boxes will be added here -->
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
feather.replace();
const workspace = document.getElementById('workspace');
let activeTextBox = null;
workspace.addEventListener('click', (e) => {
if (e.target === workspace) {
createTextBox(e.clientX - workspace.getBoundingClientRect().left,
e.clientY - workspace.getBoundingClientRect().top);
}
});
function createTextBox(x, y) {
const textBox = document.createElement('div');
textBox.className = 'text-box';
textBox.contentEditable = 'true';
textBox.style.left = `${x}px`;
textBox.style.top = `${y}px`;
// Create controls
const controls = document.createElement('div');
controls.className = 'controls';
// Font size increase
const increaseBtn = document.createElement('div');
increaseBtn.className = 'control-btn';
increaseBtn.innerHTML = '<i data-feather="plus"></i>';
increaseBtn.addEventListener('click', (e) => {
e.stopPropagation();
const currentSize = parseInt(window.getComputedStyle(textBox).fontSize) || 16;
textBox.style.fontSize = `${currentSize + 2}px`;
});
// Font size decrease
const decreaseBtn = document.createElement('div');
decreaseBtn.className = 'control-btn';
decreaseBtn.innerHTML = '<i data-feather="minus"></i>';
decreaseBtn.addEventListener('click', (e) => {
e.stopPropagation();
const currentSize = parseInt(window.getComputedStyle(textBox).fontSize) || 16;
textBox.style.fontSize = `${Math.max(8, currentSize - 2)}px`;
});
// Bold
const boldBtn = document.createElement('div');
boldBtn.className = 'control-btn';
boldBtn.innerHTML = '<i data-feather="bold"></i>';
boldBtn.addEventListener('click', (e) => {
e.stopPropagation();
document.execCommand('bold', false, null);
});
// Italic
const italicBtn = document.createElement('div');
italicBtn.className = 'control-btn';
italicBtn.innerHTML = '<i data-feather="italic"></i>';
italicBtn.addEventListener('click', (e) => {
e.stopPropagation();
document.execCommand('italic', false, null);
});
// Delete
const deleteBtn = document.createElement('div');
deleteBtn.className = 'control-btn';
deleteBtn.innerHTML = '<i data-feather="trash-2"></i>';
deleteBtn.addEventListener('click', (e) => {
e.stopPropagation();
textBox.remove();
});
controls.appendChild(increaseBtn);
controls.appendChild(decreaseBtn);
controls.appendChild(boldBtn);
controls.appendChild(italicBtn);
controls.appendChild(deleteBtn);
textBox.appendChild(controls);
// Make draggable
let offsetX, offsetY;
textBox.addEventListener('mousedown', (e) => {
if (e.target === textBox || e.target.parentNode === textBox) {
activeTextBox = textBox;
offsetX = e.clientX - textBox.getBoundingClientRect().left;
offsetY = e.clientY - textBox.getBoundingClientRect().top;
// Bring to front
document.querySelectorAll('.text-box').forEach(box => {
box.style.zIndex = '1';
});
textBox.style.zIndex = '10';
}
});
document.addEventListener('mousemove', (e) => {
if (activeTextBox) {
const workspaceRect = workspace.getBoundingClientRect();
const newX = e.clientX - workspaceRect.left - offsetX;
const newY = e.clientY - workspaceRect.top - offsetY;
// Constrain within workspace
const maxX = workspaceRect.width - activeTextBox.offsetWidth;
const maxY = workspaceRect.height - activeTextBox.offsetHeight;
activeTextBox.style.left = `${Math.max(0, Math.min(newX, maxX))}px`;
activeTextBox.style.top = `${Math.max(0, Math.min(newY, maxY))}px`;
}
});
document.addEventListener('mouseup', () => {
activeTextBox = null;
});
workspace.appendChild(textBox);
textBox.focus();
feather.replace();
}
});
</script>
<script>feather.replace();</script>
</body>
</html>