Class: Prosereflect::BulletList

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

Overview

BulletList class represents an unordered list in ProseMirror.

Constant Summary collapse

PM_TYPE =
'bullet_list'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

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

Constructor Details

#initialize(attributes = {}) ⇒ BulletList

Returns a new instance of BulletList.



21
22
23
24
25
# File 'lib/prosereflect/bullet_list.rb', line 21

def initialize(attributes = {})
  attributes[:content] ||= []
  attributes[:attrs] ||= { 'bullet_style' => nil }
  super
end

Class Method Details

.create(attrs = nil) ⇒ Object



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

def self.create(attrs = nil)
  new(attrs: attrs)
end

Instance Method Details

#add_item(text) ⇒ Object



41
42
43
44
45
46
# File 'lib/prosereflect/bullet_list.rb', line 41

def add_item(text)
  item = ListItem.new
  item.add_paragraph(text)
  add_child(item)
  item
end

#add_items(items_content) ⇒ Object

Add multiple items at once



55
56
57
58
59
# File 'lib/prosereflect/bullet_list.rb', line 55

def add_items(items_content)
  items_content.each do |item_content|
    add_item(item_content)
  end
end

#bullet_styleObject



37
38
39
# File 'lib/prosereflect/bullet_list.rb', line 37

def bullet_style
  @bullet_style || attrs&.[]('bullet_style')
end

#bullet_style=(value) ⇒ Object



31
32
33
34
35
# File 'lib/prosereflect/bullet_list.rb', line 31

def bullet_style=(value)
  @bullet_style = value
  self.attrs ||= {}
  attrs['bullet_style'] = value
end

#item_at(index) ⇒ Object

Get item at specific position



62
63
64
65
66
# File 'lib/prosereflect/bullet_list.rb', line 62

def item_at(index)
  return nil if index.negative?

  items[index]
end

#itemsObject



48
49
50
51
52
# File 'lib/prosereflect/bullet_list.rb', line 48

def items
  return [] unless content

  content
end

#text_contentObject

Get text content with proper formatting



69
70
71
72
73
# File 'lib/prosereflect/bullet_list.rb', line 69

def text_content
  return '' unless content

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

#to_hObject

Override to_h to exclude empty attrs



76
77
78
79
80
81
# File 'lib/prosereflect/bullet_list.rb', line 76

def to_h
  hash = super
  hash['attrs'] ||= {}
  hash['attrs']['bullet_style'] = bullet_style
  hash
end