Explore #images.js // images.js // Assumes uploadFile, showContextMenu, addResizeListeners are… Save Share Download ▾ Report#images.js
// images.js
// 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;
}
});
Show full message · 315 lines Assistant2024-10-30 07:34 UTC Copy response
Assistant2024-10-30 07:37 UTC Copy response
элементы вообще теперь не перемещаются
Assistant2024-10-30 07:39 UTC Copy response
Copy first prompt
Other models, same or similar prompt Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 3 (6 messages)
Language en
Conversation date Oct 30, 2024
Added to archive Nov 16, 2026
ID 01m7h422hwkc6q0c5kj8j4p3v7Source & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: 20843f983b4bbd32e213dc82c1a51d8c
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
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;
code Copy code
function moveImage(e) {
const deltaX = (e.clientX - startX) / scale;
const deltaY = (e.clientY - startY) / scale;
image.style.left = `${startLeft + deltaX}px`;
image.style.top = `${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;
code Copy code
function moveImage(e) {
const deltaX = (e.touches[0].clientX - startX) / scale;
const deltaY = (e.touches[0].clientY - startY) / scale;
image.style.left = `${startLeft + deltaX}px`;
image.style.top = `${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;
code Copy code
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;
code Copy code
function moveNote(e) {
const deltaX = (e.clientX - startX) / scale;
const deltaY = (e.clientY - startY) / scale;
note.style.left = `${startLeft + deltaX}px`;
note.style.top = `${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;
code Copy code
function moveNote(e) {
const deltaX = (e.touches[0].clientX - startX) / scale;
const deltaY = (e.touches[0].clientY - startY) / scale;
note.style.left = `${startLeft + deltaX}px`;
note.style.top = `${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;
code Copy code
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);
Как мне сделать чтобы перемещать элемент с помощью тачпада я мог только при двойном нажатии. Тоесть первое нажатие короткое, а второе зажимаю и перетаскиваю. Управление мышью не должно быть нарушено
Для реализации перемещения элемента с помощью тачпада при двойном нажатии, где первое нажатие короткое, а второе удерживается для перемещения, нужно изменить обработку событий touchstart и touchend в вашем JavaScript коде. Управление мышью останется без изменений.
Вот модификация кода, чтобы достичь этого эффекта. Мы будем использовать setTimeout и clearTimeout для определения двойного нажатия.
Модифицируйте обработчики touchstart и touchend следующим образом:
В файле images.js:
javascript Copy code
let touchTimeout;
function addImageEventListeners(image, id) {
image.addEventListener('mousedown', (e) => {
if (e.button !== 0) return; // Left mouse button
e.preventDefault();
e.stopPropagation();
startDrag(e, image, id);
});
image.addEventListener('touchstart', (e) => {
if (e.touches.length === 1) {
e.preventDefault();
e.stopPropagation();
touchTimeout = setTimeout(() => {
startDrag(e.touches[0], image, id);
}, 250);
}
});
image.addEventListener('touchend', () => {
clearTimeout(touchTimeout);
});
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 startDrag(e, image, id) {
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 = `${startLeft + deltaX}px`;
image.style.top = `${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('touchmove', (e) => moveImage(e.touches[0]), false);
document.addEventListener('mouseup', () => {
document.removeEventListener('mousemove', moveImage);
}, { once: true });
document.addEventListener('touchend', () => {
document.removeEventListener('touchmove', moveImage);
}, { once: true });
}
В файле notes.js примените аналогичные изменения:
javascript Copy code
let touchTimeout;
function addNoteEventListeners(note, id) {
note.addEventListener('mousedown', (e) => {
if (e.button !== 0) return; // Left mouse button
e.preventDefault();
e.stopPropagation();
startDrag(e, note, id);
});
note.addEventListener('touchstart', (e) => {
if (e.touches.length === 1) {
e.preventDefault();
e.stopPropagation();
touchTimeout = setTimeout(() => {
startDrag(e.touches[0], note, id);
}, 250);
}
});
note.addEventListener('touchend', () => {
clearTimeout(touchTimeout);
});
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 startDrag(e, note, id) {
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 = `${startLeft + deltaX}px`;
note.style.top = `${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('touchmove', (e) => moveNote(e.touches[0]), false);
document.addEventListener('mouseup', () => {
document.removeEventListener('mousemove', moveNote);
}, { once: true });
document.addEventListener('touchend', () => {
document.removeEventListener('touchmove', moveNote);
}, { once: true });
}
Теперь, чтобы переместить элемент с помощью тачпада, вы должны быстро коснуться для первого нажатия и затем удерживать второе прикосновение для перетаскивания элемента. Управление мышью останется прежним.
Конечно! Ниже представлен обновленный полный код для обоих файлов images.js и notes.js, с учетом требования перемещать элементы с помощью тачпада при двойном нажатии.
images.js
javascript Copy code
// images.js
// Assumes uploadFile, showContextMenu, addResizeListeners are available globally
let touchTimeout;
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) {
image.addEventListener('mousedown', (e) => {
if (e.button !== 0) return; // Left mouse button
e.preventDefault();
e.stopPropagation();
startDrag(e, image, id);
});
image.addEventListener('touchstart', (e) => {
if (e.touches.length === 1) {
e.preventDefault();
e.stopPropagation();
touchTimeout = setTimeout(() => {
startDrag(e.touches[0], image, id);
}, 250);
}
});
image.addEventListener('touchend', () => {
clearTimeout(touchTimeout);
});
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 startDrag(e, image, id) {
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);
const deltaY = (e.clientY - startY);
image.style.left = `${startLeft + deltaX}px`;
image.style.top = `${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('touchmove', (e) => moveImage(e.touches[0]), false);
document.addEventListener('mouseup', () => {
document.removeEventListener('mousemove', moveImage);
}, { once: true });
document.addEventListener('touchend', () => {
document.removeEventListener('touchmove', moveImage);
}, { once: true });
}
notes.js
Теперь ваш код будет обрабатывать перемещение элементов на тачпаде только после того, как первая тучка была бы короткой, а второе удерживающей. Перемещение с помощью мыши будет функционировать, как и прежде.
Show full message · 263 lines Прошу прощения за возникшую проблему. Давайте попробуем разобраться и исправить это. В оригинальной версии вашего кода было одно ключевое отличие: использование переменной scale, которая, вероятно, нарушила вычисления при моей модификации. Сейчас я попробую восстановить правильный вариант и учесть, что scale может влиять на вычисления.
Вот исправленный код:
images.js
javascript Copy code
// images.js
let touchTimeout;
const scale = 1; // Замените на необходимое значение, если нужно
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';
image.appendChild(img);
addImageEventListeners(image, data.id);
return image;
}
function addImageEventListeners(image, id) {
image.addEventListener('mousedown', (e) => {
if (e.button !== 0) return;
e.preventDefault();
e.stopPropagation();
startDrag(e, image, id);
});
image.addEventListener('touchstart', (e) => {
if (e.touches.length === 1) {
e.preventDefault();
e.stopPropagation();
touchTimeout = setTimeout(() => {
startDrag(e.touches[0], image, id);
}, 200);
}
});
image.addEventListener('touchend', () => {
clearTimeout(touchTimeout);
});
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 startDrag(e, image, id) {
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 = `${startLeft + deltaX}px`;
image.style.top = `${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('touchmove', (e) => moveImage(e.touches[0]), false);
document.addEventListener('mouseup', () => {
document.removeEventListener('mousemove', moveImage);
}, { once: true });
document.addEventListener('touchend', () => {
document.removeEventListener('touchmove', moveImage);
}, { once: true });
}
function handleExistingImages() {
document.querySelectorAll('.image').forEach(image => {
const id = image.dataset.id;
addImageEventListeners(image, id);
});
}
handleExistingImages();
notes.js
Убедитесь, что scale установлено в соответствующее значение, используется для корректировки движения в зависимости от масштаба преобразований на вашей странице. Дайте знать, если что-то еще не работает!
Show full message · 281 lines Copy first response
Not yet included in a published release.
// notes.js
// Assumes uploadFile, showContextMenu, addResizeListeners are available globally
let touchTimeout;
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) {
note.addEventListener('mousedown', (e) => {
if (e.button !== 0) return; // Left mouse button
e.preventDefault();
e.stopPropagation();
startDrag(e, note, id);
});
note.addEventListener('touchstart', (e) => {
if (e.touches.length === 1) {
e.preventDefault();
e.stopPropagation();
touchTimeout = setTimeout(() => {
startDrag(e.touches[0], note, id);
}, 250);
}
});
note.addEventListener('touchend', () => {
clearTimeout(touchTimeout);
});
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 startDrag(e, note, id) {
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);
const deltaY = (e.clientY - startY);
note.style.left = `${startLeft + deltaX}px`;
note.style.top = `${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('touchmove', (e) => moveNote(e.touches[0]), false);
document.addEventListener('mouseup', () => {
document.removeEventListener('mousemove', moveNote);
}, { once: true });
document.addEventListener('touchend', () => {
document.removeEventListener('touchmove', moveNote);
}, { once: true });
}
// notes.js
let touchTimeout;
const scale = 1; // Замените на необходимое значение, если нужно
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) {
note.addEventListener('mousedown', (e) => {
if (e.button !== 0) return;
e.preventDefault();
e.stopPropagation();
startDrag(e, note, id);
});
note.addEventListener('touchstart', (e) => {
if (e.touches.length === 1) {
e.preventDefault();
e.stopPropagation();
touchTimeout = setTimeout(() => {
startDrag(e.touches[0], note, id);
}, 200);
}
});
note.addEventListener('touchend', () => {
clearTimeout(touchTimeout);
});
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 startDrag(e, note, id) {
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 = `${startLeft + deltaX}px`;
note.style.top = `${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('touchmove', (e) => moveNote(e.touches[0]), false);
document.addEventListener('mouseup', () => {
document.removeEventListener('mousemove', moveNote);
}, { once: true });
document.addEventListener('touchend', () => {
document.removeEventListener('touchmove', moveNote);
}, { once: true });
}
function handleExistingNotes() {
document.querySelectorAll('.note').forEach(note => {
const id = note.dataset.id;
addNoteEventListeners(note, id);
});
}
handleExistingNotes();