@@ -3,7 +3,7 @@ import Backbone from 'backbone';
3
3
import $ from '../../utils/cash-dom' ;
4
4
import Extender from '../../utils/extender' ;
5
5
import { hasWin , isEmptyObj , wait } from '../../utils/mixins' ;
6
- import { AddOptions , Model , Collection , ObjectAny } from '../../common' ;
6
+ import { Model , Collection , ObjectAny } from '../../common' ;
7
7
import Selected from './Selected' ;
8
8
import FrameView from '../../canvas/view/FrameView' ;
9
9
import Editor from '..' ;
@@ -45,6 +45,7 @@ import { CanvasSpotBuiltInTypes } from '../../canvas/model/CanvasSpot';
45
45
import DataSourceManager from '../../data_sources' ;
46
46
import { ComponentsEvents } from '../../dom_components/types' ;
47
47
import { InitEditorConfig } from '../..' ;
48
+ import { EditorEvents } from '../types' ;
48
49
49
50
Backbone . $ = $ ;
50
51
@@ -89,6 +90,7 @@ const logs = {
89
90
export interface EditorLoadOptions {
90
91
/** Clear the editor state (eg. dirty counter, undo manager, etc.). */
91
92
clear ?: boolean ;
93
+ initial ?: boolean ;
92
94
}
93
95
94
96
export default class EditorModel extends Model {
@@ -333,6 +335,7 @@ export default class EditorModel extends Model {
333
335
*/
334
336
loadOnStart ( ) {
335
337
const { projectData, headless } = this . config ;
338
+ const loadOpts : EditorLoadOptions = { initial : true } ;
336
339
const sm = this . Storage ;
337
340
338
341
// In `onLoad`, the module will try to load the data from its configurations.
@@ -345,16 +348,16 @@ export default class EditorModel extends Model {
345
348
} ;
346
349
347
350
if ( headless ) {
348
- projectData && this . loadData ( projectData ) ;
351
+ projectData && this . loadData ( projectData , loadOpts ) ;
349
352
postLoad ( ) ;
350
353
} else {
351
354
// Defer for storage load events.
352
355
this . _storageTimeout = setTimeout ( async ( ) => {
353
356
if ( projectData ) {
354
- this . loadData ( projectData ) ;
357
+ this . loadData ( projectData , loadOpts ) ;
355
358
} else if ( sm ?. canAutoload ( ) ) {
356
359
try {
357
- await this . load ( ) ;
360
+ await this . load ( { } , loadOpts ) ;
358
361
} catch ( error ) {
359
362
this . logError ( error as string ) ;
360
363
}
@@ -388,7 +391,7 @@ export default class EditorModel extends Model {
388
391
389
392
if ( ! opts . isClear ) {
390
393
this . updateItr && clearTimeout ( this . updateItr ) ;
391
- this . updateItr = setTimeout ( ( ) => this . trigger ( ' update' ) ) ;
394
+ this . updateItr = setTimeout ( ( ) => this . trigger ( EditorEvents . update ) ) ;
392
395
}
393
396
394
397
if ( this . config . noticeOnUnload ) {
@@ -851,7 +854,7 @@ export default class EditorModel extends Model {
851
854
*/
852
855
async load < T extends StorageOptions > ( options ?: T , loadOptions : EditorLoadOptions = { } ) {
853
856
const result = await this . Storage . load ( options ) ;
854
- this . loadData ( result ) ;
857
+ this . loadData ( result , loadOptions ) ;
855
858
// Wait in order to properly update the dirty counter (#5385)
856
859
await wait ( ) ;
857
860
@@ -875,12 +878,15 @@ export default class EditorModel extends Model {
875
878
return JSON . parse ( JSON . stringify ( result ) ) ;
876
879
}
877
880
878
- loadData ( data : ProjectData = { } ) : ProjectData {
879
- if ( ! isEmptyObj ( data ) ) {
881
+ loadData ( project : ProjectData = { } , opts : EditorLoadOptions = { } ) : ProjectData {
882
+ let loaded = false ;
883
+ if ( ! isEmptyObj ( project ) ) {
880
884
this . storables . forEach ( ( module ) => module . clear ( ) ) ;
881
- this . storables . forEach ( ( module ) => module . load ( data ) ) ;
885
+ this . storables . forEach ( ( module ) => module . load ( project ) ) ;
886
+ loaded = true ;
882
887
}
883
- return data ;
888
+ this . trigger ( EditorEvents . projectLoad , { project, loaded, initial : ! ! opts . initial } ) ;
889
+ return project ;
884
890
}
885
891
886
892
/**
@@ -1025,7 +1031,7 @@ export default class EditorModel extends Model {
1025
1031
*/
1026
1032
destroyAll ( ) {
1027
1033
const { config, view } = this ;
1028
- this . trigger ( ' destroy' ) ;
1034
+ this . trigger ( EditorEvents . destroy ) ;
1029
1035
const editor = this . getEditor ( ) ;
1030
1036
// @ts -ignore
1031
1037
const { editors = [ ] } = config . grapesjs || { } ;
@@ -1048,7 +1054,7 @@ export default class EditorModel extends Model {
1048
1054
editors . splice ( editors . indexOf ( editor ) , 1 ) ;
1049
1055
//@ts -ignore
1050
1056
hasWin ( ) && $ ( config . el ) . empty ( ) . attr ( this . attrsOrig ) ;
1051
- this . trigger ( ' destroyed' ) ;
1057
+ this . trigger ( EditorEvents . destroyed ) ;
1052
1058
}
1053
1059
1054
1060
getEditing ( ) : Component | undefined {
@@ -1066,12 +1072,13 @@ export default class EditorModel extends Model {
1066
1072
}
1067
1073
1068
1074
log ( msg : string , opts : any = { } ) {
1075
+ const logEvent = EditorEvents . log ;
1069
1076
const { ns, level = 'debug' } = opts ;
1070
- this . trigger ( 'log' , msg , opts ) ;
1071
- level && this . trigger ( `log :${ level } ` , msg , opts ) ;
1077
+ this . trigger ( logEvent , msg , opts ) ;
1078
+ level && this . trigger ( `${ logEvent } :${ level } ` , msg , opts ) ;
1072
1079
1073
1080
if ( ns ) {
1074
- const logNs = `log -${ ns } ` ;
1081
+ const logNs = `${ logEvent } -${ ns } ` ;
1075
1082
this . trigger ( logNs , msg , opts ) ;
1076
1083
level && this . trigger ( `${ logNs } :${ level } ` , msg , opts ) ;
1077
1084
}
0 commit comments