lml / sketchily

Embed SVG-edit in Rails HTML forms
MIT License
26 stars 9 forks source link

Sketchily

Sketchily allows the easy integration of svg-edit with any rails application.

Currently supports and provides svg-edit-2.7.

Installation

Add this line to your application's Gemfile:

gem 'sketchily'

And then execute:

$ bundle

Or install it yourself:

$ gem install sketchily

Add //= require sketchily to your application.js. It should look similar to this:

//= require jquery
//= require jquery_ujs
//= require sketchily

Note: sketchily will automatically add the following files to your asset precompilation list:

sketchily.js
svg-edit/canvg/canvg.js
svg-edit/canvg/rgbcolor.js
svg-edit/extensions/ext-*.js
svg-edit/locale/lang.*.js

Other html files and images used by svg-edit will also be precompiled.

Usage

Sketchily adds new form elements which can be accessed by calling:

This gem also adds a helper method that can be called to display the resulting SVG images (without an editor):

sketchily and sketchily_tag functions

Currently available options are:

The hyperlink tool is disabled by default, as embedded links and scripts do not work with the display method used by sketchily_show. The sketchily_show helper will display svg's inside of tags, which are treated as static images by most browsers. We consider this to be a necessary precaution when dealing with user-generated svg's.

Sketchily requires a unique id (by default, this is set in the same way as hidden_field) each time it is called in the same page. However, some uses of form_for can generate repeated ids (e.g. multiple form_for @my_object.new in the same page). In those cases, you need to generate and specify your own unique ids. A possible solution is to use one of the many uuid generator gems.

It is recommended that the database entries associated with sketchily form elements be of type text.

The following examples assume that the database table for @my_object has a sketch column of type text.

Example usage (haml):

= form_for @my_object do |f|
  = f.sketchily :sketch, :hide_menu => true

sketchily_show helper

Currently available options are:

Passing only one of those options should keep the aspect ratio of the SVG constant in most browsers.

Example usage (haml):

= sketchily_show @my_object.sketch, :width => "500"

Customizing svg-edit

See ConfigOptions for information about how to configure svg-edit. Please note that most common options, like width and height should be set directly on calls to sketchily's functions in ruby code.

Browser Support

Although more testing is needed, we currently believe sketchily supports any browsers that svg-edit supports, namely:

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Create specs for your feature
  4. Ensure that all specs pass
  5. Commit your changes (git commit -am 'Add some feature')
  6. Push to the branch (git push origin my-new-feature)
  7. Create new pull request