Class: PandaCms::RichTextComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/panda_cms/rich_text_component.rb

Overview

Text component

Constant Summary collapse

KIND =
"rich_text"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key: :text_component, text: "Lorem ipsum...", editable: true, **options) ⇒ RichTextComponent

Returns a new instance of RichTextComponent.



16
17
18
19
20
21
# File 'app/components/panda_cms/rich_text_component.rb', line 16

def initialize(key: :text_component, text: "Lorem ipsum...", editable: true, **options)
  @key = key
  @text = text
  @options = options || {}
  @editable = editable
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



13
14
15
# File 'app/components/panda_cms/rich_text_component.rb', line 13

def content
  @content
end

#editableObject

Returns the value of attribute editable.



12
13
14
# File 'app/components/panda_cms/rich_text_component.rb', line 12

def editable
  @editable
end

#optionsObject

Returns the value of attribute options.



14
15
16
# File 'app/components/panda_cms/rich_text_component.rb', line 14

def options
  @options
end

Instance Method Details

#before_renderObject

Check if the element is editable and set up the content



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/components/panda_cms/rich_text_component.rb', line 24

def before_render
  @editable &&= params[:embed_id].present? && params[:embed_id] == Current.page.id && Current.user.admin?
  block = PandaCms::Block.find_by(kind: "rich_text", key: @key, panda_cms_template_id: Current.page.panda_cms_template_id)
  block_content = block.block_contents.find_by(panda_cms_page_id: Current.page.id)
  if block_content.nil?
    block_content = PandaCms::BlockContent.create(block: block, panda_cms_page_id: Current.page.id, content: "")
  end

  @content = block_content.cached_content || block_content.content
  @options[:id] = block_content.id

  if @editable
    @options[:data] = {
      page_id: Current.page.id,
      mode: "rich_text"
    }

    @content = block_content.content
  elsif @content.is_a?(Hash)
    renderer = PandaCms::EditorJs::Renderer.new(@content)
    @content = renderer.render
  else
    @content = @content.html_safe
  end
rescue => e
  if Rails.env.production?
    Sentry.capture_exception(e) if defined?(Sentry)
  else
    raise e
  end
  false
end

#render?Boolean

Only render the component if there is some content set, or if the component is editable

Returns:

  • (Boolean)


58
59
60
# File 'app/components/panda_cms/rich_text_component.rb', line 58

def render?
  @content.present? || @editable
end