Class: EditorJs::Blocks::Base
- Inherits:
-
Object
- Object
- EditorJs::Blocks::Base
- Includes:
- ActionView::Helpers::TagHelper, ActionView::Helpers::TextHelper, ERB::Util
- Defined in:
- lib/editor_js/blocks/base.rb
Direct Known Subclasses
AttachesBlock, ChecklistBlock, CodeBlock, DelimiterBlock, EmbedBlock, HeaderBlock, ImageBlock, ListBlock, MarkdownBlock, ParagraphBlock, QuoteBlock, TableBlock, WarningBlock
Constant Summary collapse
- InvalidBlockDataError =
Class.new(StandardError)
- InvalidBlockTypeError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#output_buffer ⇒ Object
ActionView::Helpers::TagHelper requires output_buffer accessor.
-
#raw ⇒ Object
ActionView::Helpers::TagHelper requires output_buffer accessor.
Class Method Summary collapse
Instance Method Summary collapse
- #css_name(name = nil) ⇒ Object
- #data ⇒ Object
- #decode_html(string) ⇒ Object
-
#initialize(raw) ⇒ Base
constructor
A new instance of Base.
- #output ⇒ Object
-
#plain ⇒ Object
Render plain text, for full-text searching.
-
#render(_options = {}) ⇒ Object
Render HTML.
-
#sanitize! ⇒ Object
Sanitize content of data.
-
#schema ⇒ Object
Define JSON format of data.
- #type ⇒ Object
-
#valid? ⇒ Boolean
Validate data.
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_buffer ⇒ Object
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 |
#raw ⇒ Object
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 |
.registry ⇒ Object
79 80 81 |
# File 'lib/editor_js/blocks/base.rb', line 79 def self.registry @registry ||= {} end |
.type ⇒ Object
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 |
#data ⇒ Object
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 |
#output ⇒ Object
58 59 60 |
# File 'lib/editor_js/blocks/base.rb', line 58 def output @content end |
#plain ⇒ Object
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
27 28 29 |
# File 'lib/editor_js/blocks/base.rb', line 27 def render( = {}) raise NotImplementedError end |
#sanitize! ⇒ Object
Sanitize content of data
32 |
# File 'lib/editor_js/blocks/base.rb', line 32 def sanitize!; end |
#schema ⇒ Object
Define JSON format of data
22 23 24 |
# File 'lib/editor_js/blocks/base.rb', line 22 def schema raise NotImplementedError end |
#type ⇒ Object
42 43 44 |
# File 'lib/editor_js/blocks/base.rb', line 42 def type self.class.type end |
#valid? ⇒ Boolean
Validate data
38 39 40 |
# File 'lib/editor_js/blocks/base.rb', line 38 def valid? JSON::Validator.validate(schema, data) end |