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 3.7KB

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