kendo-uikendo-editor

kendo ui editor how to modify user selection with range object


Kendo UI 2015.2.805 Kendo UI Editor for Jacascript

I want to extend the kendo ui editor by adding a custom tool that will convert a user selected block that spans two or more paragraphs into block of single spaced text. This can be done by locating all interior p tags and converting them into br tags, taking care not to change the first or last tag.

My problem is working with the range object.

Getting the range is easy:

var range = editor.getRange();

The range object has a start and end container, and a start and end offset (within that container). I can access the text (without markup)

console.log(range.toString());

Oddly, other examples I have seen, including working examples, show that

console.log(range);

will dump the text, however that does not work in my project, I just get the word 'Range', which is the type of the object. This concerns me.

However, all I really need however is a start and end offset in the editor's markup (editor.value()) then I can locate and change the p's to br's.

I've read the telerik documentation and the referenced quirksmode site's explanation of html ranges, and while informative nothing shows how to locate the range withing the text (which seems pretty basic to me).

I suspect I'm overlooking something simple.

Given a range object how can I locate the start and end offset within the editor's content?

EDIT: After additional research it appears much more complex than I anticipated. It seems I must deal with the range and/or selection objects rather than directly with the editor content. Smarter minds than I came up with the range object for reasons I cannot fathom.

Here is what I have so far:

var range = letterEditor.editor.getRange();

var divSelection;
divSelection = range.cloneRange();  
//cloning may be needless extra work...

//here manipulate the divSelection to how I want it.
//divSeletion is a range, not sure how to manipulate it

var sel = letterEditor.editor.getSelection()
sel.removeAllRanges();
sel.addRange(divSelection);

EDIT 2:

Based on Tim Down's Solution I came up with this simple test:

var html;
var sel = letterEditor.editor.getSelection();
if (sel.rangeCount) {
    var container = document.createElement("div");
    for (var i = 0, len = sel.rangeCount; i < len; ++i) {
        container.appendChild(sel.getRangeAt(i).cloneContents());
    }
    html = container.innerHTML;
}

html = html.replace("</p><p>", "<br/>")

var range = letterEditor.editor.getRange();
range.deleteContents();
var div = document.createElement("div");
div.innerHTML = html;
var frag = document.createDocumentFragment(), child;
while ((child = div.firstChild)) {
    frag.appendChild(child);
}
range.insertNode(frag);

The first part, getting the html selection works fine, the second part also works however the editor inserts

tags around all lines so the result is incorrect; extra lines including fragments of the selection.

The editor supports a view html popup which shows the editor content as html and it allows for editing the html. If I change the targeted p tags to br's I get the desired result. (The editor does support br as a default line feed vs p, but I want p's most of the time). That I can edit the html with the html viewer tool lets me know this is possible, I just need identify the selection start and end in the editor content, then a simple textual replacement via regex on the editor value would do the trick.

Edit 3:

Poking around kendo.all.max.js I discovered that pressing shift+enter creates a br instead of a p tag for the line feed. I was going to extend it to do just that as a workaround for the single-space tool. I would still like a solution to this if anyone knows, but for now I will instruct users to shift-enter for single spaced blocks of text.


Solution

  • This will accomplish it. Uses Tim Down's code to get html. RegEx could probably be made more efficient. 'Trick' is using split = false in insertHtml.

    var sel = letterEditor.editor.getSelection();
    if (sel.rangeCount) {
    
        var container = document.createElement("div");
        for (var i = 0, len = sel.rangeCount; i < len; ++i) {
            container.appendChild(sel.getRangeAt(i).cloneContents());
        }
        var block = container.innerHTML;
    
        var rgx = new RegExp(/<br class="k-br">/gi);
        block = block.replace(rgx, "");
    
        rgx = new RegExp(/<\/p><p>/gi);
        block = block.replace(rgx, "<br/>");
    
        rgx = new RegExp(/<\/p>|<p>/gi);
        block = block.replace(rgx, "");
    
        letterEditor.editor.exec("insertHtml", { html: block, split: false });
    
    }