Home Web Front-end JS Tutorial A brief analysis of the implementation principles of online WYSIWYG HTML editor_javascript skills

A brief analysis of the implementation principles of online WYSIWYG HTML editor_javascript skills

May 16, 2016 pm 04:02 PM
html Implementation principle editor

Nowadays, website development is increasingly promoting user experience, and there are more and more tools to provide users with convenience, and the online HTML content editor should be considered one of the "older" ones. Those with simple functions can provide users with text style control, such as text color, font size, etc.; while those with complex functions can even provide powerful functions similar to Word. Although there are many open source editors now, not many are really easy to use, so their improvement work is always in progress.

Most editors on the Internet today have very powerful functions. Relatively speaking, they also require a lot of configuration during use. Of course, the code will naturally be "bloated". If we don't need such a powerful editor, we can implement one ourselves, because the code is not complicated. The following is a bit of personal experience, for reference only (taking ExtJS HTMLEditor as an example).

1. Initialization. When the page has finished loading, add an IFrame to the page (optional). What should be noted here is that to determine the status of the page, wait until the page is completely loaded before proceeding to prevent errors in which certain elements cannot be found.

2. Open the editing function. Make the IFrame editable (the code below comes from ExtJS’s HTMLEditor):

Copy code The code is as follows:

// Get the window object of iframe
getWin : function(){
           return Ext.isIE ? this.iframe.contentWindow : window.frames[this.iframe.name];
},

//Get the document object of iframe
getDoc: function(){
           return Ext.isIE ? this.getWin().document: (this.iframe.contentDocument || this.getWin().document);
},

//Open the document object and write initialization content to it to be compatible with FireFox
doc = this.getDoc();
doc.open();
doc.write('');
//Open document object editing mode
doc.designMode = "on";
doc.close();

This way you can write content into this simple editor.

3. Get the content of the editor, the code is as follows:

Copy code The code is as follows:

//Get the editor’s body object
var body = doc.body || doc.documentElement;
//Get the content of the editor
var content = body.innerHTML;
//Process the content, such as replacing some special characters, etc.
//Some code

//Return content
return content;

4. Add style settings. Although the above editor implements basic functions, it is really too simple. Some simple style implementations should be added. The document's execCommand method makes this idea possible.

Copy code The code is as follows:

//Uniform execution command method
function execCmd(cmd, value){
//To obtain the doc object, refer to the above code
//Call the execCommand method to execute the command
doc.execCommand(cmd, false, value === undefined ? null : value);
};

//Change the selected font to bold, Ctrl-B
execCmd('bold');
//Underline, Ctrl-U
execCmd('underline');
//Change to italics, Ctrl-I
execCmd('italic');
//Set the color of the text
execCmd('forecolor', Ext.isSafari || Ext.isIE ? '#' color : color);
//Insert a piece of content at the cursor
function insertAtCursor(text){
//To obtain the win object, refer to the code above
if(Ext.isIE){
      win.focus();
var r = doc.selection.createRange();
If(r){
         r.collapse(true);
          r.pasteHTML(text);                          }else if(Ext.isGecko || Ext.isOpera){
      win.focus();
​​ execCmd('InsertHTML', text);
}else if(Ext.isSafari){
​​ execCmd('InsertText', text);
}
}

5. Go one step further. Now you can change the style. If the editor has a toolbar (this should be inevitable), then we also want the buttons on the toolbar to be automatically highlighted or displayed normally according to the style of the cursor position. The queryCommandState() method of document allows this idea to be realized.


Copy code The code is as follows:
//To obtain the doc object, refer to the opposite side
//Whether the cursor is bold
var isBold = doc.queryCommandState('bold');
if(isBold){
//Change the style of the Bold button
}
//Of course the above code can be merged, this is just a hint

//Underline
doc.queryCommandState('underline');
//italic
doc.queryCommandState('italic');

This article only provides a simple idea for implementing the editor, and some of the codes can be used directly. It is recommended that friends who want to implement their own editor can refer to the HTMLEditor code in ExtJS, which is simple and clear, and can be extended on it.

One final reminder: Be sure to pay attention to browser compatibility issues, and don’t wait until the end to test compatibility. For such a large amount of JavaScript code, adjustments are more painful.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles