Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the inline CKEditor scroll inside div?

The inline CKEditor is scrolling with the text correctly when the scroll is done on body. The following is an official demo.

http://sdk.ckeditor.com/samples/inline.html

However, when the scroll is not in a body but in a iframe or div, the scroll is not working even till the latest version now (version 4.5.5). There are some question asked here, for example,

Ckeditor inline editor stays on screen when scrolling a "scrollable" div

This solution works but I may have more than 20 editor on the same page and checking everyone is too heavy for my page.

CKEditor inline editor on elements within an iframe

Another solution that looks fine is not working for me, but what I want is some light solution like this one.

P.S. For those who want to test if your solution is working, here is beginning of the code that you may test on: https://jsfiddle.net/pteryvk3/6/

  <script src="//cdn.ckeditor.com/4.4.7/standard/ckeditor.js"></script>
<div id='' style="overflow: scroll;height:500px;width:90%;position:absolute">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In est ipsum, elementum id ipsum vel, aliquam lobortis ligula. Nam vel purus eget nulla bibendum interdum at non orci. Nulla facilisi. Vivamus aliquet sapien risus. Mauris molestie efficitur pharetra. Aliquam erat volutpat. Fusce ac leo pretium, ornare libero et, tincidunt erat. Nunc tempus tortor eget ex ultricies, a cursus nibh fringilla.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. In est ipsum, elementum id ipsum vel, aliquam lobortis ligula. Nam vel purus eget nulla bibendum interdum at non orci. Nulla facilisi. Vivamus aliquet sapien risus. Mauris molestie efficitur pharetra. Aliquam erat volutpat. Fusce ac leo pretium, ornare libero et, tincidunt erat. Nunc tempus tortor eget ex ultricies, a cursus nibh fringilla.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. In est ipsum, elementum id ipsum vel, aliquam lobortis ligula. Nam vel purus eget nulla bibendum interdum at non orci. Nulla facilisi. Vivamus aliquet sapien risus. Mauris molestie efficitur pharetra. Aliquam erat volutpat. Fusce ac leo pretium, ornare libero et, tincidunt erat. Nunc tempus tortor eget ex ultricies, a cursus nibh fringilla.

<br>
<div id='editable' style="color:red" contenteditable=true>editable div</div>
<br>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla maximus, urna nec condimentum pellentesque, quam lacus vulputate neque, in efficitur nunc nibh at odio. Mauris elementum sed dui et gravida. Donec a facilisis augue. Donec pellentesque dolor a dui varius dapibus. Nunc posuere sem non quam tristique, vitae placerat risus eleifend. Mauris sed ligula a odio imperdiet accumsan quis pellentesque lorem. Aliquam sed tincidunt mi, a tristique ex. Pellentesque eu convallis turpis, non auctor massa. Aliquam vel consectetur ipsum. Vestibulum lacus nisi, tincidunt ut nunc sit amet, dapibus laoreet dui. Duis et tempus erat.

Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec eu luctus nunc, ac consequat risus. Nam et tellus elit. Mauris aliquam, eros mattis placerat euismod, nunc nunc blandit sapien, quis hendrerit dui arcu nec felis. Aenean ut mattis mauris, a viverra est. Cras tincidunt elit quis rhoncus suscipit. Nullam sit amet lectus arcu. Sed at lacus congue turpis volutpat convallis. Ut dapibus, nulla nec sodales rhoncus, risus metus sollicitudin orci, sit amet congue urna mauris vitae dui. Integer vel leo neque. Aenean sit amet dui eget mi eleifend hendrerit eget finibus leo. Sed at orci eu nisl cursus mollis.

Donec suscipit sem at sem tristique, ac blandit ipsum tincidunt. Aenean ullamcorper massa erat, eget ultricies nunc ornare id. Donec sit amet sodales ligula. Nunc luctus, leo ut eleifend suscipit, mi neque efficitur neque, iaculis congue nisl ex aliquet mi. Aliquam non fringilla dui, ut mattis neque. Morbi ut massa dolor. Praesent vulputate finibus mauris, aliquam mollis ante iaculis quis. Mauris lobortis nisi et diam eleifend, varius mollis tellus dictum. Praesent mattis auctor odio in sodales. Duis tempus volutpat arcu quis vestibulum. Pellentesque enim tellus, dignissim pharetra neque sit amet, mattis pellentesque ipsum. In feugiat, leo et interdum ultrices, justo mi laoreet quam, vitae convallis dolor quam sit amet massa. In sed lacinia libero. Sed leo quam, tempus ut mi in, vehicula convallis turpis. Nulla a consectetur sem. Integer vehicula arcu sed laoreet ornare.

