/** * The form designer's sidebar (`form-design`, which `form:design` opens): what the form's * fields are. With no field selected it lists them by page, and a click selects one and * scrolls to it; with one selected it shows its settings and, for a radio group or a checkbox, * its buttons, with a + that adds one next in line (`form.addWidget`) and, for a radio group, * a copy of the whole group (`form.duplicate`). How a field looks is the style panel's. */ import { AnnotationToken, useAnnotationState } from '@embedpdf/react/annotation'; import { FormToken, type FormField, type FormFieldKind, type WidgetAnnotation, } from '@embedpdf/react/form'; import { useT } from '@embedpdf/react/i18n'; import { InteractionToken } from '@embedpdf/react/interaction'; import { useOptionalCapability, useOptionalSelector, usePageList } from '@embedpdf/react/runtime'; import { useStage } from '@embedpdf/react/stage'; import { useMemo } from 'react'; import { FORM_DESIGN_TOOL, isFormDesignTool } from '../config/commands'; import { FieldSettings } from './field-settings'; import { Icon } from './icons'; /** Each kind's icon and name, as its field tool shows them. */ const FAMILIES: Readonly>> = { text: { icon: 'formTextfield', labelKey: 'commands.form.textfield' }, checkbox: { icon: 'formCheckbox', labelKey: 'commands.form.checkbox' }, radio: { icon: 'formRadio', labelKey: 'commands.form.radio' }, combobox: { icon: 'formSelect', labelKey: 'commands.form.select' }, listbox: { icon: 'formListbox', labelKey: 'commands.form.listbox' }, pushbutton: { icon: 'formButton', labelKey: 'commands.form.button' }, signature: { icon: 'signature', labelKey: 'commands.form.signature' }, }; const NO_FIELDS: readonly FormField[] = []; const NO_WIDGETS: readonly WidgetAnnotation[] = []; /** A widget's object number, the key its rows and its selection share. */ const numberOf = (widget: WidgetAnnotation): number => widget.ref.kind === 'objectNumber' ? widget.ref.objectNumber : -1; /** The form's own actions: select a widget and show it, design on first if it was off. */ function useFormDesign() { const annotation = useOptionalCapability(AnnotationToken); const interaction = useOptionalCapability(InteractionToken); const stage = useStage(); const activeTool = useOptionalSelector( InteractionToken, (tools) => tools.getActiveToolId(), null, ); return { select(widget: WidgetAnnotation) { // The widgets are lent only while a form is designed. if (!isFormDesignTool(activeTool)) interaction?.activateTool(FORM_DESIGN_TOOL); annotation?.selection.select([widget.ref]); // Where the widget stands: a turned one's `box` is its frame before the turn. stage.reveal(widget.page, { rect: widget.rect }); }, /** Several widgets as the selection (a group just copied), so one drag moves them all. */ selectAll(widgets: readonly WidgetAnnotation[]) { const [first] = widgets; if (!first) return; if (!isFormDesignTool(activeTool)) interaction?.activateTool(FORM_DESIGN_TOOL); annotation?.selection.select(widgets.map((widget) => widget.ref)); stage.reveal(first.page, { rect: first.rect }); }, clear() { annotation?.selection.select([]); }, }; } /** How far a copy lands from its original, in page points: Duplicate's own step. */ const COPY_STEP = 10; export function FormDesignPanel() { const form = useOptionalCapability(FormToken); const selected = useOptionalSelector( FormToken, (capability) => capability.getSelectedField(), null, ); if (!form) return null; return selected ? : ; } /** Every field of the form, by page, in reading order. */ function FieldList() { const t = useT(); const design = useFormDesign(); const pages = usePageList(); const form = useOptionalCapability(FormToken); const fields = useOptionalSelector(FormToken, (capability) => capability.list(), NO_FIELDS); const groups = useMemo(() => { const indexOf = new Map(pages.map((page) => [page.ref.objectNumber, page.index])); const rows = fields.flatMap((field) => { const place = form?.getWidgets(field.ref)[0]; const index = place ? indexOf.get(place.page.objectNumber) : undefined; return place && index !== undefined ? [{ field, place, index }] : []; }); rows.sort( (left, right) => left.index - right.index || left.place.rect.y - right.place.rect.y || left.place.rect.x - right.place.rect.x, ); const byPage = new Map(); for (const row of rows) byPage.set(row.index, [...(byPage.get(row.index) ?? []), row]); return [...byPage]; }, [fields, form, pages]); if (groups.length === 0) { return (

{t('formDesign.empty')}

); } return (
{groups.map(([index, rows]) => (

{t('formDesign.page', { params: { page: index + 1 } })}

    {rows.map(({ field, place }) => { const family = FAMILIES[field.kind]; return (
  • ); })}
))}
); } /** The selected field: its settings and, for a radio group or a checkbox, its buttons. */ function SelectedField({ field }: { field: FormField }) { const t = useT(); const design = useFormDesign(); const family = FAMILIES[field.kind]; return (

{field.name}

{t('demo.fieldSettingsTitle')}

{(field.kind === 'radio' || field.kind === 'checkbox') && }
); } /** A radio group's or a checkbox's buttons, each selectable, and a + for one more. */ function Buttons({ field }: { field: FormField & { kind: 'radio' | 'checkbox' } }) { const t = useT(); const design = useFormDesign(); const form = useOptionalCapability(FormToken); const places = useOptionalSelector( FormToken, (capability) => capability.getWidgets(field.ref), NO_WIDGETS, ); const chosen = useAnnotationState((state) => state.selected); const isChosen = (place: WidgetAnnotation) => chosen.some( (record) => record.ref.kind === 'objectNumber' && record.ref.objectNumber === numberOf(place), ); const radio = field.kind === 'radio'; const pages = usePageList(); const add = async () => { if (!form) return; const { widget } = await form.addWidget(field.ref); design.select(widget); }; // The whole group again, beside this one: right and down, or left or up where that keeps on // its page a button that would leave it, as Duplicate places a copy. const duplicateGroup = async () => { if (!form) return; const sizes = new Map(pages.map((page) => [page.ref.objectNumber, page.size])); const step = (axis: 'x' | 'y', side: 'width' | 'height'): number => { const leaves = places.some((place) => { const size = sizes.get(place.page.objectNumber); return !!size && place.rect[axis] + place.rect[side] + COPY_STEP > size[side]; }); return leaves && places.every((place) => place.rect[axis] - COPY_STEP >= 0) ? -COPY_STEP : COPY_STEP; }; const { widgets } = await form.duplicate([field.ref], { offset: { x: step('x', 'width'), y: step('y', 'height') }, }); design.selectAll(widgets); }; return (

{t(radio ? 'formDesign.buttons' : 'formDesign.boxes')}

    {places.map((place, at) => { const widget = field.widgets.find((each) => each.objectNumber === numberOf(place)); return (
  • ); })}
{radio && ( )}
); }