buddypress-docs icon indicating copy to clipboard operation
buddypress-docs copied to clipboard

Quick insertion of an image from the attachments

Open yura121 opened this issue 12 years ago • 2 comments

It would be great to create a link/button beside an attachment image to quickly insert image into text.

yura121 avatar Nov 08 '13 11:11 yura121

That's a nice idea, but for technical reasons, it's not currently possible to display an attached image inline. Let's keep this ticket open just in case that changes in the future.

On 11/08/2013 06:00 AM, Yura Gerassimenko wrote:

It would be great to create a link/button beside an attachment image to quickly insert image into text.

— Reply to this email directly or view it on GitHub https://github.com/boonebgorges/buddypress-docs/issues/369.

boonebgorges avatar Nov 08 '13 12:11 boonebgorges

Has deployed attached image so: 2014 01 23-20 33 35

function bpdInsertImage(imgSrc) {
    if (jQuery('textarea#doc_content').is(':visible')) {
        var xImg = '<img src="' + imgSrc + '" />';
        jQuery('textarea#doc_content').insertAtCaret(xImg);
    } else {
        var ed = tinyMCE.getInstanceById('doc_content');
        var newNode = ed.getDoc().createElement('img');
        newNode.src = imgSrc;
        ed.execCommand('mceInsertContent', false, newNode.outerHTML);
    }
}
jQuery.fn.insertAtCaret = function(myValue) {
    return this.each(function() {
        var me = this;
        if (document.selection) { // IE
            me.focus();
            sel = document.selection.createRange();
            sel.text = myValue;
            me.focus();
        } else if (me.selectionStart || me.selectionStart == '0') { // Real browsers
            var startPos = me.selectionStart, endPos = me.selectionEnd, scrollTop = me.scrollTop;
            me.value = me.value.substring(0, startPos) + myValue + me.value.substring(endPos, me.value.length);
            me.focus();
            me.selectionStart = startPos + myValue.length;
            me.selectionEnd = startPos + myValue.length;
            me.scrollTop = scrollTop;
        } else {
            me.value += myValue;
            me.focus();
        }
    });
};

templatetags.php

            if (in_array($attachment_ext, array('jpg', 'jpeg', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico'))) {
                $attachment_insert_html = sprintf(
                    '<span class="sep">|</span><a href="javascript:;" onclick="bpdInsertImage(\'%s\');" class="doc-attachment-insert">%s</a>',
                    $attachment_url,
                    __( 'Insert', 'bp-docs' )
                );
            }

yura121 avatar Jan 23 '14 14:01 yura121