diff --git a/package.json b/package.json index 9d90e169b..4e77b5bb4 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "url": "https://github.com/artf/grapesjs.git" }, "dependencies": { - "backbone": "1.3.3", + "backbone": "1.4.1", "backbone-undo": "^0.2.5", "codemirror": "^5.63.0", "codemirror-formatting": "^1.0.0", diff --git a/src/abstract/Collection.ts b/src/abstract/Collection.ts index 2a4bd4c2d..e0d25542b 100644 --- a/src/abstract/Collection.ts +++ b/src/abstract/Collection.ts @@ -1,6 +1,33 @@ -import Backbone from 'backbone'; +import Backbone, { AddOptions } from 'backbone'; +import { isArray, isObject } from 'underscore'; import Model from './Model'; -export default class Collection< - TModel extends Model = Model -> extends Backbone.Collection {} +type Module = TModel extends Model ? M : unknown; +type ModelConstructor = { new (mod: Module, attr: any): TModel }; + +export default class Collection extends Backbone.Collection { + module!: Module; + private newModel!: { new (mod: Module, attr: any): TModel }; + + //modelConstructor = {new (mod: Module, attr: any): TModel} + add(model: Array> | TModel, options?: AddOptions): TModel; + add(models: Array> | TModel>, options?: AddOptions): TModel[]; + add(model: unknown, options?: AddOptions): any { + var models = isArray(model) ? model : [model]; + models = models.map(m => (m instanceof this.newModel ? m : new this.newModel(this.module, m))); + return super.add(isArray(model) ? models : models[0], options); + } + + constructor( + module: Module, + models: TModel[] | Array>, + modelConstructor: ModelConstructor + ) { + super(models, { module, modelConstructor }); + } + + preinitialize(models?: TModel[] | Array>, options?: any) { + this.newModel = options.modelConstructor; + this.module = options.module; + } +} diff --git a/src/panels/index.ts b/src/panels/index.ts index 97ba4287c..c3cfb5411 100644 --- a/src/panels/index.ts +++ b/src/panels/index.ts @@ -28,11 +28,12 @@ import { Module } from '../abstract'; import EditorModel from '../editor/model/Editor'; import defaults from './config/config'; +import Button from './model/Button'; import Panel from './model/Panel'; import Panels from './model/Panels'; import PanelsView from './view/PanelsView'; -export default class PanelsManager extends Module { +export default class PanelManager extends Module { //config = {}; panels: Panels; PanelsViewObj?: PanelsView; @@ -44,7 +45,7 @@ export default class PanelsManager extends Module { */ constructor(em: EditorModel) { super(em, 'Panels', defaults); - this.panels = new Panels(this.config.defaults); + this.panels = new Panels(this, this.config.defaults); for (var name in defaults) { //@ts-ignore if (!(name in this.config)) this.config[name] = defaults[name]; @@ -79,7 +80,7 @@ export default class PanelsManager extends Module { * buttons : [...], * }); */ - addPanel(panel: Panel) { + addPanel(panel: Panel | Array>) { return this.panels.add(panel); } diff --git a/src/panels/model/Button.ts b/src/panels/model/Button.ts index 71c6e3967..667f2829a 100644 --- a/src/panels/model/Button.ts +++ b/src/panels/model/Button.ts @@ -1,7 +1,9 @@ -import { Model } from '../../common'; +import PanelManager from '..'; +import { Model } from '../../abstract'; +import EditorModel from '../../editor/model/Editor'; import Buttons from './Buttons'; -export default class Button extends Model { +export default class Button extends Model { defaults() { return { id: '', @@ -22,10 +24,10 @@ export default class Button extends Model { }; } - constructor(options: any) { - super(options); + constructor(module: PanelManager, options: any) { + super(module, options); if (this.get('buttons').length) { - this.set('buttons', new Buttons(this.get('buttons'))); + this.set('buttons', new Buttons(this.module, this.get('buttons'))); } } } diff --git a/src/panels/model/Buttons.ts b/src/panels/model/Buttons.ts index ff430a352..64c2e0305 100644 --- a/src/panels/model/Buttons.ts +++ b/src/panels/model/Buttons.ts @@ -1,7 +1,11 @@ -import { Collection } from '../../common'; +import PanelManager from '..'; +import { Collection } from '../../abstract'; import Button from './Button'; export default class Buttons extends Collection