Skip to content

Commit bb1f805

Browse files
committed
Merge pull request code-dot-org#557 from code-dot-org/82528944_embed_msm_blocks
[Delivers #82528944] Add ability to embed Blockly blocks within multi/match content
2 parents 617a293 + c06816a commit bb1f805

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

dashboard/app/controllers/levels_controller.rb

Lines changed: 15 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]
@@ -204,6 +204,20 @@ def can_modify?
204204
end
205205
end
206206

207+
def embed_blocks
208+
authorize! :read, :level
209+
@level = Level.find(params[:level_id])
210+
@block_type = params[:block_type]
211+
@app = @level.game.app
212+
@options = {
213+
readonly: true,
214+
locale: js_locale,
215+
baseUrl: "#{ActionController::Base.asset_host}/blockly/",
216+
blocks: @level.properties[@block_type]
217+
}
218+
render :embed_blocks, layout: false
219+
end
220+
207221
private
208222
# Use callbacks to share common setup or constraints between actions.
209223
def set_level

dashboard/app/helpers/levels_helper.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,29 @@ 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+
content_tag(:iframe, '', {
291+
src: url_for(controller: :levels, action: :embed_blocks, level_id: level.id, block_type: ext.slice(1..-1)).strip,
292+
width: width ? width.strip : '100%',
293+
scrolling: 'no',
294+
seamless: 'seamless',
295+
style: 'border: none;',
296+
})
285297
elsif File.extname(path) == '.level'
286298
base_level = File.basename(path, '.level')
287299
level = Level.find_by(name: base_level)
288-
"<div class='aspect-ratio'><iframe src='#{url_for(:id => level.id, :controller => 'levels', :action => 'show', :embed => true).strip}' width='#{width ? width.strip : '100%'}' scrolling='no' seamless='seamless' style='border: none;'></iframe></div>"
300+
content_tag(:div,
301+
content_tag(:iframe, '', {
302+
src: url_for(id: level.id, controller: :levels, action: :show, embed: true).strip,
303+
width: (width ? width.strip : '100%'),
304+
scrolling: 'no',
305+
seamless: 'seamless',
306+
style: 'border: none;'
307+
}), {class: 'aspect-ratio'})
289308
else
290309
data_t(prefix + '.' + @level.name, text)
291310
end
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)