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::*;
use tracing::{debug, info};
use crate::components::styled_button::{ButtonVariant, StyledButton};
use crate::components::styled_icon::{Icon, StyledIcon};
@ -126,7 +127,10 @@ impl RteMsg {
| HeadingSize::H3
| HeadingSize::H4
| 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")),
},
RteMsg::InsertUnorderedList => Some(ExecCommand::new("insertUnorderedList")),
@ -150,7 +154,7 @@ impl RteMsg {
| RteMsg::TableSetVisibility(..) => None,
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(el) = el.dyn_into::<web_sys::HtmlElement>() {
if let Err(e) = el.focus() {
@ -271,13 +275,10 @@ impl StyledRteState {
match m.to_command() {
Some(ExecCommand { name, param }) => {
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>()
}) {
Some(doc) => doc,
_ => return,
};
if let Err(e) = doc.exec_command_with_show_ui_and_value(name, false, param) {
if let Err(e) =
html_document().exec_command_with_show_ui_and_value(name, false, param)
{
error!(e)
}
if self.restore_range().is_err() {
@ -286,6 +287,9 @@ impl StyledRteState {
self.schedule_focus(orders);
}
_ => match m {
RteMsg::InsertHeading(heading) => {
wrap_into(heading.as_str());
}
// code
RteMsg::InsertCode(b) => {
if *b {
@ -378,7 +382,7 @@ impl StyledRteState {
}
fn store_range(&mut self) {
self.range = seed::html_document()
self.range = html_document()
.get_selection()
.ok()
.unwrap_or(None)
@ -386,12 +390,11 @@ impl StyledRteState {
}
fn restore_range(&mut self) -> Result<(), String> {
let doc = seed::html_document();
let sel = doc
.get_selection()
.ok()
let sel = html_document().get_selection().ok();
let sel = sel
.unwrap_or(None)
.ok_or_else(|| "Restoring selection failed. Unable to obtain select".to_string())?;
sel.collapse_to_start();
let r = self
.range
.as_ref()
@ -996,7 +999,14 @@ impl<'outer> StyledRte<'outer> {
seed::div![
C!["tablePreview"],
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]
}
}
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()
}