2
votes

Redactor WYSIWYG from: https://github.com/dybskiy/redactor-js

JQuery version: jquery-1.10.1.min.js (also tried 1.9.0 which comes bundled in demo)

Quite simple really, edit icon appears top right of content, onclick change content to WYSIWYG editor, also change icon to X for cancel

Onclick X, destroy editor, WYSIWYG editor back to original text

Following the quidance from this page: http://imperavi.com/redactor/examples/click-to-edit/ The difference is, I don't want 2 functions, I'm combining it into 1 function, depending on which class the editContainer has, will depend on which if-else is executed

Problem: .redactor('destroy'); does not work

<style>
    #editContainer {
        position: absolute;
        right: 5px;
        top: 5px;
        width: 16px;
        height: 16px;
    }
    #editContainer.edit {
        background: url("../img/icon-edit-16.png") no-repeat 0 0;
        z-index: 999;
        cursor: pointer;
    }
    #editContainer.cancel {
        background: url("../img/icon-error.png") no-repeat 0 0;
        z-index: 999;
        cursor: pointer;
    }
</style>
<div id='headerRightContent'>
    xxx
</div>

<div id='editContainer' class='edit' onclick=cmsEdit();></div>
<script type='text/javascript'>
    function cmsEdit() {
        if ( $('#editContainer').hasClass('edit') ) {
            $('#headerRightContent').redactor({ focus: true });
            $('#editContainer').removeClass('edit').addClass('cancel');
        }else if ( $('#editContainer').hasClass('cancel') ) {
            var html = $('#headerRightContent').redactor('get');
            $('#headerRightContent').redactor('destroy');
            $('#editContainer').removeClass('cancel').addClass('edit');
        }
    }
</script>
2
Have you try jQuery('#headerRightContent').redactor('destroy'); ? - EpokK
I ended up going for ckeditor, as I was in a rush to get this out. sorry for not being able to try your solution - Mark
OK, so you can valid my answer. - EpokK
For anyone else still stuck with this - the command I called to successfully destroy redactor was $(.redactor).destroyEditor(); - Richard Hedges

2 Answers

3
votes

For anyone experiencing this problem with Redactor II the command has changed slightly.

$('#headerRightContent').redactor('core.destroy');
2
votes

If you buy a licence of RedactorJS, destroy method works fine with the last version 9.1.1

OR update your code with this:

destroy: function()
{
    clearInterval(this.autosaveInterval);

    $(window).off('.redactor');
    this.$element.off('.redactor').removeData('redactor');

    var html = this.get();

    if (this.opts.textareamode)
    {
        this.$box.after(this.$source);
        this.$box.remove();
        this.$source.val(html).show();
    }
    else
    {
        var $elem = this.$editor;
        if (this.opts.iframe) $elem = this.$element;

        this.$box.after($elem);
        this.$box.remove();

        $elem.removeClass('redactor_editor').removeClass('redactor_editor_wym').removeAttr('contenteditable').html(html).show();
    }

    if (this.opts.air)
    {
        $('.redactor_air').remove();
    }
},