sketchily 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -59,8 +59,8 @@ Currently available options are:
59
59
  - `width` total width of editor
60
60
  - `height` total height of editor
61
61
 
62
- - `bkgd_color` canvas background color (3 or 6 hex digit html color format; not saved with image)
63
- - `bkgd_url` canvas background raster image url (not saved with image)
62
+ - `bkgd_color` canvas background color (3 or 6 hex digit html color format; not saved with image; see sketchily_show)
63
+ - `bkgd_url` canvas background image url (not saved with image; see bkgd_file option for sketchily_show)
64
64
 
65
65
  - `canvas_width` initial canvas width
66
66
  - `canvas_height` initial canvas height
@@ -68,8 +68,9 @@ Currently available options are:
68
68
 
69
69
  - `hide_rulers` true if you want to hide the canvas rulers
70
70
  - `hide_menu` true if you want svg-edit's menu to be hidden
71
- - `hide_image_tool` true if you want to hide the image tool button
72
- - `hide_hyperlink_tool` true if you want to hide the hyperlink tool button
71
+ - `hide_image_tool` true if you want to hide the `image tool` button
72
+
73
+ - `show_hyperlink_tool` true if you want to show the `hyperlink tool` button (see explanation below)
73
74
 
74
75
  - `show_layers` (true if you want the layer selector to display automatically when the editor is loaded)
75
76
 
@@ -81,6 +82,10 @@ Currently available options are:
81
82
 
82
83
  - other standard html attributes for the input tag
83
84
 
85
+ The `hyperlink tool` is disabled by default, as embedded links and scripts do not work with the display method used by sketchily_show.
86
+ The sketchily_show helper will display svg's inside of <img> tags, which are treated as static images by most browsers.
87
+ We consider this to be a necessary precaution when dealing with user-generated svg's.
88
+
84
89
  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.
85
90
  However, some uses of `form_for` can generate repeated ids (e.g. multiple `form_for @my_object.new` in the same page).
86
91
  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.
@@ -103,8 +108,11 @@ Example usage (haml):
103
108
 
104
109
  Currently available options are:
105
110
 
106
- - `width` (width of resulting image object)
107
- - `height` (height of resulting image object)
111
+ - `width` width of resulting image object
112
+ - `height` height of resulting image object
113
+
114
+ - `bkgd_color` background color (3 or 6 hex digit html color format)
115
+ - `bkgd_file` background image file path (must be a local image file)
108
116
 
109
117
  Passing only one of those options should keep the aspect ratio of the SVG constant in most browsers.
110
118
 
@@ -1,5 +1,31 @@
1
1
  module SketchilyHelper
2
2
  def sketchily_show(uri, options = {})
3
+ if options[:bkgd_color] || options[:bkgd_file]
4
+ require 'nokogiri'
5
+ doc = Nokogiri::XML(Base64.decode64(uri)) {|xml| xml.noblanks}
6
+
7
+ if options[:bkgd_file]
8
+ data_uri = "data:image/svg+xml;base64,#{Base64.strict_encode64(open(options[:bkgd_file]).read)}"
9
+ bkgd_file_node = Nokogiri::XML::Node.new 'g', doc
10
+ Nokogiri::XML::Builder.with(bkgd_file_node) do |xml|
11
+ xml.title 'Background Image Layer'
12
+ xml.image :id => 'background_image', :width => '100%', :height => '100%', :preserveAspectRatio => 'xMinYMin', 'xlink:href' => data_uri
13
+ end
14
+ doc.at('g').before(bkgd_file_node)
15
+ end
16
+
17
+ if options[:bkgd_color]
18
+ bkgd_color_node = Nokogiri::XML::Node.new 'g', doc
19
+ Nokogiri::XML::Builder.with(bkgd_color_node) do |xml|
20
+ xml.title 'Background Color Layer'
21
+ xml.rect :id => 'background_color', :width => '100%', :height => '100%', :fill => "##{options[:bkgd_color]}"
22
+ end
23
+ doc.at('g').before(bkgd_color_node)
24
+ end
25
+ print doc.to_xml
26
+ uri = Base64.strict_encode64(doc.to_xml)
27
+ end
28
+
3
29
  "<img src='data:image/svg+xml;base64,#{uri}' #{"width='#{options[:width]}'" if options[:width]} #{"height='#{options[:height]}'" if options[:height]}/>".html_safe
4
30
  end
5
31
 
