Module: PhraseApp::InContextEditor::ViewHelpers

Defined in:
lib/phraseapp-in-context-editor-ruby/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#load_in_context_editor(opts = {}) ⇒ Object Also known as: phraseapp_in_context_editor_js



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/phraseapp-in-context-editor-ruby/view_helpers.rb', line 6

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.,
    "datacenter" => PhraseApp::InContextEditor.datacenter,
    "prefix" => PhraseApp::InContextEditor.prefix,
    "suffix" => PhraseApp::InContextEditor.suffix,
    "origin" => PhraseApp::InContextEditor.origin
  }.merge(opts)

  snippet = <<-EOS
  <script nonce='#{content_security_policy_nonce}'>
    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