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