@nascentdigital/funnel-editor 6.1.9 → 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 -0
  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 +16 -7
  25. package/dist/core/domain/Block.js +98 -78
  26. package/dist/core/domain/BlockConfiguration.d.ts +35 -30
  27. package/dist/core/domain/BlockConfiguration.js +338 -254
  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 +10 -23
  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,69 +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
12
  this.log = funnel_core_1.Logger.get('editor:page:blockConfig');
13
- this.eventBus = new funnel_core_1.EventBus();
13
+ this.eventBus = block.eventBus;
14
+ this.block = block;
14
15
  this.assetStore = assetStore;
15
16
  this.collectionStore = collectionStore;
16
- const fields = [];
17
- this._fields = fields;
18
- this._editableFields = fields;
19
- this._scope = 'desktop';
20
- 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;
21
27
  this._slotState = { options: [] };
22
- // handle section blocks
23
- if (blockOrSectionBlock instanceof SectionBlock_1.SectionBlock) {
24
- // capture parents
25
- this.sectionBlock = blockOrSectionBlock;
26
- this.block = blockOrSectionBlock.block;
27
- }
28
- // or regular block
29
- else {
30
- this.block = blockOrSectionBlock;
31
- }
32
- // sync
33
- this.reset();
28
+ // sync on mount
29
+ this.syncAll(true);
34
30
  // force sync if a field is modified
35
- this.block.fieldChanged$
36
- .pipe((0, rxjs_1.debounceTime)(100))
37
- .subscribe(() => this.sync());
38
31
  this.eventBus.events$
39
- .pipe((0, rxjs_1.filter)(event => funnel_core_1.Field.Event.filter(event) && event.type === 'changed'))
40
- .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
+ });
41
42
  }
