Skip to content

Commit

Permalink
Update handling of basic and admin edges
Browse files Browse the repository at this point in the history
  • Loading branch information
aptkingston committed Oct 1, 2024
1 parent 2099ee8 commit 354b504
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
export let target
const flow = useSvelteFlow()

Check failure on line 22 in packages/builder/src/components/backend/RoleEditor/RoleEdge.svelte

View workflow job for this annotation

GitHub Actions / lint

'flow' is assigned a value but never used. Allowed unused vars must match /^_/u
const { updateRole, selectedNodes } = getContext("flow")
const { deleteEdge, selectedNodes } = getContext("flow")
let iconHovered = false
let edgeHovered = false
Expand Down Expand Up @@ -53,13 +53,6 @@
return classes
}
const deleteEdge = async () => {
flow.deleteElements({
edges: [{ id }],
})
await updateRole(target)
}
const onEdgeMouseOver = () => {
edgeHovered = true
}
Expand Down Expand Up @@ -92,7 +85,7 @@
style:transform="translate(-50%, -50%) translate({labelX}px,{labelY}px)"
class="edge-label nodrag nopan"
class:active
on:click={deleteEdge}
on:click={() => deleteEdge(id)}
on:mouseover={() => (iconHovered = true)}
on:mouseout={() => (iconHovered = false)}
>
Expand Down
52 changes: 36 additions & 16 deletions packages/builder/src/components/backend/RoleEditor/RoleFlow.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,33 @@
$: handleExternalRoleChanges($roles)
// Converts a role doc into a node structure
const roleToNode = role => ({
id: role._id,
sourcePosition: Position.Right,
targetPosition: Position.Left,
type: "role",
position: { x: 0, y: 0 },
data: {
...role.uiMetadata,
custom: !role._id.match(/[A-Z]+/),
},
})
const roleToNode = role => {
const custom = !role._id.match(/[A-Z]+/)
return {
id: role._id,
sourcePosition: Position.Right,
targetPosition: Position.Left,
type: "role",
position: { x: 0, y: 0 },
data: {
...role.uiMetadata,
custom,
},
deletable: custom,
draggable: custom,
connectable: custom,
}
}
// Converts a node structure back into a role doc
const nodeToRole = node => {
const role = $roles.find(x => x._id === node.id)
// const inherits = $edges.filter(x => x.target === node.id).map(x => x.source)
const inherits = $edges.filter(x => x.target === node.id).map(x => x.source)
// TODO save inherits array
return {
...role,
// inherits,
inherits,
uiMetadata: {
displayName: node.data.displayName,
color: node.data.color,
Expand All @@ -63,7 +70,11 @@
const rolesToLayout = roles => {
let nodes = []
let edges = []
for (let role of roles.filter(role => role._id !== Roles.PUBLIC)) {
// Remove some builtins
const ignoredRoles = [Roles.PUBLIC, Roles.POWER]
roles = roles.filter(role => !ignoredRoles.includes(role._id))
for (let role of roles) {
// Add node for this role
nodes.push(roleToNode(role))
Expand Down Expand Up @@ -138,7 +149,6 @@
description: "Custom role",
},
permissionId: "write",
inherits: Roles.BASIC,
})
await tick()
selectNode(roleId)
Expand All @@ -148,7 +158,7 @@
const updateRole = async (roleId, metadata) => {
// Don't update builtins
const node = $nodes.find(x => x.id === roleId)
if (!node || !node.data.custom) {
if (!node) {
return
}
Expand All @@ -170,6 +180,15 @@
}
}
const deleteEdge = async edgeId => {
const edge = $edges.find(x => x.id === edgeId)
if (!edge) {
return
}
edges.set($edges.filter(x => x.id !== edgeId))
await updateRole(edge.target)
}
// Saves a new connection
const onConnect = async connection => {
await updateRole(connection.target)
Expand All @@ -183,6 +202,7 @@
createRole,
updateRole,
deleteRole,
deleteEdge,
})
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
export let data
export let id
export let selected
export let isConnectable
const { dragging, updateRole, deleteRole } = getContext("flow")
Expand All @@ -30,7 +31,7 @@
$: nameError = validateName(tempDisplayName, $roles)
$: descriptionError = validateDescription(tempDescription)
$: invalid = nameError || descriptionError
$: targetClasses = `target${$dragging ? "" : " hidden"}`
$: targetClasses = `target${!$dragging ? " hidden" : ""}`
const validateName = (name, roles) => {
if (!name?.length) {
Expand Down Expand Up @@ -91,15 +92,13 @@
</div>
{/if}
</div>
{#if id !== Roles.BASIC}
{#if isConnectable}
<Handle
type="target"
position={Position.Left}
class={targetClasses}
isConnectable={$dragging}
/>
{/if}
{#if id !== Roles.ADMIN}
<Handle type="source" position={Position.Right} />
{/if}
</div>
Expand Down
50 changes: 26 additions & 24 deletions packages/builder/src/components/backend/RoleEditor/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const dagreLayout = ({ nodes, edges }) => {
dagreGraph.setDefaultEdgeLabel(() => ({}))
dagreGraph.setGraph({
rankdir: "LR",
ranksep: GridResolution * 8,
ranksep: GridResolution * 4,
nodesep: GridResolution * 2,
})
nodes.forEach(node => {
Expand All @@ -19,6 +19,26 @@ export const dagreLayout = ({ nodes, edges }) => {
edges.forEach(edge => {
dagreGraph.setEdge(edge.source, edge.target)
})

// Add ephemeral edges for basic and admin so that we can position them properly

for (let node of nodes) {
if (
!edges.some(x => x.target === node.id) &&
node.id !== Roles.BASIC &&
node.id !== Roles.ADMIN
) {
dagreGraph.setEdge(Roles.BASIC, node.id)
}
if (
!edges.some(x => x.source === node.id) &&
node.id !== Roles.BASIC &&
node.id !== Roles.ADMIN
) {
dagreGraph.setEdge(node.id, Roles.ADMIN)
}
}

dagre.layout(dagreGraph)
nodes.forEach(node => {
const pos = dagreGraph.node(node.id)
Expand All @@ -34,32 +54,14 @@ export const dagreLayout = ({ nodes, edges }) => {

// Adds additional edges as needed to the flow structure to ensure compatibility with BB role logic
const sanitiseLayout = ({ nodes, edges }) => {
let additions = []

for (let node of nodes) {
// If a node does not inherit anything, let it inherit basic
if (!edges.some(x => x.target === node.id) && node.id !== Roles.BASIC) {
additions.push({
id: Helpers.uuid(),
source: Roles.BASIC,
target: node.id,
animated: true,
})
}

// If a node is not inherited by anything, let it be inherited by admin
if (!edges.some(x => x.source === node.id) && node.id !== Roles.ADMIN) {
additions.push({
id: Helpers.uuid(),
source: node.id,
target: Roles.ADMIN,
})
}
}
// Remove any inheritance of basic and admin since this is implied
edges = edges.filter(
edge => edge.source !== Roles.BASIC && edge.target !== Roles.ADMIN
)

return {
nodes,
edges: [...edges, ...additions],
edges,
}
}

Expand Down

0 comments on commit 354b504

Please sign in to comment.