Browse Source

Start building the init method

pull/663/head
Artur Arseniev 9 years ago
parent
commit
faa7fb839a
  1. 8
      src/dom_components/index.js
  2. 6
      src/editor/config/config.js
  3. 12
      src/editor/model/Editor.js
  4. 3
      src/grapesjs/config/config.js
  5. 74
      src/undo_manager/index.js

8
src/dom_components/index.js

@ -34,6 +34,7 @@
*/ */
module.exports = () => { module.exports = () => {
var c = {}; var c = {};
let em;
const defaults = require('./config/config'); const defaults = require('./config/config');
const Component = require('./model/Component'); const Component = require('./model/Component');
const ComponentView = require('./view/ComponentView'); const ComponentView = require('./view/ComponentView');
@ -166,7 +167,7 @@ module.exports = () => {
*/ */
init(config) { init(config) {
c = config || {}; c = config || {};
const em = c.em; em = c.em;
if (em) { if (em) {
c.components = em.config.components || c.components; c.components = em.config.components || c.components;
@ -228,7 +229,10 @@ module.exports = () => {
* @private * @private
*/ */
onLoad() { onLoad() {
this.getComponents().reset(c.components); const comps = this.getComponents();
comps.reset(c.components);
const um = em && em.get('UndoManager');
um && um.add(comps);
}, },
/** /**

6
src/editor/config/config.js

@ -11,9 +11,6 @@ module.exports = {
// Show an alert before unload the page with unsaved changes // Show an alert before unload the page with unsaved changes
noticeOnUnload: true, noticeOnUnload: true,
// Enable/Disable undo manager
undoManager: true,
// Show paddings and margins // Show paddings and margins
showOffsets: false, showOffsets: false,
@ -80,6 +77,9 @@ module.exports = {
// Dom element // Dom element
el: '', el: '',
// Configurations for Undo Manager
undoManager: {},
//Configurations for Asset Manager //Configurations for Asset Manager
assetManager: {}, assetManager: {},

12
src/editor/model/Editor.js

@ -3,6 +3,7 @@ import { isUndefined, defaults } from 'underscore';
const deps = [ const deps = [
require('utils'), require('utils'),
require('keymaps'), require('keymaps'),
require('undo_manager'),
require('storage_manager'), require('storage_manager'),
require('device_manager'), require('device_manager'),
require('parser'), require('parser'),
@ -58,9 +59,7 @@ module.exports = Backbone.Model.extend({
// Load modules // Load modules
deps.forEach(name => this.loadModule(name)); deps.forEach(name => this.loadModule(name));
//this.initUndoManager();
this.initUndoManager();
this.on('change:selectedComponent', this.componentSelected, this); this.on('change:selectedComponent', this.componentSelected, this);
this.on('change:changesCount', this.updateChanges, this); this.on('change:changesCount', this.updateChanges, this);
}, },
@ -97,8 +96,8 @@ module.exports = Backbone.Model.extend({
// I've initialized undo manager in initialize() because otherwise the // I've initialized undo manager in initialize() because otherwise the
// editor will unable to fetch the instance via 'editor.UndoManager' but // editor will unable to fetch the instance via 'editor.UndoManager' but
// I need to cleare the stack now as it was dirtied by 'onLoad' method // I need to cleare the stack now as it was dirtied by 'onLoad' method
this.um.clear(); //this.um && this.um.clear();
this.initUndoManager(); //this.initUndoManager();
this.get('modules').forEach(module => this.get('modules').forEach(module =>
module.postLoad && module.postLoad(this) module.postLoad && module.postLoad(this)
); );
@ -231,7 +230,7 @@ module.exports = Backbone.Model.extend({
/** /**
* Initialize Undo manager * Initialize Undo manager
* @private * @private
* */ * *
initUndoManager() { initUndoManager() {
const canvas = this.get('Canvas'); const canvas = this.get('Canvas');
@ -304,6 +303,7 @@ module.exports = Backbone.Model.extend({
UndoManager.addUndoType("change:src", customUndoType); UndoManager.addUndoType("change:src", customUndoType);
} }
}, },
*/
/** /**
* Callback on component selection * Callback on component selection

3
src/grapesjs/config/config.js

@ -18,9 +18,6 @@ module.exports = {
// Enable/Disable the possibility to copy(ctrl + c) & paste(ctrl + v) components // Enable/Disable the possibility to copy(ctrl + c) & paste(ctrl + v) components
copyPaste: true, copyPaste: true,
// Enable/Disable undo manager
undoManager: true,
// Storage Manager // Storage Manager
storageManager: {}, storageManager: {},

74
src/undo_manager/index.js

@ -1,5 +1,5 @@
/** /**
* This module allows to create shortcuts for functions and commands (via command id) * This module allows to manage the stack of changes applied on canvas
* *
* You can access the module in this way * You can access the module in this way
* ```js * ```js
@ -10,8 +10,10 @@
import UndoManager from 'backbone-undo'; import UndoManager from 'backbone-undo';
module.exports = () => { module.exports = () => {
let em;
let config; let config;
let um; let um;
let beforeCache;
const configDef = {}; const configDef = {};
const keymaps = {}; const keymaps = {};
@ -100,8 +102,42 @@ module.exports = () => {
*/ */
init(opts = {}) { init(opts = {}) {
config = { ...opts, ...configDef }; config = { ...opts, ...configDef };
this.em = config.em; em = config.em;
this.em = em;
um = new UndoManager({ track: true, register: [] }); um = new UndoManager({ track: true, register: [] });
um.changeUndoType('change', { condition: false });
const updated = () => em.trigger('change:selectedComponent');
const customUndoType = {
on(object, value, opt = {}) {
!beforeCache && (beforeCache = object.previousAttributes());
if (opt.avoidStore) {
return;
} else {
const result = {
object,
before: beforeCache,
after: object.toJSON()
};
beforeCache = null;
return result;
}
},
undo(model, bf, af, opt) {
model.set(bf);
updated();
},
redo(model, bf, af, opt) {
model.set(af);
updated();
}
};
const events = ['style', 'attributes', 'content', 'src'];
events.forEach(ev => um.addUndoType(`change:${ev}`, customUndoType));
return this; return this;
}, },
@ -145,7 +181,7 @@ module.exports = () => {
* Start/resume tracking changes * Start/resume tracking changes
*/ */
start() { start() {
em.startTracking(); um.startTracking();
}, },
@ -153,7 +189,7 @@ module.exports = () => {
* Stop tracking changes * Stop tracking changes
*/ */
stop() { stop() {
em.stopTracking(); um.stopTracking();
}, },
@ -161,7 +197,7 @@ module.exports = () => {
* Undo last change * Undo last change
*/ */
undo() { undo() {
em.undo(1); um.undo(1);
}, },
@ -169,7 +205,7 @@ module.exports = () => {
* Undo all changes * Undo all changes
*/ */
undoAll() { undoAll() {
em.undoAll(); um.undoAll();
}, },
@ -177,7 +213,7 @@ module.exports = () => {
* Redo last change * Redo last change
*/ */
redo() { redo() {
em.redo(1); um.redo(1);
}, },
@ -185,7 +221,25 @@ module.exports = () => {
* Redo all changes * Redo all changes
*/ */
redoAll() { redoAll() {
em.redoAll(); um.redoAll();
},
/**
* Checks if there is an available undo
* @return {Boolean}
*/
hasUndo() {
return um.isAvailable('undo');
},
/**
* Checks if there is an available redo
* @return {Boolean}
*/
hasRedo() {
return um.isAvailable('redo');
}, },
@ -194,14 +248,14 @@ module.exports = () => {
* @return {Array} * @return {Array}
*/ */
getStack() { getStack() {
return um.stack;
}, },
/** /**
* Clear the stack * Clear the stack
*/ */
clear() { clear() {
um.clear();
} }
}; };
}; };

Loading…
Cancel
Save