42
- get fields() {
43
- return this._fields;
43
+ get events$() {
44
+ return this.eventBus.events$.pipe((0, rxjs_1.filter)(BlockConfiguration.Event.filter));
44
45
  }
45
- get editableFields() {
46
- return this._editableFields;
46
+ get fields() {
47
+ const { _fields, _variant } = this;
48
+ return _fields[_variant].filter(field => !!field.schemaOverride);
47
49
  }
48
- set editableFields(fields) {
49
- this._editableFields = fields;
50
- 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;
51
97
  }
52
- get scope() {
53
- return this._scope;
98
+ get variant() {
99
+ return this._variant;
54
100
  }
55
- set scope(scope) {
101
+ set variant(variant) {
56
102
  // ignore if unchanged
57
- if (this._scope === scope) {
103
+ if (this._variant === variant) {
58
104
  return;
59
105
  }
60
- // update + raise event
61
- 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
62
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
+ }
63
133
  }
64
- get blocksFields() {
134
+ get blockFields() {
65
135
  return [
66
136
  this.nameField,
67
137
  this.themeField
68
138
  ].filter(funnel_core_1.Value.isDefined);
69
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
+ }
70
147
  get blockFieldData() {
71
148
  const { assetStore, collectionStore, block } = this;
72
149
  return block.fields.reduce((data, field) => {
@@ -139,29 +216,15 @@ class BlockConfiguration {
139
216
  return data;
140
217
  }, {});
141
218
  }
142
- get desktopFields() {
143
- return [
144
- ...Object.values(this.extendedFields),
145
- this.getEditableField('contentAlign'),
146
- this.getEditableField('contentSplit')
147
- ].filter(funnel_core_1.Value.isDefined);
148
- }
149
- get mobileFields() {
150
- return [
151
- ...Object.values(this.extendedFields)
152
- ].filter(funnel_core_1.Value.isDefined);
153
- }
154
219
  get nameField() {
155
- const { block, sectionBlock } = this;
156
- return sectionBlock
157
- ? this.getEditableField(block.nameField.id)
158
- : undefined;
220
+ const { block } = this;
221
+ return block.nameField;
159
222
  }
160
223
  get themeField() {
161
- return this.getEditableField(funnel_ui_1.BlockConfig.Presets.ThemeOnly.key);
224
+ return this.getPossibleField(funnel_ui_1.BlockConfig.Presets.ThemeOnly.key);
162
225
  }
163
226
  get layoutField() {
164
- return this._editableFields.find(field => field.id === 'layout');
227
+ return this.fields.find(field => field.id === 'layout');
165
228
  }
166
229
  get slotState() {
167
230
  return this._slotState;
@@ -197,56 +260,46 @@ class BlockConfiguration {
197
260
  field.value = funnel_core_1.Schema.EnumField.Value.toData(newOption);
198
261
  }
199
262
  get hasBlockConfig() {
200
- return this.blocksFields.length > 0
201
- || this.desktopFields.length > 0
202
- || this.mobileFields.length > 0;
263
+ return this.fields.length > 0;
203
264
  }
204
265
  get hasPanels() {
205
- // build list of panel keys
206
- const panelKeys = [
207
- ...Object.values(funnel_ui_1.MessagePanel.Config.getKeys('message')),
208
- ...Object.values(funnel_ui_1.ListPanel.Config.getKeys('list')),
209
- ...Object.values(funnel_ui_1.VisualPanel.Config.getKeys('visual'))
210
- ];
211
- // return true if any keys exist
212
- return this._editableFields.some(field => panelKeys.includes(field.id));
266
+ return this.block.schema instanceof funnel_core_1.Schema.PanelledBlock;
213
267
  }
214
268
  get messageFields() {
215
269
  const keys = funnel_ui_1.MessagePanel.Config.getKeys('message');
216
270
  return {
217
- textAlign: this.getEditableField(keys.textAlign),
218
- width: this.getEditableField(keys.width),
219
- horizontalAlign: this.getEditableField(keys.horizontalAlign),
220
- 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)
221
275
  };
222
276
  }
223
277
  get listFields() {
224
278
  const keys = funnel_ui_1.ListPanel.Config.getKeys('list');
225
279
  return {
226
- layout: this.getEditableField(keys.layout),
227
- width: this.getEditableField(keys.width),
228
- horizontalAlign: this.getEditableField(keys.horizontalAlign),
229
- 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)
230
284
  };
231
285
  }
232
286
  get visualFields() {
233
287
  const keys = funnel_ui_1.VisualPanel.Config.getKeys('visual');
234
288
  return {
235
- aspect: this.getEditableField(keys.aspect),
236
- anchor: this.getEditableField(keys.anchor),
237
- width: this.getEditableField(keys.width),
238
- horizontalAlign: this.getEditableField(keys.horizontalAlign),
239
- controls: this.getEditableField(keys.controls),
240
- autoPlay: this.getEditableField(keys.autoPlay),
241
- loop: this.getEditableField(keys.loop),
242
- 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)
243
297
  };
244
298
  }
245
299
  get extendedFields() {
246
300
  // get all top-level keys
247
- const { block, _editableFields } = this;
301
+ const { fields } = this;
248
302
  const coreKeys = [
249
- block.nameField.id,
250
303
  funnel_ui_1.BlockConfig.Presets.ThemeOnly.key,
251
304
  'layout',
252
305
  'slotVariation',
@@ -257,7 +310,7 @@ class BlockConfiguration {
257
310
  ...Object.values(funnel_ui_1.VisualPanel.Config.getKeys('visual'))
258
311
  ];
259
312
  // return all editable fields, minus the core keys
260
- return _editableFields.reduce((fields, field) => {
313
+ return fields.reduce((fields, field) => {
261
314
  // add field if not core
262
315
  if (!coreKeys.includes(field.id)) {
263
316
  fields[field.id] = field;
@@ -269,24 +322,12 @@ class BlockConfiguration {
269
322
  get layout() {
270
323
  return this._layout;
271
324
  }
272
- get events$() {
273
- return this.eventBus.events$;
274
- }
275
- get valid() {
276
- return this.editableFields.every(field => field.valid);
277
- }
278
- get modified() {
279
- const original = this.fields
280
- .filter(field => field.hasValue)
281
- .map(field => field.value);
282
- const updated = this.editableFields
283
- .filter(field => field.hasValue)
284
- .map(field => field.value);
285
- return !funnel_core_1.Value.equals(original, updated);
325
+ getPossibleField(fieldId) {
326
+ return this.fields.find(field => field.id === fieldId);
286
327
  }
287
328
  getField(fieldId) {
288
329
  // return field or throw
289
- const field = this.fields.find(field => field.id === fieldId);
330
+ const field = this.getPossibleField(fieldId);
290
331
  if (!field) {
291
332
  throw new funnel_core_1.IllegalStateError(`Unable to resolve field ${this.block.name}/${fieldId}`);
292
333
  }
@@ -316,89 +357,6 @@ class BlockConfiguration {
316
357
  return fieldValue;
317
358
  }
318
359
  }
319
- getEditableField(fieldId) {
320
- return this._editableFields.find(field => field.id === fieldId);
321
- }
322
- save() {
323
- // save each modified field
324
- const { sectionBlock, block, editableFields } = this;
325
- editableFields.forEach(field => {
326
- // skip if not modified / valid
327
- const { id, modified, valid } = field;
328
- // handle name change field
329
- if (id === block.nameField.id) {
330
- // return if not modified or not valid
331
- if (!modified || !valid) {
332
- return;
333
- }
334
- // throw if it's not a section (should never happen)
335
- if (!sectionBlock || block.nameField.schema.type !== field.schema.type) {
336
- throw new funnel_core_1.IllegalStateError('Attempt to save nameField in regular Block');
337
- }
338
- // or sync upwards
339
- else {
340
- block.nameField.value = field.value;
341
- }
342
- }
343
- // otherwise, handle other fields
344
- else {
345
- // throw if field doesn't exist (should never happen)
346
- const originalField = block.getConfig(field.id);
347
- if (!originalField) {
348
- throw new funnel_core_1.IllegalStateError(`Unable to find field in block: ${field.id}`);
349
- }
350
- // skip if the original field had a value and is now not modified or is not valid
351
- if ((originalField.hasValue && !modified) || !valid) {
352
- return;
353
- }
354
- // save the field, if the checks pass
355
- else {
356
- // or throw if type doesn't match
357
- if (originalField.schema.type !== field.schema.type) {
358
- throw new funnel_core_1.IllegalStateError(`Block field mismatch for "${field.id}" (${originalField.schema.type} vs ${field.schema.type})`);
359
- }
360
- // or sync upwards
361
- else {
362
- originalField.value = field.value;
363
- }
364
- }
365
- }
366
- });
367
- // clear any removed (i.e. non-editable) fields in the original block
368
- block.config.forEach(originalField => {
369
- // remove original field if it has a value, but isn't editable
370
- if (originalField.hasValue && !editableFields.some(editableField => editableField.id === originalField.id)) {
371
- // ensure field is optional
372
- if (originalField.required) {
373
- throw new funnel_core_1.InvalidOperationError(`Config schema for ${originalField.id} has been removed but isn't optional()`);
374
- }
375
- // and then opt-out
376
- originalField.optOut();
377
- }
378
- });
379
- }
380
- resetToDefault() {
381
- // set the editable fields to their default values
382
- this.editableFields.forEach(field => {
383
- // throw if default config is not set
384
- if (!this._defaultConfig) {
385
- throw new funnel_core_1.IllegalStateError('Default config not available to perform reset');
386
- }
387
- // extract default value
388
- const defaultValue = this._defaultConfig[field.id];
389
- // throw if value is not available
390
- if (!defaultValue) {
391
- throw new funnel_core_1.IllegalStateError(`Default value not available for field: ${field.id}`);
392
- }
393
- // set the default value
394
- field.coerceValue(defaultValue);
395
- });
396
- // sync with the values
397
- this.sync();
398
- }
399
- get isConfigInDefault() {
400
- return this._isConfigInDefault;
401
- }
402
360
  setPanelLayout(layout) {
403
361
  var _a, _b;
404
362
  // bail if no layout field (shouldn't happen)
@@ -431,58 +389,120 @@ class BlockConfiguration {
431
389
  const configForLayout = schema.defaultLayoutConfigs[layout];
432
390
  if (configForLayout) {
433
391
  log.d('setPanelLayout - applying default config for layout', configForLayout);
434
- this.setConfigValues(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
+ });
435
398
  }
436
399
  }
437
- sync() {
438
- var _a;
439
- // resolve props
440
- const { log, block, blockFieldData: fieldData, fields, editableFields } = this;
441
- log.v('sync - starting sync for block');
442
- // recompute the editable fields
443
- const configData = editableFields.reduce((data, field) => {
444
- 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
+ }
445
417
  // keep reducing
446
418
  return data;
447
419
  }, {});
420
+ const data = Object.assign(Object.assign({}, savedData), liveData);
448
421
  // get customized schema (if there are effects)
449
422
  const blockSchema = block.schema;
450
423
  const { getConfig } = blockSchema;
451
424
  const getLayout = blockSchema instanceof funnel_core_1.Schema.PanelledBlock
452
425
  ? blockSchema.getLayout
453
426
  : undefined;
454
- const { updatedConfig = configData, updatedConfigSchema = blockSchema.config, defaultConfig = {} } = getConfig
455
- ? 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 })
456
429
  : {};
457
- // create new config fields
458
- const configFields = fields
459
- // drop fields not in config
460
- .filter(field => (field.id in updatedConfigSchema && updatedConfigSchema[field.id]) || field.id === block.nameField.id)
461
- // create new field and swizzle the value if the option is not available
462
- .map(field => {
463
- // 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
464
434
  const updatedSchema = updatedConfigSchema[field.id];
465
435
  const updatedValue = updatedConfig[field.id];
466
- const clone = field.clone({ schema: updatedSchema === null || updatedSchema === void 0 ? void 0 : updatedSchema.require() });
467
- // if not a name field, throw if type doesn't match
468
- if (updatedSchema && clone.id !== block.nameField.id) {
469
- // fail if value type does not match with schema
470
- if (updatedValue && updatedValue.type !== updatedSchema.type) {
471
- console.error('[BlockConfiguration] updated value doesn\'t match schema type', { updatedSchema, updatedValue });
472
- throw new funnel_core_1.IllegalStateError(`Expected schema value of type "${updatedSchema.type}" but found "${updatedValue.type}"`);
473
- }
436
+ if (!updatedSchema) {
437
+ log.s(`sync - opting out of unused field "${field.id}"`);
438
+ field.overrideSchema(undefined, true);
439
+ field.reset(null, true);
474
440
  }
475
- // update value
476
- clone.coerceValue(updatedValue);
477
- // return cloned field
478
- return clone;
479
- });
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
+ }, {});
480
500
  // resolve layouts
481
501
  this._layout = getLayout
482
- ? getLayout({ config: updatedConfig, fields: fieldData })
502
+ ? getLayout({ variant, config, fields: fieldData })
483
503
  : undefined;
484
- // setup slot
485
- const slotField = configFields.find(field => field.id === 'slotVariation');
504
+ // update slots
505
+ const slotField = fields.find(field => field.id === 'slotVariation');
486
506
  const slotOptions = (slotField ? Object.keys(slotField.schema.options) : []);
487
507
  const slotOption = (_a = slotField === null || slotField === void 0 ? void 0 : slotField.value) === null || _a === void 0 ? void 0 : _a.value;
488
508
  const slotOptionIndex = slotOption ? slotOptions.indexOf(slotOption) : -1;
@@ -492,44 +512,94 @@ class BlockConfiguration {
492
512
  currentIndex: slotOptionIndex < 0 ? undefined : slotOptionIndex,
493
513
  currentOption: slotOption
494
514
  };
495
- // assign to editable fields (raised event)
496
- this.editableFields = configFields;
497
- // compute the default config
498
- this._defaultConfig = configFields.reduce((data, field) => {
499
- if (defaultConfig[field.id]) {
500
- return Object.assign(Object.assign({}, data), { [field.id]: defaultConfig[field.id] });
501
- }
502
- return data;
503
- }, {});
504
- // determine if the values of the editable fields are same as default
505
- this._isConfigInDefault = funnel_core_1.Value.equals(Object.values(this._defaultConfig), this.editableFields.map(field => field.value));
506
- }
507
- reset() {
508
- // create new fields
509
- const fields = [];
510
- // add name field
511
- const { eventBus, sectionBlock, block } = this;
512
- if (sectionBlock) {
513
- const nameField = block.nameField.clone({ eventBus });
514
- nameField.reset(funnel_core_1.Schema.TextField.Value.toData(block.name));
515
- 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');
516
526
  }
517
- // add fields
518
- block.config.forEach(field => fields.push(field.clone({ eventBus })));
519
- // bind fields
520
- this._fields = fields;
521
- this._editableFields = fields;
522
- // force sync
523
- this.sync();
524
- }
525
- setConfigValues(config) {
526
- console.trace('[BlockConfiguration] setting config', config);
527
- Object.entries(config).forEach(([fieldId, value]) => {
528
- const field = this.getEditableField(fieldId);
529
- if (field) {
530
- 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}`);
531
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);
532
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;
533
603
  }
534
604
  raiseEvent(type) {
535
605
  this.eventBus.publish(BlockConfiguration.Event.create({ type, configuration: this }));
@@ -551,3 +621,17 @@ exports.BlockConfiguration = BlockConfiguration;
551
621
  Event.create = create;
552
622
  })(Event = BlockConfiguration.Event || (BlockConfiguration.Event = {}));
553
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
+ }