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, #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 = {}) ⇒ BulletList

Returns a new instance of BulletList.



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

def initialize(attributes = {})
  attributes[:content] ||= []
  # Only apply default if attrs key is completely absent
  unless attributes.key?(:attrs) || attributes.key?("attrs")
    attributes[:attrs] = { "bullet_style" => nil }
  end
  super
end

Class Method Details

.create(*args) ⇒ Object

Use *args to distinguish between create (no args) and create(nil) create with no args -> defaults applied create(nil) from parser -> no defaults, attrs explicitly nil



32
33
34
35
36
37
38
39
40
# File 'lib/prosereflect/bullet_list.rb', line 32

def self.create(*args)
  if args.empty?
    # No attrs provided - let initialize apply defaults
    new(type: PM_TYPE)
  else
    attrs = args[0]
    new({ type: PM_TYPE, attrs: attrs })
  end
end

Instance Method Details

#add_item(text) ⇒ Object



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

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



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

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

#bullet_styleObject



86
87
88
# File 'lib/prosereflect/bullet_list.rb', line 86

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

#bullet_style=(value) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/prosereflect/bullet_list.rb', line 78

def bullet_style=(value)
  @bullet_style = value
  return if value.nil?

  self.attrs ||= {}
  attrs["bullet_style"] = value
end

#item_at(index) ⇒ Object

Get item at specific position



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

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

  items[index]
end

#itemsObject



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

def items
  return [] unless content

  content
end

#text_contentObject

Get text content with proper formatting



70
71
72
73
74
75
76
# File 'lib/prosereflect/bullet_list.rb', line 70

def text_content
  return "" unless content

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