Skip to content

Commit 3ca70a6

Browse files
authored
Fix pages load (#6551)
* Ensure the load of pages is passing via the add method. * Add `canvas:frame:unload` event * Up ts device events
1 parent b6f5ee9 commit 3ca70a6

File tree

6 files changed

+43
-26
lines changed

6 files changed

+43
-26
lines changed

packages/core/src/canvas/model/Canvas.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ export default class Canvas extends ModuleModel<CanvasModule> {
5252
const { em } = this;
5353
em.setSelected();
5454
em.get('readyCanvas') && em.stopDefault(); // We have to stop before changing current frames
55-
prev?.getFrames().map((frame) => frame.disable());
55+
prev?.getFrames().map((frame) => {
56+
frame.disable();
57+
frame._emitUnload();
58+
});
5659
this.set('frames', page.getFrames());
5760
this.updateDevice({ frame: page.getMainFrame() });
5861
}

packages/core/src/canvas/model/Frame.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { createId, isComponent, isObject } from '../../utils/mixins';
88
import FrameView from '../view/FrameView';
99
import Frames from './Frames';
1010
import { CssRuleJSON } from '../../css_composer/model/CssRule';
11+
import CanvasEvents from '../types';
1112

1213
const keyAutoW = '__aw';
1314
const keyAutoH = '__ah';
@@ -230,6 +231,14 @@ export default class Frame extends ModuleModel<CanvasModule> {
230231
this.em.trigger('frame:updated', { frame: this, ...data });
231232
}
232233

234+
_emitUnload() {
235+
this._emitWithEditor(CanvasEvents.frameUnload, { frame: this });
236+
}
237+
238+
_emitWithEditor(event: string, data?: Record<string, any>) {
239+
[this.em, this].forEach((item) => item?.trigger(event, data));
240+
}
241+
233242
hasAutoHeight() {
234243
const { height } = this.attributes;
235244

packages/core/src/canvas/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,15 @@ export enum CanvasEvents {
149149
* });
150150
*/
151151
frameLoadBody = 'canvas:frame:load:body',
152+
153+
/**
154+
* @event `canvas:frame:unload` Frame is unloading from the canvas.
155+
* @example
156+
* editor.on('canvas:frame:unload', ({ frame }) => {
157+
* console.log('Unloading frame', frame);
158+
* });
159+
*/
160+
frameUnload = 'canvas:frame:unload',
152161
}
153162
/**{END_EVENTS}*/
154163

packages/core/src/device_manager/types.ts

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,31 @@ export enum DeviceEvents {
33
/**
44
* @event `device:add` New device added to the collection. The `Device` is passed as an argument.
55
* @example
6-
* editor.on('device:add', ({device}) => { ... });
6+
* editor.on('device:add', (device) => { ... });
77
*/
88
add = 'device:add',
9-
10-
/**
11-
* @event `device:add:before` Event triggered before a new device is added.
12-
* @example
13-
* editor.on('device:add:before', ({device}) => { ... });
14-
*/
159
addBefore = 'device:add:before',
1610

1711
/**
1812
* @event `device:remove` Device removed from the collection. The `Device` is passed as an argument.
1913
* @example
20-
* editor.on('device:remove', ({device}) => { ... });
14+
* editor.on('device:remove', (device) => { ... });
2115
*/
2216
remove = 'device:remove',
23-
24-
/**
25-
* @event `device:remove:before` Event triggered before a device is removed.
26-
* @example
27-
* editor.on('device:remove:before', ({device}) => { ... });
28-
*/
2917
removeBefore = 'device:remove:before',
3018

3119
/**
3220
* @event `device:select` A new device is selected. The `Device` is passed as an argument.
3321
* @example
34-
* editor.on('device:select', ({device}) => { ... });
22+
* editor.on('device:select', (device) => { ... });
3523
*/
3624
select = 'device:select',
37-
38-
/**
39-
* @event `device:select:before` Event triggered before a new device is selected.
40-
* @example
41-
* editor.on('device:select:before', ({device}) => { ... });
42-
*/
4325
selectBefore = 'device:select:before',
4426

4527
/**
4628
* @event `device:update` Device updated. The `Device` and the object containing changes are passed as arguments.
4729
* @example
48-
* editor.on('device:update', ({device}) => { ... });
30+
* editor.on('device:update', (device) => { ... });
4931
*/
5032
update = 'device:update',
5133

packages/core/src/dom_components/model/Components.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,9 @@ Component> {
314314
if (isString(models)) {
315315
models = this.parseString(models, opt)!;
316316
} else if (isArray(models)) {
317-
models.forEach((item: string, index: number) => {
317+
// Avoid "Cannot assign to read only property '0' of object '[object Array]'
318+
models = [...models];
319+
(models as any).forEach((item: string, index: number) => {
318320
if (isString(item)) {
319321
const nodes = this.parseString(item, opt);
320322
(models as any)[index] = isArray(nodes) && !nodes.length ? null : nodes;

packages/core/src/pages/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default class PageManager extends ItemManagerModule<PageManagerConfig, Pa
7777
bindAll(this, '_onPageChange');
7878
const model = new ModuleModel(this, { _undo: true });
7979
this.model = model;
80-
this.pages.on('reset', (coll) => coll.at(0) && this.select(coll.at(0)));
80+
this.pages.on('reset', this.__onReset, this);
8181
this.pages.on('all', this.__onChange, this);
8282
model.on(chnSel, this._onPageChange);
8383
}
@@ -88,6 +88,11 @@ export default class PageManager extends ItemManagerModule<PageManagerConfig, Pa
8888
em.trigger(events.all, { event, page, options });
8989
}
9090

91+
__onReset() {
92+
const firstPage = this.pages.at(0);
93+
firstPage && this.select(firstPage);
94+
}
95+
9196
onLoad() {
9297
const { pages, config, em } = this;
9398
const opt = { silent: true };
@@ -272,7 +277,14 @@ export default class PageManager extends ItemManagerModule<PageManagerConfig, Pa
272277
}
273278

274279
load(data: any) {
275-
const result = this.loadProjectData(data, { all: this.pages, reset: true });
280+
const result = this.loadProjectData(data, {
281+
all: this.pages,
282+
reset: true,
283+
onResult: (result: PageProperties[], opts: AddOptions) => {
284+
result.forEach((pageProps) => this.add(pageProps, opts));
285+
this.__onReset();
286+
},
287+
});
276288
this.pages.forEach((page) => page.getFrames().initRefs());
277289
return result;
278290
}

0 commit comments

Comments
 (0)