Add auto layout and fit when doing any role operation other than updating metadata

This commit is contained in:
Andrew Kingston 2024-10-03 14:13:22 +01:00
parent a4aac8aae0
commit 818732250b
No known key found for this signature in database
2 changed files with 19 additions and 26 deletions

View File

@ -2,25 +2,10 @@
import { Button, ActionButton } from "@budibase/bbui" import { Button, ActionButton } from "@budibase/bbui"
import { useSvelteFlow } from "@xyflow/svelte" import { useSvelteFlow } from "@xyflow/svelte"
import { getContext } from "svelte" import { getContext } from "svelte"
import { autoLayout } from "./utils" import { ZoomDuration } from "./constants"
import { MaxAutoZoom, ZoomDuration } from "./constants"
const { nodes, edges, createRole } = getContext("flow") const { createRole, layoutAndFit } = getContext("flow")
const flow = useSvelteFlow() const flow = useSvelteFlow()
const autoFit = () =>
flow.fitView({ maxZoom: MaxAutoZoom, duration: ZoomDuration })
const addRole = async () => {
await createRole()
doAutoLayout()
autoFit()
}
const doAutoLayout = () => {
const layout = autoLayout({ nodes: $nodes, edges: $edges })
nodes.set(layout.nodes)
edges.set(layout.edges)
}
</script> </script>
<div class="control top-left"> <div class="control top-left">
@ -36,11 +21,10 @@
on:click={() => flow.zoomOut({ duration: ZoomDuration })} on:click={() => flow.zoomOut({ duration: ZoomDuration })}
/> />
</div> </div>
<Button secondary on:click={autoFit}>Zoom to fit</Button> <Button secondary on:click={layoutAndFit}>Auto layout</Button>
<Button secondary on:click={doAutoLayout}>Auto layout</Button>
</div> </div>
<div class="control bottom-right"> <div class="control bottom-right">
<Button icon="Add" cta on:click={addRole}>Add role</Button> <Button icon="Add" cta on:click={createRole}>Add role</Button>
</div> </div>
<style> <style>

View File

@ -6,7 +6,6 @@
Background, Background,
BackgroundVariant, BackgroundVariant,
useSvelteFlow, useSvelteFlow,
getNodesBounds,
} from "@xyflow/svelte" } from "@xyflow/svelte"
import "@xyflow/svelte/dist/style.css" import "@xyflow/svelte/dist/style.css"
import RoleNode from "./RoleNode.svelte" import RoleNode from "./RoleNode.svelte"
@ -23,7 +22,7 @@
} from "./utils" } from "./utils"
import { setContext, tick } from "svelte" import { setContext, tick } from "svelte"
import Controls from "./Controls.svelte" import Controls from "./Controls.svelte"
import { GridResolution, MaxAutoZoom } from "./constants" import { GridResolution, MaxAutoZoom, ZoomDuration } from "./constants"
import { roles } from "stores/builder" import { roles } from "stores/builder"
import { Roles } from "constants/backend" import { Roles } from "constants/backend"
import { getSequentialName } from "helpers/duplicate" import { getSequentialName } from "helpers/duplicate"
@ -77,6 +76,14 @@
}) })
} }
// Automatically lays out all roles and edges and zooms to fit them
const layoutAndFit = () => {
const layout = autoLayout({ nodes: $nodes, edges: $edges })
nodes.set(layout.nodes)
edges.set(layout.edges)
flow.fitView({ maxZoom: MaxAutoZoom, duration: ZoomDuration })
}
const createRole = async () => { const createRole = async () => {
const roleId = Helpers.uuid() const roleId = Helpers.uuid()
await roles.save({ await roles.save({
@ -91,6 +98,7 @@
permissionId: "write", permissionId: "write",
}) })
await tick() await tick()
layoutAndFit()
// Select the new node // Select the new node
nodes.update($nodes => { nodes.update($nodes => {
@ -114,6 +122,8 @@
} }
const deleteRole = async roleId => { const deleteRole = async roleId => {
nodes.set($nodes.filter(node => node.id !== roleId))
layoutAndFit()
const role = $roles.find(role => role._id === roleId) const role = $roles.find(role => role._id === roleId)
if (role) { if (role) {
roles.delete(role) roles.delete(role)
@ -122,15 +132,13 @@
const deleteEdge = async edgeId => { const deleteEdge = async edgeId => {
const edge = $edges.find(edge => edge.id === edgeId) const edge = $edges.find(edge => edge.id === edgeId)
if (!edge) {
return
}
edges.set($edges.filter(edge => edge.id !== edgeId)) edges.set($edges.filter(edge => edge.id !== edgeId))
layoutAndFit()
await updateRole(edge.target) await updateRole(edge.target)
} }
// Updates roles which have had a new connection
const onConnect = async connection => { const onConnect = async connection => {
layoutAndFit()
await updateRole(connection.target) await updateRole(connection.target)
} }
@ -144,6 +152,7 @@
updateRole, updateRole,
deleteRole, deleteRole,
deleteEdge, deleteEdge,
layoutAndFit,
}) })
</script> </script>