update(examples): Add Save & Restore example
* add localforage to dev deps
This commit is contained in:
@@ -0,0 +1,60 @@
|
|||||||
|
<script lang="ts" setup>
|
||||||
|
import localforage from 'localforage'
|
||||||
|
import { useZoomPanHelper, OnLoadParams, FlowExportObject, Node } from '~/index'
|
||||||
|
|
||||||
|
localforage.config({
|
||||||
|
name: 'vue-flow',
|
||||||
|
storeName: 'flows',
|
||||||
|
})
|
||||||
|
|
||||||
|
const flowKey = 'example-flow'
|
||||||
|
|
||||||
|
const getNodeId = () => `randomnode_${+new Date()}`
|
||||||
|
|
||||||
|
const { transform } = useZoomPanHelper()
|
||||||
|
|
||||||
|
type ControlsProps = {
|
||||||
|
flowInstance?: OnLoadParams
|
||||||
|
}
|
||||||
|
|
||||||
|
const props = defineProps<ControlsProps>()
|
||||||
|
const emit = defineEmits(['restore', 'add'])
|
||||||
|
|
||||||
|
const onSave = () => {
|
||||||
|
if (props.flowInstance) {
|
||||||
|
const flow = props.flowInstance.toObject()
|
||||||
|
localforage.setItem(flowKey, flow)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const onRestore = () => {
|
||||||
|
const restoreFlow = async () => {
|
||||||
|
const flow: FlowExportObject | null = await localforage.getItem(flowKey)
|
||||||
|
|
||||||
|
console.log(flow)
|
||||||
|
if (flow) {
|
||||||
|
const [x = 0, y = 0] = flow.position
|
||||||
|
emit('restore', flow.elements ?? [])
|
||||||
|
transform({ x, y, zoom: flow.zoom || 0 })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
restoreFlow()
|
||||||
|
}
|
||||||
|
|
||||||
|
const onAdd = () => {
|
||||||
|
const newNode = {
|
||||||
|
id: `random_node-${getNodeId()}`,
|
||||||
|
data: { label: 'Added node' },
|
||||||
|
position: { x: Math.random() * window.innerWidth - 100, y: Math.random() * window.innerHeight },
|
||||||
|
} as Node
|
||||||
|
emit('add', newNode)
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<template>
|
||||||
|
<div class="save__controls">
|
||||||
|
<button @click="onSave">save</button>
|
||||||
|
<button @click="onRestore">restore</button>
|
||||||
|
<button @click="onAdd">add node</button>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<script lang="ts" setup>
|
||||||
|
import Controls from './Controls.vue'
|
||||||
|
import Flow, { addEdge, Connection, Node, Edge, Elements, OnLoadParams, removeElements } from '~/index'
|
||||||
|
|
||||||
|
import './save.css'
|
||||||
|
|
||||||
|
const initialElements: Elements = [
|
||||||
|
{ id: '1', data: { label: 'Node 1' }, position: { x: 100, y: 100 } },
|
||||||
|
{ id: '2', data: { label: 'Node 2' }, position: { x: 100, y: 200 } },
|
||||||
|
{ id: 'e1-2', source: '1', target: '2' },
|
||||||
|
] as Elements
|
||||||
|
|
||||||
|
const elements = ref(initialElements)
|
||||||
|
const flowInstance = ref()
|
||||||
|
|
||||||
|
const onConnect = (params: Connection | Edge) => (elements.value = addEdge(params, elements.value))
|
||||||
|
const onElementsRemove = (elementsToRemove: Elements) => (elements.value = removeElements(elementsToRemove, elements.value))
|
||||||
|
const onLoad = (instance: OnLoadParams) => (flowInstance.value = instance)
|
||||||
|
const onRestore = (els: Elements) => (elements.value = els)
|
||||||
|
const onAdd = (el: Node) => (elements.value = elements.value.concat(el))
|
||||||
|
</script>
|
||||||
|
<template>
|
||||||
|
<Flow :elements="elements" @elements-remove="onElementsRemove" @connect="onConnect" @load="onLoad">
|
||||||
|
<Controls :flow-instance="flowInstance" @restore="onRestore" @add="onAdd" />
|
||||||
|
</Flow>
|
||||||
|
</template>
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
.save__controls {
|
||||||
|
position: absolute;
|
||||||
|
right: 10px;
|
||||||
|
top: 10px;
|
||||||
|
z-index: 4;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.save__controls button {
|
||||||
|
margin-left: 5px;
|
||||||
|
}
|
||||||
@@ -65,6 +65,14 @@ export const routes: RouterOptions['routes'] = [
|
|||||||
path: '/overview',
|
path: '/overview',
|
||||||
component: () => import('./Overview/Overview.vue'),
|
component: () => import('./Overview/Overview.vue'),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: '/provider',
|
||||||
|
component: () => import('./Provider/ProviderExample.vue'),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/save-restore',
|
||||||
|
component: () => import('./SaveRestore/SaveRestoreExample.vue'),
|
||||||
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
export const router = createRouter({
|
export const router = createRouter({
|
||||||
|
|||||||
@@ -59,6 +59,7 @@
|
|||||||
"eslint": "^7.32.0",
|
"eslint": "^7.32.0",
|
||||||
"eslint-config-prettier": "^8.3.0",
|
"eslint-config-prettier": "^8.3.0",
|
||||||
"eslint-plugin-prettier": "^3.4.1",
|
"eslint-plugin-prettier": "^3.4.1",
|
||||||
|
"localforage": "^1.10.0",
|
||||||
"np": "^7.5.0",
|
"np": "^7.5.0",
|
||||||
"pnpm": "^6.18.0",
|
"pnpm": "^6.18.0",
|
||||||
"postcss": "^8.3.9",
|
"postcss": "^8.3.9",
|
||||||
|
|||||||
Generated
+18
@@ -22,6 +22,7 @@ specifiers:
|
|||||||
eslint-config-prettier: ^8.3.0
|
eslint-config-prettier: ^8.3.0
|
||||||
eslint-plugin-prettier: ^3.4.1
|
eslint-plugin-prettier: ^3.4.1
|
||||||
fast-deep-equal: ^3.1.3
|
fast-deep-equal: ^3.1.3
|
||||||
|
localforage: ^1.10.0
|
||||||
np: ^7.5.0
|
np: ^7.5.0
|
||||||
pinia: ^2.0.0-rc.14
|
pinia: ^2.0.0-rc.14
|
||||||
pnpm: ^6.18.0
|
pnpm: ^6.18.0
|
||||||
@@ -68,6 +69,7 @@ devDependencies:
|
|||||||
eslint: 7.32.0
|
eslint: 7.32.0
|
||||||
eslint-config-prettier: 8.3.0_eslint@7.32.0
|
eslint-config-prettier: 8.3.0_eslint@7.32.0
|
||||||
eslint-plugin-prettier: 3.4.1_6e975bd57c7acf028c1a9ddbbf60c898
|
eslint-plugin-prettier: 3.4.1_6e975bd57c7acf028c1a9ddbbf60c898
|
||||||
|
localforage: 1.10.0
|
||||||
np: 7.5.0
|
np: 7.5.0
|
||||||
pnpm: 6.18.0
|
pnpm: 6.18.0
|
||||||
postcss: 8.3.9
|
postcss: 8.3.9
|
||||||
@@ -3416,6 +3418,10 @@ packages:
|
|||||||
engines: {node: '>= 4'}
|
engines: {node: '>= 4'}
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/immediate/3.0.6:
|
||||||
|
resolution: {integrity: sha1-nbHb0Pr43m++D13V5Wu2BigN5ps=}
|
||||||
|
dev: true
|
||||||
|
|
||||||
/import-cwd/3.0.0:
|
/import-cwd/3.0.0:
|
||||||
resolution: {integrity: sha512-4pnzH16plW+hgvRECbDWpQl3cqtvSofHWh44met7ESfZ8UZOWWddm8hEyDTqREJ9RbYHY8gi8DqmaelApoOGMg==}
|
resolution: {integrity: sha512-4pnzH16plW+hgvRECbDWpQl3cqtvSofHWh44met7ESfZ8UZOWWddm8hEyDTqREJ9RbYHY8gi8DqmaelApoOGMg==}
|
||||||
engines: {node: '>=8'}
|
engines: {node: '>=8'}
|
||||||
@@ -3967,6 +3973,12 @@ packages:
|
|||||||
type-check: 0.4.0
|
type-check: 0.4.0
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/lie/3.1.1:
|
||||||
|
resolution: {integrity: sha1-mkNrLMd0bKWd56QfpGmz77dr2H4=}
|
||||||
|
dependencies:
|
||||||
|
immediate: 3.0.6
|
||||||
|
dev: true
|
||||||
|
|
||||||
/lilconfig/2.0.3:
|
/lilconfig/2.0.3:
|
||||||
resolution: {integrity: sha512-EHKqr/+ZvdKCifpNrJCKxBTgk5XupZA3y/aCPY9mxfgBzmgh93Mt/WqjjQ38oMxXuvDokaKiM3lAgvSH2sjtHg==}
|
resolution: {integrity: sha512-EHKqr/+ZvdKCifpNrJCKxBTgk5XupZA3y/aCPY9mxfgBzmgh93Mt/WqjjQ38oMxXuvDokaKiM3lAgvSH2sjtHg==}
|
||||||
engines: {node: '>=10'}
|
engines: {node: '>=10'}
|
||||||
@@ -4050,6 +4062,12 @@ packages:
|
|||||||
mlly: 0.2.10
|
mlly: 0.2.10
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/localforage/1.10.0:
|
||||||
|
resolution: {integrity: sha512-14/H1aX7hzBBmmh7sGPd+AOMkkIrHM3Z1PAyGgZigA1H1p5O5ANnMyWzvpAETtG68/dC4pC0ncy3+PPGzXZHPg==}
|
||||||
|
dependencies:
|
||||||
|
lie: 3.1.1
|
||||||
|
dev: true
|
||||||
|
|
||||||
/locate-path/2.0.0:
|
/locate-path/2.0.0:
|
||||||
resolution: {integrity: sha1-K1aLJl7slExtnA3pw9u7ygNUzY4=}
|
resolution: {integrity: sha1-K1aLJl7slExtnA3pw9u7ygNUzY4=}
|
||||||
engines: {node: '>=4'}
|
engines: {node: '>=4'}
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@ import { KeyCode, PanOnScrollMode } from '~/types/panel'
|
|||||||
|
|
||||||
export type ElementId = string
|
export type ElementId = string
|
||||||
|
|
||||||
export type FlowElement<T = any> = Partial<Node<T>> | Partial<Edge<T>>
|
export type FlowElement<T = any> = Node<T> | Edge<T>
|
||||||
|
|
||||||
export type Elements<T = any> = Array<FlowElement<T>>
|
export type Elements<T = any> = Array<FlowElement<T>>
|
||||||
|
|
||||||
|
|||||||
+10
-6
@@ -273,7 +273,6 @@ const parseElements = (nodes: Node[], edges: Edge[]): Elements => [
|
|||||||
|
|
||||||
n.position = n.__rf.position
|
n.position = n.__rf.position
|
||||||
|
|
||||||
n.__rf = {} as any
|
|
||||||
return n
|
return n
|
||||||
}),
|
}),
|
||||||
...edges.map((e) => ({ ...e })),
|
...edges.map((e) => ({ ...e })),
|
||||||
@@ -282,11 +281,16 @@ const parseElements = (nodes: Node[], edges: Edge[]): Elements => [
|
|||||||
export const onLoadGetElements = (currentStore: FlowStore) => (): Elements =>
|
export const onLoadGetElements = (currentStore: FlowStore) => (): Elements =>
|
||||||
parseElements(currentStore.nodes || [], currentStore.edges || [])
|
parseElements(currentStore.nodes || [], currentStore.edges || [])
|
||||||
|
|
||||||
export const onLoadToObject = (currentStore: FlowStore) => (): FlowExportObject => ({
|
export const onLoadToObject = (currentStore: FlowStore) => (): FlowExportObject => {
|
||||||
elements: parseElements(currentStore.nodes || [], currentStore.edges || []),
|
// we have to stringify/parse so objects containing refs (like nodes and edges) can potentially be saved in a storage
|
||||||
position: [currentStore.transform[0], currentStore.transform[1]],
|
return JSON.parse(
|
||||||
zoom: currentStore.transform[2],
|
JSON.stringify({
|
||||||
})
|
elements: parseElements(currentStore.nodes || [], currentStore.edges || []),
|
||||||
|
position: [currentStore.transform[0], currentStore.transform[1]],
|
||||||
|
zoom: currentStore.transform[2],
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export const getTransformForBounds = (
|
export const getTransformForBounds = (
|
||||||
bounds: Rect,
|
bounds: Rect,
|
||||||
|
|||||||
Reference in New Issue
Block a user