Class: TipTap::Nodes::Text

Inherits:
TipTap::Node show all
Defined in:
lib/tip_tap/nodes/text.rb

Instance Attribute Summary collapse

Attributes included from HtmlRenderable

#output_buffer

Attributes included from HasContent

#attrs, #content

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JsonRenderable

#include_empty_content_in_json?

Methods included from HtmlRenderable

#html_class_name, #html_tag, included

Methods included from HasContent

#add_content, #blank?, #each, #find_node, included, #size

Methods included from Registerable

included, #type_name

Constructor Details

#initialize(content, **attributes) {|_self| ... } ⇒ Text

Returns a new instance of Text.

Yields:

  • (_self)

Yield Parameters:



12
13
14
15
16
# File 'lib/tip_tap/nodes/text.rb', line 12

def initialize(content, **attributes)
  @text = content
  @marks = Array(attributes[:marks]).map(&:deep_stringify_keys)
  yield self if block_given?
end

Instance Attribute Details

#marksObject (readonly)

Returns the value of attribute marks.



8
9
10
# File 'lib/tip_tap/nodes/text.rb', line 8

def marks
  @marks
end

#textObject (readonly)

Returns the value of attribute text.



8
9
10
# File 'lib/tip_tap/nodes/text.rb', line 8

def text
  @text
end

Class Method Details

.from_json(json) ⇒ Object



18
19
20
21
22
# File 'lib/tip_tap/nodes/text.rb', line 18

def self.from_json(json)
  json.deep_stringify_keys!

  new(json["text"], marks: Array(json["marks"]))
end

Instance Method Details

#bold?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/tip_tap/nodes/text.rb', line 53

def bold?
  has_mark_with_type?("bold")
end

#code?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/tip_tap/nodes/text.rb', line 69

def code?
  has_mark_with_type?("code")
end

#highlight?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/tip_tap/nodes/text.rb', line 81

def highlight?
  has_mark_with_type?("highlight")
end

#italic?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/tip_tap/nodes/text.rb', line 49

def italic?
  has_mark_with_type?("italic")
end

#link?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/tip_tap/nodes/text.rb', line 61

def link?
  has_mark_with_type?("link")
end

#strike?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/tip_tap/nodes/text.rb', line 65

def strike?
  has_mark_with_type?("strike")
end

#subscript?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/tip_tap/nodes/text.rb', line 77

def subscript?
  has_mark_with_type?("subscript")
end

#superscript?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/tip_tap/nodes/text.rb', line 73

def superscript?
  has_mark_with_type?("superscript")
end

#text_style?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/tip_tap/nodes/text.rb', line 85

def text_style?
  has_mark_with_type?("textStyle")
end

#to_hObject



24
25
26
27
28
# File 'lib/tip_tap/nodes/text.rb', line 24

def to_h
  data = {type: type_name, text: text || ""}
  data[:marks] = marks.map(&:deep_symbolize_keys) unless marks.empty?
  data
end

#to_htmlObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tip_tap/nodes/text.rb', line 30

def to_html
  value = text
  value = (:sup, value) if superscript?
  value = (:sub, value) if subscript?
  value = highlight_tag(value) if highlight?
  value = (:code, value) if code?
  value = (:u, value) if underline?
  value = (:em, value) if italic?
  value = (:strong, value) if bold?
  value = (:s, value) if strike?
  value = (:a, value, href: link_href, target: link_target) if link?
  value = (:span, value, style: inline_style_content(text_styles)) if text_style?
  value
end

#to_plain_text(separator: " ") ⇒ Object



45
46
47
# File 'lib/tip_tap/nodes/text.rb', line 45

def to_plain_text(separator: " ")
  text
end

#underline?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/tip_tap/nodes/text.rb', line 57

def underline?
  has_mark_with_type?("underline")
end