Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/website/e2e/workshop-workflow-runs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ test('Cloud upload, refresh, partial delivery and downloads retain one run @mobi
await page.setViewportSize({ width: 320, height: 851 })
await page.getByRole('tab', { name: 'Details', exact: true }).click()
await expect(
page.getByRole('img', { name: 'Workflow', exact: true })
page.getByRole('img', { name: /nodes of this workflow/i })
).toBeVisible()
const panelRight = await page
.getByRole('tabpanel', { name: 'Details', exact: true })
Expand Down
189 changes: 189 additions & 0 deletions apps/website/src/components/workshop/WorkflowGraph.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<script setup lang="ts">
import { computed, onMounted, ref, useTemplateRef } from 'vue'

import type { GraphPicture } from '../../lib/workshop/workflow-graph'
import { linkPath, readGraphPicture } from '../../lib/workshop/workflow-graph'
import { t } from '../../i18n/translations'
import WorkflowGraphNode from './WorkflowGraphNode.vue'

const {
source,
samples = [],
fallback
} = defineProps<{
/** Where the template JSON is published. */
source: string
/** The template's own pictures: a before and an after, or just the after. */
samples?: readonly string[]
/** The flat export, for when the JSON cannot be read. */
fallback?: string
}>()

const picture = ref<GraphPicture>()
const failed = ref(false)

const frame = useTemplateRef<HTMLDivElement>('frame')
const scale = ref(1)
const panX = ref(0)
const panY = ref(0)
const dragging = ref(false)

onMounted(async () => {
try {
const response = await fetch(source)
if (!response.ok) throw new Error(String(response.status))
const drawn = readGraphPicture(await response.json(), samples)
// Nothing to draw reads to the reader exactly as a refusal does.
if (drawn.nodes.length === 0) throw new Error('empty')
picture.value = drawn
} catch {
failed.value = true
}
})

const transform = computed(
() => `translate(${panX.value} ${panY.value}) scale(${scale.value})`
)

function zoomBy(factor: number) {
scale.value = Math.min(3, Math.max(0.2, scale.value * factor))
}

function reset() {
scale.value = 1
panX.value = 0
panY.value = 0
}

function onPointerDown(event: PointerEvent) {
dragging.value = true
frame.value?.setPointerCapture(event.pointerId)
}

function onPointerMove(event: PointerEvent) {
if (!dragging.value) return
panX.value += event.movementX
panY.value += event.movementY
}

function onPointerUp(event: PointerEvent) {
dragging.value = false
frame.value?.releasePointerCapture(event.pointerId)
}

const control =
'inline-flex size-7 cursor-pointer items-center justify-center rounded-lg text-content-secondary transition-colors hover:bg-transparency-white-t8 hover:text-content-bright'
</script>

<template>
<div
ref="frame"
class="relative h-112 touch-none overflow-hidden rounded-2xl bg-hub-surface select-none lg:h-128"
:class="dragging ? 'cursor-grabbing' : 'cursor-grab'"
data-testid="workflow-graph"
@pointerdown="onPointerDown"
@pointermove="onPointerMove"
@pointerup="onPointerUp"
@pointercancel="onPointerUp"
>
<svg
v-if="picture"
:viewBox="picture.viewBox"
class="size-full"
role="img"
:aria-label="t('workshop.workflow.graphAlt')"
preserveAspectRatio="xMidYMid meet"
>
<g :transform="transform">
<!-- Whoever built the graph framed and named parts of it, and that
framing is most of what makes it readable on the canvas. -->
<g v-for="group in picture.groups" :key="group.id">
<rect
:x="group.x"
:y="group.y"
:width="group.width"
:height="group.height"
rx="12"
:fill="group.color"
fill-opacity="0.16"
:stroke="group.color"
stroke-opacity="0.5"
/>
<text
:x="group.x + 14"
:y="group.y + 22"
:fill="group.color"
font-size="14"
font-weight="600"
>
{{ group.title }}
</text>
</g>

<path
v-for="link in picture.links"
:key="link.id"
:d="linkPath(link)"
fill="none"
:stroke="link.color"
stroke-width="2.5"
stroke-opacity="0.65"
/>
<WorkflowGraphNode v-for="node in picture.nodes" :key="node.id" :node />
</g>
</svg>

<!-- The flat export is what this page showed before, so a graph that
cannot be read falls back to it rather than to nothing. -->
<img
v-else-if="failed && fallback"
:src="fallback"
:alt="t('workshop.workflow.graphAlt')"
loading="lazy"
class="size-full object-contain"
data-testid="workflow-graph-flat"
/>

<p
v-else
class="flex size-full items-center justify-center text-sm text-content-muted"
>
{{
failed
? t('workshop.workflow.graphFailed')
: t('workshop.workflow.graphLoading')
}}
</p>

<span
v-if="picture"
class="pointer-events-none absolute top-3 left-3 rounded-full bg-black/50 px-2 py-0.5 text-3xs/4 font-bold tracking-wider text-content-secondary uppercase backdrop-blur-md"
>
{{ t('workshop.workflow.graphHint') }}
</span>

<div
v-if="picture"
class="absolute right-3 bottom-3 flex items-center gap-1 rounded-xl bg-black/50 p-1 backdrop-blur-md"
>
<button type="button" :class="control" @click="zoomBy(1 / 1.2)">
<span aria-hidden="true">&minus;</span>
<span class="sr-only">{{ t('workshop.workflow.zoomOut') }}</span>
</button>
<span class="px-1 font-mono text-2xs text-content-secondary tabular-nums">
{{ Math.round(scale * 100) }}%
</span>
<button type="button" :class="control" @click="zoomBy(1.2)">
<span aria-hidden="true">+</span>
<span class="sr-only">{{ t('workshop.workflow.zoomIn') }}</span>
</button>
<button
type="button"
class="cursor-pointer rounded-lg px-2 text-2xs text-content-secondary transition-colors hover:text-content-bright"
@click="reset"
>
{{ t('workshop.workflow.zoomReset') }}
</button>
</div>
</div>
</template>
128 changes: 128 additions & 0 deletions apps/website/src/components/workshop/WorkflowGraphNode.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<script setup lang="ts">
import type { GraphNode } from '../../lib/workshop/workflow-graph'

const { node } = defineProps<{ node: GraphNode }>()

// The canvas has a palette of its own, and the point of the preview is that it
// reads like the canvas. These are the editor's greys, not the site's.
const BODY = '#1e1e22'
const EDGE = '#ffffff1f'
const TITLE_TEXT = '#f5f5f5'
const SLOT_TEXT = '#c9c9c9'
const WIDGET_FILL = '#00000040'
const WIDGET_EDGE = '#ffffff14'
const WIDGET_TEXT = '#d4d4d4'

const TITLE_BAR = 24
const CORNER = 10
const WIDGET_INSET = 8
const WIDGET_PAD = 9
const LINE_HEIGHT = 14

/** The header, drawn as a rounded top rather than a clipped rectangle. */
const header = (n: GraphNode) =>
`M ${n.x} ${n.y + CORNER} a ${CORNER} ${CORNER} 0 0 1 ${CORNER} ${-CORNER} h ${n.width - CORNER * 2} a ${CORNER} ${CORNER} 0 0 1 ${CORNER} ${CORNER} v ${TITLE_BAR} h ${-n.width} z`
</script>

<template>
<g :opacity="node.dimmed ? 0.4 : 1">
<rect
:x="node.x"
:y="node.y"
:width="node.width"
:height="node.height"
rx="10"
:fill="node.body ?? BODY"
:stroke="EDGE"
/>
<path
:d="header(node)"
:fill="node.header ?? node.accent"
:fill-opacity="node.header ? 1 : 0.28"
/>
<text
:x="node.x + 12"
:y="node.y + 22"
:fill="TITLE_TEXT"
font-size="14"
font-weight="600"
>
{{ node.title }}
</text>

<g v-for="slot in node.inputs" :key="`in-${slot.name}-${slot.y}`">
<circle :cx="node.x" :cy="node.y + slot.y" r="4.5" :fill="slot.color" />
<text
:x="node.x + 12"
:y="node.y + slot.y + 4"
:fill="SLOT_TEXT"
font-size="11"
>
{{ slot.name }}
</text>
</g>

<g v-for="slot in node.outputs" :key="`out-${slot.name}-${slot.y}`">
<circle
:cx="node.x + node.width"
:cy="node.y + slot.y"
r="4.5"
:fill="slot.color"
/>
<text
:x="node.x + node.width - 12"
:y="node.y + slot.y + 4"
:fill="SLOT_TEXT"
font-size="11"
text-anchor="end"
>
{{ slot.name }}
</text>
</g>

<!-- The prompt, the file, the settings: what the node is actually set to,
which is the difference between a diagram and a picture of this graph. -->
<g v-for="widget in node.widgets" :key="widget.id">
<rect
:x="node.x + WIDGET_INSET"
:y="node.y + widget.y"
:width="node.width - WIDGET_INSET * 2"
:height="widget.height"
rx="9"
:fill="WIDGET_FILL"
:stroke="WIDGET_EDGE"
/>
<text
v-for="(line, index) in widget.lines"
:key="index"
:x="node.x + WIDGET_INSET + WIDGET_PAD"
:y="node.y + widget.y + WIDGET_PAD + 11 + index * LINE_HEIGHT"
:fill="WIDGET_TEXT"
font-size="11"
>
{{ line }}
</text>
</g>

<g v-if="node.picture">
<clipPath :id="`sample-${node.id}`">
<rect
:x="node.x + WIDGET_INSET"
:y="node.y + node.picture.y"
:width="node.width - WIDGET_INSET * 2"
:height="node.picture.height"
rx="9"
/>
</clipPath>
<image
:href="node.picture.href"
:x="node.x + WIDGET_INSET"
:y="node.y + node.picture.y"
:width="node.width - WIDGET_INSET * 2"
:height="node.picture.height"
preserveAspectRatio="xMidYMid slice"
:clip-path="`url(#sample-${node.id})`"
/>
</g>
</g>
</template>
Loading
Loading