Skip to main content

ignite_core/
editor.rs

1//! The rich text editor of [`FieldKind::Wysiwyg`](crate::FieldKind::Wysiwyg) fields.
2//!
3//! Ignite is MIT licensed and bundles no editor. The panel renders a wysiwyg field as a
4//! `<textarea data-ignite-editor="wysiwyg">` holding the HTML, and, when the app has
5//! registered an editor script, loads it on each form that has such a field. The script
6//! turns each marked textarea into an editor and keeps the textarea's value up to date, so
7//! the form submits the HTML as usual.
8//!
9//! ```ignore
10//! ignite_core::register_editor("/vendor/editor/init.js");
11//! ```
12
13use std::sync::{PoisonError, RwLock};
14
15static EDITOR: RwLock<Option<String>> = RwLock::new(None);
16
17/// Sets the URL of the script that turns wysiwyg textareas into an editor. The panel loads
18/// it with `<script src="..." defer>` on each form with a wysiwyg field, from the next request
19/// on. Registering again replaces the URL. With no script registered, a wysiwyg field is a
20/// plain textarea.
21pub fn register_editor(script_url: impl Into<String>) {
22    let mut editor = EDITOR.write().unwrap_or_else(PoisonError::into_inner);
23    *editor = Some(script_url.into());
24}
25
26/// The URL of the registered editor script, if the app registered one.
27pub fn editor_script() -> Option<String> {
28    EDITOR
29        .read()
30        .unwrap_or_else(PoisonError::into_inner)
31        .clone()
32}