Module: TipTap::HasContent

Includes:
Enumerable
Included in:
Node
Defined in:
lib/tip_tap/has_content.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



9
10
11
# File 'lib/tip_tap/has_content.rb', line 9

def attrs
  @attrs
end

#contentObject (readonly)

Returns the value of attribute content.



9
10
11
# File 'lib/tip_tap/has_content.rb', line 9

def content
  @content
end

Class Method Details

.included(base) ⇒ Object



11
12
13
# File 'lib/tip_tap/has_content.rb', line 11

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#add_content(node) ⇒ Object Also known as: <<



34
35
36
# File 'lib/tip_tap/has_content.rb', line 34

def add_content(node)
  @content << node
end

#blank?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/tip_tap/has_content.rb', line 43

def blank?
  content&.all?(&:blank?)
end

#eachObject



25
26
27
# File 'lib/tip_tap/has_content.rb', line 25

def each
  content.each { |child| yield child }
end

#find_node(type_class_or_name) ⇒ Object



29
30
31
32
# File 'lib/tip_tap/has_content.rb', line 29

def find_node(type_class_or_name)
  node_type = type_class_or_name.is_a?(String) ? TipTap.node_for(type_class_or_name) : type_class_or_name
  find { |child| child.is_a?(node_type) }
end

#initialize(content = [], **attributes) {|_self| ... } ⇒ Object

Create a new document or node, optionally passing in attributes (attrs) and nodes

Parameters:

  • nodes (Array)

    An array of nodes to add to this node

  • attributes (Hash)

    A hash of attributes to add to this node e.g. { ‘level’ => 1 }

Yields:

  • (_self)

Yield Parameters:



18
19
20
21
22
23
# File 'lib/tip_tap/has_content.rb', line 18

def initialize(content = [], **attributes)
  # This will convert the attrs key to camelcase for example :image_id is converted into 'imageId'
  @attrs = Hash(attributes).deep_transform_keys { |key| key.to_s.camelcase(:lower) }
  @content = content
  yield self if block_given?
end

#sizeObject



39
40
41
# File 'lib/tip_tap/has_content.rb', line 39

def size
  content.size
end