@nascentdigital/funnel-editor 6.1.7 → 7.0.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.
Files changed (82) hide show
  1. package/dist/collection/components/CollectionHeader.js +17 -3
  2. package/dist/core/components/fields/EnumField.js +3 -1
  3. package/dist/core/components/fields/FieldHeader.js +16 -23
  4. package/dist/core/components/fields/FieldLayout.d.ts +1 -1
  5. package/dist/core/components/fields/FieldLayout.js +10 -18
  6. package/dist/core/components/fields/collection/CollectionEditor.js +7 -7
  7. package/dist/core/components/index.d.ts +1 -0
  8. package/dist/core/components/index.js +1 -0
  9. package/dist/core/components/interaction/Combobox.js +17 -28
  10. package/dist/core/components/interaction/Popup.js +14 -2
  11. package/dist/core/components/interaction/QuickPicker.js +0 -1
  12. package/dist/core/components/panels/MessagePanel.d.ts +7 -0
  13. package/dist/core/components/panels/MessagePanel.js +53 -0
  14. package/dist/core/components/panels/index.d.ts +2 -0
  15. package/dist/core/components/panels/index.js +18 -0
  16. package/dist/core/components/panels/visual/MediaEditor.d.ts +12 -0
  17. package/dist/core/components/panels/visual/MediaEditor.js +47 -0
  18. package/dist/core/components/panels/visual/VisualPanel.d.ts +7 -0
  19. package/dist/core/components/panels/visual/VisualPanel.js +332 -0
  20. package/dist/core/components/panels/visual/index.d.ts +1 -0
  21. package/dist/core/components/panels/visual/index.js +17 -0
  22. package/dist/core/components/visuals/Icon.d.ts +1 -1
  23. package/dist/core/components/visuals/Icon.js +12 -0
  24. package/dist/core/domain/Block.d.ts +18 -8
  25. package/dist/core/domain/Block.js +101 -78
  26. package/dist/core/domain/BlockConfiguration.d.ts +38 -32
  27. package/dist/core/domain/BlockConfiguration.js +348 -255
  28. package/dist/core/domain/CollectionFieldProxy.d.ts +1 -0
  29. package/dist/core/domain/CollectionFieldProxy.js +5 -0
  30. package/dist/core/domain/CollectionSummary.d.ts +1 -1
  31. package/dist/core/domain/CollectionSummary.js +1 -1
  32. package/dist/core/domain/Collections.d.ts +2 -0
  33. package/dist/core/domain/Collections.js +6 -0
  34. package/dist/core/domain/IPage.d.ts +3 -2
  35. package/dist/core/domain/Item.d.ts +1 -1
  36. package/dist/core/domain/ItemBase.d.ts +2 -0
  37. package/dist/core/domain/ItemBase.js +9 -0
  38. package/dist/core/domain/Page.d.ts +1 -1
  39. package/dist/core/domain/SectionBlock.d.ts +4 -5
  40. package/dist/core/domain/SectionBlock.js +3 -10
  41. package/dist/core/hooks/useBlockConfiguration.d.ts +1 -7
  42. package/dist/core/hooks/useBlockConfiguration.js +1 -6
  43. package/dist/core/repository/PageSerializer.js +11 -10
  44. package/dist/core/repository/SerializeCollectionUpdate.js +4 -4
  45. package/dist/core/repository/SerializePageUpdate.d.ts +2 -1
  46. package/dist/core/repository/SerializePageUpdate.js +79 -24
  47. package/dist/page/components/BlockEditor.js +3 -3
  48. package/dist/page/components/config/BlockConfigButton.d.ts +3 -1
  49. package/dist/page/components/config/BlockConfigButton.js +12 -24
  50. package/dist/page/components/config/BlockPreview.d.ts +7 -6
  51. package/dist/page/components/config/BlockPreview.js +30 -15
  52. package/dist/page/components/config/index.d.ts +1 -2
  53. package/dist/page/components/config/index.js +1 -2
  54. package/dist/page/components/config/preview/ColumnLayout.d.ts +7 -4
  55. package/dist/page/components/config/preview/ColumnLayout.js +14 -2
  56. package/dist/page/components/config/preview/ItemLayout.d.ts +7 -4
  57. package/dist/page/components/config/preview/ItemLayout.js +24 -11
  58. package/dist/page/components/config/preview/ListPanel.d.ts +7 -4
  59. package/dist/page/components/config/preview/ListPanel.js +4 -2
  60. package/dist/page/components/config/preview/MessagePanel.d.ts +7 -4
  61. package/dist/page/components/config/preview/MessagePanel.js +6 -3
  62. package/dist/page/components/config/preview/Panel.d.ts +14 -4
  63. package/dist/page/components/config/preview/Panel.js +25 -7
  64. package/dist/page/components/config/preview/PreviewSize.d.ts +1 -0
  65. package/dist/page/components/config/preview/PreviewSize.js +2 -0
  66. package/dist/page/components/config/preview/RowLayout.d.ts +7 -4
  67. package/dist/page/components/config/preview/RowLayout.js +15 -3
  68. package/dist/page/components/config/preview/StackLayout.d.ts +7 -4
  69. package/dist/page/components/config/preview/StackLayout.js +14 -2
  70. package/dist/page/components/config/preview/VisualPanel.d.ts +7 -4
  71. package/dist/page/components/config/preview/VisualPanel.js +7 -5
  72. package/dist/page/components/config/preview/index.d.ts +2 -0
  73. package/dist/page/components/config/preview/index.js +2 -0
  74. package/dist/page/components/panels/BlockConfigPanel.d.ts +7 -1
  75. package/dist/page/components/panels/BlockConfigPanel.js +9 -7
  76. package/dist/page/screens/PageEditor.js +3 -4
  77. package/dist/styles/index.css +74 -56
  78. package/package.json +2 -2
  79. package/dist/page/components/config/BlockConfigurationProvider.d.ts +0 -16
  80. package/dist/page/components/config/BlockConfigurationProvider.js +0 -25
  81. package/dist/page/components/config/useBlockConfiguration$.d.ts +0 -3
  82. package/dist/page/components/config/useBlockConfiguration$.js +0 -22
