Explore // board.js let scale = 0.5; // Initial scale let translateX = 0; let translateY = 0;… Save Share Download ▾ Report// board.js
let scale = 0.5; // Initial scale
let translateX = 0;
let translateY = 0;
const board = document.getElementById('board');
const boardWrapper = document.getElementById('board-wrapper');
// Constants for zoom
const MIN_SCALE = 0.1; // Minimum scale
const MAX_SCALE = 5; // Maximum scale
const ZOOM_FACTOR = 0.1; // Zoom speed
// Apply initial scale
board.style.transform = translate(${translateX}px, ${translateY}px) scale(${scale});
boardWrapper.style.backgroundPosition = ${translateX}px ${translateY}px;
// Show images after setting scale
document.querySelectorAll('.image').forEach(img => {
img.classList.remove('hidden');
});
boardWrapper.addEventListener('wheel', (e) => {
e.preventDefault();
code Copy code
const previousScale = scale;
scale += e.deltaY < 0 ? ZOOM_FACTOR : -ZOOM_FACTOR;
scale = Math.min(Math.max(MIN_SCALE, scale), MAX_SCALE);
// Calculate cursor position relative to the board
const rect = board.getBoundingClientRect();
const offsetX = (e.clientX - rect.left) / previousScale;
const offsetY = (e.clientY - rect.top) / previousScale;
// Adjust translate to keep cursor point at the same board position
translateX -= offsetX * (scale - previousScale);
translateY -= offsetY * (scale - previousScale);
board.style.transform = `translate(${translateX}px, ${translateY}px) scale(${scale})`;
boardWrapper.style.backgroundPosition = `${translateX}px ${translateY}px`;
});
boardWrapper.addEventListener('touchstart', (e) => {
if (e.touches.length === 2) {
e.preventDefault();
const touch1 = e.touches[0];
const touch2 = e.touches[1];
const startDistance = Math.hypot(touch2.clientX - touch1.clientX, touch2.clientY - touch1.clientY);
const startScale = scale;
Show full message · 450 lines Assistant2024-10-30 07:44 UTC Copy response
Copy first prompt
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language en
Conversation date Oct 30, 2024
Added to archive Nov 16, 2026
ID 01m7h422hw3qg1xa584g3azw7cSource & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: 698a6d80c23bfb20d48d5c225f50adeb
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
function zoom(e) {
if (e.touches.length !== 2) return;
const touch1 = e.touches[0];
const touch2 = e.touches[1];
const currentDistance = Math.hypot(touch2.clientX - touch1.clientX, touch2.clientY - touch1.clientY);
const newScale = startScale * (currentDistance / startDistance);
scale = Math.min(Math.max(MIN_SCALE, newScale), MAX_SCALE);
// Calculate midpoint of touches
const midX = (touch1.clientX + touch2.clientX) / 2;
const midY = (touch1.clientY + touch2.clientY) / 2;
// Calculate offset relative to previous scale
const rect = board.getBoundingClientRect();
const offsetX = (midX - rect.left) / previousScale;
const offsetY = (midY - rect.top) / previousScale;
// Adjust translate to keep midpoint at same board position
translateX -= offsetX * (scale - startScale);
translateY -= offsetY * (scale - startScale);
board.style.transform = `translate(${translateX}px, ${translateY}px) scale(${scale})`;
boardWrapper.style.backgroundPosition = `${translateX}px ${translateY}px`;
}
document.addEventListener('touchmove', zoom);
document.addEventListener('touchend', () => {
document.removeEventListener('touchmove', zoom);
}, { once: true });
}
let isPanning = false;
let startPanX, startPanY;
boardWrapper.addEventListener('mousedown', (e) => {
if (e.button === 2) { // Right mouse button
isPanning = true;
startPanX = e.clientX - translateX;
startPanY = e.clientY - translateY;
boardWrapper.style.cursor = 'grabbing';
}
});
boardWrapper.addEventListener('mouseup', (e) => {
if (e.button === 2) { // Right mouse button
isPanning = false;
boardWrapper.style.cursor = 'grab';
}
});
boardWrapper.addEventListener('mousemove', (e) => {
if (!isPanning) return;
e.preventDefault();
translateX = e.clientX - startPanX;
translateY = e.clientY - startPanY;
board.style.transform = translate(${translateX}px, ${translateY}px) scale(${scale});
boardWrapper.style.backgroundPosition = ${translateX}px ${translateY}px;
});
// Add touch events for panning
boardWrapper.addEventListener('touchstart', (e) => {
if (e.touches.length === 1) {
isPanning = true;
startPanX = e.touches[0].clientX - translateX;
startPanY = e.touches[0].clientY - translateY;
boardWrapper.style.cursor = 'grabbing';
}
});
boardWrapper.addEventListener('touchend', (e) => {
isPanning = false;
boardWrapper.style.cursor = 'grab';
});
boardWrapper.addEventListener('touchmove', (e) => {
if (!isPanning) return;
e.preventDefault();
translateX = e.touches[0].clientX - startPanX;
translateY = e.touches[0].clientY - startPanY;
board.style.transform = translate(${translateX}px, ${translateY}px) scale(${scale});
boardWrapper.style.backgroundPosition = ${translateX}px ${translateY}px;
});
// Prevent context menu on board-wrapper
boardWrapper.addEventListener('contextmenu', (e) => {
e.preventDefault();
});
// Assumes uploadFile, showContextMenu, addResizeListeners are available globally
socket.on('new_image', (data) => {
const image = createImageElement(data);
document.getElementById('board').appendChild(image);
});
socket.on('updated_image', (data) => {
const image = document.querySelector(.image[data-id="{data.id}"]);
if (image) {
image.style.left = {data.x}px;
image.style.top = d a t a . y p x ; i m a g e . s t y l e . w i d t h = {data.y}px;
image.style.width = d a t a . y p x ; ima g e . s t y l e . w i d t h = {data.width}px;
image.style.height = ${data.height}px;
image.querySelector('img').src = data.url;
}
});
socket.on('element_deleted', (data) => {
if (data.type === 'image') {
const element = document.querySelector(.image[data-id="${data.id}"]);
if (element) {
element.remove();
}
}
});
function createImageElement(data) {
const image = document.createElement('div');
image.className = 'image';
image.dataset.id = data.id;
image.style.left = d a t a . x p x ; i m a g e . s t y l e . t o p = {data.x}px;
image.style.top = d a t a . x p x ; ima g e . s t y l e . t o p = {data.y}px;
image.style.width = d a t a . w i d t h p x ; i m a g e . s t y l e . h e i g h t = {data.width}px;
image.style.height = d a t a . w i d t h p x ; ima g e . s t y l e . h e i g h t = {data.height}px;
const img = document.createElement('img');
img.src = data.url;
img.style.width = '100%';
img.style.height = '100%';
img.style.objectFit = 'contain'; // Maintains aspect ratio
addImageEventListeners(image, data.id);
function addImageEventListeners(image, id) {
image.addEventListener('mousedown', (e) => {
if (e.button !== 0) return; // Left mouse button
e.preventDefault();
e.stopPropagation();
const startX = e.clientX;
const startY = e.clientY;
const startLeft = parseFloat(image.style.left) || 0;
const startTop = parseFloat(image.style.top) || 0;
function moveImage(e) {
const deltaX = (e.clientX - startX) / scale;
const deltaY = (e.clientY - startY) / scale;
image.style.left = s t a r t L e f t + d e l t a X p x ; i m a g e . s t y l e . t o p = {startLeft + deltaX}px;
image.style.top = s t a r t L e f t + d e l t a X p x ; ima g e . s t y l e . t o p = {startTop + deltaY}px;
const x = parseFloat(image.style.left) || 0;
const y = parseFloat(image.style.top) || 0;
const width = image.offsetWidth;
const height = image.offsetHeight;
socket.emit('update_image', { id, url: image.querySelector('img').src, x, y, width, height });
}
document.addEventListener('mousemove', moveImage);
document.addEventListener('mouseup', () => {
document.removeEventListener('mousemove', moveImage);
}, { once: true });
});
image.addEventListener('touchstart', (e) => {
e.preventDefault();
e.stopPropagation();
const startX = e.touches[0].clientX;
const startY = e.touches[0].clientY;
const startLeft = parseFloat(image.style.left) || 0;
const startTop = parseFloat(image.style.top) || 0;
function moveImage(e) {
const deltaX = (e.touches[0].clientX - startX) / scale;
const deltaY = (e.touches[0].clientY - startY) / scale;
image.style.left = s t a r t L e f t + d e l t a X p x ; i m a g e . s t y l e . t o p = {startLeft + deltaX}px;
image.style.top = s t a r t L e f t + d e l t a X p x ; ima g e . s t y l e . t o p = {startTop + deltaY}px;
const x = parseFloat(image.style.left) || 0;
const y = parseFloat(image.style.top) || 0;
const width = image.offsetWidth;
const height = image.offsetHeight;
socket.emit('update_image', { id, url: image.querySelector('img').src, x, y, width, height });
}
document.addEventListener('touchmove', moveImage);
document.addEventListener('touchend', () => {
document.removeEventListener('touchmove', moveImage);
}, { once: true });
});
image.addEventListener('dblclick', (e) => {
e.preventDefault();
e.stopPropagation();
showContextMenu(image, id, e.clientX, e.clientY);
});
image.addEventListener('touchstart', (e) => {
if (e.touches.length === 2) {
e.preventDefault();
e.stopPropagation();
showContextMenu(image, id, e.touches[0].clientX, e.touches[0].clientY);
}
});
addResizeListeners(image, id, 'image');
}
function handleExistingImages() {
document.querySelectorAll('.image').forEach(image => {
const id = image.dataset.id;
const url = image.querySelector('img').src;
const x = image.offsetLeft;
const y = image.offsetTop;
const width = image.offsetWidth;
const height = image.offsetHeight;
image.addEventListener('dblclick', (e) => {
e.preventDefault();
e.stopPropagation();
showContextMenu(image, id, e.clientX, e.clientY);
});
addImageEventListeners(image, id);
});
}
// Assumes uploadFile, showContextMenu, addResizeListeners are available globally
socket.on('new_note', (data) => {
const note = createNoteElement(data);
document.getElementById('board').appendChild(note);
});
socket.on('updated_note', (data) => {
const note = document.querySelector(.note[data-id="{data.id}"]);
if (note) {
note.style.left = {data.x}px;
note.style.top = d a t a . y p x ; n o t e . s t y l e . w i d t h = {data.y}px;
note.style.width = d a t a . y p x ; n o t e . s t y l e . w i d t h = {data.width}px;
note.style.height = ${data.height}px;
note.querySelector('textarea').value = data.text;
}
});
socket.on('element_deleted', (data) => {
if (data.type === 'note') {
const element = document.querySelector(.note[data-id="${data.id}"]);
if (element) {
element.remove();
}
}
});
function createNoteElement(data) {
const note = document.createElement('div');
note.className = 'note';
note.dataset.id = data.id;
note.style.left = d a t a . x p x ; n o t e . s t y l e . t o p = {data.x}px;
note.style.top = d a t a . x p x ; n o t e . s t y l e . t o p = {data.y}px;
note.style.width = d a t a . w i d t h p x ; n o t e . s t y l e . h e i g h t = {data.width}px;
note.style.height = d a t a . w i d t h p x ; n o t e . s t y l e . h e i g h t = {data.height}px;
const textarea = document.createElement('textarea');
textarea.value = data.text;
textarea.addEventListener('input', () => {
const x = note.offsetLeft;
const y = note.offsetTop;
const width = note.offsetWidth;
const height = note.offsetHeight;
socket.emit('update_note', { id: data.id, text: textarea.value, x, y, width, height });
});
textarea.addEventListener('mousedown', (e) => {
e.stopPropagation();
});
note.appendChild(textarea);
addNoteEventListeners(note, data.id);
function addNoteEventListeners(note, id) {
note.addEventListener('mousedown', (e) => {
if (e.button !== 0) return; // Left mouse button
e.preventDefault();
e.stopPropagation();
const startX = e.clientX;
const startY = e.clientY;
const startLeft = parseFloat(note.style.left) || 0;
const startTop = parseFloat(note.style.top) || 0;
function moveNote(e) {
const deltaX = (e.clientX - startX) / scale;
const deltaY = (e.clientY - startY) / scale;
note.style.left = s t a r t L e f t + d e l t a X p x ; n o t e . s t y l e . t o p = {startLeft + deltaX}px;
note.style.top = s t a r t L e f t + d e l t a X p x ; n o t e . s t y l e . t o p = {startTop + deltaY}px;
const x = parseFloat(note.style.left) || 0;
const y = parseFloat(note.style.top) || 0;
const width = note.offsetWidth;
const height = note.offsetHeight;
socket.emit('update_note', { id, text: note.querySelector('textarea').value, x, y, width, height });
}
document.addEventListener('mousemove', moveNote);
document.addEventListener('mouseup', () => {
document.removeEventListener('mousemove', moveNote);
}, { once: true });
});
note.addEventListener('touchstart', (e) => {
e.preventDefault();
e.stopPropagation();
const startX = e.touches[0].clientX;
const startY = e.touches[0].clientY;
const startLeft = parseFloat(note.style.left) || 0;
const startTop = parseFloat(note.style.top) || 0;
function moveNote(e) {
const deltaX = (e.touches[0].clientX - startX) / scale;
const deltaY = (e.touches[0].clientY - startY) / scale;
note.style.left = s t a r t L e f t + d e l t a X p x ; n o t e . s t y l e . t o p = {startLeft + deltaX}px;
note.style.top = s t a r t L e f t + d e l t a X p x ; n o t e . s t y l e . t o p = {startTop + deltaY}px;
const x = parseFloat(note.style.left) || 0;
const y = parseFloat(note.style.top) || 0;
const width = note.offsetWidth;
const height = note.offsetHeight;
socket.emit('update_note', { id, text: note.querySelector('textarea').value, x, y, width, height });
}
document.addEventListener('touchmove', moveNote);
document.addEventListener('touchend', () => {
document.removeEventListener('touchmove', moveNote);
}, { once: true });
});
note.addEventListener('dblclick', (e) => {
e.preventDefault();
e.stopPropagation();
showContextMenu(note, id, e.clientX, e.clientY);
});
note.addEventListener('touchstart', (e) => {
if (e.touches.length === 2) {
e.preventDefault();
e.stopPropagation();
showContextMenu(note, id, e.touches[0].clientX, e.touches[0].clientY);
}
});
addResizeListeners(note, id, 'note');
}
function handleExistingNotes() {
document.querySelectorAll('.note').forEach(note => {
const id = note.dataset.id;
const text = note.querySelector('textarea').value;
const x = note.offsetLeft;
const y = note.offsetTop;
const width = note.offsetWidth;
const height = note.offsetHeight;
note.querySelector('textarea').addEventListener('input', () => {
const x = note.offsetLeft;
const y = note.offsetTop;
const width = note.offsetWidth;
const height = note.offsetHeight;
socket.emit('update_note', { id, text: note.querySelector('textarea').value, x, y, width, height });
});
note.querySelector('textarea').addEventListener('mousedown', (e) => {
e.stopPropagation();
});
note.addEventListener('dblclick', (e) => {
e.preventDefault();
e.stopPropagation();
showContextMenu(note, id, e.clientX, e.clientY);
});
addNoteEventListeners(note, id);
});
}
Как мне сделать чтобы перемещать элемент с помощью тачпада я мог только при двойном нажатии. Тоесть первое нажатие короткое, а второе зажимаю и перетаскиваю. Управление мышью не должно быть нарушено
To implement a system where elements can only be moved via the touchpad after a double-tap (or double-press) while keeping regular mouse interactions unaffected, you'll need to differentiate between touch-based interactions and mouse-based interactions.
Here's a step-by-step guide on how to achieve this:
Distinguish Between Mouse and Touch Inputs:
Utilize Pointer Events (pointerdown, pointermove, etc.) which provide a pointerType property. This helps in identifying whether the interaction is from a mouse, touch, or pen input.
Implement Double-Tap Detection for Touch Inputs:
For touch inputs, require a double-tap to activate the dragging functionality. This ensures that single taps (which might be unintentional) do not trigger dragging.
Maintain Regular Mouse Dragging:
For mouse interactions, keep the existing dragging functionality intact without any modifications.
Prevent Conflicts Between Touch and Mouse Events:
Ensure that the drag operations triggered by touch events do not interfere with mouse event handlers and vice versa.
Below is an updated version of your images.js and notes.js files incorporating these changes, along with explanations.
Updated images.js
javascript Copy code
// images.js
// Assumes uploadFile, showContextMenu, addResizeListeners are available globally
let lastTap = 0; // Track the last tap time for double-tap detection
const DOUBLE_TAP_DELAY = 300; // Time threshold for double-tap in ms
socket.on('new_image', (data) => {
const image = createImageElement(data);
document.getElementById('board').appendChild(image);
});
socket.on('updated_image', (data) => {
const image = document.querySelector(`.image[data-id="${data.id}"]`);
if (image) {
image.style.left = `${data.x}px`;
image.style.top = `${data.y}px`;
image.style.width = `${data.width}px`;
image.style.height = `${data.height}px`;
image.querySelector('img').src = data.url;
}
});
socket.on('element_deleted', (data) => {
if (data.type === 'image') {
const element = document.querySelector(`.image[data-id="${data.id}"]`);
if (element) {
element.remove();
}
}
});
function createImageElement(data) {
const image = document.createElement('div');
image.className = 'image';
image.dataset.id = data.id;
image.style.left = `${data.x}px`;
image.style.top = `${data.y}px`;
image.style.width = `${data.width}px`;
image.style.height = `${data.height}px`;
const img = document.createElement('img');
img.src = data.url;
img.style.width = '100%';
img.style.height = '100%';
img.style.objectFit = 'contain'; // Maintains aspect ratio
image.appendChild(img);
addImageEventListeners(image, data.id);
return image;
}
function addImageEventListeners(image, id) {
let isDragging = false;
let dragStartX, dragStartY;
let initialLeft, initialTop;
// Pointer event handler to unify mouse and touch interactions
image.addEventListener('pointerdown', (e) => {
if (e.pointerType === 'touch') {
const currentTime = new Date().getTime();
const tapLength = currentTime - lastTap;
if (tapLength < DOUBLE_TAP_DELAY && tapLength > 0) {
// Double-tap detected, initiate dragging
isDragging = true;
dragStartX = e.clientX;
dragStartY = e.clientY;
initialLeft = parseFloat(image.style.left) || 0;
initialTop = parseFloat(image.style.top) || 0;
image.setPointerCapture(e.pointerId);
} else {
// Not a double-tap, ignore or handle single-tap if needed
}
lastTap = currentTime;
} else if (e.pointerType === 'mouse') {
// Regular mouse dragging
isDragging = true;
dragStartX = e.clientX;
dragStartY = e.clientY;
initialLeft = parseFloat(image.style.left) || 0;
initialTop = parseFloat(image.style.top) || 0;
image.setPointerCapture(e.pointerId);
}
});
image.addEventListener('pointermove', (e) => {
if (!isDragging) return;
const deltaX = (e.clientX - dragStartX) / scale;
const deltaY = (e.clientY - dragStartY) / scale;
image.style.left = `${initialLeft + deltaX}px`;
image.style.top = `${initialTop + deltaY}px`;
const x = parseFloat(image.style.left) || 0;
const y = parseFloat(image.style.top) || 0;
const width = image.offsetWidth;
const height = image.offsetHeight;
socket.emit('update_image', { id, url: image.querySelector('img').src, x, y, width, height });
});
image.addEventListener('pointerup', (e) => {
if (isDragging) {
isDragging = false;
image.releasePointerCapture(e.pointerId);
}
});
image.addEventListener('pointercancel', (e) => {
if (isDragging) {
isDragging = false;
image.releasePointerCapture(e.pointerId);
}
});
// Prevent default touch behavior to avoid conflicts
image.addEventListener('touchstart', (e) => {
e.preventDefault();
}, { passive: false });
image.addEventListener('dblclick', (e) => {
e.preventDefault();
e.stopPropagation();
showContextMenu(image, id, e.clientX, e.clientY);
});
image.addEventListener('contextmenu', (e) => {
e.preventDefault();
});
addResizeListeners(image, id, 'image');
}
function handleExistingImages() {
document.querySelectorAll('.image').forEach(image => {
const id = image.dataset.id;
const url = image.querySelector('img').src;
const x = image.offsetLeft;
const y = image.offsetTop;
const width = image.offsetWidth;
const height = image.offsetHeight;
image.addEventListener('dblclick', (e) => {
e.preventDefault();
e.stopPropagation();
showContextMenu(image, id, e.clientX, e.clientY);
});
addImageEventListeners(image, id);
});
}
Updated notes.js
Explanation of Changes
Using Pointer Events:
Pointer Events provide a unified way to handle different input types (mouse, touch, pen). They include properties like pointerType which make it easier to differentiate input sources.
Replaced mousedown, mousemove, and mouseup with pointerdown, pointermove, and pointerup respectively. This change ensures better handling of different input types.
Double-Tap Detection:
Introduced lastTap (for images) and lastTapNote (for notes) variables to track the time of the last tap.
On a pointerdown event, check if the time since the last tap is within the DOUBLE_TAP_DELAY. If so, consider it a double-tap and initiate dragging.
This mechanism ensures that dragging via touchpad requires a deliberate double-tap, preventing accidental drags from single taps or swipe gestures.
Maintaining Mouse Functionality:
For pointerType === 'mouse', the dragging functionality remains unchanged. This ensures that users can continue to drag elements using the mouse as before.
Preventing Default Behavior:
Added touchstart event listeners with e.preventDefault() to prevent default touch behaviors (like scrolling or zooming) that might interfere with the custom drag functionality. The { passive: false } option is specified to allow preventDefault() to work on touch events.
Pointer Capture:
Used setPointerCapture and releasePointerCapture to ensure that the element captures all pointer events during the drag operation. This improves the reliability of drag interactions.
Context Menu Handling:
Added a contextmenu event listener to prevent the default context menu from appearing, which could interfere with custom context menus.
Preventing Event Propagation:
In touch-specific event handlers, used e.stopPropagation() to prevent events from bubbling up and potentially triggering unintended behaviors.
Additional Considerations
Accessibility:
Ensure that the double-tap requirement does not hinder accessibility. Provide alternative means for users who might have difficulty performing double-taps.
Feedback to Users:
Providing visual feedback (like highlighting the element or showing a drag indicator) when an element is ready to be dragged after a double-tap can enhance user experience.
Debouncing:
Ensure that the double-tap detection logic is robust and accounts for edge cases, such as very fast or very slow taps.
Testing Across Devices:
Test the implementation across various devices (touchscreens, touchpads, mice) to ensure consistent behavior.
Final Thoughts
By implementing the above changes, you'll ensure that elements on the board can only be moved via the touchpad after a deliberate double-tap, while regular mouse dragging remains unaffected. This approach strikes a balance between preventing accidental drags and maintaining intuitive interactions for desktop users.
Show full message · 403 lines Copy first response
Not yet included in a published release.
// notes.js
// Assumes uploadFile, showContextMenu, addResizeListeners are available globally
let lastTapNote = 0; // Track the last tap time for double-tap detection
const DOUBLE_TAP_DELAY = 300; // Time threshold for double-tap in ms
socket.on('new_note', (data) => {
const note = createNoteElement(data);
document.getElementById('board').appendChild(note);
});
socket.on('updated_note', (data) => {
const note = document.querySelector(`.note[data-id="${data.id}"]`);
if (note) {
note.style.left = `${data.x}px`;
note.style.top = `${data.y}px`;
note.style.width = `${data.width}px`;
note.style.height = `${data.height}px`;
note.querySelector('textarea').value = data.text;
}
});
socket.on('element_deleted', (data) => {
if (data.type === 'note') {
const element = document.querySelector(`.note[data-id="${data.id}"]`);
if (element) {
element.remove();
}
}
});
function createNoteElement(data) {
const note = document.createElement('div');
note.className = 'note';
note.dataset.id = data.id;
note.style.left = `${data.x}px`;
note.style.top = `${data.y}px`;
note.style.width = `${data.width}px`;
note.style.height = `${data.height}px`;
const textarea = document.createElement('textarea');
textarea.value = data.text;
textarea.addEventListener('input', () => {
const x = note.offsetLeft;
const y = note.offsetTop;
const width = note.offsetWidth;
const height = note.offsetHeight;
socket.emit('update_note', { id: data.id, text: textarea.value, x, y, width, height });
});
textarea.addEventListener('mousedown', (e) => {
e.stopPropagation();
});
note.appendChild(textarea);
addNoteEventListeners(note, data.id);
return note;
}
function addNoteEventListeners(note, id) {
let isDragging = false;
let dragStartX, dragStartY;
let initialLeft, initialTop;
// Pointer event handler to unify mouse and touch interactions
note.addEventListener('pointerdown', (e) => {
if (e.pointerType === 'touch') {
const currentTime = new Date().getTime();
const tapLength = currentTime - lastTapNote;
if (tapLength < DOUBLE_TAP_DELAY && tapLength > 0) {
// Double-tap detected, initiate dragging
isDragging = true;
dragStartX = e.clientX;
dragStartY = e.clientY;
initialLeft = parseFloat(note.style.left) || 0;
initialTop = parseFloat(note.style.top) || 0;
note.setPointerCapture(e.pointerId);
} else {
// Not a double-tap, ignore or handle single-tap if needed
}
lastTapNote = currentTime;
} else if (e.pointerType === 'mouse') {
// Regular mouse dragging
isDragging = true;
dragStartX = e.clientX;
dragStartY = e.clientY;
initialLeft = parseFloat(note.style.left) || 0;
initialTop = parseFloat(note.style.top) || 0;
note.setPointerCapture(e.pointerId);
}
});
note.addEventListener('pointermove', (e) => {
if (!isDragging) return;
const deltaX = (e.clientX - dragStartX) / scale;
const deltaY = (e.clientY - dragStartY) / scale;
note.style.left = `${initialLeft + deltaX}px`;
note.style.top = `${initialTop + deltaY}px`;
const x = parseFloat(note.style.left) || 0;
const y = parseFloat(note.style.top) || 0;
const width = note.offsetWidth;
const height = note.offsetHeight;
socket.emit('update_note', { id, text: note.querySelector('textarea').value, x, y, width, height });
});
note.addEventListener('pointerup', (e) => {
if (isDragging) {
isDragging = false;
note.releasePointerCapture(e.pointerId);
}
});
note.addEventListener('pointercancel', (e) => {
if (isDragging) {
isDragging = false;
note.releasePointerCapture(e.pointerId);
}
});
// Prevent default touch behavior to avoid conflicts
note.addEventListener('touchstart', (e) => {
e.preventDefault();
}, { passive: false });
note.querySelector('textarea').addEventListener('dblclick', (e) => {
e.preventDefault();
e.stopPropagation();
showContextMenu(note, id, e.clientX, e.clientY);
});
note.addEventListener('contextmenu', (e) => {
e.preventDefault();
});
addResizeListeners(note, id, 'note');
}
function handleExistingNotes() {
document.querySelectorAll('.note').forEach(note => {
const id = note.dataset.id;
const text = note.querySelector('textarea').value;
const x = note.offsetLeft;
const y = note.offsetTop;
const width = note.offsetWidth;
const height = note.offsetHeight;
note.querySelector('textarea').addEventListener('input', () => {
const x = note.offsetLeft;
const y = note.offsetTop;
const width = note.offsetWidth;
const height = note.offsetHeight;
socket.emit('update_note', { id, text: note.querySelector('textarea').value, x, y, width, height });
});
note.querySelector('textarea').addEventListener('mousedown', (e) => {
e.stopPropagation();
});
note.addEventListener('dblclick', (e) => {
e.preventDefault();
e.stopPropagation();
showContextMenu(note, id, e.clientX, e.clientY);
});
addNoteEventListeners(note, id);
});
}