LibWeb: Add support for range deletion.

This commit is contained in:
asynts 2020-12-01 23:36:12 +01:00 committed by Andreas Kling
parent bbcc5a9332
commit 43dc47a494
Notes: sideshowbarker 2024-07-19 00:57:20 +09:00
8 changed files with 107 additions and 6 deletions

View File

@ -39,6 +39,24 @@ Position::~Position()
{
}
Range Range::normalized() const
{
if (!is_valid())
return {};
if (m_start.node() == m_end.node()) {
if (m_start.offset() <= m_end.offset())
return *this;
return { m_end, m_start };
}
if (m_start.node()->is_before(*m_end.node()))
return *this;
return { m_end, m_start };
}
const LogStream& operator<<(const LogStream& stream, const Position& position)
{
if (!position.node())

View File

@ -62,6 +62,38 @@ private:
unsigned m_offset { 0 };
};
class Range {
public:
Range() = default;
Range(const Position& start, const Position& end)
: m_start(start)
, m_end(end)
{
}
bool is_valid() const { return m_start.is_valid() && m_end.is_valid(); }
void set(const Position& start, const Position& end)
{
m_start = start;
m_end = end;
}
void set_start(const Position& start) { m_start = start; }
void set_end(const Position& end) { m_end = end; }
const Position& start() const { return m_start; }
Position& start() { return m_start; }
const Position& end() const { return m_end; }
Position& end() { return m_end; }
Range normalized() const;
private:
Position m_start, m_end;
};
const LogStream& operator<<(const LogStream&, const Position&);
}

View File

@ -51,6 +51,7 @@ class MouseEvent;
class Node;
class ParentNode;
class Position;
class Range;
class Text;
class Timer;
class Window;

View File

@ -53,4 +53,12 @@ LayoutRange LayoutRange::normalized() const
return { m_end, m_start };
}
DOM::Range LayoutRange::to_dom_range() const
{
if (!is_valid())
return {};
return { m_start.to_dom_position(), m_end.to_dom_position() };
}
}

View File

@ -68,6 +68,8 @@ public:
LayoutRange normalized() const;
DOM::Range to_dom_range() const;
private:
LayoutPosition m_start;
LayoutPosition m_end;

View File

@ -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);

View File

@ -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:

View File

@ -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;