Sha256: 681e6639cd70cae27b009cd20c8f3ff7310ace538b1d85bcc966b81a616478b7

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module PandaCms
  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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
panda_cms-0.6.3 app/lib/panda_cms/editor_js/blocks/table.rb
panda_cms-0.6.2 app/lib/panda_cms/editor_js/blocks/table.rb
panda_cms-0.6.1 app/lib/panda_cms/editor_js/blocks/table.rb