-
Notifications
You must be signed in to change notification settings - Fork 0
/
YjsDemo.vue
128 lines (123 loc) · 3.41 KB
/
YjsDemo.vue
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<template>
<div>
<network-setting @network="networkTrigger"></network-setting>
<div class="flex-list">
<div class="flex-list-item">
<yjs ref="yjsInput1" @output="output1" />
</div>
<div class="flex-list-item">
<yjs ref="yjsInput2" @output="output2" />
</div>
</div>
</div>
</template>
<script>
import NetworkSetting from "../components/NetworkSetting.vue";
import Yjs from "../components/Yjs.vue";
import { defaultNetwork } from "../lib/network";
export default {
components: {
NetworkSetting,
Yjs,
},
data() {
return {
channel1: null,
channel2: null,
};
},
mounted() {
this.channel1 = defaultNetwork.createChannel("yjsInput1");
this.channel1.receive((value) => {
const { type, vector, items, ds } = JSON.parse(value);
if (type === 'step1') {
const vec = this.$refs.yjsInput1.getVector();
this.channel1.send(
"yjsInput2",
JSON.stringify({ type: 'step2', vector: vec })
);
} else if (type === 'step2') {
const missing = this.$refs.yjsInput1.getMissing(vector);
const ds = this.$refs.yjsInput1.getDeleteSet();
const vec = this.$refs.yjsInput1.getVector();
this.channel1.send(
"yjsInput2",
JSON.stringify({ type: 'step3', vector: vec, items: missing, ds: ds })
);
} else if (type === 'step3') {
this.$refs.yjsInput1.merge(items, ds);
}
});
this.channel2 = defaultNetwork.createChannel("yjsInput2");
this.channel2.receive((value) => {
const { type, vector, items, ds } = JSON.parse(value);
if (type === 'step1') {
const vec = this.$refs.yjsInput2.getVector();
this.channel2.send(
"yjsInput1",
JSON.stringify({ type: 'step2', vector: vec })
);
} else if (type === 'step2') {
const missing = this.$refs.yjsInput2.getMissing(vector);
const ds = this.$refs.yjsInput2.getDeleteSet();
const vec = this.$refs.yjsInput2.getVector();
this.channel2.send(
"yjsInput1",
JSON.stringify({ type: 'step3', vector: vec, items: missing, ds: ds })
);
} else if (type === 'step3') {
this.$refs.yjsInput2.merge(items, ds);
}
});
},
methods: {
networkTrigger({ online, delay }) {
const delay1 = parseInt(delay);
defaultNetwork.setDelay(delay1);
defaultNetwork.setEnable(online);
},
output1({ value }) {
// console.log("output1", value);
const payload = {
type: 'step1',
};
this.channel1.send("yjsInput2", JSON.stringify(payload));
},
output2({ value }) {
// console.log("output2", value);
const payload = {
type: 'step1',
};
this.channel2.send("yjsInput1", JSON.stringify(payload));
},
},
};
</script>
<style>
.flex-list {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
background: #fff;
}
.flex-list .flex-list-item {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
width: 40%;
padding-bottom: 20px;
}
.flex-list .flex-list-item:nth-child(2n + 1) {
margin-right: 4%;
}
</style>