Class: RenderEditorjs::Document

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::OutputSafetyHelper
Defined in:
lib/render_editorjs/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, renderer = RenderEditorjs::DefaultRenderer.new) ⇒ Document

Returns a new instance of Document.



9
10
11
12
13
# File 'lib/render_editorjs/document.rb', line 9

def initialize(content, renderer = RenderEditorjs::DefaultRenderer.new)
  @renderer = renderer
  @content = content.is_a?(Hash) ? content : parse_json(content)
  @errors = []
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



7
8
9
# File 'lib/render_editorjs/document.rb', line 7

def content
  @content
end

#errorsObject (readonly)

Returns the value of attribute errors.



7
8
9
# File 'lib/render_editorjs/document.rb', line 7

def errors
  @errors
end

#rendererObject (readonly)

Returns the value of attribute renderer.



7
8
9
# File 'lib/render_editorjs/document.rb', line 7

def renderer
  @renderer
end

Instance Method Details

#renderObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/render_editorjs/document.rb', line 23

def render
  return "" unless valid_renderer?

  safe_join(
    content["blocks"].map do |block|
      block_renderer = block_renderers(block["type"])
      next unless block_renderer

      block_renderer.render(block["data"])
    end.compact
  )
end

#valid?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
# File 'lib/render_editorjs/document.rb', line 15

def valid?
  return false unless valid_renderer?

  validate_blocks

  @errors.empty?
end