canvassize.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // NOTE 전체적으로 refactoring 필요
  2. Trex.module("make padding area inside Canvas with editor width",
  3. function(editor, toolbar, sidebar, canvas) {
  4. var _wysiwygPanel = canvas.getPanel(Trex.Canvas.__WYSIWYG_MODE);
  5. if (!_wysiwygPanel) {
  6. return;
  7. }
  8. var _elWysiwyg = _wysiwygPanel.el;
  9. var SCROLL_WIDTH = 16;
  10. var REQUIRED_MINIMUM_PADDING = 28;
  11. var BORDER_OF_CANVAS = 2;
  12. var _elLeftSpace;
  13. var _elRightSpace;
  14. var _elLeftSpaceChild;
  15. var _elRightSpaceChild;
  16. var sizeConfig = canvas.getSizeConfig();
  17. var currentCanvasWidth = canvas.getContainerWidth();
  18. var contentWidth = sizeConfig.contentWidth.toNumber();
  19. var contentPadding = sizeConfig.contentPadding.toNumber();
  20. var fixedContentWidth = (currentCanvasWidth > contentWidth); // canvas.js: _sizeConfig.contentWidth = _sizeConfig.wrapWidth 와 얽힌 문제임.
  21. //배경이 적용되었을 경우 사이즈를 변경한다.
  22. canvas.observeJob('canvas.apply.background', function(data) {
  23. adjustCanvasPadding({
  24. top: (data && data.topLeftHeight)? data.topLeftHeight.parsePx(): 0,
  25. right: (data && data.midRightWidth)? data.midRightWidth.parsePx(): 0,
  26. bottom: (data && data.botLeftHeight)? data.botLeftHeight.parsePx(): 0,
  27. left: (data && data.midLeftWidth)? data.midLeftWidth.parsePx(): 0
  28. });
  29. });
  30. //NOTE: 메일, 편지지가 적용되었을 에디터 영역의 패딩을 조정한다.
  31. canvas.observeJob('canvas.apply.letterpaper', function(data) {
  32. adjustCanvasPadding({
  33. top: (data && data.topHeight)? data.topHeight.parsePx(): 0,
  34. right: (data && (data.midColor || data.midUrl))? contentPadding: 0,
  35. bottom: (data && data.botHeight)? data.botHeight.parsePx(): 0,
  36. left: (data && (data.midColor || data.midUrl))? contentPadding: 0
  37. });
  38. });
  39. // contentWidth가 지정된 경우만, 패딩을 조정할 필요가 있다.
  40. if(fixedContentWidth) {
  41. // iframe이 로딩되면 패딩영역을 추가한다.
  42. canvas.observeJob(Trex.Ev.__IFRAME_LOAD_COMPLETE, function() {
  43. adjustCanvasPadding();
  44. createGuideArea();
  45. updatePaddingSpace();
  46. });
  47. //모드를 변경하였을 경우 패딩영역 처리
  48. canvas.observeJob(Trex.Ev.__CANVAS_MODE_CHANGE, function() {
  49. adjustGuidAreaPosition();
  50. updatePaddingSpace();
  51. });
  52. //에디터 래퍼의 너비가 변하였을 경우 패딩영역의 위치를 조절한다.
  53. canvas.observeJob(Trex.Ev.__CANVAS_WRAP_WIDTH_CHANGE, onCanvasWidthChanged);
  54. canvas.observeJob(Trex.Ev.__CANVAS_NORMAL_SCREEN_CHANGE, onCanvasWidthChanged);
  55. canvas.observeJob(Trex.Ev.__CANVAS_FULL_SCREEN_CHANGE, onCanvasWidthChanged);
  56. // 아래 코드의 필요성은 확인 필요
  57. if (!$tx.msie_nonstd) {
  58. if ($tx.gecko) {
  59. $tx.setStyle(_elWysiwyg, {
  60. overflowX: 'auto',
  61. overflowY: 'auto'
  62. });
  63. } else if ($tx.chrome){
  64. $tx.setStyle(_elWysiwyg.contentDocument.documentElement, {
  65. overflowX: 'auto',
  66. overflowY: 'auto'
  67. });
  68. } else {
  69. $tx.setStyle(_elWysiwyg, {
  70. overflowX: 'auto',
  71. overflowY: 'scroll'
  72. });
  73. }
  74. }
  75. }
  76. canvas.getCanvasGuideSize = function(){
  77. return calculdateGuideArea().leftWidth.parsePx();
  78. };
  79. function adjustCanvasPadding(skinStyle) {
  80. _wysiwygPanel.addStyle(calculdateCanvasPadding(skinStyle));
  81. }
  82. //iframe 패딩과 패딩영역의 사이즈를 계산한다.
  83. function calculdateCanvasPadding(skinStyle) {
  84. var canvasPadding = {};
  85. var direction = ['top', 'bottom', 'left', 'right'];
  86. for (var i = 0; i < direction.length; i++) {
  87. var key = direction[i];
  88. canvasPadding[key] = (skinStyle && skinStyle[key]) || contentPadding;
  89. }
  90. if (fixedContentWidth) {
  91. canvasPadding.left = Math.max(Math.ceil(getGuideAreaWidth()), 0);
  92. canvasPadding.right = Math.max(Math.floor(getGuideAreaWidth()), 0); // for quirks mode
  93. return {
  94. width: contentWidth.toPx(),
  95. paddingLeft: "0",
  96. paddingRight: "0",
  97. paddingTop: canvasPadding.top.toPx(),
  98. paddingBottom: canvasPadding.bottom.toPx(),
  99. marginLeft: canvasPadding.left.toPx(),
  100. marginRight: canvasPadding.right.toPx()
  101. };
  102. } else {
  103. return {
  104. paddingTop: canvasPadding.top.toPx(),
  105. paddingRight: canvasPadding.right.toPx(),
  106. paddingBottom: canvasPadding.bottom.toPx(),
  107. paddingLeft: canvasPadding.left.toPx()
  108. };
  109. }
  110. }
  111. function getGuideAreaWidth() {
  112. return (currentCanvasWidth - contentWidth - BORDER_OF_CANVAS - SCROLL_WIDTH) / 2;
  113. }
  114. function calculdateGuideArea() {
  115. var _guideAreaWidth = getGuideAreaWidth();
  116. if(_guideAreaWidth < REQUIRED_MINIMUM_PADDING) {
  117. return {
  118. leftWidth: '0',
  119. rightWidth: '0',
  120. rightPos: '0'
  121. };
  122. } else {
  123. return {
  124. leftWidth: Math.ceil(_guideAreaWidth - contentPadding).toPx(),
  125. rightWidth: Math.max(0, (Math.floor(_guideAreaWidth - contentPadding))).toPx(),
  126. rightPos: (contentWidth + Math.ceil(_guideAreaWidth + contentPadding)).toPx()
  127. };
  128. }
  129. }
  130. function isGuideAreaCreated() {
  131. return _elLeftSpace && _elRightSpace;
  132. }
  133. var queuedJob;
  134. function onCanvasWidthChanged() {
  135. // for quirks mode
  136. clearTimeout(queuedJob);
  137. queuedJob = setTimeout(function() {
  138. currentCanvasWidth = canvas.getContainerWidth();
  139. adjustPanelPandding();
  140. }, 4);
  141. }
  142. function adjustPanelPandding() {
  143. adjustCanvasPadding();
  144. adjustGuidAreaPosition();
  145. updatePaddingSpace();
  146. }
  147. function createGuideArea() {
  148. var canvasTextColor = canvas.getStyleConfig().color;
  149. _elLeftSpace = tx.div({ className: "tx-wysiwyg-padding" });
  150. _elLeftSpaceChild = tx.div({
  151. className: "tx-wysiwyg-padding-divL",
  152. style: {
  153. borderColor: canvasTextColor
  154. }
  155. });
  156. _elRightSpace = tx.div({ className: "tx-wysiwyg-padding" });
  157. _elRightSpaceChild = tx.div({
  158. className: "tx-wysiwyg-padding-divR",
  159. style: {
  160. borderColor: canvasTextColor
  161. }
  162. });
  163. var _elHolder = canvas.wysiwygEl;
  164. _elLeftSpace.appendChild(_elLeftSpaceChild);
  165. _elHolder.insertBefore(_elLeftSpace, _elWysiwyg);
  166. _elRightSpace.appendChild(_elRightSpaceChild);
  167. _elHolder.insertBefore(_elRightSpace, _elWysiwyg);
  168. adjustGuidAreaPosition();
  169. }
  170. function adjustGuidAreaPosition() {
  171. if (isGuideAreaCreated()) {
  172. var _guideAreaSizes = calculdateGuideArea();
  173. $tx.setStyle(_elLeftSpace, {
  174. width: _guideAreaSizes.leftWidth
  175. });
  176. $tx.setStyle(_elRightSpace, {
  177. width: _guideAreaSizes.rightWidth,
  178. left: _guideAreaSizes.rightPos
  179. });
  180. var enoughSpaceForGuideArea = _guideAreaSizes.leftWidth.parsePx() > REQUIRED_MINIMUM_PADDING;
  181. var showGuideArea = canvas.getConfig().showGuideArea;
  182. var guideAreaBorder = enoughSpaceForGuideArea && showGuideArea ? "1px solid" : "0 none";
  183. $tx.setStyle(_elLeftSpaceChild, {
  184. borderRight: guideAreaBorder,
  185. borderBottom: guideAreaBorder
  186. });
  187. $tx.setStyle(_elRightSpaceChild, {
  188. borderLeft: guideAreaBorder,
  189. borderBottom: guideAreaBorder
  190. });
  191. }
  192. }
  193. function updatePaddingSpace() {
  194. if (isGuideAreaCreated()) {
  195. if (canvas.mode == Trex.Canvas.__WYSIWYG_MODE) {
  196. $tx.show(_elLeftSpace);
  197. $tx.show(_elRightSpace);
  198. } else {
  199. $tx.hide(_elLeftSpace);
  200. $tx.hide(_elRightSpace);
  201. }
  202. }
  203. }
  204. }
  205. );