/** * The form as a flow chart (xyflow): steps, contact steps and ends with the paths between them. * Nodes are dragged to arrange them, connected by drawing from the right handle to another node, * and field types from the palette are dropped onto the canvas (new step) or onto a node (new * field in it). The analytics mode (Analytics.tsx) draws the figures of the published form over it. */ import React, { memo, useCallback, useContext, useEffect, useMemo, useState } from 'react'; import { createStore, useStore, type StoreApi } from 'zustand'; import { Background, BaseEdge, ControlButton, Controls, EdgeLabelRenderer, getBezierPath, Handle, Panel, MarkerType, MiniMap, Position, ReactFlow, ReactFlowProvider, applyNodeChanges, useReactFlow, type Connection, type Edge as FlowEdge, type EdgeProps, type InternalNode, type Node as FlowNode, type NodeChange, type NodeProps, type AriaLabelConfig, useConnection, useUpdateNodeInternals, } from '@xyflow/react'; import type { FormDocument, FormNode } from '@funnelforms/form-schema'; import type { LogicHint } from '../logic'; import { coverage } from '../coverage'; import { addRulePath, canAddRule, clearOutput, connectOutput, FOLD_FROM, NUMBER_TYPES, OTHERWISE, outputsOf, setNumberRule, type NumberRule, type Outputs, type RuleOutput, type RuleTone } from '../ports'; import { localeTag } from '@funnelforms/form-core'; import { testKind, testSteps } from '../experiments'; import { addContact, addEnd, addField, addStep, fieldKind, nodeName, setPositions, STEP_FIELD_TYPES, text, type StepKind } from '../document'; import type { Selection } from '../store'; import { kindName } from './Actions'; import { AnalyticsLegend, AnalyticsPanel, edgeFigures, NodeAnalyticsView, useAnalytics } from './Analytics'; import { PredictionPanel, usePrediction } from './Prediction'; import { createPlayback, planPlayback, PlaybackContext, PlaybackControl, PlaybackLayer, usePlayedShare, type PlaybackBox } from './PredictionPlayback'; import { contactFieldName, stepFieldName } from './catalog'; import { isHidden, prefillParam } from './Prefill'; import { SHORTCUTS_EVENT, useBuilder, useEnv } from './context'; import { describeCondition, edgeLabel, fieldIndex, fieldTitle, opName, ruleText } from './describe'; import { hintText, restText, useHints } from './Hints'; import { __, sprintf } from './i18n'; import { REVEAL_EVENT } from './Questions'; import { isQuizEdge } from '../quiz'; import { FiguresContext, NodeFigures } from './Stats'; import { TypeIcon } from './typeIcons'; import { ProBadge } from './ui'; import { proUrl } from '../edition'; import type { CanvasAnalytics, EdgeAnalytics } from '../stats'; /** Type of the data dragged from the palette. */ export const DRAG_TYPE = 'application/x-funnelforms-builder'; /** What the palette drags: a step with a field type, a contact step, an end, or a field. */ export type PaletteItem = { kind: 'step'; type: StepKind } | { kind: 'contact' } | { kind: 'end' } | { kind: 'field'; type: string }; type NodeData = { node: FormNode; locale: string; start: boolean; unreachable: boolean; /** The variant this step shows in an A/B test (A, B, …); empty outside a test. */ variant: string; /** Hints about the node and its paths, in words. */ hints: Array<{ level: LogicHint[ 'level' ]; text: string }>; /** Contact steps: what runs on sending. */ actions: string[]; selectedField: string | undefined; onSelectField: ( fieldId: string ) => void; onAfterSending: () => void; /** Where the paths of the node start (none on an end). */ outputs: OutputsView | null; onChooseField: ( fieldId: string ) => void; onSelectPath: ( edgeId: string ) => void; onAddRule: () => void; /** Takes the path away from an output (ports.ts, clearOutput). */ onClearOutput: ( handle: string ) => void; /** Changes a number rule in place; `part` names what was typed, so a run of keystrokes is one step to undo. */ onSetNumber: ( edgeId: string, fieldId: string, rule: NumberRule, part: string ) => void; }; /** An output of a node in words: an answer or a rule, with the dot its path starts at. */ type OutputRow = { kind: 'answer' | 'rule'; /** Rules checked before the answers, the answers, rules checked after them. */ group: 'first' | 'answer' | 'after'; handle: string; label: string; title: string; /** The path starting here; none for an answer that follows "Otherwise". */ edge?: string; /** The node that path leads to. */ to?: string; tone?: RuleTone; symbol?: string; warn?: boolean; /** A rule on a number of the step the card changes in place: its field, unit and bounds. */ number?: { field: string; rule: NumberRule; unit: string; unitBefore: boolean; max?: number; step?: number }; }; type OutputsView = { rows: OutputRow[]; /** Several fields with answers: the one the outputs follow, and the others to choose. */ fields: Array<{ id: string; label: string }>; field?: string; /** Answers of a multiple choice: the first path in the order decides. */ multiple: boolean; /** open: some answers have no path (a warning); stuck: no path leads on at all. */ otherwise: { label: string; target: string; title: string; edge?: string; to?: string; state: 'set' | 'none' | 'open' | 'stuck'; warn?: boolean }; /** A rule can be added from the card: always shown where rules are the way to branch (a number), else on the selected card. */ addRule: false | 'always' | 'quiet'; /** The paths are set elsewhere (the results of a quiz): the card shows them and changes nothing. */ locked: boolean; }; type CanvasNode = FlowNode; /** The handle the paths end at, at the head of a card (a second one takes drops anywhere on it). */ const TARGET = 'in'; const KIND_NAMES = (): Record => ( { step: __( 'Step', 'funnelforms-pro' ), contact: __( 'Contact step', 'funnelforms-pro' ), end: __( 'End', 'funnelforms-pro' ), } ); const NodeView = memo( function NodeView( { data, selected }: NodeProps ) { const { node, locale, start, unreachable, variant, hints, actions, selectedField, onSelectField, onAfterSending } = data; // While a path is drawn from another node, the whole card takes the drop. const drop = useConnection( ( c ) => c.inProgress && c.fromNode.id !== node.id ); // The card the path under the pointer leads to. const aimed = useStore( useContext( HotContext ), ( s ) => s.to === node.id ); const problems = hints.filter( ( h ) => 'info' !== h.level ); const worst = problems.some( ( h ) => 'error' === h.level ) ? 'error' : 'warn'; const figures = useContext( FiguresContext )?.get( node.id ); // The figures of the prediction or of the analytics; one of the two modes is on at a time. const forecast = 'free' === process.env.FUNNELFORMS_EDITION ? undefined : usePrediction()?.figures?.nodes.get( node.id ); const measured = 'free' === process.env.FUNNELFORMS_EDITION ? undefined : useAnalytics()?.figures?.nodes.get( node.id ); const analytics = forecast ?? measured; // While the visitors of the prediction play, its figures count up with the dots. const played = 'free' === process.env.FUNNELFORMS_EDITION ? null : usePlayedShare( node.id ); const name = nodeName( node, locale ); const fields = 'fields' in node ? ( node.fields as Array<{ id: string; type: string; label: Record; showWhen?: unknown; props?: unknown }> ) : []; return (
{ KIND_NAMES()[ node.type ] } { start ? { __( 'Start', 'funnelforms-pro' ) } : null } { variant ? ( { sprintf( __( 'A/B · %s', 'funnelforms-pro' ), variant ) } ) : null } { unreachable ? { __( 'Not reachable', 'funnelforms-pro' ) } : null } { problems.length && ! unreachable ? ( h.text ).join( '\n' ) }> { 'error' === worst ? __( 'Stuck', 'funnelforms-pro' ) : __( 'Check', 'funnelforms-pro' ) } ) : null }
{ name || { __( 'Without a title', 'funnelforms-pro' ) } }
{ fields.length > 1 || 'contact' === node.type ? (
    { fields.map( ( f ) => (
  • ) ) }
) : fields[ 0 ] ? (
{ loadingMeta( fields[ 0 ] ) ?? stepFieldName( fields[ 0 ].type ) }
) : null } { 'end' === node.type ?
{ endKind( node ) }
: null } { 'contact' === node.type ? ( ) : null } { 'free' !== process.env.FUNNELFORMS_EDITION && analytics ? : figures ? : null } { data.outputs ? : null }
); } ); /** The path the pointer is on, on a card or on the canvas: its row, its line and the card it leads to light up together. */ type Hot = { edge: string | null; to: string | null }; const COLD: Hot = { edge: null, to: null }; const HotContext = React.createContext>( createStore( () => COLD ) ); const SIGNS: Record = { lt: '<', lte: '≤', eq: '=', gte: '≥', gt: '>', neq: '≠', between: '↔' }; const NUMBER_OPS: Array = [ 'lt', 'lte', 'eq', 'gte', 'gt', 'between' ]; /** A number of a rule on the card; what is typed counts once it is a number. */ function NumberBox( { value, label, step, disabled, onCommit }: { value: number; label: string; step?: number; disabled: boolean; onCommit: ( n: number ) => void } ) { const [ draft, setDraft ] = useState( null ); const shown = draft ?? String( value ); return ( e.stopPropagation() } onKeyDown={ ( e ) => e.stopPropagation() } onBlur={ () => setDraft( null ) } onChange={ ( e ) => { setDraft( e.target.value ); const n = Number( e.target.value ); if ( '' !== e.target.value.trim() && Number.isFinite( n ) ) { onCommit( n ); } } } /> ); } /** A rule on the number of the step, changed where it stands: its comparison and its number, or two for "between". */ function NumberRuleInput( { number, disabled, onChange }: { number: NonNullable; disabled: boolean; onChange: ( rule: NumberRule, key: string ) => void } ) { const { rule } = number; const low = 'between' === rule.op ? rule.min : rule.value; const setOp = ( op: NumberRule[ 'op' ] ): void => { if ( 'between' === op ) { onChange( { op, min: low, max: 'between' === rule.op ? rule.max : Math.max( low, number.max ?? low ) }, 'op' ); } else { onChange( { op, value: low }, 'op' ); } }; const ops = NUMBER_OPS.includes( rule.op ) ? NUMBER_OPS : [ rule.op, ...NUMBER_OPS ]; return ( { number.unit && number.unitBefore ? { number.unit } : null } { 'between' === rule.op ? ( <> onChange( { ...rule, min: n }, 'min' ) } /> onChange( { ...rule, max: n }, 'max' ) } /> ) : ( onChange( { ...rule, value: n }, 'value' ) } /> ) } { number.unit && ! number.unitBefore ? { number.unit } : null } ); } type OutputActions = Pick; /** * The outputs of a node, each with the dot its path starts at, in the order the form checks * them: rules checked first, the answers of a choice (empty dot: follows "Otherwise"), rules * checked after them, and "Otherwise" last. A number rule of the step is changed in place. From * six answers on, the answers without a path of their own fold away. */ function NodeOutputs( { id, outputs, selected, onChooseField, onSelectPath, onAddRule, onClearOutput, onSetNumber }: { id: string; outputs: OutputsView; selected: boolean } & OutputActions ) { const env = useEnv(); const readOnly = env.readOnly || outputs.locked; const [ unfolded, setUnfolded ] = useState( false ); const answers = outputs.rows.filter( ( r ) => 'answer' === r.kind ); const foldable = answers.length >= FOLD_FROM && answers.some( ( r ) => ! r.edge ); const hidden = foldable && ! unfolded ? answers.filter( ( r ) => ! r.edge ).length : 0; const rows = hidden ? outputs.rows.filter( ( r ) => 'rule' === r.kind || r.edge ) : outputs.rows; const update = useUpdateNodeInternals(); // The dots move or change with the rows: React Flow measures them again. const key = rows.map( ( r ) => r.handle + ( r.number ? r.number.rule.op : '' ) ).join( ' ' ) + ' ' + outputs.otherwise.state; useEffect( () => update( id ), [ key, id, update ] ); const { otherwise } = outputs; const linked = answers.filter( ( r ) => r.edge ).length; const hotStore = useContext( HotContext ); const mine = [ ...outputs.rows.map( ( r ) => r.edge ), otherwise.edge ]; const hot = useStore( hotStore, ( s ) => ( s.edge && mine.includes( s.edge ) ? s.edge : null ) ); const hover = ( edge: string | undefined, to: string | undefined ) => ( edge ? { onMouseEnter: () => hotStore.setState( { edge, to: to ?? null } ), onMouseLeave: () => hotStore.setState( COLD ) } : {} ); const stop = ( run: () => void ) => ( e: React.MouseEvent ): void => { e.stopPropagation(); run(); }; const toggle = ( label: string, onClick: () => void ): React.ReactElement => ( ); const label = ( text: string, edge: string | undefined ): React.ReactElement => edge ? ( ) : ( { text } ); const clear = ( handle: string, name: string ): React.ReactElement | null => readOnly ? null : ( ); return (
{ outputs.fields.length > 1 ? ( ) : null } { rows.map( ( r, i ) => (
0 && rows[ i - 1 ]?.group !== r.group ? ' is-group-start' : '' ) } title={ r.title } data-path={ r.edge } { ...hover( r.edge, r.to ) } > { 'rule' === r.kind && ! r.number ? ( ) : null } { r.number && r.edge ? onSetNumber( r.edge as string, r.number?.field ?? '', rule, part ) } /> : label( r.label, r.edge ) } { r.edge ? clear( r.handle, sprintf( 'rule' === r.kind ? /* translators: %s: a rule, e.g. "< 20" */ __( 'Delete the rule “%s”', 'funnelforms-pro' ) : /* translators: %s: an answer or "Otherwise" */ __( 'Remove the path of “%s”', 'funnelforms-pro' ), ( r.symbol ? r.symbol + ' ' : '' ) + r.label ) ) : null }
) ) } { foldable ? (
{ hidden ? { sprintf( /* translators: %d: number of answers without a path of their own */ __( '%d more like “Otherwise”', 'funnelforms-pro' ), hidden ) } : null } { hidden ? toggle( __( 'Show all', 'funnelforms-pro' ), () => setUnfolded( true ) ) : toggle( __( 'Show fewer', 'funnelforms-pro' ), () => setUnfolded( false ) ) }
) : null } { outputs.multiple && linked > 1 ?
{ __( 'With several answers chosen, the first answer in this list with its own step decides.', 'funnelforms-pro' ) }
: null } { ! readOnly && ( 'always' === outputs.addRule || ( 'quiet' === outputs.addRule && selected ) ) ? ( ) : null }
{ otherwise.label } { otherwise.edge ? (
); } /** What an end shows, in a word or two. */ function endKind( node: Extract ): string { if ( node.success?.show && node.redirect ) { return __( 'Message, then redirect', 'funnelforms-pro' ); } if ( node.redirect ) { return __( 'Redirect', 'funnelforms-pro' ); } return node.success?.show ? __( 'Message', 'funnelforms-pro' ) : __( 'Shows nothing yet', 'funnelforms-pro' ); } const nodeTypes = { form: NodeView }; type PathData = { /** The path in the form (a path for several answers is drawn once per answer). */ edge?: string; /** Short label on the path; the whole condition is its tooltip. */ label?: string; title?: string; /** A number in front: the order the paths of a step are checked in. */ order?: number; warn?: boolean; otherwise?: boolean; /** Analytics mode: views that took the path. */ figures?: EdgeAnalytics; /** Where along the path the label sits (0.5: the middle). */ along?: number; }; type PathEdge = FlowEdge; /** A point of a cubic path ("M x,y C x,y x,y x,y", as getBezierPath draws it) at t from 0 to 1. */ export function pointOn( path: string, t: number ): [ number, number ] | null { const n = path.match( /-?\d+(?:\.\d+)?(?:e-?\d+)?/gi )?.map( Number ) ?? []; if ( n.length !== 8 ) { return null; } const [ x0 = 0, y0 = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0, x3 = 0, y3 = 0 ] = n; const u = 1 - t; const at = ( a: number, b: number, c: number, d: number ): number => u * u * u * a + 3 * u * u * t * b + 3 * u * t * t * c + t * t * t * d; return [ at( x0, x1, x2, x3 ), at( y0, y1, y2, y3 ) ]; } /** Where a node is for the playback of the visitors: its box and where its paths start and end. */ function boxOfNode( node: InternalNode | undefined ): PlaybackBox | null { const width = node?.measured.width; const height = node?.measured.height; if ( ! node || ! width || ! height ) { return null; } const { x, y } = node.internals.positionAbsolute; // As the paths are drawn: from the middle of the right handle to the middle of the left one. const targets = node.internals.handleBounds?.target; const target = targets?.find( ( h ) => TARGET === h.id ) ?? targets?.[ 0 ]; const sources = node.internals.handleBounds?.source; const source = sources?.find( ( h ) => OTHERWISE === h.id ) ?? sources?.[ 0 ]; return { x, y, width, height, in: target ? [ x + target.x, y + target.y + target.height / 2 ] : [ x, y + height / 2 ], out: source ? [ x + source.x + source.width, y + source.y + source.height / 2 ] : [ x + width, y + height / 2 ], }; } /** Points along the path between two nodes, as PathView draws it. */ function wayPoints( from: PlaybackBox, to: PlaybackBox ): Array<[ number, number ]> { const [ path ] = getBezierPath( { sourceX: from.out[ 0 ], sourceY: from.out[ 1 ], sourcePosition: Position.Right, targetX: to.in[ 0 ], targetY: to.in[ 1 ], targetPosition: Position.Left } ); const points: Array<[ number, number ]> = []; for ( let i = 0; i <= 24; i++ ) { const point = pointOn( path, i / 24 ); if ( point ) { points.push( point ); } } return points.length ? points : [ from.out, to.in ]; } /** Space kept between a path's pill and the steps beside it, and the narrowest and widest pill, in px. */ const LABEL_GAP = 8; const LABEL_MIN = 72; const LABEL_MAX = 200; /** Size of the arrowhead at the end of a path, in px. */ const ARROW = 13; /** A path with its condition as a readable label in a pill. */ const PathView = memo( function PathView( { id, sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, markerEnd, selected, data }: EdgeProps ) { const [ path, midX, midY ] = getBezierPath( { sourceX, sourceY, sourcePosition, targetX, targetY, targetPosition } ); const [ x, y ] = data?.along ? pointOn( path, data.along ) ?? [ midX, midY ] : [ midX, midY ]; const f = data?.figures; const shown = 'free' !== process.env.FUNNELFORMS_EDITION && f ? edgeFigures( f ) : null; // The pill sits in the middle between the two steps and is never wider than the room there, so it // does not cover their titles; the condition is cut short (it is the tooltip in full). const room = targetX - sourceX - 2 * LABEL_GAP; const maxWidth = room > 0 ? Math.max( LABEL_MIN, Math.min( LABEL_MAX, room ) ) : undefined; return ( <> { data?.label || data?.warn || shown ? (
{ data?.order ? { data.order } : null } { data?.warn ? ( ) : null } { data?.label ? { data.label } : null } { shown ? ( { f?.strongest ? ( <> { __( 'Strongest path', 'funnelforms-pro' ) + ': ' } ) : null } { shown.label } ) : null }
) : null } ); } ); const edgeTypes = { path: PathView }; type NodeActions = { onSelectField: ( nodeId: string, fieldId: string ) => void; onAfterSending: ( nodeId: string ) => void; onChooseField: ( nodeId: string, fieldId: string ) => void; onSelectPath: ( edgeId: string ) => void; onAddRule: ( nodeId: string ) => void; onClearOutput: ( nodeId: string, handle: string ) => void; onSetNumber: ( edgeId: string, fieldId: string, rule: NumberRule, part: string ) => void; }; /** The outputs of a node in words, as its card shows them. */ function outputsView( doc: FormDocument, node: FormNode, outputs: Outputs | undefined, hints: LogicHint[], fields: ReturnType ): OutputsView | null { if ( 'end' === node.type || ! outputs ) { return null; } const locale = doc.locales.default; const name = ( id: string ): string => { const target = doc.nodes.find( ( n ) => n.id === id ); return target ? nodeName( target, locale ) || __( 'Without a title', 'funnelforms-pro' ) : '?'; }; const mine = hints.filter( ( h ) => h.node === node.id ); const warned = new Set( mine.filter( ( h ) => h.edge && 'info' !== h.level ).map( ( h ) => h.edge ) ); const answers: OutputRow[] = outputs.answers.map( ( a ): OutputRow => { const answer = text( a.option.label, locale ).replace( /<[^>]*>/g, ' ' ).replace( /\s+/g, ' ' ).trim() || a.option.value; return a.edge ? { kind: 'answer', group: 'answer', handle: a.handle, label: answer, edge: a.edge.id, to: a.edge.to, warn: warned.has( a.edge.id ), title: sprintf( /* translators: %1$s: answer, %2$s: step */ __( 'Answer “%1$s” leads to %2$s', 'funnelforms-pro' ), answer, name( a.edge.to ) ) } : { kind: 'answer', group: 'answer', handle: a.handle, label: answer, title: __( 'Follows “Otherwise”. Drag from here to give this answer its own path.', 'funnelforms-pro' ) }; } ); const rule = ( r: RuleOutput ): OutputRow => { const row: OutputRow = { kind: 'rule', group: r.first ? 'first' : 'after', handle: r.handle, label: ruleText( r, doc, locale, fields ), edge: r.edge.id, to: r.edge.to, tone: r.tone, symbol: r.symbol, warn: warned.has( r.edge.id ), title: sprintf( /* translators: %1$s: condition, %2$s: step */ __( 'Rule “%1$s” leads to %2$s', 'funnelforms-pro' ), r.edge.when ? describeCondition( r.edge.when, doc, locale, fields ) : __( 'Always', 'funnelforms-pro' ), name( r.edge.to ) ), }; const n = r.number; if ( n?.rule && 'free' !== process.env.FUNNELFORMS_EDITION ) { const props = n.field.props as { unit?: { text: Record; position: 'before' | 'after' }; max?: number; step?: number }; row.number = { field: n.field.id, rule: n.rule, unit: props.unit ? text( props.unit.text, locale ).replace( /<[^>]*>/g, '' ).trim() : '', unitBefore: 'before' === props.unit?.position }; if ( undefined !== props.max ) { row.number.max = props.max; } if ( undefined !== props.step ) { row.number.step = props.step; } } return row; }; // In the order the form checks them: rules before the answers, the answers, rules after them. const rows = [ ...outputs.rules.filter( ( r ) => r.first ).map( rule ), ...answers, ...outputs.rules.filter( ( r ) => ! r.first ).map( rule ) ]; const fallback = outputs.otherwise; const stuck = mine.some( ( h ) => 'deadEnd' === h.code || 'contactNoEnd' === h.code ); const open = mine.some( ( h ) => 'noOtherwise' === h.code ); const rest = rows.length ? restText( coverage( doc, node )?.rest, locale ) : ''; // "Otherwise" once a path has a condition; until then the one way on. const label = 'contact' === node.type ? __( 'After that', 'funnelforms-pro' ) : rows.some( ( r ) => r.edge ) ? __( 'Otherwise', 'funnelforms-pro' ) : __( 'Go to', 'funnelforms-pro' ); let target = __( 'Not needed', 'funnelforms-pro' ); let state: OutputsView[ 'otherwise' ][ 'state' ] = 'none'; if ( fallback ) { target = name( fallback.to ); state = 'set'; } else if ( open ) { target = rest ? sprintf( /* translators: %s: answers without a path, e.g. "Other, 20 – 30" */ __( 'no path for: %s', 'funnelforms-pro' ), rest ) : __( 'Nowhere (visitors get stuck)', 'funnelforms-pro' ); state = 'open'; } else if ( stuck ) { target = 'contact' === node.type ? __( 'Nowhere (empty form)', 'funnelforms-pro' ) : __( 'Nowhere (visitors get stuck)', 'funnelforms-pro' ); state = 'stuck'; } const field = outputs.field; const own = 'step' === node.type ? node.fields : []; let addRule: OutputsView[ 'addRule' ] = false; const locked = doc.edges.some( ( e ) => e.from === node.id && isQuizEdge( doc, e ) ); if ( 'free' !== process.env.FUNNELFORMS_EDITION && ! locked && canAddRule( doc, node.id ) ) { addRule = ! field && own.some( ( f ) => NUMBER_TYPES.includes( f.type ) ) ? 'always' : 'quiet'; } return { rows, fields: outputs.fields.map( ( f ) => ( { id: f.id, label: fieldTitle( f, locale ) } ) ), field: field?.id, multiple: !! ( field && ( field.props as { multiple?: boolean } ).multiple ), otherwise: { label, target, title: label + ': ' + target + ( fallback && rest ? '\n' + sprintf( /* translators: %s: the answers that take the path without condition */ __( 'Applies to: %s', 'funnelforms-pro' ), rest ) : '' ), edge: fallback?.id, to: fallback?.to, state, warn: !! fallback && warned.has( fallback.id ), }, addRule, locked, }; } function buildNodes( doc: FormDocument, selection: Selection, hints: LogicHint[], outputs: Map, actions: NodeActions, previous: CanvasNode[] ): CanvasNode[] { const { onSelectField, onAfterSending, onChooseField, onSelectPath, onAddRule, onClearOutput, onSetNumber } = actions; const locale = doc.locales.default; const fields = fieldIndex( doc ); const before = new Map( previous.map( ( n ) => [ n.id, n ] ) ); const variants = new Map(); for ( const experiment of doc.experiments ?? [] ) { if ( 'design' !== testKind( experiment ) ) { testSteps( doc, experiment.id ).forEach( ( nodes, variant ) => nodes.forEach( ( id ) => variants.set( id, variant.toUpperCase() ) ) ); } } return doc.nodes.map( ( node, index ) => { const position = doc.layout?.nodes[ node.id ] ?? { x: index * 320, y: 0 }; const old = before.get( node.id ); return { id: node.id, type: 'form' as const, position, // Keep the measured size so edges stay drawn until the node is measured again. ...( old?.measured ? { measured: old.measured } : {} ), selected: selection.node === node.id, data: { node, locale, start: doc.start === node.id, unreachable: hints.some( ( h ) => h.node === node.id && 'unreachable' === h.code ), variant: 'free' === process.env.FUNNELFORMS_EDITION ? '' : variants.get( node.id ) ?? '', hints: hints.filter( ( h ) => h.node === node.id && 'unreachable' !== h.code ).map( ( h ) => ( { level: h.level, text: hintText( h, doc, locale ) } ) ), actions: 'contact' === node.type ? doc.actions.filter( ( a ) => ! a.node || a.node === node.id ).map( ( a ) => kindName( a.kind ) ) : [], selectedField: selection.node === node.id ? selection.field : undefined, onSelectField: ( fieldId: string ) => onSelectField( node.id, fieldId ), onAfterSending: () => onAfterSending( node.id ), outputs: outputsView( doc, node, outputs.get( node.id ), hints, fields ), onChooseField: ( fieldId: string ) => onChooseField( node.id, fieldId ), onSelectPath, onAddRule: () => onAddRule( node.id ), onClearOutput: ( handle: string ) => onClearOutput( node.id, handle ), onSetNumber, }, }; } ); } /** Colour of the arrowhead of a path, as its output on the card. */ const EDGE_COLOURS: Record = { answer: '#8f73f7', otherwise: '#a39db3', next: '#8a84a0', below: '#d9730d', above: '#168f63', range: '#6a30f5', equal: '#6a30f5', rule: '#2f6fed', }; /** Colour of the paths of the selected step and of the path under the pointer. */ const EDGE_ACTIVE = '#6a30f5'; /** `hot`: the path the pointer is on, on its card or on the canvas. */ export function buildEdges( doc: FormDocument, selection: Selection, hints: LogicHint[], analytics: CanvasAnalytics | null, outputs: Map, hot: string | null = null ): PathEdge[] { const locale = doc.locales.default; const fields = fieldIndex( doc ); const warned = new Set( hints.filter( ( h ) => h.edge && 'info' !== h.level ).map( ( h ) => h.edge ) ); const list: PathEdge[] = []; const active = new Set(); for ( const edge of doc.edges ) { const siblings = doc.edges.filter( ( e ) => e.from === edge.from ); const ports = outputs.get( edge.from ); // The card names the output of each path, so the path itself carries no label. const rule = ports?.rules.find( ( r ) => r.edge.id === edge.id ); let kind: string = rule ? 'rule' : 'answer'; if ( edge.default ) { // The only path of a step is its way on; beside paths with a condition it is "Otherwise". kind = siblings.length > 1 ? 'otherwise' : 'next'; } const tone = rule?.tone; const words = edge.when ? edgeLabel( edge.when, doc, locale, edge.from, fields ) : siblings.length > 1 ? __( 'Otherwise', 'funnelforms-pro' ) : ''; const data: PathData = { edge: edge.id }; if ( edge.when ) { data.title = describeCondition( edge.when, doc, locale, fields ); } if ( warned.has( edge.id ) ) { data.warn = true; data.title = hints .filter( ( h ) => h.edge === edge.id ) .map( ( h ) => hintText( h, doc, locale ) ) .concat( data.title ?? [] ) .join( '\n' ); } const figures = analytics?.edges.get( edge.id ); if ( figures ) { data.figures = figures; } // The paths of one step start at the same point, so their labels in the middle meet; nearer // their own step they stand apart. Not where other paths end at that step too. if ( siblings.length > 1 && 1 === doc.edges.filter( ( e ) => e.to === edge.to ).length ) { data.along = 0.7; } const from = doc.nodes.find( ( n ) => n.id === edge.from ); const to = doc.nodes.find( ( n ) => n.id === edge.to ); // The paths of the selected step stand out, and the path under the pointer. const out = selection.node === edge.from; const lit = hot === edge.id; const chosen = selection.edge === edge.id; const colour = out || lit || chosen ? EDGE_ACTIVE : EDGE_COLOURS[ tone ?? kind ]; const className = 'ffxb-edge ffxb-edge--' + kind + ( tone && tone !== kind ? ` ffxb-edge--${ tone }` : '' ) + ( data.warn ? ' ffxb-edge--warn' : '' ) + ( figures ? ( figures.sessions ? '' : ' ffxb-edge--none' ) + ( figures.strongest ? ' ffxb-edge--strongest' : '' ) : '' ) + ( out ? ' is-out' : '' ) + ( selection.node === edge.to ? ' is-in' : '' ) + ( lit ? ' is-hot' : '' ); // A path for several answers starts at each of them; the figures stand on the first. ( ports?.handles.get( edge.id ) ?? [ undefined ] ).forEach( ( handle, i ) => { if ( out || lit || chosen ) { active.add( i ? `${ edge.id }#${ handle ?? i }` : edge.id ); } list.push( { id: i ? `${ edge.id }#${ handle ?? i }` : edge.id, type: 'path' as const, source: edge.from, target: edge.to, sourceHandle: handle, targetHandle: TARGET, // Screen readers name the path by its steps, not by their ids. ariaLabel: from && to ? sprintf( __( 'Path from %1$s to %2$s', 'funnelforms-pro' ), nodeName( from, locale ), nodeName( to, locale ) ) + ( words ? ': ' + words : '' ) : undefined, data: i ? { ...data, figures: undefined, warn: undefined } : data, selected: chosen, className, // The head keeps its size on thick analytics paths (markers grow with the stroke by default). markerEnd: figures && ! i ? { type: MarkerType.ArrowClosed, color: colour, width: ARROW + figures.width, height: ARROW + figures.width, markerUnits: 'userSpaceOnUse' } : { type: MarkerType.ArrowClosed, color: colour, width: ARROW, height: ARROW }, } ); } ); } // Drawn last, the paths that stand out lie above the other paths (and, as all paths, below the cards). return active.size ? [ ...list.filter( ( e ) => ! active.has( e.id ) ), ...list.filter( ( e ) => active.has( e.id ) ) ] : list; } function readItem( event: React.DragEvent ): PaletteItem | null { const raw = event.dataTransfer.getData( DRAG_TYPE ); if ( ! raw ) { return null; } try { return JSON.parse( raw ) as PaletteItem; } catch { return null; } } /** The first nodes on the default path: what the canvas shows first in a large form. */ function opening( doc: FormDocument, count: number ): Array<{ id: string }> { const ids: string[] = []; let current: string | undefined = doc.start; while ( current && ! ids.includes( current ) && ids.length < count ) { ids.push( current ); const from: string = current; const out = doc.edges.filter( ( e ) => e.from === from ); current = ( out.find( ( e ) => e.default ) ?? out[ 0 ] )?.to; } return ids.map( ( id ) => ( { id } ) ); } function Flow() { const { store, ctx, readOnly } = useEnv(); const doc = useBuilder( ( s ) => s.doc ); const selection = useBuilder( ( s ) => s.selection ); const { change, select } = store.getState(); const flow = useReactFlow(); const [ nodes, setNodes ] = useState( [] ); // Large forms open on their beginning, readable, instead of all of it tiny. const [ focus ] = useState( () => ( doc && doc.nodes.length > 6 ? opening( doc, 3 ) : undefined ) ); const hints = useHints(); const prediction = 'free' === process.env.FUNNELFORMS_EDITION ? null : usePrediction(); const predicted = prediction?.figures ?? null; const measured = 'free' === process.env.FUNNELFORMS_EDITION ? null : useAnalytics()?.figures ?? null; const shown = predicted ?? measured; // Off until turned on: over the canvas it hides steps. const [ map, setMap ] = useState( () => readMapChoice() ?? false ); // The visitors of the prediction played on the canvas; gone with the prediction mode. const [ playback ] = useState( createPlayback ); const playing = useStore( playback.store, ( s ) => s.playing ); useEffect( () => { if ( ! predicted ) { playback.reset(); } }, [ predicted, playback ] ); const boxOf = useCallback( ( id: string ) => boxOfNode( flow.getInternalNode( id ) ), [ flow ] ); const traces = prediction?.result?.simulation.traces; const play = useCallback( () => { if ( traces?.length ) { playback.start( planPlayback( traces, boxOf, wayPoints, prediction?.resultOf?.model.personas.map( ( p ) => p.id ), flow.getNodes().map( ( n ) => n.id ) ) ); } }, [ traces, boxOf, playback, prediction?.resultOf, flow ] ); // The path under the pointer, on a card or on the canvas. const [ hotStore ] = useState( () => createStore( () => COLD ) ); const hot = useStore( hotStore, ( s ) => s.edge ); // The field whose answers are the outputs, for steps with several (chosen on the card). const [ chosen, setChosen ] = useState>( {} ); const outputs = useMemo( () => new Map( ( doc?.nodes ?? [] ).map( ( n ) => [ n.id, outputsOf( doc as FormDocument, n, chosen[ n.id ] ) ] as const ) ), [ doc, chosen ] ); const actions = useMemo( () => ( { onSelectField: ( node, field ) => select( { node, field } ), onAfterSending: ( node ) => select( { node }, 'logic' ), onChooseField: ( node, field ) => setChosen( ( c ) => ( { ...c, [ node ]: field } ) ), onSelectPath: ( edge ) => select( { edge } ), onAddRule: ( node ) => { let id: string | null = null; let inPlace = false; change( ( d ) => { id = addRulePath( d, node ); const source = d.nodes.find( ( n ) => n.id === node ); // A number rule is changed on the card; other rules open in the panel. inPlace = !! id && !! source && outputsOf( d, source ).rules.some( ( r ) => r.edge.id === id && r.number?.rule ); } ); if ( id ) { select( inPlace ? { node } : { edge: id } ); } }, onClearOutput: ( node, handle ) => { change( ( d ) => clearOutput( d, node, handle ) ); hotStore.setState( COLD ); }, onSetNumber: ( edge, field, rule, part ) => change( ( d ) => setNumberRule( d, edge, field, rule ), `number:${ edge }:${ part }` ), } ), [ select, change, hotStore ] ); useEffect( () => { if ( doc ) { setNodes( ( previous ) => buildNodes( doc, selection, hints, outputs, actions, previous ) ); } }, [ doc, selection, hints, outputs, actions ] ); const edges = useMemo( () => ( doc ? buildEdges( doc, selection, hints, shown, outputs, hot ) : [] ), [ doc, selection, hints, shown, outputs, hot ] ); const toggleMap = (): void => { setMap( ! map ); try { window.localStorage.setItem( MAP_KEY, map ? '0' : '1' ); } catch { // The choice is then kept only until the page is left. } }; // The question list asks to bring a step into view. useEffect( () => { const reveal = ( event: Event ): void => { const id = ( event as CustomEvent ).detail; if ( flow.getNode( id ) ) { void flow.fitView( { nodes: [ { id } ], duration: 300, maxZoom: Math.max( 1, flow.getZoom() ) } ); } }; window.addEventListener( REVEAL_EVENT, reveal ); return () => window.removeEventListener( REVEAL_EVENT, reveal ); }, [ flow ] ); const onNodesChange = useCallback( ( changes: NodeChange[] ) => { // Moving and measuring happen here; removing and selecting go through the store. const kept = changes.filter( ( c ) => 'position' === c.type || 'dimensions' === c.type ); if ( kept.length ) { setNodes( ( current ) => applyNodeChanges( kept, current ) ); } }, [] ); const onDragStop = useCallback( ( _event: React.MouseEvent, _node: CanvasNode, moved: CanvasNode[] ) => { const positions = Object.fromEntries( moved.map( ( n ) => [ n.id, n.position ] ) ); change( ( d ) => setPositions( d, positions ), 'move:' + Object.keys( positions ).join( ',' ) ); }, [ change ] ); const onConnect = useCallback( ( connection: Connection ) => { let id: string | null = null; change( ( d ) => { id = connectOutput( d, connection.source, connection.sourceHandle, connection.target ); } ); if ( id ) { select( { edge: id } ); } }, [ change, select ] ); const onDrop = ( event: React.DragEvent ): void => { const item = readItem( event ); if ( ! item || readOnly ) { return; } event.preventDefault(); const target = ( event.target as Element ).closest?.( '[data-node]' )?.getAttribute( 'data-node' ) ?? null; const position = flow.screenToFlowPosition( { x: event.clientX, y: event.clientY } ); let created: Selection = {}; change( ( d ) => { const node = target ? d.nodes.find( ( n ) => n.id === target ) : undefined; if ( node && 'end' !== node.type && ( 'field' === item.kind || 'step' === item.kind ) ) { // A field type dropped onto a node goes into that node. const field = addField( d, node.id, item.type, ctx ); if ( field ) { created = { node: node.id, field }; return; } } const placement = { position: { x: position.x - 120, y: position.y - 40 } }; if ( 'step' === item.kind && ( 'loading' === item.type || ( STEP_FIELD_TYPES as readonly string[] ).includes( item.type ) ) ) { created = { node: addStep( d, item.type, ctx, placement ) }; } else if ( 'contact' === item.kind ) { created = { node: addContact( d, ctx, placement ) }; } else if ( 'end' === item.kind ) { created = { node: addEnd( d, ctx, placement ) }; } } ); select( created ); }; if ( ! doc ) { return null; } return ( className={ hot ? 'has-hot' : undefined } nodes={ nodes } edges={ edges } nodeTypes={ nodeTypes } edgeTypes={ edgeTypes } onNodesChange={ onNodesChange } onNodeDragStop={ onDragStop } onConnect={ onConnect } onNodeClick={ ( _e, node ) => select( { node: node.id } ) } onEdgeClick={ ( _e, edge ) => select( { edge: edge.data?.edge ?? edge.id } ) } onEdgeMouseEnter={ ( _e, edge ) => hotStore.setState( { edge: edge.data?.edge ?? edge.id, to: edge.target } ) } onEdgeMouseLeave={ () => hotStore.setState( COLD ) } connectionRadius={ 28 } onPaneClick={ () => select( {} ) } onDragOver={ ( e ) => { if ( e.dataTransfer.types.includes( DRAG_TYPE ) ) { e.preventDefault(); e.dataTransfer.dropEffect = 'copy'; } } } onDrop={ onDrop } nodesDraggable={ ! readOnly } nodesConnectable={ ! readOnly } deleteKeyCode={ null } selectionKeyCode={ null } multiSelectionKeyCode={ null } fitView fitViewOptions={ { padding: 0.15, minZoom: 0.6, maxZoom: 1, nodes: focus } } minZoom={ 0.2 } maxZoom={ 1.5 } proOptions={ { hideAttribution: true } } aria-label={ __( 'Steps of the form', 'funnelforms-pro' ) } ariaLabelConfig={ flowLabels() } > window.dispatchEvent( new Event( SHORTCUTS_EVENT ) ) } aria-label={ __( 'Keyboard shortcuts', 'funnelforms-pro' ) } title={ __( 'Keyboard shortcuts', 'funnelforms-pro' ) }> { map ? ( ( { step: '#a98bf9', contact: '#6a30f5', end: '#d9cdfc' } )[ ( n.data as NodeData ).node.type ] } /> ) : null } { 'free' === process.env.FUNNELFORMS_EDITION ? ( ) : (
) }
{ 'free' !== process.env.FUNNELFORMS_EDITION && predicted && prediction?.result && prediction.resultOf ? ( <> ) : null } { 'free' !== process.env.FUNNELFORMS_EDITION && shown ? ( ) : null }
); } /** In Funnelforms Free: the switch of the analytics mode leads to the page about Pro. */ function ProAnalytics() { const url = proUrl(); // As in Pro, the figures need the right to see entries: without it there is no switch. if ( ! useEnv().rights.entries ) { return null; } const content = ( <> { __( 'Analytics', 'funnelforms-pro' ) } ); return url ? ( { content } ) : ( { content } ); } const MAP_KEY = 'ffxb-canvas-map'; /** Whether the user turned the overview map on or off before; null when never. */ function readMapChoice(): boolean | null { try { const value = window.localStorage.getItem( MAP_KEY ); return null === value ? null : '1' === value; } catch { return null; } } /** The next thing to do while the form is still taking shape. */ /** The labels React Flow reads out and shows on its controls, in the language of the admin. */ function flowLabels(): Partial { /* translators: %1$s: horizontal position, %2$s: vertical position of the step on the canvas. */ const moved: Record = { up: __( 'Step moved up. New position: %1$s, %2$s.', 'funnelforms-pro' ), down: __( 'Step moved down. New position: %1$s, %2$s.', 'funnelforms-pro' ), left: __( 'Step moved left. New position: %1$s, %2$s.', 'funnelforms-pro' ), right: __( 'Step moved right. New position: %1$s, %2$s.', 'funnelforms-pro' ), }; return { 'node.a11yDescription.default': __( 'Press Enter or Space to select a step.', 'funnelforms-pro' ), 'node.a11yDescription.keyboardDisabled': __( 'Press Enter or Space to select a step. Then move it with the arrow keys; Escape cancels.', 'funnelforms-pro' ), 'node.a11yDescription.ariaLiveMessage': ( { direction, x, y }: { direction: string; x: number; y: number } ) => sprintf( moved[ direction ] ?? moved.right ?? '', Math.round( x ), Math.round( y ) ), 'edge.a11yDescription.default': __( 'Press Enter or Space to select a path.', 'funnelforms-pro' ), 'controls.ariaLabel': __( 'Canvas controls', 'funnelforms-pro' ), 'controls.zoomIn.ariaLabel': __( 'Zoom in', 'funnelforms-pro' ), 'controls.zoomOut.ariaLabel': __( 'Zoom out', 'funnelforms-pro' ), 'controls.fitView.ariaLabel': __( 'Show all steps', 'funnelforms-pro' ), 'minimap.ariaLabel': __( 'Overview map', 'funnelforms-pro' ), 'handle.ariaLabel': __( 'Connection point', 'funnelforms-pro' ), }; } function CanvasTip( { doc }: { doc: FormDocument } ) { let tip: string | null = null; if ( 1 === doc.nodes.length ) { tip = __( 'Drag a field type from the left onto the canvas for the next step, or onto this step to ask more on it.', 'funnelforms-pro' ); } else if ( 0 === doc.edges.length ) { tip = __( 'Connect the steps: drag from the dot on the right of a step to the next one.', 'funnelforms-pro' ); } else if ( ! doc.nodes.some( ( n ) => 'end' === n.type ) ) { tip = __( 'Add an end from the left: it shows a thank-you message or leads to a page.', 'funnelforms-pro' ); } return tip ? ( { tip } ) : null; } export function Canvas() { return (
); } /** "Loading animation · 4 s" on a step that is one: what it is and how long it waits. */ function loadingMeta( field: { type: string; props?: unknown } ): string | null { if ( 'loading' !== fieldKind( field ) ) { return null; } const seconds = ( ( field.props as { autoAdvanceMs?: number } ).autoAdvanceMs ?? 0 ) / 1000; const number = new Intl.NumberFormat( localeTag( document.documentElement.lang ), { maximumFractionDigits: 1 } ).format( seconds ); return `${ stepFieldName( 'loading' ) } · ${ sprintf( /* translators: %s: number of seconds */ __( '%s s', 'funnelforms-pro' ), number ) }`; }