mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-08-17 01:10:28 +03:00
Ladybird/AppKit: Implement pasting Web data to the clipboard
This commit is contained in:
parent
6732a38cf8
commit
9e637de58a
Notes:
sideshowbarker
2024-07-17 00:23:42 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/9e637de58a Pull-request: https://github.com/SerenityOS/serenity/pull/21878
@ -189,6 +189,15 @@ struct HideCursor {
|
||||
|
||||
#pragma mark - Private methods
|
||||
|
||||
static void copy_data_to_clipboard(StringView data, NSPasteboardType pasteboard_type)
|
||||
{
|
||||
auto* ns_data = Ladybird::string_to_ns_data(data);
|
||||
|
||||
auto* pasteBoard = [NSPasteboard generalPasteboard];
|
||||
[pasteBoard clearContents];
|
||||
[pasteBoard setData:ns_data forType:pasteboard_type];
|
||||
}
|
||||
|
||||
- (void)updateViewportRect:(Ladybird::WebViewBridge::ForResize)for_resize
|
||||
{
|
||||
auto content_rect = [self frame];
|
||||
@ -667,6 +676,21 @@ struct HideCursor {
|
||||
alpha:1.0];
|
||||
[self.observer onThemeColorChange:color];
|
||||
};
|
||||
|
||||
m_web_view_bridge->on_insert_clipboard_entry = [](auto const& data, auto const&, auto const& mime_type) {
|
||||
NSPasteboardType pasteboard_type = nil;
|
||||
|
||||
// https://w3c.github.io/clipboard-apis/#os-specific-well-known-format
|
||||
if (mime_type == "text/plain"sv)
|
||||
pasteboard_type = NSPasteboardTypeString;
|
||||
else if (mime_type == "text/html"sv)
|
||||
pasteboard_type = NSPasteboardTypeHTML;
|
||||
else if (mime_type == "text/png"sv)
|
||||
pasteboard_type = NSPasteboardTypePNG;
|
||||
|
||||
if (pasteboard_type)
|
||||
copy_data_to_clipboard(data, pasteboard_type);
|
||||
};
|
||||
}
|
||||
|
||||
- (void)colorPickerClosed:(NSNotification*)notification
|
||||
@ -679,18 +703,9 @@ struct HideCursor {
|
||||
return (NSScrollView*)[self superview];
|
||||
}
|
||||
|
||||
static void copy_text_to_clipboard(StringView text)
|
||||
{
|
||||
auto* string = Ladybird::string_to_ns_string(text);
|
||||
|
||||
auto* pasteBoard = [NSPasteboard generalPasteboard];
|
||||
[pasteBoard clearContents];
|
||||
[pasteBoard setString:string forType:NSPasteboardTypeString];
|
||||
}
|
||||
|
||||
- (void)copy:(id)sender
|
||||
{
|
||||
copy_text_to_clipboard(m_web_view_bridge->selected_text());
|
||||
copy_data_to_clipboard(m_web_view_bridge->selected_text(), NSPasteboardTypeString);
|
||||
}
|
||||
|
||||
- (void)selectAll:(id)sender
|
||||
@ -730,7 +745,7 @@ static void copy_text_to_clipboard(StringView text)
|
||||
|
||||
- (void)copyLink:(id)sender
|
||||
{
|
||||
copy_text_to_clipboard(m_context_menu_url.serialize());
|
||||
copy_data_to_clipboard(m_context_menu_url.serialize(), NSPasteboardTypeString);
|
||||
}
|
||||
|
||||
- (void)copyImage:(id)sender
|
||||
|
@ -20,6 +20,8 @@ namespace Ladybird {
|
||||
String ns_string_to_string(NSString*);
|
||||
NSString* string_to_ns_string(StringView);
|
||||
|
||||
NSData* string_to_ns_data(StringView);
|
||||
|
||||
NSDictionary* deserialize_json_to_dictionary(StringView);
|
||||
|
||||
Gfx::IntRect ns_rect_to_gfx_rect(NSRect);
|
||||
|
@ -16,8 +16,12 @@ String ns_string_to_string(NSString* string)
|
||||
|
||||
NSString* string_to_ns_string(StringView string)
|
||||
{
|
||||
auto* data = [NSData dataWithBytes:string.characters_without_null_termination() length:string.length()];
|
||||
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
||||
return [[NSString alloc] initWithData:string_to_ns_data(string) encoding:NSUTF8StringEncoding];
|
||||
}
|
||||
|
||||
NSData* string_to_ns_data(StringView string)
|
||||
{
|
||||
return [NSData dataWithBytes:string.characters_without_null_termination() length:string.length()];
|
||||
}
|
||||
|
||||
NSDictionary* deserialize_json_to_dictionary(StringView json)
|
||||
|
Loading…
Reference in New Issue
Block a user