Skip to content
SchmiedmayerLabPublic

About

FHIR questionnaire editor

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Anvil

A web-based editor for FHIR R4 Questionnaires. It runs entirely in the browser: no server, no backend. You can import JSON, edit, validate, preview and export.

Quick start

pnpm install
pnpm dev        # http://localhost:5173

Other scripts, run from the repository root:

Command What it does
pnpm build Production build of the app → packages/anvil/dist
pnpm preview Serve that build locally
pnpm test Unit and component tests (Vitest)
pnpm test:coverage The tests with a coverage report (packages/*/coverage)
pnpm typecheck TypeScript in every package
pnpm lint ESLint
pnpm check All three checks

Needs Node 22+ and pnpm 10 (corepack enable picks the pinned version).

What it does

  • Import: open a .json file, drop it on the window, or paste JSON. It accepts a Questionnaire, or a Bundle and lets you pick one of its Questionnaires.

  • Export: download or copy the JSON. Empty values are removed and keys follow FHIR element order. Elements the editor doesn't know survive the round trip unchanged.

  • Outline: drag items to reorder or nest them, or use the keyboard (↑↓ select, ⌥↑↓ move, ⌥←→ nest). Right-click an item for the rest: add an item below or inside, duplicate, move, copy its link ID or JSON, delete. Deleting asks first and says what goes with it.

  • Inspector: every R4 item element, including answer options, initial values, conditions (enableWhen) and extensions. List rows reorder by their drag handle (or ↑↓ on the focused handle). Renaming a linkId updates the conditions that point at it.

  • Coded answers: Convert to codes turns text answer options into coded ones and updates the conditions that used them. Move to shared value set moves a question's options into a value set other questions can use.

  • Value sets: define ValueSets inside the questionnaire (contained) and point questions at them (answerValueSet: "#id"). Edit them in the questionnaire's Value sets section or right on a question that uses one; renaming one updates every reference.

  • Units: quantity questions list the units they accept (questionnaire-unitOption, UCUM by default, with suggestions) or take them from a value set (questionnaire-unitValueSet).

  • Undo and redo: every edit can be undone. Hovering the buttons lists the steps, and clicking one goes back (or forward) to it. After an undo or redo the changed item is selected, the changed value flashes, and a notice says what changed, with a button to reverse it.

  • Validate: checks run on every change and cover:

    • structure: element names, cardinality, primitive formats;
    • the Questionnaire invariants que-0 … que-13;
    • contained resources and value sets (dom-2, dom-3, dom-4, vsd-1, statuses, missing or duplicate codes);
    • broken enableWhen references, mismatched answer types and answers that are not among the options or in the value set;
    • common mistakes, such as a choice question with no options or units on a non-quantity question.

    Issues show next to the field, in the outline and in the Issues tab.

  • Preview and JSON: a side panel, opened from the toolbar's Preview and JSON buttons or the validation status. The preview is a fillable form that applies enableWhen live and honours the standard item controls, value set answers and unit pickers. The JSON tab lets you edit the raw JSON and apply it. The panel starts closed, and the editor remembers whether you left it open.

  • Autosave: the current questionnaire is kept in the browser's localStorage.

Repository layout

packages/
  anvil-core/   Generic editor library: FHIR model, import/export, validation,
                React UI and the plugin API. Knows nothing about our own extensions.
  anvil/        The app we deploy. Renders anvil-core's editor and adds our own
                plugins (src/plugins). Depends on anvil-core.

anvil-core exports TypeScript source; the app's Vite build compiles it, so there is no separate library build step.

Extending the editor (plugins)

Everything in the inspector comes from plugins, including the built-in fields: corePlugin provides the R4 elements and standardExtensionsPlugin provides the common FHIR/SDC extensions. A plugin can contribute any of these:

Key Purpose
sections Inspector sections (for an item or for the questionnaire)
itemFields Fields in the item inspector; React components or extensionField(…)
questionnaireFields Fields in the questionnaire inspector
itemControls Codes offered in the Item control picker, from any code system
itemTemplates Entries in the Add item menu, e.g. a preconfigured slider
extensions Labels for extension URLs in the generic extensions list
validators Extra validation rules
previewRenderers How matching items render in the preview

Plugins apply in order. When a later plugin registers something with the same id, it replaces the earlier entry, which is how you override a built-in field. Fields that edit an extension declare its URL (extensionUrls); the generic Extensions list then hides that URL, so the extension isn't edited in two places.

import { definePlugin, extensionField, getExtensionValue } from 'anvil-core';

export const acmePlugin = definePlugin({
  id: 'acme',
  sections: [{ id: 'acme', label: 'Acme', order: 45, target: 'item' }],

  // Extra codes for the questionnaire-itemControl extension.
  itemControls: [
    { system: 'https://acme.org/fhir/CodeSystem/item-control', code: 'mood-scale', display: 'Mood scale', appliesTo: ['integer'] },
  ],

  // A field for a custom extension, in one call.
  itemFields: [
    extensionField({
      id: 'acme.sliderLabel',
      section: 'acme',
      url: 'https://acme.org/fhir/StructureDefinition/slider-label',
      label: 'Slider label',
      valueType: 'String',
      appliesTo: ['integer', 'decimal'],
    }),
    // …or any React component: ({ item, update }) => …
  ],

  validators: [
    (ctx) =>
      ctx.items
        .filter((v) => v.item.type === 'integer' && !getExtensionValue(v.item, 'https://acme.org/fhir/StructureDefinition/slider-label'))
        .map((v) => ({ severity: 'warning' as const, code: 'acme.label', message: 'Add a slider label', path: v.path, itemPath: v.itemPath })),
  ],
});

Then pass it to the editor:

<AnvilEditor plugins={[acmePlugin]} />

A field component receives { item, uid, document, update, registry }. update takes an immer recipe: update((draft) => { draft.text = 'Hi'; }). For text inputs, pass { coalesce: someKey } so typing a word becomes one undo step. The inspector's controls (Field, TextInput, NumberInput, Select, Checkbox, ValueInput, CodingInput, ValueSetPicker, DragHandle, …) are exported, so plugin fields can match the built-in ones. A field that throws shows an error box in its place; the rest of the editor keeps working.

Undo and redo work for plugin fields without extra code: steps are named from what changed. Three optional hooks make them better:

  • update(recipe, { label: 'Set the slider label' }) names the step in the undo history.
  • elements: ['text'] on a field definition (or extensionUrls) lets an undo highlight that field when it changes those elements.
  • data-flash-path={flashPath(uid, `answerOption[${i}]`)} on a list row lets an undo highlight just that row.

useEditor() also gives fields notify(kind, message) for notices and confirm({ title, message }) for a confirmation dialog.

Anvil's own additions go in packages/anvil/src/plugins.

Deployment

pnpm build produces a static site in packages/anvil/dist. Asset URLs are relative and the app has no client-side routes, so the same build works at a domain root or under any sub-path. No rewrites or server configuration are needed.

GitHub Pages

.github/workflows/deploy-pages.yml builds and publishes the app on every push to main; it can also be started by hand from the Actions tab.

One-time setup: Settings → Pages → Build and deployment → Source: GitHub Actions.

S3

scripts/deploy-s3.sh <bucket> [prefix]

The script builds the app and syncs it to s3://<bucket>/<prefix>/ with the AWS CLI, using your usual AWS credentials (AWS_PROFILE and so on). Files under assets/ have hashed names and are cached for a year; index.html is always revalidated. Serve the bucket with S3 static website hosting or through CloudFront, with index.html as the default root object.

About

FHIR questionnaire editor

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages