Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert HTML before an element in CKEditor

Tags:

ckeditor

What is the best way to programmatically insert HTML (that represents a CKEditor widget) before an existing element in CKEditor?

The content editable is not in focus and is not currently being edited.

For example, suppose the contents of the editor are:

<h1>Here's a title</h1>
<h2>Here's a subtitle</h2>
<p>Here's a paragraph</p>
<p>Here's a paragraph</p>
<p>Here's a paragraph</p>

Now, say I have a reference to the second <p> element. What is the best way to insert html before this tag? (Keeping in mind that the HTML that I want to insert will become a Ckeditor widget after inserting.)

Thank you very much for any help,

Michael

like image 787
Michael I Avatar asked Mar 21 '14 00:03

Michael I


People also ask

How to add HTML tag in CKEditor?

Do you want to add some HTML code right in the inner CKEditor area, but don't know how? You need HTML Insert. This add-on allows working with HTML code on the page you edit, highlights HTML syntax, and lets inserting a new code or edit the selected existing code.

What is CKEditor replace?

The CKEditor 4 Find and Replace feature allows for finding and replacing any text in the editor easily. It helps the user find words, word parts or phrases matching the case of the searched text, which is especially helpful in lengthy documents and one that may utilize certain words in different contexts.

How do I edit CKEditor?

Classic Editor with Default Source Editing Area Source editing is provided by the Source Editing Area plugin. Follow the steps below to try it out: Click the Source button to display the HTML source of this text in the source editing area. Click the Source button again to return to the WYSIWYG view.


1 Answers

With the current API it is not possible to insert HTML string at the specific position without involving selection (EDIT: since CKEditor 4.5.0 it is possible – read below), because the editor.insertHtml method inserts in the selection position. However, if you have a simple situation that your HTML string contains just one element (with some ancestors), then you can easily use editor.insertElement on a lower level, when you can specify range at which you want to insert element:

var range = editor.createRange(),
    element = CKEDITOR.dom.element.createFromHtml( elementHtml );

// Place range before the <p> element.
range.setStartAt( elementP, CKEDITOR.POSITION_BEFORE_START );
// Make sure it's collapsed.
range.collapse( true );

// Insert element at the range position.
editor.editable().insertElement( element, range );

As you can see this code uses editable.insertElement, which is used by editor.insertElement.

PS. Remember that insertElement will not upcast and initialize your widget. You can find more about this here - CKEditor, initialize widget added with insertElement.

Since 4.5.0

CKEditor 4.5.0 introduced editor.editable().insertHtmlIntoRange() as well as a range parameter for editor.insertHtml(). The latter method is a more high-level one, so it will take care of undo manager and setting selection in place of insertion. The former one is more a low-level method and it only inserts the data.

like image 106
Reinmar Avatar answered Sep 28 '22 17:09

Reinmar