class EditorJs::Blocks::Base
Constants
- InvalidBlockDataError
- InvalidBlockTypeError
Attributes
output_buffer[RW]
ActionView::Helpers::TagHelper requires output_buffer
accessor
raw[RW]
ActionView::Helpers::TagHelper requires output_buffer
accessor
Public Class Methods
inherited(parent)
click to toggle source
Calls superclass method
# File lib/editor_js/blocks/base.rb, line 66 def self.inherited(parent) ::EditorJs::Blocks::Base.registry[parent.type] = parent super end
load(block_data)
click to toggle source
# File lib/editor_js/blocks/base.rb, line 71 def self.load(block_data) block_data = JSON.parse(block_data) unless block_data.is_a?(Hash) klass = ::EditorJs::Blocks::Base.registry[block_data['type'].underscore] raise InvalidBlockTypeError, block_data['type'] if klass.nil? klass.new(block_data) end
new(raw)
click to toggle source
# File lib/editor_js/blocks/base.rb, line 15 def initialize(raw) @raw = raw @content = cast_block_data_to_hash(raw.deep_dup) sanitize! end
registry()
click to toggle source
# File lib/editor_js/blocks/base.rb, line 79 def self.registry @registry ||= {} end
type()
click to toggle source
# File lib/editor_js/blocks/base.rb, line 62 def self.type @type ||= self.to_s.underscore.split('/').last.gsub('_block', '') end
Public Instance Methods
css_name(name = nil)
click to toggle source
# File lib/editor_js/blocks/base.rb, line 54 def css_name(name = nil) "#{css_prefix}#{name}" end
data()
click to toggle source
# File lib/editor_js/blocks/base.rb, line 46 def data @content['data'] end
decode_html(string)
click to toggle source
# File lib/editor_js/blocks/base.rb, line 50 def decode_html(string) html_decoder.decode(string) end
output()
click to toggle source
# File lib/editor_js/blocks/base.rb, line 58 def output @content end
plain()
click to toggle source
Render plain text, for full-text searching
# File lib/editor_js/blocks/base.rb, line 35 def plain; end
render(_options = {})
click to toggle source
Render HTML
# File lib/editor_js/blocks/base.rb, line 27 def render(_options = {}) raise NotImplementedError end
sanitize!()
click to toggle source
Sanitize content of data
# File lib/editor_js/blocks/base.rb, line 32 def sanitize!; end
schema()
click to toggle source
Define JSON format of data
# File lib/editor_js/blocks/base.rb, line 22 def schema raise NotImplementedError end
type()
click to toggle source
# File lib/editor_js/blocks/base.rb, line 42 def type self.class.type end
valid?()
click to toggle source
Validate data
# File lib/editor_js/blocks/base.rb, line 38 def valid? JSON::Validator.validate(schema, data) end
Private Instance Methods
cast_block_data_to_hash(block_data)
click to toggle source
# File lib/editor_js/blocks/base.rb, line 85 def cast_block_data_to_hash(block_data) raise InvalidBlockDataError, block_data unless block_data.is_a?(Hash) block_data = block_data.deep_stringify_keys unless block_data['type'].underscore == type raise InvalidBlockDataError,\ "block type <#{block_data['type']}> doesn't match <#{type}>" end block_data rescue JSON::ParserError => _e raise InvalidBlockDataError, "Invalid JSON: #{block_data}" end
css_prefix()
click to toggle source
# File lib/editor_js/blocks/base.rb, line 99 def css_prefix @css_prefix ||= "#{EditorJs.css_name_prefix}#{type}" end
html_decoder()
click to toggle source
# File lib/editor_js/blocks/base.rb, line 103 def html_decoder @html_decoder ||= begin with_customized_html_mappings do HTMLEntities::Decoder.new('expanded') end end end
with_customized_html_mappings() { || ... }
click to toggle source
# File lib/editor_js/blocks/base.rb, line 111 def with_customized_html_mappings original_mappings = HTMLEntities::MAPPINGS['expanded'] customized_mappings = original_mappings.dup customized_mappings['nbsp'] = 32 HTMLEntities::MAPPINGS['expanded'] = customized_mappings yield ensure HTMLEntities::MAPPINGS['expanded'] = original_mappings end