Ckeditor add Custom button

Source: Internet
Author: User

1. Download ckeditor. Here we download ckeditor 3.6.2.

2. There are compressed code and source code in it. The directory where the source code is located is _ source. There are some examples for use.

In this section, you need to add a Custom button during use.

 

2. For example, I want to add the "insert code" button and name it code. Create a new codefolder under ckeditor/plugins, add a small image such as code.gif to the codefolder, and then create a plugin. js file in the code folder. The content is as follows:

// One-click Layout
(Function (){

B = 'format _ wz ';

Ckeditor. plugins. Add (B ,{

Requires: ['styles ', 'click'],

Init: function (){

A. addcommand (B, ckeditor. plugins. AutoFormat. commands. AutoFormat );

A. UI. addbutton ('format _ wz ',{

Label: "one-click layout ",

Command: 'format _ wz ',

Icon: This. Path + "format_wz.png"

});

}

});

Ckeditor. plugins. AutoFormat = {

Commands :{

AutoFormat :{

Exec: function (editor ){

Formattext (editor );

}

}

}

};

// Execution Method
Function formattext (editor ){

VaR myeditor = editor;

// Obtain the source code to edit
VaR editorhtml = myeditor. getdata ();
// Execute your operations here .....

Editorhtml = editorhtml. Replace (/(<\/? (?! BR | p | IMG | A | H1 | H2 | H3 | H4 | H5 | H6) [^> \/] *) \/?> /GI ,'');
// Add a style to the P label so that each section is automatically Indented by two characters
Editorhtml = editorhtml. Replace (/\ <[p | p]>/g, "<P style = 'text-indent: 2em '> ");

// Put the content back in the editor
Editor. setdata (HTML );

}

})();

3. Modify config. js to register the code plug-in. Use the following code to replace the original content of config. JS:

 

Ckeditor. editorconfig = function (config ){

// Configure ckfinder

Config. extraplugins = "code, uploadimg ";

// Create a plug-in config. toolbar_temp = [

{Name: 'document', items: ['code', 'format _ wz', 'save']},

{Name: 'styles ', items: ['styles', 'format', 'font', 'fontsize']}];

Config. toolbar_full = [

{Name: 'document', items: ['code', 'source', '-', 'format _ wz', '-', 'save', 'newpage ', 'docprops', 'preview', 'print', '-', 'templates']},

{Name: 'tool', items: ['maximize', 'showbuckets', '-', 'about']}];

};

Note that my ckeditor configuration is completed by modifying config. js.

4. Install ckeditor and add the following JS Code to the script tag on the page to introduce ckeditor:

VaR editor = ckeditor. Replace ('P _ content_content ',{
Fullpage: True,
Extraplugins: 'uploadimg, format_wz, docprops'
});
Ckfinder. setupckeditor (Editor, 'ckfinder ');

5. Refresh the page and you will see the button you added.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.