Praesent blandit id arcu sit amet varius. Sed auctor aliquet posuere. Nullam pretium, velit vel rhoncus dignissim, lacus tellus tincidunt sapien, nec volutpat nibh lectus sed nunc. Aenean malesuada est ornare gravida tincidunt. Phasellus eu sem non odio sollicitudin rutrum a vel tellus. Phasellus mauris tellus, convallis ac placerat vitae, accumsan nec orci. Nunc quis turpis porta, ornare lectus pretium, vestibulum magna. Integer eleifend lacinia mi dapibus pretium. Phasellus viverra nisi ligula, sit amet finibus lorem lobortis sit amet. Nam diam enim, condimentum quis facilisis id, vestibulum at tortor. Ut mollis, quam a vulputate efficitur, sem magna iaculis eros, quis dictum nulla dui vitae orci. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

Nulla nec lobortis libero. Aliquam in rhoncus eros. In mauris ipsum, cursus non consequat at, euismod id tortor. Aenean in pellentesque mauris. Vivamus tincidunt vitae lorem et venenatis. Curabitur auctor consectetur gravida. Aliquam scelerisque eros a tellus laoreet condimentum eget sit amet metus. Etiam auctor, libero eget convallis laoreet, velit eros ullamcorper risus, a sodales erat dolor vel orci. Curabitur et bibendum eros, id laoreet enim. Quisque cursus, est ac auctor bibendum, libero sapien rutrum tellus, at semper mauris velit rhoncus leo. Pellentesque consectetur tortor id arcu molestie, id convallis velit luctus.

Maecenas mattis id risus ut convallis. Vivamus iaculis ut odio feugiat tincidunt. Integer pellentesque purus euismod velit ultricies, vitae lobortis nunc sagittis. Etiam rhoncus vel dolor eget venenatis. Donec porta sit amet lacus non finibus. Fusce sagittis condimentum augue nec interdum. Duis auctor vitae nisl at aliquet. In vulputate convallis nibh, eget viverra arcu ultrices eget. Morbi eu eleifend metus, ac egestas quam. Vivamus id libero dapibus, tempus mauris at, auctor lacus.

Integer elementum nibh eget dolor tincidunt, non tempus arcu convallis. Mauris quis lacus in enim dapibus tempor eu vitae odio. Sed quis mattis nisl. Cras hendrerit dolor sit amet velit luctus interdum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Etiam lectus quam, interdum non diam nec, vestibulum molestie quam. Fusce non elementum lacus. Curabitur eget vestibulum diam. Integer nec lorem leo. Suspendisse finibus, tellus sed varius faucibus, ex dui imperdiet sapien, quis ultrices massa arcu quis est. Duis ultrices diam a lobortis dictum. Nunc eu est lobortis, pellentesque turpis vitae, finibus diam. Quisque sit amet pretium nunc, non mattis augue.

Phasellus ac lacus erat. Donec suscipit tincidunt nisi vitae volutpat. Curabitur ullamcorper massa lacus, id fringilla dui convallis vitae. Praesent non enim eget dui placerat finibus vitae at ante. Suspendisse nec mauris vel urna porta porta. In vitae rhoncus tellus. Praesent eu nisl non mauris semper porttitor mollis vel leo.

Duis condimentum aliquam viverra. Integer leo lacus, bibendum non augue eu, blandit mollis eros. Quisque finibus pretium nisi eu placerat. In ultrices rutrum tellus molestie auctor. Nunc lobortis venenatis euismod. Aliquam et orci eget neque accumsan varius. Cras ac eros ut nulla tempor efficitur aliquet id enim. Nunc dictum mattis nulla, eu vehicula sapien vulputate nec.

Sed fermentum nulla ac tortor suscipit, nec varius neque eleifend. Morbi odio ligula, eleifend nec dignissim non, venenatis et ante. Nam finibus molestie tortor vel consectetur. Cras iaculis ultricies sem, id volutpat libero tempor quis. Maecenas sagittis, tellus sit amet aliquet laoreet, orci ante viverra justo, id venenatis nisl diam vitae lectus. Donec in interdum nisl. Proin accumsan massa vitae felis elementum, eget volutpat sem aliquet. Sed erat ligula, rhoncus vitae ipsum at, tristique tincidunt leo. In id est non erat interdum ultrices at et velit. Sed non tortor euismod, tempor velit quis, volutpat eros. Vivamus tristique efficitur felis at sodales. Pellentesque posuere rutrum suscipit. Sed convallis elit at nibh maximus luctus. Duis congue risus ante, sed dignissim arcu aliquam sed. Nullam porta ex nibh, ut convallis lectus efficitur nec. Integer lacus dui, fermentum ac sollicitudin vel, luctus et nulla.


</div>
like image 791
cytsunny Avatar asked Dec 01 '15 11:12

cytsunny


2 Answers

The answer you linked to should work for you: https://stackoverflow.com/a/25041833/1579789

But instead of using a costly .find(), put your ckeditor instances into an array and then loop through the array, accessing your elements with a snappy document.getElementsByClassName('ckeditor'); or something similar

like image 196
Elon Zito Avatar answered Nov 11 '22 12:11

Elon Zito


A bit tricky but I think this solution will work for you: https://jsfiddle.net/yfa5pjtx/1/

Note that in order to position the toolbar correctly I used the jquery-ui lib. It's possible to do positioning calculations without it, but it's much easier to use jquery-ui for that.

This is function that will do the magic for you:

function repositionCKEditorToolbar() {
    // If we don't have any active instance of CKEDITOR - return
    if (!CKEDITOR.currentInstance) {
        return;
    }
    // Save the elements we need to work with
    currentActiveEditor = $(CKEDITOR.currentInstance.element.$)
    currentToolbar = $('#cke_' + currentActiveEditor.attr('id'))

    // Save the heights we will need
    toolbar_height = currentToolbar.height()
    parent_height = currentActiveEditor.parent().outerHeight(true)

    if (currentActiveEditor.position().top > parent_height) {
        // In case the toolbar is "out" of the container we should hide it.
        // It can happen because the toolbar is placed (in the DOM) outside the container.
        currentToolbar.css({top: '-1000px'})
    } else if (currentActiveEditor.position().top > toolbar_height) {
        // In case we have enough space to position the toolbar above the editor
        currentToolbar.position({my: "left bottom", at: "left top", of: currentActiveEditor, collision: "none none"})
    } else {
        if (parent_height - currentActiveEditor.height() - currentActiveEditor.position().top > toolbar_height) {
            // Only if have enough space under the editor and that area is visible - put the toolbar there.
            currentToolbar.position({my: "left top", at: "left bottom", of: currentActiveEditor, collision: "none none"})
        }
    }
}
$('#container').on('scroll', function() {
    // Call the repositioning function on each scroll of the container
    repositionCKEditorToolbar();
});

UPDATE

Here is the same code, but without the requirements of JQuery-UI lib: Note the new absolutePositionFromTop function that is responsible for the feature we needed from the jquery-ui lib.

function absolutePositionFromTop(el) {
    fromTop = 0;
    while ($(el)[0] != $('body')[0]) {
        fromTop = fromTop + $(el).position().top
        el = $(el).parent()
    }
    return fromTop;
}

function repositionCKEditorToolbar() {
    // If we don't have any active instance of CKEDITOR - return
    if (!CKEDITOR.currentInstance) {
        return;
    }
    // Save the elements we need to work with
    currentActiveEditor = $(CKEDITOR.currentInstance.element.$)
    currentToolbar = $('#cke_' + currentActiveEditor.attr('id'))

    // Save the heights we will need
    toolbar_height = currentToolbar.height()
    parent_height = currentActiveEditor.parent().outerHeight(true)

    if (currentActiveEditor.position().top > parent_height) {
        // In case the toolbar is "out" of the container we should hide it.
        // It can happen because the toolbar is placed (in the DOM) outside the container.
        currentToolbar.css({top: '-1000px'})
    } else if (currentActiveEditor.position().top > toolbar_height) {
        // In case we have enough space to position the toolbar above the editor
        newTop = absolutePositionFromTop(currentActiveEditor) - currentToolbar.height()
        currentToolbar.css({top : newTop + 'px'});
    } else {
        if (parent_height - currentActiveEditor.height() - currentActiveEditor.position().top > toolbar_height) {
            // Only if have enough space under the editor and that area is visible - put the toolbar there.
            newTop = absolutePositionFromTop(currentActiveEditor) + currentActiveEditor.height()
            currentToolbar.css({top : newTop + 'px'});
        }
    }
}
$('#container').on('scroll', function(ev) {
    // Call the repositioning function on each scroll of the container
    repositionCKEditorToolbar();
});

And a working jsfiddle.

like image 45
Dekel Avatar answered Nov 11 '22 10:11

Dekel