@@ -0,0 +1,36 @@
1
+ module SketchilyHelper
2
+ def sketchily_show(uri, options = {})
3
+ if options[:bkgd_color] || options[:bkgd_file]
4
+ require 'nokogiri'
5
+ doc = Nokogiri::XML(Base64.decode64(uri)) {|xml| xml.noblanks}
6
+
7
+ if options[:bkgd_file]
8
+ data_uri = "data:image;base64,#{Base64.strict_encode64(open(options[:bkgd_file]).read)}"
9
+ bkgd_file_node = Nokogiri::XML::Node.new 'g', doc
10
+ Nokogiri::XML::Builder.with(bkgd_file_node) do |xml|
11
+ xml.title 'Background Image Layer'
12
+ xml.image :id => 'background_image', :width => '100%', :height => '100%', :preserveAspectRatio => 'xMinYMin', 'xlink:href' => data_uri
13
+ end
14
+ doc.at('g').before(bkgd_file_node)
15
+ end
16
+
17
+ if options[:bkgd_color]
18
+ bkgd_color_node = Nokogiri::XML::Node.new 'g', doc
19
+ Nokogiri::XML::Builder.with(bkgd_color_node) do |xml|
20
+ xml.title 'Background Color Layer'
21
+ xml.rect :id => 'background_color', :width => '100%', :height => '100%', :fill => "##{options[:bkgd_color]}"
22
+ end
23
+ doc.at('g').before(bkgd_color_node)
24
+ end
25
+ print doc.to_xml
26
+ uri = Base64.strict_encode64(doc.to_xml)
27
+ end
28
+
29
+ "<img src='data:image/svg+xml;base64,#{uri}' #{"width='#{options[:width]}'" if options[:width]} #{"height='#{options[:height]}'" if options[:height]}/>".html_safe
30
+ end
31
+
32
+ def svg_show(uri, options = {})
33
+ sketchily_show(uri, options)
34
+ end
35
+ end
36
+
@@ -13,7 +13,7 @@
13
13
  hide_rulers ||= false
14
14
  hide_menu ||= false
15
15
  hide_image_tool ||= false
16
- hide_hyperlink_tool ||= false
16
+ show_hyperlink_tool ||= false
17
17
  show_layers ||= false
18
18
  url ||= nil
19
19
  %>
@@ -47,7 +47,7 @@
47
47
  :value => value,
48
48
  :hide_menu => hide_menu,
49
49
  :hide_image_tool => hide_image_tool,
50
- :hide_hyperlink_tool => hide_hyperlink_tool,
50
+ :show_hyperlink_tool => show_hyperlink_tool,
51
51
  :url => url} %>
52
52
  </script>
53
53
 
@@ -5,7 +5,7 @@
5
5
  # Additionally, they may override the following variables:
6
6
  width ||= "750px"
7
7
  height ||= "650px"
8
- bkgd_color ||= "FFFFFF"
8
+ bkgd_color ||= "FFF"
9
9
  bkgd_url ||= ""
10
10
  canvas_width ||= 640
11
11
  canvas_height ||= 480
@@ -13,7 +13,7 @@
13
13
  hide_rulers ||= false
14
14
  hide_menu ||= false
15
15
  hide_image_tool ||= false
16
- hide_hyperlink_tool ||= false
16
+ show_hyperlink_tool ||= false
17
17
  show_layers ||= false
18
18
  url ||= nil
19
19
  %>
@@ -46,8 +46,8 @@
46
46
  :locals => {:id => id,
47
47
  :value => value,
48
48
  :hide_menu => hide_menu,
49
+ :show_hyperlink_tool => show_hyperlink_tool,
49
50
  :hide_image_tool => hide_image_tool,
50
- :hide_hyperlink_tool => hide_hyperlink_tool,
51
51
  :url => url} %>
52
52
  </script>
53
53
 
@@ -5,7 +5,7 @@
5
5
  # value
6
6
  # hide_menu
7
7
  # hide_image_tool
8
- # hide_hyperlink_tool
8
+ # show_hyperlink_tool
9
9
  # url %>
10
10
 
11
11
  var svgCanvas_<%= id %> = null;
