Sha256: 970401641c3808b48a8a76f23a422de23608ff33d3a46d88395b10d1fdc9704a

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

module Panda
  module CMS
    module EditorJs
      module Blocks
        class Table < Base
          def render
            content = data["content"]
            with_headings = data["withHeadings"]

            html_safe(<<~HTML)
              <div class="overflow-x-auto">
                <table class="min-w-full">
                  #{render_rows(content, with_headings)}
                </table>
              </div>
            HTML
          end

          private

          def render_rows(content, with_headings)
            rows = []
            index = 0

            while index < content.length
              rows << if index == 0 && with_headings
                render_header_row(content[index])
              else
                render_data_row(content[index])
              end
              index += 1
            end

            rows.join("\n")
          end

          def render_header_row(row)
            cells = row.map { |cell| "<th>#{sanitize(cell)}</th>" }
            "<tr>#{cells.join}</tr>"
          end

          def render_data_row(row)
            cells = row.map { |cell| "<td>#{sanitize(cell)}</td>" }
            "<tr>#{cells.join}</tr>"
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
panda-cms-0.7.3 lib/panda/cms/editor_js/blocks/table.rb
panda-cms-0.7.2 lib/panda/cms/editor_js/blocks/table.rb
panda-cms-0.7.0 lib/panda/cms/editor_js/blocks/table.rb