CKEditor - Change image source

微笑、不失礼 提交于 2019-12-01 02:31:37

问题


I have made some custom functionality to the CKEditor. In short, it shows a div tag with 5 links, for Small, Medium, Large X-Large and Original size.

When I click the links, it changes the SRC attribute of the image to the correct size.

It works, but it doesn't persist back to the editor. It's like the Image i get through the click event target, is not part of the Source code.

How can I change the Source code, when manipulating with the elements in the editor?

My code looks like this:

$(target).ckeditor(function (editor) {
    $(this.document.$).bind("click", function (event) {
        var target = $(event.target);

        if (target.is("img")) {
            var p = $("<div contenteditable='false' class='image-properties'>" + Milkshake.Resources.Text.size + ": <a class='sizeLink' href='#size1Img'>S</a>&nbsp;<a class='sizeLink' href='#size2Img'>M</a>&nbsp;<a class='sizeLink' href='#size3Img'>L</a>&nbsp;<a class='sizeLink' href='#size4Img'>XL</a>&nbsp;<a class='sizeLink' href='#size5Img'>Org.</a></div>");
            p.css("top", target.position().top);

            var regex = new RegExp(/(size\d{1}img)/i);
            var match = regex.exec(target.attr("src"));

            if (match != null) {
                var imgSrize = match[0];
                p.find("a[href=#" + imgSrize + "]").addClass("selected");
            }

            p.delegate("a", "click", function (e) {
                var link = $(e.target);

                if (!link.is(".selected")) {
                    $(".selected", link.parent()).removeClass("selected");
                    link.addClass("selected");

                    var imageSrc = target.attr("src");
                    imageSrc = imageSrc.replace(/(size\d{1}img)/i, link.attr("href").substring(1));

                    target.attr("src", imageSrc);
                    target.css("width", "");
                    target.css("height", "");
                }

                e.preventDefault();
            });

            p.insertAfter(target);
        } else if (!target.is("div.image-properties")) {
            $("div.image-properties", target.parent()).remove();
        }
    });

回答1:


The src of images and href of links are protected in CKEditor to avoid browser bugs (when copying, dragging or sometimes even just loading the content), so you must update also this custom attribute:

data-cke-saved-src



来源:https://stackoverflow.com/questions/5593296/ckeditor-change-image-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!