From dff6a281c13ed6bd7432d11e2144c408fbf6ce5a Mon Sep 17 00:00:00 2001 From: jerl Date: Mon, 15 Apr 2024 22:23:14 -0700 Subject: [PATCH] space fixes --- client/space.js | 42 +++++++++++++++++++++++++++++++----------- src/save_span.js | 17 +++++++++-------- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/client/space.js b/client/space.js index 5f31cc4..ecd06fb 100644 --- a/client/space.js +++ b/client/space.js @@ -33,15 +33,18 @@ function save(span) { }); } -function add(x, y, scale = 1) { +function add(s) { let span = document.createElement('span'); span.classList.add('span'); + if (s.id) + span.id = 'span' + s.id; + span.innerText = s.content; span.contentEditable = true; span.spellcheck = false; - span.scale = scale; - span.style.left = `${x}px`; - span.style.top = `${y}px`; - span.style.transform = `translate(-50%, -50%) scale(${scale})`; + span.scale = s.scale; + span.style.left = `${s.x}px`; + span.style.top = `${s.y}px`; + span.style.transform = `translate(-50%, -50%) scale(${s.scale})`; span.onkeydown = function(event) { if (event.key === 'Enter' && !event.getModifierState('Shift')) { event.preventDefault(); @@ -89,6 +92,21 @@ function add(x, y, scale = 1) { return span; } +window.socket.on('span', msg => { + if (msg.thread !== spaceId) + return; + let span = document.getElementById('span' + msg.id); + if (span) { + span.innerText = msg.content; + span.x = msg.x; + span.y = msg.y; + span.scale = msg.scale; + span.style.transform = `translate(-50%, -50%) scale(${msg.scale})`; + } + else + add(msg); +}); + export default function loadSpace() { if (!space) { space = document.getElementById('space'); @@ -102,7 +120,12 @@ export default function loadSpace() { editing = null; return; } - editing = add(event.offsetX + space.scrollLeft, event.offsetY + space.scrollTop); + editing = add({ + x: event.offsetX + space.scrollLeft, + y: event.offsetY + space.scrollTop, + scale: 1, + content: '' + }); editing.focus(); }; } @@ -113,10 +136,7 @@ export default function loadSpace() { window.emit('get_space', { thread: window.threadId }, msg => { if (!msg.success) console.log('get space failed: ' + msg.message); - for (const span of msg.spans) { - let el = add(span.x, span.y, span.scale); - el.innerText = span.content; - el.id = 'span' + span.id; - } + for (const span of msg.spans) + add(span); }); }; diff --git a/src/save_span.js b/src/save_span.js index a9112d4..2f32cb2 100644 --- a/src/save_span.js +++ b/src/save_span.js @@ -47,14 +47,15 @@ const save_span = async (msg, respond, socket, io) => { if (permissions.rows.length > 0 || members.includes(username)) { const sockets = io.cache[username]; for (let s of sockets) { - io.to(s).emit('span', { - id, - thread: msg.thread, - content: msg.content, - x: msg.x, - y: msg.y, - scale: msg.scale - }); + if (s !== socket.id) + io.to(s).emit('span', { + id, + thread: msg.thread, + content: msg.content, + x: msg.x, + y: msg.y, + scale: msg.scale + }); } } }