-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
LexicalCollaborationContext.ts
64 lines (55 loc) · 1.51 KB
/
LexicalCollaborationContext.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import type {Doc} from 'yjs';
import {createContext, useContext} from 'react';
type CollaborationContextType = {
clientID: number;
color: string;
isCollabActive: boolean;
name: string;
yjsDocMap: Map<string, Doc>;
};
const entries = [
['Cat', 'rgb(125, 50, 0)'],
['Dog', 'rgb(100, 0, 0)'],
['Rabbit', 'rgb(150, 0, 0)'],
['Frog', 'rgb(200, 0, 0)'],
['Fox', 'rgb(200, 75, 0)'],
['Hedgehog', 'rgb(0, 75, 0)'],
['Pigeon', 'rgb(0, 125, 0)'],
['Squirrel', 'rgb(75, 100, 0)'],
['Bear', 'rgb(125, 100, 0)'],
['Tiger', 'rgb(0, 0, 150)'],
['Leopard', 'rgb(0, 0, 200)'],
['Zebra', 'rgb(0, 0, 250)'],
['Wolf', 'rgb(0, 100, 150)'],
['Owl', 'rgb(0, 100, 100)'],
['Gull', 'rgb(100, 0, 100)'],
['Squid', 'rgb(150, 0, 150)'],
];
const randomEntry = entries[Math.floor(Math.random() * entries.length)];
export const CollaborationContext = createContext<CollaborationContextType>({
clientID: 0,
color: randomEntry[1],
isCollabActive: false,
name: randomEntry[0],
yjsDocMap: new Map(),
});
export function useCollaborationContext(
username?: string,
color?: string,
): CollaborationContextType {
const collabContext = useContext(CollaborationContext);
if (username != null) {
collabContext.name = username;
}
if (color != null) {
collabContext.color = color;
}
return collabContext;
}