From b84b9b62e1d36303cf27beb8e79be9a6bf6ae00e Mon Sep 17 00:00:00 2001 From: "Lu[ke] Wilson" Date: Sat, 1 Mar 2025 17:56:13 +0000 Subject: [PATCH] stop chat going cross-room --- src/chat.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/editor.js | 35 +---------------------------------- src/session.js | 5 ++++- 3 files changed, 54 insertions(+), 35 deletions(-) create mode 100644 src/chat.js diff --git a/src/chat.js b/src/chat.js new file mode 100644 index 0000000..9aab8b4 --- /dev/null +++ b/src/chat.js @@ -0,0 +1,49 @@ +import { pastamirror } from './main.js'; +import { getRoomName, getSession } from './session.js'; + +export function getChatSessionName() { + return `session:${getRoomName()}:chat`; +} + +export function sendChatMessage({ docId, message, from, user, color }) { + const session = getSession(); + session._pubSubClient.publish(getChatSessionName(), { + docId, + message, + user, + from, + color, + }); +} + +export function handleChatMessage(message) { + const view = pastamirror.editorViews.get(message.docId); + let from = message.from; + const pos = view.coordsAtPos(from); + const chatContainer = document.querySelector('.chat-container'); + if (pos) { + const messageContainer = document.createElement('div'); + messageContainer.innerText = message.message; + const pointer_color = message.color; + messageContainer.style = `position:fixed;top:${pos.top}px;left:${pos.left}px`; + messageContainer.style.color = pointer_color; + messageContainer.classList.add('rising-animation'); + messageContainer.classList.add('message-container'); + chatContainer?.appendChild(messageContainer); + setTimeout(() => { + messageContainer.remove(); + }, 7000); + } else { + console.warn('could not get line position'); + } +} + +export function subscribeToChat() { + const session = getSession(); + session._pubSubClient.subscribe(getChatSessionName(), (args) => handleChatMessage(args.message)); +} + +export function unsubscribeFromChat() { + const session = getSession(); + session._pubSubClient.unsubscribe(getChatSessionName()); +} diff --git a/src/editor.js b/src/editor.js index db3f020..8632a9c 100644 --- a/src/editor.js +++ b/src/editor.js @@ -15,7 +15,7 @@ import { getColorFromUserHue, getSettings } from './settings.js'; import { insertNewline } from '@codemirror/commands'; import { nudelAlert } from './alert.js'; import { strudelAutocomplete } from './strudel-autocomplete.js'; -import { pastamirror } from './main.js'; +import { handleChatMessage, sendChatMessage } from './chat.js'; import { getSession } from './session.js'; // we need to access these variables from the strudel iframe: @@ -394,37 +394,4 @@ export class PastaMirror { } } } - - chat(message) { - const view = this.editorViews.get(message.docId); - let from = message.from; - const pos = view.coordsAtPos(from); - const chatContainer = document.querySelector('.chat-container'); - if (pos) { - const messageContainer = document.createElement('div'); - messageContainer.innerText = message.message; - const pointer_color = message.color; - messageContainer.style = `position:fixed;top:${pos.top}px;left:${pos.left}px`; - messageContainer.style.color = pointer_color; - messageContainer.classList.add('rising-animation'); - messageContainer.classList.add('message-container'); - chatContainer?.appendChild(messageContainer); - setTimeout(() => { - messageContainer.remove(); - }, 7000); - } else { - console.warn('could not get line position'); - } - } -} - -export function sendChatMessage({ docId, message, from, user, color }) { - const session = getSession(); - session._pubSubClient.publish(`session:pastagang:chat`, { - docId, - message, - user, - from, - color, - }); } diff --git a/src/session.js b/src/session.js index 1494723..61f9776 100644 --- a/src/session.js +++ b/src/session.js @@ -3,6 +3,7 @@ import { getStdSource } from './export.js'; import { pastamirror, Frame } from './main.js'; import { clearGlobalError, setError, clearLocalError } from './error.js'; import { getSettings } from './settings.js'; +import { getChatSessionName, handleChatMessage, subscribeToChat, unsubscribeFromChat } from './chat.js'; const PASTAGANG_ROOM_NAME = 'pastagang3'; @@ -74,12 +75,14 @@ function makeSession() { // session._pubSubClient seems to take a while to be defined.. // this might or might not be a good place to make sure its ready // the event might call multiple times so... do i need to unsub??? - session._pubSubClient.subscribe(`session:pastagang:chat`, (args) => pastamirror.chat(args.message)); + subscribeToChat(); }); session.on('pubsub:close', () => { // untested setError('Disconnected from Server...'); // unsub session:pastagang:chat here? + // lets try (?) + unsubscribeFromChat(); }); // hydra