Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial Javascript clipboard support #29298

Merged
merged 3 commits into from
Jun 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions platform/javascript/os_javascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,47 @@ const char *OS_JavaScript::get_audio_driver_name(int p_driver) const {
return "JavaScript";
}

// Clipboard
extern "C" EMSCRIPTEN_KEEPALIVE void update_clipboard(const char *p_text) {
// Only call set_clipboard from OS (sets local clipboard)
OS::get_singleton()->OS::set_clipboard(p_text);
}

void OS_JavaScript::set_clipboard(const String &p_text) {
OS::set_clipboard(p_text);
/* clang-format off */
int err = EM_ASM_INT({
var text = UTF8ToString($0);
if (!navigator.clipboard || !navigator.clipboard.writeText)
return 1;
navigator.clipboard.writeText(text).catch(e => {
// Setting OS clipboard is only possible from an input callback.
console.error("Setting OS clipboard is only possible from an input callback for the HTML5 plafrom. Exception:", e);
});
return 0;
}, p_text.utf8().get_data());
/* clang-format on */
ERR_EXPLAIN("Clipboard API is not supported.");
ERR_FAIL_COND(err);
}

String OS_JavaScript::get_clipboard() const {
/* clang-format off */
EM_ASM({
try {
navigator.clipboard.readText().then(function (result) {
ccall('update_clipboard', 'void', ['string'], [result]);
}).catch(function (e) {
// Fail graciously.
});
} catch (e) {
// Fail graciously.
}
});
/* clang-format on */
return this->OS::get_clipboard();
}

// Lifecycle
int OS_JavaScript::get_current_video_driver() const {
return video_driver_index;
Expand Down Expand Up @@ -939,6 +980,11 @@ Error OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver,
(['mouseover', 'mouseleave', 'focus', 'blur']).forEach(function(event, index) {
Module.canvas.addEventListener(event, send_notification.bind(null, notifications[index]));
});
// Clipboard
const update_clipboard = cwrap('update_clipboard', null, ['string']);
window.addEventListener('paste', function(evt) {
update_clipboard(evt.clipboardData.getData('text'));
}, true);
},
MainLoop::NOTIFICATION_WM_MOUSE_ENTER,
MainLoop::NOTIFICATION_WM_MOUSE_EXIT,
Expand Down
3 changes: 3 additions & 0 deletions platform/javascript/os_javascript.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ class OS_JavaScript : public OS_Unix {
virtual int get_audio_driver_count() const;
virtual const char *get_audio_driver_name(int p_driver) const;

virtual void set_clipboard(const String &p_text);
virtual String get_clipboard() const;

virtual MainLoop *get_main_loop() const;
void run_async();
bool main_loop_iterate();
Expand Down