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//! registries.register_editor("/vendor/editor/init.js");
11//! ```
12
13use std::sync::PoisonError;
14
15use crate::registries::Registries;
16
17impl Registries {
18    /// Sets the URL of the script that turns wysiwyg textareas into an editor. The panel loads
19    /// it with `<script src="..." defer>` on each form with a wysiwyg field, from the next
20    /// request on. Registering again replaces the URL. With no script registered, a wysiwyg
21    /// field is a plain textarea.
22    pub fn register_editor(&self, script_url: impl Into<String>) {
23        *self
24            .inner
25            .editor
26            .write()
27            .unwrap_or_else(PoisonError::into_inner) = Some(script_url.into());
28    }
29
30    /// The URL of the registered editor script, if the app registered one.
31    pub fn editor_script(&self) -> Option<String> {
32        self.inner
33            .editor
34            .read()
35            .unwrap_or_else(PoisonError::into_inner)
36            .clone()
37    }
38}
39
40/// Sets the editor script of the default app, as 0.7.1 did. See
41/// [`Registries::register_editor`](crate::Registries::register_editor).
42pub fn register_editor(script_url: impl Into<String>) {
43    Registries::default_app().register_editor(script_url);
44}
45
46/// The editor script of the default app. See
47/// [`Registries::editor_script`](crate::Registries::editor_script).
48pub fn editor_script() -> Option<String> {
49    Registries::default_app().editor_script()
50}