quickformat.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. function getFirstChild(knode) {
  13. var child = knode.first();
  14. while (child && child.first()) {
  15. child = child.first();
  16. }
  17. return child;
  18. }
  19. self.clickToolbar(name, function() {
  20. self.focus();
  21. var doc = self.edit.doc,
  22. range = self.cmd.range,
  23. child = K(doc.body).first(), next,
  24. nodeList = [], subList = [],
  25. bookmark = range.createBookmark(true);
  26. while(child) {
  27. next = child.next();
  28. var firstChild = getFirstChild(child);
  29. if (!firstChild || firstChild.name != 'img') {
  30. if (blockMap[child.name]) {
  31. child.html(child.html().replace(/^(\s|&nbsp;| )+/ig, ''));
  32. child.css('text-indent', '2em');
  33. } else {
  34. subList.push(child);
  35. }
  36. if (!next || (blockMap[next.name] || blockMap[child.name] && !blockMap[next.name])) {
  37. if (subList.length > 0) {
  38. nodeList.push(subList);
  39. }
  40. subList = [];
  41. }
  42. }
  43. child = next;
  44. }
  45. K.each(nodeList, function(i, subList) {
  46. var wrapper = K('<p style="text-indent:2em;"></p>', doc);
  47. subList[0].before(wrapper);
  48. K.each(subList, function(i, knode) {
  49. wrapper.append(knode);
  50. });
  51. });
  52. range.moveToBookmark(bookmark);
  53. self.addBookmark();
  54. });
  55. });
  56. /**
  57. --------------------------
  58. abcd<br />
  59. 1234<br />
  60. to
  61. <p style="text-indent:2em;">
  62. abcd<br />
  63. 1234<br />
  64. </p>
  65. --------------------------
  66. &nbsp; abcd<img>1233
  67. <p>1234</p>
  68. to
  69. <p style="text-indent:2em;">abcd<img>1233</p>
  70. <p style="text-indent:2em;">1234</p>
  71. --------------------------
  72. */