|
|
@ -45,6 +45,7 @@ import { CanvasSpotBuiltInTypes } from '../../canvas/model/CanvasSpot'; |
|
|
import DataSourceManager from '../../data_sources'; |
|
|
import DataSourceManager from '../../data_sources'; |
|
|
import { ComponentsEvents } from '../../dom_components/types'; |
|
|
import { ComponentsEvents } from '../../dom_components/types'; |
|
|
import { InitEditorConfig } from '../..'; |
|
|
import { InitEditorConfig } from '../..'; |
|
|
|
|
|
import { EditorEvents } from '../types'; |
|
|
|
|
|
|
|
|
Backbone.$ = $; |
|
|
Backbone.$ = $; |
|
|
|
|
|
|
|
|
@ -89,6 +90,7 @@ const logs = { |
|
|
export interface EditorLoadOptions { |
|
|
export interface EditorLoadOptions { |
|
|
/** Clear the editor state (eg. dirty counter, undo manager, etc.). */ |
|
|
/** Clear the editor state (eg. dirty counter, undo manager, etc.). */ |
|
|
clear?: boolean; |
|
|
clear?: boolean; |
|
|
|
|
|
initial?: boolean; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export default class EditorModel extends Model { |
|
|
export default class EditorModel extends Model { |
|
|
@ -333,6 +335,7 @@ export default class EditorModel extends Model { |
|
|
*/ |
|
|
*/ |
|
|
loadOnStart() { |
|
|
loadOnStart() { |
|
|
const { projectData, headless } = this.config; |
|
|
const { projectData, headless } = this.config; |
|
|
|
|
|
const loadOpts: EditorLoadOptions = { initial: true }; |
|
|
const sm = this.Storage; |
|
|
const sm = this.Storage; |
|
|
|
|
|
|
|
|
// In `onLoad`, the module will try to load the data from its configurations.
|
|
|
// In `onLoad`, the module will try to load the data from its configurations.
|
|
|
@ -345,16 +348,16 @@ export default class EditorModel extends Model { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
if (headless) { |
|
|
if (headless) { |
|
|
projectData && this.loadData(projectData); |
|
|
projectData && this.loadData(projectData, loadOpts); |
|
|
postLoad(); |
|
|
postLoad(); |
|
|
} else { |
|
|
} else { |
|
|
// Defer for storage load events.
|
|
|
// Defer for storage load events.
|
|
|
this._storageTimeout = setTimeout(async () => { |
|
|
this._storageTimeout = setTimeout(async () => { |
|
|
if (projectData) { |
|
|
if (projectData) { |
|
|
this.loadData(projectData); |
|
|
this.loadData(projectData, loadOpts); |
|
|
} else if (sm?.canAutoload()) { |
|
|
} else if (sm?.canAutoload()) { |
|
|
try { |
|
|
try { |
|
|
await this.load(); |
|
|
await this.load({}, loadOpts); |
|
|
} catch (error) { |
|
|
} catch (error) { |
|
|
this.logError(error as string); |
|
|
this.logError(error as string); |
|
|
} |
|
|
} |
|
|
@ -388,7 +391,7 @@ export default class EditorModel extends Model { |
|
|
|
|
|
|
|
|
if (!opts.isClear) { |
|
|
if (!opts.isClear) { |
|
|
this.updateItr && clearTimeout(this.updateItr); |
|
|
this.updateItr && clearTimeout(this.updateItr); |
|
|
this.updateItr = setTimeout(() => this.trigger('update')); |
|
|
this.updateItr = setTimeout(() => this.trigger(EditorEvents.update)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (this.config.noticeOnUnload) { |
|
|
if (this.config.noticeOnUnload) { |
|
|
@ -851,7 +854,7 @@ export default class EditorModel extends Model { |
|
|
*/ |
|
|
*/ |
|
|
async load<T extends StorageOptions>(options?: T, loadOptions: EditorLoadOptions = {}) { |
|
|
async load<T extends StorageOptions>(options?: T, loadOptions: EditorLoadOptions = {}) { |
|
|
const result = await this.Storage.load(options); |
|
|
const result = await this.Storage.load(options); |
|
|
this.loadData(result); |
|
|
this.loadData(result, loadOptions); |
|
|
// Wait in order to properly update the dirty counter (#5385)
|
|
|
// Wait in order to properly update the dirty counter (#5385)
|
|
|
await wait(); |
|
|
await wait(); |
|
|
|
|
|
|
|
|
@ -875,12 +878,15 @@ export default class EditorModel extends Model { |
|
|
return JSON.parse(JSON.stringify(result)); |
|
|
return JSON.parse(JSON.stringify(result)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
loadData(data: ProjectData = {}): ProjectData { |
|
|
loadData(project: ProjectData = {}, opts: EditorLoadOptions = {}): ProjectData { |
|
|
if (!isEmptyObj(data)) { |
|
|
let loaded = false; |
|
|
|
|
|
if (!isEmptyObj(project)) { |
|
|
this.storables.forEach((module) => module.clear()); |
|
|
this.storables.forEach((module) => module.clear()); |
|
|
this.storables.forEach((module) => module.load(data)); |
|
|
this.storables.forEach((module) => module.load(project)); |
|
|
|
|
|
loaded = true; |
|
|
} |
|
|
} |
|
|
return data; |
|
|
this.trigger(EditorEvents.projectLoad, { project, loaded, initial: !!opts.initial }); |
|
|
|
|
|
return project; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -1025,7 +1031,7 @@ export default class EditorModel extends Model { |
|
|
*/ |
|
|
*/ |
|
|
destroyAll() { |
|
|
destroyAll() { |
|
|
const { config, view } = this; |
|
|
const { config, view } = this; |
|
|
this.trigger('destroy'); |
|
|
this.trigger(EditorEvents.destroy); |
|
|
const editor = this.getEditor(); |
|
|
const editor = this.getEditor(); |
|
|
// @ts-ignore
|
|
|
// @ts-ignore
|
|
|
const { editors = [] } = config.grapesjs || {}; |
|
|
const { editors = [] } = config.grapesjs || {}; |
|
|
@ -1048,7 +1054,7 @@ export default class EditorModel extends Model { |
|
|
editors.splice(editors.indexOf(editor), 1); |
|
|
editors.splice(editors.indexOf(editor), 1); |
|
|
//@ts-ignore
|
|
|
//@ts-ignore
|
|
|
hasWin() && $(config.el).empty().attr(this.attrsOrig); |
|
|
hasWin() && $(config.el).empty().attr(this.attrsOrig); |
|
|
this.trigger('destroyed'); |
|
|
this.trigger(EditorEvents.destroyed); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
getEditing(): Component | undefined { |
|
|
getEditing(): Component | undefined { |
|
|
@ -1066,12 +1072,13 @@ export default class EditorModel extends Model { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
log(msg: string, opts: any = {}) { |
|
|
log(msg: string, opts: any = {}) { |
|
|
|
|
|
const logEvent = EditorEvents.log; |
|
|
const { ns, level = 'debug' } = opts; |
|
|
const { ns, level = 'debug' } = opts; |
|
|
this.trigger('log', msg, opts); |
|
|
this.trigger(logEvent, msg, opts); |
|
|
level && this.trigger(`log:${level}`, msg, opts); |
|
|
level && this.trigger(`${logEvent}:${level}`, msg, opts); |
|
|
|
|
|
|
|
|
if (ns) { |
|
|
if (ns) { |
|
|
const logNs = `log-${ns}`; |
|
|
const logNs = `${logEvent}-${ns}`; |
|
|
this.trigger(logNs, msg, opts); |
|
|
this.trigger(logNs, msg, opts); |
|
|
level && this.trigger(`${logNs}:${level}`, msg, opts); |
|
|
level && this.trigger(`${logNs}:${level}`, msg, opts); |
|
|
} |
|
|
} |
|
|
|