How can I prevent line breaks from being inserted when calling insertText?

128 Views Asked by At

I'm creating a CKeditor plugin to integrate the DokuWiki Encrypted Passwords plugin with CKEditor provided via the CKGEdit plugin

Basically, all it does is add a button which when you click it with some text highlighted, it will replace higlighted text with <decrypt>U2FsdGVkX19N38+3QA5dTlRVRwzVaEW4E1TlehveNVw=</decrypt> when viewing the page this becomes a clickable link that prompts you for the decryption key and then reveals the text.

The problem I'm having is if the encryption string is too long, then CKEditor inserts <br/>s when needed. These, in turn, are converted to // in the page.txt file which causes the decrypt to break since the encryption text is no longer the same.

I tried wrapping the decrypt block in pre tags:

<pre>
<decrypt>U2FsdGVkX19GucuXTn9YK063K5rubz3OVIUuanWcx7Vm35qjvIulEUqOrQeS8RRD
J0/3HlQXJHSMpFcqFkKE8B3GuyXLYgEqSNBeCbI0levJZpIxA/4daa12kpgM3Y4s
kZh6ABQ3VYs0J3CSce5XFdoKo5C8E1t8R6OU0DAyjBc=</decrypt>
</pre>

which did work in that there were no longer linebreaks inserted automatically, but causes the decrypt block to render as plain text on the page.

Here's the relevant code:

CKEDITOR.plugins.add('dokuencrypt', {
    icons: 'dokuencrypt',
    init: function (editor) {

        editor.addCommand('encryptSelected', {
            exec: function (editor) {
                handleEncrypt(editor);
            }
        });

        editor.ui.addButton('dokuencrypt', {
            label: 'Encrypt text',
            command: 'encryptSelected',
        });
    }
});

// handles encrypt and decrypt text
function handleEncrypt(editor) {
    var sample = sample = editor.getSelection().getSelectedText();
    // right here is where I want to prevent the new lines from being inserted
    editor.insertText('<decrypt>' + GibberishAES.enc(sample, passElt.value).replace(/\n$|\r$|\r\n$|<br\>$/g, '') + '</decrypt>')
}

One thing I've tried is adding a save event which replaces all instances of <br /> and // with nothing, but because I'm doing this via the CKGedit plugin it doesn't call the normal CKEditor save.

As you can also see I've tried replacing the
when inserting the text, but at that point the line breaks have not been entered. .replace(/\n$|\r$|\r\n$|<br \>$/g, '')

I've considered other workarounds like adding a trigger in the Dokuwiki which when a new page is saved it replaces the text, but I'd rather fix this where the problem actually is.

Summary: how can I prevent CKEditor from inserting new lines automatically

1

There are 1 best solutions below

0
kalenpw On

Still open to cleaner solutions, but I needed something to work for the time being so I added this to the Dokuwiki javascript which removes the unneeded \\ and allows decryption to work properly.

jQuery(document).ready(function () {
    let encryptedTitles = document.querySelectorAll('.encryptedpasswords');
    for (let i = 0; i < encryptedTitles.length; i++) {
        let oldTitle = encryptedTitles[i].title;
        let newTitle = oldTitle.replace(/\\/g, "");

        encryptedTitles[i].title = newTitle;
    }
});