Class: ProseMirror::Node

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(type, attrs = {}, content = [], marks = [], text = nil) ⇒ Node

Create a new Node

Parameters:

  • type (String)

    The node type name

  • attrs (Hash) (defaults to: {})

    Node attributes

  • content (Array<Node>) (defaults to: [])

    Child nodes

  • marks (Array<Mark>) (defaults to: [])

    Node marks

  • text (String, nil) (defaults to: nil)

    Text content for text nodes, nil otherwise



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

#attrsObject (readonly)

Returns the value of attribute attrs.



5
6
7
# File 'lib/prose_mirror/node.rb', line 5

def attrs
  @attrs
end

#contentObject (readonly)

Returns the value of attribute content.



5
6
7
# File 'lib/prose_mirror/node.rb', line 5

def content
  @content
end

#marksObject (readonly)

Returns the value of attribute marks.



5
6
7
# File 'lib/prose_mirror/node.rb', line 5

def marks
  @marks
end

#textObject (readonly)

Returns the value of attribute text.



5
6
7
# File 'lib/prose_mirror/node.rb', line 5

def text
  @text
end

#typeObject (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

Parameters:

  • index (Integer)

    The index of the child node to retrieve

Returns:

  • (Node, nil)

    The child node, or nil if not found



55
56
57
# File 'lib/prose_mirror/node.rb', line 55

def child(index)
  @content[index]
end

#child_countInteger

Get the number of child nodes

Returns:

  • (Integer)

    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

Yields:

  • (node, index)

    Yields each child node and its index

Yield Parameters:

  • node (Node)

    The child node

  • index (Integer)

    The index of the child node



71
72
73
# File 'lib/prose_mirror/node.rb', line 71

def each_with_index(&block)
  @content.each_with_index(&block)
end

#is_blockBoolean

Check if this is a block node

Returns:

  • (Boolean)

    true if this is a block node, false otherwise



34
35
36
# File 'lib/prose_mirror/node.rb', line 34

def is_block
  @type.is_block
end

#is_textBoolean

Check if this is a text node

Returns:

  • (Boolean)

    true if this is a text node, false otherwise



28
29
30
# File 'lib/prose_mirror/node.rb', line 28

def is_text
  !@text.nil?
end

#node_sizeInteger

Get the size of this node For text nodes, returns the text length For other nodes, returns 1

Returns:

  • (Integer)

    The node size



63
64
65
# File 'lib/prose_mirror/node.rb', line 63

def node_size
  @text ? @text.length : 1
end

#text_contentString

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

Returns:

  • (String)

    The text content



42
43
44
# File 'lib/prose_mirror/node.rb', line 42

def text_content
  is_text ? @text : @content.map(&:text_content).join("")
end

#with_text(new_text) ⇒ Node

Create a new text node with the given text but same marks

Parameters:

  • new_text (String)

    The new text content

Returns:

  • (Node)

    A new text node with the same marks but different text



78
79
80
# File 'lib/prose_mirror/node.rb', line 78

def with_text(new_text)
  Node.new(@type.name, @attrs, [], @marks, new_text)
end