Skip to content

Commit 37ce4b1

Browse files
committed
Working example
1 parent 2097c60 commit 37ce4b1

2 files changed

Lines changed: 18 additions & 65 deletions

File tree

examples/simple-yjs-widget/notebooks/simple.ipynb

Lines changed: 10 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": null,
66
"id": "2a245ed3-5838-4be6-ad5e-9fa75b9c7ab0",
77
"metadata": {},
88
"outputs": [],
@@ -13,7 +13,7 @@
1313
},
1414
{
1515
"cell_type": "code",
16-
"execution_count": 2,
16+
"execution_count": null,
1717
"id": "6d9538c9-4858-4c9d-b347-18965660115d",
1818
"metadata": {},
1919
"outputs": [],
@@ -64,55 +64,28 @@
6464
},
6565
{
6666
"cell_type": "code",
67-
"execution_count": 3,
67+
"execution_count": null,
6868
"id": "8cfb218c-e6fe-48da-a6ee-599bfe524e15",
6969
"metadata": {},
70-
"outputs": [
71-
{
72-
"data": {
73-
"application/vnd.jupyter.ywidget-view+json": {
74-
"model_id": "4a2524e43c7244d9b93aeb95383cdc85",
75-
"version_major": 2,
76-
"version_minor": 0
77-
},
78-
"text/plain": [
79-
"<__main__.MySlider object at 0x7fe44436af90>"
80-
]
81-
},
82-
"execution_count": 3,
83-
"metadata": {},
84-
"output_type": "execute_result"
85-
}
86-
],
70+
"outputs": [],
8771
"source": [
8872
"w = MySlider()\n",
8973
"w"
9074
]
9175
},
9276
{
9377
"cell_type": "code",
94-
"execution_count": 4,
78+
"execution_count": null,
9579
"id": "9269a3f1-a061-478f-8728-7393a4aec2d0",
9680
"metadata": {},
97-
"outputs": [
98-
{
99-
"data": {
100-
"text/plain": [
101-
"50.0"
102-
]
103-
},
104-
"execution_count": 4,
105-
"metadata": {},
106-
"output_type": "execute_result"
107-
}
108-
],
81+
"outputs": [],
10982
"source": [
11083
"w.value"
11184
]
11285
},
11386
{
11487
"cell_type": "code",
115-
"execution_count": 5,
88+
"execution_count": null,
11689
"id": "8ac7ea5c-8315-44eb-aa62-f223f86d267d",
11790
"metadata": {},
11891
"outputs": [],
@@ -122,7 +95,7 @@
12295
},
12396
{
12497
"cell_type": "code",
125-
"execution_count": 6,
98+
"execution_count": null,
12699
"id": "bd2c92fb-9653-4de5-8a20-d463205f3a9e",
127100
"metadata": {},
128101
"outputs": [],
@@ -132,32 +105,13 @@
132105
},
133106
{
134107
"cell_type": "code",
135-
"execution_count": 7,
108+
"execution_count": null,
136109
"id": "3a5680c6-77ae-4f71-a982-b3447dcb4fd0",
137110
"metadata": {},
138-
"outputs": [
139-
{
140-
"data": {
141-
"text/plain": [
142-
"36.0"
143-
]
144-
},
145-
"execution_count": 7,
146-
"metadata": {},
147-
"output_type": "execute_result"
148-
}
149-
],
111+
"outputs": [],
150112
"source": [
151113
"w.value"
152114
]
153-
},
154-
{
155-
"cell_type": "code",
156-
"execution_count": null,
157-
"id": "839e129b-4040-4293-9287-abe4bfd5491d",
158-
"metadata": {},
159-
"outputs": [],
160-
"source": []
161115
}
162116
],
163117
"metadata": {

examples/simple-yjs-widget/src/index.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,24 @@ class MySlider {
2828
this.state.set('value', 50);
2929
this.state.set('step', 1);
3030

31-
this._stateChanged();
31+
this.slider.min = this.state.get('min');
32+
this.slider.max = this.state.get('max');
33+
this.slider.value = this.state.get('value');
34+
this.slider.step = this.state.get('step');
3235

3336
this.slider.onchange = this._sliderChanged.bind(this);
3437

3538
node.appendChild(this.slider);
3639
}
3740

38-
_stateChanged(): void {
39-
this.slider.min = this.state.get('min');
40-
this.slider.max = this.state.get('max');
41-
this.slider.value = this.state.get('value');
42-
this.slider.step = this.state.get('step');
41+
_stateChanged(change: Y.YMapEvent<any>): void {
42+
for (const key of change.keysChanged) {
43+
this.slider[key] = change.target.toJSON()[key];
44+
}
4345
}
4446

4547
_sliderChanged(): void {
46-
this.state.set('min', parseInt(this.slider.min ?? '0'));
47-
this.state.set('max', parseInt(this.slider.max ?? '100'));
4848
this.state.set('value', parseInt(this.slider.value ?? '50'));
49-
this.state.set('step', parseInt(this.slider.step ?? '1'));
5049
}
5150

5251
state: Y.Map<any>;

0 commit comments

Comments
 (0)