class EditorJs::Document

Constants

SCHEMA

Public Class Methods

new(str_or_hash) click to toggle source
# File lib/editor_js/document.rb, line 24
def initialize(str_or_hash)
  str_or_hash = JSON.parse(str_or_hash) unless str_or_hash.is_a?(Hash)
  @content = str_or_hash
  @blocks = []
end

Public Instance Methods

output() click to toggle source
# File lib/editor_js/document.rb, line 58
def output
  return @output if instance_variable_defined?(:@output)

  @output = valid? ? @content.merge('blocks' => @blocks.map(&:output)) : {}
end
plain() click to toggle source
# File lib/editor_js/document.rb, line 50
def plain
  return @renderred_plain if instance_variable_defined?(:@renderred_plain)

  @renderred_plain = valid? && @blocks.map(&:plain).select do |text|
    text if text.present?
  end.join('. ') || ''
end
render() click to toggle source
# File lib/editor_js/document.rb, line 44
def render
  return @renderred_html if instance_variable_defined?(:@renderred_html)

  @renderred_html = valid? ? @blocks.map(&:render).join.html_safe : ''
end
valid?() click to toggle source
# File lib/editor_js/document.rb, line 30
def valid?
  return @valid if instance_variable_defined?(:@valid)

  @valid = JSON::Validator.validate(SCHEMA, @content)
  return false unless @valid

  blocks = @content['blocks'].map do |blk_data|
    EditorJs::Blocks::Base.load(blk_data)
  end
  @valid = blocks.all?(&:valid?)
  @blocks = blocks if @valid
  @valid
end