@@ -48,13 +48,13 @@ function initEmbed_<%= id %>() {
48
48
  var toolsTop = doc.getElementById('tools_top');
49
49
  toolsTop.style.left = '5px';
50
50
  <% end %>
51
-
51
+
52
52
  <% if hide_image_tool %>
53
53
  var imageTool = doc.getElementById('tool_image');
54
54
  imageTool.parentNode.removeChild(imageTool);
55
55
  <% end %>
56
56
 
57
- <% if hide_hyperlink_tool %>
57
+ <% unless show_hyperlink_tool %>
58
58
  var hyperlinkTool = doc.getElementById('tool_make_link');
59
59
  hyperlinkTool.parentNode.removeChild(hyperlinkTool);
60
60
  <% end %>
@@ -5,7 +5,7 @@
5
5
  # value
6
6
  # hide_menu
7
7
  # hide_image_tool
8
- # hide_hyperlink_tool
8
+ # show_hyperlink_tool
9
9
  # url %>
10
10
 
11
11
  var svgCanvas_<%= id %> = null;
@@ -48,19 +48,21 @@ function initEmbed_<%= id %>() {
48
48
  var toolsTop = doc.getElementById('tools_top');
49
49
  toolsTop.style.left = '5px';
50
50
  <% end %>
51
+
52
+ <% unless show_hyperlink_tool %>
53
+ var hyperlinkTool = doc.getElementById('tool_make_link');
54
+ hyperlinkTool.parentNode.removeChild(hyperlinkTool);
55
+ <% end %>
51
56
 
52
57
  <% if hide_image_tool %>
53
58
  var imageTool = doc.getElementById('tool_image');
54
59
  imageTool.parentNode.removeChild(imageTool);
55
60
  <% end %>
56
61
 
57
- <% if hide_hyperlink_tool %>
58
- var hyperlinkTool = doc.getElementById('tool_make_link');
59
- hyperlinkTool.parentNode.removeChild(hyperlinkTool);
62
+ <% unless url %>
63
+ svgCanvas_<%= id %>.setSvgString(sketchily_decode64("<%= value.try(:squish) %>"));
60
64
  <% end %>
61
65
 
62
- svgCanvas_<%= id %>.setSvgString(sketchily_decode64("<%= value.try(:squish) %>"));
63
-
64
66
  attachSubmitHandler_<%= id %>();
65
67
  }
66
68
 
@@ -1,3 +1,3 @@
1
1
  module Sketchily
2
- VERSION = "1.3.0"
2
+ VERSION = "1.4.0"
3
3
  end
@@ -1,3 +1,3 @@
1
1
  module Sketchily
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
data/lib/sketchily.rb CHANGED
@@ -5,7 +5,7 @@ require "sketchily/sketchily_tag"
5
5
  module Sketchily
6
6
  ASSET_FILES = %w(sketchily.css sketchily.js sketchily_embed.js canvg/canvg.js canvg/rgbcolor.js extensions/ext-*.js locale/lang.*.js)
7
7
 
8
- SKETCHILY_OPTIONS = [:width, :height, :bkgd_color, :bkgd_url, :canvas_width, :canvas_height, :canvas_expansion, :hide_rulers, :hide_menu, :hide_image_tool, :hide_hyperlink_tool, :show_layers, :url]
8
+ SKETCHILY_OPTIONS = [:width, :height, :bkgd_color, :bkgd_url, :canvas_width, :canvas_height, :canvas_expansion, :hide_rulers, :hide_menu, :hide_image_tool, :show_hyperlink_tool, :show_layers, :url]
9
9
  TAG_OPTIONS = [:id, :value, :index]
10
10
  OPTIONS = SKETCHILY_OPTIONS + TAG_OPTIONS
11
11
 
data/lib/sketchily.rb~ CHANGED
@@ -5,7 +5,7 @@ require "sketchily/sketchily_tag"
5
5
  module Sketchily
6
6
  ASSET_FILES = %w(sketchily.css sketchily.js sketchily_embed.js canvg/canvg.js canvg/rgbcolor.js extensions/ext-*.js locale/lang.*.js)
7
7
 
8
- SKETCHILY_OPTIONS = [:width, :height, :canvas_width, :canvas_height, :canvas_expansion, :hide_rulers, :hide_menu, :hide_image_tool, :hide_hyperlink_tool, :show_layers, :url]
8
+ SKETCHILY_OPTIONS = [:width, :height, :bkgd_color, :bkgd_url, :canvas_width, :canvas_height, :canvas_expansion, :hide_rulers, :hide_menu, :show_hyperlink_tool, :hide_image_tool, :show_layers, :url]
9
9
  TAG_OPTIONS = [:id, :value, :index]
10
10
  OPTIONS = SKETCHILY_OPTIONS + TAG_OPTIONS
11
11