payload-canvas 0.3.0-next.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1010 -55
- package/dist/admin/AiKeyField.d.ts +8 -0
- package/dist/admin/AiKeyField.js +54 -0
- package/dist/admin/BuilderTab.js +4 -1
- package/dist/admin/DocumentEditView.d.ts +9 -0
- package/dist/admin/DocumentEditView.js +45 -0
- package/dist/admin/LayoutField.d.ts +2 -2
- package/dist/admin/LayoutField.js +10 -7
- package/dist/admin/TemplateConditionsField.d.ts +9 -0
- package/dist/admin/TemplateConditionsField.js +22 -0
- package/dist/admin/aiKeyField.scss +58 -0
- package/dist/admin/client.d.ts +3 -0
- package/dist/admin/client.js +6 -0
- package/dist/admin/diff/LayoutDiff.js +3 -3
- package/dist/admin/diff/changes.js +13 -0
- package/dist/admin/diff/layoutDiff.scss +48 -48
- package/dist/admin/editor/Canvas.js +20 -5
- package/dist/admin/editor/DragLayer.js +1 -1
- package/dist/admin/editor/Editor.d.ts +3 -1
- package/dist/admin/editor/Editor.js +6 -3
- package/dist/admin/editor/InlineToolbar.js +1 -1
- package/dist/admin/editor/Inspector.js +115 -23
- package/dist/admin/editor/Library.js +40 -15
- package/dist/admin/editor/Outline.js +26 -19
- package/dist/admin/editor/Overlay.js +9 -4
- package/dist/admin/editor/actions.d.ts +18 -2
- package/dist/admin/editor/actions.js +53 -3
- package/dist/admin/editor/assistant/AssistantPanel.js +4 -2
- package/dist/admin/editor/assistant/ConnectAgents.js +1 -1
- package/dist/admin/editor/assistant/assistant.scss +65 -65
- package/dist/admin/editor/classes/ClassManager.d.ts +4 -0
- package/dist/admin/editor/classes/ClassManager.js +56 -0
- package/dist/admin/editor/classes/controller.d.ts +112 -0
- package/dist/admin/editor/classes/controller.js +326 -0
- package/dist/admin/editor/classes/manager.scss +262 -0
- package/dist/admin/editor/classes/managerModel.d.ts +5 -0
- package/dist/admin/editor/classes/managerModel.js +27 -0
- package/dist/admin/editor/comments/CommentLayer.d.ts +2 -0
- package/dist/admin/editor/comments/CommentLayer.js +143 -0
- package/dist/admin/editor/comments/CommentsPanel.d.ts +8 -0
- package/dist/admin/editor/comments/CommentsPanel.js +73 -0
- package/dist/admin/editor/comments/Thread.d.ts +26 -0
- package/dist/admin/editor/comments/Thread.js +84 -0
- package/dist/admin/editor/comments/comments.scss +539 -0
- package/dist/admin/editor/comments/controller.d.ts +48 -0
- package/dist/admin/editor/comments/controller.js +142 -0
- package/dist/admin/editor/contentMode.d.ts +17 -0
- package/dist/admin/editor/contentMode.js +37 -0
- package/dist/admin/editor/dnd/DropOutlines.d.ts +1 -0
- package/dist/admin/editor/dnd/DropOutlines.js +73 -0
- package/dist/admin/editor/dnd/dnd.scss +45 -2
- package/dist/admin/editor/dnd/mode.d.ts +4 -0
- package/dist/admin/editor/dnd/mode.js +39 -0
- package/dist/admin/editor/editor.scss +138 -183
- package/dist/admin/editor/empty/EmptyStart.js +21 -43
- package/dist/admin/editor/empty/empty.scss +3 -3
- package/dist/admin/editor/fields/AddButton.d.ts +15 -0
- package/dist/admin/editor/fields/AddButton.js +14 -0
- package/dist/admin/editor/fields/ArrayField.js +4 -3
- package/dist/admin/editor/fields/CheckedInputs.d.ts +1 -0
- package/dist/admin/editor/fields/ConditionRows.d.ts +19 -0
- package/dist/admin/editor/fields/ConditionRows.js +137 -0
- package/dist/admin/editor/fields/GroupField.d.ts +5 -2
- package/dist/admin/editor/fields/GroupField.js +6 -2
- package/dist/admin/editor/fields/ListWhereField.d.ts +8 -0
- package/dist/admin/editor/fields/ListWhereField.js +20 -0
- package/dist/admin/editor/fields/QueryFiltersField.d.ts +18 -0
- package/dist/admin/editor/fields/QueryFiltersField.js +41 -0
- package/dist/admin/editor/fields/RelatedField.d.ts +19 -0
- package/dist/admin/editor/fields/RelatedField.js +44 -0
- package/dist/admin/editor/fields/SortField.d.ts +13 -0
- package/dist/admin/editor/fields/SortField.js +37 -0
- package/dist/admin/editor/fields/WhereField.d.ts +25 -0
- package/dist/admin/editor/fields/WhereField.js +47 -0
- package/dist/admin/editor/fields/access.d.ts +1 -1
- package/dist/admin/editor/fields/collectionFields.d.ts +6 -0
- package/dist/admin/editor/fields/collectionFields.js +15 -0
- package/dist/admin/editor/fields/conditions.d.ts +99 -0
- package/dist/admin/editor/fields/conditions.js +339 -0
- package/dist/admin/editor/fields/fields.scss +346 -34
- package/dist/admin/editor/fields/listOptions.d.ts +44 -0
- package/dist/admin/editor/fields/listOptions.js +111 -0
- package/dist/admin/editor/fields/queryFilterRows.d.ts +8 -0
- package/dist/admin/editor/fields/queryFilterRows.js +29 -0
- package/dist/admin/editor/fields/whereRows.d.ts +27 -0
- package/dist/admin/editor/fields/whereRows.js +167 -0
- package/dist/admin/editor/generate/GenerateImage.js +1 -1
- package/dist/admin/editor/generate/generate.scss +48 -48
- package/dist/admin/editor/icons.d.ts +17 -1
- package/dist/admin/editor/icons.js +33 -2
- package/dist/admin/editor/inline.js +9 -0
- package/dist/admin/editor/insert/InsertHandle.js +5 -2
- package/dist/admin/editor/insert/picker.d.ts +3 -1
- package/dist/admin/editor/insert/picker.js +17 -10
- package/dist/admin/editor/insert/spots.d.ts +12 -1
- package/dist/admin/editor/insert/spots.js +49 -10
- package/dist/admin/editor/layout/FrameResize.js +1 -1
- package/dist/admin/editor/layout/LeftPanel.js +12 -7
- package/dist/admin/editor/layout/layout.scss +4 -4
- package/dist/admin/editor/live/PresenceUI.js +1 -1
- package/dist/admin/editor/live/presence.scss +1 -1
- package/dist/admin/editor/live/useMultiplayer.js +9 -1
- package/dist/admin/editor/locale/LocaleField.d.ts +2 -2
- package/dist/admin/editor/locale/LocaleField.js +4 -2
- package/dist/admin/editor/locale/locale.scss +12 -12
- package/dist/admin/editor/media/ImageEditor.js +1 -1
- package/dist/admin/editor/media/media.scss +23 -23
- package/dist/admin/editor/menu/Menu.js +1 -1
- package/dist/admin/editor/menu/blockMenu.js +32 -18
- package/dist/admin/editor/menu/menu.scss +16 -16
- package/dist/admin/editor/motion/MotionPanel.js +2 -2
- package/dist/admin/editor/motion/model.d.ts +2 -2
- package/dist/admin/editor/motion/model.js +2 -2
- package/dist/admin/editor/motion/motion.scss +2 -2
- package/dist/admin/editor/names.js +11 -3
- package/dist/admin/editor/regions/RegionOverlay.js +1 -1
- package/dist/admin/editor/regions/RegionPane.js +1 -1
- package/dist/admin/editor/renderField.d.ts +5 -1
- package/dist/admin/editor/renderField.js +73 -13
- package/dist/admin/editor/runtime.d.ts +14 -0
- package/dist/admin/editor/runtime.js +68 -6
- package/dist/admin/editor/sections/ComponentFields.d.ts +8 -0
- package/dist/admin/editor/sections/ComponentFields.js +116 -0
- package/dist/admin/editor/sections/PropertyControls.d.ts +18 -0
- package/dist/admin/editor/sections/PropertyControls.js +65 -0
- package/dist/admin/editor/sections/components.d.ts +29 -0
- package/dist/admin/editor/sections/components.js +112 -0
- package/dist/admin/editor/sections/components.scss +238 -0
- package/dist/admin/editor/sections/variants.d.ts +32 -0
- package/dist/admin/editor/sections/variants.js +59 -0
- package/dist/admin/editor/sections/variants.scss +90 -0
- package/dist/admin/editor/shortcuts.d.ts +4 -1
- package/dist/admin/editor/shortcuts.js +19 -3
- package/dist/admin/editor/store.d.ts +18 -0
- package/dist/admin/editor/store.js +46 -0
- package/dist/admin/editor/styles/ClassBar.d.ts +14 -0
- package/dist/admin/editor/styles/ClassBar.js +90 -0
- package/dist/admin/editor/styles/ClassCss.d.ts +4 -0
- package/dist/admin/editor/styles/ClassCss.js +28 -0
- package/dist/admin/editor/styles/ClassDialog.d.ts +2 -0
- package/dist/admin/editor/styles/ClassDialog.js +151 -0
- package/dist/admin/editor/styles/ColorPicker.js +6 -3
- package/dist/admin/editor/styles/RawClasses.d.ts +26 -1
- package/dist/admin/editor/styles/RawClasses.js +184 -32
- package/dist/admin/editor/styles/StylesPanel.d.ts +3 -1
- package/dist/admin/editor/styles/StylesPanel.js +53 -11
- package/dist/admin/editor/styles/VariantBar.js +3 -3
- package/dist/admin/editor/styles/classEdit.d.ts +4 -0
- package/dist/admin/editor/styles/classEdit.js +20 -0
- package/dist/admin/editor/styles/classes.scss +381 -0
- package/dist/admin/editor/styles/context.d.ts +24 -4
- package/dist/admin/editor/styles/context.js +32 -9
- package/dist/admin/editor/styles/controls.d.ts +3 -1
- package/dist/admin/editor/styles/controls.js +31 -13
- package/dist/admin/editor/styles/popover.d.ts +6 -0
- package/dist/admin/editor/styles/popover.js +5 -5
- package/dist/admin/editor/styles/sections.js +2 -2
- package/dist/admin/editor/styles/styles.scss +115 -139
- package/dist/admin/editor/styles/suggestions.d.ts +22 -0
- package/dist/admin/editor/styles/suggestions.js +77 -0
- package/dist/admin/editor/styles/tokens.js +14 -0
- package/dist/admin/editor/templates/Bindable.d.ts +14 -6
- package/dist/admin/editor/templates/Bindable.js +146 -61
- package/dist/admin/editor/templates/SamplePicker.js +2 -2
- package/dist/admin/editor/templates/Visibility.d.ts +9 -0
- package/dist/admin/editor/templates/Visibility.js +54 -0
- package/dist/admin/editor/templates/binding.d.ts +11 -4
- package/dist/admin/editor/templates/binding.js +23 -3
- package/dist/admin/editor/templates/templates.scss +117 -90
- package/dist/admin/editor/templates/visibilityRows.d.ts +22 -0
- package/dist/admin/editor/templates/visibilityRows.js +83 -0
- package/dist/admin/editor/tooltip/tooltip.scss +4 -4
- package/dist/admin/editor/topbar/ContentModeToggle.d.ts +5 -0
- package/dist/admin/editor/topbar/ContentModeToggle.js +26 -0
- package/dist/admin/editor/topbar/DocumentActions.d.ts +1 -1
- package/dist/admin/editor/topbar/DocumentActions.js +6 -5
- package/dist/admin/editor/topbar/DocumentTitle.js +8 -3
- package/dist/admin/editor/topbar/TopBar.js +15 -3
- package/dist/admin/editor/topbar/contentMode.scss +35 -0
- package/dist/admin/editor/topbar/screens/DrawerRouter.d.ts +1 -1
- package/dist/admin/editor/topbar/screens/ScreenDrawer.js +1 -1
- package/dist/admin/editor/topbar/screens/drawerRouterSupport.d.ts +1 -1
- package/dist/admin/editor/topbar/screens/screens.scss +20 -20
- package/dist/admin/editor/topbar/settingsDrawer.js +3 -3
- package/dist/admin/editor/topbar/settingsDrawer.scss +7 -7
- package/dist/admin/editor/topbar/topbar.scss +50 -48
- package/dist/admin/editor/ui/Select.js +1 -1
- package/dist/admin/editor/ui/controls.scss +26 -26
- package/dist/admin/formChoice.d.ts +3 -0
- package/dist/admin/formChoice.js +10 -0
- package/dist/admin/layoutField.scss +69 -0
- package/dist/admin/screen/BuilderScreen.d.ts +6 -1
- package/dist/admin/screen/BuilderScreen.js +5 -4
- package/dist/admin/server/BuilderView.js +10 -3
- package/dist/admin/templateConditions.scss +24 -0
- package/dist/ai/adapters/anthropic.d.ts +2 -2
- package/dist/ai/adapters/anthropic.js +17 -4
- package/dist/ai/adapters/cloudflare-gateway.js +7 -1
- package/dist/ai/adapters/cloudflare-workers-ai.js +2 -0
- package/dist/ai/adapters/openai-compatible.js +1 -0
- package/dist/ai/adapters/openrouter.js +2 -0
- package/dist/ai/config.d.ts +24 -4
- package/dist/ai/config.js +27 -8
- package/dist/ai/endpoint.d.ts +5 -2
- package/dist/ai/endpoint.js +31 -26
- package/dist/ai/images/cloudflare-workers-ai.js +6 -2
- package/dist/ai/images/openai.js +8 -3
- package/dist/ai/images/openrouter.js +6 -2
- package/dist/ai/images/service.d.ts +10 -4
- package/dist/ai/images/service.js +24 -8
- package/dist/ai/loop.d.ts +5 -5
- package/dist/ai/openai-format.d.ts +7 -0
- package/dist/ai/openai-format.js +17 -5
- package/dist/ai/prompt.d.ts +8 -0
- package/dist/ai/prompt.js +30 -4
- package/dist/ai/settings/index.d.ts +62 -0
- package/dist/ai/settings/index.js +264 -0
- package/dist/ai/settings/secret.d.ts +11 -0
- package/dist/ai/settings/secret.js +46 -0
- package/dist/ai/settings/shared.d.ts +22 -0
- package/dist/ai/settings/shared.js +3 -0
- package/dist/ai/tools.d.ts +3 -0
- package/dist/ai/tools.js +83 -10
- package/dist/ai/types.d.ts +43 -0
- package/dist/blocks/builtins/adjacentLinks.d.ts +5 -0
- package/dist/blocks/builtins/adjacentLinks.js +76 -0
- package/dist/blocks/builtins/carousel.d.ts +5 -0
- package/dist/blocks/builtins/carousel.js +115 -0
- package/dist/blocks/builtins/disclosure.d.ts +14 -0
- package/dist/blocks/builtins/disclosure.js +97 -0
- package/dist/blocks/builtins/embed.d.ts +15 -0
- package/dist/blocks/builtins/embed.js +47 -0
- package/dist/blocks/builtins/faq.d.ts +17 -0
- package/dist/blocks/builtins/faq.js +88 -0
- package/dist/blocks/builtins/filterLink.d.ts +4 -0
- package/dist/blocks/builtins/filterLink.js +35 -0
- package/dist/blocks/builtins/icon.d.ts +10 -0
- package/dist/blocks/builtins/icon.js +45 -0
- package/dist/blocks/builtins/index.d.ts +40 -0
- package/dist/blocks/builtins/index.js +71 -0
- package/dist/blocks/builtins/marquee.d.ts +5 -0
- package/dist/blocks/builtins/marquee.js +68 -0
- package/dist/blocks/builtins/popup.d.ts +5 -0
- package/dist/blocks/builtins/popup.js +83 -0
- package/dist/blocks/builtins/sectionRef.d.ts +40 -0
- package/dist/blocks/builtins/sectionRef.js +100 -0
- package/dist/blocks/builtins/shared.d.ts +28 -0
- package/dist/blocks/builtins/shared.js +62 -0
- package/dist/blocks/builtins/tableOfContents.d.ts +7 -0
- package/dist/blocks/builtins/tableOfContents.js +38 -0
- package/dist/blocks/builtins/tabs.d.ts +6 -0
- package/dist/blocks/builtins/tabs.js +87 -0
- package/dist/blocks/client/adjacentLinks.d.ts +4 -0
- package/dist/blocks/client/adjacentLinks.js +4 -0
- package/dist/blocks/client/carousel.d.ts +45 -0
- package/dist/blocks/client/carousel.js +53 -0
- package/dist/blocks/client/embed.d.ts +21 -0
- package/dist/blocks/client/embed.js +23 -0
- package/dist/blocks/client/field.d.ts +7 -0
- package/dist/blocks/client/field.js +8 -0
- package/dist/blocks/client/filterLink.d.ts +19 -0
- package/dist/blocks/client/filterLink.js +30 -0
- package/dist/blocks/client/helpers.d.ts +21 -0
- package/dist/blocks/client/helpers.js +24 -0
- package/dist/blocks/client/marquee.d.ts +14 -0
- package/dist/blocks/client/marquee.js +25 -0
- package/dist/blocks/client/menu.d.ts +46 -0
- package/dist/blocks/client/menu.js +49 -0
- package/dist/blocks/client/popup.d.ts +15 -0
- package/dist/blocks/client/popup.js +16 -0
- package/dist/blocks/client/tabs.d.ts +10 -0
- package/dist/blocks/client/tabs.js +11 -0
- package/dist/blocks/defaults.d.ts +15 -34
- package/dist/blocks/defaults.js +339 -180
- package/dist/blocks/index.d.ts +4 -3
- package/dist/blocks/index.js +3 -2
- package/dist/classes/actions.d.ts +97 -0
- package/dist/classes/actions.js +296 -0
- package/dist/classes/collection.d.ts +5 -0
- package/dist/classes/collection.js +189 -0
- package/dist/classes/config.d.ts +34 -0
- package/dist/classes/config.js +8 -0
- package/dist/classes/endpoints.d.ts +2 -0
- package/dist/classes/endpoints.js +120 -0
- package/dist/classes/index.d.ts +36 -0
- package/dist/classes/index.js +42 -0
- package/dist/classes/mcp.d.ts +6 -0
- package/dist/classes/mcp.js +161 -0
- package/dist/classes/shared.d.ts +131 -0
- package/dist/classes/shared.js +300 -0
- package/dist/classes/sheet.d.ts +34 -0
- package/dist/classes/sheet.js +126 -0
- package/dist/classes/usage.d.ts +57 -0
- package/dist/classes/usage.js +208 -0
- package/dist/comments/collection.d.ts +38 -0
- package/dist/comments/collection.js +226 -0
- package/dist/comments/config.d.ts +15 -0
- package/dist/comments/config.js +2 -0
- package/dist/comments/index.d.ts +20 -0
- package/dist/comments/index.js +22 -0
- package/dist/comments/shared.d.ts +54 -0
- package/dist/comments/shared.js +104 -0
- package/dist/core/attrs.d.ts +27 -0
- package/dist/core/attrs.js +132 -0
- package/dist/core/bindings.d.ts +171 -15
- package/dist/core/bindings.js +697 -27
- package/dist/core/blockNames.d.ts +9 -0
- package/dist/core/blockNames.js +11 -0
- package/dist/core/blocks.d.ts +62 -4
- package/dist/core/blocks.js +164 -7
- package/dist/core/classes.d.ts +18 -2
- package/dist/core/classes.js +37 -4
- package/dist/core/components.d.ts +72 -0
- package/dist/core/components.js +294 -0
- package/dist/core/contentMode.d.ts +27 -0
- package/dist/core/contentMode.js +0 -0
- package/dist/core/dropTarget.d.ts +11 -0
- package/dist/core/dropTarget.js +24 -1
- package/dist/core/fieldAccess.js +1 -1
- package/dist/core/fieldHooks.d.ts +1 -1
- package/dist/core/fields.d.ts +5 -0
- package/dist/core/fields.js +11 -0
- package/dist/core/headings.d.ts +24 -0
- package/dist/core/headings.js +55 -0
- package/dist/core/index.d.ts +10 -3
- package/dist/core/index.js +10 -3
- package/dist/core/layers.d.ts +9 -0
- package/dist/core/layers.js +29 -0
- package/dist/core/motion.d.ts +24 -15
- package/dist/core/motion.js +20 -12
- package/dist/core/operations.d.ts +6 -0
- package/dist/core/operations.js +124 -6
- package/dist/core/payloadBlocks.js +7 -1
- package/dist/core/references.js +28 -0
- package/dist/core/schema.js +35 -1
- package/dist/core/sectionOrigin.d.ts +6 -0
- package/dist/core/sectionOrigin.js +21 -0
- package/dist/core/sections.d.ts +72 -0
- package/dist/core/sections.js +331 -0
- package/dist/core/templateChoice.d.ts +24 -0
- package/dist/core/templateChoice.js +43 -0
- package/dist/core/tree.js +37 -2
- package/dist/core/types.d.ts +219 -2
- package/dist/core/validate.d.ts +4 -1
- package/dist/core/validate.js +48 -6
- package/dist/core/visibility.d.ts +63 -0
- package/dist/core/visibility.js +214 -0
- package/dist/core/whereValues.d.ts +22 -0
- package/dist/core/whereValues.js +43 -0
- package/dist/css/classes.d.ts +16 -0
- package/dist/css/classes.js +172 -0
- package/dist/css/index.d.ts +32 -1
- package/dist/css/index.js +64 -5
- package/dist/css/tokens.d.ts +14 -1
- package/dist/css/tokens.js +61 -2
- package/dist/index.d.ts +6 -3
- package/dist/index.js +7 -2
- package/dist/live/contentMode.d.ts +24 -0
- package/dist/live/contentMode.js +39 -0
- package/dist/live/draftAccess.js +1 -1
- package/dist/live/endpoints.js +3 -0
- package/dist/live/session.d.ts +6 -1
- package/dist/live/session.js +8 -0
- package/dist/live/storage.js +4 -2
- package/dist/live/types.d.ts +16 -1
- package/dist/mcp/index.d.ts +12 -1
- package/dist/mcp/index.js +177 -48
- package/dist/mcp/shared.d.ts +123 -30
- package/dist/mcp/shared.js +222 -11
- package/dist/plugin/hook.d.ts +7 -1
- package/dist/plugin/hook.js +30 -4
- package/dist/plugin/index.d.ts +27 -8
- package/dist/plugin/index.js +113 -26
- package/dist/plugin/links.d.ts +24 -0
- package/dist/plugin/links.js +27 -0
- package/dist/plugin/listCollections.d.ts +20 -3
- package/dist/plugin/listCollections.js +54 -8
- package/dist/plugin/references.d.ts +5 -0
- package/dist/plugin/references.js +1 -1
- package/dist/plugin/sections.d.ts +14 -7
- package/dist/plugin/sections.js +69 -23
- package/dist/plugin/sectionsUsedIn.d.ts +6 -0
- package/dist/plugin/sectionsUsedIn.js +24 -0
- package/dist/plugin/templateEndpoint.d.ts +9 -0
- package/dist/plugin/templateEndpoint.js +49 -0
- package/dist/plugin/templates.d.ts +4 -0
- package/dist/plugin/templates.js +42 -12
- package/dist/protocol/index.d.ts +19 -2
- package/dist/react/canvas/BuilderCanvas.js +149 -24
- package/dist/react/canvas/classes.d.ts +17 -0
- package/dist/react/canvas/classes.js +102 -0
- package/dist/react/canvas/controls.d.ts +31 -0
- package/dist/react/canvas/controls.js +75 -0
- package/dist/react/canvas/fetchDocs.d.ts +17 -3
- package/dist/react/canvas/fetchDocs.js +51 -10
- package/dist/react/canvas/inline/rich/editor.js +1 -6
- package/dist/react/canvas/measure.d.ts +12 -1
- package/dist/react/canvas/measure.js +130 -7
- package/dist/react/canvas/resolveLayout.d.ts +1 -1
- package/dist/react/canvas/resolveLayout.js +19 -7
- package/dist/react/canvas/serverBlocks.d.ts +11 -0
- package/dist/react/canvas/serverBlocks.js +28 -1
- package/dist/react/canvas/thumbnail/ThumbnailCanvas.js +18 -5
- package/dist/react/classes/ClassesStyle.d.ts +27 -0
- package/dist/react/classes/ClassesStyle.js +27 -0
- package/dist/react/components/Button.d.ts +1 -1
- package/dist/react/components/Button.js +4 -2
- package/dist/react/components/CollectionList.d.ts +9 -3
- package/dist/react/components/CollectionList.js +28 -7
- package/dist/react/components/Container.d.ts +5 -2
- package/dist/react/components/Container.js +10 -4
- package/dist/react/components/Field.js +2 -2
- package/dist/react/components/Heading.js +9 -2
- package/dist/react/components/Image.d.ts +9 -0
- package/dist/react/components/Image.js +16 -1
- package/dist/react/components/Link.d.ts +1 -1
- package/dist/react/components/Link.js +5 -3
- package/dist/react/components/List.d.ts +2 -1
- package/dist/react/components/List.js +8 -2
- package/dist/react/components/Menu.d.ts +26 -1
- package/dist/react/components/Menu.js +116 -25
- package/dist/react/components/RichText.d.ts +6 -2
- package/dist/react/components/RichText.js +23 -2
- package/dist/react/components/builtins/Accordion.d.ts +10 -0
- package/dist/react/components/builtins/Accordion.js +38 -0
- package/dist/react/components/builtins/AdjacentLinks.d.ts +11 -0
- package/dist/react/components/builtins/AdjacentLinks.js +20 -0
- package/dist/react/components/builtins/Carousel.d.ts +24 -0
- package/dist/react/components/builtins/Carousel.js +209 -0
- package/dist/react/components/builtins/Disclosure.d.ts +8 -0
- package/dist/react/components/builtins/Disclosure.js +14 -0
- package/dist/react/components/builtins/Embed.d.ts +11 -0
- package/dist/react/components/builtins/Embed.js +23 -0
- package/dist/react/components/builtins/EmbedHtml.d.ts +15 -0
- package/dist/react/components/builtins/EmbedHtml.js +51 -0
- package/dist/react/components/builtins/EmbedPreview.d.ts +34 -0
- package/dist/react/components/builtins/EmbedPreview.js +105 -0
- package/dist/react/components/builtins/FilterLink.d.ts +8 -0
- package/dist/react/components/builtins/FilterLink.js +30 -0
- package/dist/react/components/builtins/Icon.d.ts +14 -0
- package/dist/react/components/builtins/Icon.js +28 -0
- package/dist/react/components/builtins/Marquee.d.ts +8 -0
- package/dist/react/components/builtins/Marquee.js +15 -0
- package/dist/react/components/builtins/Popup.d.ts +26 -0
- package/dist/react/components/builtins/Popup.js +146 -0
- package/dist/react/components/builtins/SectionRef.d.ts +8 -0
- package/dist/react/components/builtins/SectionRef.js +21 -0
- package/dist/react/components/builtins/Tab.d.ts +7 -0
- package/dist/react/components/builtins/Tab.js +21 -0
- package/dist/react/components/builtins/TableOfContents.d.ts +21 -0
- package/dist/react/components/builtins/TableOfContents.js +36 -0
- package/dist/react/components/builtins/Tabs.d.ts +17 -0
- package/dist/react/components/builtins/Tabs.js +71 -0
- package/dist/react/components/builtins/glyphs.d.ts +8 -0
- package/dist/react/components/builtins/glyphs.js +7 -0
- package/dist/react/components/builtins/index.d.ts +49 -0
- package/dist/react/components/builtins/index.js +70 -0
- package/dist/react/components/builtins/tabsContext.d.ts +12 -0
- package/dist/react/components/builtins/tabsContext.js +6 -0
- package/dist/react/components/index.js +13 -8
- package/dist/react/components/lazy.d.ts +32 -0
- package/dist/react/components/lazy.js +20 -0
- package/dist/react/components/lazyBlock.d.ts +17 -0
- package/dist/react/components/lazyBlock.js +23 -0
- package/dist/react/index.d.ts +8 -3
- package/dist/react/index.js +7 -2
- package/dist/react/motion/runtime.js +10 -6
- package/dist/react/motion/style.js +1 -1
- package/dist/react/render/PayloadRoot.js +1 -1
- package/dist/react/render/RenderLayout.d.ts +9 -7
- package/dist/react/render/RenderLayout.js +145 -41
- package/dist/react/render/canvasMode.d.ts +13 -0
- package/dist/react/render/canvasMode.js +13 -0
- package/dist/react/render/canvasServer.js +4 -3
- package/dist/react/render/canvasServerTypes.d.ts +5 -0
- package/dist/react/render/lists.d.ts +139 -5
- package/dist/react/render/lists.js +401 -9
- package/dist/react/render/marks.d.ts +15 -0
- package/dist/react/render/marks.js +23 -0
- package/dist/react/render/payload.d.ts +8 -1
- package/dist/react/render/payload.js +12 -3
- package/dist/react/render/preview.d.ts +11 -0
- package/dist/react/render/preview.js +13 -0
- package/dist/react/render/relations.d.ts +10 -0
- package/dist/react/render/relations.js +39 -0
- package/dist/react/render/resolve.d.ts +26 -2
- package/dist/react/render/resolve.js +52 -25
- package/dist/react/render/reveal.d.ts +17 -0
- package/dist/react/render/reveal.js +39 -0
- package/dist/react/render/sections.d.ts +15 -0
- package/dist/react/render/sections.js +79 -0
- package/dist/react/render/templates.d.ts +47 -0
- package/dist/react/render/templates.js +139 -0
- package/dist/react/render/types.d.ts +66 -4
- package/dist/react/server.d.ts +3 -38
- package/dist/react/server.js +9 -86
- package/dist/react/theme/ThemeLiveLoader.d.ts +4 -0
- package/dist/react/theme/ThemeLiveLoader.js +5 -0
- package/dist/react/theme/ThemeStyle.d.ts +13 -2
- package/dist/react/theme/ThemeStyle.js +14 -4
- package/dist/theme/admin/ColorField.d.ts +2 -2
- package/dist/theme/admin/ColorField.js +2 -2
- package/dist/theme/admin/FontField.d.ts +2 -2
- package/dist/theme/admin/FontField.js +2 -2
- package/dist/theme/admin/SaveSignal.js +2 -2
- package/dist/theme/admin/Variables.d.ts +11 -0
- package/dist/theme/admin/Variables.js +115 -0
- package/dist/theme/admin/fields.scss +161 -48
- package/dist/theme/client.d.ts +1 -0
- package/dist/theme/client.js +2 -0
- package/dist/theme/css.d.ts +6 -1
- package/dist/theme/css.js +12 -4
- package/dist/theme/global.js +83 -0
- package/dist/theme/index.d.ts +1 -0
- package/dist/theme/index.js +1 -0
- package/dist/theme/server.d.ts +15 -0
- package/dist/theme/server.js +43 -0
- package/dist/theme/usage.d.ts +23 -0
- package/dist/theme/usage.js +68 -0
- package/dist/theme/variables.d.ts +72 -0
- package/dist/theme/variables.js +231 -0
- package/package.json +25 -28
- package/dist/react/components/Quote.d.ts +0 -3
- package/dist/react/components/Quote.js +0 -12
- package/dist/react/components/Spacer.d.ts +0 -3
- package/dist/react/components/Spacer.js +0 -6
package/dist/core/bindings.js
CHANGED
|
@@ -5,8 +5,10 @@
|
|
|
5
5
|
// the bound props take the document's value. Missing values (undefined, null, "", []) keep the
|
|
6
6
|
// literal prop.
|
|
7
7
|
import { isLinkField } from '../blocks/link.js';
|
|
8
|
+
import { COLLECTION_LIST_BLOCK, PAGE_PARAM, REPEAT_BLOCK } from './blockNames.js';
|
|
8
9
|
import { dataFields } from './fields.js';
|
|
9
10
|
import { isPlainObject, walkBlocks } from './tree.js';
|
|
11
|
+
import { VALUELESS_OPERATORS, visibilityConditions, visibilityGroups } from './visibility.js';
|
|
10
12
|
// ---------------------------------------------------------------------------
|
|
11
13
|
// Names shared by the plugin, the renderer and the editor
|
|
12
14
|
// ---------------------------------------------------------------------------
|
|
@@ -20,14 +22,33 @@ export const TEMPLATE_DEFAULT_FIELD = 'isDefault';
|
|
|
20
22
|
export const TEMPLATE_PREVIEW_FIELD = 'previewDocument';
|
|
21
23
|
/** Templates collection: the layout field. */
|
|
22
24
|
export const TEMPLATE_LAYOUT_FIELD = 'layout';
|
|
25
|
+
/**
|
|
26
|
+
* Templates collection: an optional Payload `where` query against the document. A document
|
|
27
|
+
* without a template of its own uses the matching template with the highest priority
|
|
28
|
+
* (`TEMPLATE_PRIORITY_FIELD`), before the default template.
|
|
29
|
+
*/
|
|
30
|
+
export const TEMPLATE_CONDITIONS_FIELD = 'conditions';
|
|
31
|
+
/**
|
|
32
|
+
* Templates collection: a number that orders templates with conditions. When several match a
|
|
33
|
+
* document, the highest priority wins; on equal priority the older template wins.
|
|
34
|
+
*/
|
|
35
|
+
export const TEMPLATE_PRIORITY_FIELD = 'priority';
|
|
23
36
|
/** Template-enabled collections: the relationship to the document's own template. */
|
|
24
37
|
export const DOCUMENT_TEMPLATE_FIELD = 'template';
|
|
38
|
+
/**
|
|
39
|
+
* A template's conditions as a `where` query, or `null` when it has none: only a plain object
|
|
40
|
+
* with at least one key counts (an empty object would match every document).
|
|
41
|
+
*/
|
|
42
|
+
export function templateConditions(template) {
|
|
43
|
+
const value = template?.[TEMPLATE_CONDITIONS_FIELD];
|
|
44
|
+
return isPlainObject(value) && Object.keys(value).length > 0 ? value : null;
|
|
45
|
+
}
|
|
46
|
+
// The block and query names client components need live in a module without imports.
|
|
47
|
+
export { COLLECTION_LIST_BLOCK, PAGE_PARAM, REPEAT_BLOCK };
|
|
25
48
|
/** Virtual binding path: the frontend URL of the context document (from the link resolver). */
|
|
26
49
|
export const URL_PATH = '$url';
|
|
27
50
|
/** The Field block: shows the context document's value at `props.path`. */
|
|
28
51
|
export const FIELD_BLOCK = 'field';
|
|
29
|
-
/** The collection list block: renders its `item` slot once per loaded document. */
|
|
30
|
-
export const COLLECTION_LIST_BLOCK = 'collectionList';
|
|
31
52
|
/** Slot of the collection list block that holds the template for one item. */
|
|
32
53
|
export const LIST_ITEM_SLOT = 'item';
|
|
33
54
|
/**
|
|
@@ -37,6 +58,20 @@ export const LIST_ITEM_SLOT = 'item';
|
|
|
37
58
|
export const LIST_ITEMS_PROP = '$items';
|
|
38
59
|
/** Runtime-only prop of the Field block: the resolved value. It is never stored. */
|
|
39
60
|
export const FIELD_VALUE_PROP = 'value';
|
|
61
|
+
/** Collection list slot shown when the list has no documents. */
|
|
62
|
+
export const LIST_EMPTY_SLOT = 'empty';
|
|
63
|
+
/** Virtual binding path inside a repeat item: the row value itself (a document, an upload, a text). */
|
|
64
|
+
export const ITEM_PATH = '$item';
|
|
65
|
+
/** Virtual binding path inside a collection list or repeat item: the item number, from 1. */
|
|
66
|
+
export const INDEX_PATH = '$index';
|
|
67
|
+
/** Virtual path prefix inside a collection list or repeat item: the enclosing document (`$parent.name`). */
|
|
68
|
+
export const PARENT_PATH = '$parent';
|
|
69
|
+
/** Virtual path prefix: the page's own document (`$page.title`), also inside list and repeat items. */
|
|
70
|
+
export const PAGE_PATH = '$page';
|
|
71
|
+
/** True for the slot that repeats per item: the `item` slot of a collection list or a repeat. */
|
|
72
|
+
export function isRepeatSlot(type, slot) {
|
|
73
|
+
return slot === LIST_ITEM_SLOT && (type === COLLECTION_LIST_BLOCK || type === REPEAT_BLOCK);
|
|
74
|
+
}
|
|
40
75
|
/** A `{ relationTo, value }` pair (polymorphic relationships). */
|
|
41
76
|
function isRelationPair(value) {
|
|
42
77
|
return isPlainObject(value) && typeof value.relationTo === 'string' && 'value' in value;
|
|
@@ -73,6 +108,51 @@ export function getByPath(doc, path) {
|
|
|
73
108
|
}
|
|
74
109
|
return current;
|
|
75
110
|
}
|
|
111
|
+
const wholeNumber = (value) => typeof value === 'number' && Number.isFinite(value) && value > 0 ? Math.floor(value) : undefined;
|
|
112
|
+
/**
|
|
113
|
+
* The rows of a repeat: the value at `props.path` in the context (an array field's rows, a hasMany
|
|
114
|
+
* field's documents, IDs or values; one value counts as one row), after `offset` and `limit`.
|
|
115
|
+
* The path may start with `$parent.` or `$page.`.
|
|
116
|
+
*/
|
|
117
|
+
export function repeatRows(context, props) {
|
|
118
|
+
const path = typeof props?.path === 'string' ? props.path.trim() : '';
|
|
119
|
+
if (!path)
|
|
120
|
+
return [];
|
|
121
|
+
const value = pathValue(context, path);
|
|
122
|
+
const rows = (Array.isArray(value) ? value : [value]).filter((row) => row !== undefined && row !== null);
|
|
123
|
+
const offset = wholeNumber(props?.offset) ?? 0;
|
|
124
|
+
const limit = wholeNumber(props?.limit);
|
|
125
|
+
return rows.slice(offset, limit === undefined ? undefined : offset + limit);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The context one repeat row binds to: the row's own fields (an array row, or the related document
|
|
129
|
+
* of a relationship or upload), `$item` (the row value), `$index` (from 1), `$parent` (the
|
|
130
|
+
* enclosing document) and `$page`. `collection` is the related collection for `$url`; a
|
|
131
|
+
* `{ relationTo, value }` row names its own.
|
|
132
|
+
*/
|
|
133
|
+
export function repeatRowContext(row, index, around, collection = '') {
|
|
134
|
+
const pair = isRelationPair(row);
|
|
135
|
+
const value = pair ? row.value : row;
|
|
136
|
+
return {
|
|
137
|
+
...itemContext(around, index),
|
|
138
|
+
collection: pair ? row.relationTo : collection,
|
|
139
|
+
doc: isPlainObject(value) ? value : {},
|
|
140
|
+
item: value,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
/** The context of one collection list item (a listed document). */
|
|
144
|
+
export function listItemContext(doc, index, around, collection) {
|
|
145
|
+
return { ...itemContext(around, index), collection, doc };
|
|
146
|
+
}
|
|
147
|
+
/** `$index`, `$parent` and `$page` of an item inside `around`. */
|
|
148
|
+
function itemContext(around, index) {
|
|
149
|
+
const out = { index: index + 1 };
|
|
150
|
+
if (around) {
|
|
151
|
+
out.parent = around.doc;
|
|
152
|
+
out.page = around.page ?? around.doc;
|
|
153
|
+
}
|
|
154
|
+
return out;
|
|
155
|
+
}
|
|
76
156
|
/** True for values that count as "no value": the literal prop stays. */
|
|
77
157
|
export function isMissing(value) {
|
|
78
158
|
return value === undefined || value === null || value === '' || (Array.isArray(value) && value.length === 0);
|
|
@@ -263,22 +343,498 @@ function setByPath(props, path, value) {
|
|
|
263
343
|
const nested = isPlainObject(current) ? current : {};
|
|
264
344
|
return { ...props, [key]: setByPath(nested, rest, value) };
|
|
265
345
|
}
|
|
266
|
-
/**
|
|
267
|
-
|
|
346
|
+
/**
|
|
347
|
+
* The related collection of every relationship and upload field with one `relationTo`, at any
|
|
348
|
+
* depth of groups, named tabs and arrays (blocks fields are left out). Plain JSON: pass it to
|
|
349
|
+
* `RenderLayout` and `loadLayoutData` as `relations`. The plugin sends it to the canvas itself.
|
|
350
|
+
*/
|
|
351
|
+
export function bindingRelations(collections) {
|
|
352
|
+
const out = {};
|
|
353
|
+
for (const collection of collections) {
|
|
354
|
+
const into = {};
|
|
355
|
+
walkRelations(collection.fields, '', into);
|
|
356
|
+
if (Object.keys(into).length > 0)
|
|
357
|
+
out[collection.slug] = into;
|
|
358
|
+
}
|
|
359
|
+
return out;
|
|
360
|
+
}
|
|
361
|
+
function walkRelations(fields, prefix, into) {
|
|
362
|
+
for (const field of dataFields(fields)) {
|
|
363
|
+
const path = prefix ? `${prefix}.${field.name}` : field.name;
|
|
364
|
+
if ((field.type === 'relationship' || field.type === 'upload') && typeof field.relationTo === 'string')
|
|
365
|
+
into[path] = field.relationTo;
|
|
366
|
+
else if (field.type === 'group' || field.type === 'array')
|
|
367
|
+
walkRelations(field.fields ?? [], path, into);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
/** `<path>.$url`: the URL of the related document at `path`. */
|
|
371
|
+
const RELATED_URL_SUFFIX = `.${URL_PATH}`;
|
|
372
|
+
/**
|
|
373
|
+
* The collection of the document at `path` of the context document, by `relations`: each
|
|
374
|
+
* relationship on the way moves to its collection. Row numbers are skipped. Undefined when the
|
|
375
|
+
* path does not end on a relationship.
|
|
376
|
+
*/
|
|
377
|
+
function relationOf(collection, path, relations) {
|
|
378
|
+
let current = collection;
|
|
379
|
+
let local = '';
|
|
380
|
+
for (const part of path.split('.')) {
|
|
381
|
+
if (/^\d+$/.test(part))
|
|
382
|
+
continue;
|
|
383
|
+
local = local ? `${local}.${part}` : part;
|
|
384
|
+
const next = relations?.[current]?.[local];
|
|
385
|
+
if (next) {
|
|
386
|
+
current = next;
|
|
387
|
+
local = '';
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
return local === '' && path !== '' ? current : undefined;
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* The value at one plain path: a field of the context document, or a virtual path: `$url`,
|
|
394
|
+
* `$item` (and `$item.url`), `$index`, `$parent.<path>`, `$page.<path>`.
|
|
395
|
+
*/
|
|
396
|
+
function pathValue(context, path, options) {
|
|
268
397
|
if (path === URL_PATH)
|
|
269
398
|
return options?.url?.(context) ?? undefined;
|
|
270
|
-
|
|
399
|
+
if (path.endsWith(RELATED_URL_SUFFIX)) {
|
|
400
|
+
const target = path.slice(0, -RELATED_URL_SUFFIX.length);
|
|
401
|
+
const value = pathValue(context, target, options);
|
|
402
|
+
const pair = isRelationPair(value);
|
|
403
|
+
const doc = pair ? value.value : value;
|
|
404
|
+
// Only a loaded document has the fields a URL needs (an id alone does not).
|
|
405
|
+
const head = target.split('.')[0];
|
|
406
|
+
const own = head === PARENT_PATH || head === PAGE_PATH || head === ITEM_PATH ? undefined : relationOf(context.collection, target, options?.relations);
|
|
407
|
+
const collection = pair ? value.relationTo : own;
|
|
408
|
+
if (!collection || !isPlainObject(doc))
|
|
409
|
+
return undefined;
|
|
410
|
+
return options?.url?.({ collection, doc }) ?? undefined;
|
|
411
|
+
}
|
|
412
|
+
const dot = path.indexOf('.');
|
|
413
|
+
const head = dot < 0 ? path : path.slice(0, dot);
|
|
414
|
+
const rest = dot < 0 ? '' : path.slice(dot + 1);
|
|
415
|
+
let root;
|
|
416
|
+
switch (head) {
|
|
417
|
+
case ITEM_PATH:
|
|
418
|
+
root = context.item;
|
|
419
|
+
break;
|
|
420
|
+
case INDEX_PATH:
|
|
421
|
+
return rest ? undefined : context.index;
|
|
422
|
+
case PARENT_PATH:
|
|
423
|
+
root = context.parent;
|
|
424
|
+
break;
|
|
425
|
+
case PAGE_PATH:
|
|
426
|
+
root = context.page ?? context.doc;
|
|
427
|
+
break;
|
|
428
|
+
default:
|
|
429
|
+
return getByPath(context.doc, path);
|
|
430
|
+
}
|
|
431
|
+
return rest ? getByPath(root, rest) : root;
|
|
432
|
+
}
|
|
433
|
+
const currencyFormats = new Map();
|
|
434
|
+
/** A number from a number or a numeric text; null otherwise. */
|
|
435
|
+
function numberOf(value) {
|
|
436
|
+
if (typeof value === 'number')
|
|
437
|
+
return Number.isFinite(value) ? value : null;
|
|
438
|
+
if (typeof value !== 'string' || value.trim() === '')
|
|
439
|
+
return null;
|
|
440
|
+
const parsed = Number(value);
|
|
441
|
+
return Number.isFinite(parsed) ? parsed : null;
|
|
271
442
|
}
|
|
272
443
|
/**
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
* `props.value`. Returns the same block when nothing changes.
|
|
444
|
+
* `text` cut to `count` words or characters, with "…" when something was cut. A cut by characters
|
|
445
|
+
* ends on a whole word when that keeps at least half of them.
|
|
276
446
|
*/
|
|
277
|
-
|
|
278
|
-
|
|
447
|
+
function truncate(text, count, unit) {
|
|
448
|
+
const flat = text.replace(/\s+/g, ' ').trim();
|
|
449
|
+
if (unit === 'words') {
|
|
450
|
+
const words = flat.split(' ');
|
|
451
|
+
return words.length <= count ? flat : `${words.slice(0, count).join(' ')}…`;
|
|
452
|
+
}
|
|
453
|
+
if (flat.length <= count)
|
|
454
|
+
return flat;
|
|
455
|
+
const cut = flat.slice(0, count);
|
|
456
|
+
const space = cut.lastIndexOf(' ');
|
|
457
|
+
return `${(space >= count / 2 ? cut.slice(0, space) : cut).trimEnd()}…`;
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Formatters for binding expressions (`price|currency:EUR`). Each takes a value that is not
|
|
461
|
+
* missing and returns text, or undefined when it cannot format the value. Arguments follow the
|
|
462
|
+
* name, separated by ":". The editor's format picker lists them (`BINDING_FORMATTER_INFO`).
|
|
463
|
+
*/
|
|
464
|
+
export const BINDING_FORMATTERS = {
|
|
465
|
+
/** `date[:locale[:timeZone]]`, e.g. `date:en-GB:Europe/London`. Default `en`, UTC. */
|
|
466
|
+
date(value, [locale, timeZone]) {
|
|
467
|
+
const date = value instanceof Date ? value : typeof value === 'string' || typeof value === 'number' ? new Date(value) : null;
|
|
468
|
+
if (!date || Number.isNaN(date.getTime()))
|
|
469
|
+
return undefined;
|
|
470
|
+
try {
|
|
471
|
+
return new Intl.DateTimeFormat(locale || 'en', { dateStyle: 'medium', timeZone: timeZone || 'UTC' }).format(date);
|
|
472
|
+
}
|
|
473
|
+
catch {
|
|
474
|
+
return formatDate(date);
|
|
475
|
+
}
|
|
476
|
+
},
|
|
477
|
+
/** `currency[:code[:locale]]`, e.g. `currency:EUR` gives "€1,200". Whole amounts have no cents. */
|
|
478
|
+
currency(value, [code, locale]) {
|
|
479
|
+
const n = numberOf(value);
|
|
480
|
+
if (n === null)
|
|
481
|
+
return undefined;
|
|
482
|
+
const currency = (code || 'USD').toUpperCase();
|
|
483
|
+
const language = locale || 'en';
|
|
484
|
+
const whole = Number.isInteger(n);
|
|
485
|
+
const key = `${currency}\u0000${language}\u0000${whole}`;
|
|
486
|
+
try {
|
|
487
|
+
let format = currencyFormats.get(key);
|
|
488
|
+
if (!format) {
|
|
489
|
+
format = new Intl.NumberFormat(language, {
|
|
490
|
+
style: 'currency',
|
|
491
|
+
currency,
|
|
492
|
+
currencyDisplay: 'narrowSymbol',
|
|
493
|
+
...(whole ? { minimumFractionDigits: 0, maximumFractionDigits: 0 } : {}),
|
|
494
|
+
});
|
|
495
|
+
currencyFormats.set(key, format);
|
|
496
|
+
}
|
|
497
|
+
return format.format(n);
|
|
498
|
+
}
|
|
499
|
+
catch {
|
|
500
|
+
return undefined;
|
|
501
|
+
}
|
|
502
|
+
},
|
|
503
|
+
/** `number[:locale]`: digit grouping, e.g. "1,200". */
|
|
504
|
+
number(value, [locale]) {
|
|
505
|
+
const n = numberOf(value);
|
|
506
|
+
if (n === null)
|
|
507
|
+
return undefined;
|
|
508
|
+
try {
|
|
509
|
+
return new Intl.NumberFormat(locale || 'en').format(n);
|
|
510
|
+
}
|
|
511
|
+
catch {
|
|
512
|
+
return String(n);
|
|
513
|
+
}
|
|
514
|
+
},
|
|
515
|
+
/** `uppercase`: the plain text in capitals. */
|
|
516
|
+
uppercase(value) {
|
|
517
|
+
return toPlainText(value)?.toUpperCase();
|
|
518
|
+
},
|
|
519
|
+
/** `lowercase`: the plain text in small letters. */
|
|
520
|
+
lowercase(value) {
|
|
521
|
+
return toPlainText(value)?.toLowerCase();
|
|
522
|
+
},
|
|
523
|
+
/**
|
|
524
|
+
* `truncate:count[:words]`: the plain text (rich text too) cut to `count` characters, or to
|
|
525
|
+
* `count` words with `:words`, with "…" when something was cut. E.g. `truncate:160`, `truncate:25:words`.
|
|
526
|
+
*/
|
|
527
|
+
truncate(value, [count, unit]) {
|
|
528
|
+
const text = toPlainText(value);
|
|
529
|
+
if (text === undefined)
|
|
530
|
+
return undefined;
|
|
531
|
+
const n = numberOf(count);
|
|
532
|
+
if (n === null || n < 1)
|
|
533
|
+
return text;
|
|
534
|
+
return truncate(text, Math.floor(n), unit === 'words' ? 'words' : 'characters');
|
|
535
|
+
},
|
|
536
|
+
/**
|
|
537
|
+
* `readingTime[:wordsPerMinute[:suffix]]`: the time to read the text (rich text too), rounded up
|
|
538
|
+
* to whole minutes, at least 1. E.g. "5 min read"; `readingTime:200:Min. Lesezeit` gives
|
|
539
|
+
* "5 Min. Lesezeit". Default 200 words per minute. Undefined for a text without words.
|
|
540
|
+
*/
|
|
541
|
+
readingTime(value, [wordsPerMinute, suffix]) {
|
|
542
|
+
const words = toPlainText(value)?.match(/\S+/g)?.length ?? 0;
|
|
543
|
+
if (words === 0)
|
|
544
|
+
return undefined;
|
|
545
|
+
const rate = numberOf(wordsPerMinute);
|
|
546
|
+
const minutes = Math.max(1, Math.ceil(words / (rate !== null && rate > 0 ? rate : 200)));
|
|
547
|
+
return `${minutes} ${suffix?.trim() || 'min read'}`;
|
|
548
|
+
},
|
|
549
|
+
};
|
|
550
|
+
/** The formatters the editor offers: a label and the arguments in order. */
|
|
551
|
+
export const BINDING_FORMATTER_INFO = {
|
|
552
|
+
date: {
|
|
553
|
+
label: 'Date',
|
|
554
|
+
args: [
|
|
555
|
+
{ label: 'Language', placeholder: 'en' },
|
|
556
|
+
{ label: 'Time zone', placeholder: 'UTC' },
|
|
557
|
+
],
|
|
558
|
+
},
|
|
559
|
+
number: { label: 'Number', args: [{ label: 'Language', placeholder: 'en' }] },
|
|
560
|
+
currency: {
|
|
561
|
+
label: 'Currency',
|
|
562
|
+
args: [
|
|
563
|
+
{ label: 'Currency code', placeholder: 'USD' },
|
|
564
|
+
{ label: 'Language', placeholder: 'en' },
|
|
565
|
+
],
|
|
566
|
+
},
|
|
567
|
+
uppercase: { label: 'Uppercase', args: [] },
|
|
568
|
+
lowercase: { label: 'Lowercase', args: [] },
|
|
569
|
+
truncate: {
|
|
570
|
+
label: 'Shorten',
|
|
571
|
+
args: [
|
|
572
|
+
{ label: 'Length', placeholder: '160' },
|
|
573
|
+
{
|
|
574
|
+
label: 'Count',
|
|
575
|
+
options: [
|
|
576
|
+
{ value: '', label: 'characters' },
|
|
577
|
+
{ value: 'words', label: 'words' },
|
|
578
|
+
],
|
|
579
|
+
},
|
|
580
|
+
],
|
|
581
|
+
},
|
|
582
|
+
readingTime: {
|
|
583
|
+
label: 'Reading time',
|
|
584
|
+
args: [
|
|
585
|
+
{ label: 'Words per minute', placeholder: '200' },
|
|
586
|
+
{ label: 'Text after the number', placeholder: 'min read' },
|
|
587
|
+
],
|
|
588
|
+
},
|
|
589
|
+
};
|
|
590
|
+
/**
|
|
591
|
+
* A value through one formatter, written as in a binding: "currency:EUR", "date:en-GB:Europe/London",
|
|
592
|
+
* "number" or "truncate:160". Undefined for an unknown formatter, a missing value or a value the
|
|
593
|
+
* formatter cannot read. The field block's `format` prop uses it.
|
|
594
|
+
*/
|
|
595
|
+
export function formatBindingValue(value, format) {
|
|
596
|
+
const formatter = formatterOf(format.trim());
|
|
597
|
+
if (!formatter || isMissing(value))
|
|
598
|
+
return undefined;
|
|
599
|
+
return BINDING_FORMATTERS[formatter.name](value, formatter.args);
|
|
600
|
+
}
|
|
601
|
+
/** One `|` part read as a formatter (`name:arg:arg`). Null when the name is not a formatter. */
|
|
602
|
+
function formatterOf(part) {
|
|
603
|
+
const [name = '', ...args] = part.split(':');
|
|
604
|
+
return Object.hasOwn(BINDING_FORMATTERS, name) ? { name, args } : null;
|
|
605
|
+
}
|
|
606
|
+
/** The text of a formatter: the name and the arguments joined by ":", without trailing empty arguments. */
|
|
607
|
+
export function bindingFormatText(format) {
|
|
608
|
+
const args = format.args.map((arg) => arg.trim());
|
|
609
|
+
while (args.length > 0 && !args.at(-1))
|
|
610
|
+
args.pop();
|
|
611
|
+
return [format.name, ...args].join(':');
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* The paths and formatters of one expression. The first part is always a path. After it, a part
|
|
615
|
+
* whose name (before the first ":") is a formatter is a formatter, also when a field has that
|
|
616
|
+
* name: `title|date` formats `title` and never falls back to a field called `date` (put such a
|
|
617
|
+
* field first: `date|title`). The other parts are fallback paths. Paths after a formatter are
|
|
618
|
+
* ignored.
|
|
619
|
+
*/
|
|
620
|
+
function splitExpression(expression) {
|
|
621
|
+
const paths = [];
|
|
622
|
+
const formatters = [];
|
|
623
|
+
expression.split('|').forEach((raw, i) => {
|
|
624
|
+
const part = raw.trim();
|
|
625
|
+
if (!part)
|
|
626
|
+
return;
|
|
627
|
+
const formatter = i > 0 ? formatterOf(part) : null;
|
|
628
|
+
if (formatter)
|
|
629
|
+
formatters.push(formatter);
|
|
630
|
+
else if (formatters.length === 0)
|
|
631
|
+
paths.push(part);
|
|
632
|
+
});
|
|
633
|
+
return { paths, formatters };
|
|
634
|
+
}
|
|
635
|
+
/** One expression: the first path whose value is not missing, through the formatters in order. */
|
|
636
|
+
function expressionValue(context, expression, options) {
|
|
637
|
+
const { paths, formatters } = splitExpression(expression);
|
|
638
|
+
let value;
|
|
639
|
+
for (const path of paths) {
|
|
640
|
+
value = pathValue(context, path, options);
|
|
641
|
+
if (!isMissing(value))
|
|
642
|
+
break;
|
|
643
|
+
}
|
|
644
|
+
for (const formatter of formatters) {
|
|
645
|
+
if (isMissing(value))
|
|
646
|
+
return undefined;
|
|
647
|
+
value = BINDING_FORMATTERS[formatter.name](value, formatter.args);
|
|
648
|
+
}
|
|
649
|
+
return value;
|
|
650
|
+
}
|
|
651
|
+
export function bindingParts(binding) {
|
|
652
|
+
if (binding.includes('{{'))
|
|
653
|
+
return null;
|
|
654
|
+
const { paths, formatters } = splitExpression(binding);
|
|
655
|
+
if (paths.length === 0 || formatters.length > 1)
|
|
656
|
+
return null;
|
|
657
|
+
return { paths, format: formatters[0] ?? null };
|
|
658
|
+
}
|
|
659
|
+
/** The binding string of `BindingParts`. Trailing empty arguments are left out. */
|
|
660
|
+
export function bindingFromParts(parts) {
|
|
661
|
+
const head = parts.paths.join('|');
|
|
662
|
+
return parts.format ? `${head}|${bindingFormatText(parts.format)}` : head;
|
|
663
|
+
}
|
|
664
|
+
const TEMPLATE = /\{\{([^{}]+)\}\}/g;
|
|
665
|
+
/** True when a binding is more than one plain path: a template string, a fallback chain or a formatter. */
|
|
666
|
+
export function isBindingExpression(binding) {
|
|
667
|
+
return binding.includes('{{') || binding.includes('|');
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* The value of a binding. The syntax is fixed to these forms:
|
|
671
|
+
* - a path: `title`, `author.name`, or a virtual path (`$url`, `$item`, `$index`, `$parent.name`, `$page.title`);
|
|
672
|
+
* - a fallback chain: `subtitle|title` (the first value that is not missing);
|
|
673
|
+
* - formatters after the paths: `publishedAt|date:en-GB:Europe/London`, `price|currency:EUR`, `summary|truncate:160`
|
|
674
|
+
* (after the first part, a name that is a formatter is always a formatter, see `expressionValue`);
|
|
675
|
+
* - a template string: `Enquire about {{name}}`. Each `{{expression}}` becomes text; a missing one
|
|
676
|
+
* becomes empty. When every one is missing, the value is missing (the literal prop stays).
|
|
677
|
+
*/
|
|
678
|
+
export function bindingValue(context, binding, options) {
|
|
679
|
+
if (!binding.includes('{{'))
|
|
680
|
+
return expressionValue(context, binding, options);
|
|
681
|
+
let found = false;
|
|
682
|
+
const text = binding.replace(TEMPLATE, (_, expression) => {
|
|
683
|
+
const part = toPlainText(expressionValue(context, expression, options));
|
|
684
|
+
if (part === undefined)
|
|
685
|
+
return '';
|
|
686
|
+
found = true;
|
|
687
|
+
return part;
|
|
688
|
+
});
|
|
689
|
+
return found ? text : undefined;
|
|
690
|
+
}
|
|
691
|
+
/** The plain field paths a binding reads (no formatters, no virtual `$url`, `$item`, `$index`). */
|
|
692
|
+
export function bindingPaths(binding) {
|
|
693
|
+
const expressions = binding.includes('{{') ? [...binding.matchAll(TEMPLATE)].map((m) => m[1]) : [binding];
|
|
694
|
+
return expressions.flatMap((expression) => splitExpression(expression).paths.map(plainPath).filter(isFieldPath));
|
|
695
|
+
}
|
|
696
|
+
/** `<path>.$url` reads the field at `path`. */
|
|
697
|
+
const plainPath = (path) => (path.endsWith(RELATED_URL_SUFFIX) ? path.slice(0, -RELATED_URL_SUFFIX.length) : path);
|
|
698
|
+
/** False for virtual paths that are not fields: `$url`, `$item...`, `$index`. */
|
|
699
|
+
const isFieldPath = (path) => path !== URL_PATH && path.split('.')[0] !== ITEM_PATH && path !== INDEX_PATH;
|
|
700
|
+
/** The field paths a block's visibility conditions read (no virtual `$item`, `$index`). */
|
|
701
|
+
export function visibilityFieldPaths(visibility) {
|
|
702
|
+
return visibilityConditions(visibility)
|
|
703
|
+
.flatMap((condition) => [condition.field, ...(condition.valueField ? [condition.valueField] : [])])
|
|
704
|
+
.map(plainPath)
|
|
705
|
+
.filter(isFieldPath);
|
|
706
|
+
}
|
|
707
|
+
/** The number of items: a list's length, 0 for an empty value, 1 for any other value. */
|
|
708
|
+
function itemCount(value) {
|
|
709
|
+
if (Array.isArray(value))
|
|
710
|
+
return value.length;
|
|
711
|
+
return isMissing(value) || value === false ? 0 : 1;
|
|
712
|
+
}
|
|
713
|
+
/** True when a value matches `expected`: itself, a document's id, or any item of a list. */
|
|
714
|
+
function matches(value, expected) {
|
|
715
|
+
if (Array.isArray(value))
|
|
716
|
+
return value.some((item) => matches(item, expected));
|
|
717
|
+
if (isRelationPair(value))
|
|
718
|
+
return matches(value.value, expected);
|
|
719
|
+
if (isPlainObject(value))
|
|
720
|
+
return value.id !== undefined && String(value.id) === expected;
|
|
721
|
+
return value !== undefined && value !== null && String(value) === expected;
|
|
722
|
+
}
|
|
723
|
+
/** A number to compare: a number, a numeric text, or a date (its time). Null otherwise. */
|
|
724
|
+
function comparable(value) {
|
|
725
|
+
const number = numberOf(value);
|
|
726
|
+
if (number !== null)
|
|
727
|
+
return number;
|
|
728
|
+
if (value instanceof Date)
|
|
729
|
+
return value.getTime();
|
|
730
|
+
if (typeof value === 'string' && /^\d{4}-\d{2}-\d{2}/.test(value)) {
|
|
731
|
+
const time = Date.parse(value);
|
|
732
|
+
return Number.isNaN(time) ? null : time;
|
|
733
|
+
}
|
|
734
|
+
return null;
|
|
735
|
+
}
|
|
736
|
+
/** The values another field holds, to compare with: list items, document ids and plain values. */
|
|
737
|
+
function fieldValues(value) {
|
|
738
|
+
if (Array.isArray(value))
|
|
739
|
+
return value.flatMap(fieldValues);
|
|
740
|
+
if (isRelationPair(value))
|
|
741
|
+
return fieldValues(value.value);
|
|
742
|
+
if (isPlainObject(value))
|
|
743
|
+
return value.id === undefined || value.id === null ? [] : [String(value.id)];
|
|
744
|
+
return isMissing(value) ? [] : [String(value)];
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
* A condition with `valueField`: compares the field's value with the other field's value
|
|
748
|
+
* (`other`). Equal means one shared value or document. A missing other value never equals.
|
|
749
|
+
*/
|
|
750
|
+
function fieldConditionHolds(value, condition, other) {
|
|
751
|
+
switch (condition.operator) {
|
|
752
|
+
case 'equals':
|
|
753
|
+
case 'in':
|
|
754
|
+
return fieldValues(other).some((item) => matches(value, item));
|
|
755
|
+
case 'not_equals':
|
|
756
|
+
case 'not_in':
|
|
757
|
+
return !fieldValues(other).some((item) => matches(value, item));
|
|
758
|
+
case 'like':
|
|
759
|
+
case 'contains':
|
|
760
|
+
return textHolds(value, toPlainText(other) ?? '', condition.operator);
|
|
761
|
+
default:
|
|
762
|
+
return compareHolds(value, Array.isArray(other) ? other[0] : other, condition.operator);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
/** `contains` (any case) or `like` (every word, any case). An empty wanted text always holds. */
|
|
766
|
+
function textHolds(value, wanted, operator) {
|
|
767
|
+
const text = (toPlainText(value) ?? '').toLowerCase();
|
|
768
|
+
const words = wanted.trim().toLowerCase();
|
|
769
|
+
if (!words)
|
|
770
|
+
return true;
|
|
771
|
+
return operator === 'contains' ? text.includes(words) : words.split(/\s+/).every((word) => text.includes(word));
|
|
772
|
+
}
|
|
773
|
+
/** Greater and less compare numbers and dates only: other values never pass. */
|
|
774
|
+
function compareHolds(value, limitValue, operator) {
|
|
775
|
+
const actual = comparable(value);
|
|
776
|
+
const limit = comparable(limitValue);
|
|
777
|
+
if (actual === null || limit === null)
|
|
778
|
+
return false;
|
|
779
|
+
if (operator === 'greater_than')
|
|
780
|
+
return actual > limit;
|
|
781
|
+
if (operator === 'greater_than_equal')
|
|
782
|
+
return actual >= limit;
|
|
783
|
+
if (operator === 'less_than')
|
|
784
|
+
return actual < limit;
|
|
785
|
+
return actual <= limit;
|
|
786
|
+
}
|
|
787
|
+
/** True when one visibility condition holds for the context. `false` counts as empty. */
|
|
788
|
+
function conditionHolds(context, condition, options) {
|
|
789
|
+
const raw = pathValue(context, condition.field, options);
|
|
790
|
+
const value = condition.count ? itemCount(raw) : raw;
|
|
791
|
+
const filled = condition.count ? value !== 0 : !isMissing(value) && value !== false;
|
|
792
|
+
if (condition.valueField !== undefined && !VALUELESS_OPERATORS.has(condition.operator)) {
|
|
793
|
+
return fieldConditionHolds(value, condition, pathValue(context, condition.valueField, options));
|
|
794
|
+
}
|
|
795
|
+
const expected = condition.value;
|
|
796
|
+
switch (condition.operator) {
|
|
797
|
+
case 'is_set':
|
|
798
|
+
return filled;
|
|
799
|
+
case 'is_empty':
|
|
800
|
+
return !filled;
|
|
801
|
+
case 'equals':
|
|
802
|
+
case 'not_equals': {
|
|
803
|
+
// A checkbox without a value counts as false.
|
|
804
|
+
const actual = (value === undefined || value === null) && typeof expected === 'boolean' ? false : value;
|
|
805
|
+
return matches(actual, String(expected ?? '')) === (condition.operator === 'equals');
|
|
806
|
+
}
|
|
807
|
+
case 'in':
|
|
808
|
+
case 'not_in': {
|
|
809
|
+
const list = Array.isArray(expected) ? expected : expected === undefined ? [] : [expected];
|
|
810
|
+
return list.some((item) => matches(value, String(item))) === (condition.operator === 'in');
|
|
811
|
+
}
|
|
812
|
+
case 'like':
|
|
813
|
+
case 'contains':
|
|
814
|
+
return textHolds(value, String(expected ?? ''), condition.operator);
|
|
815
|
+
default:
|
|
816
|
+
return compareHolds(value, expected, condition.operator);
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* True when the block's visibility conditions match the context: all of them, or any entry with
|
|
821
|
+
* `match: "any"` (a group entry matches when all its conditions match). No conditions: true.
|
|
822
|
+
*/
|
|
823
|
+
export function isVisible(context, visibility, options) {
|
|
824
|
+
const groups = visibilityGroups(visibility);
|
|
825
|
+
if (groups.length === 0)
|
|
826
|
+
return true;
|
|
827
|
+
return groups.some((group) => group.every((condition) => conditionHolds(context, condition, options)));
|
|
828
|
+
}
|
|
829
|
+
/**
|
|
830
|
+
* Puts the values of `bindings` (prop path -> binding) into `props`. Bindings without a value
|
|
831
|
+
* stay in `left`, and their props keep the literal value. Never mutates.
|
|
832
|
+
*/
|
|
833
|
+
export function bindProps(props, bindings, context, definition, options) {
|
|
834
|
+
let next = props;
|
|
279
835
|
let changed = false;
|
|
280
836
|
const left = {};
|
|
281
|
-
for (const [propPath, fieldPath] of Object.entries(
|
|
837
|
+
for (const [propPath, fieldPath] of Object.entries(bindings)) {
|
|
282
838
|
const field = definition ? propField(definition.fields, propPath) : undefined;
|
|
283
839
|
const value = coerceValue(bindingValue(context, fieldPath, options), field);
|
|
284
840
|
if (value === undefined) {
|
|
@@ -286,15 +842,29 @@ export function resolveBlockBindings(block, context, definition, options) {
|
|
|
286
842
|
continue;
|
|
287
843
|
}
|
|
288
844
|
const path = propPath.split('.');
|
|
289
|
-
|
|
845
|
+
next = setByPath(next, path, value);
|
|
290
846
|
// Binding a link group's URL makes it a URL link.
|
|
291
847
|
if (path.length > 1 && path.at(-1) === 'url' && definition) {
|
|
292
848
|
const parent = propField(definition.fields, path.slice(0, -1).join('.'));
|
|
293
849
|
if (parent && isLinkField(parent))
|
|
294
|
-
|
|
850
|
+
next = setByPath(next, [...path.slice(0, -1), 'type'], 'url');
|
|
295
851
|
}
|
|
296
852
|
changed = true;
|
|
297
853
|
}
|
|
854
|
+
return { props: next, left, changed };
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Resolves one block's bindings (not its children). Bound props with a value take it; applied
|
|
858
|
+
* bindings are removed from the result, unresolved ones stay. Visibility conditions that do not
|
|
859
|
+
* match make the block `hidden`. A Field block gets `props.value`, a repeat its rows (`$items`).
|
|
860
|
+
* Returns the same block when nothing changes.
|
|
861
|
+
*/
|
|
862
|
+
export function resolveBlockBindings(block, context, definition, options) {
|
|
863
|
+
const hidden = block.visibility ? !isVisible(context, block.visibility, options) : false;
|
|
864
|
+
const bound = bindProps(block.props ?? {}, block.bindings ?? {}, context, definition, options);
|
|
865
|
+
let props = bound.props;
|
|
866
|
+
let changed = hidden || bound.changed;
|
|
867
|
+
const left = bound.left;
|
|
298
868
|
if (block.type === FIELD_BLOCK && typeof props.path === 'string' && props.path) {
|
|
299
869
|
const value = bindingValue(context, props.path, options);
|
|
300
870
|
if (!isMissing(value)) {
|
|
@@ -302,9 +872,15 @@ export function resolveBlockBindings(block, context, definition, options) {
|
|
|
302
872
|
changed = true;
|
|
303
873
|
}
|
|
304
874
|
}
|
|
875
|
+
if (block.type === REPEAT_BLOCK) {
|
|
876
|
+
props = { ...props, [LIST_ITEMS_PROP]: repeatRows(context, props) };
|
|
877
|
+
changed = true;
|
|
878
|
+
}
|
|
305
879
|
if (!changed)
|
|
306
880
|
return block;
|
|
307
881
|
const next = { ...block, props };
|
|
882
|
+
if (hidden)
|
|
883
|
+
next.hidden = true;
|
|
308
884
|
if (Object.keys(left).length > 0)
|
|
309
885
|
next.bindings = left;
|
|
310
886
|
else
|
|
@@ -312,9 +888,10 @@ export function resolveBlockBindings(block, context, definition, options) {
|
|
|
312
888
|
return next;
|
|
313
889
|
}
|
|
314
890
|
/**
|
|
315
|
-
* Resolves every block's bindings against the context document.
|
|
316
|
-
* list is left alone: its blocks bind to
|
|
317
|
-
* removed, so resolving twice
|
|
891
|
+
* Resolves every block's bindings against the context document. Repeats get their rows
|
|
892
|
+
* (`$items`). The `item` slot of a collection list or a repeat is left alone: its blocks bind to
|
|
893
|
+
* each listed document or row at render time. Applied bindings are removed, so resolving twice
|
|
894
|
+
* is safe. Never mutates.
|
|
318
895
|
*/
|
|
319
896
|
export function resolveBindings(layout, context, blocks, options) {
|
|
320
897
|
const definitions = new Map(blocks.map((d) => [d.type, d]));
|
|
@@ -324,7 +901,7 @@ export function resolveBindings(layout, context, blocks, options) {
|
|
|
324
901
|
return next;
|
|
325
902
|
const slots = Object.fromEntries(Object.entries(next.slots).map(([name, children]) => [
|
|
326
903
|
name,
|
|
327
|
-
next.type
|
|
904
|
+
isRepeatSlot(next.type, name) ? children : visit(children),
|
|
328
905
|
]));
|
|
329
906
|
return { ...next, slots };
|
|
330
907
|
});
|
|
@@ -404,23 +981,117 @@ export function bindingProblem(fields, propPath, source) {
|
|
|
404
981
|
return 'one-line text cannot show rich text';
|
|
405
982
|
return null;
|
|
406
983
|
}
|
|
984
|
+
/** `fields` with the `prefix.` taken off every path (children included). */
|
|
985
|
+
function relativeTo(fields, prefix) {
|
|
986
|
+
return fields.map((field) => {
|
|
987
|
+
const out = { ...field, path: field.path.startsWith(`${prefix}.`) ? field.path.slice(prefix.length + 1) : field.path };
|
|
988
|
+
if (field.children)
|
|
989
|
+
out.children = relativeTo(field.children, prefix);
|
|
990
|
+
return out;
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
/** `fields` under a virtual root (`$parent`, `$page`) as one group, without other virtual paths. Null when empty. */
|
|
994
|
+
function underRoot(root, label, fields) {
|
|
995
|
+
const own = (fields ?? []).filter((field) => !field.path.startsWith('$'));
|
|
996
|
+
if (own.length === 0)
|
|
997
|
+
return null;
|
|
998
|
+
const prefix = (list) => list.map((field) => {
|
|
999
|
+
const out = { ...field, path: `${root}.${field.path}` };
|
|
1000
|
+
if (field.children)
|
|
1001
|
+
out.children = prefix(field.children);
|
|
1002
|
+
return out;
|
|
1003
|
+
});
|
|
1004
|
+
return { path: root, label, type: 'group', children: prefix(own) };
|
|
1005
|
+
}
|
|
1006
|
+
/**
|
|
1007
|
+
* The bindable fields inside a list block's `item` slot. A collection list: its collection's
|
|
1008
|
+
* sources. A repeat: `$item`, `$index`, the row's fields (the related collection's sources for a
|
|
1009
|
+
* relationship or upload to one collection, with `$url` only when the repeat's `collection` names
|
|
1010
|
+
* it; else the field's children, an array row) and `$parent` (the fields around it). Both get
|
|
1011
|
+
* `$page` (the page document's `page` fields) when given. `fields` are the sources around the
|
|
1012
|
+
* block. Undefined when unknown.
|
|
1013
|
+
*/
|
|
1014
|
+
export function itemSources(block, fields, sources, page) {
|
|
1015
|
+
const pageGroup = underRoot(PAGE_PATH, 'Page', page);
|
|
1016
|
+
if (block.type === COLLECTION_LIST_BLOCK) {
|
|
1017
|
+
const collection = block.props?.collection;
|
|
1018
|
+
const listed = typeof collection === 'string' && collection ? sources[collection] : undefined;
|
|
1019
|
+
return listed ? [...listed, ...(pageGroup ? [pageGroup] : [])] : undefined;
|
|
1020
|
+
}
|
|
1021
|
+
const path = typeof block.props?.path === 'string' ? block.props.path.trim() : '';
|
|
1022
|
+
const field = path && fields ? findBindingField(fields, path) : undefined;
|
|
1023
|
+
if (!field)
|
|
1024
|
+
return undefined;
|
|
1025
|
+
const relationTo = typeof field.relationTo === 'string' ? field.relationTo : undefined;
|
|
1026
|
+
const related = relationTo
|
|
1027
|
+
? sources[relationTo]?.filter((f) => f.path !== URL_PATH || block.props?.collection === relationTo)
|
|
1028
|
+
: undefined;
|
|
1029
|
+
const { hasMany: _hasMany, children: _children, ...row } = field;
|
|
1030
|
+
const parentGroup = underRoot(PARENT_PATH, 'Enclosing document', fields);
|
|
1031
|
+
return [
|
|
1032
|
+
{ ...row, path: ITEM_PATH, label: 'This row' },
|
|
1033
|
+
{ path: INDEX_PATH, label: 'Row number', type: 'number' },
|
|
1034
|
+
...(related ?? relativeTo(field.children ?? [], field.path)),
|
|
1035
|
+
...(parentGroup ? [parentGroup] : []),
|
|
1036
|
+
...(pageGroup ? [pageGroup] : []),
|
|
1037
|
+
];
|
|
1038
|
+
}
|
|
1039
|
+
/** The collection lists and repeats whose `item` slot holds the block, outermost first. */
|
|
1040
|
+
export function repeatAncestors(layout, id) {
|
|
1041
|
+
const path = [];
|
|
1042
|
+
const visit = (blocks) => {
|
|
1043
|
+
for (const block of blocks) {
|
|
1044
|
+
if (block.id === id)
|
|
1045
|
+
return true;
|
|
1046
|
+
for (const [slot, children] of Object.entries(block.slots ?? {})) {
|
|
1047
|
+
path.push({ block, slot });
|
|
1048
|
+
if (visit(children))
|
|
1049
|
+
return true;
|
|
1050
|
+
path.pop();
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
return false;
|
|
1054
|
+
};
|
|
1055
|
+
if (!visit(layout.blocks))
|
|
1056
|
+
return [];
|
|
1057
|
+
return path.filter(({ block, slot }) => isRepeatSlot(block.type, slot)).map(({ block }) => block);
|
|
1058
|
+
}
|
|
1059
|
+
/**
|
|
1060
|
+
* The bindable fields a block reads: the template collection's sources (with a `$page` group),
|
|
1061
|
+
* narrowed by every collection list and repeat around it. Undefined when unknown (no collection, or a repeat path
|
|
1062
|
+
* missing from the sources).
|
|
1063
|
+
*/
|
|
1064
|
+
export function scopeSources(layout, id, sources, collection) {
|
|
1065
|
+
const page = collection ? sources[collection] : undefined;
|
|
1066
|
+
// `$page.<path>` reads the page's document at the top level too (the same document there).
|
|
1067
|
+
const pageGroup = underRoot(PAGE_PATH, 'Page', page);
|
|
1068
|
+
let fields = page ? [...page, ...(pageGroup ? [pageGroup] : [])] : undefined;
|
|
1069
|
+
for (const repeat of repeatAncestors(layout, id))
|
|
1070
|
+
fields = itemSources(repeat, fields, sources, page);
|
|
1071
|
+
return fields;
|
|
1072
|
+
}
|
|
407
1073
|
/**
|
|
408
1074
|
* Checks every binding against the bindable fields of the document it reads: the template's
|
|
409
|
-
* target collection,
|
|
410
|
-
*
|
|
411
|
-
* alone: the renderer keeps the literal
|
|
1075
|
+
* target collection, inside a collection list's `item` slot the listed collection, and inside a
|
|
1076
|
+
* repeat's `item` slot the row (`itemSources`). Problems have the code `binding` (they block
|
|
1077
|
+
* publishing only). Paths missing from `sources` are left alone: the renderer keeps the literal
|
|
1078
|
+
* value. `$item`, `$index` and expressions are always allowed.
|
|
412
1079
|
*/
|
|
413
1080
|
export function validateBindings(layout, blocks, sources, collection) {
|
|
414
1081
|
const definitions = new Map(blocks.map((d) => [d.type, d]));
|
|
415
1082
|
const errors = [];
|
|
416
|
-
const visit = (list, path,
|
|
1083
|
+
const visit = (list, path, fields) => {
|
|
417
1084
|
list.forEach((block, i) => {
|
|
418
1085
|
const at = `${path}[${i}]`;
|
|
419
1086
|
const definition = definitions.get(block.type);
|
|
420
|
-
const fields = scope ? sources[scope] : undefined;
|
|
421
1087
|
if (definition && fields) {
|
|
422
1088
|
for (const [propPath, fieldPath] of Object.entries(block.bindings ?? {})) {
|
|
423
|
-
|
|
1089
|
+
// Expressions (templates, fallbacks, formatters) and the row's own value are not checked.
|
|
1090
|
+
if (isBindingExpression(fieldPath))
|
|
1091
|
+
continue;
|
|
1092
|
+
if (fieldPath.split('.')[0] === ITEM_PATH || fieldPath === INDEX_PATH)
|
|
1093
|
+
continue;
|
|
1094
|
+
const source = fieldPath === URL_PATH || fieldPath.endsWith(RELATED_URL_SUFFIX) ? { path: URL_PATH, label: 'Page URL', type: 'text' } : findBindingField(fields, fieldPath);
|
|
424
1095
|
if (!source)
|
|
425
1096
|
continue;
|
|
426
1097
|
const problem = bindingProblem(definition.fields, propPath, source);
|
|
@@ -430,12 +1101,11 @@ export function validateBindings(layout, blocks, sources, collection) {
|
|
|
430
1101
|
}
|
|
431
1102
|
}
|
|
432
1103
|
for (const [name, children] of Object.entries(block.slots ?? {})) {
|
|
433
|
-
|
|
434
|
-
const next = listed ? (typeof block.props?.collection === 'string' ? block.props.collection : null) : scope;
|
|
435
|
-
visit(children, `${at}.slots.${name}`, next);
|
|
1104
|
+
visit(children, `${at}.slots.${name}`, isRepeatSlot(block.type, name) ? itemSources(block, fields, sources, page) : fields);
|
|
436
1105
|
}
|
|
437
1106
|
});
|
|
438
1107
|
};
|
|
439
|
-
|
|
1108
|
+
const page = collection ? sources[collection] : undefined;
|
|
1109
|
+
visit(layout.blocks, 'blocks', page);
|
|
440
1110
|
return errors;
|
|
441
1111
|
}
|