Class: EditorJs::Blocks::TableBlock

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

Overview

table 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



59
60
61
62
# File 'lib/editor_js/blocks/table_block.rb', line 59

def plain
  str = data['content'].flatten.join(', ')
  decode_html Sanitize.fragment(str).gsub(/(, )+/, ', ').strip
end

#render(_options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/editor_js/blocks/table_block.rb', line 21

def render(_options = {})
  (:div, class: css_name) do
    (:table) do
      data['content'].map do |row|
        (:tr) do
          row.map { |col|  :td, col.html_safe }.join().html_safe
        end
      end.join().html_safe
    end
  end
end

#safe_tagsObject



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

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

#sanitize!Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/editor_js/blocks/table_block.rb', line 46

def sanitize!
  data['content'] = data['content'].map do |row|
    (row || []).map do |cell_value|
      Sanitize.fragment(
        cell_value,
        elements: safe_tags.keys,
        attributes: safe_tags.select { |_k, v| v },
        remove_contents: false
      )
    end
  end
end

#schemaObject



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

def schema
  YAML.safe_load(<<~YAML)
    type: object
    additionalProperties: false
    properties:
      content:
        type: array
        items:
          type: array
          items:
            type: string
  YAML
end