Class: EditorJs::Blocks::ParagraphBlock

Inherits:
Base
  • Object
show all
Defined in:
lib/editor_js/blocks/paragraph_block.rb

Overview

paragraph block

Constant Summary

Constants inherited from Base

Base::InvalidBlockDataError, Base::InvalidBlockTypeError

Instance Attribute Summary

Attributes inherited from Base

#output_buffer, #raw

Instance Method Summary collapse

Methods inherited from Base

#css_name, #data, #decode_html, inherited, #initialize, load, #output, registry, type, #type, #valid?

Constructor Details

This class inherits a constructor from EditorJs::Blocks::Base

Instance Method Details

#plainObject



56
57
58
# File 'lib/editor_js/blocks/paragraph_block.rb', line 56

def plain
  decode_html(Sanitize.fragment data['text']).strip
end

#render(_options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/editor_js/blocks/paragraph_block.rb', line 23

def render(_options = {})
  alignment = data['alignment']
  class_name_str = css_name
  if alignment.present?
    class_name_str = [
      class_name_str,
      css_name("__#{alignment}")
    ].join(' ')
  end
  (:div, class: class_name_str) { data['text'].html_safe }
end

#safe_tagsObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/editor_js/blocks/paragraph_block.rb', line 35

def safe_tags
  {
    'b' => nil,
    'i' => nil,
    'u' => ['class'],
    'del' => ['class'],
    'a' => ['href'],
    'mark' => ['class'],
    'code' => ['class']
  }
end

#sanitize!Object



47
48
49
50
51
52
53
54
# File 'lib/editor_js/blocks/paragraph_block.rb', line 47

def sanitize!
  data['text'] = Sanitize.fragment(
    data['text'],
    elements: safe_tags.keys,
    attributes: safe_tags.select { |_k, v| v },
    remove_contents: true
  )
end

#schemaObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/editor_js/blocks/paragraph_block.rb', line 7

def schema
  YAML.safe_load(<<~YAML)
    type: object
    additionalProperties: false
    properties:
      text:
        type: string
      alignment:
        type: string
        enum:
          - align-left
          - align-center
          - align-right
  YAML
end