Current section

Files

Jump to
ckeditor5_phoenix lib components root_value_sentinel.ex
Raw

lib/components/root_value_sentinel.ex

defmodule CKEditor5.Components.RootValueSentinel do
@moduledoc """
This module defines a Phoenix component that serves as a sentinel for the root value
of the CKEditor 5 instance in a LiveView. It renders a hidden div element that holds
the current value of the editor, which can be used for synchronization between the client
and server.
"""
use Phoenix.Component
@doc """
Renders a hidden div element that contains the current value of the editor. This is used
as a sentinel for the editor's root value, allowing for synchronization between the client and server.
"""
attr :editor_id, :string,
required: true,
doc: "The ID for the editor instance."
attr :value, :string,
required: true,
doc: "The current value of the editor."
attr :root, :string,
required: false,
default: "main",
doc:
"The name of the root element for multi-root editors. If not provided, it defaults to 'main'."
attr :root_attrs, :map,
required: false,
default: %{},
doc:
"A map of HTML attributes to apply to the editor root element (e.g., class, data-*, etc.)."
def render(assigns) do
~H"""
<div
id={"#{@editor_id}_#{@root}_sentinel"}
style="display:none"
phx-hook="CKRootValueSentinel"
data-cke-editor-id={@editor_id}
data-cke-root-name={@root}
data-cke-value={@value}
data-cke-root-attrs={Jason.encode!(@root_attrs)}
>
</div>
"""
end
end