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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. }
  62. function stopEdit () {
  63. input.blur();
  64. input.removeEventListener("keyup", textChangeHandler);
  65. }
  66. function textChangeHandler (evt) {
  67. if (evt && evt.which===13) {
  68. clickHandler(curText.x,curText.y + 1.5*curText.size);
  69. }
  70. if (performance.now() - curText.lastSending > 100) {
  71. if (curText.sentText !== input.value) {
  72. Tools.drawAndSend({
  73. 'type' : "update",
  74. 'field' : curText.id,
  75. 'txt' : input.value
  76. });
  77. curText.sentText = input.value;
  78. curText.lastSending = performance.now();
  79. }
  80. } else {
  81. clearTimeout(curText.timeout);
  82. curText.timeout = setTimeout(textChangeHandler, 200);
  83. }
  84. }
  85. function draw(data, isLocal) {
  86. switch(data.type) {
  87. case "new":
  88. createTextField(data);
  89. break;
  90. case "update":
  91. var textField = document.getElementById(data.field);
  92. if (textField===null) {
  93. console.log("Text: Hmmm... I received text that belongs to an unknown text field");
  94. return false;
  95. }
  96. updateText(textField, data.txt);
  97. break;
  98. default:
  99. console.log("Text: Draw instruction with unknown type. ", data);
  100. break;
  101. }
  102. }
  103. function updateText (textField, text) {
  104. textField.textContent = text;
  105. }
  106. function createTextField (fieldData) {
  107. var elem = Tools.createSVGElement("text");
  108. elem.id = fieldData.id;
  109. elem.setAttribute("x", fieldData.x);
  110. elem.setAttribute("y", fieldData.y);
  111. elem.setAttribute("font-size", fieldData.size);
  112. elem.style.fill = fieldData.color;
  113. if (fieldData.text) elem.textContent = fieldData.text;
  114. svg.appendChild(elem);
  115. return elem;
  116. }
  117. Tools.add({ //The new tool
  118. "name" : "Text",
  119. "listeners" : {
  120. "press" : clickHandler,
  121. },
  122. "draw" : draw,
  123. "stylesheet" : "tools/text/text.css",
  124. "mouseCursor" : "text"
  125. });
  126. })(); //End of code isolation