Class: Prosereflect::Table
- Defined in:
- lib/prosereflect/table.rb
Overview
TODO: support for table attributes Table class represents a ProseMirror table. It contains rows, each of which can contain cells.
Constant Summary collapse
- PM_TYPE =
'table'
Class Method Summary collapse
Instance Method Summary collapse
-
#add_header(header_cells) ⇒ Object
Add a header row to the table.
-
#add_row(cell_contents = []) ⇒ Object
Add a data row to the table.
-
#add_rows(rows_data) ⇒ Object
Add multiple rows at once.
-
#cell_at(row_index, col_index) ⇒ Object
Get cell at specific position (skips header).
- #data_rows ⇒ Object
- #header_row ⇒ Object
-
#initialize(attributes = {}) ⇒ Table
constructor
A new instance of Table.
- #rows ⇒ Object
-
#to_h ⇒ Object
Override to_h to handle empty content and attributes properly.
Methods inherited from Node
#add_child, #find_all, #find_children, #find_first, #marks, #marks=, #parse_content, #process_attrs_data, #raw_marks, #text_content, #to_yaml
Constructor Details
#initialize(attributes = {}) ⇒ Table
Returns a new instance of Table.
22 23 24 25 |
# File 'lib/prosereflect/table.rb', line 22 def initialize(attributes = {}) attributes[:content] ||= [] super end |
Class Method Details
.create(attrs = nil) ⇒ Object
27 28 29 |
# File 'lib/prosereflect/table.rb', line 27 def self.create(attrs = nil) new(type: PM_TYPE, attrs: attrs, content: []) end |
Instance Method Details
#add_header(header_cells) ⇒ Object
Add a header row to the table
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/prosereflect/table.rb', line 56 def add_header(header_cells) row = TableRow.create header_cells.each do |cell_content| header = TableHeader.create header.add_paragraph(cell_content) row.add_child(header) end add_child(row) row end |
#add_row(cell_contents = []) ⇒ Object
Add a data row to the table
68 69 70 71 72 73 74 75 |
# File 'lib/prosereflect/table.rb', line 68 def add_row(cell_contents = []) row = TableRow.create cell_contents.each do |cell_content| row.add_cell(cell_content) end add_child(row) row end |
#add_rows(rows_data) ⇒ Object
Add multiple rows at once
78 79 80 81 82 |
# File 'lib/prosereflect/table.rb', line 78 def add_rows(rows_data) rows_data.each do |row_data| add_row(row_data) end end |
#cell_at(row_index, col_index) ⇒ Object
Get cell at specific position (skips header)
46 47 48 49 50 51 52 53 |
# File 'lib/prosereflect/table.rb', line 46 def cell_at(row_index, col_index) return nil if row_index.negative? || col_index.negative? data_row = data_rows[row_index] return nil unless data_row data_row.cells[col_index] end |
#data_rows ⇒ Object
41 42 43 |
# File 'lib/prosereflect/table.rb', line 41 def data_rows rows[1..] || [] end |
#header_row ⇒ Object
37 38 39 |
# File 'lib/prosereflect/table.rb', line 37 def header_row rows.first end |
#rows ⇒ Object
31 32 33 34 35 |
# File 'lib/prosereflect/table.rb', line 31 def rows return [] unless content content end |
#to_h ⇒ Object
Override to_h to handle empty content and attributes properly
85 86 87 88 89 90 91 92 93 |
# File 'lib/prosereflect/table.rb', line 85 def to_h result = super result['content'] ||= [] if result['attrs'] result['attrs'] = result['attrs'].is_a?(Hash) && result['attrs'][:attrs] ? result['attrs'][:attrs] : result['attrs'] result.delete('attrs') if result['attrs'].empty? end result end |