There are 2 steps to add your custom component:
- Add a custom snippet in
examples/snippets/snippets.html
- Add component handler file
The following snippet is for component with type is x
.
<div data-type="component-x" data-preview="/path/to/preview/of/snippet" data-keditor-title="Text block" data-keditor-categories="Text;Heading">
<div class="page-header">
<h1 style="margin-bottom: 30px; font-size: 50px;"><b class="text-uppercase">Cras justo odio</b> <small>Donec id elit non mi</small></h1>
<p class="lead"><em>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</em></p>
</div>
</div>
Details:
data-type
should becomponent-${name}
. Example:component-x
forx
componentdata-preview
is url of preview image for componentdata-keditor-title
is text title for component. Will be showed under preview image in snippet modaldata-keditor-categories
is list of categories for component. Used for filter and search in snippet modal
You need to do following steps to add your component handler file:
- Create a file inside
src/components/
folder - Import your file in
src/components/index.js
file - Add content for your file as following structure:
import KEditor from 'keditor';
KEditor.components['x'] = {
/**
* Function will be called when initializing a component with this type
* @param {jQuery} contentArea
* @param {jQuery} container
* @param {jQuery} component
* @param {KEditor} keditor KEditor instance which is calling this function
*/
init: function (contentArea, container, component, keditor) {
},
/**
* Function will be called for getting content of component from method of KEditor `target.keditor('getContent')`
* @param {jQuery} component This component is cloned from original component. So you can do anything with it, event deleted
* @param {KEditor} keditor KEditor instance which is calling this function
* @return {String}
*/
getContent: function (component, keditor) {
},
/**
* Function will be called when deleting component
* @param {jQuery} component
* @param {KEditor} keditor KEditor instance which is calling this function
*/
destroy: function (component, keditor) {
},
// Enable setting panel for this type or not
settingEnabled: true,
// Title of setting panel
settingTitle: 'X component',
/**
* Initialize setting form of this type
* @param {jQuery} form Form contains all setting of this type and is child of `div[id="keditor-setting-forms"]`
* @param {KEditor} keditor KEditor instance which is calling this function
*/
initSettingForm: function (form, keditor) {
},
/**
* Show setting form for this type. This function will be called when user clicks on setting button of component when setting panel is hidden. You can fulfill form controls in this function.
* @param {jQuery} form Form contains all setting of this type and is child of `div[id="keditor-setting-forms"]`
* @param {jQuery} component Component will be applied setting
* @param {KEditor} keditor KEditor instance which is calling this function
*/
showSettingForm: function (form, component, keditor) {
},
/**
* Hide setting form for this type. This function will be called when user clicks again on setting button of component when setting panel is showed. You can clear setting form in this function
* @param {jQuery} form Form contains all setting of this type and is child of `div[id="keditor-setting-forms"]`
* @param {KEditor} keditor KEditor instance which is calling this function
*/
hideSettingForm: function (form, keditor) {
}
};
Note: KEditor.getSettingComponent()
is method for getting which component is setting