Class: PandaCms::CodeComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- PandaCms::CodeComponent
- Defined in:
- app/components/panda_cms/code_component.rb
Overview
Text component
Constant Summary collapse
- KIND =
"code"
Instance Method Summary collapse
- #call ⇒ Object
- #component_is_editable? ⇒ Boolean
-
#initialize(key: :text_component, text: "", editable: true, **options) ⇒ CodeComponent
constructor
A new instance of CodeComponent.
- #is_embedded? ⇒ Boolean
Constructor Details
#initialize(key: :text_component, text: "", editable: true, **options) ⇒ CodeComponent
Returns a new instance of CodeComponent.
12 13 14 15 16 17 18 19 20 |
# File 'app/components/panda_cms/code_component.rb', line 12 def initialize(key: :text_component, text: "", editable: true, **) @key = key @text = text @options = || {} @options[:id] ||= "code-#{key.to_s.dasherize}" @editable = editable raise BlockError.new("Key 'code' is not allowed for CodeComponent") if key == :code end |
Instance Method Details
#call ⇒ Object
22 23 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 |
# File 'app/components/panda_cms/code_component.rb', line 22 def call # TODO: For the non-editable version, grab this from a cache or similar? block = PandaCms::Block.find_by(kind: KIND, key: @key, panda_cms_template_id: Current.page.panda_cms_template_id) if block.nil? raise PandaCms::MissingBlockError.new("Block with key #{@key} not found for page #{Current.page.title}") unless Rails.env.production? return false end block_content = block.block_contents.find_by(panda_cms_page_id: Current.page.id) code_content = block_content&.content.to_s if component_is_editable? @options[:contenteditable] = "plaintext-only" @options[:data] = { "editable-kind": "html", "editable-page-id": Current.page.id, "editable-block-content-id": block_content&.id } @options[:class] = "block bg-yellow-50 font-mono p-2 border-2 border-yellow-700" @options[:style] = "white-space: pre-wrap;" @options[:id] = "editor-#{block_content&.id}" # TODO: Switch between the HTML and the preview? content_tag(:div, code_content, @options, true) else code_content.html_safe end end |
#component_is_editable? ⇒ Boolean
52 53 54 55 |
# File 'app/components/panda_cms/code_component.rb', line 52 def component_is_editable? # TODO: Permissions @editable && && Current.user&.admin end |
#is_embedded? ⇒ Boolean
57 58 59 60 |
# File 'app/components/panda_cms/code_component.rb', line 57 def # TODO: Check security on this - embed_id should match something? request.params.dig(:embed_id).present? end |