-
Notifications
You must be signed in to change notification settings - Fork 8
feat: CSR-176 Render custom_elements' json-render output format (optional). #534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
drubot
wants to merge
1
commit into
2.x
Choose a base branch
from
feature/CSR-176-json-render
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import type { PropType, VNode } from 'vue' | ||
| import { defineComponent, computed, h } from 'vue' | ||
| import { JSONUIProvider, Renderer } from '@json-render/vue' | ||
| import { useDrupalCe } from '../composables/useDrupalCe' | ||
| import type { JsonRenderSpec, JsonRenderElement } from '../types' | ||
|
|
||
| type JsonRenderRegistry = Record<string, unknown> | ||
|
|
||
| /** | ||
| * Renders a custom_elements json-render spec via `@json-render/vue`. | ||
| * | ||
| * Registered globally only when the `jsonRender` module option is enabled, so | ||
| * the optional `@json-render/vue` dependency stays out of the bundle | ||
| * otherwise. | ||
| * | ||
| * - Element types resolve through the module's regular custom-element | ||
| * component resolution, so the same components serve markup, JSON and | ||
| * json-render rendering. | ||
| * - `drupal-markup` elements carry inline HTML in `props.markup` and render | ||
| * through the app's `drupal-markup` component (`content` prop). | ||
| * - json-render walks `children` only; the named `slots` of an element are | ||
| * bridged by rendering each slot entry as a sub-spec rooted at it. | ||
| */ | ||
| export default defineComponent({ | ||
| name: 'DrupalCeJsonRender', | ||
| props: { | ||
| spec: { | ||
| type: Object as PropType<JsonRenderSpec>, | ||
| required: true, | ||
| }, | ||
| }, | ||
| setup(props) { | ||
| const { resolveCustomElement } = useDrupalCe() | ||
|
|
||
| const makeRegistryComponent = (component: unknown, type: string, spec: JsonRenderSpec, registry: JsonRenderRegistry) => { | ||
| const wrapper = (jsonRenderProps: { element: JsonRenderElement }, context: { slots: Record<string, (() => VNode[]) | undefined> }) => { | ||
| const element = jsonRenderProps.element | ||
| if (type === 'drupal-markup') { | ||
| return h(component as object, { content: element.props?.markup ?? '' }) | ||
| } | ||
| const slotFunctions: Record<string, () => VNode | VNode[] | undefined> = {} | ||
| if (context.slots.default) { | ||
| slotFunctions.default = context.slots.default | ||
| } | ||
| Object.entries(element.slots ?? {}).forEach(([slotName, elementKeys]) => { | ||
| slotFunctions[slotName] = () => elementKeys.map(elementKey => h(Renderer, { | ||
| key: elementKey, | ||
| spec: { ...spec, root: elementKey }, | ||
| registry, | ||
| })) | ||
| }) | ||
| return h(component as object, element.props ?? {}, slotFunctions) | ||
| } | ||
| wrapper.props = ['element', 'emit', 'on', 'bindings', 'loading'] | ||
| return wrapper | ||
| } | ||
|
|
||
| const registry = computed<JsonRenderRegistry>(() => { | ||
| const spec = props.spec | ||
| const registry: JsonRenderRegistry = {} | ||
| Object.values(spec.elements).forEach((element) => { | ||
| if (registry[element.type]) { | ||
| return | ||
| } | ||
| const component = resolveCustomElement(element.type) | ||
| if (component) { | ||
| registry[element.type] = makeRegistryComponent(component, element.type, spec, registry) | ||
| } | ||
| }) | ||
| return registry | ||
| }) | ||
|
|
||
| // The renderer requires its provider contexts; JSONUIProvider bundles | ||
| // them all with defaults. | ||
| return () => h(JSONUIProvider, { registry: registry.value }, { | ||
| default: () => h(Renderer, { spec: props.spec, registry: registry.value }), | ||
| }) | ||
| }, | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,17 @@ import type { DrupalResolvedLibrary } from './drupalLibraryLoader' | |
| import type { UseFetchOptions, AsyncData } from '#app' | ||
| import { callWithNuxt } from '#app' | ||
| import { useRuntimeConfig, useState, useFetch, navigateTo, createError, h, resolveComponent, setResponseStatus, useNuxtApp, useRequestHeaders, ref, watch, useRequestEvent, computed, useHead, toRef, useRoute, useRouter, useSlots } from '#imports' | ||
| import type { DrupalCePage, DrupalCeApiResponse } from '../../types' | ||
| import type { DrupalCePage, DrupalCeApiResponse, JsonRenderSpec } from '../../types' | ||
|
|
||
| /** | ||
| * Whether the given custom elements content is a json-render spec — the flat | ||
| * element-map format custom_elements can emit (issue #3580092) — rather than | ||
| * a nested explicit/legacy custom element object. | ||
| */ | ||
| export const isJsonRenderSpec = (content: unknown): content is JsonRenderSpec => | ||
| typeof content === 'object' && content !== null && !Array.isArray(content) | ||
| && typeof (content as JsonRenderSpec).root === 'string' | ||
| && typeof (content as JsonRenderSpec).elements === 'object' && (content as JsonRenderSpec).elements !== null | ||
|
|
||
| // Cache the dynamic import of the library loader in a single module-level | ||
| // promise. All loadLibrary() callers await the *same* promise, so their | ||
|
|
@@ -537,6 +547,19 @@ export const useDrupalCe = () => { | |
| return customElements.map(element => renderCustomElementsToVNodes(element)) | ||
| } | ||
|
|
||
| // Handle the json-render format: a flat element map plus a root reference. | ||
| if (isJsonRenderSpec(customElements)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should not have to guess here. when the setting is on, use it. |
||
| // Resolved directly by name: the component only exists when the | ||
| // `jsonRender` module option registered it, and the custom-element | ||
| // fallback resolution must not kick in for this internal component. | ||
| const component = useNuxtApp().vueApp.component('DrupalCeJsonRender') | ||
| if (component) { | ||
| return h(component, { spec: customElements }) | ||
| } | ||
| console.error('[nuxtjs-drupal-ce] Received a json-render spec, but json-render support is not enabled. Set the `jsonRender` module option and install the optional `@json-render/vue` dependency (plus its `zod` peer).') | ||
| return null | ||
| } | ||
|
|
||
| // Handle single custom element object based on configured format | ||
| if (config.customElementJsonFormat === 'explicit') { | ||
| // Verify format is explicit: check for keys that are NOT element/props/slots | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does not make sense as component really. this is the wrong tool