forked from GrapesJS/grapesjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
446 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
define(function () { | ||
return { | ||
|
||
'blocks': [], | ||
|
||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* * [add](#add) | ||
* * [get](#get) | ||
* * [getAll](#getall) | ||
* * [render](#render) | ||
* | ||
* Block manager helps managing various, draggable, pieces of content that could be reused inside templates. | ||
* | ||
* Before using methods you should get first the module from the editor instance, in this way: | ||
* | ||
* ```js | ||
* var blockManager = editor.BlockManager; | ||
* ``` | ||
* | ||
* @module BlockManager | ||
* @param {Object} config Configurations | ||
* @param {Array<Object>} [config.blocks=[]] Default blocks | ||
* @example | ||
* ... | ||
* blockManager: { | ||
* blocks: [ | ||
* {id:'h1-block' label: 'Heading', content:'<h1>...</h1>'}, | ||
* ... | ||
* ], | ||
* } | ||
* ... | ||
*/ | ||
define(function(require) { | ||
|
||
return function(config) { | ||
var c = config || {}, | ||
defaults = require('./config/config'), | ||
Blocks = require('./model/Blocks'), | ||
BlocksView = require('./view/BlocksView'); | ||
|
||
for (var name in defaults) { | ||
if (!(name in c)) | ||
c[name] = defaults[name]; | ||
} | ||
|
||
var blocks = new Blocks(c.blocks); | ||
var view = new BlocksView({ collection: blocks }, c); | ||
|
||
return { | ||
|
||
/** | ||
* Add new block to the collection. | ||
* @param {string} id Block id | ||
* @param {Object} opts Options | ||
* @param {string} opts.label Name of the block | ||
* @param {string} opts.content HTML content | ||
* @param {Object} [opts.attributes={}] Block attributes | ||
* @return {Block} Added block | ||
* @example | ||
* blockManager.add('h1-block', { | ||
* label: 'Heading', | ||
* content: '<h1>Put your title here</h1>', | ||
* attributes: { | ||
* title: 'Insert h1 block' | ||
* } | ||
* }); | ||
*/ | ||
add: function(id, opts){ | ||
var obj = opts || {}; | ||
obj.id = id; | ||
return blocks.add(obj); | ||
}, | ||
|
||
/** | ||
* Return block by id | ||
* @param {string} id Block id | ||
* @example | ||
* var block = blockManager.get('h1-block'); | ||
* console.log(JSON.stringify(block)); | ||
* // {label: 'Heading', content: '<h1>Put your ...', ...} | ||
*/ | ||
get: function(id){ | ||
return blocks.get(id); | ||
}, | ||
|
||
/** | ||
* Return all blocks | ||
* @return {Collection} | ||
* @example | ||
* var blocks = blockManager.getAll(); | ||
* console.log(JSON.stringify(blocks)); | ||
* // [{label: 'Heading', content: '<h1>Put your ...'}, ...] | ||
*/ | ||
getAll: function(){ | ||
return blocks; | ||
}, | ||
|
||
/** | ||
* Render blocks | ||
* @return {HTMLElement} | ||
*/ | ||
render: function(){ | ||
return view.render().el; | ||
}, | ||
|
||
}; | ||
|
||
}; | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
define(['backbone'], | ||
function(Backbone){ | ||
|
||
return Backbone.Model.extend({ | ||
|
||
defaults :{ | ||
label: '', | ||
content: '', | ||
attributes: {}, | ||
}, | ||
|
||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
define(['backbone','./Block'], | ||
function (Backbone, Block) { | ||
|
||
return Backbone.Collection.extend({ | ||
|
||
model: Block, | ||
|
||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
define(['backbone'], | ||
function(Backbone) { | ||
|
||
return Backbone.View.extend({ | ||
|
||
initialize: function(o) { | ||
this.config = o.config || {}; | ||
this.listenTo(this.model, 'destroy', this.remove); | ||
}, | ||
|
||
render: function() { | ||
this.el.innerHTML = this.model.get('label'); | ||
return this; | ||
}, | ||
|
||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
define(['backbone', './BlockView'], | ||
function(Backbone, BlockView) { | ||
|
||
return Backbone.View.extend({ | ||
|
||
initialize: function(opts, config) { | ||
this.config = config || {}; | ||
this.ppfx = this.config.pStylePrefix || ''; | ||
this.listenTo(this.collection, 'add', this.addTo); | ||
}, | ||
|
||
/** | ||
* Add new model to the collection | ||
* @param {Model} model | ||
* @private | ||
* */ | ||
addTo: function(model){ | ||
this.add(model); | ||
}, | ||
|
||
/** | ||
* Render new model inside the view | ||
* @param {Model} model | ||
* @param {Object} fragment Fragment collection | ||
* */ | ||
add: function(model, fragment){ | ||
var frag = fragment || null; | ||
var view = new BlockView({ | ||
model: model, | ||
attributes: model.get('attributes'), | ||
}, this.config); | ||
var rendered = view.render().el; | ||
|
||
if(frag) | ||
frag.appendChild(rendered); | ||
else | ||
this.$el.append(rendered); | ||
}, | ||
|
||
|
||
|
||
render: function() { | ||
var frag = document.createDocumentFragment(); | ||
this.$el.empty(); | ||
|
||
this.collection.each(function(model){ | ||
this.add(model, frag); | ||
}, this); | ||
|
||
this.$el.append(frag); | ||
this.$el.addClass(this.ppfx + 'blocks-c'); | ||
return this; | ||
}, | ||
|
||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
define(function() { | ||
|
||
return { | ||
|
||
run: function(editor, sender) { | ||
var config = editor.Config; | ||
var pfx = config.stylePrefix; | ||
var bm = editor.BlockManager; | ||
if(!this.blocks){ | ||
this.blocks = bm.render(); | ||
var panels = editor.Panels; | ||
if(!panels.getPanel('views-container')) | ||
panelC = panels.addPanel({id: 'views-container'}); | ||
else | ||
panelC = panels.getPanel('views-container'); | ||
panelC.set('appendContent', this.blocks).trigger('change:appendContent'); | ||
} | ||
|
||
this.blocks.style.display = 'block'; | ||
}, | ||
|
||
stop: function() { | ||
if(this.blocks) | ||
this.blocks.style.display = 'none'; | ||
} | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.