1
0

trumbowyg.resizimg.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. ; (function ($) {
  2. 'use strict';
  3. var defaultOptions = {
  4. minSize: 32,
  5. step: 4
  6. };
  7. function preventDefault(e) {
  8. e.stopPropagation();
  9. e.preventDefault();
  10. }
  11. var ResizeWithCanvas = function (trumbowyg) {
  12. // variable to create canvas and save img in resize mode
  13. this.resizeCanvas = document.createElement('canvas');
  14. // to allow canvas to get focus
  15. this.resizeCanvas.setAttribute('tabindex', '0');
  16. this.resizeCanvas.id = 'trumbowyg-resizimg-' + (+new Date());
  17. this.ctx = null;
  18. this.resizeImg = null;
  19. this.pressEscape = function (obj) {
  20. obj.reset();
  21. };
  22. this.pressBackspaceOrDelete = function (obj) {
  23. $(obj.resizeCanvas).remove();
  24. obj.resizeImg = null;
  25. if (trumbowyg !== null){
  26. trumbowyg.syncCode();
  27. // notify changes
  28. trumbowyg.$c.trigger('tbwchange');
  29. }
  30. };
  31. // PRIVATE FUNCTION
  32. var focusedNow = false;
  33. var isCursorSeResize = false;
  34. // calculate offset to change mouse over square in the canvas
  35. var offsetX, offsetY;
  36. var reOffset = function (canvas) {
  37. var BB = canvas.getBoundingClientRect();
  38. offsetX = BB.left;
  39. offsetY = BB.top;
  40. };
  41. var updateCanvas = function (canvas, ctx, img, canvasWidth, canvasHeight) {
  42. ctx.translate(0.5, 0.5);
  43. ctx.lineWidth = 1;
  44. // image
  45. ctx.drawImage(img, 5, 5, canvasWidth - 10, canvasHeight - 10);
  46. // border
  47. ctx.beginPath();
  48. ctx.rect(5, 5, canvasWidth - 10, canvasHeight - 10);
  49. ctx.stroke();
  50. // square in the angle
  51. ctx.beginPath();
  52. ctx.fillStyle = 'rgb(255, 255, 255)';
  53. ctx.rect(canvasWidth - 10, canvasHeight - 10, 9, 9);
  54. ctx.fill();
  55. ctx.stroke();
  56. // get the offset to change the mouse cursor
  57. reOffset(canvas);
  58. return ctx;
  59. };
  60. // PUBLIC FUNCTION
  61. // necessary to correctly print cursor over square. Called once for instance. Useless with trumbowyg.
  62. this.init = function () {
  63. var _this = this;
  64. $(window).on('scroll resize', function () {
  65. _this.reCalcOffset();
  66. });
  67. };
  68. this.reCalcOffset = function () {
  69. reOffset(this.resizeCanvas);
  70. };
  71. this.canvasId = function () {
  72. return this.resizeCanvas.id;
  73. };
  74. this.isActive = function () {
  75. return this.resizeImg !== null;
  76. };
  77. this.isFocusedNow = function () {
  78. return focusedNow;
  79. };
  80. this.blurNow = function () {
  81. focusedNow = false;
  82. };
  83. // restore image in the HTML of the editor
  84. this.reset = function () {
  85. if (this.resizeImg === null) {
  86. return;
  87. }
  88. // set style of image to avoid issue on resize because this attribute have priority over width and height attribute
  89. this.resizeImg.setAttribute('style', 'width: 100%; max-width: ' + (this.resizeCanvas.clientWidth - 10) + 'px; height: auto; max-height: ' + (this.resizeCanvas.clientHeight - 10) + 'px;');
  90. $(this.resizeCanvas).replaceWith($(this.resizeImg));
  91. // reset canvas style
  92. this.resizeCanvas.removeAttribute('style');
  93. this.resizeImg = null;
  94. };
  95. // setup canvas with points and border to allow the resizing operation
  96. this.setup = function (img, resizableOptions) {
  97. this.resizeImg = img;
  98. if (!this.resizeCanvas.getContext) {
  99. return false;
  100. }
  101. focusedNow = true;
  102. // draw canvas
  103. this.resizeCanvas.width = $(this.resizeImg).width() + 10;
  104. this.resizeCanvas.height = $(this.resizeImg).height() + 10;
  105. this.resizeCanvas.style.margin = '-5px';
  106. this.ctx = this.resizeCanvas.getContext('2d');
  107. // replace image with canvas
  108. $(this.resizeImg).replaceWith($(this.resizeCanvas));
  109. updateCanvas(this.resizeCanvas, this.ctx, this.resizeImg, this.resizeCanvas.width, this.resizeCanvas.height);
  110. // enable resize
  111. $(this.resizeCanvas).resizable(resizableOptions)
  112. .on('mousedown', preventDefault);
  113. var _this = this;
  114. $(this.resizeCanvas)
  115. .on('mousemove', function (e) {
  116. var mouseX = Math.round(e.clientX - offsetX);
  117. var mouseY = Math.round(e.clientY - offsetY);
  118. var wasCursorSeResize = isCursorSeResize;
  119. _this.ctx.rect(_this.resizeCanvas.width - 10, _this.resizeCanvas.height - 10, 9, 9);
  120. isCursorSeResize = _this.ctx.isPointInPath(mouseX, mouseY);
  121. if (wasCursorSeResize !== isCursorSeResize) {
  122. this.style.cursor = isCursorSeResize ? 'se-resize' : 'default';
  123. }
  124. })
  125. .on('keydown', function (e) {
  126. if (!_this.isActive()) {
  127. return;
  128. }
  129. var x = e.keyCode;
  130. if (x === 27) { // ESC
  131. _this.pressEscape(_this);
  132. } else if (x === 8 || x === 46) { // BACKSPACE or DELETE
  133. _this.pressBackspaceOrDelete(_this);
  134. }
  135. })
  136. .on('focus', preventDefault)
  137. .on('blur', function () {
  138. _this.reset();
  139. // save changes
  140. if (trumbowyg !== null){
  141. trumbowyg.syncCode();
  142. // notify changes
  143. trumbowyg.$c.trigger('tbwchange');
  144. }
  145. });
  146. this.resizeCanvas.focus();
  147. return true;
  148. };
  149. // update the canvas after the resizing
  150. this.refresh = function () {
  151. if (!this.resizeCanvas.getContext) {
  152. return;
  153. }
  154. this.resizeCanvas.width = this.resizeCanvas.clientWidth;
  155. this.resizeCanvas.height = this.resizeCanvas.clientHeight;
  156. updateCanvas(this.resizeCanvas, this.ctx, this.resizeImg, this.resizeCanvas.width, this.resizeCanvas.height);
  157. };
  158. };
  159. $.extend(true, $.trumbowyg, {
  160. plugins: {
  161. resizimg: {
  162. destroyResizable: function () {},
  163. init: function (trumbowyg) {
  164. var destroyResizable = this.destroyResizable;
  165. // object to interact with canvas
  166. var resizeWithCanvas = new ResizeWithCanvas(trumbowyg);
  167. this.destroyResizable = function () {
  168. // clean html code
  169. trumbowyg.$ed.find('canvas.resizable')
  170. .resizable('destroy')
  171. .off('mousedown', preventDefault)
  172. .removeClass('resizable');
  173. resizeWithCanvas.reset();
  174. trumbowyg.syncCode();
  175. };
  176. trumbowyg.o.plugins.resizimg = $.extend(true, {},
  177. defaultOptions,
  178. trumbowyg.o.plugins.resizimg || {},
  179. {
  180. resizable: {
  181. resizeWidth: false,
  182. onDragStart: function (ev, $el) {
  183. var opt = trumbowyg.o.plugins.resizimg;
  184. var x = ev.pageX - $el.offset().left;
  185. var y = ev.pageY - $el.offset().top;
  186. if (x < $el.width() - opt.minSize || y < $el.height() - opt.minSize) {
  187. return false;
  188. }
  189. },
  190. onDrag: function (ev, $el, newWidth, newHeight) {
  191. var opt = trumbowyg.o.plugins.resizimg;
  192. if (newHeight < opt.minSize) {
  193. newHeight = opt.minSize;
  194. }
  195. newHeight -= newHeight % opt.step;
  196. $el.height(newHeight);
  197. return false;
  198. },
  199. onDragEnd: function () {
  200. // resize update canvas information
  201. resizeWithCanvas.refresh();
  202. trumbowyg.syncCode();
  203. }
  204. }
  205. }
  206. );
  207. function initResizable() {
  208. trumbowyg.$ed.find('img')
  209. .off('click')
  210. .on('click', function (e) {
  211. // if I'm already do a resize, reset it
  212. if (resizeWithCanvas.isActive()) {
  213. resizeWithCanvas.reset();
  214. }
  215. // initialize resize of image
  216. resizeWithCanvas.setup(this, trumbowyg.o.plugins.resizimg.resizable);
  217. preventDefault(e);
  218. });
  219. }
  220. trumbowyg.$c.on('tbwinit', function () {
  221. initResizable();
  222. // disable resize when click on other items
  223. trumbowyg.$ed.on('click', function (e) {
  224. // check if I've clicked out of canvas or image to reset it
  225. if ($(e.target).is('img') || e.target.id === resizeWithCanvas.canvasId()) {
  226. return;
  227. }
  228. preventDefault(e);
  229. resizeWithCanvas.reset();
  230. //sync
  231. trumbowyg.syncCode();
  232. // notify changes
  233. trumbowyg.$c.trigger('tbwchange');
  234. });
  235. trumbowyg.$ed.on('scroll', function () {
  236. resizeWithCanvas.reCalcOffset();
  237. });
  238. });
  239. trumbowyg.$c.on('tbwfocus tbwchange', initResizable);
  240. trumbowyg.$c.on('tbwresize', function () {
  241. resizeWithCanvas.reCalcOffset();
  242. });
  243. // Destroy
  244. trumbowyg.$c.on('tbwblur', function () {
  245. // when canvas is created the tbwblur is called
  246. // this code avoid to destroy the canvas that allow the image resizing
  247. if (resizeWithCanvas.isFocusedNow()) {
  248. resizeWithCanvas.blurNow();
  249. } else {
  250. destroyResizable();
  251. }
  252. });
  253. },
  254. destroy: function () {
  255. this.destroyResizable();
  256. }
  257. }
  258. }
  259. });
  260. })(jQuery);