Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The changes are not limited to the text selection when using the inline CKEDITOR #5

Open
jckaashoek opened this issue Apr 15, 2014 · 6 comments

Comments

@jckaashoek
Copy link

When using the inline CKEDITOR and texttransform, the updates are not limited to the selected text, the entire paragraph appears to get changed.

@jckaashoek jckaashoek changed the title The changes are not limited to the text selectin when using the inline CKEDITOR The changes are not limited to the text selection when using the inline CKEDITOR Apr 15, 2014
@saugataroy
Copy link

I've also faced this bug. In inline mode the buttons do not respond properly and the entire paragraph gets transformed.

@jckaashoek
Copy link
Author

Fixed the issue, here is an example one of the code snippets

editor.addCommand('transformTextToUppercase',
{
exec : function()
{
var selection = editor.getSelection();
if (selection.getSelectedText().length > 0) {
var theText = selection.getSelectedText();
editor.insertText(theText.toLocaleUpperCase());
}
} //func
});

@saugataroy
Copy link

Thanks! it is working perfectly now.

@dlorin
Copy link

dlorin commented Mar 7, 2019

But if the selection contains some strong or background text, the style becomes "normal". You have to go thru the nodes using walker.next().

You just have to stop the transformation at the end of the selection (with "longueur")

var transformSelectedText = function(editor, transformFunc) {

    var selection = editor.getSelection();
    longueur = selection.getSelectedText().length;
    if (longueur > 0) {
	selection.lock();
	var range = selection.getRanges()[0], walker = new CKEDITOR.dom.walker(range), node, nodeText;

	while ((node = walker.next())) {

	    if (node.type == CKEDITOR.NODE_TEXT && node.getText()) {
		nodeText = node.$.textContent;
		if (node.equals(range.startContainer)) {
		    nodeText = nodeText.substr(0, range.startOffset) + transformFunc(nodeText.substr(range.startOffset, longueur))
			   + nodeText.substr(range.startOffset + longueur);

		} else if (node.equals(range.endContainer)) {
		    nodeText = transformFunc(nodeText.substr(0, range.endOffset)) + nodeText.substr(range.endOffset);
		} else {
		    nodeText = transformFunc(nodeText);
		}
		node.$.textContent = nodeText;
	    }
	}
	selection.unlock(true);
    }
}

@Lokavidu
Copy link

@dlorin thank you, your modification to the function solves the issue! the plugin is now working perfectly for inline editor again. thanks

@onderceylan
Copy link
Owner

Hey @dlorin, why don't you create a PR with this change? It looks good and validated already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants