WebGLCopyAndPaste.jslib 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2020, Gregg Tavares.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Gregg Tavares. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. mergeInto(LibraryManager.library, {
  32. initWebGLCopyAndPaste__postset: '_initWebGLCopyAndPaste();',
  33. initWebGLCopyAndPaste: function() {
  34. // for some reason only on Safari does Unity call
  35. // preventDefault so let's prevent preventDefault
  36. // so the browser will generate copy and paste events
  37. window.addEventListener = function(origFn) {
  38. function noop() {}
  39. // I hope c,x,v are universal
  40. const keys = {'c': true, 'x': true, 'v': true};
  41. // Emscripten doesn't support the spread operator or at
  42. // least the one used by Unity 2019.4.1
  43. return function(name, fn) {
  44. const args = Array.prototype.slice.call(arguments);
  45. if (name !== 'keypress') {
  46. return origFn.apply(this, args);
  47. }
  48. args[1] = function(event) {
  49. const hArgs = Array.prototype.slice.call(arguments);
  50. if (keys[event.key.toLowerCase()] &&
  51. ((event.metaKey ? 1 : 0) + (event.ctrlKey ? 1 : 0)) === 1) {
  52. event.preventDefault = noop;
  53. }
  54. return fn.apply(this, hArgs);
  55. };
  56. return origFn.apply(this, args);
  57. };
  58. }(window.addEventListener);
  59. _initWebGLCopyAndPaste = function(objectNamePtr, cutCopyFuncNamePtr, pasteFuncNamePtr) {
  60. window.becauseUnityIsBadWithJavascript_webglCopyAndPaste =
  61. window.becauseUnityIsBadWithJavascript_webglCopyAndPaste || {
  62. initialized: false,
  63. objectName: Pointer_stringify(objectNamePtr),
  64. cutCopyFuncName: Pointer_stringify(cutCopyFuncNamePtr),
  65. pasteFuncName: Pointer_stringify(pasteFuncNamePtr),
  66. };
  67. const g = window.becauseUnityIsBadWithJavascript_webglCopyAndPaste;
  68. if (!g.initialized) {
  69. window.addEventListener('cut', function(e) {
  70. e.preventDefault();
  71. SendMessage(g.objectName, g.cutCopyFuncName, 'x');
  72. event.clipboardData.setData('text/plain', g.clipboardStr);
  73. });
  74. window.addEventListener('copy', function(e) {
  75. e.preventDefault();
  76. SendMessage(g.objectName, g.cutCopyFuncName, 'c');
  77. event.clipboardData.setData('text/plain', g.clipboardStr);
  78. });
  79. window.addEventListener('paste', function(e) {
  80. const str = e.clipboardData.getData('text');
  81. SendMessage(g.objectName, g.pasteFuncName, str);
  82. });
  83. }
  84. };
  85. },
  86. passCopyToBrowser: function(stringPtr) {
  87. const g = window.becauseUnityIsBadWithJavascript_webglCopyAndPaste;
  88. const str = Pointer_stringify(stringPtr);
  89. g.clipboardStr = str;
  90. },
  91. });