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

Fix enter right after mention crashing the editor #904

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions crates/wysiwyg/src/composer_model/new_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,4 +480,28 @@ mod test {
</ul>"
);
}

#[test]
fn test_enter_before_mention() {
let mut model = cm(
r#"|<a data-mention-type="user" href="https://matrix.to/#/@carol:matrix.org" contenteditable="false">@carol</a>"#,
);
model.enter();
assert_eq!(
tx(&model),
r#"<p>&nbsp;</p><p>|<a data-mention-type="user" href="https://matrix.to/#/@carol:matrix.org" contenteditable="false">@carol</a></p>"#
)
}

#[test]
fn test_enter_after_mention() {
let mut model = cm(
r#"<a data-mention-type="user" href="https://matrix.to/#/@carol:matrix.org" contenteditable="false">@carol</a>|"#,
);
model.enter();
assert_eq!(
tx(&model),
r#"<p><a data-mention-type="user" href="https://matrix.to/#/@carol:matrix.org" contenteditable="false">@carol</a></p><p>&nbsp;|</p>"#
)
}
}
9 changes: 9 additions & 0 deletions crates/wysiwyg/src/dom/dom_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,10 +904,12 @@ where

let is_container_node: bool;
let is_text_node: bool;
let is_mention_node: bool;
{
let node = self.lookup_node(&cur_handle);
is_container_node = node.is_container_node();
is_text_node = node.is_text_node();
is_mention_node = node.is_mention_node();
}

if is_container_node {
Expand All @@ -926,6 +928,13 @@ where
from_handle,
to_handle,
));
} else if is_mention_node {
// Mentions only have 1 char length:
// If the offset is 0 the selection was before the node and the mention should be part of the new subtree.
// If it's 1 it should be kept in the current DOM (do nothing).
if start_offset == 0 {
nodes.push(self.remove(&cur_handle));
}
} else {
nodes.push(self.remove(&cur_handle));
}
Expand Down
Loading