Skip to content

Commit e59fb83

Browse files
committed
0.5.0: customizable user metadata and renderfunction
1 parent b3937c2 commit e59fb83

File tree

5 files changed

+47
-24
lines changed

5 files changed

+47
-24
lines changed

dist/cursor-chat.es.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9846,7 +9846,7 @@ var randomColor = { exports: {} };
98469846
});
98479847
})(randomColor, randomColor.exports);
98489848
var randomcolor = randomColor.exports;
9849-
function cursorFactory(cursor) {
9849+
function defaultCursorRenderer(cursor) {
98509850
const htmlFragment = `<div id="cursor_${cursor.id}" class="cursor">
98519851
<svg
98529852
xmlns="http://www.w3.org/2000/svg"
@@ -9874,20 +9874,20 @@ function cursorFactory(cursor) {
98749874
const cursorEl = template.content.firstChild;
98759875
return cursorEl;
98769876
}
9877-
const initCursorChat = (room_id, triggerKey = "/", cursorDivId = "cursor-chat-layer", chatDivId = "cursor-chat-box") => {
9877+
const initCursorChat = (room_id = `cursor-chat-room-${window.location.host + window.location.pathname}`, triggerKey = "/", cursorDivId = "cursor-chat-layer", chatDivId = "cursor-chat-box", userMetaData = {}, renderCursor = defaultCursorRenderer) => {
98789878
const cursorDiv = document.getElementById(cursorDivId);
98799879
const chatDiv = document.getElementById(chatDivId);
98809880
if (!cursorDiv || !chatDiv) {
98819881
throw `Couldn't find cursor-chat-related divs! Make sure DOM content is fully loaded before initializing`;
98829882
}
98839883
const me = {
98849884
id: nanoid(),
9885-
color: randomcolor(),
98869885
x: 0,
98879886
y: 0,
9888-
chat: ""
9887+
chat: "",
9888+
color: randomcolor(),
9889+
userMetaData
98899890
};
9890-
room_id = room_id || `cursor-chat-room-${window.location.host + window.location.pathname}`;
98919891
const doc2 = new Doc();
98929892
const provider = new WebrtcProvider(room_id, doc2);
98939893
const others = doc2.getMap("state");
@@ -9941,7 +9941,7 @@ const initCursorChat = (room_id, triggerKey = "/", cursorDivId = "cursor-chat-la
99419941
switch (change.action) {
99429942
case "add":
99439943
const new_cursor = others.get(cursor_id);
9944-
const new_cursor_div = cursorFactory(new_cursor);
9944+
const new_cursor_div = renderCursor(new_cursor);
99459945
new_cursor_div.classList.add("new");
99469946
cursorDiv.appendChild(new_cursor_div);
99479947
const add_point_closure = ([x, y]) => new_cursor_div.style.setProperty("transform", `translate(${x}px, ${y}px)`);
@@ -9976,4 +9976,4 @@ const initCursorChat = (room_id, triggerKey = "/", cursorDivId = "cursor-chat-la
99769976
});
99779977
return cleanup;
99789978
};
9979-
export { initCursorChat };
9979+
export { defaultCursorRenderer, initCursorChat };

dist/cursor-chat.umd.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main.d.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
export declare const initCursorChat: (room_id?: string, triggerKey?: string, cursorDivId?: string, chatDivId?: string) => () => void;
1+
declare type UserMetadata<T> = {
2+
[key: string]: T;
3+
};
4+
interface Cursor<T> {
5+
id: string;
6+
x: number;
7+
y: number;
8+
chat: string;
9+
color: string;
10+
userMetaData: UserMetadata<T>;
11+
}
12+
export declare function defaultCursorRenderer<T>(cursor: Cursor<T>): HTMLElement;
13+
export declare const initCursorChat: <T>(room_id?: string, triggerKey?: string, cursorDivId?: string, chatDivId?: string, userMetaData?: UserMetadata<T>, renderCursor?: <T_1>(cursor: Cursor<T_1>) => HTMLElement) => () => void;
14+
export {};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cursor-chat",
3-
"version": "0.4.9",
3+
"version": "0.5.0",
44
"license": "MIT",
55
"files": [
66
"dist"

src/main.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ import { PerfectCursor } from 'perfect-cursors'
66
import { nanoid } from 'nanoid'
77
import randomcolor from 'randomcolor'
88

9-
interface Cursor {
9+
type UserMetadata<T> = { [key: string]: T }
10+
11+
interface Cursor<T> {
1012
id: string
11-
color: string
1213
x: number
1314
y: number
1415
chat: string
16+
color: string
17+
userMetaData: UserMetadata<T>
1518
}
1619

17-
function cursorFactory(cursor: Cursor): HTMLElement {
20+
export function defaultCursorRenderer<T>(cursor: Cursor<T>): HTMLElement {
1821
const htmlFragment = `<div id="cursor_${cursor.id}" class="cursor">
1922
<svg
2023
xmlns="http://www.w3.org/2000/svg"
@@ -43,30 +46,37 @@ function cursorFactory(cursor: Cursor): HTMLElement {
4346
return cursorEl;
4447
}
4548

46-
export const initCursorChat = (room_id?: string, triggerKey = "/", cursorDivId = "cursor-chat-layer", chatDivId = "cursor-chat-box") => {
49+
export const initCursorChat = <T>(
50+
room_id: string = `cursor-chat-room-${window.location.host + window.location.pathname}`,
51+
triggerKey: string = "/",
52+
cursorDivId: string = "cursor-chat-layer",
53+
chatDivId: string = "cursor-chat-box",
54+
userMetaData: UserMetadata<T> = {},
55+
renderCursor: <T>(cursor: Cursor<T>) => HTMLElement = defaultCursorRenderer,
56+
) => {
4757
const cursorDiv = document.getElementById(cursorDivId)!
4858
const chatDiv = document.getElementById(chatDivId)! as HTMLInputElement
4959

5060
if (!cursorDiv || !chatDiv) {
5161
throw `Couldn't find cursor-chat-related divs! Make sure DOM content is fully loaded before initializing`
5262
}
5363

54-
const me: Cursor = {
64+
const me: Cursor<T> = {
5565
id: nanoid(),
56-
color: randomcolor(),
5766
x: 0,
5867
y: 0,
59-
chat: ""
68+
chat: "",
69+
color: randomcolor(),
70+
userMetaData,
6071
}
6172

62-
room_id = room_id || `cursor-chat-room-${window.location.host + window.location.pathname}`
6373
const doc = new Y.Doc()
6474
const provider = new WebrtcProvider(
6575
room_id,
6676
doc
6777
)
6878

69-
const others: Y.Map<Cursor> = doc.getMap("state")
79+
const others: Y.Map<Cursor<T>> = doc.getMap("state")
7080
let sendUpdate = false
7181

7282
const cleanup = () => {
@@ -125,7 +135,7 @@ export const initCursorChat = (room_id?: string, triggerKey = "/", cursorDivId =
125135
case 'add':
126136
// make a new cursor
127137
const new_cursor = others.get(cursor_id)!;
128-
const new_cursor_div = cursorFactory(new_cursor);
138+
const new_cursor_div = renderCursor(new_cursor);
129139
new_cursor_div.classList.add("new")
130140
cursorDiv.appendChild(new_cursor_div);
131141
const add_point_closure = ([x, y]: number[]) => new_cursor_div.style.setProperty("transform", `translate(${x}px, ${y}px)`);

0 commit comments

Comments
 (0)