Skip to content

Commit aef4b90

Browse files
committed
[Finishes #82528944] Add ability to embed Blockly blocks within multi/match content
1 parent 4964685 commit aef4b90

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

dashboard/app/controllers/levels_controller.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class LevelsController < ApplicationController
77
before_filter :authenticate_user!
88
before_filter :can_modify?, except: [:show, :index]
99
skip_before_filter :verify_params_before_cancan_loads_model, :only => [:create, :update_blocks]
10-
load_and_authorize_resource :except => [:create, :update_blocks, :edit_blocks]
10+
load_and_authorize_resource :except => [:create, :update_blocks, :edit_blocks, :embed_blocks]
1111
check_authorization
1212

1313
before_action :set_level, only: [:show, :edit, :update, :destroy]
@@ -196,6 +196,21 @@ def can_modify?
196196
end
197197
end
198198

199+
def embed_blocks
200+
authorize! :read, :level
201+
@level = Level.find(params[:level_id])
202+
@block_type = params[:block_type]
203+
@app = @level.game.app
204+
@options = {
205+
readonly: true,
206+
locale: js_locale,
207+
baseUrl: "#{ActionController::Base.asset_host}/blockly/",
208+
blocks: @level.properties[@block_type]
209+
}
210+
puts "blocks: #{@level[@block_type]}, block type=#{@block_type}"
211+
render :embed_blocks, layout: false
212+
end
213+
199214
private
200215
# Use callbacks to share common setup or constraints between actions.
201216
def set_level

dashboard/app/helpers/levels_helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,12 @@ def string_or_image(prefix, text)
282282
path, width = text.split(',')
283283
if %w(.jpg .png .gif).include? File.extname(path)
284284
"<img src='#{path.strip}' #{"width='#{width.strip}'" if width}></img>"
285+
elsif File.extname(path).ends_with? '_blocks'
286+
# '.start_blocks' takes the XML from the start_blocks of the specified level.
287+
ext = File.extname(path)
288+
base_level = File.basename(path, ext)
289+
level = Level.find_by(name: base_level)
290+
"<iframe src='#{url_for(:controller => :levels, :action => :embed_blocks, :level_id => level.id, :block_type => ext.tap{|x|x.slice!(0)}).strip}' width='#{width ? width.strip : '100%'}' scrolling='no' seamless='seamless' style='border: none;'></iframe>"
285291
elsif File.extname(path) == '.level'
286292
base_level = File.basename(path, '.level')
287293
level = Level.find_by(name: base_level)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<%- # Embedded Blockly block html template. Derived from /blockly/src/templates/readonly.html.ejs, converted to erb %>
2+
<%- blockly_path = "#{ActionController::Base.asset_host}/blockly/" %>
3+
<%- options = @options %>
4+
<%- app = @app %>
5+
<!DOCTYPE html>
6+
<html dir="<%= locale_dir %>">
7+
<head>
8+
<meta charset="utf-8">
9+
<title>Blockly</title>
10+
<script type="text/javascript" src="<%= "#{blockly_path}js/#{options[:locale]}/vendor.js" %>"></script>
11+
<script type="text/javascript" src="<%= "#{blockly_path}js/#{options[:locale]}/#{app}.js" %>"></script>
12+
<script type="text/javascript">
13+
<% # delay to onload to fix IE9. %>
14+
window.onload = function() {
15+
<%= app %>Main(<%=raw options.to_json %>);
16+
};
17+
</script>
18+
</head>
19+
<body>
20+
<div id="blockly" class="readonly"></div>
21+
<style>
22+
html, body {
23+
background-color: transparent;
24+
margin: 0;
25+
padding:0;
26+
overflow: hidden;
27+
height: 100%;
28+
font-family: 'Gotham A', 'Gotham B', sans-serif;
29+
}
30+
.blocklyText, .blocklyMenuText, .blocklyTreeLabel, .blocklyHtmlInput,
31+
.blocklyIconMark, .blocklyTooltipText, .goog-menuitem-content {
32+
font-family: 'Gotham A', 'Gotham B', sans-serif;
33+
}
34+
#blockly>svg {
35+
background-color: transparent;
36+
border: none;
37+
}
38+
#blockly {
39+
position: absolute;
40+
top: 0;
41+
left: 0;
42+
overflow: hidden;
43+
height: 100%;
44+
width: 100%;
45+
}
46+
</style>
47+
</body>
48+
</html>

dashboard/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767

6868
resources :levels do
6969
get 'edit_blocks/:type', to: 'levels#edit_blocks', as: 'edit_blocks'
70+
get 'embed_blocks/:block_type', to: 'levels#embed_blocks', as: 'embed_blocks'
7071
post 'update_blocks/:type', to: 'levels#update_blocks', as: 'update_blocks'
7172
post 'clone', to: 'levels#clone'
7273
end

0 commit comments

Comments
 (0)