Sha256: 85e7233712c4cca25a49a3699406ab39dc316068f84a66ce0b1c9a7ee20bac7e

Contents?: true

Size: 1.07 KB

Versions: 11

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module EditorJs
  module Blocks
    class TableBlock < Base
      def schema
        YAML.safe_load(<<~YAML)
          type: object
          additionalProperties: false
          properties:
            content:
              type: array
              items:
                type: array
                items:
                  type: string
        YAML
      end

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

      def sanitize!
        data['content'] = data['content'].map do |row|
          row = (row || []).map do |cell_value|
            Sanitize.fragment(cell_value, remove_contents: true)
          end
        end
      end

      def plain
        decode_html data['content'].flatten.join(', ').gsub(/(, )+/, ', ')
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
editor_js-0.3.3 lib/editor_js/blocks/table_block.rb
editor_js-0.3.2 lib/editor_js/blocks/table_block.rb
editor_js-0.3.1 lib/editor_js/blocks/table_block.rb
editor_js-0.3.0 lib/editor_js/blocks/table_block.rb
editor_js-0.2.5 lib/editor_js/blocks/table_block.rb
editor_js-0.2.4 lib/editor_js/blocks/table_block.rb
editor_js-0.2.3 lib/editor_js/blocks/table_block.rb
editor_js-0.2.2 lib/editor_js/blocks/table_block.rb
editor_js-0.2.1 lib/editor_js/blocks/table_block.rb
editor_js-0.2.0 lib/editor_js/blocks/table_block.rb
editor_js-0.1.0 lib/editor_js/blocks/table_block.rb