1
votes

Is there a way to measure an element inside a tinymce editor instance? I need to measure the width of a table and it's columns so that I can find the relative column widths of the table, the code below returns 0 even though the table has content inside it.

var doc = tinymce.get('text_box_desc0').getDoc();
var width = $('table', doc).width();
alert(width); 
1

1 Answers

1
votes

Yes, this is possible.

Here is an example of how to use getBoundingClientRect() to get some information

        var ed = tinymce.get('text_box_desc0');

        // this will take the first table in the editor, you may define another selector to get 'your' table element 
        var elem = $(ed.getBody()).find('table:first').get(0);
        try {
            box = elem.getBoundingClientRect();
        } 
        catch(e) 
        {
            console.log('error creating box: ' + e);
        }
        // various info
        var doc = ed.getDoc(),
            docElem = doc.documentElement,
            body = ed.getBody(),
            win = ed.getWin(),
            clientTop  = docElem.clientTop  || body.clientTop  || 0,
            clientLeft = docElem.clientLeft || body.clientLeft || 0,
            scrollTop  = win.pageYOffset || jQuery.support.boxModel && docElem.scrollTop  || body.scrollTop,
            scrollLeft = win.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft,
            top  = box.top  + scrollTop  - clientTop,
            left = box.left + scrollLeft - clientLeft;

What you are looking for is

box.width;

but there are even more attributes like

box.height;