chore(core): filter addition and removal changes

Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
braks
2022-12-13 15:49:20 +01:00
committed by Braks
parent f31f0073f7
commit 217ee6b0df
2 changed files with 55 additions and 46 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
<script lang="ts" setup> <script lang="ts" setup>
import type { Node } from '@vue-flow/core' import type { Node } from '@vue-flow/core'
import { VueFlow, useVueFlow } from '@vue-flow/core/src/index' import { VueFlow, useVueFlow } from '@vue-flow/core'
import { Background, BackgroundVariant } from '@vue-flow/background' import { Background, BackgroundVariant } from '@vue-flow/background'
import { Controls } from '@vue-flow/controls' import { Controls } from '@vue-flow/controls'
+21 -12
View File
@@ -79,16 +79,32 @@ export const applyChanges = <
changes: C[], changes: C[],
elements: T[], elements: T[],
): T[] => { ): T[] => {
let elementIds = elements.map((el) => el.id) const addRemoveChanges = changes.filter((c) => c.type === 'add' || c.type === 'remove') as (
| NodeAddChange
| EdgeAddChange
| NodeRemoveChange
| EdgeRemoveChange
)[]
addRemoveChanges.forEach((change) => {
switch (change.type) {
case 'add':
elements.push(<T>change.item)
break
case 'remove':
elements.splice(
elements.findIndex((el) => el.id === change.id),
1,
)
break
}
})
const elementIds = elements.map((el) => el.id)
elements.forEach((element) => { elements.forEach((element) => {
const currentChanges = changes.filter((c) => (<any>c).id === element.id) const currentChanges = changes.filter((c) => (<any>c).id === element.id)
for (const currentChange of currentChanges) { for (const currentChange of currentChanges) {
if (currentChange.type === 'add') {
const item = <T>currentChange.item
return elements.push(item)
} else {
switch (currentChange.type) { switch (currentChange.type) {
case 'select': case 'select':
;(element as FlowElement).selected = currentChange.selected ;(element as FlowElement).selected = currentChange.selected
@@ -131,13 +147,6 @@ export const applyChanges = <
} }
} }
break break
case 'remove':
if (elementIds.includes(currentChange.id)) {
elements.splice(elements.indexOf(element), 1)
elementIds = elements.map((el) => el.id)
}
break
}
} }
} }
}) })