Browse Source

Update Editor API in docs

docs
Artur Arseniev 8 years ago
parent
commit
f2548f0e4a
  1. 2
      docs/.vuepress/config.js
  2. 9
      docs/README.md
  3. 28
      docs/api.js
  4. 143
      docs/api/editor.md
  5. 2
      docs/faq.md
  6. 67
      src/editor/index.js

2
docs/.vuepress/config.js

@ -36,7 +36,7 @@ module.exports = {
selectText: 'IT',
label: 'Italiano',
nav: [
{ text: 'Supporto ❤️', link: 'https://opencollective.com/grapesjs' },
{ text: 'Supportaci', link: 'https://opencollective.com/grapesjs' },
],
sidebar: [
'/',

9
docs/README.md

@ -17,15 +17,14 @@ Mainly, GrapesJS was designed to be used inside some CMS to speed up the creatio
## Demos
To show up of what GrapesJS is capable of we have created some presets to use as official demos.
## Quick Start
To show up of what GrapesJS is capable of we have created some presets.
* [grapesjs-preset-webpage](https://github.com/artf/grapesjs-preset-webpage) - [Webpage Builder](http://artf.github.io/grapesjs/demo.html)
* [grapesjs-preset-newsletter](https://github.com/artf/grapesjs-preset-newsletter) - [Newsletter Builder](http://artf.github.io/grapesjs/demo-newsletter-editor.html)
* [grapesjs-mjml](https://github.com/artf/grapesjs-mjml) - [Newsletter Builder with MJML](http://artf.github.io/grapesjs/demo-mjml.html)
You can actually use them as a starting point for your editors, so, just clone and extend them as you wish.
For a quick start with GrapesJS check the section below.
You can actually use them as a starting point for your editors, so, just follow the instructions on the repository to get a quick start for your builder.
@ -33,6 +32,8 @@ For a quick start with GrapesJS check the section below.
## Download
Latest version: [![npm](https://img.shields.io/npm/v/grapesjs.svg?colorB=e67891)](https://www.npmjs.com/package/grapesjs)
You can download GrapesJS from one of this sources
* CDNs

28
docs/api.js

@ -6,20 +6,20 @@ const srcRoot = path.join(docRoot, '../src/');
const binRoot = path.join(docRoot, '../node_modules/.bin/');
const cmds = [
['editor/index.js', 'editor.md'],
['asset_manager/index.js', 'assets.md'],
['block_manager/index.js', 'block_manager.md'],
['commands/index.js', 'commands.md'],
['dom_components/index.js', 'components.md'],
['panels/index.js', 'panels.md'],
['style_manager/index.js', 'style_manager.md'],
['storage_manager/index.js', 'storage_manager.md'],
['device_manager/index.js', 'device_manager.md'],
['selector_manager/index.js', 'selector_manager.md'],
['css_composer/index.js', 'css_composer.md'],
['modal_dialog/index.js', 'modal_dialog.md'],
['rich_text_editor/index.js', 'rich_text_editor.md'],
['keymaps/index.js', 'keymaps.md'],
['undo_manager/index.js', 'undo_manager.md'],
//['asset_manager/index.js', 'assets.md'],
//['block_manager/index.js', 'block_manager.md'],
//['commands/index.js', 'commands.md'],
//['dom_components/index.js', 'components.md'],
//['panels/index.js', 'panels.md'],
//['style_manager/index.js', 'style_manager.md'],
//['storage_manager/index.js', 'storage_manager.md'],
//['device_manager/index.js', 'device_manager.md'],
//['selector_manager/index.js', 'selector_manager.md'],
//['css_composer/index.js', 'css_composer.md'],
//['modal_dialog/index.js', 'modal_dialog.md'],
//['rich_text_editor/index.js', 'rich_text_editor.md'],
//['keymaps/index.js', 'keymaps.md'],
//['undo_manager/index.js', 'undo_manager.md'],
].map(entry =>
`${binRoot}documentation build ${srcRoot}/${entry[0]} -o ${docRoot}/api/${entry[1]} -f md --shallow --markdown-toc false`)
.join(' && ');

143
docs/api/editor.md

@ -1,17 +1,27 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
## $
## Editor
Editor class contains the top level API which you'll probably use to custom the editor or extend it with plugins.
You get the Editor instance on init method
Editor contains the top level API which you'll probably use to customize the editor or extend it with plugins.
You get the Editor instance on init method and you can pass options via its [Configuration Object][1]
```js
var editor = grapesjs.init({...});
const editor = grapesjs.init({
// options
});
```
# Available Events
## Available Events
You can make use of available events in this way
```js
editor.on('EVENT-NAME', (some, argument) => {
// do something
})
```
## Components
### Components
- `component:add` - Triggered when a new component is added to the editor, the model is passed as an argument to the callback
- `component:remove` - Triggered when a component is removed, the model is passed as an argument to the callback
@ -24,7 +34,7 @@ var editor = grapesjs.init({...});
- `component:deselected` - Component deselected, the deselected model is passed as an argument to the callback
- `component:toggled` - Component selection changed, toggled model is passed as an argument to the callback
## Blocks
### Blocks
- `block:add` - New block added
- `block:remove` - Block removed
@ -32,7 +42,7 @@ var editor = grapesjs.init({...});
- `block:drag` - Dragging block, the block's model and the drag event are passed as arguments
- `block:drag:stop` - Dragging of the block is stopped. As agruments for the callback you get, the dropped component model (if dropped successfully) and the model of the block
## Assets
### Assets
- `asset:add` - New asset added
- `asset:remove` - Asset removed
@ -41,19 +51,19 @@ var editor = grapesjs.init({...});
- `asset:upload:error` - On any error in upload, passes the error as an argument
- `asset:upload:response` - On upload response, passes the result as an argument
## Keymaps
### Keymaps
- `keymap:add` - New keymap added. The new keyamp object is passed as an argument
- `keymap:remove` - Keymap removed. The removed keyamp object is passed as an argument
- `keymap:emit` - Some keymap emitted, in arguments you get keymapId, shortcutUsed, Event
- `keymap:emit:{keymapId}` - `keymapId` emitted, in arguments you get keymapId, shortcutUsed, Event
## Style Manager
### Style Manager
- `styleManager:change` - Triggered on style property change from new selected component, the view of the property is passed as an argument to the callback
- `styleManager:change:{propertyName}` - As above but for a specific style property
## Storages
### Storages
- `storage:start` - Before the storage request is started
- `storage:start:store` - Before the store request. The object to store is passed as an argumnet (which you can edit)
@ -67,7 +77,7 @@ var editor = grapesjs.init({...});
- `storage:error:store` - Error on store request, passes the error as an argument
- `storage:error:load` - Error on load request, passes the error as an argument
## Canvas
### Canvas
- `canvas:dragenter` - When something is dragged inside the canvas, `DataTransfer` instance passed as an argument
- `canvas:dragover` - When something is dragging on canvas, `DataTransfer` instance passed as an argument
@ -76,16 +86,16 @@ var editor = grapesjs.init({...});
- `canvas:dragdata` - On any dataTransfer parse, `DataTransfer` instance and the `result` are passed as arguments.
By changing `result.content` you're able to customize what is dropped
## Selectors
### Selectors
- `selector:add` - Triggers when a new selector/class is created
## RTE
### RTE
- `rte:enable` - RTE enabled. The view, on which RTE is enabled, is passed as an argument
- `rte:disable` - RTE disabled. The view, on which RTE is disabled, is passed as an argument
## Commands
### Commands
- `run:{commandName}` - Triggered when some command is called to run (eg. editor.runCommand('preview'))
- `stop:{commandName}` - Triggered when some command is called to stop (eg. editor.stopCommand('preview'))
@ -93,48 +103,13 @@ var editor = grapesjs.init({...});
- `stop:{commandName}:before` - Triggered before the command is called to stop
- `abort:{commandName}` - Triggered when the command execution is aborted (`editor.on(`run:preview:before`, opts => opts.abort = 1);`)
## General
### General
- `canvasScroll` - Triggered when the canvas is scrolle
- `undo` - Undo executed
- `redo` - Redo executed
- `load` - When the editor is loaded
### Parameters
- `config` **[Object][1]** Configurations
- `config.container` **[string][2]** ='' Selector for the editor container, eg. '#myEditor'
- `config.components` **([string][2] \| [Array][3]&lt;[Object][1]>)** HTML string or object of components (optional, default `''`)
- `config.style` **([string][2] \| [Array][3]&lt;[Object][1]>)** CSS string or object of rules (optional, default `''`)
- `config.fromElement` **[Boolean][4]** If true, will fetch HTML and CSS from selected container (optional, default `false`)
- `config.undoManager` **[Boolean][4]** Enable/Disable undo manager (optional, default `true`)
- `config.autorender` **[Boolean][4]** If true renders editor on init (optional, default `true`)
- `config.noticeOnUnload` **[Boolean][4]** Enable/Disable alert message before unload the page (optional, default `true`)
- `config.height` **[string][2]** Height for the editor container (optional, default `'900px'`)
- `config.width` **[string][2]** Width for the editor container (optional, default `'100%'`)
- `config.storage` **[Object][1]** Storage manager configuration, see the relative documentation (optional, default `{}`)
- `config.styleManager` **[Object][1]** Style manager configuration, see the relative documentation (optional, default `{}`)
- `config.commands` **[Object][1]** Commands configuration, see the relative documentation (optional, default `{}`)
- `config.domComponents` **[Object][1]** Components configuration, see the relative documentation (optional, default `{}`)
- `config.panels` **[Object][1]** Panels configuration, see the relative documentation (optional, default `{}`)
- `config.showDevices` **[Object][1]** If true render a select of available devices inside style manager panel (optional, default `true`)
- `config.keepEmptyTextNodes` **[Boolean][4]** If false, removes empty text nodes when parsed (unless they contain a space) (optional, default `false`)
- `config.defaultCommand` **[string][2]** Command to execute when no other command is running (optional, default `'select-comp'`)
- `config.plugins` **[Array][3]** Array of plugins to execute on start (optional, default `[]`)
- `config.pluginsOpts` **[Object][1]** Custom options for plugins (optional, default `{}`)
### Examples
```javascript
var editor = grapesjs.init({
container : '#gjs',
components: '<div class="txt-red">Hello world!</div>',
style: '.txt-red{color: red}',
});
```
## AssetManager
## getConfig
Returns configuration object
@ -162,7 +137,7 @@ Returns CSS built inside canvas
### Parameters
- `opts` **[Object][1]** Options (optional, default `{}`)
- `opts` **[Object][3]** Options (optional, default `{}`)
Returns **[string][2]** CSS string
@ -176,7 +151,7 @@ Returns **[string][2]** JS string
Returns components in JSON format object
Returns **[Object][1]**
Returns **[Object][3]**
## setComponents
@ -184,7 +159,7 @@ Set components inside editor's canvas. This method overrides actual components
### Parameters
- `components` **([Array][3]&lt;[Object][1]> | [Object][1] \| [string][2])** HTML string or components model
- `components` **([Array][4]&lt;[Object][3]> | [Object][3] \| [string][2])** HTML string or components model
### Examples
@ -206,9 +181,9 @@ Add components
### Parameters
- `components` **([Array][3]&lt;[Object][1]> | [Object][1] \| [string][2])** HTML string or components model
- `opts` **[Object][1]** Options
- `opts.avoidUpdateStyle` **[Boolean][4]** If the HTML string contains styles,
- `components` **([Array][4]&lt;[Object][3]> | [Object][3] \| [string][2])** HTML string or components model
- `opts` **[Object][3]** Options
- `opts.avoidUpdateStyle` **[Boolean][5]** If the HTML string contains styles,
by default, they will be created and, if already exist, updated. When this option
is true, styles already created will not be updated. (optional, default `false`)
@ -224,13 +199,13 @@ editor.addComponents({
});
```
Returns **(Model | [Array][3]&lt;Model>)**
Returns **(Model | [Array][4]&lt;Model>)**
## getStyle
Returns style in JSON format object
Returns **[Object][1]**
Returns **[Object][3]**
## setStyle
@ -238,7 +213,7 @@ Set style inside editor's canvas. This method overrides actual style
### Parameters
- `style` **([Array][3]&lt;[Object][1]> | [Object][1] \| [string][2])** CSS string or style model
- `style` **([Array][4]&lt;[Object][3]> | [Object][3] \| [string][2])** CSS string or style model
### Examples
@ -263,7 +238,7 @@ Returns **Model**
Returns an array of all selected components
Returns **[Array][3]**
Returns **[Array][4]**
## getSelectedToStyle
@ -281,7 +256,7 @@ Select a component
### Parameters
- `el` **(Component | [HTMLElement][5])** Component to select
- `el` **(Component | [HTMLElement][6])** Component to select
### Examples
@ -300,7 +275,7 @@ Add component to selection
### Parameters
- `el` **(Component | [HTMLElement][5] \| [Array][3])** Component to select
- `el` **(Component | [HTMLElement][6] \| [Array][4])** Component to select
### Examples
@ -316,7 +291,7 @@ Remove component from selection
### Parameters
- `el` **(Component | [HTMLElement][5] \| [Array][3])** Component to select
- `el` **(Component | [HTMLElement][6] \| [Array][4])** Component to select
### Examples
@ -332,7 +307,7 @@ Toggle component selection
### Parameters
- `el` **(Component | [HTMLElement][5] \| [Array][3])** Component to select
- `el` **(Component | [HTMLElement][6] \| [Array][4])** Component to select
### Examples
@ -380,7 +355,7 @@ Execute command
### Parameters
- `id` **[string][2]** Command ID
- `options` **[Object][1]** Custom options (optional, default `{}`)
- `options` **[Object][3]** Custom options (optional, default `{}`)
### Examples
@ -397,7 +372,7 @@ Stop the command if stop method was provided
### Parameters
- `id` **[string][2]** Command ID
- `options` **[Object][1]** Custom options (optional, default `{}`)
- `options` **[Object][3]** Custom options (optional, default `{}`)
### Examples
@ -413,9 +388,9 @@ Store data to the current storage
### Parameters
- `clb` **[Function][6]** Callback function
- `clb` **[Function][7]** Callback function
Returns **[Object][1]** Stored data
Returns **[Object][3]** Stored data
## load
@ -423,23 +398,23 @@ Load data from the current storage
### Parameters
- `clb` **[Function][6]** Callback function
- `clb` **[Function][7]** Callback function
Returns **[Object][1]** Stored data
Returns **[Object][3]** Stored data
## getContainer
Returns container element. The one which was indicated as 'container'
on init method
Returns **[HTMLElement][5]**
Returns **[HTMLElement][6]**
## getDirtyCount
Return the count of changes made to the content and not yet stored.
This count resets at any `store()`
Returns **[number][7]**
Returns **[number][8]**
## setCustomRte
@ -447,7 +422,7 @@ Replace the built-in Rich Text Editor with a custom one.
### Parameters
- `obj` **[Object][1]** Custom RTE Interface
- `obj` **[Object][3]** Custom RTE Interface
### Examples
@ -485,7 +460,7 @@ Attach event
### Parameters
- `event` **[string][2]** Event name
- `callback` **[Function][6]** Callback function
- `callback` **[Function][7]** Callback function
Returns **this**
@ -496,7 +471,7 @@ Detach event
### Parameters
- `event` **[string][2]** Event name
- `callback` **[Function][6]** Callback function
- `callback` **[Function][7]** Callback function
Returns **this**
@ -518,18 +493,20 @@ Destroy the editor
Render editor
Returns **[HTMLElement][5]**
Returns **[HTMLElement][6]**
[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[1]: https://github.com/artf/grapesjs/blob/master/src/editor/config/config.js
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
[5]: https://developer.mozilla.org/docs/Web/HTML/Element
[6]: https://developer.mozilla.org/docs/Web/HTML/Element
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number

2
docs/faq.md

@ -3,3 +3,5 @@ title: Faq
---
# FAQ
Coming soon

67
src/editor/index.js

@ -1,14 +1,23 @@
/**
* Editor class contains the top level API which you'll probably use to custom the editor or extend it with plugins.
* You get the Editor instance on init method
* Editor contains the top level API which you'll probably use to customize the editor or extend it with plugins.
* You get the Editor instance on init method and you can pass options via its [Configuration Object](https://github.com/artf/grapesjs/blob/master/src/editor/config/config.js)
*
* ```js
* var editor = grapesjs.init({...});
* const editor = grapesjs.init({
* // options
* });
* ```
*
* # Available Events
* ## Available Events
*
* You can make use of available events in this way
* ```js
* editor.on('EVENT-NAME', (some, argument) => {
* // do something
* })
* ```
*
* ## Components
* ### Components
* * `component:add` - Triggered when a new component is added to the editor, the model is passed as an argument to the callback
* * `component:remove` - Triggered when a component is removed, the model is passed as an argument to the callback
* * `component:clone` - Triggered when a new component is added by a clone command, the model is passed as an argument to the callback
@ -19,28 +28,28 @@
* * `component:selected` - New component selected, the selected model is passed as an argument to the callback
* * `component:deselected` - Component deselected, the deselected model is passed as an argument to the callback
* * `component:toggled` - Component selection changed, toggled model is passed as an argument to the callback
* ## Blocks
* ### Blocks
* * `block:add` - New block added
* * `block:remove` - Block removed
* * `block:drag:start` - Started dragging block, model of the block is passed as an argument
* * `block:drag` - Dragging block, the block's model and the drag event are passed as arguments
* * `block:drag:stop` - Dragging of the block is stopped. As agruments for the callback you get, the dropped component model (if dropped successfully) and the model of the block
* ## Assets
* ### Assets
* * `asset:add` - New asset added
* * `asset:remove` - Asset removed
* * `asset:upload:start` - Before the upload is started
* * `asset:upload:end` - After the upload is ended
* * `asset:upload:error` - On any error in upload, passes the error as an argument
* * `asset:upload:response` - On upload response, passes the result as an argument
* ## Keymaps
* ### Keymaps
* * `keymap:add` - New keymap added. The new keyamp object is passed as an argument
* * `keymap:remove` - Keymap removed. The removed keyamp object is passed as an argument
* * `keymap:emit` - Some keymap emitted, in arguments you get keymapId, shortcutUsed, Event
* * `keymap:emit:{keymapId}` - `keymapId` emitted, in arguments you get keymapId, shortcutUsed, Event
* ## Style Manager
* ### Style Manager
* * `styleManager:change` - Triggered on style property change from new selected component, the view of the property is passed as an argument to the callback
* * `styleManager:change:{propertyName}` - As above but for a specific style property
* ## Storages
* ### Storages
* * `storage:start` - Before the storage request is started
* * `storage:start:store` - Before the store request. The object to store is passed as an argumnet (which you can edit)
* * `storage:start:load` - Before the load request. Items to load are passed as an argumnet (which you can edit)
@ -52,56 +61,31 @@
* * `storage:error` - On any error on storage request, passes the error as an argument
* * `storage:error:store` - Error on store request, passes the error as an argument
* * `storage:error:load` - Error on load request, passes the error as an argument
* ## Canvas
* ### Canvas
* * `canvas:dragenter` - When something is dragged inside the canvas, `DataTransfer` instance passed as an argument
* * `canvas:dragover` - When something is dragging on canvas, `DataTransfer` instance passed as an argument
* * `canvas:drop` - Something is dropped in canvas, `DataTransfer` instance and the dropped model are passed as arguments
* * `canvas:dragend` - When a drag operation is ended, `DataTransfer` instance passed as an argument
* * `canvas:dragdata` - On any dataTransfer parse, `DataTransfer` instance and the `result` are passed as arguments.
* By changing `result.content` you're able to customize what is dropped
* ## Selectors
* ### Selectors
* * `selector:add` - Triggers when a new selector/class is created
* ## RTE
* ### RTE
* * `rte:enable` - RTE enabled. The view, on which RTE is enabled, is passed as an argument
* * `rte:disable` - RTE disabled. The view, on which RTE is disabled, is passed as an argument
* ## Commands
* ### Commands
* * `run:{commandName}` - Triggered when some command is called to run (eg. editor.runCommand('preview'))
* * `stop:{commandName}` - Triggered when some command is called to stop (eg. editor.stopCommand('preview'))
* * `run:{commandName}:before` - Triggered before the command is called
* * `stop:{commandName}:before` - Triggered before the command is called to stop
* * `abort:{commandName}` - Triggered when the command execution is aborted (`editor.on(`run:preview:before`, opts => opts.abort = 1);`)
* ## General
* ### General
* * `canvasScroll` - Triggered when the canvas is scrolle
* * `undo` - Undo executed
* * `redo` - Redo executed
* * `load` - When the editor is loaded
*
* @param {Object} config Configurations
* @param {string} config.container='' Selector for the editor container, eg. '#myEditor'
* @param {string|Array<Object>} [config.components=''] HTML string or object of components
* @param {string|Array<Object>} [config.style=''] CSS string or object of rules
* @param {Boolean} [config.fromElement=false] If true, will fetch HTML and CSS from selected container
* @param {Boolean} [config.undoManager=true] Enable/Disable undo manager
* @param {Boolean} [config.autorender=true] If true renders editor on init
* @param {Boolean} [config.noticeOnUnload=true] Enable/Disable alert message before unload the page
* @param {string} [config.height='900px'] Height for the editor container
* @param {string} [config.width='100%'] Width for the editor container
* @param {Object} [config.storage={}] Storage manager configuration, see the relative documentation
* @param {Object} [config.styleManager={}] Style manager configuration, see the relative documentation
* @param {Object} [config.commands={}] Commands configuration, see the relative documentation
* @param {Object} [config.domComponents={}] Components configuration, see the relative documentation
* @param {Object} [config.panels={}] Panels configuration, see the relative documentation
* @param {Object} [config.showDevices=true] If true render a select of available devices inside style manager panel
* @param {Boolean} [config.keepEmptyTextNodes=false] If false, removes empty text nodes when parsed (unless they contain a space)
* @param {string} [config.defaultCommand='select-comp'] Command to execute when no other command is running
* @param {Array} [config.plugins=[]] Array of plugins to execute on start
* @param {Object} [config.pluginsOpts={}] Custom options for plugins
* @example
* var editor = grapesjs.init({
* container : '#gjs',
* components: '<div class="txt-red">Hello world!</div>',
* style: '.txt-red{color: red}',
* });
* @module Editor
*/
import $ from 'cash-dom';
@ -157,6 +141,7 @@ module.exports = config => {
/**
* @property {AssetManager}
* @private
*/
AssetManager: em.get('AssetManager'),

Loading…
Cancel
Save