not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
5.7 KiB

  1. /* Paste mode will paste the selection made with the copy mode */
  2. define([], function() {
  3. function initGui() {
  4. PaintApp.elements.pasteButton = document.getElementById('paste-button');
  5. PaintApp.paletteModesButtons.push(PaintApp.elements.pasteButton);
  6. PaintApp.elements.pasteButton.addEventListener('click', function() {
  7. PaintApp.paletteRemoveActiveClass();
  8. PaintApp.addActiveClassToElement(PaintApp.elements.pasteButton);
  9. PaintApp.switchMode('Paste');
  10. });
  11. }
  12. var Copy = {
  13. dataImage: undefined,
  14. initGui: initGui,
  15. onMouseDown: function(event) {
  16. return function() {
  17. /* We hide the floating element */
  18. if (PaintApp.data.currentElement) {
  19. PaintApp.data.currentElement.element.parentElement.removeChild(PaintApp.data.currentElement.element);
  20. PaintApp.data.currentElement = undefined;
  21. }
  22. /* Check for paste data */
  23. if (!PaintApp.modes.Paste.dataImage) {
  24. return;
  25. }
  26. PaintApp.modes.Paste.releasedFinger = false;
  27. PaintApp.handleCurrentFloatingElement();
  28. var left = event.point.x - PaintApp.modes.Paste.dataImage.width / 2 + 'px';
  29. var top = event.point.y + 55 - PaintApp.modes.Paste.dataImage.height / 2 + 'px';
  30. var ctx = PaintApp.elements.canvas.getContext('2d');
  31. var p = event.point;
  32. /* We create an img dom element where the user clicked */
  33. var element = document.createElement('img');
  34. element.src = PaintApp.modes.Paste.dataImage.data;
  35. element.style.width = PaintApp.modes.Paste.dataImage.width + 'px';
  36. element.style.height = PaintApp.modes.Paste.dataImage.height + 'px';
  37. element.style.position = 'absolute';
  38. element.style.left = left;
  39. element.style.padding = '0px';
  40. element.style.border = '5px dotted #500';
  41. element.style.top = top;
  42. document.body.appendChild(element);
  43. /* We store the floating element */
  44. PaintApp.data.currentElement = {
  45. type: 'paste',
  46. element: element,
  47. startPoint: {
  48. x: parseInt(element.style.left) + element.getBoundingClientRect().width / 2,
  49. y: parseInt(element.style.top) + element.getBoundingClientRect().height / 2
  50. }
  51. };
  52. /* This will prevent bugs for mobile platforms if the user just do a single tap we won't get onMouseDrag and onMouseUp correctly*/
  53. if (PaintApp.modes.Paste.releasedFinger) {
  54. ctx = PaintApp.elements.canvas.getContext('2d');
  55. /* We draw the copied area to the canvas where the user clicked */
  56. ctx.drawImage(PaintApp.data.currentElement.element, 5 + PaintApp.data.currentElement.element.getBoundingClientRect().left, PaintApp.data.currentElement.element.getBoundingClientRect().top - 55 + 5);
  57. /* If the activity is shared we send the element to everyone */
  58. if (PaintApp.data.isShared) {
  59. var drawImage = {
  60. src: PaintApp.collaboration.compress(PaintApp.data.currentElement.element.src),
  61. left: PaintApp.data.currentElement.element.getBoundingClientRect().left,
  62. top: PaintApp.data.currentElement.element.getBoundingClientRect().top - 55 + 5,
  63. width: PaintApp.data.currentElement.element.getBoundingClientRect().width,
  64. height: PaintApp.data.currentElement.element.getBoundingClientRect().height
  65. };
  66. PaintApp.collaboration.sendMessage({
  67. action: 'drawImage',
  68. data: drawImage
  69. })
  70. }
  71. PaintApp.data.currentElement.element.parentElement.removeChild(PaintApp.data.currentElement.element);
  72. PaintApp.data.currentElement = undefined;
  73. PaintApp.saveCanvas();
  74. }
  75. }();
  76. },
  77. onMouseDrag: function(event) {
  78. if (!PaintApp.data.currentElement) {
  79. return;
  80. }
  81. /* We move the floating pasting where the mouse is dragged */
  82. var left = event.point.x - PaintApp.modes.Paste.dataImage.width / 2 + 'px';
  83. var top = event.point.y + 55 - PaintApp.modes.Paste.dataImage.height / 2 + 'px';
  84. PaintApp.data.currentElement.element.style.left = left;
  85. PaintApp.data.currentElement.element.style.top = top;
  86. },
  87. onMouseUp: function(event) {
  88. PaintApp.modes.Paste.releasedFinger = true;
  89. if (!PaintApp.data.currentElement) {
  90. return;
  91. }
  92. var ctx = PaintApp.elements.canvas.getContext('2d');
  93. /* We draw the copied area to the canvas where the user clicked */
  94. ctx.drawImage(PaintApp.data.currentElement.element, PaintApp.data.currentElement.element.getBoundingClientRect().left, PaintApp.data.currentElement.element.getBoundingClientRect().top - 55, PaintApp.data.currentElement.element.getBoundingClientRect().width, PaintApp.data.currentElement.element.getBoundingClientRect().height);
  95. /* If the activity is shared we send the element to everyone */
  96. if (PaintApp.data.isShared) {
  97. var drawImage = {
  98. src: PaintApp.data.currentElement.element.src,
  99. left: PaintApp.data.currentElement.element.getBoundingClientRect().left,
  100. top: PaintApp.data.currentElement.element.getBoundingClientRect().top - 55,
  101. width: PaintApp.data.currentElement.element.getBoundingClientRect().width,
  102. height: PaintApp.data.currentElement.element.getBoundingClientRect().height
  103. };
  104. PaintApp.collaboration.sendMessage({
  105. action: 'drawImage',
  106. data: drawImage
  107. })
  108. }
  109. PaintApp.data.currentElement.element.parentElement.removeChild(PaintApp.data.currentElement.element);
  110. PaintApp.data.currentElement = undefined;
  111. PaintApp.saveCanvas();
  112. }
  113. };
  114. return Copy;
  115. });