Giter VIP home page Giter VIP logo

google-closure-editor-imagedialog-plugin's Introduction

A closure TrogEditor plugin for image uploading/inserting.

DESCRIPTION

google-closure-image-plugin is a TrogEditor plugin like the one exists in Gmail. It uploading file or inserting a url on the web.

Uploading using an iframe(goog.net.iframeio) which is automatically fired when a using picked a local file.

INSTALLATION

There are two ways to using this plugin, one was:

copy to closure library dir

  • copy editor/* to closure/goog/editor/plugins/
  • update closure/goog/deps.js
  • edit closure/goog/editor/command, add image plugin:
    goog.editor.Command = {
                        ....
                        IMAGE: 'ImageDialogPlugin'
    }
  • require and register your plugin just like the others:
      goog.require('goog.editor.plugins.ImageBubble');
      goog.require('goog.editor.plugins.ImageDialogPlugin');
      ...
      var trogField = new goog.editor.Field(editorId);
      ...
      trogField.registerPlugin(new goog.editor.plugins.ImageBubble());
      trogField.registerPlugin(new goog.editor.plugins.ImageDialogPlugin(config));
      
      // Specify the buttons to add to the toolbar, using built in default buttons.
      var buttons = [
        ...
        goog.editor.Command.IMAGE,
        ...
      ];
      
      var myToolbar =
        goog.ui.editor.DefaultToolbar.makeToolbar(buttons,
                                                  goog.dom.getElement(toolbarId));

Or if you want to make upstream closure clean

Assume your dir placed like:

.
|-- closure
|-- google-closure-image-plugin

In your html:

<script src="closure/closure/goog/base.js" type="text/javascript"></script> 
<script src="closure-closure-image-plugin/deps.js type="text/javascript"></script>

var buttons = [
  ...
  goog.editor.Command.IMAGE,
  ...
];

For a full example please checkout editor.js which is the one we are using, it replace the textarea with closure trog editor.

Config

For file upload, the plugin need to know the form action url, you could pass the config when register the plugin, it also allow you to append extra code to upload form, eg, you want to append a hidden token value to the form.

var config = {
  actionUrl : '/upload',
  extraCode: '<input name="token" type="hidden" value="TOKEN_VALUE_FOO" />'
}
...
trogField.registerPlugin(new goog.editor.plugins.ImageDialogPlugin(config));

Server side returns

This should return from the server side(eg: Python, Rails, PHP), the returned JSON format should as the followings:

// on succcess
{"status": 0, "imageUrl": "http://youdomain/foo.png"}

// on error
{"status": 1, "errorMsg": "Upload failed!"}

A Rails example:

  def upload
    begin
      if params[:file].nil?
        raise StandardError.new('Unknown file.')
      end

      media = Media.new(:media => params[:file])
      media.save!

      ret = {
        :status => 0,
        :imageUrl => media.url
      }
    rescue => e
      ret = {
        :status => 1,
        :errorMsg => e.message
      }
    end
    
    render :text => ret.to_json, :layout => false, :content_type => 'application/json'
  end

TODO

  • uploading indicator
  • allow changing image width?

Thanks

This plugin influenced by the followings:

google-closure-editor-imagedialog-plugin's People

Contributors

yinhm avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.