Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
Replace paragraphs with line breaks in message output (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnyandrew committed Oct 18, 2023
1 parent f0c1347 commit 0663938
Show file tree
Hide file tree
Showing 29 changed files with 781 additions and 306 deletions.
26 changes: 13 additions & 13 deletions crates/wysiwyg/src/composer_model/delete_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,12 @@ where
};
}
match self.state.dom.lookup_node_mut(&location.node_handle) {
// we should never be passed a container
DomNode::Container(_) => ComposerUpdate::keep(),
DomNode::LineBreak(_) => {
// for a linebreak, remove it if we started the operation from the whitespace
// char type, otherwise keep it
match start_type {
CharType::Whitespace => self.delete_to_cursor(
direction.increment(location.index_in_dom()),
),
_ => ComposerUpdate::keep(),
}
}
DomNode::Container(_) | DomNode::LineBreak(_) => match start_type {
CharType::Whitespace => self.delete_to_cursor(
direction.increment(location.index_in_dom()),
),
_ => ComposerUpdate::keep(),
},
DomNode::Mention(_) => self
.delete_to_cursor(direction.increment(location.index_in_dom())),
DomNode::Text(node) => {
Expand Down Expand Up @@ -354,7 +348,13 @@ where
) -> Option<CharType> {
let node = self.state.dom.lookup_node(&location.node_handle);
match node {
DomNode::Container(_) => None,
DomNode::Container(node) => {
if node.is_empty() {
Some(CharType::Whitespace)
} else {
None
}
}
DomNode::LineBreak(_) => {
// we have to treat linebreaks as chars, this type fits best
Some(CharType::Whitespace)
Expand Down
149 changes: 110 additions & 39 deletions crates/wysiwyg/src/composer_model/example_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl ComposerModel<Utf16String> {
root.fmt_html(
&mut buf,
Some(&mut selection_writer),
ToHtmlState::default(),
&ToHtmlState::default(),
false,
);
if range.is_empty().not() {
Expand Down Expand Up @@ -591,24 +591,95 @@ mod test {

#[test]
fn cm_creates_correct_component_model_newlines() {
let t0 = cm("|<br />");
assert_eq!(t0.state.start, 0);
assert_eq!(t0.state.end, 0);
assert_eq!(t0.get_content_as_html(), utf16("<br />"));
// TODO: There should only be one node for the br tag
//assert_eq!(t0.state.dom.children().len(), 1);

let t1 = cm("<br />|<br />");
assert_eq!(t1.state.start, 1);
assert_eq!(t1.state.end, 1);
assert_eq!(t1.get_content_as_html(), utf16("<br /><br />"));
// TODO: assert_eq!(t1.state.dom.children().len(), 2);

let t2 = cm("<br /><br />|");
assert_eq!(t2.state.start, 2);
assert_eq!(t2.state.end, 2);
assert_eq!(t2.get_content_as_html(), utf16("<br /><br />"));
// TODO: assert_eq!(t1.state.dom.children().len(), 2);
assert_eq!(
cm("<br />|").get_content_as_html(),
"<p>\u{a0}</p><p>\u{a0}</p>"
);
assert_eq!(
cm("<br /><br />|").get_content_as_html(),
"<p>\u{a0}</p><p>\u{a0}</p><p>\u{a0}</p>"
);
assert_eq!(
cm("<br />|<br />").get_content_as_html(),
"<p>\u{a0}</p><p>\u{a0}</p><p>\u{a0}</p>"
);
assert_eq!(cm("a<br />b|").get_content_as_html(), "<p>a</p><p>b</p>");
assert_eq!(
cm("a<br />|<br />b").get_content_as_html(),
"<p>a</p><p>\u{a0}</p><p>b</p>"
);
assert_eq!(
cm("a<br />b|<br />c").get_content_as_html(),
"<p>a</p><p>b</p><p>c</p>"
);
assert_eq!(
cm("a<br />|b<br />c").get_content_as_html(),
"<p>a</p><p>b</p><p>c</p>"
);
assert_eq!(
cm("<b>a<br />|b<br />c</b>").get_content_as_html(),
"<p><b>a</b></p><p><b>b</b></p><p><b>c</b></p>"
);
assert_eq!(
cm("|<br />").get_content_as_html(),
"<p>\u{a0}</p><p>\u{a0}</p>"
);
assert_eq!(
cm("aaa<br />|bbb").get_content_as_html(),
"<p>aaa</p><p>bbb</p>"
);
assert_eq!(
cm("aaa|<br />bbb").get_content_as_html(),
"<p>aaa</p><p>bbb</p>"
);
assert_eq!(
cm("aa{a<br />b}|bb").get_content_as_html(),
"<p>aaa</p><p>bbb</p>"
);
assert_eq!(cm("aa{a<br />b}|bb").state.start, 2);
assert_eq!(cm("aa{a<br />b}|bb").state.end, 5);
assert_eq!(
cm("aa|{a<br />b}bb").get_content_as_html(),
"<p>aaa</p><p>bbb</p>"
);
assert_eq!(cm("aa|{a<br />b}bb").state.start, 5);
assert_eq!(cm("aa|{a<br />b}bb").state.end, 2);
assert_eq!(
cm("aa{<br />b}|bb").get_content_as_html(),
"<p>aa</p><p>bbb</p>"
);
assert_eq!(cm("aa{<br />b}|bb").state.start, 2);
assert_eq!(cm("aa{<br />b}|bb").state.end, 4);
assert_eq!(
cm("aa|{<br />b}bb").get_content_as_html(),
"<p>aa</p><p>bbb</p>"
);
assert_eq!(cm("aa|{<br />b}bb").state.start, 4);
assert_eq!(cm("aa|{<br />b}bb").state.end, 2);
assert_eq!(
cm("aa{a<br />b}|bb").get_content_as_html(),
"<p>aaa</p><p>bbb</p>"
);
assert_eq!(cm("aa{a<br />b}|bb").state.start, 2);
assert_eq!(cm("aa{a<br />b}|bb").state.end, 5);
assert_eq!(
cm("aa|{a<br />}bb").get_content_as_html(),
"<p>aaa</p><p>bb</p>"
);
assert_eq!(cm("aa|{a<br />}bb").state.start, 4);
assert_eq!(cm("aa|{a<br />}bb").state.end, 2);
assert_eq!(
cm("aa{<br />}|bb").get_content_as_html(),
"<p>aa</p><p>bb</p>"
);
assert_eq!(cm("aa{<br />}|bb").state.start, 2);
assert_eq!(cm("aa{<br />}|bb").state.end, 3);
assert_eq!(
cm("aa|{<br />}bb").get_content_as_html(),
"<p>aa</p><p>bb</p>"
);
assert_eq!(cm("aa|{<br />}bb").state.start, 3);
assert_eq!(cm("aa|{<br />}bb").state.end, 2);
}

#[test]
Expand Down Expand Up @@ -814,27 +885,27 @@ mod test {
assert_that!("AAA<b>B{BB</b>C}|CC").roundtrips();
assert_that!("AAA<b>B|{BB</b>C}CC").roundtrips();
assert_that!("<ul><li>~|</li></ul>").roundtrips();
assert_that!("<br />|").roundtrips();
assert_that!("<br /><br />|").roundtrips();
assert_that!("<br />|<br />").roundtrips();
assert_that!("<br />|<br />").roundtrips();
assert_that!("a<br />b|").roundtrips();
assert_that!("a<br />|<br />b").roundtrips();
assert_that!("a<br />b|<br />c").roundtrips();
assert_that!("a<br />|b<br />c").roundtrips();
assert_that!("<b>a<br />|b<br />c</b>").roundtrips();
assert_that!("|<br />").roundtrips();
assert_that!("aaa<br />|bbb").roundtrips();
assert_that!("aaa|<br />bbb").roundtrips();
assert_that!("aa{a<br />b}|bb").roundtrips();
assert_that!("aa|{a<br />b}bb").roundtrips();
assert_that!("aa{<br />b}|bb").roundtrips();
assert_that!("aa|{<br />b}bb").roundtrips();
assert_that!("aa{a<br />b}|bb").roundtrips();
assert_that!("aa|{a<br />}bb").roundtrips();
assert_that!("aa{<br />}|bb").roundtrips();
assert_that!("aa|{<br />}bb").roundtrips();
assert_that!("<ol><li>a|</li></ol>").roundtrips();
assert_that!("<p>aa{a</p><p>b}|bb</p>").roundtrips();
assert_that!("<p>aa|{a}</p><p>bb</p>").roundtrips();
assert_that!("<p> </p><p> |</p>").roundtrips();
assert_that!("<p> </p><p> |</p><p> </p>").roundtrips();
assert_that!("<p>a</p><p>b|</p>").roundtrips();
assert_that!("<p>a</p><p>|b</p>").roundtrips();
assert_that!("<p>a</p><p>b|</p><p>c</p>").roundtrips();
assert_that!("<p>a</p><p>|b</p><p>c</p>").roundtrips();
assert_that!("<p><b>a</b></p><p><b>|b</b></p><p><b>c</b></p>")
.roundtrips();
assert_that!("<p> |</p><p> </p>").roundtrips();
assert_that!("<p>aaa</p><p>|bbb</p>").roundtrips();
assert_that!("<p>aaa|</p><p>bbb</p>").roundtrips();
assert_that!("<p>aa{a</p><p>b}|bb</p>").roundtrips();
assert_that!("<p>aa|{a</p><p>b}bb</p>").roundtrips();
assert_that!("<p>aa</p><p>{b}|bb</p>").roundtrips();
assert_that!("<p>aa</p><p>|{b}bb</p>").roundtrips();
assert_that!("<p>aa|{a}</p><p>bb</p>").roundtrips();
assert_that!("<p>aa</p><p>|bb</p>").roundtrips();
assert_that!("<p>aa|</p><p>bb</p>").roundtrips();
}

#[test]
Expand Down
12 changes: 8 additions & 4 deletions crates/wysiwyg/src/composer_model/format_inline_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@ mod test {
fn inline_code_with_formatting_preserves_line_breaks() {
let mut model = cm("<b>{bold</b><br /><i>text}|</i>");
model.inline_code();
assert_eq!(tx(&model), "<code>{bold<br />text}|</code>");
assert_eq!(
tx(&model),
"<p><code>{bold</code></p><p><code>text}|</code></p>"
);
}

#[test]
Expand Down Expand Up @@ -249,7 +252,8 @@ mod test {
model.inline_code();
assert_eq!(
tx(&model),
"<b><u>bo</u></b><code>{ld<br />te}|</code><i>xt</i>"
"<p><b><u>bo</u></b><code>{ld</code></p><p><code>te}|</code><i>xt</i></p>",

);
}

Expand All @@ -260,7 +264,7 @@ mod test {
model.inline_code();
assert_eq!(
tx(&model),
"<b><u>bo</u></b><code>{ld<br />te}|</code><i>xt</i>"
"<p><b><u>bo</u></b><code>{ld</code></p><p><code>te}|</code><i>xt</i></p>",
);
}

Expand Down Expand Up @@ -328,7 +332,7 @@ mod test {
fn unformat_inline_code_same_row_with_line_breaks() {
let mut model = cm("<code>{bold<br />text}|</code>");
model.inline_code();
assert_eq!(tx(&model), "{bold<br />text}|");
assert_eq!(tx(&model), "<p>{bold</p><p>text}|</p>");
}

#[test]
Expand Down
5 changes: 4 additions & 1 deletion crates/wysiwyg/src/composer_model/new_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ where
Generic => {
self.do_new_line_in_paragraph(first_leaf, block_location);
}
_ => panic!("Unexpected kind block node with inline contents"),
_ => panic!(
"Unexpected kind {:?} with inline contents",
block_location.kind
),
}
self.create_update_replace_all()
}
Expand Down
4 changes: 2 additions & 2 deletions crates/wysiwyg/src/composer_model/quotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ mod test {
model.quote();
assert_eq!(
tx(&model),
"<blockquote><p><b>Some |text</b></p></blockquote><b><br />Next line</b>"
"<blockquote><p><b>Some |text</b></p></blockquote><p><b>Next line</b></p>"
)
}

Expand All @@ -263,7 +263,7 @@ mod test {
model.quote();
assert_eq!(
tx(&model),
"<blockquote><ul><li><b>Some {text<br />Next line</b></li><li><i>Second}| item</i></li></ul></blockquote><ul><li>Third item</li></ul>"
"<blockquote><ul><li><p><b>Some {text</b></p><p><b>Next line</b></p></li><li><i>Second}| item</i></li></ul></blockquote><ul><li>Third item</li></ul>"
)
}

Expand Down
21 changes: 12 additions & 9 deletions crates/wysiwyg/src/dom/dom_block_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,21 @@ mod test {
let model = cm("Some text| <b>and bold </b><br/><i>and italic</i>");
let (s, e) = model.safe_selection();
let ret = model.state.dom.find_nodes_to_wrap_in_block(s, e).unwrap();
assert_eq!(ret.ancestor_handle, DomHandle::from_raw(Vec::new()));
assert_eq!(ret.start_handle, DomHandle::from_raw(vec![0]));
assert_eq!(ret.end_handle, DomHandle::from_raw(vec![1, 0]));
// <br> has been converted to <p> hence the extra depth
assert_eq!(ret.ancestor_handle, DomHandle::from_raw(vec![]));
assert_eq!(ret.start_handle, DomHandle::from_raw(vec![0, 0]));
assert_eq!(ret.end_handle, DomHandle::from_raw(vec![0, 1, 0]));
}

#[test]
fn find_ranges_to_wrap_several_nodes_with_line_break_at_start() {
let model = cm("Some text <br/><b>and bold </b><i>|and italic</i>");
let (s, e) = model.safe_selection();
let ret = model.state.dom.find_nodes_to_wrap_in_block(s, e).unwrap();
assert_eq!(ret.ancestor_handle, DomHandle::from_raw(Vec::new()));
assert_eq!(ret.start_handle, DomHandle::from_raw(vec![2, 0]));
assert_eq!(ret.end_handle, DomHandle::from_raw(vec![3, 0]));
// <br> has been converted to <p> hence the extra depth
assert_eq!(ret.ancestor_handle, DomHandle::from_raw(vec![]));
assert_eq!(ret.start_handle, DomHandle::from_raw(vec![1, 0, 0]));
assert_eq!(ret.end_handle, DomHandle::from_raw(vec![1, 1, 0]));
}

#[test]
Expand All @@ -242,9 +244,10 @@ mod test {
);
let (s, e) = model.safe_selection();
let ret = model.state.dom.find_nodes_to_wrap_in_block(s, e).unwrap();
assert_eq!(ret.ancestor_handle, DomHandle::from_raw(vec![0, 0]));
assert_eq!(ret.start_handle, DomHandle::from_raw(vec![0, 0, 3, 0]));
assert_eq!(ret.end_handle, DomHandle::from_raw(vec![0, 0, 3, 0]));
// <br> has been converted to <p> hence the extra depth
assert_eq!(ret.ancestor_handle, DomHandle::from_raw(vec![0, 0, 1]));
assert_eq!(ret.start_handle, DomHandle::from_raw(vec![0, 0, 1, 0, 0]));
assert_eq!(ret.end_handle, DomHandle::from_raw(vec![0, 0, 1, 0, 0]));
}

#[test]
Expand Down
Loading

0 comments on commit 0663938

Please sign in to comment.