Class: EditorJs::Blocks::Base

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper, ActionView::Helpers::TextHelper, ERB::Util
Defined in:
lib/editor_js/blocks/base.rb

Constant Summary collapse

InvalidBlockDataError =
Class.new(StandardError)
InvalidBlockTypeError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
# 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

Instance Attribute Details

#output_bufferObject

ActionView::Helpers::TagHelper requires output_buffer accessor



13
14
15
# File 'lib/editor_js/blocks/base.rb', line 13

def output_buffer
  @output_buffer
end

#rawObject

ActionView::Helpers::TagHelper requires output_buffer accessor



13
14
15
# File 'lib/editor_js/blocks/base.rb', line 13

def raw
  @raw
end

Class Method Details

.inherited(parent) ⇒ Object



66
67
68
69
# 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) ⇒ Object



71
72
73
74
75
76
77
# 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

.registryObject



79
80
81
# File 'lib/editor_js/blocks/base.rb', line 79

def self.registry
  @registry ||= {}
end

.typeObject



62
63
64
# File 'lib/editor_js/blocks/base.rb', line 62

def self.type
  @type ||= self.to_s.underscore.split('/').last.gsub('_block', '')
end

Instance Method Details

#css_name(name = nil) ⇒ Object



54
55
56
# File 'lib/editor_js/blocks/base.rb', line 54

def css_name(name = nil)
  "#{css_prefix}#{name}"
end

#dataObject



46
47
48
# File 'lib/editor_js/blocks/base.rb', line 46

def data
  @content['data']
end

#decode_html(string) ⇒ Object



50
51
52
# File 'lib/editor_js/blocks/base.rb', line 50

def decode_html(string)
  html_decoder.decode(string)
end

#outputObject



58
59
60
# File 'lib/editor_js/blocks/base.rb', line 58

def output
  @content
end

#plainObject

Render plain text, for full-text searching



35
# File 'lib/editor_js/blocks/base.rb', line 35

def plain; end

#render(_options = {}) ⇒ Object

Render HTML

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/editor_js/blocks/base.rb', line 27

def render(_options = {})
  raise NotImplementedError
end

#sanitize!Object

Sanitize content of data



32
# File 'lib/editor_js/blocks/base.rb', line 32

def sanitize!; end

#schemaObject

Define JSON format of data

Raises:

  • (NotImplementedError)


22
23
24
# File 'lib/editor_js/blocks/base.rb', line 22

def schema
  raise NotImplementedError
end

#typeObject



42
43
44
# File 'lib/editor_js/blocks/base.rb', line 42

def type
  self.class.type
end

#valid?Boolean

Validate data

Returns:

  • (Boolean)


38
39
40
# File 'lib/editor_js/blocks/base.rb', line 38

def valid?
  JSON::Validator.validate(schema, data)
end