quickformat.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*******************************************************************************
  2. * KindEditor - WYSIWYG HTML Editor for Internet
  3. * Copyright (C) 2006-2011 kindsoft.net
  4. *
  5. * @author Roddy <luolonghao@gmail.com>
  6. * @site http://www.kindsoft.net/
  7. * @licence http://www.kindsoft.net/license.php
  8. *******************************************************************************/
  9. KindEditor.plugin('quickformat', function(K) {
  10. var self = this, name = 'quickformat',
  11. blockMap = K.toMap('blockquote,center,div,h1,h2,h3,h4,h5,h6,p');
  12. self.clickToolbar(name, function() {
  13. self.focus();
  14. var doc = self.edit.doc,
  15. range = self.cmd.range,
  16. child = K(doc.body).first(), next,
  17. nodeList = [], subList = [],
  18. bookmark = range.createBookmark(true);
  19. while(child) {
  20. next = child.next();
  21. if (blockMap[child.name]) {
  22. child.html(child.html().replace(/^(\s|&nbsp;| )+/ig, ''));
  23. child.css('text-indent', '2em');
  24. } else {
  25. subList.push(child);
  26. }
  27. if (!next || (blockMap[next.name] || blockMap[child.name] && !blockMap[next.name])) {
  28. if (subList.length > 0) {
  29. nodeList.push(subList);
  30. }
  31. subList = [];
  32. }
  33. child = next;
  34. }
  35. K.each(nodeList, function(i, subList) {
  36. var wrapper = K('<p style="text-indent:2em;"></p>', doc);
  37. subList[0].before(wrapper);
  38. K.each(subList, function(i, knode) {
  39. wrapper.append(knode);
  40. });
  41. });
  42. range.moveToBookmark(bookmark);
  43. self.addBookmark();
  44. });
  45. });
  46. /**
  47. --------------------------
  48. abcd<br />
  49. 1234<br />
  50. to
  51. <p style="text-indent:2em;">
  52. abcd<br />
  53. 1234<br />
  54. </p>
  55. --------------------------
  56. &nbsp; abcd<img>1233
  57. <p>1234</p>
  58. to
  59. <p style="text-indent:2em;">abcd<img>1233</p>
  60. <p style="text-indent:2em;">1234</p>
  61. --------------------------
  62. */