-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDeepStructure.tsx
43 lines (40 loc) · 1.21 KB
/
DeepStructure.tsx
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
import React from "react";
import { useY } from "react-yjs";
import * as Y from "yjs";
export const DeepStructure: React.FC = () => {
const [yPosts] = React.useState(() => {
// initialize a Y.Doc and get the settings
// when the component mounts
const yDoc = new Y.Doc();
const yPosts = yDoc.getArray<Y.Map<string | Y.Array<string>>>("posts");
const yPost = new Y.Map<string | Y.Array<string>>();
yPosts.push([yPost]);
yPost.set("title", "Notes");
const yTags = new Y.Array<string>();
yTags.push(["cooking", "vegetables"]);
yPost.set("tags", yTags);
return yPosts;
});
const yTagsOfFirstPost = yPosts.get(0).get("tags") as Y.Array<string>;
const tagsOfFirstPost = useY(yTagsOfFirstPost);
return (
<>
{tagsOfFirstPost.map((tag, index) => {
return (
<div key={`${tag}-${index}`}>
{tag}
<button
onClick={() => {
const tags = yPosts.get(0).get("tags") as Y.Array<string>;
tags.delete(index);
}}
>
x
</button>
</div>
);
})}
<div>Result: {JSON.stringify(yPosts.toJSON(), null, 2)}</div>
</>
);
};