How to disable elements selection and resizing in contenteditable div?

前端 未结 12 2196
被撕碎了的回忆
被撕碎了的回忆 2020-12-01 14:16

E.g. I have the following layout:

相关标签:
12条回答
  • 2020-12-01 14:30

    I have the same problem with CKEditor 4.4.7 in IE11. As a workaround, I save the current dimensions of an element on "mousedown" and set the "min-width", "max-width", "min-height" and "max-height" style properties to it's current dimensions. By that the element will be displayed in it's original size during resize. On "mouseup" I restore the style properties of the modified element. Here is my code:

    $('textarea').ckeditor().on('instanceReady.ckeditor', function(event, editor) {
        var $doc = $(editor.document.$);
        $doc.on("mousedown", "table,img", function() {
            var $this = $(this);
            var widthAttrValue = $this.attr("width");
            if (widthAttrValue) {
                $this.data("widthAttrValue", widthAttrValue);
            }
            var widthStyleValue = this.style.width;
            if (widthStyleValue) {
                $this.data("widthStyleValue", widthStyleValue);
            }
            var width = widthStyleValue || widthAttrValue || String($this.width())+"px";
            var height = this.style.height || $this.attr("height") || String($this.height())+"px";
            $this.css({
                "min-width": width,
                "max-width": width,
                "min-height": height,
                "max-height": height,                                       
            });
            $doc.data("mouseDownElem",$this);
        }).on("mouseup", function() {
            var $elem = $doc.data("mouseDownElem");
            if ($elem) {
                $elem.removeAttr("height").css("height","");
                var widthAttrValue = $elem.data("widthAttrValue");
                if (widthAttrValue) {
                    $elem.attr("width", widthAttrValue);
                    $elem.removeData("widthAttrValue");
                } else {
                    $elem.removeAttr("width");
                }
                var widthStyleValue = $elem.data("widthStyleValue");
                if (widthStyleValue) {                                      
                    $elem.removeData("widthStyleValue");
                } 
                $elem.css({
                    "min-width":"",
                    "max-width":"",
                    "min-height":"",
                    "max-height":"",
                    "width": widthStyleValue || ""
                });
                if (!$.trim($elem.attr("style"))) {
                    $elem.removeAttr("style");
                }
                $doc.removeData("mouseDownElem");
            }
        });
    });
    
    0 讨论(0)
  • 2020-12-01 14:34

    Nothing anyone else recommended here or in other threads really worked for me, but I solved it by doing:

    [contenteditable="true"] p:empty {
        display: inline-block;
    }
    

    This way the resize boxes disappeared, but I could still set my cursor below or in the P blocks to edit them.

    0 讨论(0)
  • 2020-12-01 14:35

    I had the same problem because I put CSS rules for the max-width onto all child elements within the contenteditable. Removing it or restricting it to images did the trick.

    [contenteditable] * { max-width: 100%; } // causes the issue
    [contenteditable] img { max-width: 100%; } // works fine for me
    

    Make sure that no <p> elements are affected by the max-width property.

    0 讨论(0)
  • 2020-12-01 14:37

    I had the same problem. It appears that from previous posts here there are certain behaviors that IE recognizes and will add this paragraph focus/resize. For me it was because I had a style for paragraphs within the contenteditible div.

    Removing:

    div[contenteditble="true"] p{
       min-height:1em;
    }
    

    Fixed it for me.

    0 讨论(0)
  • 2020-12-01 14:41

    This post was critical when solving this issue for me (works in tinyMCE):

    How to Remove Resize handles and border of div with contentEditable and size style

    By placing a contenteditable DIV within a non contenteditable DIV the handles do not appear in IE or FF but you can still edit the content

    Ex.

    <div class="outerContainer" contenteditable="false">
        <div class="innerContainer" contenteditable="true">
        </div>
    </div>
    
    0 讨论(0)
  • 2020-12-01 14:41

    What solved the problem for me was removing a max-width: 100% !important; line from the CSS properties of the DOM elements within the contenteditable DIV. Hope it helps!

    BTW this does not happen on MS Edge... fingers crossed that this shows a movement in the right direction by MS :)

    0 讨论(0)
提交回复
热议问题