yabwe / medium-editor

Medium.com WYSIWYG editor clone. Uses contenteditable API to implement a rich text solution.
https://yabwe.github.io/medium-editor/
Other
16.04k stars 1.85k forks source link

How can I show the toolbar when an image is clicked? #270

Closed mingfang closed 9 years ago

mingfang commented 10 years ago

Currently the toolbar will only show when text is selected. I have to show the toolbar when an image is clicked. something like this

$("#editable img").click(function(){editor.showToolbar()})

Is there a way to programmatically show the toolbar?

mingfang commented 9 years ago

I figured it out.

jacksoncapper commented 9 years ago

How?

mingfang commented 9 years ago

Here is the code that I used. It's been a while since I wrote it and it was for v1.9.4

       var imageEditor = new MediumEditor('#editable-image',{
            disableEditing:true,
            buttons: ['upload', 'video'],
            extensions: {
                upload: new UploadExtension(),
                video: new LinkExtension()
            }
        });
        var hideAllToolbars = function(){
            if (window.getSelection) {
                if (window.getSelection().empty) {  // Chrome
                    window.getSelection().empty();
                } else if (window.getSelection().removeAllRanges) {  // Firefox
                    window.getSelection().removeAllRanges();
                }
            } else if (document.selection) {  // IE?
                document.selection.empty();
            }
            categoryEditor.hideToolbarActions();
            bodyEditor.hideToolbarActions();
        };
        $("#editable-image img" ).click(function(event){
            hideAllToolbars();
        });
        $("#editable-image img" ).dblclick(function(event){
            defaultLeft = (imageEditor.options.diffLeft) - (imageEditor.toolbar.offsetWidth / 2);
            halfOffsetWidth = imageEditor.toolbar.offsetWidth / 2;
            imageEditor.toolbar.style.left = (defaultLeft  + event.pageX) + 'px';
            imageEditor.toolbar.style.top = (event.pageY - imageEditor.toolbar.offsetHeight - 16) + 'px';
            imageEditor.toolbar.classList.add('medium-toolbar-arrow-under');
            imageEditor.toolbar.classList.remove('medium-toolbar-arrow-over');
            imageEditor.showToolbarActions();
        });