parent
27277a8844
commit
9f32a8b4af
@ -10,13 +10,23 @@
|
|||||||
</VueFlow>
|
</VueFlow>
|
||||||
|
|
||||||
<div class="menu">
|
<div class="menu">
|
||||||
<button @click="addRandomNode">
|
<button @click="addRandomNode('input')">
|
||||||
Добавить
|
+ Вход
|
||||||
|
</button>
|
||||||
|
<button @click="addRandomNode()">
|
||||||
|
+ Обычная
|
||||||
|
</button>
|
||||||
|
<button @click="addRandomNode('output')">
|
||||||
|
+ Выход
|
||||||
</button>
|
</button>
|
||||||
<button @click="reset">
|
<button @click="reset">
|
||||||
Сбросить
|
Сбросить
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div v-if="clientId" class="info">
|
||||||
|
Ваш ID: {{ clientId }} | Клиентов: {{ clients.size }}
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
@ -37,14 +47,15 @@ const { addNodes, onConnect, addEdges } = useVueFlow()
|
|||||||
|
|
||||||
onConnect(addEdges)
|
onConnect(addEdges)
|
||||||
|
|
||||||
const { reset } = useCollaborativeFlow(() => props.diagramId)
|
const { clientId, clients, reset } = useCollaborativeFlow(() => props.diagramId)
|
||||||
|
|
||||||
function addRandomNode() {
|
function addRandomNode(type?: Node['type']) {
|
||||||
addNodes({
|
addNodes({
|
||||||
id: Math.random().toString(),
|
id: Math.random().toString(),
|
||||||
|
type,
|
||||||
position: { x: Math.random() * 500, y: Math.random() * 500 },
|
position: { x: Math.random() * 500, y: Math.random() * 500 },
|
||||||
label: 'Random Node',
|
|
||||||
data: {
|
data: {
|
||||||
|
label: 'Random Node',
|
||||||
hello: 'world',
|
hello: 'world',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@ -61,4 +72,10 @@ function addRandomNode() {
|
|||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
gap: 8px;
|
gap: 8px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.info {
|
||||||
|
position: fixed;
|
||||||
|
bottom: 16px;
|
||||||
|
right: 16px;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import type { Edge, Node } from '@vue-flow/core'
|
|||||||
import type { MaybeRefOrGetter } from 'vue'
|
import type { MaybeRefOrGetter } from 'vue'
|
||||||
import type { YMapEvent } from 'yjs'
|
import type { YMapEvent } from 'yjs'
|
||||||
import { useVueFlow } from '@vue-flow/core'
|
import { useVueFlow } from '@vue-flow/core'
|
||||||
import { onScopeDispose, shallowRef, toValue, watch } from 'vue'
|
import { onScopeDispose, ref, shallowRef, toValue, watch } from 'vue'
|
||||||
import { WebsocketProvider } from 'y-websocket'
|
import { WebsocketProvider } from 'y-websocket'
|
||||||
import * as Y from 'yjs'
|
import * as Y from 'yjs'
|
||||||
|
|
||||||
@ -14,6 +14,9 @@ export function useCollaborativeFlow(diagramId: MaybeRefOrGetter<string>) {
|
|||||||
const yNodes = yDoc.getMap<Node>('nodes')
|
const yNodes = yDoc.getMap<Node>('nodes')
|
||||||
const yEdges = yDoc.getMap<Edge>('edges')
|
const yEdges = yDoc.getMap<Edge>('edges')
|
||||||
|
|
||||||
|
const clientId = ref<number>()
|
||||||
|
const clients = shallowRef(new Map())
|
||||||
|
|
||||||
const {
|
const {
|
||||||
addNodes,
|
addNodes,
|
||||||
addEdges,
|
addEdges,
|
||||||
@ -190,6 +193,19 @@ export function useCollaborativeFlow(diagramId: MaybeRefOrGetter<string>) {
|
|||||||
diagramId,
|
diagramId,
|
||||||
yDoc,
|
yDoc,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
clientId.value = provider.value.awareness.clientID
|
||||||
|
clients.value = provider.value.awareness.getStates()
|
||||||
|
|
||||||
|
// provider.value.awareness.setLocalStateField('nigger', true)
|
||||||
|
|
||||||
|
provider.value.awareness.on('update', ({ added, updated, removed }) => {
|
||||||
|
// console.log('update', added, updated, removed)
|
||||||
|
|
||||||
|
// const changedClients = added.concat(updated).concat(removed)
|
||||||
|
// broadcastAwarenessMessage(awarenessProtocol.encodeAwarenessUpdate(awareness, changedClients))
|
||||||
|
clients.value = provider.value!.awareness.getStates()
|
||||||
|
})
|
||||||
}, { immediate: true })
|
}, { immediate: true })
|
||||||
|
|
||||||
onScopeDispose(() => {
|
onScopeDispose(() => {
|
||||||
@ -206,6 +222,8 @@ export function useCollaborativeFlow(diagramId: MaybeRefOrGetter<string>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
clientId,
|
||||||
|
clients,
|
||||||
reset,
|
reset,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user