-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
54 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters