@nodehive/nextjs 0.5.0 → 0.5.2
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/dist/puck-editor/internal/api/media-params.js +13 -3
- package/dist/puck-editor/internal/api/media-params.js.map +1 -1
- package/dist/puck-editor/internal/content-model.d.ts +14 -0
- package/dist/puck-editor/internal/content-model.js.map +1 -1
- package/dist/puck-editor/internal/editor/use-scroll-lock-outside.d.ts +25 -2
- package/dist/puck-editor/internal/editor/use-scroll-lock-outside.js +44 -16
- package/dist/puck-editor/internal/editor/use-scroll-lock-outside.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,13 +1,23 @@
|
|
|
1
1
|
import { DrupalJsonApiParams } from 'drupal-jsonapi-params';
|
|
2
2
|
import { mediaEntityType } from "../content-model.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The sparse fieldset for one media bundle: what the editor needs, plus what
|
|
6
|
+
* the consumer declared in `media.extraFields`. Without this the fields the
|
|
7
|
+
* components read off a media item (a caption, a credit) never leave Drupal.
|
|
8
|
+
*/
|
|
9
|
+
function fieldsFor(media, type, own) {
|
|
10
|
+
const extra = media.extraFields?.[type] ?? [];
|
|
11
|
+
return extra.length ? [...new Set([...own, ...extra])] : own;
|
|
12
|
+
}
|
|
3
13
|
function addImageFields(apiParams, media) {
|
|
4
14
|
apiParams.addInclude([media.imageFileField, 'thumbnail']);
|
|
5
|
-
apiParams.addFields(mediaEntityType(media.imageType), ['name', 'thumbnail', media.imageFileField]);
|
|
15
|
+
apiParams.addFields(mediaEntityType(media.imageType), fieldsFor(media, media.imageType, ['name', 'thumbnail', media.imageFileField]));
|
|
6
16
|
apiParams.addFields('file--image', ['uri']);
|
|
7
17
|
}
|
|
8
18
|
function addRemoteVideoFields(apiParams, media) {
|
|
9
19
|
apiParams.addInclude(['thumbnail']);
|
|
10
|
-
apiParams.addFields(mediaEntityType(media.remoteVideoType), ['name', 'thumbnail', media.remoteVideoUrlField]);
|
|
20
|
+
apiParams.addFields(mediaEntityType(media.remoteVideoType), fieldsFor(media, media.remoteVideoType, ['name', 'thumbnail', media.remoteVideoUrlField]));
|
|
11
21
|
}
|
|
12
22
|
export function buildMediaParams(type, media) {
|
|
13
23
|
const apiParams = new DrupalJsonApiParams();
|
|
@@ -17,7 +27,7 @@ export function buildMediaParams(type, media) {
|
|
|
17
27
|
addRemoteVideoFields(apiParams, media);
|
|
18
28
|
} else if (type === media.videoType) {
|
|
19
29
|
apiParams.addInclude([media.imageFileField]);
|
|
20
|
-
apiParams.addFields(mediaEntityType(media.videoType), ['name', media.imageFileField, media.videoFileField]);
|
|
30
|
+
apiParams.addFields(mediaEntityType(media.videoType), fieldsFor(media, media.videoType, ['name', media.imageFileField, media.videoFileField]));
|
|
21
31
|
}
|
|
22
32
|
return apiParams;
|
|
23
33
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"media-params.js","names":["DrupalJsonApiParams","mediaEntityType","
|
|
1
|
+
{"version":3,"file":"media-params.js","names":["DrupalJsonApiParams","mediaEntityType","fieldsFor","media","type","own","extra","extraFields","length","Set","addImageFields","apiParams","addInclude","imageFileField","addFields","imageType","addRemoteVideoFields","remoteVideoType","remoteVideoUrlField","buildMediaParams","videoType","videoFileField","buildMediaListParams","query","offset","limit","addSort","addFilter","addPageOffset","Number","addPageLimit"],"sources":["../../../../src/puck-editor/internal/api/media-params.ts"],"sourcesContent":["import { DrupalJsonApiParams } from 'drupal-jsonapi-params';\n\nimport { mediaEntityType, type PuckMediaConfig } from '../content-model.ts';\n\n/**\n * The sparse fieldset for one media bundle: what the editor needs, plus what\n * the consumer declared in `media.extraFields`. Without this the fields the\n * components read off a media item (a caption, a credit) never leave Drupal.\n */\nfunction fieldsFor(\n media: PuckMediaConfig,\n type: string,\n own: string[]\n): string[] {\n const extra = media.extraFields?.[type] ?? [];\n return extra.length ? [...new Set([...own, ...extra])] : own;\n}\n\nfunction addImageFields(\n apiParams: DrupalJsonApiParams,\n media: PuckMediaConfig\n) {\n apiParams.addInclude([media.imageFileField, 'thumbnail']);\n apiParams.addFields(\n mediaEntityType(media.imageType),\n fieldsFor(media, media.imageType, [\n 'name',\n 'thumbnail',\n media.imageFileField,\n ])\n );\n apiParams.addFields('file--image', ['uri']);\n}\n\nfunction addRemoteVideoFields(\n apiParams: DrupalJsonApiParams,\n media: PuckMediaConfig\n) {\n apiParams.addInclude(['thumbnail']);\n apiParams.addFields(\n mediaEntityType(media.remoteVideoType),\n fieldsFor(media, media.remoteVideoType, [\n 'name',\n 'thumbnail',\n media.remoteVideoUrlField,\n ])\n );\n}\n\nexport function buildMediaParams(type: string, media: PuckMediaConfig) {\n const apiParams = new DrupalJsonApiParams();\n if (type === media.imageType) {\n addImageFields(apiParams, media);\n } else if (type === media.remoteVideoType) {\n addRemoteVideoFields(apiParams, media);\n } else if (type === media.videoType) {\n apiParams.addInclude([media.imageFileField]);\n apiParams.addFields(\n mediaEntityType(media.videoType),\n fieldsFor(media, media.videoType, [\n 'name',\n media.imageFileField,\n media.videoFileField,\n ])\n );\n }\n return apiParams;\n}\n\nexport function buildMediaListParams(\n type: string,\n media: PuckMediaConfig,\n query?: string,\n offset?: string,\n limit?: string\n) {\n const apiParams = new DrupalJsonApiParams().addSort('created', 'DESC');\n if (query) {\n apiParams.addFilter('name', query, 'CONTAINS');\n }\n if (offset) {\n apiParams.addPageOffset(Number(offset));\n }\n if (limit) {\n apiParams.addPageLimit(Number(limit));\n }\n\n if (type === media.imageType) {\n addImageFields(apiParams, media);\n } else if (type === media.remoteVideoType) {\n addRemoteVideoFields(apiParams, media);\n }\n return apiParams;\n}\n"],"mappings":"AAAA,SAASA,mBAAmB,QAAQ,uBAAuB;AAE3D,SAASC,eAAe,QAA8B,qBAAqB;;AAE3E;AACA;AACA;AACA;AACA;AACA,SAASC,SAASA,CAChBC,KAAsB,EACtBC,IAAY,EACZC,GAAa,EACH;EACV,MAAMC,KAAK,GAAGH,KAAK,CAACI,WAA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,IAAI,KAAKD,KAAK,CAACiB,SAAS,EAAE;IACnCT,SAAS,CAACC,UAAU,CAAC,CAACT,KAAK,CAACU,cAAc,CAAC,CAAC;IAC5CF,SAAS,CAACG,SAAS,CACjBb,eAAe,CAACE,KAAK,CAACiB,SAAS,CAAC,EAChClB,SAAS,CAACC,KAAK,EAAEA,KAAK,CAACiB,SAAS,EAAE,CAChC,MAAM,EACNjB,KAAK,CAACU,cAAc,EACpBV,KAAK,CAACkB,cAAc,CACrB,CACH,CAAC;EACH;EACA,OAAOV,SAAS;AAClB;AAEA,OAAO,SAASW,oBAAoBA,CAClClB,IAAY,EACZD,KAAsB,EACtBoB,KAAc,EACdC,MAAe,EACfC,KAAc,EACd;EACA,MAAMd,SAAS,GAAG,IAAIX,mBAAmB,CAAC,CAAC,CAAC0B,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC;EACtE,IAAIH,KAAK,EAAE;IACTZ,SAAS,CAACgB,SAAS,CAAC,MAAM,EAAEJ,KAAK,EAAE,UAAU,CAAC;EAChD;EACA,IAAIC,MAAM,EAAE;IACVb,SAAS,CAACiB,aAAa,CAACC,MAAM,CAACL,MAAM,CAAC,CAAC;EACzC;EACA,IAAIC,KAAK,EAAE;IACTd,SAAS,CAACmB,YAAY,CAACD,MAAM,CAACJ,KAAK,CAAC,CAAC;EACvC;EAEA,IAAIrB,IAAI,KAAKD,KAAK,CAACY,SAAS,EAAE;IAC5BL,cAAc,CAACC,SAAS,EAAER,KAAK,CAAC;EAClC,CAAC,MAAM,IAAIC,IAAI,KAAKD,KAAK,CAACc,eAAe,EAAE;IACzCD,oBAAoB,CAACL,SAAS,EAAER,KAAK,CAAC;EACxC;EACA,OAAOQ,SAAS;AAClB","ignoreList":[]}
|
|
@@ -50,6 +50,20 @@ export type PuckMediaConfig = {
|
|
|
50
50
|
* types (the only bundles with an implemented upload flow).
|
|
51
51
|
*/
|
|
52
52
|
uploadTypes: string[];
|
|
53
|
+
/**
|
|
54
|
+
* Extra attributes to request per media bundle slug, on top of the ones the
|
|
55
|
+
* editor needs for itself.
|
|
56
|
+
*
|
|
57
|
+
* The media queries use sparse fieldsets, so a field nobody asks for never
|
|
58
|
+
* reaches the component. Declare the ones your components read off the media
|
|
59
|
+
* item, for example a caption or a credit maintained in Drupal:
|
|
60
|
+
*
|
|
61
|
+
* media: { extraFields: { image: ['field_caption'] } }
|
|
62
|
+
*
|
|
63
|
+
* Also list them in the `fields` option of the selector field, otherwise the
|
|
64
|
+
* value is dropped again before it is stored on the block.
|
|
65
|
+
*/
|
|
66
|
+
extraFields?: Record<string, string[]>;
|
|
53
67
|
};
|
|
54
68
|
export type PuckContentModelInput = {
|
|
55
69
|
bundles?: PuckBundleConfig[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"content-model.js","names":["transformProps","mediaEntityType","slug","DEFAULT_METADATA_FIELDS","title","description","image","DEFAULT_BUNDLES","type","dataField","DEFAULT_TEMPLATES","fragmentType","categoryField","aiDescriptionField","aiInstructionsField","DEFAULT_MAX_UPLOAD_BYTES","DEFAULT_MEDIA","imageType","imageFileField","remoteVideoType","remoteVideoUrlField","videoType","videoFileField","audioType","documentType","uploadTypes","resolveMedia","input","media","humanizeType","replace","charAt","toUpperCase","slice","resolveContentModel","bundles","length","map","bundle","label","creatable","createDefaults","metadataFields","templates","maxUploadBytes","getBundle","model","find","normalizePuckData","parsed","root","props","content","zones","emptyPuckData","isValidComponent","entry","sanitizeContent","data","source","Array","isArray","filter","dropped","console","warn","applyPropTransforms","propTransforms","config","Object","keys","error","parsePuckData","raw","nodeId","JSON","parse","creatableBundles"],"sources":["../../../src/puck-editor/internal/content-model.ts"],"sourcesContent":["// Single source for everything the module assumes about the Drupal content\n// model. The same resolved model drives the server handlers (createPuckApi)\n// and the client editor (PuckEditorConfigProvider); passing different values\n// to the two sides makes publish validation drift from the editor UI.\n\nimport { transformProps } from '@puckeditor/core';\nimport type { Config, Data } from '@puckeditor/core';\n\nexport type PuckPropTransforms = Parameters<typeof transformProps>[1];\n\nexport type PuckMetadataFields = {\n /** Node field for the SEO title; false when the bundle has no such field. */\n title?: string | false;\n description?: string | false;\n image?: string | false;\n};\n\nexport type PuckBundleConfig = {\n /** JSON:API resource type, e.g. 'node--puck_page'. */\n type: string;\n /** Node field holding the serialized Puck data. */\n dataField: string;\n /** Shown in the new-page type chooser; defaults to a humanized type. */\n label?: string;\n /** Offered by the new-page action; defaults to true. */\n creatable?: boolean;\n createDefaults?: { title?: string };\n metadataFields?: PuckMetadataFields;\n};\n\nexport type PuckTemplatesConfig = {\n /** NodeHive fragment type storing editor templates. */\n fragmentType: string;\n dataField: string;\n categoryField: string;\n aiDescriptionField: string;\n aiInstructionsField: string;\n};\n\n/**\n * The Drupal media model. Bundle machine names are the slug only (the module\n * derives the JSON:API `media--<slug>` type and the `/jsonapi/media/<slug>`\n * endpoints); source field names are the raw Drupal field machine names.\n * Defaults are the NodeHive standard, so consumers with a standard backend\n * pass nothing.\n */\nexport type PuckMediaConfig = {\n imageType: string;\n imageFileField: string;\n remoteVideoType: string;\n remoteVideoUrlField: string;\n videoType: string;\n videoFileField: string;\n audioType: string;\n documentType: string;\n /**\n * Bundles the upload endpoint accepts. Defaults to the image + remote_video\n * types (the only bundles with an implemented upload flow).\n */\n uploadTypes: string[];\n};\n\nexport type PuckContentModelInput = {\n bundles?: PuckBundleConfig[];\n templates?: Partial<PuckTemplatesConfig>;\n maxUploadBytes?: number;\n media?: Partial<PuckMediaConfig>;\n};\n\nexport type ResolvedPuckBundle = {\n type: string;\n dataField: string;\n label: string;\n creatable: boolean;\n createDefaults: { title?: string };\n metadataFields: Required<PuckMetadataFields>;\n};\n\nexport type PuckContentModel = {\n bundles: ResolvedPuckBundle[];\n templates: PuckTemplatesConfig;\n maxUploadBytes: number;\n media: PuckMediaConfig;\n};\n\n/** JSON:API resource type for a media bundle slug, e.g. 'image' → 'media--image'. */\nexport function mediaEntityType(slug: string): string {\n return `media--${slug}`;\n}\n\nexport const DEFAULT_METADATA_FIELDS: Required<PuckMetadataFields> = {\n title: 'field_metadata_title',\n description: 'field_metadata_description',\n image: 'field_metadata_image',\n};\n\nconst DEFAULT_BUNDLES: PuckBundleConfig[] = [\n { type: 'node--puck_page', dataField: 'field_puck_data' },\n];\n\nconst DEFAULT_TEMPLATES: PuckTemplatesConfig = {\n fragmentType: 'puck_template',\n dataField: 'field_puck_template_data',\n categoryField: 'field_puck_template_category',\n aiDescriptionField: 'field_puck_ai_description',\n aiInstructionsField: 'field_puck_ai_instructions',\n};\n\nconst DEFAULT_MAX_UPLOAD_BYTES = 8 * 1024 * 1024;\n\nconst DEFAULT_MEDIA: PuckMediaConfig = {\n imageType: 'image',\n imageFileField: 'field_media_image',\n remoteVideoType: 'remote_video',\n remoteVideoUrlField: 'field_media_oembed_video',\n videoType: 'video',\n videoFileField: 'field_media_video_file',\n audioType: 'audio',\n documentType: 'document',\n uploadTypes: ['image', 'remote_video'],\n};\n\nfunction resolveMedia(input?: Partial<PuckMediaConfig>): PuckMediaConfig {\n const media = { ...DEFAULT_MEDIA, ...input };\n if (input?.uploadTypes == null) {\n media.uploadTypes = [media.imageType, media.remoteVideoType];\n }\n return media;\n}\n\nfunction humanizeType(type: string): string {\n const slug = type.replace(/^node--/, '').replace(/[_-]+/g, ' ');\n return slug.charAt(0).toUpperCase() + slug.slice(1);\n}\n\nexport function resolveContentModel(\n input?: PuckContentModelInput\n): PuckContentModel {\n const bundles = (\n input?.bundles?.length ? input.bundles : DEFAULT_BUNDLES\n ).map((bundle) => ({\n type: bundle.type,\n dataField: bundle.dataField,\n label: bundle.label ?? humanizeType(bundle.type),\n creatable: bundle.creatable ?? true,\n createDefaults: { title: bundle.createDefaults?.title },\n metadataFields: { ...DEFAULT_METADATA_FIELDS, ...bundle.metadataFields },\n }));\n return {\n bundles,\n templates: { ...DEFAULT_TEMPLATES, ...input?.templates },\n maxUploadBytes: input?.maxUploadBytes ?? DEFAULT_MAX_UPLOAD_BYTES,\n media: resolveMedia(input?.media),\n };\n}\n\nexport function getBundle(\n model: PuckContentModel,\n type: string | undefined\n): ResolvedPuckBundle | undefined {\n return model.bundles.find((bundle) => bundle.type === type);\n}\n\n// root.props must always exist: transformProps treats a root without props\n// as legacy data and would write root transforms to top-level root keys.\nconst normalizePuckData = (parsed: Partial<Data> | null): Data =>\n ({\n root: { props: {}, ...parsed?.root },\n content: parsed?.content ?? [],\n zones: parsed?.zones ?? {},\n }) as Data;\n\nconst emptyPuckData = (): Data => normalizePuckData(null);\n\nconst isValidComponent = (entry: unknown): boolean =>\n typeof entry === 'object' &&\n entry !== null &&\n typeof (entry as { type?: unknown }).type === 'string' &&\n typeof (entry as { props?: unknown }).props === 'object' &&\n (entry as { props?: unknown }).props !== null;\n\nfunction sanitizeContent(data: Data, source: string): Data {\n if (!Array.isArray(data.content)) return { ...data, content: [] };\n const content = data.content.filter(isValidComponent);\n const dropped = data.content.length - content.length;\n if (dropped > 0) {\n console.warn(\n `puck-editor: dropped ${dropped} malformed content ${\n dropped === 1 ? 'entry' : 'entries'\n } for ${source}`\n );\n }\n return { ...data, content };\n}\n\nexport function applyPropTransforms(\n data: Data,\n propTransforms: PuckPropTransforms | undefined,\n config: Config | undefined,\n source: string\n): Data {\n if (!propTransforms || Object.keys(propTransforms).length === 0) return data;\n if (!config) {\n console.warn(\n `puck-editor: propTransforms for ${source} were applied without a config; components nested in slots are not transformed. Pass the Puck config alongside the transforms.`\n );\n }\n try {\n return transformProps(data, propTransforms, config);\n } catch (error) {\n console.error(\n `puck-editor: propTransforms failed for ${source}; using untransformed data`,\n error\n );\n return data;\n }\n}\n\nexport function parsePuckData(\n raw: unknown,\n nodeId?: string,\n propTransforms?: PuckPropTransforms,\n config?: Config\n): Data {\n const source = `node \"${nodeId ?? 'unknown'}\"`;\n if (typeof raw !== 'string' || !raw) return emptyPuckData();\n try {\n const data = sanitizeContent(\n normalizePuckData(JSON.parse(raw) as Partial<Data> | null),\n source\n );\n return applyPropTransforms(data, propTransforms, config, source);\n } catch (error) {\n console.error(\n `puck-editor: failed to parse Puck data for ${source}`,\n error\n );\n return emptyPuckData();\n }\n}\n\nexport function creatableBundles(\n model: PuckContentModel\n): ResolvedPuckBundle[] {\n return model.bundles.filter((bundle) => bundle.creatable);\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;;AAEA,SAASA,cAAc,QAAQ,kBAAkB;;AAkCjD;AACA;AACA;AACA;AACA;AACA;AACA;;AAwCA;AACA,OAAO,SAASC,eAAeA,CAACC,IAAY,EAAU;EACpD,OAAO,UAAUA,IAAI,EAAE;AACzB;AAEA,OAAO,MAAMC,uBAAqD,GAAG;EACnEC,KAAK,EAAE,sBAAsB;EAC7BC,WAAW,EAAE,4BAA4B;EACzCC,KAAK,EAAE;AACT,CAAC;AAED,MAAMC,eAAmC,GAAG,CAC1C;EAAEC,IAAI,EAAE,iBAAiB;EAAEC,SAAS,EAAE;AAAkB,CAAC,CAC1D;AAED,MAAMC,iBAAsC,GAAG;EAC7CC,YAAY,EAAE,eAAe;EAC7BF,SAAS,EAAE,0BAA0B;EACrCG,aAAa,EAAE,8BAA8B;EAC7CC,kBAAkB,EAAE,2BAA2B;EAC/CC,mBAAmB,EAAE;AACvB,CAAC;AAED,MAAMC,wBAAwB,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI;AAEhD,MAAMC,aAA8B,GAAG;EACrCC,SAAS,EAAE,OAAO;EAClBC,cAAc,EAAE,mBAAmB;EACnCC,eAAe,EAAE,cAAc;EAC/BC,mBAAmB,EAAE,0BAA0B;EAC/CC,SAAS,EAAE,OAAO;EAClBC,cAAc,EAAE,wBAAwB;EACxCC,SAAS,EAAE,OAAO;EAClBC,YAAY,EAAE,UAAU;EACxBC,WAAW,EAAE,CAAC,OAAO,EAAE,cAAc;AACvC,CAAC;AAED,SAASC,YAAYA,CAACC,KAAgC,EAAmB;EACvE,MAAMC,KAAK,GAAG;IAAE,GAAGZ,aAAa;IAAE,GAAGW;EAAM,CAAC;EAC5C,IAAIA,KAAK,EAAEF,WAAW,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B,EACD;EACtB,OAAOA,KAAK,CAACX,OAAO,CAAC2B,MAAM,CAAExB,MAAM,IAAKA,MAAM,CAACE,SAAS,CAAC;AAC3D","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"content-model.js","names":["transformProps","mediaEntityType","slug","DEFAULT_METADATA_FIELDS","title","description","image","DEFAULT_BUNDLES","type","dataField","DEFAULT_TEMPLATES","fragmentType","categoryField","aiDescriptionField","aiInstructionsField","DEFAULT_MAX_UPLOAD_BYTES","DEFAULT_MEDIA","imageType","imageFileField","remoteVideoType","remoteVideoUrlField","videoType","videoFileField","audioType","documentType","uploadTypes","resolveMedia","input","media","humanizeType","replace","charAt","toUpperCase","slice","resolveContentModel","bundles","length","map","bundle","label","creatable","createDefaults","metadataFields","templates","maxUploadBytes","getBundle","model","find","normalizePuckData","parsed","root","props","content","zones","emptyPuckData","isValidComponent","entry","sanitizeContent","data","source","Array","isArray","filter","dropped","console","warn","applyPropTransforms","propTransforms","config","Object","keys","error","parsePuckData","raw","nodeId","JSON","parse","creatableBundles"],"sources":["../../../src/puck-editor/internal/content-model.ts"],"sourcesContent":["// Single source for everything the module assumes about the Drupal content\n// model. The same resolved model drives the server handlers (createPuckApi)\n// and the client editor (PuckEditorConfigProvider); passing different values\n// to the two sides makes publish validation drift from the editor UI.\n\nimport { transformProps } from '@puckeditor/core';\nimport type { Config, Data } from '@puckeditor/core';\n\nexport type PuckPropTransforms = Parameters<typeof transformProps>[1];\n\nexport type PuckMetadataFields = {\n /** Node field for the SEO title; false when the bundle has no such field. */\n title?: string | false;\n description?: string | false;\n image?: string | false;\n};\n\nexport type PuckBundleConfig = {\n /** JSON:API resource type, e.g. 'node--puck_page'. */\n type: string;\n /** Node field holding the serialized Puck data. */\n dataField: string;\n /** Shown in the new-page type chooser; defaults to a humanized type. */\n label?: string;\n /** Offered by the new-page action; defaults to true. */\n creatable?: boolean;\n createDefaults?: { title?: string };\n metadataFields?: PuckMetadataFields;\n};\n\nexport type PuckTemplatesConfig = {\n /** NodeHive fragment type storing editor templates. */\n fragmentType: string;\n dataField: string;\n categoryField: string;\n aiDescriptionField: string;\n aiInstructionsField: string;\n};\n\n/**\n * The Drupal media model. Bundle machine names are the slug only (the module\n * derives the JSON:API `media--<slug>` type and the `/jsonapi/media/<slug>`\n * endpoints); source field names are the raw Drupal field machine names.\n * Defaults are the NodeHive standard, so consumers with a standard backend\n * pass nothing.\n */\nexport type PuckMediaConfig = {\n imageType: string;\n imageFileField: string;\n remoteVideoType: string;\n remoteVideoUrlField: string;\n videoType: string;\n videoFileField: string;\n audioType: string;\n documentType: string;\n /**\n * Bundles the upload endpoint accepts. Defaults to the image + remote_video\n * types (the only bundles with an implemented upload flow).\n */\n uploadTypes: string[];\n /**\n * Extra attributes to request per media bundle slug, on top of the ones the\n * editor needs for itself.\n *\n * The media queries use sparse fieldsets, so a field nobody asks for never\n * reaches the component. Declare the ones your components read off the media\n * item, for example a caption or a credit maintained in Drupal:\n *\n * media: { extraFields: { image: ['field_caption'] } }\n *\n * Also list them in the `fields` option of the selector field, otherwise the\n * value is dropped again before it is stored on the block.\n */\n extraFields?: Record<string, string[]>;\n};\n\nexport type PuckContentModelInput = {\n bundles?: PuckBundleConfig[];\n templates?: Partial<PuckTemplatesConfig>;\n maxUploadBytes?: number;\n media?: Partial<PuckMediaConfig>;\n};\n\nexport type ResolvedPuckBundle = {\n type: string;\n dataField: string;\n label: string;\n creatable: boolean;\n createDefaults: { title?: string };\n metadataFields: Required<PuckMetadataFields>;\n};\n\nexport type PuckContentModel = {\n bundles: ResolvedPuckBundle[];\n templates: PuckTemplatesConfig;\n maxUploadBytes: number;\n media: PuckMediaConfig;\n};\n\n/** JSON:API resource type for a media bundle slug, e.g. 'image' → 'media--image'. */\nexport function mediaEntityType(slug: string): string {\n return `media--${slug}`;\n}\n\nexport const DEFAULT_METADATA_FIELDS: Required<PuckMetadataFields> = {\n title: 'field_metadata_title',\n description: 'field_metadata_description',\n image: 'field_metadata_image',\n};\n\nconst DEFAULT_BUNDLES: PuckBundleConfig[] = [\n { type: 'node--puck_page', dataField: 'field_puck_data' },\n];\n\nconst DEFAULT_TEMPLATES: PuckTemplatesConfig = {\n fragmentType: 'puck_template',\n dataField: 'field_puck_template_data',\n categoryField: 'field_puck_template_category',\n aiDescriptionField: 'field_puck_ai_description',\n aiInstructionsField: 'field_puck_ai_instructions',\n};\n\nconst DEFAULT_MAX_UPLOAD_BYTES = 8 * 1024 * 1024;\n\nconst DEFAULT_MEDIA: PuckMediaConfig = {\n imageType: 'image',\n imageFileField: 'field_media_image',\n remoteVideoType: 'remote_video',\n remoteVideoUrlField: 'field_media_oembed_video',\n videoType: 'video',\n videoFileField: 'field_media_video_file',\n audioType: 'audio',\n documentType: 'document',\n uploadTypes: ['image', 'remote_video'],\n};\n\nfunction resolveMedia(input?: Partial<PuckMediaConfig>): PuckMediaConfig {\n const media = { ...DEFAULT_MEDIA, ...input };\n if (input?.uploadTypes == null) {\n media.uploadTypes = [media.imageType, media.remoteVideoType];\n }\n return media;\n}\n\nfunction humanizeType(type: string): string {\n const slug = type.replace(/^node--/, '').replace(/[_-]+/g, ' ');\n return slug.charAt(0).toUpperCase() + slug.slice(1);\n}\n\nexport function resolveContentModel(\n input?: PuckContentModelInput\n): PuckContentModel {\n const bundles = (\n input?.bundles?.length ? input.bundles : DEFAULT_BUNDLES\n ).map((bundle) => ({\n type: bundle.type,\n dataField: bundle.dataField,\n label: bundle.label ?? humanizeType(bundle.type),\n creatable: bundle.creatable ?? true,\n createDefaults: { title: bundle.createDefaults?.title },\n metadataFields: { ...DEFAULT_METADATA_FIELDS, ...bundle.metadataFields },\n }));\n return {\n bundles,\n templates: { ...DEFAULT_TEMPLATES, ...input?.templates },\n maxUploadBytes: input?.maxUploadBytes ?? DEFAULT_MAX_UPLOAD_BYTES,\n media: resolveMedia(input?.media),\n };\n}\n\nexport function getBundle(\n model: PuckContentModel,\n type: string | undefined\n): ResolvedPuckBundle | undefined {\n return model.bundles.find((bundle) => bundle.type === type);\n}\n\n// root.props must always exist: transformProps treats a root without props\n// as legacy data and would write root transforms to top-level root keys.\nconst normalizePuckData = (parsed: Partial<Data> | null): Data =>\n ({\n root: { props: {}, ...parsed?.root },\n content: parsed?.content ?? [],\n zones: parsed?.zones ?? {},\n }) as Data;\n\nconst emptyPuckData = (): Data => normalizePuckData(null);\n\nconst isValidComponent = (entry: unknown): boolean =>\n typeof entry === 'object' &&\n entry !== null &&\n typeof (entry as { type?: unknown }).type === 'string' &&\n typeof (entry as { props?: unknown }).props === 'object' &&\n (entry as { props?: unknown }).props !== null;\n\nfunction sanitizeContent(data: Data, source: string): Data {\n if (!Array.isArray(data.content)) return { ...data, content: [] };\n const content = data.content.filter(isValidComponent);\n const dropped = data.content.length - content.length;\n if (dropped > 0) {\n console.warn(\n `puck-editor: dropped ${dropped} malformed content ${\n dropped === 1 ? 'entry' : 'entries'\n } for ${source}`\n );\n }\n return { ...data, content };\n}\n\nexport function applyPropTransforms(\n data: Data,\n propTransforms: PuckPropTransforms | undefined,\n config: Config | undefined,\n source: string\n): Data {\n if (!propTransforms || Object.keys(propTransforms).length === 0) return data;\n if (!config) {\n console.warn(\n `puck-editor: propTransforms for ${source} were applied without a config; components nested in slots are not transformed. Pass the Puck config alongside the transforms.`\n );\n }\n try {\n return transformProps(data, propTransforms, config);\n } catch (error) {\n console.error(\n `puck-editor: propTransforms failed for ${source}; using untransformed data`,\n error\n );\n return data;\n }\n}\n\nexport function parsePuckData(\n raw: unknown,\n nodeId?: string,\n propTransforms?: PuckPropTransforms,\n config?: Config\n): Data {\n const source = `node \"${nodeId ?? 'unknown'}\"`;\n if (typeof raw !== 'string' || !raw) return emptyPuckData();\n try {\n const data = sanitizeContent(\n normalizePuckData(JSON.parse(raw) as Partial<Data> | null),\n source\n );\n return applyPropTransforms(data, propTransforms, config, source);\n } catch (error) {\n console.error(\n `puck-editor: failed to parse Puck data for ${source}`,\n error\n );\n return emptyPuckData();\n }\n}\n\nexport function creatableBundles(\n model: PuckContentModel\n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AC;AAC3D","ignoreList":[]}
|
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a wheel event starting at `target` reaches something that can
|
|
3
|
+
* consume it, walking up to and INCLUDING `allow`.
|
|
4
|
+
*
|
|
5
|
+
* Including `allow` is the point: a dialog carries the overflow on its own
|
|
6
|
+
* root, and everything inside it (grids, tiles, images) cannot scroll. A walk
|
|
7
|
+
* that stops one element short never asks the one element that could, and the
|
|
8
|
+
* event gets cancelled although the dialog had room left.
|
|
9
|
+
*
|
|
10
|
+
* Kept free of the DOM so it can be tested without one.
|
|
11
|
+
*/
|
|
12
|
+
export declare function wheelIsConsumable<T>(target: T | null, allow: T | null, parentOf: (el: T) => T | null, canConsume: (el: T) => boolean): boolean;
|
|
1
13
|
/**
|
|
2
14
|
* Locks the document's background scroll while `active` is true, without
|
|
3
15
|
* touching <html>/<body> styles — so libraries that mirror those attributes
|
|
@@ -5,10 +17,21 @@
|
|
|
5
17
|
* iframes' own scroll.
|
|
6
18
|
*
|
|
7
19
|
* Works by intercepting wheel and touchmove on the document and only
|
|
8
|
-
* letting events through when a scrollable
|
|
9
|
-
*
|
|
20
|
+
* letting events through when a scrollable element between the target and the
|
|
21
|
+
* allowed container can consume them. Events anywhere else get
|
|
10
22
|
* preventDefault'd, so scroll can't chain to the body behind the overlay.
|
|
11
23
|
*
|
|
24
|
+
* Dialogs count as allowed on top of `allowSelector`: a Radix dialog is
|
|
25
|
+
* portaled to `document.body` and therefore sits outside the overlay, so
|
|
26
|
+
* matching on the selector alone would classify every wheel event inside the
|
|
27
|
+
* media picker as "outside" and cancel it. The picker is then unusable on any
|
|
28
|
+
* window short enough for its list to overflow.
|
|
29
|
+
*
|
|
30
|
+
* The walk includes the allowed container itself. A dialog carries the
|
|
31
|
+
* overflow on its own root, and everything inside it (grids, tiles, images)
|
|
32
|
+
* cannot scroll; stopping one element short meant never asking the one element
|
|
33
|
+
* that could.
|
|
34
|
+
*
|
|
12
35
|
* Skips in iframes so the preview document isn't affected.
|
|
13
36
|
*/
|
|
14
37
|
export declare function useScrollLockOutside(active: boolean, allowSelector: string): void;
|
|
@@ -2,6 +2,29 @@
|
|
|
2
2
|
|
|
3
3
|
import { c as _c } from "react/compiler-runtime";
|
|
4
4
|
import { useEffect } from 'react';
|
|
5
|
+
const DIALOG_SELECTOR = '[role="dialog"]';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Whether a wheel event starting at `target` reaches something that can
|
|
9
|
+
* consume it, walking up to and INCLUDING `allow`.
|
|
10
|
+
*
|
|
11
|
+
* Including `allow` is the point: a dialog carries the overflow on its own
|
|
12
|
+
* root, and everything inside it (grids, tiles, images) cannot scroll. A walk
|
|
13
|
+
* that stops one element short never asks the one element that could, and the
|
|
14
|
+
* event gets cancelled although the dialog had room left.
|
|
15
|
+
*
|
|
16
|
+
* Kept free of the DOM so it can be tested without one.
|
|
17
|
+
*/
|
|
18
|
+
export function wheelIsConsumable(target, allow, parentOf, canConsume) {
|
|
19
|
+
if (!allow) return false;
|
|
20
|
+
let el = target;
|
|
21
|
+
while (el) {
|
|
22
|
+
if (canConsume(el)) return true;
|
|
23
|
+
if (el === allow) break;
|
|
24
|
+
el = parentOf(el);
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
5
28
|
|
|
6
29
|
/**
|
|
7
30
|
* Locks the document's background scroll while `active` is true, without
|
|
@@ -10,10 +33,21 @@ import { useEffect } from 'react';
|
|
|
10
33
|
* iframes' own scroll.
|
|
11
34
|
*
|
|
12
35
|
* Works by intercepting wheel and touchmove on the document and only
|
|
13
|
-
* letting events through when a scrollable
|
|
14
|
-
*
|
|
36
|
+
* letting events through when a scrollable element between the target and the
|
|
37
|
+
* allowed container can consume them. Events anywhere else get
|
|
15
38
|
* preventDefault'd, so scroll can't chain to the body behind the overlay.
|
|
16
39
|
*
|
|
40
|
+
* Dialogs count as allowed on top of `allowSelector`: a Radix dialog is
|
|
41
|
+
* portaled to `document.body` and therefore sits outside the overlay, so
|
|
42
|
+
* matching on the selector alone would classify every wheel event inside the
|
|
43
|
+
* media picker as "outside" and cancel it. The picker is then unusable on any
|
|
44
|
+
* window short enough for its list to overflow.
|
|
45
|
+
*
|
|
46
|
+
* The walk includes the allowed container itself. A dialog carries the
|
|
47
|
+
* overflow on its own root, and everything inside it (grids, tiles, images)
|
|
48
|
+
* cannot scroll; stopping one element short meant never asking the one element
|
|
49
|
+
* that could.
|
|
50
|
+
*
|
|
17
51
|
* Skips in iframes so the preview document isn't affected.
|
|
18
52
|
*/
|
|
19
53
|
export function useScrollLockOutside(active, allowSelector) {
|
|
@@ -29,25 +63,16 @@ export function useScrollLockOutside(active, allowSelector) {
|
|
|
29
63
|
return;
|
|
30
64
|
}
|
|
31
65
|
const canConsumeScroll = _temp;
|
|
66
|
+
const isAllowed = target => target?.closest?.(`${allowSelector}, ${DIALOG_SELECTOR}`) ?? null;
|
|
32
67
|
const onWheel = e => {
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
if (!
|
|
68
|
+
const target_0 = e.target;
|
|
69
|
+
const consumable = wheelIsConsumable(target_0, isAllowed(target_0), _temp2, el_1 => canConsumeScroll(el_1, e.deltaY));
|
|
70
|
+
if (!consumable) {
|
|
36
71
|
e.preventDefault();
|
|
37
|
-
return;
|
|
38
72
|
}
|
|
39
|
-
let el_0 = target;
|
|
40
|
-
while (el_0 && el_0 !== allow) {
|
|
41
|
-
if (canConsumeScroll(el_0, e.deltaY)) {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
el_0 = el_0.parentElement;
|
|
45
|
-
}
|
|
46
|
-
e.preventDefault();
|
|
47
73
|
};
|
|
48
74
|
const onTouchMove = e_0 => {
|
|
49
|
-
|
|
50
|
-
if (!target_0?.closest?.(allowSelector)) {
|
|
75
|
+
if (!isAllowed(e_0.target)) {
|
|
51
76
|
e_0.preventDefault();
|
|
52
77
|
}
|
|
53
78
|
};
|
|
@@ -73,6 +98,9 @@ export function useScrollLockOutside(active, allowSelector) {
|
|
|
73
98
|
}
|
|
74
99
|
useEffect(t0, t1);
|
|
75
100
|
}
|
|
101
|
+
function _temp2(el_0) {
|
|
102
|
+
return el_0.parentElement;
|
|
103
|
+
}
|
|
76
104
|
function _temp(el, deltaY) {
|
|
77
105
|
const style = getComputedStyle(el);
|
|
78
106
|
if (!/auto|scroll|overlay/.test(style.overflowY)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-scroll-lock-outside.js","names":["c","_c","useEffect","useScrollLockOutside","active","allowSelector","$","t0","t1","window","self","top","canConsumeScroll","_temp","
|
|
1
|
+
{"version":3,"file":"use-scroll-lock-outside.js","names":["c","_c","useEffect","DIALOG_SELECTOR","wheelIsConsumable","target","allow","parentOf","canConsume","el","useScrollLockOutside","active","allowSelector","$","t0","t1","window","self","top","canConsumeScroll","_temp","isAllowed","closest","onWheel","e","target_0","consumable","_temp2","el_1","deltaY","preventDefault","onTouchMove","e_0","document","addEventListener","passive","removeEventListener","el_0","parentElement","style","getComputedStyle","test","overflowY","he","scrollHeight","clientHeight","scrollTop"],"sources":["../../../../src/puck-editor/internal/editor/use-scroll-lock-outside.ts"],"sourcesContent":["'use client';\n\nimport { useEffect } from 'react';\n\nconst DIALOG_SELECTOR = '[role=\"dialog\"]';\n\n/**\n * Whether a wheel event starting at `target` reaches something that can\n * consume it, walking up to and INCLUDING `allow`.\n *\n * Including `allow` is the point: a dialog carries the overflow on its own\n * root, and everything inside it (grids, tiles, images) cannot scroll. A walk\n * that stops one element short never asks the one element that could, and the\n * event gets cancelled although the dialog had room left.\n *\n * Kept free of the DOM so it can be tested without one.\n */\nexport function wheelIsConsumable<T>(\n target: T | null,\n allow: T | null,\n parentOf: (el: T) => T | null,\n canConsume: (el: T) => boolean\n): boolean {\n if (!allow) return false;\n let el = target;\n while (el) {\n if (canConsume(el)) return true;\n if (el === allow) break;\n el = parentOf(el);\n }\n return false;\n}\n\n/**\n * Locks the document's background scroll while `active` is true, without\n * touching <html>/<body> styles — so libraries that mirror those attributes\n * into child iframes (e.g. Puck's CopyHostStyles) don't freeze those\n * iframes' own scroll.\n *\n * Works by intercepting wheel and touchmove on the document and only\n * letting events through when a scrollable element between the target and the\n * allowed container can consume them. Events anywhere else get\n * preventDefault'd, so scroll can't chain to the body behind the overlay.\n *\n * Dialogs count as allowed on top of `allowSelector`: a Radix dialog is\n * portaled to `document.body` and therefore sits outside the overlay, so\n * matching on the selector alone would classify every wheel event inside the\n * media picker as \"outside\" and cancel it. The picker is then unusable on any\n * window short enough for its list to overflow.\n *\n * The walk includes the allowed container itself. A dialog carries the\n * overflow on its own root, and everything inside it (grids, tiles, images)\n * cannot scroll; stopping one element short meant never asking the one element\n * that could.\n *\n * Skips in iframes so the preview document isn't affected.\n */\nexport function useScrollLockOutside(\n active: boolean,\n allowSelector: string\n): void {\n useEffect(() => {\n if (!active) return;\n if (typeof window !== 'undefined' && window.self !== window.top) return;\n\n const canConsumeScroll = (el: Element, deltaY: number): boolean => {\n const style = getComputedStyle(el);\n if (!/auto|scroll|overlay/.test(style.overflowY)) return false;\n const he = el as HTMLElement;\n if (he.scrollHeight <= he.clientHeight) return false;\n if (deltaY < 0 && he.scrollTop <= 0) return false;\n if (deltaY > 0 && he.scrollTop + he.clientHeight >= he.scrollHeight)\n return false;\n return true;\n };\n\n const isAllowed = (target: Element | null): Element | null =>\n target?.closest?.(`${allowSelector}, ${DIALOG_SELECTOR}`) ?? null;\n\n const onWheel = (e: WheelEvent) => {\n const target = e.target as Element | null;\n const consumable = wheelIsConsumable(\n target,\n isAllowed(target),\n (el) => el.parentElement,\n (el) => canConsumeScroll(el, e.deltaY)\n );\n if (!consumable) {\n e.preventDefault();\n }\n };\n\n const onTouchMove = (e: TouchEvent) => {\n if (!isAllowed(e.target as Element | null)) {\n e.preventDefault();\n }\n };\n\n document.addEventListener('wheel', onWheel, { passive: false });\n document.addEventListener('touchmove', onTouchMove, { passive: false });\n return () => {\n document.removeEventListener('wheel', onWheel);\n document.removeEventListener('touchmove', onTouchMove);\n };\n }, [active, allowSelector]);\n}\n"],"mappings":"AAAA,YAAY;;AAAC,SAAAA,CAAA,IAAAC,EAAA;AAEb,SAASC,SAAS,QAAQ,OAAO;AAEjC,MAAMC,eAAe,GAAG,iBAAiB;;AAEzC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,iBAAiBA,CAC/BC,MAAgB,EAChBC,KAAe,EACfC,QAA6B,EAC7BC,UAA8B,EACrB;EACT,IAAI,CAACF,KAAK,EAAE,OAAO,KAAK;EACxB,IAAIG,EAAE,GAAGJ,MAAM;EACf,OAAOI,EAAE,EAAE;IACT,IAAID,UAAU,CAACC,EAAE,CAAC,EAAE,OAAO,IAAI;IAC/B,IAAIA,EAAE,KAAKH,KAAK,EAAE;IAClBG,EAAE,GAAGF,QAAQ,CAACE,EAAE,CAAC;EACnB;EACA,OAAO,KAAK;AACd;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAAAC,qBAAAC,MAAA,EAAAC,aAAA;EAAA,MAAAC,CAAA,GAAAZ,EAAA;EAAA,IAAAa,EAAA;EAAA,IAAAC,EAAA;EAAA,IAAAF,CAAA,QAAAF,MAAA,IAAAE,CAAA,QAAAD,aAAA;IAIKE,EAAA,GAAAA,CAAA;MACR,IAAI,CAACH,MAAM;QAAA;MAAA;MACX,IAAI,OAAOK,MAAM,KAAK,WAAyC,IAA1BA,MAAM,CAAAC,IAAK,KAAKD,MAAM,CAAAE,GAAI;QAAA;MAAA;MAE/D,MAAAC,gBAAA,GAAyBC,KASxB;MAED,MAAAC,SAAA,GAAkBhB,MAAA,IAChBA,MAAM,EAAAiB,OAAmD,GAAvC,GAAGV,aAAa,KAAKT,eAAe,EAAU,CAAC,IAAjE,IAAiE;MAEnE,MAAAoB,OAAA,GAAgB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