Skip to content

Commit

Permalink
Start building the init method
Browse files Browse the repository at this point in the history
  • Loading branch information
artf committed Dec 10, 2017
1 parent dde00d9 commit faa7fb8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 24 deletions.
8 changes: 6 additions & 2 deletions src/dom_components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*/
module.exports = () => {
var c = {};
let em;
const defaults = require('./config/config');
const Component = require('./model/Component');
const ComponentView = require('./view/ComponentView');
Expand Down Expand Up @@ -166,7 +167,7 @@ module.exports = () => {
*/
init(config) {
c = config || {};
const em = c.em;
em = c.em;

if (em) {
c.components = em.config.components || c.components;
Expand Down Expand Up @@ -228,7 +229,10 @@ module.exports = () => {
* @private
*/
onLoad() {
this.getComponents().reset(c.components);
const comps = this.getComponents();
comps.reset(c.components);
const um = em && em.get('UndoManager');
um && um.add(comps);
},

/**
Expand Down
6 changes: 3 additions & 3 deletions src/editor/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ module.exports = {
// Show an alert before unload the page with unsaved changes
noticeOnUnload: true,

// Enable/Disable undo manager
undoManager: true,

// Show paddings and margins
showOffsets: false,

Expand Down Expand Up @@ -80,6 +77,9 @@ module.exports = {
// Dom element
el: '',

// Configurations for Undo Manager
undoManager: {},

//Configurations for Asset Manager
assetManager: {},

Expand Down
12 changes: 6 additions & 6 deletions src/editor/model/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { isUndefined, defaults } from 'underscore';
const deps = [
require('utils'),
require('keymaps'),
require('undo_manager'),
require('storage_manager'),
require('device_manager'),
require('parser'),
Expand Down Expand Up @@ -58,9 +59,7 @@ module.exports = Backbone.Model.extend({

// Load modules
deps.forEach(name => this.loadModule(name));

this.initUndoManager();

//this.initUndoManager();
this.on('change:selectedComponent', this.componentSelected, this);
this.on('change:changesCount', this.updateChanges, this);
},
Expand Down Expand Up @@ -97,8 +96,8 @@ module.exports = Backbone.Model.extend({
// I've initialized undo manager in initialize() because otherwise the
// 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
this.um.clear();
this.initUndoManager();
//this.um && this.um.clear();
//this.initUndoManager();
this.get('modules').forEach(module =>
module.postLoad && module.postLoad(this)
);
Expand Down Expand Up @@ -231,7 +230,7 @@ module.exports = Backbone.Model.extend({
/**
* Initialize Undo manager
* @private
* */
* *
initUndoManager() {
const canvas = this.get('Canvas');
Expand Down Expand Up @@ -304,6 +303,7 @@ module.exports = Backbone.Model.extend({
UndoManager.addUndoType("change:src", customUndoType);
}
},
*/

/**
* Callback on component selection
Expand Down
3 changes: 0 additions & 3 deletions src/grapesjs/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ module.exports = {
// Enable/Disable the possibility to copy(ctrl + c) & paste(ctrl + v) components
copyPaste: true,

// Enable/Disable undo manager
undoManager: true,

// Storage Manager
storageManager: {},

Expand Down
74 changes: 64 additions & 10 deletions src/undo_manager/index.js
Original file line number Diff line number Diff line change
@@ -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
* ```js
Expand All @@ -10,8 +10,10 @@
import UndoManager from 'backbone-undo';

module.exports = () => {
let em;
let config;
let um;
let beforeCache;
const configDef = {};
const keymaps = {};

Expand Down Expand Up @@ -100,8 +102,42 @@ module.exports = () => {
*/
init(opts = {}) {
config = { ...opts, ...configDef };
this.em = config.em;
em = config.em;
this.em = em;
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;
},

Expand Down Expand Up @@ -145,47 +181,65 @@ module.exports = () => {
* Start/resume tracking changes
*/
start() {
em.startTracking();
um.startTracking();
},


/**
* Stop tracking changes
*/
stop() {
em.stopTracking();
um.stopTracking();
},


/**
* Undo last change
*/
undo() {
em.undo(1);
um.undo(1);
},


/**
* Undo all changes
*/
undoAll() {
em.undoAll();
um.undoAll();
},


/**
* Redo last change
*/
redo() {
em.redo(1);
um.redo(1);
},


/**
* Redo all changes
*/
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');
},


Expand All @@ -194,14 +248,14 @@ module.exports = () => {
* @return {Array}
*/
getStack() {

return um.stack;
},

/**
* Clear the stack
*/
clear() {

um.clear();
}
};
};

0 comments on commit faa7fb8

Please sign in to comment.