Advertisement
Guest User

Untitled

a guest
Aug 15th, 2014
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.41 KB | None | 0 0
  1. /**
  2. * Editor Toolkit: TinyMCE plugin
  3. */
  4.  
  5. (function($) {
  6. tinymce.create('tinymce.plugins.editor_toolkit', {
  7. init : function(ed, url) {
  8. var VK = {
  9. DELETE: 46, BACKSPACE: 8, ENTER: 13, TAB: 9, SPACEBAR: 32, UP: 38, DOWN: 40, LEFT: 37, RIGHT: 39,
  10. modifierPressed: function (e) {
  11. return e.shiftKey || e.ctrlKey || e.altKey;
  12. }
  13. };
  14.  
  15. function scrollToThisNode(node, padding) {
  16. vp = ed.dom.getViewPort( ed.getWin() )
  17. y = ed.dom.getPos(node).y;
  18. if (y < vp.y || y + padding > vp.y + vp.h) {
  19. ed.getWin().scrollTo(0, y < vp.y ? y : y - vp.h + padding);
  20. }
  21. }
  22.  
  23.  
  24.  
  25.  
  26. // ed.onInit.add(function(ed) {
  27. // ed.dom.addClass(ed.getBody(), 'editor-toolkit');
  28.  
  29. // // Last-ditch effort to structure content
  30. // $('#post').submit(function(){
  31. // $( ed.getBody() ).children('p').each(function(){
  32. // $(this).replaceWith('<div><p>' + $(this).html() + '</p></div>');
  33. // });
  34. // return true;
  35. // });
  36.  
  37. // });
  38.  
  39.  
  40. // Adds an observer to the onInit event using tinyMCE.init
  41. tinyMCE.init({
  42. setup : function(ed) {
  43. ed.onInit.add(function(ed) {
  44. console.debug('Editor is done: ' + ed.id);
  45. ed.dom.addClass(ed.getBody(), 'editor-toolkit');
  46.  
  47. // Last-ditch effort to structure content
  48. $('#post').submit(function(){
  49. $( ed.getBody() ).children('p').each(function(){
  50. $(this).replaceWith('<div><p>' + $(this).html() + '</p></div>');
  51. });
  52. return true;
  53. });
  54. });
  55. }
  56. });
  57.  
  58. ed.on('BeforeSetcontent', function(ed, o){
  59. if (adminpage == 'post-new-php' && !$( ed.getBody() ).hasClass('editor-toolkit-init') ) {
  60. o.content = '<div><p><br /></p>';
  61. ed.dom.addClass(ed.getBody(), 'editor-toolkit-init');
  62. }
  63. });
  64.  
  65.  
  66. // ed.onBeforeSetContent.add(function(ed, o) {
  67. // if (adminpage == 'post-new-php' && !$( ed.getBody() ).hasClass('editor-toolkit-init') ) {
  68. // o.content = '<div><p><br /></p>';
  69. // ed.dom.addClass(ed.getBody(), 'editor-toolkit-init');
  70. // }
  71. // });
  72.  
  73. ed.on('KeyDown', function(ed, event){
  74. if ( event.keyCode === VK.DELETE && !VK.modifierPressed(event) ) {
  75.  
  76. }
  77. if ( event.keyCode === VK.BACKSPACE && !VK.modifierPressed(event) ) {
  78.  
  79. }
  80. if ( event.keyCode === VK.ENTER && !VK.modifierPressed(event) ) {
  81. setTimeout(function() {
  82. var currentNode = ed.dom.getParent(ed.selection.getRng().startContainer, ed.dom.isBlock);
  83. var prevSibling = $(currentNode).length > 0 ? currentNode.previousSibling : false;
  84. var nextSibling = $(currentNode).length > 0 ? currentNode.nextSibling : false;
  85. var prevPrevSibling = $(prevSibling).length > 0 ? prevSibling.previousSibling : false;
  86. var nodeIndex = $(currentNode).length > 0 ? $(currentNode).index() : false;
  87. var nodeIsLast = $(currentNode).length > 0 ? $(currentNode).is(':last-child') : null;
  88.  
  89. var nodeParent = ed.dom.getParent(currentNode, 'div');
  90. var nodeParentPrevSibling = $(nodeParent).length > 0 ? nodeParent.previousSibling : false;
  91. var nodeParentNextSibling = $(nodeParent).length > 0 ? nodeParent.nextSibling : false;
  92. var nodeParentChildren = $(nodeParent).length > 0 ? $(nodeParent).children().length : false;
  93.  
  94. var contentBody = ed.getBody();
  95. var contentFirstChild = ed.getBody().firstChild;
  96.  
  97. var newNodeData = '<p><br /></p>';
  98.  
  99. function isEmpty(node) {
  100. if (ed.dom.isEmpty(node) || node.firstChild.nodeName === 'BR' || $(node).html() === '&nbsp;')
  101. return true;
  102. else
  103. return false;
  104.  
  105. }
  106.  
  107. switch(true) {
  108. // Start new block at selection point ("after")
  109. case nodeIsLast && nodeIndex + 1 === nodeParentChildren && nodeParentChildren > 3:
  110. case nodeIndex === 2 && nodeParentChildren === 3 && ( !isEmpty(prevSibling) || !isEmpty(prevPrevSibling) ):
  111. if ( isEmpty(prevSibling) && isEmpty(currentNode) ) {
  112. $(nodeParent).after('<div>' + newNodeData + '</div>');
  113. ed.dom.remove(prevSibling);
  114. ed.dom.remove(currentNode);
  115. newBlock = nodeParent.nextSibling;
  116. ed.execCommand('mceSelectNode', false, newBlock.firstChild.firstChild);
  117. ed.selection.select(newBlock.firstChild.firstChild, true); // Extra to get FF to show cursor
  118. scrollToThisNode(newBlock, 100);
  119. }
  120. break;
  121. // Start new block at selection point ("before")
  122. case nodeIndex === 2:
  123. if ( isEmpty(prevSibling) && isEmpty(prevPrevSibling) ) {
  124. $(nodeParent).before('<div>' + newNodeData + '</div>');
  125. ed.dom.remove(prevSibling);
  126. ed.dom.remove(prevPrevSibling);
  127. newBlock = nodeParent.previousSibling;
  128. if ( $(newBlock).is(':first-child') ) {
  129. ed.execCommand('mceSelectNode', false, newBlock.firstChild.firstChild);
  130. ed.selection.select(newBlock.firstChild.firstChild, true); // Extra to get FF to show cursor
  131. scrollToThisNode(newBlock, 100);
  132. }
  133. }
  134. break;
  135. // Correct new paragraph created outside a block
  136. case currentNode && currentNode.nodeName === 'P' && !nodeParent:
  137. newNodeData = $(currentNode).html();
  138. $(currentNode).after('<div><p>' + newNodeData + '</p></div>');
  139. newBlock = currentNode.nextSibling;
  140. ed.dom.remove(currentNode);
  141. break;
  142. // Rare but best to cover all angles (problematic on empty pages)
  143. case contentFirstChild.nodeName === 'P':
  144. $(contentFirstChild).replaceWith('<div>' + newNodeData + '</div>');
  145. newBlock = ed.getBody().firstChild;
  146. ed.execCommand('mceSelectNode', false, newBlock.firstChild.firstChild);
  147. ed.selection.select(newBlock.firstChild.firstChild, true); // Extra to get FF to show cursor
  148. break;
  149. // Fix empty block
  150. case ed.dom.is(ed.selection.getNode(), 'div') && isEmpty(currentNode):
  151. ed.execCommand('mceReplaceContent', false, '<p></p>');
  152. break;
  153. }
  154.  
  155. // Run a check for paragraphs outside a block, at root level
  156. setTimeout(function() {
  157. $(contentBody).children('p').each(function(){
  158. $(this).replaceWith('<div><p>' + $(this).html() + '</p></div>');
  159. });
  160. }, 250);
  161.  
  162. ed.execCommand('mceCleanup');
  163. }, 0);
  164. }
  165. });
  166.  
  167. // ed.onKeyDown.add(function(ed, event) {
  168. // if ( event.keyCode === VK.DELETE && !VK.modifierPressed(event) ) {
  169.  
  170. // }
  171. // if ( event.keyCode === VK.BACKSPACE && !VK.modifierPressed(event) ) {
  172.  
  173. // }
  174. // if ( event.keyCode === VK.ENTER && !VK.modifierPressed(event) ) {
  175. // setTimeout(function() {
  176. // var currentNode = ed.dom.getParent(ed.selection.getRng().startContainer, ed.dom.isBlock);
  177. // var prevSibling = $(currentNode).length > 0 ? currentNode.previousSibling : false;
  178. // var nextSibling = $(currentNode).length > 0 ? currentNode.nextSibling : false;
  179. // var prevPrevSibling = $(prevSibling).length > 0 ? prevSibling.previousSibling : false;
  180. // var nodeIndex = $(currentNode).length > 0 ? $(currentNode).index() : false;
  181. // var nodeIsLast = $(currentNode).length > 0 ? $(currentNode).is(':last-child') : null;
  182.  
  183. // var nodeParent = ed.dom.getParent(currentNode, 'div');
  184. // var nodeParentPrevSibling = $(nodeParent).length > 0 ? nodeParent.previousSibling : false;
  185. // var nodeParentNextSibling = $(nodeParent).length > 0 ? nodeParent.nextSibling : false;
  186. // var nodeParentChildren = $(nodeParent).length > 0 ? $(nodeParent).children().length : false;
  187.  
  188. // var contentBody = ed.getBody();
  189. // var contentFirstChild = ed.getBody().firstChild;
  190.  
  191. // var newNodeData = '<p><br /></p>';
  192.  
  193. // function isEmpty(node) {
  194. // if (ed.dom.isEmpty(node) || node.firstChild.nodeName === 'BR' || $(node).html() === '&nbsp;')
  195. // return true;
  196. // else
  197. // return false;
  198.  
  199. // }
  200.  
  201. // switch(true) {
  202. // // Start new block at selection point ("after")
  203. // case nodeIsLast && nodeIndex + 1 === nodeParentChildren && nodeParentChildren > 3:
  204. // case nodeIndex === 2 && nodeParentChildren === 3 && ( !isEmpty(prevSibling) || !isEmpty(prevPrevSibling) ):
  205. // if ( isEmpty(prevSibling) && isEmpty(currentNode) ) {
  206. // $(nodeParent).after('<div>' + newNodeData + '</div>');
  207. // ed.dom.remove(prevSibling);
  208. // ed.dom.remove(currentNode);
  209. // newBlock = nodeParent.nextSibling;
  210. // ed.execCommand('mceSelectNode', false, newBlock.firstChild.firstChild);
  211. // ed.selection.select(newBlock.firstChild.firstChild, true); // Extra to get FF to show cursor
  212. // scrollToThisNode(newBlock, 100);
  213. // }
  214. // break;
  215. // // Start new block at selection point ("before")
  216. // case nodeIndex === 2:
  217. // if ( isEmpty(prevSibling) && isEmpty(prevPrevSibling) ) {
  218. // $(nodeParent).before('<div>' + newNodeData + '</div>');
  219. // ed.dom.remove(prevSibling);
  220. // ed.dom.remove(prevPrevSibling);
  221. // newBlock = nodeParent.previousSibling;
  222. // if ( $(newBlock).is(':first-child') ) {
  223. // ed.execCommand('mceSelectNode', false, newBlock.firstChild.firstChild);
  224. // ed.selection.select(newBlock.firstChild.firstChild, true); // Extra to get FF to show cursor
  225. // scrollToThisNode(newBlock, 100);
  226. // }
  227. // }
  228. // break;
  229. // // Correct new paragraph created outside a block
  230. // case currentNode && currentNode.nodeName === 'P' && !nodeParent:
  231. // newNodeData = $(currentNode).html();
  232. // $(currentNode).after('<div><p>' + newNodeData + '</p></div>');
  233. // newBlock = currentNode.nextSibling;
  234. // ed.dom.remove(currentNode);
  235. // break;
  236. // // Rare but best to cover all angles (problematic on empty pages)
  237. // case contentFirstChild.nodeName === 'P':
  238. // $(contentFirstChild).replaceWith('<div>' + newNodeData + '</div>');
  239. // newBlock = ed.getBody().firstChild;
  240. // ed.execCommand('mceSelectNode', false, newBlock.firstChild.firstChild);
  241. // ed.selection.select(newBlock.firstChild.firstChild, true); // Extra to get FF to show cursor
  242. // break;
  243. // // Fix empty block
  244. // case ed.dom.is(ed.selection.getNode(), 'div') && isEmpty(currentNode):
  245. // ed.execCommand('mceReplaceContent', false, '<p></p>');
  246. // break;
  247. // }
  248.  
  249. // // Run a check for paragraphs outside a block, at root level
  250. // setTimeout(function() {
  251. // $(contentBody).children('p').each(function(){
  252. // $(this).replaceWith('<div><p>' + $(this).html() + '</p></div>');
  253. // });
  254. // }, 250);
  255.  
  256. // ed.execCommand('mceCleanup');
  257. // }, 0);
  258. // }
  259. // });
  260.  
  261. },
  262. createControl : function(n, cm) {
  263. return null;
  264. },
  265. getInfo : function() {
  266. return {
  267. longname : "Editor Toolkit",
  268. author : 'Studio Anino',
  269. authorurl : 'http://studioanino.com/',
  270. infourl : 'http://studioanino.com/',
  271. version : "1.0"
  272. };
  273. }
  274. });
  275. tinymce.PluginManager.add('editor_toolkit', tinymce.plugins.editor_toolkit);
  276. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement