WebEditorClientGtk.cpp [plain text]
#include "config.h"
#include "WebEditorClient.h"
#include <WebCore/Document.h>
#include <WebCore/Editor.h>
#include <WebCore/EventNames.h>
#include <WebCore/Frame.h>
#include <WebCore/KeyboardEvent.h>
#include <WebCore/Pasteboard.h>
#include <WebCore/PlatformKeyboardEvent.h>
#include <WebCore/TextIterator.h>
#include <WebCore/markup.h>
#include <WebPage.h>
#include <wtf/Variant.h>
#include <wtf/glib/GRefPtr.h>
namespace WebKit {
using namespace WebCore;
bool WebEditorClient::handleGtkEditorCommand(Frame& frame, const String& command, bool allowTextInsertion)
{
if (command == "GtkInsertEmoji"_s) {
if (!allowTextInsertion)
return false;
m_page->showEmojiPicker(frame);
return true;
}
return false;
}
bool WebEditorClient::executePendingEditorCommands(Frame& frame, const Vector<WTF::String>& pendingEditorCommands, bool allowTextInsertion)
{
Vector<Variant<Editor::Command, String>> commands;
for (auto& commandString : pendingEditorCommands) {
if (commandString.startsWith("Gtk"))
commands.append(commandString);
else {
Editor::Command command = frame.editor().command(commandString);
if (command.isTextInsertion() && !allowTextInsertion)
return false;
commands.append(WTFMove(command));
}
}
for (auto& commandVariant : commands) {
if (WTF::holds_alternative<String>(commandVariant)) {
if (!handleGtkEditorCommand(frame, WTF::get<String>(commandVariant), allowTextInsertion))
return false;
} else {
auto& command = WTF::get<Editor::Command>(commandVariant);
if (!command.execute())
return false;
}
}
return true;
}
void WebEditorClient::handleKeyboardEvent(KeyboardEvent& event)
{
auto* platformEvent = event.underlyingPlatformEvent();
if (!platformEvent)
return;
if (platformEvent->handledByInputMethod())
return;
ASSERT(event.target());
auto* frame = downcast<Node>(event.target())->document().frame();
ASSERT(frame);
const Vector<String> pendingEditorCommands = platformEvent->commands();
if (!pendingEditorCommands.isEmpty()) {
if (platformEvent->type() == PlatformEvent::RawKeyDown) {
if (executePendingEditorCommands(*frame, pendingEditorCommands, false))
event.setDefaultHandled();
return;
}
if (executePendingEditorCommands(*frame, pendingEditorCommands, frame->editor().canEdit())) {
event.setDefaultHandled();
return;
}
}
if (!frame->editor().canEdit())
return;
if (event.type() != eventNames().keypressEvent)
return;
if (event.charCode() < ' ')
return;
if (platformEvent->controlKey() || platformEvent->altKey())
return;
if (frame->editor().insertText(platformEvent->text(), &event))
event.setDefaultHandled();
}
void WebEditorClient::updateGlobalSelection(Frame* frame)
{
if (!frame->selection().isRange())
return;
auto range = frame->selection().selection().toNormalizedRange();
if (!range)
return;
PasteboardWebContent pasteboardContent;
pasteboardContent.canSmartCopyOrDelete = false;
pasteboardContent.text = plainText(*range);
pasteboardContent.markup = serializePreservingVisualAppearance(frame->selection().selection(), ResolveURLs::YesExcludingLocalFileURLsForPrivacy);
Pasteboard::createForGlobalSelection()->write(pasteboardContent);
}
bool WebEditorClient::shouldShowUnicodeMenu()
{
return true;
}
}