Remove RTE

This commit is contained in:
Adrian Woźniak 2023-04-13 15:22:28 +02:00
parent 1002f0c0e5
commit d509ca1397
3 changed files with 99 additions and 17 deletions

View File

@ -100,6 +100,9 @@ pub enum RteMsg {
InsertCode(bool), InsertCode(bool),
CodeChanged(String), CodeChanged(String),
InjectCode, InjectCode,
StoreSelection(web_sys::Selection),
ClearSelection,
} }
impl RteMsg { impl RteMsg {
@ -212,6 +215,7 @@ pub struct StyledRteState {
pub identifier: uuid::Uuid, pub identifier: uuid::Uuid,
pub heading_state: StyledSelectState, pub heading_state: StyledSelectState,
range: Option<web_sys::Range>, range: Option<web_sys::Range>,
selection: Option<web_sys::Selection>,
} }
impl StyledRteState { impl StyledRteState {
@ -228,6 +232,7 @@ impl StyledRteState {
range: None, range: None,
identifier: uuid::Uuid::new_v4(), identifier: uuid::Uuid::new_v4(),
heading_state: StyledSelectState::new(field_id, vec![]), heading_state: StyledSelectState::new(field_id, vec![]),
selection: None,
} }
} }
@ -237,6 +242,13 @@ impl StyledRteState {
let m = match msg { let m = match msg {
Msg::Rte(field, m) if field == &self.field_id => m, Msg::Rte(field, m) if field == &self.field_id => m,
Msg::StyledSelectChanged(id, StyledSelectChanged::Changed(Some(n)))
if id == &self.field_id =>
{
let size = HeadingSize::from_u32(*n);
self.try_wrap_into(size.as_str());
return;
}
_ => return, _ => return,
}; };
@ -313,7 +325,7 @@ impl StyledRteState {
if self.restore_range().is_err() { if self.restore_range().is_err() {
return; return;
} }
let doc = seed::html_document(); let doc = html_document();
let r = match self.range.as_ref() { let r = match self.range.as_ref() {
Some(r) => r, Some(r) => r,
_ => return, _ => return,
@ -327,6 +339,12 @@ impl StyledRteState {
error!(e); error!(e);
} }
} }
RteMsg::StoreSelection(sel) => {
self.selection = Some(sel.clone());
}
RteMsg::ClearSelection => {
self.selection = None;
}
}; };
// orders.skip().send_msg(Msg::StrInputChanged( // orders.skip().send_msg(Msg::StrInputChanged(
// self.field_id.clone(), // self.field_id.clone(),
@ -376,7 +394,12 @@ impl StyledRteState {
} }
fn try_wrap_into(&self, name: &str) -> Option<()> { fn try_wrap_into(&self, name: &str) -> Option<()> {
let sel = document().get_selection().ok()??; tracing::info!(">>>> {:?}", self.selection);
let sel = self
.selection
.clone()
.or(document().get_selection().ok().flatten())?;
let r = sel.get_range_at(0).ok()?; let r = sel.get_range_at(0).ok()?;
let start = r.start_container(); let start = r.start_container();
@ -427,7 +450,7 @@ impl StyledRteState {
if start == end if start == end
&& start && start
.dyn_ref::<web_sys::HtmlElement>() .dyn_ref::<HtmlElement>()
.filter(|x| x.tag_name() == name) .filter(|x| x.tag_name() == name)
.is_some() .is_some()
{ {
@ -438,10 +461,10 @@ impl StyledRteState {
); );
} }
let start = start.parent_element()?; let start = start.parent_element()?;
let start_parent = start.dyn_ref::<web_sys::HtmlElement>()?; let start_parent = start.dyn_ref::<HtmlElement>()?;
let end = end.parent_element()?; let end = end.parent_element()?;
let end_parent = end.dyn_ref::<web_sys::HtmlElement>()?; let end_parent = end.dyn_ref::<HtmlElement>()?;
if start_parent == end_parent && start_parent.tag_name().as_str() == name { if start_parent == end_parent && start_parent.tag_name().as_str() == name {
Some(start) Some(start)
@ -451,8 +474,13 @@ impl StyledRteState {
} }
fn is_in_rte(&self) -> Option<bool> { fn is_in_rte(&self) -> Option<bool> {
tracing::info!(">>>> {:?}", self.selection);
let id = self.identifier.to_string(); let id = self.identifier.to_string();
let sel = document().get_selection().ok()??; let sel = self
.selection
.clone()
.or(document().get_selection().ok().flatten())?;
let r = sel.get_range_at(0).ok()?; let r = sel.get_range_at(0).ok()?;
let mut current: web_sys::Node = r.start_container().ok()?; let mut current: web_sys::Node = r.start_container().ok()?;
while let Some(c) = current.parent_element() { while let Some(c) = current.parent_element() {
@ -614,12 +642,32 @@ impl<'outer> StyledRte<'outer> {
let first_row = self.first_row(click_handler.clone()); let first_row = self.first_row(click_handler.clone());
let click_detect_font_style = mouse_ev(Ev::Click, |ev| { let click_detect_font_style = {
let target = ev.target()?; let s = self.heading_state.unwrap().values.clone();
let el = to_html_el(&target); let id = self.field_id.clone();
tracing::info!("{:?} {:?}", el.tag_name(), el.id()); mouse_ev(Ev::Click, move |ev| {
None as Option<Msg> let target = ev.target()?;
}); let el = to_html_el(&target);
find_head_type(el.clone(), &s).map(|size| {
Msg::StyledSelectChanged(id, StyledSelectChanged::Changed(Some(size.as_u32())))
})
})
};
let store_selection = {
let id = self.field_id.clone();
ev(Ev::Blur, move |_e| {
window()
.get_selection()
.ok()
.flatten()
.map(|sel| Msg::Rte(id, RteMsg::StoreSelection(sel)))
})
};
let clear_selection = {
let id = self.field_id.clone();
ev(Ev::Focus, move |_e| Msg::Rte(id, RteMsg::ClearSelection))
};
div![ div![
C!["styledRte"], C!["styledRte"],
@ -631,7 +679,9 @@ impl<'outer> StyledRte<'outer> {
Tag::from("bq-rte"), Tag::from("bq-rte"),
C!["editor", self.field_id.to_str()], C!["editor", self.field_id.to_str()],
attrs![At::ContentEditable => true], attrs![At::ContentEditable => true],
click_detect_font_style click_detect_font_style,
store_selection,
clear_selection,
] ]
] ]
] ]
@ -1002,3 +1052,35 @@ impl<'outer> StyledRte<'outer> {
span![C!["styledRteButton"], attrs![At::Title => title], button] span![C!["styledRteButton"], attrs![At::Title => title], button]
} }
} }
fn find_head_type(mut el: HtmlElement, selected: &[u32]) -> Option<HeadingSize> {
let mut tag_name;
loop {
tag_name = el.tag_name();
if &tag_name == "BQ-RTE" && matches!(selected, &[] | &[0]) {
return None;
}
if &tag_name == "BODY" {
return None;
}
match tag_name.as_str() {
"H1" => return Some(HeadingSize::H1),
"H2" => return Some(HeadingSize::H2),
"H3" => return Some(HeadingSize::H3),
"H4" => return Some(HeadingSize::H4),
"H5" => return Some(HeadingSize::H5),
"H6" => return Some(HeadingSize::H6),
"BQ-RTE" if matches!(selected, &[] | &[0]) => return None,
"BQ-RTE" => return Some(HeadingSize::Normal),
"BODY" => return None,
_ => (),
}
let Some(parent) = el.parent_element() else {
break;
};
el = parent.dyn_into().unwrap();
}
None
}

View File

@ -25,7 +25,7 @@ use crate::{BuildMsg, FieldId, Msg};
pub struct CloseCreateIssueModal; pub struct CloseCreateIssueModal;
impl BuildMsg for CloseCreateIssueModal { impl BuildMsg for CloseCreateIssueModal {
fn build(&self, element: &Option<web_sys::Element>) -> Option<Msg> { fn build(&self, _element: &Option<web_sys::Element>) -> Option<Msg> {
Some(Msg::ModalDropped) Some(Msg::ModalDropped)
} }

View File

@ -82,7 +82,7 @@ pub fn view(model: &Model) -> Node<Msg> {
avatar, avatar,
username_field, username_field,
email_field, email_field,
editor_mode_select(page), // editor_mode_select(page),
current_project, current_project,
submit_field, submit_field,
], ],
@ -148,7 +148,7 @@ fn project_select_option(project: &Project) -> StyledSelectOption<'_> {
} }
} }
#[inline(always)] #[allow(dead_code)]
fn editor_mode_select(page: &ProfilePage) -> Node<Msg> { fn editor_mode_select(page: &ProfilePage) -> Node<Msg> {
let time_tracking = StyledCheckbox { let time_tracking = StyledCheckbox {
options: Some( options: Some(
@ -171,7 +171,7 @@ fn editor_mode_select(page: &ProfilePage) -> Node<Msg> {
.render() .render()
} }
#[inline(always)] #[allow(dead_code)]
fn editor_mode_checkbox_option<'l>( fn editor_mode_checkbox_option<'l>(
tem: TextEditorMode, tem: TextEditorMode,
state: &StyledCheckboxState, state: &StyledCheckboxState,