alohaeditor / Aloha-Editor

Aloha Editor is a JavaScript content editing library
http://www.alohaeditor.org
Other
2.46k stars 535 forks source link

Keep class of element on formating #1636

Open bernhardriegler opened 4 years ago

bernhardriegler commented 4 years ago

IS: When formatting an element (example: p to h1) all classes on the initial element are lost. SHOULD: keep classes on element change

Suggestion: add a check if config says that classes should be kept and ensure classes are carried over to new element: https://github.com/alohaeditor/Aloha-Editor/blob/dev/src/lib/aloha/jquery.aloha.js#L246

jQuery.fn.zap = function () {
    if(Aloha.settings && Aloha.settings.plugins && Aloha.settings.plugins.format && Aloha.settings.plugins.format.keepClass) {
        var elem = this.get(0);
        var parent = elem ? elem.parentElement : false;
        if (parent) {
            var classListWithoutRemoveClass = Array.prototype.slice.call(elem.classList).filter(function (className) {
                return className !== "preparedForRemoval"
            });
            classListWithoutRemoveClass.forEach(function (item) {
                parent.classList.add(item);
            }); 
        }   
    }
    this.each(function () {
        jQuery(this.childNodes).insertBefore(this);
    }).remove();
};