Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * WHITEBOPHIR
  3. *********************************************************
  4. * @licstart The following is the entire license notice for the
  5. * JavaScript code in this page.
  6. *
  7. * Copyright (C) 2013 Ophir LOJKINE
  8. *
  9. *
  10. * The JavaScript code in this page is free software: you can
  11. * redistribute it and/or modify it under the terms of the GNU
  12. * General Public License (GNU GPL) as published by the Free Software
  13. * Foundation, either version 3 of the License, or (at your option)
  14. * any later version. The code is distributed WITHOUT ANY WARRANTY;
  15. * without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
  17. *
  18. * As additional permission under GNU GPL version 3 section 7, you
  19. * may distribute non-source (e.g., minimized or compacted) forms of
  20. * that code without the copy of the GNU GPL normally required by
  21. * section 4, provided you include this license notice and a URL
  22. * through which recipients can access the Corresponding Source.
  23. *
  24. * @licend
  25. */
  26. (function hand() { //Code isolation
  27. const selectorStates = {
  28. pointing: 0,
  29. selecting: 1,
  30. moving: 2
  31. }
  32. var selected = null;
  33. var selected_els = [];
  34. var selectionRect = createSelectorRect();
  35. var selectionRectTranslation;
  36. var translation_elements = [];
  37. var selectorState = selectorStates.pointing;
  38. var last_sent = 0;
  39. function getParentMathematics(el) {
  40. var target
  41. var a = el
  42. var els = [];
  43. while (a) {
  44. els.unshift(a);
  45. a = a.parentElement;
  46. }
  47. var parentMathematics = els.find(el => el.getAttribute("class") === "MathElement");
  48. if ((parentMathematics) && parentMathematics.tagName === "svg") {
  49. target = parentMathematics;
  50. }
  51. return target ?? el;
  52. }
  53. function createSelectorRect() {
  54. var shape = Tools.createSVGElement("rect");
  55. shape.id = "selectionRect";
  56. shape.x.baseVal.value = 0;
  57. shape.y.baseVal.value = 0;
  58. shape.width.baseVal.value = 0;
  59. shape.height.baseVal.value = 0;
  60. shape.setAttribute("stroke", "black");
  61. shape.setAttribute("stroke-width", 1);
  62. shape.setAttribute("vector-effect", "non-scaling-stroke");
  63. shape.setAttribute("fill", "none");
  64. shape.setAttribute("stroke-dasharray", "5 5");
  65. shape.setAttribute("opacity", 1);
  66. Tools.svg.appendChild(shape);
  67. return shape;
  68. }
  69. function startMovingElements(x, y, evt) {
  70. evt.preventDefault();
  71. selectorState = selectorStates.moving;
  72. selected = { x: x, y: y };
  73. // Some of the selected elements could have been deleted
  74. selected_els = selected_els.filter(el => {
  75. return Tools.svg.getElementById(el.id) !== null
  76. });
  77. translation_elements = selected_els.map(el => {
  78. let tmatrix = get_translate_matrix(el);
  79. return { x: tmatrix.e, y: tmatrix.f }
  80. });
  81. {
  82. let tmatrix = get_translate_matrix(selectionRect);
  83. selectionRectTranslation = { x: tmatrix.e, y: tmatrix.f };
  84. }
  85. }
  86. function startSelector(x, y, evt) {
  87. evt.preventDefault();
  88. selected = { x: x, y: y };
  89. selected_els = [];
  90. selectorState = selectorStates.selecting;
  91. selectionRect.x.baseVal.value = x;
  92. selectionRect.y.baseVal.value = y;
  93. selectionRect.width.baseVal.value = 0;
  94. selectionRect.height.baseVal.value = 0;
  95. selectionRect.style.display = "";
  96. tmatrix = get_translate_matrix(selectionRect);
  97. tmatrix.e = 0;
  98. tmatrix.f = 0;
  99. }
  100. function calculateSelection() {
  101. var scale = Tools.drawingArea.getCTM().a;
  102. var selectionTBBox = selectionRect.transformedBBox(scale);
  103. return Array.from(Tools.drawingArea.children).filter(el => {
  104. return transformedBBoxIntersects(
  105. selectionTBBox,
  106. el.transformedBBox(scale)
  107. )
  108. });
  109. }
  110. function moveSelection(x, y) {
  111. var dx = x - selected.x;
  112. var dy = y - selected.y;
  113. var msgs = selected_els.map((el, i) => {
  114. return {
  115. type: "update",
  116. id: el.id,
  117. deltax: dx + translation_elements[i].x,
  118. deltay: dy + translation_elements[i].y
  119. }
  120. })
  121. var msg = {
  122. _children: msgs
  123. };
  124. {
  125. let tmatrix = get_translate_matrix(selectionRect);
  126. tmatrix.e = dx + selectionRectTranslation.x;
  127. tmatrix.f = dy + selectionRectTranslation.y;
  128. }
  129. var now = performance.now();
  130. if (now - last_sent > 70) {
  131. last_sent = now;
  132. Tools.drawAndSend(msg);
  133. } else {
  134. draw(msg);
  135. }
  136. }
  137. function updateRect(x, y, rect) {
  138. rect.x.baseVal.value = Math.min(x, selected.x);
  139. rect.y.baseVal.value = Math.min(y, selected.y);
  140. rect.width.baseVal.value = Math.abs(x - selected.x);
  141. rect.height.baseVal.value = Math.abs(y - selected.y);
  142. }
  143. function get_translate_matrix(elem) {
  144. // Returns the first translate or transform matrix or makes one
  145. var translate = null;
  146. for (var i = 0; i < elem.transform.baseVal.numberOfItems; ++i) {
  147. var baseVal = elem.transform.baseVal[i];
  148. // quick tests showed that even if one changes only the fields e and f or uses createSVGTransformFromMatrix
  149. // the brower may add a SVG_TRANSFORM_MATRIX instead of a SVG_TRANSFORM_TRANSLATE
  150. if (baseVal.type === SVGTransform.SVG_TRANSFORM_TRANSLATE || baseVal.type === SVGTransform.SVG_TRANSFORM_MATRIX) {
  151. translate = baseVal;
  152. break;
  153. }
  154. }
  155. if (translate == null) {
  156. translate = elem.transform.baseVal.createSVGTransformFromMatrix(Tools.svg.createSVGMatrix());
  157. elem.transform.baseVal.appendItem(translate);
  158. }
  159. return translate.matrix;
  160. }
  161. function draw(data) {
  162. if (data._children) {
  163. batchCall(draw, data._children);
  164. }
  165. else {
  166. switch (data.type) {
  167. case "update":
  168. var elem = Tools.svg.getElementById(data.id);
  169. if (!elem) throw new Error("Mover: Tried to move an element that does not exist.");
  170. var tmatrix = get_translate_matrix(elem);
  171. tmatrix.e = data.deltax || 0;
  172. tmatrix.f = data.deltay || 0;
  173. break;
  174. default:
  175. throw new Error("Mover: 'move' instruction with unknown type. ", data);
  176. }
  177. }
  178. }
  179. function clickSelector(x, y, evt) {
  180. var scale = Tools.drawingArea.getCTM().a
  181. selectionRect = selectionRect ?? createSelectorRect();
  182. if (pointInTransformedBBox([x, y], selectionRect.transformedBBox(scale))) {
  183. startMovingElements(x, y, evt);
  184. } else if (Tools.drawingArea.contains(evt.target)) {
  185. selectionRect.style.display = "none";
  186. selected_els = [getParentMathematics(evt.target)];
  187. startMovingElements(x, y, evt);
  188. } else {
  189. startSelector(x, y, evt);
  190. }
  191. }
  192. function releaseSelector(x, y, evt) {
  193. if (selectorState == selectorStates.selecting) {
  194. selected_els = calculateSelection();
  195. if (selected_els.length == 0) {
  196. selectionRect.style.display = "none";
  197. }
  198. }
  199. translation_elements = [];
  200. selectorState = selectorStates.pointing;
  201. }
  202. function moveSelector(x, y, evt) {
  203. if (selectorState == selectorStates.selecting) {
  204. updateRect(x, y, selectionRect);
  205. } else if (selectorState == selectorStates.moving) {
  206. moveSelection(x, y, selectionRect);
  207. }
  208. }
  209. function startHand(x, y, evt, isTouchEvent) {
  210. if (!isTouchEvent) {
  211. selected = {
  212. x: document.documentElement.scrollLeft + evt.clientX,
  213. y: document.documentElement.scrollTop + evt.clientY,
  214. }
  215. }
  216. }
  217. function moveHand(x, y, evt, isTouchEvent) {
  218. if (selected && !isTouchEvent) { //Let the browser handle touch to scroll
  219. window.scrollTo(selected.x - evt.clientX, selected.y - evt.clientY);
  220. }
  221. }
  222. function press(x, y, evt, isTouchEvent) {
  223. if (!handTool.secondary.active) startHand(x, y, evt, isTouchEvent);
  224. else clickSelector(x, y, evt, isTouchEvent);
  225. }
  226. function move(x, y, evt, isTouchEvent) {
  227. if (!handTool.secondary.active) moveHand(x, y, evt, isTouchEvent);
  228. else moveSelector(x, y, evt, isTouchEvent);
  229. }
  230. function release(x, y, evt, isTouchEvent) {
  231. move(x, y, evt, isTouchEvent);
  232. if (handTool.secondary.active) releaseSelector(x, y, evt, isTouchEvent);
  233. selected = null;
  234. }
  235. function switchTool() {
  236. selected = null;
  237. }
  238. var handTool = { //The new tool
  239. "name": "Hand",
  240. "shortcut": "h",
  241. "listeners": {
  242. "press": press,
  243. "move": move,
  244. "release": release,
  245. },
  246. "secondary": {
  247. "name": "Selector",
  248. "icon": "tools/hand/selector.svg",
  249. "active": false,
  250. "switch": switchTool,
  251. },
  252. "draw": draw,
  253. "icon": "tools/hand/hand.svg",
  254. "mouseCursor": "move",
  255. "showMarker": true,
  256. };
  257. Tools.add(handTool);
  258. Tools.change("Hand"); // Use the hand tool by default
  259. })(); //End of code isolation