Fix wrap in header

This commit is contained in:
Adrian Woźniak 2023-04-05 16:20:30 +02:00
parent fc3052630a
commit 63302fef71

View File

@ -1,5 +1,6 @@
use seed::prelude::*; use seed::prelude::*;
use seed::*; use seed::*;
use tracing::{debug, info};
use crate::components::styled_button::{ButtonVariant, StyledButton}; use crate::components::styled_button::{ButtonVariant, StyledButton};
use crate::components::styled_icon::{Icon, StyledIcon}; use crate::components::styled_icon::{Icon, StyledIcon};
@ -126,7 +127,10 @@ impl RteMsg {
| HeadingSize::H3 | HeadingSize::H3
| HeadingSize::H4 | HeadingSize::H4
| HeadingSize::H5 | HeadingSize::H5
| HeadingSize::H6 => Some(ExecCommand::new_with_param("heading", heading.as_str())), | HeadingSize::H6 => {
// Some(ExecCommand::new_with_param("heading", heading.as_str()))
None
}
HeadingSize::Normal => Some(ExecCommand::new_with_param("formatBlock", "div")), HeadingSize::Normal => Some(ExecCommand::new_with_param("formatBlock", "div")),
}, },
RteMsg::InsertUnorderedList => Some(ExecCommand::new("insertUnorderedList")), RteMsg::InsertUnorderedList => Some(ExecCommand::new("insertUnorderedList")),
@ -150,7 +154,7 @@ impl RteMsg {
| RteMsg::TableSetVisibility(..) => None, | RteMsg::TableSetVisibility(..) => None,
RteMsg::RequestFocus(identifier) => { RteMsg::RequestFocus(identifier) => {
let res = seed::document().query_selector(format!("#{}", identifier).as_str()); let res = document().query_selector(format!("#{}", identifier).as_str());
if let Ok(Some(el)) = res { if let Ok(Some(el)) = res {
if let Ok(el) = el.dyn_into::<web_sys::HtmlElement>() { if let Ok(el) = el.dyn_into::<web_sys::HtmlElement>() {
if let Err(e) = el.focus() { if let Err(e) = el.focus() {
@ -271,13 +275,10 @@ impl StyledRteState {
match m.to_command() { match m.to_command() {
Some(ExecCommand { name, param }) => { Some(ExecCommand { name, param }) => {
self.store_range(); self.store_range();
let doc = match web_sys::window().and_then(|w| w.document()).map(|d| {
wasm_bindgen::JsValue::from(d).unchecked_into::<web_sys::HtmlDocument>() if let Err(e) =
}) { html_document().exec_command_with_show_ui_and_value(name, false, param)
Some(doc) => doc, {
_ => return,
};
if let Err(e) = doc.exec_command_with_show_ui_and_value(name, false, param) {
error!(e) error!(e)
} }
if self.restore_range().is_err() { if self.restore_range().is_err() {
@ -286,6 +287,9 @@ impl StyledRteState {
self.schedule_focus(orders); self.schedule_focus(orders);
} }
_ => match m { _ => match m {
RteMsg::InsertHeading(heading) => {
wrap_into(heading.as_str());
}
// code // code
RteMsg::InsertCode(b) => { RteMsg::InsertCode(b) => {
if *b { if *b {
@ -378,7 +382,7 @@ impl StyledRteState {
} }
fn store_range(&mut self) { fn store_range(&mut self) {
self.range = seed::html_document() self.range = html_document()
.get_selection() .get_selection()
.ok() .ok()
.unwrap_or(None) .unwrap_or(None)
@ -386,12 +390,11 @@ impl StyledRteState {
} }
fn restore_range(&mut self) -> Result<(), String> { fn restore_range(&mut self) -> Result<(), String> {
let doc = seed::html_document(); let sel = html_document().get_selection().ok();
let sel = doc let sel = sel
.get_selection()
.ok()
.unwrap_or(None) .unwrap_or(None)
.ok_or_else(|| "Restoring selection failed. Unable to obtain select".to_string())?; .ok_or_else(|| "Restoring selection failed. Unable to obtain select".to_string())?;
sel.collapse_to_start();
let r = self let r = self
.range .range
.as_ref() .as_ref()
@ -996,7 +999,14 @@ impl<'outer> StyledRte<'outer> {
seed::div![ seed::div![
C!["tablePreview"], C!["tablePreview"],
seed::table![tbody![body]], seed::table![tbody![body]],
input![attrs![At::Type => "button"; At::Id => "rteInsertTable"; At::Value => "Insert"], on_submit], input![
attrs![
At::Type => "button";
At::Id => "rteInsertTable";
At::Value => "Insert"
],
on_submit
],
] ]
} }
], ],
@ -1114,3 +1124,15 @@ impl<'outer> StyledRte<'outer> {
span![C!["styledRteButton"], attrs![At::Title => title], button] span![C!["styledRteButton"], attrs![At::Title => title], button]
} }
} }
fn wrap_into(name: &str) -> Option<()> {
let sel = document().get_selection().ok()??;
let r = sel.get_range_at(0).ok()?;
let el: web_sys::Element = document().create_element(name).unwrap();
let node = el.dyn_ref::<web_sys::Node>().unwrap();
if let Err(e) = r.surround_contents(&node) {
error!("{}", e);
}
sel.collapse(Some(node)).ok()
}