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, #copy, #cut, #descendants, #eq?, #find_all, #find_children, #find_first, #marks, #marks=, #node, #node_size, #nodes_between, #parse_content, #process_attrs_data, #raw_marks, #resolve, #text?, #text_content, #to_yaml
Constructor Details
#initialize(attributes = {}) ⇒ Table
Returns a new instance of Table.
20 21 22 23 |
# File 'lib/prosereflect/table.rb', line 20 def initialize(attributes = {}) attributes[:content] ||= [] super end |
Class Method Details
.create(attrs = nil) ⇒ Object
25 26 27 |
# File 'lib/prosereflect/table.rb', line 25 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
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/prosereflect/table.rb', line 54 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
66 67 68 69 70 71 72 73 |
# File 'lib/prosereflect/table.rb', line 66 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
76 77 78 79 80 |
# File 'lib/prosereflect/table.rb', line 76 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)
44 45 46 47 48 49 50 51 |
# File 'lib/prosereflect/table.rb', line 44 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
39 40 41 |
# File 'lib/prosereflect/table.rb', line 39 def data_rows rows[1..] || [] end |
#header_row ⇒ Object
35 36 37 |
# File 'lib/prosereflect/table.rb', line 35 def header_row rows.first end |
#rows ⇒ Object
29 30 31 32 33 |
# File 'lib/prosereflect/table.rb', line 29 def rows return [] unless content content end |
#to_h ⇒ Object
Override to_h to handle empty content and attributes properly
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/prosereflect/table.rb', line 83 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 |