Class: ProseMirror::Node
- Inherits:
-
Object
- Object
- ProseMirror::Node
- Defined in:
- lib/prose_mirror/node.rb
Overview
Represents a node in a ProseMirror document This can be a block node (paragraph, heading), inline node, or a text node
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#marks ⇒ Object
readonly
Returns the value of attribute marks.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#child(index) ⇒ Node?
Get a child node at the specified index.
-
#child_count ⇒ Integer
Get the number of child nodes.
-
#each_with_index {|node, index| ... } ⇒ Object
Iterate over each child node with its index.
-
#initialize(type, attrs = {}, content = [], marks = [], text = nil) ⇒ Node
constructor
Create a new Node.
-
#is_block ⇒ Boolean
Check if this is a block node.
-
#is_text ⇒ Boolean
Check if this is a text node.
-
#node_size ⇒ Integer
Get the size of this node For text nodes, returns the text length For other nodes, returns 1.
-
#text_content ⇒ String
Get the text content of this node For text nodes, returns the text directly For non-text nodes, returns the concatenated text content of all child nodes.
-
#with_text(new_text) ⇒ Node
Create a new text node with the given text but same marks.
Constructor Details
#initialize(type, attrs = {}, content = [], marks = [], text = nil) ⇒ Node
Create a new Node
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/prose_mirror/node.rb', line 13 def initialize(type, attrs = {}, content = [], marks = [], text = nil) @type = OpenStruct.new( name: type, is_block: true, is_leaf: content.empty? && !text, inline_content: !!text ) @attrs = attrs @content = content @marks = marks @text = text end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
5 6 7 |
# File 'lib/prose_mirror/node.rb', line 5 def attrs @attrs end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
5 6 7 |
# File 'lib/prose_mirror/node.rb', line 5 def content @content end |
#marks ⇒ Object (readonly)
Returns the value of attribute marks.
5 6 7 |
# File 'lib/prose_mirror/node.rb', line 5 def marks @marks end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
5 6 7 |
# File 'lib/prose_mirror/node.rb', line 5 def text @text end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
5 6 7 |
# File 'lib/prose_mirror/node.rb', line 5 def type @type end |
Instance Method Details
#child(index) ⇒ Node?
Get a child node at the specified index
55 56 57 |
# File 'lib/prose_mirror/node.rb', line 55 def child(index) @content[index] end |
#child_count ⇒ Integer
Get the number of child nodes
48 49 50 |
# File 'lib/prose_mirror/node.rb', line 48 def child_count @content.length end |
#each_with_index {|node, index| ... } ⇒ Object
Iterate over each child node with its index
71 72 73 |
# File 'lib/prose_mirror/node.rb', line 71 def each_with_index(&block) @content.each_with_index(&block) end |
#is_block ⇒ Boolean
Check if this is a block node
34 35 36 |
# File 'lib/prose_mirror/node.rb', line 34 def is_block @type.is_block end |
#is_text ⇒ Boolean
Check if this is a text node
28 29 30 |
# File 'lib/prose_mirror/node.rb', line 28 def is_text !@text.nil? end |
#node_size ⇒ Integer
Get the size of this node For text nodes, returns the text length For other nodes, returns 1
63 64 65 |
# File 'lib/prose_mirror/node.rb', line 63 def node_size @text ? @text.length : 1 end |
#text_content ⇒ String
Get the text content of this node For text nodes, returns the text directly For non-text nodes, returns the concatenated text content of all child nodes
42 43 44 |
# File 'lib/prose_mirror/node.rb', line 42 def text_content is_text ? @text : @content.map(&:text_content).join("") end |