@slicemachine/adapter-next 0.0.1

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/index.cjs ADDED
@@ -0,0 +1,699 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const pluginKit = require('@slicemachine/plugin-kit');
6
+ const prismicTsCodegen = require('prismic-ts-codegen');
7
+ const commonTags = require('common-tags');
8
+ const fs = require('node:fs/promises');
9
+ const path = require('node:path');
10
+ const pascalCase$1 = require('pascal-case');
11
+ const node_module = require('node:module');
12
+ const http = require('node:http');
13
+ const React = require('react');
14
+ const sliceSimulatorCore = require('@prismicio/slice-simulator-core');
15
+
16
+ function _interopNamespace(e) {
17
+ if (e && e.__esModule) return e;
18
+ const n = Object.create(null);
19
+ if (e) {
20
+ for (const k in e) {
21
+ if (k !== 'default') {
22
+ const d = Object.getOwnPropertyDescriptor(e, k);
23
+ Object.defineProperty(n, k, d.get ? d : {
24
+ enumerable: true,
25
+ get: function () { return e[k]; }
26
+ });
27
+ }
28
+ }
29
+ }
30
+ n["default"] = e;
31
+ return Object.freeze(n);
32
+ }
33
+
34
+ const fs__namespace = /*#__PURE__*/_interopNamespace(fs);
35
+ const path__namespace = /*#__PURE__*/_interopNamespace(path);
36
+ const http__namespace = /*#__PURE__*/_interopNamespace(http);
37
+ const React__namespace = /*#__PURE__*/_interopNamespace(React);
38
+
39
+ var name = "@slicemachine/adapter-next";
40
+
41
+ const pascalCase = (...input) => {
42
+ return pascalCase$1.pascalCase(input.filter(Boolean).join(" "));
43
+ };
44
+
45
+ const buildSliceLibraryIndexFileContents = async (args) => {
46
+ const filePath = args.helpers.joinPathFromRoot(args.libraryID, args.options.typescript ? "index.ts" : "index.js");
47
+ const sliceLibrary = await args.actions.readSliceLibrary({
48
+ libraryID: args.libraryID
49
+ });
50
+ let contents = commonTags.stripIndent`
51
+ import dynamic from 'next/dynamic'
52
+
53
+ export const components = {
54
+ ${sliceLibrary.sliceIDs.map((id) => `${id}: dynamic(() => import('./${pascalCase(id)}')),`)}
55
+ }
56
+ `;
57
+ if (args.options.format) {
58
+ contents = await args.helpers.format(contents, filePath);
59
+ }
60
+ return { filePath, contents };
61
+ };
62
+
63
+ const getJSOrTSXFileExtension = (pluginOptions) => {
64
+ if (pluginOptions.typescript) {
65
+ return "tsx";
66
+ } else if (pluginOptions.jsxExtension) {
67
+ return "jsx";
68
+ } else {
69
+ return "js";
70
+ }
71
+ };
72
+
73
+ const createModelFile$1 = async ({ dir, data, helpers, options }) => {
74
+ const filePath = path__namespace.join(dir, "model.json");
75
+ let contents = JSON.stringify(data.model);
76
+ if (options.format) {
77
+ contents = await helpers.format(contents, filePath);
78
+ }
79
+ await fs__namespace.writeFile(filePath, contents);
80
+ };
81
+ const createComponentFile = async ({ dir, data, helpers, options }) => {
82
+ const filePath = path__namespace.join(dir, `index.${getJSOrTSXFileExtension(options)}`);
83
+ const model = data.model;
84
+ const pascalID = pascalCase(model.id);
85
+ let contents;
86
+ if (options.typescript) {
87
+ contents = commonTags.stripIndent`
88
+ import { SliceComponentProps } from "@prismicio/react";
89
+ import { ${pascalID}Slice } from "./types";
90
+
91
+ /**
92
+ * Props for \`${pascalID}\`.
93
+ */
94
+ export type ${pascalID}Props = SliceComponentProps<${pascalID}Slice>;
95
+
96
+ /**
97
+ * Component for "${model.name}" Slices.
98
+ */
99
+ const ${pascalID} = ({ slice }: ${pascalID}Props): React.Element => {
100
+ return (
101
+ <section
102
+ data-slice-type={slice.slice_type}
103
+ data-slice-variation={slice.variation}
104
+ >
105
+ Placeholder component for ${model.id} (variation: {slice.variation}) Slices
106
+ </section>
107
+ );
108
+ };
109
+
110
+ export default ${pascalID}
111
+ `;
112
+ } else {
113
+ contents = commonTags.stripIndent`
114
+ /**
115
+ * @typedef {import("./types").${pascalID}Slice} ${pascalID}Slice
116
+ * @typedef {import("@prismicio/react").SliceComponentProps<${pascalID}Slice>} ${pascalID}Props
117
+ * @param {${pascalID}Props}
118
+ */
119
+ const ${pascalID} = ({ slice }) => {
120
+ return (
121
+ <section
122
+ data-slice-type={slice.slice_type}
123
+ data-slice-variation={slice.variation}
124
+ >
125
+ Placeholder component for ${model.id} (variation: {slice.variation}) Slices
126
+ </section>
127
+ );
128
+ };
129
+
130
+ export default ${pascalID};
131
+ `;
132
+ }
133
+ if (options.format) {
134
+ contents = await helpers.format(contents, filePath);
135
+ }
136
+ await fs__namespace.writeFile(filePath, contents);
137
+ };
138
+ const createTypesFile$1 = async ({ dir, data, helpers, options }) => {
139
+ const filePath = path__namespace.join(dir, "types.ts");
140
+ let contents = prismicTsCodegen.generateTypes({
141
+ sharedSliceModels: [data.model]
142
+ });
143
+ if (options.format) {
144
+ contents = await helpers.format(contents, filePath);
145
+ }
146
+ await fs__namespace.writeFile(filePath, contents);
147
+ };
148
+ const upsertSliceLibraryIndexFile = async ({
149
+ data,
150
+ actions,
151
+ helpers,
152
+ project,
153
+ options
154
+ }) => {
155
+ const { filePath, contents } = await buildSliceLibraryIndexFileContents({
156
+ libraryID: data.libraryID,
157
+ actions,
158
+ helpers,
159
+ project,
160
+ options
161
+ });
162
+ await fs__namespace.writeFile(filePath, contents);
163
+ };
164
+ const sliceCreate = async (data, context) => {
165
+ const dir = context.helpers.joinPathFromRoot(data.libraryID, pascalCase(data.model.id));
166
+ await fs__namespace.mkdir(dir, { recursive: true });
167
+ await Promise.allSettled([
168
+ createModelFile$1({ dir, data, ...context }),
169
+ createComponentFile({ dir, data, ...context }),
170
+ createTypesFile$1({ dir, data, ...context })
171
+ ]);
172
+ await upsertSliceLibraryIndexFile({ data, ...context });
173
+ };
174
+
175
+ const updateModelFile = async ({ dir, data, helpers, options }) => {
176
+ const filePath = path__namespace.join(dir, "model.json");
177
+ let contents = JSON.stringify(data.model);
178
+ if (options.format) {
179
+ contents = await helpers.format(contents, filePath);
180
+ }
181
+ await fs__namespace.writeFile(filePath, contents);
182
+ };
183
+ const updateTypesFile = async ({ dir, data, helpers, options }) => {
184
+ const filePath = path__namespace.join(dir, "types.ts");
185
+ let contents = prismicTsCodegen.generateTypes({
186
+ sharedSliceModels: [data.model]
187
+ });
188
+ if (options.format) {
189
+ contents = await helpers.format(contents, filePath);
190
+ }
191
+ await fs__namespace.writeFile(filePath, contents);
192
+ };
193
+ const sliceUpdate = async (data, context) => {
194
+ const dir = context.helpers.joinPathFromRoot(data.libraryID, pascalCase(data.model.id));
195
+ await Promise.allSettled([
196
+ updateModelFile({ dir, data, ...context }),
197
+ updateTypesFile({ dir, data, ...context })
198
+ ]);
199
+ };
200
+
201
+ const deleteSliceDir = async ({ data, helpers }) => {
202
+ const dir = helpers.joinPathFromRoot(data.libraryID, pascalCase(data.model.id));
203
+ await fs__namespace.rm(dir, { recursive: true });
204
+ };
205
+ const updateSliceLibraryIndexFile = async ({
206
+ data,
207
+ actions,
208
+ helpers,
209
+ project,
210
+ options
211
+ }) => {
212
+ const { filePath, contents } = await buildSliceLibraryIndexFileContents({
213
+ libraryID: data.libraryID,
214
+ actions,
215
+ helpers,
216
+ project,
217
+ options
218
+ });
219
+ await fs__namespace.writeFile(filePath, contents);
220
+ };
221
+ const sliceDelete = async (data, context) => {
222
+ await deleteSliceDir({ data, ...context });
223
+ await updateSliceLibraryIndexFile({ data, ...context });
224
+ };
225
+
226
+ const readJSONFile = async (path) => {
227
+ const contents = await fs__namespace.readFile(path, "utf8");
228
+ return JSON.parse(contents);
229
+ };
230
+
231
+ const sliceRead = async (data, { helpers }) => {
232
+ const filePath = helpers.joinPathFromRoot(data.libraryID, pascalCase(data.sliceID), "model.json");
233
+ return await readJSONFile(filePath);
234
+ };
235
+
236
+ const CustomTypeModelFieldType = {
237
+ Boolean: "Boolean",
238
+ Color: "Color",
239
+ Date: "Date",
240
+ Embed: "Embed",
241
+ GeoPoint: "GeoPoint",
242
+ Group: "Group",
243
+ Image: "Image",
244
+ IntegrationFields: "IntegrationFields",
245
+ Link: "Link",
246
+ Number: "Number",
247
+ Select: "Select",
248
+ Slices: "Slices",
249
+ StructuredText: "StructuredText",
250
+ Text: "Text",
251
+ Timestamp: "Timestamp",
252
+ UID: "UID",
253
+ Range: "Range",
254
+ Separator: "Separator",
255
+ LegacySlices: "Choice"
256
+ };
257
+ const CustomTypeModelSliceType = {
258
+ Slice: "Slice",
259
+ SharedSlice: "SharedSlice"
260
+ };
261
+
262
+ const isSharedSliceModel = (input) => {
263
+ return typeof input === "object" && input !== null && "type" in input && input.type === CustomTypeModelSliceType.SharedSlice;
264
+ };
265
+ const sliceLibraryRead = async (data, { helpers }) => {
266
+ const dirPath = helpers.joinPathFromRoot(data.libraryID);
267
+ const childDirs = await fs__namespace.readdir(dirPath);
268
+ const sliceIDs = [];
269
+ await Promise.all(childDirs.map(async (childDir) => {
270
+ const modelPath = path__namespace.join(dirPath, childDir, "model.json");
271
+ try {
272
+ const modelContents = await readJSONFile(modelPath);
273
+ if (isSharedSliceModel(modelContents)) {
274
+ sliceIDs.push(modelContents.id);
275
+ }
276
+ } catch (e) {
277
+ }
278
+ }));
279
+ return {
280
+ id: data.libraryID,
281
+ sliceIDs: sliceIDs.sort()
282
+ };
283
+ };
284
+
285
+ const createModelFile = async ({ dir, data, helpers, options }) => {
286
+ const filePath = path__namespace.join(dir, "index.json");
287
+ let contents = JSON.stringify(data.model);
288
+ if (options.format) {
289
+ contents = await helpers.format(contents, filePath);
290
+ }
291
+ await fs__namespace.writeFile(filePath, contents);
292
+ };
293
+ const createTypesFile = async ({ dir, data, helpers, options }) => {
294
+ const filePath = path__namespace.join(dir, "types.ts");
295
+ let contents = prismicTsCodegen.generateTypes({
296
+ customTypeModels: [data.model]
297
+ });
298
+ if (options.format) {
299
+ contents = await helpers.format(contents, filePath);
300
+ }
301
+ await fs__namespace.writeFile(filePath, contents);
302
+ };
303
+ const customTypeCreate = async (data, context) => {
304
+ const dir = context.helpers.joinPathFromRoot("customtypes", data.model.id);
305
+ await fs__namespace.mkdir(dir, { recursive: true });
306
+ await Promise.allSettled([
307
+ createModelFile({ dir, data, ...context }),
308
+ createTypesFile({ dir, data, ...context })
309
+ ]);
310
+ };
311
+
312
+ const customTypeUpdate = customTypeCreate;
313
+
314
+ const customTypeDelete = async (data, { helpers }) => {
315
+ const dir = helpers.joinPathFromRoot("customtypes", data.model.id);
316
+ await fs__namespace.rm(dir, { recursive: true });
317
+ };
318
+
319
+ const customTypeRead = async (data, { helpers }) => {
320
+ const filePath = helpers.joinPathFromRoot("customtypes", data.id, "index.json");
321
+ return await readJSONFile(filePath);
322
+ };
323
+
324
+ const isCustomTypeModel = (input) => {
325
+ return typeof input === "object" && input !== null && "json" in input;
326
+ };
327
+ const customTypeLibraryRead = async (_data, { helpers }) => {
328
+ const dirPath = helpers.joinPathFromRoot("customtypes");
329
+ const childDirs = await fs__namespace.readdir(dirPath);
330
+ const ids = [];
331
+ await Promise.all(childDirs.map(async (childDir) => {
332
+ const modelPath = path__namespace.join(dirPath, childDir, "index.json");
333
+ const modelContents = await readJSONFile(modelPath);
334
+ if (isCustomTypeModel(modelContents)) {
335
+ ids.push(modelContents.id);
336
+ }
337
+ }));
338
+ return {
339
+ ids: ids.sort()
340
+ };
341
+ };
342
+
343
+ const prettierOptions = { parser: "typescript" };
344
+ const dotPath = (segments) => {
345
+ return segments.join(".");
346
+ };
347
+ const snippetRead = async (data, { helpers }) => {
348
+ const { fieldPath } = data;
349
+ const label = "React";
350
+ switch (data.model.type) {
351
+ case CustomTypeModelFieldType.Link: {
352
+ return {
353
+ label,
354
+ language: "tsx",
355
+ code: await helpers.format(commonTags.stripIndent`
356
+ <PrismicLink field={${dotPath(fieldPath)}}>Link</PrismicLink>
357
+ `, void 0, { prettier: prettierOptions })
358
+ };
359
+ }
360
+ case CustomTypeModelFieldType.Image: {
361
+ return [
362
+ {
363
+ label: `${label} (next/image)`,
364
+ language: "tsx",
365
+ code: await helpers.format(commonTags.stripIndent`
366
+ <PrismicNextImage field={${dotPath(fieldPath)}} />
367
+ `, void 0, { prettier: prettierOptions })
368
+ },
369
+ {
370
+ label,
371
+ language: "tsx",
372
+ code: await helpers.format(commonTags.stripIndent`
373
+ <PrismicImage field={${dotPath(fieldPath)}} />
374
+ `, void 0, { prettier: prettierOptions })
375
+ }
376
+ ];
377
+ }
378
+ case CustomTypeModelFieldType.Group: {
379
+ const code = await helpers.format(commonTags.stripIndent`
380
+ <>{${dotPath(fieldPath)}.map(item => (
381
+ <>{/* Render content for item */}</>
382
+ ))}</>
383
+ `, void 0, { prettier: prettierOptions });
384
+ return {
385
+ label,
386
+ language: "tsx",
387
+ code
388
+ };
389
+ }
390
+ case CustomTypeModelFieldType.Slices: {
391
+ const code = await helpers.format(commonTags.stripIndent`
392
+ <SliceZone
393
+ slices={${dotPath(fieldPath)}}
394
+ components={components}
395
+ />
396
+ `, void 0, { prettier: prettierOptions });
397
+ return {
398
+ label,
399
+ language: "tsx",
400
+ code
401
+ };
402
+ }
403
+ default: {
404
+ return {
405
+ label,
406
+ language: "tsx",
407
+ code: await helpers.format(commonTags.stripIndent`
408
+ <>{${dotPath(fieldPath)}}</>
409
+ `, void 0, { prettier: prettierOptions })
410
+ };
411
+ }
412
+ }
413
+ };
414
+
415
+ const REQUIRED_DEPENDENCIES = [
416
+ "@prismicio/react",
417
+ "@prismicio/slice-simulator-react",
418
+ "@prismicio/client@latest",
419
+ "@prismicio/helpers"
420
+ ];
421
+ const createStep1 = async ({
422
+ project
423
+ }) => {
424
+ const require = node_module.createRequire(project.root);
425
+ return {
426
+ title: "Install packages",
427
+ body: commonTags.stripIndent`
428
+ The simulator requires extra dependencies. Run the following command to install them.
429
+
430
+ ~~~sh
431
+ npm install --save @prismicio/react @prismicio/slice-simulator-react @prismicio/client@latest @prismicio/helpers
432
+ ~~~
433
+ `,
434
+ validate: async () => {
435
+ const missingDependencies = [];
436
+ for (const dependency of REQUIRED_DEPENDENCIES) {
437
+ try {
438
+ require.resolve(dependency);
439
+ } catch (e) {
440
+ missingDependencies.push(dependency);
441
+ }
442
+ }
443
+ if (missingDependencies.length >= REQUIRED_DEPENDENCIES.length) {
444
+ return {
445
+ type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Error,
446
+ title: "Missing all dependencies",
447
+ message: commonTags.stripIndent`
448
+ Install the required dependencies to continue.
449
+ `
450
+ };
451
+ }
452
+ if (missingDependencies.length > 0) {
453
+ const formattedMissingDependencies = missingDependencies.map((missingDependency) => `\`${missingDependency}\``).join(", ");
454
+ return {
455
+ type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Warning,
456
+ title: "Missing some dependencies",
457
+ message: commonTags.stripIndent`
458
+ The following dependencies are missing: ${formattedMissingDependencies}
459
+ `
460
+ };
461
+ }
462
+ }
463
+ };
464
+ };
465
+ const createStep2 = async ({
466
+ helpers,
467
+ options
468
+ }) => {
469
+ const fileName = `slice-simulator.${getJSOrTSXFileExtension}`;
470
+ const filePath = helpers.joinPathFromRoot("pages", fileName);
471
+ let fileContents;
472
+ if (options.typescript) {
473
+ fileContents = commonTags.stripIndent`
474
+ import { GetStaticProps } from "next/types";
475
+ import { SliceSimulator } from "@prismicio/slice-simulator-react";
476
+ import { SliceZone } from "@prismicio/react";
477
+
478
+ import state from "../.slicemachine/libraries-state.json";
479
+ import { components } from "../slices";
480
+
481
+ const SliceSimulatorPage = () => {
482
+ return (
483
+ <SliceSimulator
484
+ sliceZone={(props) => <SliceZone {...props} components={components} />}
485
+ state={state}
486
+ />
487
+ );
488
+ };
489
+
490
+ export default SliceSimulatorPage;
491
+
492
+ export const getStaticProps: GetStaticProps = () => {
493
+ return {
494
+ // Exclude this page from production builds.
495
+ notFound: process.env.NODE_ENV === "production",
496
+ };
497
+ };
498
+ `;
499
+ } else {
500
+ fileContents = commonTags.stripIndent`
501
+ import { SliceSimulator } from "@prismicio/slice-simulator-react";
502
+ import { SliceZone } from "@prismicio/react";
503
+
504
+ import state from "../.slicemachine/libraries-state.json";
505
+ import { components } from "../slices";
506
+
507
+ const SliceSimulatorPage = () => {
508
+ return (
509
+ <SliceSimulator
510
+ sliceZone={(props) => <SliceZone {...props} components={components} />}
511
+ state={state}
512
+ />
513
+ );
514
+ };
515
+
516
+ export default SliceSimulatorPage;
517
+
518
+ export const getStaticProps= () => {
519
+ return {
520
+ // Exclude this page from production builds.
521
+ notFound: process.env.NODE_ENV === "production",
522
+ };
523
+ };
524
+ `;
525
+ }
526
+ fileContents = await helpers.format(fileContents, filePath);
527
+ return {
528
+ title: "Create a page for the simulator",
529
+ body: commonTags.stripIndent`
530
+ In your \`pages\` directory, create a file called \`${fileName}\` and add the following code. This route will be used to simulate and develop your components.
531
+
532
+ ~~~tsx
533
+ ${fileContents}
534
+ ~~~
535
+ `
536
+ };
537
+ };
538
+ const createStep3 = async ({
539
+ helpers
540
+ }) => {
541
+ const filePath = helpers.joinPathFromRoot("sm.json");
542
+ const fileContents = await helpers.format(`
543
+ {
544
+ "localSliceSimulatorURL": "http://localhost:3000/slice-simulator"
545
+ }
546
+ `, filePath);
547
+ return {
548
+ title: "Update `sm.json`",
549
+ body: commonTags.stripIndent`
550
+ Update your \`sm.json\` file with a \`localSliceSimulatorURL\` property pointing to your \`slice-simulator\` page.
551
+
552
+ ~~~json
553
+ ${fileContents}
554
+ ~~~
555
+ `,
556
+ validate: async () => {
557
+ const project = await helpers.getProject();
558
+ if (!("localSliceSimulatorURL" in project.config)) {
559
+ return {
560
+ type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Error,
561
+ title: "Missing `localSliceSimulatorURL` property",
562
+ message: commonTags.stripIndent`
563
+ A \`localSliceSimulatorURL\` property was not found in your \`sm.json\` file.
564
+ `
565
+ };
566
+ }
567
+ try {
568
+ if (project.config.localSliceSimulatorURL) {
569
+ new URL(project.config.localSliceSimulatorURL);
570
+ } else {
571
+ throw new Error("Undefined Slice Simulator URL");
572
+ }
573
+ } catch (e) {
574
+ return {
575
+ type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Warning,
576
+ title: "An invalid URL was provided",
577
+ message: commonTags.stripIndent`
578
+ The \`localSliceSimulatorURL\` property should be of the shape \`http://localhost:PORT/PATH\`. See the codeblock for an example.
579
+ `
580
+ };
581
+ }
582
+ const ok = await new Promise((resolve) => {
583
+ if (project.config.localSliceSimulatorURL) {
584
+ http__namespace.get(project.config.localSliceSimulatorURL, (res) => {
585
+ if (res.statusCode) {
586
+ resolve(res.statusCode >= 200 && res.statusCode < 300);
587
+ }
588
+ });
589
+ }
590
+ resolve(false);
591
+ });
592
+ if (!ok) {
593
+ return {
594
+ type: pluginKit.SliceSimulatorSetupStepValidationMessageType.Warning,
595
+ title: "Unable to connect to simulator page",
596
+ message: commonTags.stripIndent`
597
+ Check that the \`localSliceSimulatorURL\` property in \`sm.json\` is correct and try again. See the [troubleshooting page](https://prismic.io/docs/technologies/setup-slice-simulator-nextjs) for more details.
598
+ `
599
+ };
600
+ }
601
+ }
602
+ };
603
+ };
604
+ const sliceSimulatorSetupRead = async (_data, context) => {
605
+ return Promise.all([
606
+ createStep1(context),
607
+ createStep2(context),
608
+ createStep3(context)
609
+ ]);
610
+ };
611
+
612
+ const plugin = pluginKit.defineSliceMachinePlugin({
613
+ meta: {
614
+ name: name
615
+ },
616
+ defaultOptions: {
617
+ format: true
618
+ },
619
+ setup({ hook }) {
620
+ hook("slice:create", sliceCreate);
621
+ hook("slice:update", sliceUpdate);
622
+ hook("slice:delete", sliceDelete);
623
+ hook("slice:read", sliceRead);
624
+ hook("slice-library:read", sliceLibraryRead);
625
+ hook("custom-type:create", customTypeCreate);
626
+ hook("custom-type:update", customTypeUpdate);
627
+ hook("custom-type:delete", customTypeDelete);
628
+ hook("custom-type:read", customTypeRead);
629
+ hook("custom-type-library:read", customTypeLibraryRead);
630
+ hook("snippet:read", snippetRead);
631
+ hook("slice-simulator:setup:read", sliceSimulatorSetupRead);
632
+ }
633
+ });
634
+
635
+ const coreManager = new sliceSimulatorCore.CoreManager();
636
+ const SliceSimulator = ({
637
+ sliceZone: SliceZoneComp,
638
+ state,
639
+ background,
640
+ zIndex,
641
+ className
642
+ }) => {
643
+ const defaultProps = sliceSimulatorCore.getDefaultProps();
644
+ const [managedState, setManagedState] = React__namespace.useState(() => sliceSimulatorCore.getDefaultManagedState());
645
+ const [slices, setSlices] = React__namespace.useState(() => sliceSimulatorCore.getDefaultSlices());
646
+ const [message, setMessage] = React__namespace.useState(() => sliceSimulatorCore.getDefaultMessage());
647
+ React__namespace.useEffect(() => {
648
+ coreManager.stateManager.on(sliceSimulatorCore.StateManagerEventType.ManagedState, (_managedState) => {
649
+ setManagedState(_managedState);
650
+ }, "simulator-managed-state");
651
+ coreManager.stateManager.on(sliceSimulatorCore.StateManagerEventType.Slices, (_slices) => {
652
+ setSlices(_slices);
653
+ }, "simulator-slices");
654
+ coreManager.stateManager.on(sliceSimulatorCore.StateManagerEventType.Message, (_message) => {
655
+ setMessage(_message);
656
+ }, "simulator-message");
657
+ coreManager.init(state);
658
+ return () => {
659
+ coreManager.stateManager.off(sliceSimulatorCore.StateManagerEventType.ManagedState, "simulator-managed-state");
660
+ coreManager.stateManager.off(sliceSimulatorCore.StateManagerEventType.Slices, "simulator-slices");
661
+ coreManager.stateManager.off(sliceSimulatorCore.StateManagerEventType.Message, "simulator-message");
662
+ };
663
+ }, []);
664
+ const didMount = React__namespace.useRef(false);
665
+ React__namespace.useEffect(() => {
666
+ if (didMount.current) {
667
+ coreManager.stateManager.reload(state);
668
+ } else {
669
+ didMount.current = true;
670
+ }
671
+ }, [state]);
672
+ return /* @__PURE__ */ React__namespace.createElement("div", {
673
+ className: [sliceSimulatorCore.simulatorClass, className].filter(Boolean).join(" "),
674
+ style: {
675
+ zIndex: typeof zIndex === "undefined" ? defaultProps.zIndex : zIndex != null ? zIndex : void 0,
676
+ position: "fixed",
677
+ top: 0,
678
+ left: 0,
679
+ width: "100%",
680
+ height: "100vh",
681
+ overflow: "auto",
682
+ background: typeof background === "undefined" ? defaultProps.background : background != null ? background : void 0
683
+ }
684
+ }, message ? /* @__PURE__ */ React__namespace.createElement("article", {
685
+ dangerouslySetInnerHTML: { __html: message }
686
+ }) : slices.length ? /* @__PURE__ */ React__namespace.createElement("div", {
687
+ id: "root",
688
+ className: sliceSimulatorCore.simulatorRootClass,
689
+ style: managedState.status !== sliceSimulatorCore.StateManagerStatus.Loaded ? { display: "none" } : void 0,
690
+ onClickCapture: sliceSimulatorCore.onClickHandler,
691
+ onSubmitCapture: sliceSimulatorCore.disableEventHandler
692
+ }, /* @__PURE__ */ React__namespace.createElement(SliceZoneComp, {
693
+ slices
694
+ })) : null);
695
+ };
696
+
697
+ exports.SliceSimulator = SliceSimulator;
698
+ exports["default"] = plugin;
699
+ //# sourceMappingURL=index.cjs.map