@@ -4,68 +4,146 @@ exports.BlockConfiguration = void 0;
4
4
  const funnel_core_1 = require("@nascentdigital/funnel-core");
5
5
  const funnel_ui_1 = require("@nascentdigital/funnel-ui");
6
6
  const rxjs_1 = require("rxjs");
7
- const SectionBlock_1 = require("./SectionBlock");
7
+ const repository_1 = require("../repository");
8
8
  // model
9
9
  class BlockConfiguration {
10
- constructor({ block: blockOrSectionBlock, assetStore, collectionStore }) {
10
+ constructor({ block, assetStore, collectionStore, configData, variant = 'base' }) {
11
11
  // initialize core fields
12
- this.eventBus = new funnel_core_1.EventBus();
12
+ this.log = funnel_core_1.Logger.get('editor:page:blockConfig');
13
+ this.eventBus = block.eventBus;
14
+ this.block = block;
13
15
  this.assetStore = assetStore;
14
16
  this.collectionStore = collectionStore;
15
- const fields = [];
16
- this._fields = fields;
17
- this._editableFields = fields;
18
- this._scope = 'desktop';
19
- this._isConfigInDefault = false;
17
+ this._fields = funnel_core_1.Schema.Block.Config.Variant.values.reduce((fields, variant) => {
18
+ // create fields for this variant
19
+ fields[variant] = createFields({ log: this.log, variant, block, configData });
20
+ // keep reducing
21
+ return fields;
22
+ }, {});
23
+ this.configData = configData;
24
+ this._createdVariants = new Set();
25
+ this._deletedVariants = new Set();
26
+ this._variant = variant;
20
27
  this._slotState = { options: [] };
21
- // handle section blocks
22
- if (blockOrSectionBlock instanceof SectionBlock_1.SectionBlock) {
23
- // capture parents
24
- this.sectionBlock = blockOrSectionBlock;
25
- this.block = blockOrSectionBlock.block;
26
- }
27
- // or regular block
28
- else {
29
- this.block = blockOrSectionBlock;
30
- }
31
- // sync
32
- this.reset();
28
+ // sync on mount
29
+ this.syncAll(true);
33
30
  // force sync if a field is modified
34
- this.block.fieldChanged$
35
- .pipe((0, rxjs_1.debounceTime)(100))
36
- .subscribe(() => this.sync());
37
31
  this.eventBus.events$
38
- .pipe((0, rxjs_1.filter)(event => funnel_core_1.Field.Event.filter(event) && event.type === 'changed'))
39
- .subscribe(() => this.sync());
32
+ .pipe((0, rxjs_1.filter)(funnel_core_1.Field.Event.filter), (0, rxjs_1.filter)(event => (event.type === 'changed' || event.type === 'reset')
33
+ && this.fields.includes(event.field)))
34
+ .subscribe(e => {
35
+ this.log.d(`config changed - handling config update for ${block.name}/${e.field.id}`, e);
36
+ // clear value
37
+ delete this._value;
38
+ this._modifiedOn = Date.now();
39
+ // sync layout
40
+ this.sync({ variant: this.variant });
41
+ });
40
42
  }
41
- get fields() {
42
- return this._fields;
43
+ get events$() {
44
+ return this.eventBus.events$.pipe((0, rxjs_1.filter)(BlockConfiguration.Event.filter));
43
45
  }
44
- get editableFields() {
45
- return this._editableFields;
46
+ get fields() {
47
+ const { _fields, _variant } = this;
48
+ return _fields[_variant].filter(field => !!field.schemaOverride);
46
49
  }
47
- set editableFields(fields) {
48
- this._editableFields = fields;
49
- this.raiseEvent('changed');
50
+ getFields(variant) {
51
+ // skip if variant isn't available
52
+ if (!this.isVariantAvailable(variant)) {
53
+ return [];
54
+ }
55
+ // or return all active fields
56
+ const { _fields } = this;
57
+ return _fields[variant].filter(field => !!field.schemaOverride);
58
+ }
59
+ get value() {
60
+ // return existing value (if available)
61
+ const { log, block, _fields, _value } = this;
62
+ const { name } = block;
63
+ if (_value) {
64
+ log.d(`value/${name} - returning cached value`);
65
+ return _value;
66
+ }
67
+ log.s(`value/${name} - creating value for "${block.name}"`);
68
+ const value = funnel_core_1.Schema.Block.Config.Variant.values.reduce((value, variant) => {
69
+ // clear value if it doesn't exist
70
+ if (!this.isVariantAvailable(variant)) {
71
+ log.s(`value/${name} - skipping unactivated variant "${variant}"`);
72
+ value[variant] = undefined;
73
+ }
74
+ // or set value based on fields
75
+ else {
76
+ log.s(`value/${name} - serializing fields for "${variant}"`, _fields[variant]);
77
+ value[variant] = _fields[variant].reduce((data, { id, schemaOverride, value }) => {
78
+ // only apply value if we have one
79
+ if (schemaOverride && value) {
80
+ log.s(`value/${name}/${variant} - setting value for "${id}" to`, value);
81
+ data[id] = value;
82
+ }
83
+ else {
84
+ log.s(`value/${name} - ignoring inactive field "${id}"`, { schemaOverride, value });
85
+ }
86
+ // keep reducing
87
+ return data;
88
+ }, {});
89
+ }
90
+ // keep reducing
91
+ return value;
92
+ }, {});
93
+ // cache
94
+ this._value = value;
95
+ // return value
96
+ return value;
50
97
  }
51
- get scope() {
52
- return this._scope;
98
+ get variant() {
99
+ return this._variant;
53
100
  }
54
- set scope(scope) {
101
+ set variant(variant) {
55
102
  // ignore if unchanged
56
- if (this._scope === scope) {
103
+ if (this._variant === variant) {
57
104
  return;
58
105
  }
59
- // update + raise event
60
- this._scope = scope;
106
+ // blow up if the variant isn't available
107
+ if (!this.isVariantAvailable(variant)) {
108
+ throw new funnel_core_1.InvalidOperationError(`Cannot switch to unavailable variant "${variant}" - use createVariant() instead`);
109
+ }
110
+ // update variant
111
+ this._variant = variant;
112
+ // raise change
61
113
  this.raiseEvent('changed');
114
+ // sync
115
+ this.sync({ variant });
116
+ }
117
+ isVariantAvailable(variant) {
118
+ var _a, _b;
119
+ this.log.s(`isVariantAvailable - checking fields for "${variant}"`, this._fields);
120
+ // base is always available
121
+ if (variant === 'base') {
122
+ return true;
123
+ }
124
+ // deleted are never available
125
+ else if (this._deletedVariants.has(variant)) {
126
+ return false;
127
+ }
128
+ // otherwise, check if created/existing
129
+ else {
130
+ return this._createdVariants.has(variant)
131
+ || Object.keys((_b = (_a = this.configData[variant]) === null || _a === void 0 ? void 0 : _a.config) !== null && _b !== void 0 ? _b : {}).length > 0;
132
+ }
62
133
  }
63
- get blocksFields() {
134
+ get blockFields() {
64
135
  return [
65
136
  this.nameField,
66
137
  this.themeField
67
138
  ].filter(funnel_core_1.Value.isDefined);
68
139
  }
140
+ get blockExtendedFields() {
141
+ return [
142
+ ...Object.values(this.extendedFields),
143
+ this.getPossibleField('contentAlign'),
144
+ this.getPossibleField('contentSplit')
145
+ ].filter(funnel_core_1.Value.isDefined);
146
+ }
69
147
  get blockFieldData() {
70
148
  const { assetStore, collectionStore, block } = this;
71
149
  return block.fields.reduce((data, field) => {
@@ -138,29 +216,15 @@ class BlockConfiguration {
138
216
  return data;
139
217
  }, {});
140
218
  }
141
- get desktopFields() {
142
- return [
143
- ...Object.values(this.extendedFields),
144
- this.getEditableField('contentAlign'),
145
- this.getEditableField('contentSplit')
146
- ].filter(funnel_core_1.Value.isDefined);
147
- }
148
- get mobileFields() {
149
- return [
150
- ...Object.values(this.extendedFields)
151
- ].filter(funnel_core_1.Value.isDefined);
152
- }
153
219
  get nameField() {
154
- const { block, sectionBlock } = this;
155
- return sectionBlock
156
- ? this.getEditableField(block.nameField.id)
157
- : undefined;
220
+ const { block } = this;
221
+ return block.nameField;
158
222
  }
159
223
  get themeField() {
160
- return this.getEditableField(funnel_ui_1.BlockConfig.Presets.ThemeOnly.key);
224
+ return this.getPossibleField(funnel_ui_1.BlockConfig.Presets.ThemeOnly.key);
161
225
  }
162
226
  get layoutField() {
163
- return this._editableFields.find(field => field.id === 'layout');
227
+ return this.fields.find(field => field.id === 'layout');
164
228
  }
165
229
  get slotState() {
166
230
  return this._slotState;
@@ -196,56 +260,46 @@ class BlockConfiguration {
196
260
  field.value = funnel_core_1.Schema.EnumField.Value.toData(newOption);
197
261
  }
198
262
  get hasBlockConfig() {
199
- return this.blocksFields.length > 0
200
- || this.desktopFields.length > 0
201
- || this.mobileFields.length > 0;
263
+ return this.fields.length > 0;
202
264
  }
203
265
  get hasPanels() {
204
- // build list of panel keys
205
- const panelKeys = [
206
- ...Object.values(funnel_ui_1.MessagePanel.Config.getKeys('message')),
207
- ...Object.values(funnel_ui_1.ListPanel.Config.getKeys('list')),
208
- ...Object.values(funnel_ui_1.VisualPanel.Config.getKeys('visual'))
209
- ];
210
- // return true if any keys exist
211
- return this._editableFields.some(field => panelKeys.includes(field.id));
266
+ return this.block.schema instanceof funnel_core_1.Schema.PanelledBlock;
212
267
  }
213
268
  get messageFields() {
214
269
  const keys = funnel_ui_1.MessagePanel.Config.getKeys('message');
215
270
  return {
216
- textAlign: this.getEditableField(keys.textAlign),
217
- width: this.getEditableField(keys.width),
218
- horizontalAlign: this.getEditableField(keys.horizontalAlign),
219
- verticalAlign: this.getEditableField(keys.verticalAlign)
271
+ textAlign: this.getPossibleField(keys.textAlign),
272
+ width: this.getPossibleField(keys.width),
273
+ horizontalAlign: this.getPossibleField(keys.horizontalAlign),
274
+ verticalAlign: this.getPossibleField(keys.verticalAlign)
220
275
  };
221
276
  }
222
277
  get listFields() {
223
278
  const keys = funnel_ui_1.ListPanel.Config.getKeys('list');
224
279
  return {
225
- layout: this.getEditableField(keys.layout),
226
- width: this.getEditableField(keys.width),
227
- horizontalAlign: this.getEditableField(keys.horizontalAlign),
228
- verticalAlign: this.getEditableField(keys.verticalAlign)
280
+ layout: this.getPossibleField(keys.layout),
281
+ width: this.getPossibleField(keys.width),
282
+ horizontalAlign: this.getPossibleField(keys.horizontalAlign),
283
+ verticalAlign: this.getPossibleField(keys.verticalAlign)
229
284
  };
230
285
  }
231
286
  get visualFields() {
232
287
  const keys = funnel_ui_1.VisualPanel.Config.getKeys('visual');
233
288
  return {
234
- aspect: this.getEditableField(keys.aspect),
235
- anchor: this.getEditableField(keys.anchor),
236
- width: this.getEditableField(keys.width),
237
- horizontalAlign: this.getEditableField(keys.horizontalAlign),
238
- controls: this.getEditableField(keys.controls),
239
- autoPlay: this.getEditableField(keys.autoPlay),
240
- loop: this.getEditableField(keys.loop),
241
- mute: this.getEditableField(keys.mute)
289
+ aspect: this.getPossibleField(keys.aspect),
290
+ anchor: this.getPossibleField(keys.anchor),
291
+ width: this.getPossibleField(keys.width),
292
+ horizontalAlign: this.getPossibleField(keys.horizontalAlign),
293
+ controls: this.getPossibleField(keys.controls),
294
+ autoPlay: this.getPossibleField(keys.autoPlay),
295
+ loop: this.getPossibleField(keys.loop),
296
+ mute: this.getPossibleField(keys.mute)
242
297
  };
243
298
  }
244
299
  get extendedFields() {
245
300
  // get all top-level keys
246
- const { block, _editableFields } = this;
301
+ const { fields } = this;
247
302
  const coreKeys = [
248
- block.nameField.id,
249
303
  funnel_ui_1.BlockConfig.Presets.ThemeOnly.key,
250
304
  'layout',
251
305
  'slotVariation',
@@ -256,7 +310,7 @@ class BlockConfiguration {
256
310
  ...Object.values(funnel_ui_1.VisualPanel.Config.getKeys('visual'))
257
311
  ];
258
312
  // return all editable fields, minus the core keys
259
- return _editableFields.reduce((fields, field) => {
313
+ return fields.reduce((fields, field) => {
260
314
  // add field if not core
261
315
  if (!coreKeys.includes(field.id)) {
262
316
  fields[field.id] = field;
@@ -268,24 +322,12 @@ class BlockConfiguration {
268
322
  get layout() {
269
323
  return this._layout;
270
324
  }
271
- get events$() {
272
- return this.eventBus.events$;
273
- }
274
- get valid() {
275
- return this.editableFields.every(field => field.valid);
276
- }
277
- get modified() {
278
- const original = this.fields
279
- .filter(field => field.hasValue)
280
- .map(field => field.value);
281
- const updated = this.editableFields
282
- .filter(field => field.hasValue)
283
- .map(field => field.value);
284
- return !funnel_core_1.Value.equals(original, updated);
325
+ getPossibleField(fieldId) {
326
+ return this.fields.find(field => field.id === fieldId);
285
327
  }
286
328
  getField(fieldId) {
287
329
  // return field or throw
288
- const field = this.fields.find(field => field.id === fieldId);
330
+ const field = this.getPossibleField(fieldId);
289
331
  if (!field) {
290
332
  throw new funnel_core_1.IllegalStateError(`Unable to resolve field ${this.block.name}/${fieldId}`);
291
333
  }
@@ -315,165 +357,152 @@ class BlockConfiguration {
315
357
  return fieldValue;
316
358
  }
317
359
  }
318
- getEditableField(fieldId) {
319
- return this._editableFields.find(field => field.id === fieldId);
320
- }
321
- save() {
322
- // save each modified field
323
- const { sectionBlock, block, editableFields } = this;
324
- editableFields.forEach(field => {
325
- // skip if not modified / valid
326
- const { id, modified, valid } = field;
327
- // handle name change field
328
- if (id === block.nameField.id) {
329
- // return if not modified or not valid
330
- if (!modified || !valid) {
331
- return;
332
- }
333
- // throw if it's not a section (should never happen)
334
- if (!sectionBlock || block.nameField.schema.type !== field.schema.type) {
335
- throw new funnel_core_1.IllegalStateError('Attempt to save nameField in regular Block');
336
- }
337
- // or sync upwards
338
- else {
339
- block.nameField.value = field.value;
340
- }
341
- }
342
- // otherwise, handle other fields
343
- else {
344
- // throw if field doesn't exist (should never happen)
345
- const originalField = block.getConfig(field.id);
346
- if (!originalField) {
347
- throw new funnel_core_1.IllegalStateError(`Unable to find field in block: ${field.id}`);
348
- }
349
- // skip if the original field had a value and is now not modified or is not valid
350
- if ((originalField.hasValue && !modified) || !valid) {
351
- return;
352
- }
353
- // save the field, if the checks pass
354
- else {
355
- // or throw if type doesn't match
356
- if (originalField.schema.type !== field.schema.type) {
357
- throw new funnel_core_1.IllegalStateError(`Block field mismatch for "${field.id}" (${originalField.schema.type} vs ${field.schema.type})`);
358
- }
359
- // or sync upwards
360
- else {
361
- originalField.value = field.value;
362
- }
363
- }
364
- }
365
- });
366
- // clear any removed (i.e. non-editable) fields in the original block
367
- block.config.forEach(originalField => {
368
- // remove original field if it has a value, but isn't editable
369
- if (originalField.hasValue && !editableFields.some(editableField => editableField.id === originalField.id)) {
370
- // ensure field is optional
371
- if (originalField.required) {
372
- throw new funnel_core_1.InvalidOperationError(`Config schema for ${originalField.id} has been removed but isn't optional()`);
373
- }
374
- // and then opt-out
375
- originalField.optOut();
376
- }
377
- });
378
- }
379
- resetToDefault() {
380
- // set the editable fields to their default values
381
- this.editableFields.forEach(field => {
382
- // throw if default config is not set
383
- if (!this._defaultConfig) {
384
- throw new funnel_core_1.IllegalStateError('Default config not available to perform reset');
385
- }
386
- // extract default value
387
- const defaultValue = this._defaultConfig[field.id];
388
- // throw if value is not available
389
- if (!defaultValue) {
390
- throw new funnel_core_1.IllegalStateError(`Default value not available for field: ${field.id}`);
391
- }
392
- // set the default value
393
- field.coerceValue(defaultValue);
394
- });
395
- // sync with the values
396
- this.sync();
397
- }
398
- get isConfigInDefault() {
399
- return this._isConfigInDefault;
400
- }
401
- setLayout(layout) {
402
- var _a;
360
+ setPanelLayout(layout) {
361
+ var _a, _b;
403
362
  // bail if no layout field (shouldn't happen)
404
- const { block, layoutField } = this;
363
+ const { log, block, layoutField } = this;
405
364
  if (!layoutField) {
365
+ log.w('setPanelLayout - no layout field exists', layout);
406
366
  return;
407
367
  }
408
368
  // bail if no change in layout
409
369
  const previousLayout = (_a = layoutField.value) === null || _a === void 0 ? void 0 : _a.value;
410
370
  if (previousLayout === layout) {
371
+ log.v('setPanelLayout - layout is unchanged', layout);
411
372
  return;
412
373
  }
374
+ log.d('setPanelLayout - changing layout', { from: (_b = layoutField.value) === null || _b === void 0 ? void 0 : _b.value, to: layout });
413
375
  // update layout if different
414
376
  layoutField.value = funnel_core_1.Schema.EnumField.Value.toData(layout);
415
377
  // throw if this isn't a paneled layout (shouldn't happen)
416
378
  const { schema } = block;
417
379
  if (!(schema instanceof funnel_core_1.Schema.PanelledBlock)) {
380
+ log.e('setPanelLayout - cannot set layout on non-PanelledBlock');
418
381
  throw new funnel_core_1.InvalidOperationError('Cannot set layout on non-PanelledBlock instance');
419
382
  }
420
383
  // finish here if no layout change
421
384
  if (!schema.defaultLayoutConfigs) {
385
+ log.v('setPanelLayout - skipping, no layout config to evaluate');
422
386
  return;
423
387
  }
424
388
  // set config for layout
425
389
  const configForLayout = schema.defaultLayoutConfigs[layout];
426
390
  if (configForLayout) {
427
- this.setConfigValues(configForLayout);
391
+ log.d('setPanelLayout - applying default config for layout', configForLayout);
392
+ Object.entries(configForLayout).forEach(([fieldId, value]) => {
393
+ const field = this.getPossibleField(fieldId);
394
+ if (field) {
395
+ field.value = funnel_core_1.Schema.Field.Data.ForSimpleType.fromValue(field.schema, value);
396
+ }
397
+ });
428
398
  }
429
399
  }
430
- sync() {
431
- var _a;
432
- // recompute the editable fields
433
- const { block, blockFieldData: fieldData, fields, editableFields } = this;
434
- const configData = editableFields.reduce((data, field) => {
435
- data[field.id] = field.value;
400
+ sync({ variant: targetVariant = 'base', coerce = false } = {}) {
401
+ var _a, _b, _c;
402
+ // skip processing if the target isn't active AND it doesn't exist (we create new variants outside of here)
403
+ const { log, block, blockFieldData: fieldData, _fields, configData, variant: activeVariant } = this;
404
+ if (!this.isVariantAvailable(targetVariant)) {
405
+ log.d(`sync - nothing to sync for uncreated "${targetVariant}" variant`);
406
+ return;
407
+ }
408
+ this.log.d(`sync - starting sync for block "${block.name}" for "${targetVariant}"`);
409
+ // setup variables (we want to avoid affecting the active variant from outside changes)
410
+ const fields = _fields[targetVariant];
411
+ const savedData = (_b = (_a = configData[targetVariant]) === null || _a === void 0 ? void 0 : _a.config) !== null && _b !== void 0 ? _b : {};
412
+ const liveData = fields.reduce((data, field) => {
413
+ // export latest field value (if it's initialized)
414
+ if (field.schemaOverride) {
415
+ data[field.id] = field.value;
416
+ }
436
417
  // keep reducing
437
418
  return data;
438
419
  }, {});
420
+ const data = Object.assign(Object.assign({}, savedData), liveData);
439
421
  // get customized schema (if there are effects)
440
422
  const blockSchema = block.schema;
441
423
  const { getConfig } = blockSchema;
442
424
  const getLayout = blockSchema instanceof funnel_core_1.Schema.PanelledBlock
443
425
  ? blockSchema.getLayout
444
426
  : undefined;
445
- const { updatedConfig = configData, updatedConfigSchema = blockSchema.config, defaultConfig = {} } = getConfig
446
- ? getConfig({ config: configData, fields: fieldData, block: block.schema })
427
+ const { updatedConfig = data, updatedConfigSchema = blockSchema.config } = getConfig
428
+ ? getConfig({ config: data, fields: fieldData, block: block.schema })
447
429
  : {};
448
- // create new config fields
449
- const configFields = fields
450
- // drop fields not in config
451
- .filter(field => (field.id in updatedConfigSchema && updatedConfigSchema[field.id]) || field.id === block.nameField.id)
452
- // create new field and swizzle the value if the option is not available
453
- .map(field => {
454
- // clone field
430
+ log.i('sync - got config/layout using', { blockSchema, data, savedData, liveData, fieldData, updatedConfig, updatedConfigSchema, getConfig });
431
+ // update fields for target, while updating the schemas used for editing
432
+ for (const field of fields) {
433
+ // clear field schema/value if it's not included
455
434
  const updatedSchema = updatedConfigSchema[field.id];
456
435
  const updatedValue = updatedConfig[field.id];
457
- const clone = field.clone({ schema: updatedSchema === null || updatedSchema === void 0 ? void 0 : updatedSchema.require() });
458
- // if not a name field, throw if type doesn't match
459
- if (updatedSchema && clone.id !== block.nameField.id) {
460
- // fail if value type does not match with schema
461
- if (updatedValue && updatedValue.type !== updatedSchema.type) {
462
- console.error('[BlockConfiguration] updated value doesn\'t match schema type', { updatedSchema, updatedValue });
463
- throw new funnel_core_1.IllegalStateError(`Expected schema value of type "${updatedSchema.type}" but found "${updatedValue.type}"`);
464
- }
436
+ if (!updatedSchema) {
437
+ log.s(`sync - opting out of unused field "${field.id}"`);
438
+ field.overrideSchema(undefined, true);
439
+ field.reset(null, true);
465
440
  }
466
- // update value
467
- clone.coerceValue(updatedValue);
468
- // return cloned field
469
- return clone;
470
- });
441
+ // or blow up if schemas don't match
442
+ else if (updatedValue && updatedValue.type !== updatedSchema.type) {
443
+ log.e('sync - updated value doesn\'t match schema type', { updatedSchema, updatedValue });
444
+ throw new funnel_core_1.IllegalStateError(`Expected schema value of type "${updatedSchema.type}" but found "${updatedValue.type}"`);
445
+ }
446
+ // or update value
447
+ else {
448
+ log.s(`sync - updating value for field "${field.id}" to`, { updatedSchema, updatedValue });
449
+ // update schema/value
450
+ field.overrideSchema(updatedSchema.require());
451
+ field.coerceValue(updatedValue);
452
+ }
453
+ }
454
+ // sync layout/slots if active
455
+ if (targetVariant === activeVariant) {
456
+ // resolve layouts
457
+ this._layout = getLayout
458
+ ? getLayout({ variant: targetVariant, config: updatedConfig, fields: fieldData })
459
+ : undefined;
460
+ // update slots
461
+ const slotField = fields.find(field => field.id === 'slotVariation');
462
+ const slotOptions = (slotField ? Object.keys(slotField.schema.options) : []);
463
+ const slotOption = (_c = slotField === null || slotField === void 0 ? void 0 : slotField.value) === null || _c === void 0 ? void 0 : _c.value;
464
+ const slotOptionIndex = slotOption ? slotOptions.indexOf(slotOption) : -1;
465
+ this._slotState = {
466
+ field: slotField,
467
+ options: slotOptions,
468
+ currentIndex: slotOptionIndex < 0 ? undefined : slotOptionIndex,
469
+ currentOption: slotOption
470
+ };
471
+ }
472
+ // mark change
473
+ this._modifiedOn = Date.now();
474
+ delete this._value;
475
+ // cause sync side effects if not coercing
476
+ if (!coerce) {
477
+ // raise change
478
+ this.raiseEvent('modified');
479
+ }
480
+ }
481
+ syncLayout() {
482
+ var _a;
483
+ // skip if not ready
484
+ const { log, variant } = this;
485
+ if (!this.isVariantAvailable(variant)) {
486
+ log.d(`sync - nothing to sync for uncreated "${variant}" variant`);
487
+ return;
488
+ }
489
+ // get required functions/data
490
+ const { block, blockFieldData: fieldData, fields } = this;
491
+ const blockSchema = block.schema;
492
+ const getLayout = blockSchema instanceof funnel_core_1.Schema.PanelledBlock
493
+ ? blockSchema.getLayout
494
+ : undefined;
495
+ const config = fields.reduce((config, field) => {
496
+ config[field.id] = field.value;
497
+ // keep reducing
498
+ return config;
499
+ }, {});
471
500
  // resolve layouts
472
501
  this._layout = getLayout
473
- ? getLayout({ config: updatedConfig, fields: fieldData })
502
+ ? getLayout({ variant, config, fields: fieldData })
474
503
  : undefined;
475
- // setup slot
476
- const slotField = configFields.find(field => field.id === 'slotVariation');
504
+ // update slots
505
+ const slotField = fields.find(field => field.id === 'slotVariation');
477
506
  const slotOptions = (slotField ? Object.keys(slotField.schema.options) : []);
478
507
  const slotOption = (_a = slotField === null || slotField === void 0 ? void 0 : slotField.value) === null || _a === void 0 ? void 0 : _a.value;
479
508
  const slotOptionIndex = slotOption ? slotOptions.indexOf(slotOption) : -1;
@@ -483,44 +512,94 @@ class BlockConfiguration {
483
512
  currentIndex: slotOptionIndex < 0 ? undefined : slotOptionIndex,
484
513
  currentOption: slotOption
485
514
  };
486
- // assign to editable fields (raised event)
487
- this.editableFields = configFields;
488
- // compute the default config
489
- this._defaultConfig = configFields.reduce((data, field) => {
490
- if (defaultConfig[field.id]) {
491
- return Object.assign(Object.assign({}, data), { [field.id]: defaultConfig[field.id] });
492
- }
493
- return data;
494
- }, {});
495
- // determine if the values of the editable fields are same as default
496
- this._isConfigInDefault = funnel_core_1.Value.equals(Object.values(this._defaultConfig), this.editableFields.map(field => field.value));
497
- }
498
- reset() {
499
- // create new fields
500
- const fields = [];
501
- // add name field
502
- const { eventBus, sectionBlock, block } = this;
503
- if (sectionBlock) {
504
- const nameField = block.nameField.clone({ eventBus });
505
- nameField.reset(funnel_core_1.Schema.TextField.Value.toData(block.name));
506
- fields.push(nameField);
515
+ this.raiseEvent('layout.updated');
516
+ }
517
+ syncAll(coerce = false) {
518
+ // update all variants
519
+ funnel_core_1.Schema.Block.Config.Variant.values.forEach(variant => this.sync({ variant, coerce: true }));
520
+ // mark change
521
+ this._modifiedOn = Date.now();
522
+ delete this._value;
523
+ // update changes (if not coercing)
524
+ if (!coerce) {
525
+ this.raiseEvent('modified');
507
526
  }
508
- // add fields
509
- block.config.forEach(field => fields.push(field.clone({ eventBus })));
510
- // bind fields
511
- this._fields = fields;
512
- this._editableFields = fields;
513
- // force sync
514
- this.sync();
515
- }
516
- setConfigValues(config) {
517
- console.trace('[BlockConfiguration] setting config', config);
518
- Object.entries(config).forEach(([fieldId, value]) => {
519
- const field = this.getEditableField(fieldId);
520
- if (field) {
521
- field.value = funnel_core_1.Schema.Field.Data.ForSimpleType.fromValue(field.schema, value);
527
+ }
528
+ createVariant(variant) {
529
+ const { log, _fields, _createdVariants, _deletedVariants } = this;
530
+ log.v('createVariant - handling creation for', variant);
531
+ // skip if it already exists
532
+ if (this.isVariantAvailable(variant)) {
533
+ log.w('createVariant - skipping... variant already exists for', variant);
534
+ return;
535
+ }
536
+ // create new variant
537
+ _deletedVariants.delete(variant);
538
+ _createdVariants.add(variant);
539
+ // fork desktop/base into target variant
540
+ const desktopFields = _fields.base;
541
+ _fields[variant].forEach(field => {
542
+ // indicate issue if we're missing a field
543
+ const desktopField = desktopFields.find(f => f.id === field.id);
544
+ if (!desktopField) {
545
+ log.w(`createVariant - missing field for base/${field.id}`);
522
546
  }
547
+ // or sync field
548
+ else {
549
+ log.s(`createVariant - copying base field "${field.id}" with value`, desktopField.value);
550
+ field.overrideSchema(desktopField.schemaOverride, true);
551
+ field.coerceValue(desktopField.value);
552
+ }
553
+ });
554
+ // mark change
555
+ this._modifiedOn = Date.now();
556
+ delete this._value;
557
+ // raise change
558
+ this.raiseEvent('modified');
559
+ }
560
+ deleteVariant(variant) {
561
+ const { log, _fields, _createdVariants, _deletedVariants } = this;
562
+ log.v('deleteVariant - handling deletion for', variant);
563
+ // skip if it already exists
564
+ if (!this.isVariantAvailable(variant)) {
565
+ log.w('deleteVariant - skipping... variant does not exist');
566
+ return;
567
+ }
568
+ // ensure variant is removed from created
569
+ _createdVariants.delete(variant);
570
+ _deletedVariants.add(variant);
571
+ // reset field / schema override
572
+ _fields[variant].forEach(field => {
573
+ field.overrideSchema(undefined, true);
574
+ field.reset(null, true);
523
575
  });
576
+ // mark change
577
+ this._modifiedOn = Date.now();
578
+ delete this._value;
579
+ // raise change
580
+ this.raiseEvent('modified');
581
+ }
582
+ onSaveStart() {
583
+ this._savingOn = Date.now();
584
+ }
585
+ onSaveComplete() {
586
+ // reset to initial value if not modified or if save is newer
587
+ const { _createdVariants, _deletedVariants, _modifiedOn, _savingOn, configData, variant } = this;
588
+ if (_savingOn && (!_modifiedOn || _savingOn >= _modifiedOn)) {
589
+ // reset variants/value
590
+ const { base, mobile, tablet } = this.value;
591
+ _createdVariants.clear();
592
+ _deletedVariants.clear();
593
+ delete this._value;
594
+ // map out value
595
+ configData.base = Object.assign(Object.assign({ configSnapshot: {} }, configData.base), { config: base !== null && base !== void 0 ? base : {} });
596
+ configData.mobile = Object.assign(Object.assign({ configSnapshot: {} }, configData.mobile), { config: mobile });
597
+ configData.tablet = Object.assign(Object.assign({ configSnapshot: {} }, configData.tablet), { config: tablet });
598
+ // sync
599
+ this.sync({ variant });
600
+ }
601
+ // clear saving flag
602
+ delete this._savingOn;
524
603
  }
525
604
  raiseEvent(type) {
526
605
  this.eventBus.publish(BlockConfiguration.Event.create({ type, configuration: this }));
@@ -542,3 +621,17 @@ exports.BlockConfiguration = BlockConfiguration;
542
621
  Event.create = create;
543
622
  })(Event = BlockConfiguration.Event || (BlockConfiguration.Event = {}));
544
623
  })(BlockConfiguration || (exports.BlockConfiguration = BlockConfiguration = {}));
624
+ function createFields({ log, block, configData, variant }) {
625
+ var _a, _b;
626
+ const configSchema = Object.entries((_a = block.schema.config) !== null && _a !== void 0 ? _a : {});
627
+ const { config = {}, configSnapshot = {} } = (_b = configData[variant]) !== null && _b !== void 0 ? _b : {};
628
+ log.s('createSchema - creating fields for', { block, configSchema, config, variant });
629
+ return configSchema.map(([id, schema]) => repository_1.FieldSerializer.deserializeField({
630
+ id,
631
+ eventBus: block.eventBus,
632
+ schema,
633
+ content: config[id],
634
+ snapshot: configSnapshot[id],
635
+ config: true
636
+ }));
637
+ }