LibWeb+WebContent+Ladybird: Add ability to paste text from clipboard

Text can be pasted by pressing Ctrl/Cmd+V or by using button in the
context menu. For now only the Qt client is supported.
This commit is contained in:
Aliaksandr Kalenik 2024-03-22 11:56:49 +01:00 committed by Tim Flynn
commit 561e011e07
Notes: sideshowbarker 2024-07-16 23:13:25 +09:00
15 changed files with 82 additions and 4 deletions

View file

@ -93,13 +93,20 @@ void EditEventHandler::handle_delete(DOM::Range& range)
}
void EditEventHandler::handle_insert(JS::NonnullGCPtr<DOM::Position> position, u32 code_point)
{
StringBuilder builder;
builder.append_code_point(code_point);
handle_insert(position, MUST(builder.to_string()));
}
void EditEventHandler::handle_insert(JS::NonnullGCPtr<DOM::Position> position, String data)
{
if (is<DOM::Text>(*position->node())) {
auto& node = verify_cast<DOM::Text>(*position->node());
StringBuilder builder;
builder.append(node.data().bytes_as_string_view().substring_view(0, position->offset()));
builder.append_code_point(code_point);
builder.append(data);
builder.append(node.data().bytes_as_string_view().substring_view(position->offset()));
// Cut string by max length
@ -113,9 +120,7 @@ void EditEventHandler::handle_insert(JS::NonnullGCPtr<DOM::Position> position, u
} else {
auto& node = *position->node();
auto& realm = node.realm();
StringBuilder builder;
builder.append_code_point(code_point);
auto text = realm.heap().allocate<DOM::Text>(realm, node.document(), MUST(builder.to_string()));
auto text = realm.heap().allocate<DOM::Text>(realm, node.document(), data);
MUST(node.append_child(*text));
position->set_node(text);
position->set_offset(1);