Sha256: af270b3cac240fbe9d08f7c20055edc551299acb3e916255ae6b79691ed71459

Contents?: true

Size: 1.52 KB

Versions: 4

Compression:

Stored size: 1.52 KB

Contents

require "json"

module PhraseApp
  module InContextEditor
    module ViewHelpers
      def load_in_context_editor(opts = {})
        return "" unless PhraseApp::InContextEditor.enabled?

        # stringify to reduce possible errors when passing symbols
        opts = opts.nil? ? {} : opts.each_with_object({}) { |(k, v), conf|
                                  conf[k.to_s] = v
                                }

        # js options
        configuration = {
          "projectId" => PhraseApp::InContextEditor.project_id,
          "accountId" => PhraseApp::InContextEditor.account_id,
          "datacenter" => PhraseApp::InContextEditor.datacenter,
          "prefix" => PhraseApp::InContextEditor.prefix,
          "suffix" => PhraseApp::InContextEditor.suffix,
          "origin" => PhraseApp::InContextEditor.origin
        }.merge(opts)

        snippet = <<-EOS
        <script>
          window.PHRASEAPP_CONFIG = #{configuration.to_json};
          (function() {
            let phraseapp = document.createElement('script');
            phraseapp.type = 'module';
            phraseapp.async = true;
            phraseapp.src = "https://d2bgdldl6xit7z.cloudfront.net/latest/ice/index.js";
            let script = document.getElementsByTagName('script')[0];
            script.parentNode.insertBefore(phraseapp, script);
          })();
        </script>
        EOS
        snippet.respond_to?(:html_safe) ? snippet.html_safe : snippet
      end
      alias_method :phraseapp_in_context_editor_js, :load_in_context_editor
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
phraseapp-in-context-editor-ruby-3.1.1 lib/phraseapp-in-context-editor-ruby/view_helpers.rb
phraseapp-in-context-editor-ruby-3.1.0 lib/phraseapp-in-context-editor-ruby/view_helpers.rb
phraseapp-in-context-editor-ruby-3.0.1 lib/phraseapp-in-context-editor-ruby/view_helpers.rb
phraseapp-in-context-editor-ruby-2.1.1 lib/phraseapp-in-context-editor-ruby/view_helpers.rb