Class: Prosereflect::Document

Inherits:
Node
  • Object
show all
Defined in:
lib/prosereflect/document.rb

Overview

Document class represents a ProseMirror document.

Constant Summary collapse

PM_TYPE =
'doc'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#add_child, #find_all, #find_children, #find_first, #initialize, #marks, #marks=, #parse_content, #process_attrs_data, #raw_marks, #to_yaml

Constructor Details

This class inherits a constructor from Prosereflect::Node

Class Method Details

.create(attrs = nil) ⇒ Object



27
28
29
# File 'lib/prosereflect/document.rb', line 27

def self.create(attrs = nil)
  new(attrs: attrs, content: [])
end

Instance Method Details

#add_blockquote(attrs = nil) ⇒ Object

Add a blockquote to the document



111
112
113
114
115
# File 'lib/prosereflect/document.rb', line 111

def add_blockquote(attrs = nil)
  quote = Blockquote.new(attrs: attrs)
  add_child(quote)
  quote
end

#add_bullet_list(attrs = nil) ⇒ Object

Add a bullet list to the document



97
98
99
100
101
# File 'lib/prosereflect/document.rb', line 97

def add_bullet_list(attrs = nil)
  list = BulletList.new(attrs: attrs)
  add_child(list)
  list
end

#add_code_block_wrapper(attrs = nil) ⇒ Object

Add a code block wrapper to the document



125
126
127
128
129
# File 'lib/prosereflect/document.rb', line 125

def add_code_block_wrapper(attrs = nil)
  wrapper = CodeBlockWrapper.new(attrs: attrs)
  add_child(wrapper)
  wrapper
end

#add_heading(level) ⇒ Object

Add a heading to the document



56
57
58
59
60
# File 'lib/prosereflect/document.rb', line 56

def add_heading(level)
  heading = Heading.new(attrs: { 'level' => level })
  add_child(heading)
  heading
end

#add_horizontal_rule(attrs = nil) ⇒ Object

Add a horizontal rule to the document



118
119
120
121
122
# File 'lib/prosereflect/document.rb', line 118

def add_horizontal_rule(attrs = nil)
  hr = HorizontalRule.new(attrs: attrs)
  add_child(hr)
  hr
end

#add_image(src, alt = nil, _attrs = {}) ⇒ Object

Add an image to the document



80
81
82
83
84
85
86
# File 'lib/prosereflect/document.rb', line 80

def add_image(src, alt = nil, _attrs = {})
  image = Image.new
  image.src = src
  image.alt = alt if alt
  add_child(image)
  image
end

#add_ordered_list(attrs = nil) ⇒ Object

Add an ordered list to the document



104
105
106
107
108
# File 'lib/prosereflect/document.rb', line 104

def add_ordered_list(attrs = nil)
  list = OrderedList.new(attrs: attrs)
  add_child(list)
  list
end

#add_paragraph(text = nil, attrs = nil) ⇒ Object

Add a paragraph with text to the document



63
64
65
66
67
68
69
70
# File 'lib/prosereflect/document.rb', line 63

def add_paragraph(text = nil, attrs = nil)
  paragraph = Paragraph.new(attrs: attrs)

  paragraph.add_text(text) if text

  add_child(paragraph)
  paragraph
end

#add_table(attrs = nil) ⇒ Object

Add a table to the document



73
74
75
76
77
# File 'lib/prosereflect/document.rb', line 73

def add_table(attrs = nil)
  table = Table.new(attrs: attrs)
  add_child(table)
  table
end

#add_user(id) ⇒ Object

Add a user mention to the document



89
90
91
92
93
94
# File 'lib/prosereflect/document.rb', line 89

def add_user(id)
  user = User.new
  user.id = id
  add_child(user)
  user
end

#paragraphsObject



51
52
53
# File 'lib/prosereflect/document.rb', line 51

def paragraphs
  find_children(Paragraph)
end

#tablesObject



47
48
49
# File 'lib/prosereflect/document.rb', line 47

def tables
  find_children(Table)
end

#text_contentObject

Get plain text content from all nodes



132
133
134
135
136
# File 'lib/prosereflect/document.rb', line 132

def text_content
  return '' unless content

  content.map { |node| node.respond_to?(:text_content) ? node.text_content : '' }.join("\n").strip
end

#to_hObject

Override the to_h method to handle attribute arrays



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/prosereflect/document.rb', line 32

def to_h
  result = super

  # Handle array of attribute objects specially for serialization
  if attrs.is_a?(Array) && attrs.all? { |attr| attr.is_a?(Prosereflect::Attribute::Base) }
    attrs_hash = {}
    attrs.each do |attr|
      attrs_hash.merge!(attr.to_h)
    end
    result['attrs'] = attrs_hash unless attrs_hash.empty?
  end

  result
end