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.

text.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 () { //Code isolation
  27. var board = Tools.board, svg = Tools.svg;
  28. var input = document.createElement("input");
  29. input.id = "textToolInput";
  30. input.setAttribute("autocomplete", "off");
  31. var curText = {
  32. "x": 0,
  33. "y": 0,
  34. "size": 0,
  35. "color": "#000",
  36. "id": 0,
  37. "sentText": "",
  38. "lastSending": 0
  39. };
  40. function clickHandler(x, y, evt) {
  41. if (evt.target == input) return;
  42. if (evt.target.tagName === "text") {
  43. editOldText(evt.target);
  44. evt.preventDefault();
  45. return;
  46. }
  47. curText.size = parseInt(Tools.getSize() * 1.5 + 12);
  48. curText.color = Tools.getColor();
  49. curText.x = x;
  50. curText.y = y + curText.size / 2;
  51. drawCurText();
  52. evt.preventDefault();
  53. }
  54. function editOldText(elem) {
  55. curText.id = elem.id;
  56. curText.x = elem.x.baseVal[0].value;
  57. curText.y = elem.y.baseVal[0].value;
  58. curText.size = parseInt(elem.getAttribute("font-size"));
  59. curText.color = elem.getAttribute("fill");
  60. startEdit();
  61. input.value = elem.textContent;
  62. }
  63. function drawCurText() {
  64. stopEdit();
  65. //If the user clicked where there was no text, then create a new text field
  66. curText.id = Tools.generateUID("t"); //"t" for text
  67. Tools.drawAndSend({
  68. 'type': 'new',
  69. 'id': curText.id,
  70. 'color': curText.color,
  71. 'size': curText.size,
  72. 'x': curText.x,
  73. 'y': curText.y
  74. });
  75. startEdit();
  76. }
  77. function startEdit() {
  78. if (!input.parentNode) board.appendChild(input);
  79. input.value = "";
  80. input.focus();
  81. input.addEventListener("keyup", textChangeHandler);
  82. input.addEventListener("blur", textChangeHandler);
  83. }
  84. function stopEdit() {
  85. input.blur();
  86. input.removeEventListener("keyup", textChangeHandler);
  87. }
  88. function textChangeHandler(evt) {
  89. if (evt.which === 13) {
  90. curText.y += 1.5 * curText.size;
  91. return drawCurText();
  92. }
  93. if (performance.now() - curText.lastSending > 100) {
  94. if (curText.sentText !== input.value) {
  95. Tools.drawAndSend({
  96. 'type': "update",
  97. 'id': curText.id,
  98. 'txt': input.value.slice(0, 280)
  99. });
  100. curText.sentText = input.value;
  101. curText.lastSending = performance.now();
  102. }
  103. } else {
  104. clearTimeout(curText.timeout);
  105. curText.timeout = setTimeout(textChangeHandler, 500, evt);
  106. }
  107. }
  108. function draw(data, isLocal) {
  109. switch (data.type) {
  110. case "new":
  111. createTextField(data);
  112. break;
  113. case "update":
  114. var textField = document.getElementById(data.id);
  115. if (textField === null) {
  116. console.error("Text: Hmmm... I received text that belongs to an unknown text field");
  117. return false;
  118. }
  119. updateText(textField, data.txt);
  120. break;
  121. default:
  122. console.error("Text: Draw instruction with unknown type. ", data);
  123. break;
  124. }
  125. }
  126. function updateText(textField, text) {
  127. textField.textContent = text;
  128. }
  129. function createTextField(fieldData) {
  130. var elem = Tools.createSVGElement("text");
  131. elem.id = fieldData.id;
  132. elem.setAttribute("x", fieldData.x);
  133. elem.setAttribute("y", fieldData.y);
  134. elem.setAttribute("font-size", fieldData.size);
  135. elem.setAttribute("fill", fieldData.color);
  136. if (fieldData.txt) elem.textContent = fieldData.txt;
  137. svg.appendChild(elem);
  138. return elem;
  139. }
  140. Tools.add({ //The new tool
  141. "name": "Text",
  142. "icon": "T",
  143. "listeners": {
  144. "press": clickHandler,
  145. },
  146. "draw": draw,
  147. "stylesheet": "tools/text/text.css",
  148. "mouseCursor": "text"
  149. });
  150. })(); //End of code isolation