mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 19:17:35 +00:00
LibWeb: Add support for range deletion.
This commit is contained in:
parent
bbcc5a9332
commit
43dc47a494
8 changed files with 107 additions and 6 deletions
|
@ -30,10 +30,32 @@
|
|||
#include <LibWeb/Layout/LayoutPosition.h>
|
||||
#include <LibWeb/Page/Frame.h>
|
||||
|
||||
#include <LibWeb/DOM/Document.h>
|
||||
#include <LibWeb/Layout/InitialContainingBlockBox.h>
|
||||
|
||||
#include "EditEventHandler.h"
|
||||
|
||||
namespace Web {
|
||||
|
||||
void EditEventHandler::handle_delete(DOM::Range range)
|
||||
{
|
||||
if (range.start().node() != range.end().node())
|
||||
TODO();
|
||||
|
||||
if (is<DOM::Text>(*range.start().node())) {
|
||||
auto& node = downcast<DOM::Text>(*range.start().node());
|
||||
|
||||
StringBuilder builder;
|
||||
|
||||
builder.append(node.data().substring_view(0, range.start().offset()));
|
||||
builder.append(node.data().substring_view(range.end().offset()));
|
||||
node.set_data(builder.to_string());
|
||||
|
||||
m_frame.document()->layout_node()->set_selection({});
|
||||
node.invalidate_style();
|
||||
}
|
||||
}
|
||||
|
||||
void EditEventHandler::handle_delete(DOM::Position position)
|
||||
{
|
||||
if (position.offset() == 0)
|
||||
|
@ -41,6 +63,7 @@ void EditEventHandler::handle_delete(DOM::Position position)
|
|||
|
||||
if (is<DOM::Text>(*position.node())) {
|
||||
auto& node = downcast<DOM::Text>(*position.node());
|
||||
|
||||
StringBuilder builder;
|
||||
builder.append(node.data().substring_view(0, position.offset() - 1));
|
||||
builder.append(node.data().substring_view(position.offset()));
|
||||
|
@ -53,10 +76,9 @@ void EditEventHandler::handle_delete(DOM::Position position)
|
|||
|
||||
void EditEventHandler::handle_insert(DOM::Position position, u32 code_point)
|
||||
{
|
||||
// FIXME: Unicode fiasco.
|
||||
|
||||
if (is<DOM::Text>(*position.node())) {
|
||||
auto& node = downcast<DOM::Text>(*position.node());
|
||||
|
||||
StringBuilder builder;
|
||||
builder.append(node.data().substring_view(0, position.offset()));
|
||||
builder.append_code_point(code_point);
|
||||
|
|
|
@ -40,6 +40,7 @@ public:
|
|||
virtual ~EditEventHandler() = default;
|
||||
|
||||
virtual void handle_delete(DOM::Position);
|
||||
virtual void handle_delete(DOM::Range);
|
||||
virtual void handle_insert(DOM::Position, u32 code_point);
|
||||
|
||||
private:
|
||||
|
|
|
@ -345,16 +345,33 @@ bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_poin
|
|||
return focus_next_element();
|
||||
}
|
||||
|
||||
if (layout_root()->selection().is_valid()) {
|
||||
auto range = layout_root()->selection().to_dom_range();
|
||||
|
||||
if (key == KeyCode::Key_Backspace) {
|
||||
if (range.start().node()->is_editable()) {
|
||||
m_edit_event_handler->handle_delete(range);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: Check if this code point is in the printable character range.
|
||||
|
||||
m_edit_event_handler->handle_delete(range);
|
||||
m_edit_event_handler->handle_insert(m_frame.cursor_position(), code_point);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (m_frame.cursor_position().is_valid() && m_frame.cursor_position().node()->is_editable()) {
|
||||
if (key == KeyCode::Key_Backspace) {
|
||||
m_edit_event_handler->handle_delete(m_frame.cursor_position());
|
||||
return true;
|
||||
}
|
||||
|
||||
if (code_point) {
|
||||
m_edit_event_handler->handle_insert(m_frame.cursor_position(), code_point);
|
||||
return true;
|
||||
}
|
||||
// FIXME: Check if this code point is in the printable character range.
|
||||
|
||||
m_edit_event_handler->handle_insert(m_frame.cursor_position(), code_point);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue