Class: Practical::Views::TiptapDocumentComponent::Text

Inherits:
Node
  • Object
show all
Defined in:
app/components/practical/views/tiptap_document_component.rb

Constant Summary collapse

SORTED_MARKUP_TYPES =
[
  "rhino-strike", "link", "bold", "italic"
].freeze

Instance Attribute Summary

Attributes inherited from Node

#node_content

Instance Method Summary collapse

Methods inherited from Node

#initialize, #render_node_contents

Methods included from NodeRendering

#render_node

Methods included from ElementHelper

#grab, #mix

Constructor Details

This class inherits a constructor from Practical::Views::TiptapDocumentComponent::Node

Instance Method Details

#applicable_markup_typesObject



78
79
80
# File 'app/components/practical/views/tiptap_document_component.rb', line 78

def applicable_markup_types
  SORTED_MARKUP_TYPES.select{|type| node_content[:marks].any?{ |mark| mark[:type] == type }}
end

#callObject



82
83
84
85
86
87
88
# File 'app/components/practical/views/tiptap_document_component.rb', line 82

def call
  if node_content[:marks].present? && node_content[:marks].any?
    render_with_marks(markup_to_apply: applicable_markup_types)
  else
    render_plaintext
  end
end


108
109
110
# File 'app/components/practical/views/tiptap_document_component.rb', line 108

def link_attributes
  node_content[:marks]&.find{|mark| mark[:type] == "link" }&.dig(:attrs)&.slice(:href, :target, :rel).to_h
end

#render_plaintextObject



112
113
114
# File 'app/components/practical/views/tiptap_document_component.rb', line 112

def render_plaintext
  helpers.sanitize(node_content[:text])
end

#render_with_marks(markup_to_apply:) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/components/practical/views/tiptap_document_component.rb', line 90

def render_with_marks(markup_to_apply:)
  markup_type = markup_to_apply.shift
  case markup_type
  when "italic"
    tag.em{ render_with_marks(markup_to_apply: markup_to_apply) }
  when "bold"
    tag.strong { render_with_marks(markup_to_apply: markup_to_apply) }
  when "rhino-strike"
    tag.del { render_with_marks(markup_to_apply: markup_to_apply) }
  when "link"
    tag.a(**link_attributes) { render_with_marks(markup_to_apply: markup_to_apply) }
  when nil
    render_plaintext
  else
    raise UnknownMarkupTypeError
  end
end