Sha256: b33e54d4da8a739814185784e7be5a6304b868d2ced19033b85e626abd4225b7

Contents?: true

Size: 997 Bytes

Versions: 2

Compression:

Stored size: 997 Bytes

Contents

# frozen_string_literal: true

require_relative 'props_base_plugin'

module CKEditor5::Rails::Editor
  class PropsInlinePlugin < PropsBasePlugin
    attr_reader :code

    def initialize(name, code)
      super(name)

      @code = code
      validate_code!
    end

    def to_h
      {
        type: :external,
        window_name: name
      }
    end

    private

    def validate_code!
      raise ArgumentError, 'Code must be a String' unless code.is_a?(String)
    end
  end

  class InlinePluginWindowInitializer
    include ActionView::Helpers::TagHelper

    def initialize(plugin)
      @plugin = plugin
    end

    def to_html(nonce: nil)
      code = wrap_with_handlers(@plugin.code)

      tag.script(code.html_safe, nonce: nonce)
    end

    private

    def wrap_with_handlers(code)
      <<~JS
        window.addEventListener('ckeditor:request-cjs-plugin:#{@plugin.name}', () => {
          window['#{@plugin.name}'] = #{code.html_safe};
        });
      JS
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ckeditor5-1.24.1 lib/ckeditor5/rails/editor/props_inline_plugin.rb
ckeditor5-1.24.0 lib/ckeditor5/rails/editor/props_inline_plugin.rb