diff --git a/app/assets/javascripts/embed.js b/app/assets/javascripts/embed.js index 54c9a99..69db0eb 100644 --- a/app/assets/javascripts/embed.js +++ b/app/assets/javascripts/embed.js @@ -1,2 +1,2 @@ //= require base -//= require player +//= require asciinema-player diff --git a/app/assets/javascripts/player.js b/app/assets/javascripts/player.js deleted file mode 100644 index 7454583..0000000 --- a/app/assets/javascripts/player.js +++ /dev/null @@ -1,45 +0,0 @@ -//= require asciinema-player - -function tryCreatePlayer(parentNode, asciicast, options) { - function createPlayer() { - asciinema.player.js.CreatePlayer( - parentNode, - asciicast.url, - { - width: asciicast.width, - height: asciicast.height, - poster: options.poster || asciicast.snapshot, - speed: options.speed, - autoPlay: options.autoPlay, - loop: options.loop, - preload: options.preload, - startAt: options.startAt, - fontSize: options.fontSize, - theme: options.theme, - title: options.title, - author: options.author, - authorURL: options.authorURL, - authorImgURL: options.authorImgURL - } - ); - } - - function fetch() { - $.get('/api/asciicasts/' + asciicast.id + '.json', function(data) { - asciicast = data; - checkReadiness(); - }); - } - - function checkReadiness() { - if (asciicast.url) { - $('.processing-info').remove(); - createPlayer(); - } else { - $('.processing-info').show(); - setTimeout(fetch, 2000); - } - } - - checkReadiness(); -} diff --git a/app/helpers/asciicasts_helper.rb b/app/helpers/asciicasts_helper.rb index 82d528c..70892bf 100644 --- a/app/helpers/asciicasts_helper.rb +++ b/app/helpers/asciicasts_helper.rb @@ -7,6 +7,32 @@ module AsciicastsHelper skip_titlebar: skip_titlebar end + def player_tag(asciicast, options, skip_titlebar) + opts = { + src: asciicast.url, + cols: asciicast.width, + rows: asciicast.height, + poster: 'data:application/json;base64,' + Base64.encode64(asciicast.snapshot.to_json), + speed: options.speed, + autoplay: options.autoplay, + loop: options.loop, + 'start-at' => options.t, + 'font-size' => options.size, + theme: options.theme, + } + + unless skip_titlebar + opts.merge!( + title: asciicast.title, + author: asciicast.author_display_name, + 'author-url' => asciicast.author_url, + 'author-img-url' => asciicast.author_avatar_url, + ) + end + + content_tag('asciinema-player', '', opts) + end + def screenshot_javascript_tag js = assets.find_asset('embed.js').to_s content_tag(:script, js.html_safe) diff --git a/app/models/asciicast.rb b/app/models/asciicast.rb index 357eb3d..2d4a0a0 100644 --- a/app/models/asciicast.rb +++ b/app/models/asciicast.rb @@ -105,6 +105,10 @@ class Asciicast < ActiveRecord::Base data.url(options) end + def ready? + data_url && snapshot + end + def download_filename "asciicast-#{id}.json" end diff --git a/app/serializers/asciicast_serializer.rb b/app/serializers/asciicast_serializer.rb index b49d8a5..743b2bf 100644 --- a/app/serializers/asciicast_serializer.rb +++ b/app/serializers/asciicast_serializer.rb @@ -3,6 +3,8 @@ class AsciicastSerializer < ActiveModel::Serializer attributes :id, :url, :snapshot, :width, :height + delegate :private?, :title, :ready?, to: :object + def id object.to_param end @@ -21,14 +23,6 @@ class AsciicastSerializer < ActiveModel::Serializer end end - def private? - object.private? - end - - def title - object.title - end - def author_display_name object.user.display_name end diff --git a/app/views/asciicasts/_player.html.erb b/app/views/asciicasts/_player.html.erb index 2f7f87a..69385ca 100644 --- a/app/views/asciicasts/_player.html.erb +++ b/app/views/asciicasts/_player.html.erb @@ -5,28 +5,19 @@ <% end %> <% end %> -
- - +<% if asciicast.ready? %> + <%= player_tag asciicast, options, skip_titlebar %> +<% else %> + <% content_for(:head) do %> + + <% end %> - +
+

+ This recording is being pre-processed at the moment. It will open automatically in a few seconds. +

+

+ Feel free to contact support@asciinema.org if it takes longer. +

+
+<% end %> diff --git a/app/views/layouts/application.html.slim b/app/views/layouts/application.html.slim index 5595a71..c279b02 100644 --- a/app/views/layouts/application.html.slim +++ b/app/views/layouts/application.html.slim @@ -8,7 +8,7 @@ html[lang="en"] = csrf_meta_tags = stylesheet_link_tag "application", :media => "all" = javascript_include_tag "application" - = javascript_include_tag "player" + = javascript_include_tag "asciinema-player" = favicon_link_tag 'favicon.png' = render 'layouts/ga' if Rails.env.production? = content_for(:head) diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb index 83be50c..e388f48 100644 --- a/config/initializers/assets.rb +++ b/config/initializers/assets.rb @@ -5,4 +5,4 @@ Rails.application.config.assets.version = '1.0' # Precompile additional assets. # application.js, application.css, and all non-JS/CSS in app/assets folder are already added. -Rails.application.config.assets.precompile += %w( player.css player.js embed.css embed.js widget.js screenshot.css ) +Rails.application.config.assets.precompile += %w( player.css asciinema-player.js embed.css embed.js widget.js screenshot.css ) diff --git a/spec/api/asciicast_show_spec.rb b/spec/api/asciicast_show_spec.rb index 15bf364..a9540dd 100644 --- a/spec/api/asciicast_show_spec.rb +++ b/spec/api/asciicast_show_spec.rb @@ -15,7 +15,7 @@ shared_examples_for "asciicast iframe response" do end it "responds with player page using iframe layout" do - expect(response.body).to have_selector('body.iframe div.player') + expect(response.body).to have_selector('body.iframe asciinema-player') end end