Class: Prosereflect::ListItem
- Defined in:
- lib/prosereflect/list_item.rb
Overview
ListItem class represents a list item in ProseMirror.
Constant Summary collapse
- PM_TYPE =
"list_item"
Class Method Summary collapse
Instance Method Summary collapse
- #add_content(content) ⇒ Object
-
#add_hard_break(marks = nil) ⇒ Object
Add a hard break to the last paragraph, or create a new one if none exists.
- #add_paragraph(text = nil) ⇒ Object
-
#add_text(text, marks = nil) ⇒ Object
Add text to the last paragraph, or create a new one if none exists.
-
#initialize(attributes = {}) ⇒ ListItem
constructor
A new instance of ListItem.
-
#text_content ⇒ Object
Get plain text content from all nodes.
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?, #to_h, #to_yaml
Constructor Details
#initialize(attributes = {}) ⇒ ListItem
Returns a new instance of ListItem.
19 20 21 22 |
# File 'lib/prosereflect/list_item.rb', line 19 def initialize(attributes = {}) attributes[:content] ||= [] super end |
Class Method Details
.create(attrs = nil) ⇒ Object
24 25 26 |
# File 'lib/prosereflect/list_item.rb', line 24 def self.create(attrs = nil) new(attrs: attrs) end |
Instance Method Details
#add_content(content) ⇒ Object
35 36 37 |
# File 'lib/prosereflect/list_item.rb', line 35 def add_content(content) add_child(content) end |
#add_hard_break(marks = nil) ⇒ Object
Add a hard break to the last paragraph, or create a new one if none exists
48 49 50 51 52 53 |
# File 'lib/prosereflect/list_item.rb', line 48 def add_hard_break(marks = nil) last_paragraph = content&.last last_paragraph = add_paragraph if !last_paragraph || !last_paragraph.is_a?(Paragraph) last_paragraph.add_hard_break(marks) self end |
#add_paragraph(text = nil) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/prosereflect/list_item.rb', line 28 def add_paragraph(text = nil) paragraph = Paragraph.new paragraph.add_text(text) if text add_child(paragraph) paragraph end |
#add_text(text, marks = nil) ⇒ Object
Add text to the last paragraph, or create a new one if none exists
40 41 42 43 44 45 |
# File 'lib/prosereflect/list_item.rb', line 40 def add_text(text, marks = nil) last_paragraph = content&.last last_paragraph = add_paragraph if !last_paragraph || !last_paragraph.is_a?(Paragraph) last_paragraph.add_text(text, marks) self end |
#text_content ⇒ Object
Get plain text content from all nodes
56 57 58 59 60 61 62 |
# File 'lib/prosereflect/list_item.rb', line 56 def text_content return "" unless content content.map do |node| node.respond_to?(:text_content) ? node.text_content : "" end.join("\n").strip end |