plugin.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. /**
  2. * plugin.js
  3. *
  4. * Released under LGPL License.
  5. * Copyright (c) 1999-2015 Ephox Corp. All rights reserved
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. /*global tinymce:true */
  11. /*eslint consistent-this:0 */
  12. tinymce.PluginManager.add('lists', function(editor) {
  13. var self = this;
  14. function isChildOfBody(elm) {
  15. return editor.$.contains(editor.getBody(), elm);
  16. }
  17. function isBr(node) {
  18. return node && node.nodeName == 'BR';
  19. }
  20. function isListNode(node) {
  21. return node && (/^(OL|UL|DL)$/).test(node.nodeName) && isChildOfBody(node);
  22. }
  23. function isFirstChild(node) {
  24. return node.parentNode.firstChild == node;
  25. }
  26. function isLastChild(node) {
  27. return node.parentNode.lastChild == node;
  28. }
  29. function isTextBlock(node) {
  30. return node && !!editor.schema.getTextBlockElements()[node.nodeName];
  31. }
  32. function isEditorBody(elm) {
  33. return elm === editor.getBody();
  34. }
  35. editor.on('init', function() {
  36. var dom = editor.dom, selection = editor.selection;
  37. function isEmpty(elm, keepBookmarks) {
  38. var empty = dom.isEmpty(elm);
  39. if (keepBookmarks && dom.select('span[data-mce-type=bookmark]').length > 0) {
  40. return false;
  41. }
  42. return empty;
  43. }
  44. /**
  45. * Returns a range bookmark. This will convert indexed bookmarks into temporary span elements with
  46. * index 0 so that they can be restored properly after the DOM has been modified. Text bookmarks will not have spans
  47. * added to them since they can be restored after a dom operation.
  48. *
  49. * So this: <p><b>|</b><b>|</b></p>
  50. * becomes: <p><b><span data-mce-type="bookmark">|</span></b><b data-mce-type="bookmark">|</span></b></p>
  51. *
  52. * @param {DOMRange} rng DOM Range to get bookmark on.
  53. * @return {Object} Bookmark object.
  54. */
  55. function createBookmark(rng) {
  56. var bookmark = {};
  57. function setupEndPoint(start) {
  58. var offsetNode, container, offset;
  59. container = rng[start ? 'startContainer' : 'endContainer'];
  60. offset = rng[start ? 'startOffset' : 'endOffset'];
  61. if (container.nodeType == 1) {
  62. offsetNode = dom.create('span', {'data-mce-type': 'bookmark'});
  63. if (container.hasChildNodes()) {
  64. offset = Math.min(offset, container.childNodes.length - 1);
  65. if (start) {
  66. container.insertBefore(offsetNode, container.childNodes[offset]);
  67. } else {
  68. dom.insertAfter(offsetNode, container.childNodes[offset]);
  69. }
  70. } else {
  71. container.appendChild(offsetNode);
  72. }
  73. container = offsetNode;
  74. offset = 0;
  75. }
  76. bookmark[start ? 'startContainer' : 'endContainer'] = container;
  77. bookmark[start ? 'startOffset' : 'endOffset'] = offset;
  78. }
  79. setupEndPoint(true);
  80. if (!rng.collapsed) {
  81. setupEndPoint();
  82. }
  83. return bookmark;
  84. }
  85. /**
  86. * Moves the selection to the current bookmark and removes any selection container wrappers.
  87. *
  88. * @param {Object} bookmark Bookmark object to move selection to.
  89. */
  90. function moveToBookmark(bookmark) {
  91. function restoreEndPoint(start) {
  92. var container, offset, node;
  93. function nodeIndex(container) {
  94. var node = container.parentNode.firstChild, idx = 0;
  95. while (node) {
  96. if (node == container) {
  97. return idx;
  98. }
  99. // Skip data-mce-type=bookmark nodes
  100. if (node.nodeType != 1 || node.getAttribute('data-mce-type') != 'bookmark') {
  101. idx++;
  102. }
  103. node = node.nextSibling;
  104. }
  105. return -1;
  106. }
  107. container = node = bookmark[start ? 'startContainer' : 'endContainer'];
  108. offset = bookmark[start ? 'startOffset' : 'endOffset'];
  109. if (!container) {
  110. return;
  111. }
  112. if (container.nodeType == 1) {
  113. offset = nodeIndex(container);
  114. container = container.parentNode;
  115. dom.remove(node);
  116. }
  117. bookmark[start ? 'startContainer' : 'endContainer'] = container;
  118. bookmark[start ? 'startOffset' : 'endOffset'] = offset;
  119. }
  120. restoreEndPoint(true);
  121. restoreEndPoint();
  122. var rng = dom.createRng();
  123. rng.setStart(bookmark.startContainer, bookmark.startOffset);
  124. if (bookmark.endContainer) {
  125. rng.setEnd(bookmark.endContainer, bookmark.endOffset);
  126. }
  127. selection.setRng(rng);
  128. }
  129. function createNewTextBlock(contentNode, blockName) {
  130. var node, textBlock, fragment = dom.createFragment(), hasContentNode;
  131. var blockElements = editor.schema.getBlockElements();
  132. if (editor.settings.forced_root_block) {
  133. blockName = blockName || editor.settings.forced_root_block;
  134. }
  135. if (blockName) {
  136. textBlock = dom.create(blockName);
  137. if (textBlock.tagName === editor.settings.forced_root_block) {
  138. dom.setAttribs(textBlock, editor.settings.forced_root_block_attrs);
  139. }
  140. fragment.appendChild(textBlock);
  141. }
  142. if (contentNode) {
  143. while ((node = contentNode.firstChild)) {
  144. var nodeName = node.nodeName;
  145. if (!hasContentNode && (nodeName != 'SPAN' || node.getAttribute('data-mce-type') != 'bookmark')) {
  146. hasContentNode = true;
  147. }
  148. if (blockElements[nodeName]) {
  149. fragment.appendChild(node);
  150. textBlock = null;
  151. } else {
  152. if (blockName) {
  153. if (!textBlock) {
  154. textBlock = dom.create(blockName);
  155. fragment.appendChild(textBlock);
  156. }
  157. textBlock.appendChild(node);
  158. } else {
  159. fragment.appendChild(node);
  160. }
  161. }
  162. }
  163. }
  164. if (!editor.settings.forced_root_block) {
  165. fragment.appendChild(dom.create('br'));
  166. } else {
  167. // BR is needed in empty blocks on non IE browsers
  168. if (!hasContentNode && (!tinymce.Env.ie || tinymce.Env.ie > 10)) {
  169. textBlock.appendChild(dom.create('br', {'data-mce-bogus': '1'}));
  170. }
  171. }
  172. return fragment;
  173. }
  174. function getSelectedListItems() {
  175. return tinymce.grep(selection.getSelectedBlocks(), function(block) {
  176. return /^(LI|DT|DD)$/.test(block.nodeName);
  177. });
  178. }
  179. function splitList(ul, li, newBlock) {
  180. var tmpRng, fragment, bookmarks, node;
  181. function removeAndKeepBookmarks(targetNode) {
  182. tinymce.each(bookmarks, function(node) {
  183. targetNode.parentNode.insertBefore(node, li.parentNode);
  184. });
  185. dom.remove(targetNode);
  186. }
  187. bookmarks = dom.select('span[data-mce-type="bookmark"]', ul);
  188. newBlock = newBlock || createNewTextBlock(li);
  189. tmpRng = dom.createRng();
  190. tmpRng.setStartAfter(li);
  191. tmpRng.setEndAfter(ul);
  192. fragment = tmpRng.extractContents();
  193. for (node = fragment.firstChild; node; node = node.firstChild) {
  194. if (node.nodeName == 'LI' && dom.isEmpty(node)) {
  195. dom.remove(node);
  196. break;
  197. }
  198. }
  199. if (!dom.isEmpty(fragment)) {
  200. dom.insertAfter(fragment, ul);
  201. }
  202. dom.insertAfter(newBlock, ul);
  203. if (isEmpty(li.parentNode)) {
  204. removeAndKeepBookmarks(li.parentNode);
  205. }
  206. dom.remove(li);
  207. if (isEmpty(ul)) {
  208. dom.remove(ul);
  209. }
  210. }
  211. var shouldMerge = function (listBlock, sibling) {
  212. var targetStyle = editor.dom.getStyle(listBlock, 'list-style-type', true);
  213. var style = editor.dom.getStyle(sibling, 'list-style-type', true);
  214. return targetStyle === style;
  215. };
  216. function mergeWithAdjacentLists(listBlock) {
  217. var sibling, node;
  218. sibling = listBlock.nextSibling;
  219. if (sibling && isListNode(sibling) && sibling.nodeName == listBlock.nodeName && shouldMerge(listBlock, sibling)) {
  220. while ((node = sibling.firstChild)) {
  221. listBlock.appendChild(node);
  222. }
  223. dom.remove(sibling);
  224. }
  225. sibling = listBlock.previousSibling;
  226. if (sibling && isListNode(sibling) && sibling.nodeName == listBlock.nodeName && shouldMerge(listBlock, sibling)) {
  227. while ((node = sibling.firstChild)) {
  228. listBlock.insertBefore(node, listBlock.firstChild);
  229. }
  230. dom.remove(sibling);
  231. }
  232. }
  233. /**
  234. * Normalizes the all lists in the specified element.
  235. */
  236. function normalizeList(element) {
  237. tinymce.each(tinymce.grep(dom.select('ol,ul', element)), function(ul) {
  238. var sibling, parentNode = ul.parentNode;
  239. // Move UL/OL to previous LI if it's the only child of a LI
  240. if (parentNode.nodeName == 'LI' && parentNode.firstChild == ul) {
  241. sibling = parentNode.previousSibling;
  242. if (sibling && sibling.nodeName == 'LI') {
  243. sibling.appendChild(ul);
  244. if (isEmpty(parentNode)) {
  245. dom.remove(parentNode);
  246. }
  247. }
  248. }
  249. // Append OL/UL to previous LI if it's in a parent OL/UL i.e. old HTML4
  250. if (isListNode(parentNode)) {
  251. sibling = parentNode.previousSibling;
  252. if (sibling && sibling.nodeName == 'LI') {
  253. sibling.appendChild(ul);
  254. }
  255. }
  256. });
  257. }
  258. function outdent(li) {
  259. var ul = li.parentNode, ulParent = ul.parentNode, newBlock;
  260. function removeEmptyLi(li) {
  261. if (isEmpty(li)) {
  262. dom.remove(li);
  263. }
  264. }
  265. if (isEditorBody(ul)) {
  266. return true;
  267. }
  268. if (li.nodeName == 'DD') {
  269. dom.rename(li, 'DT');
  270. return true;
  271. }
  272. if (isFirstChild(li) && isLastChild(li)) {
  273. if (ulParent.nodeName == "LI") {
  274. dom.insertAfter(li, ulParent);
  275. removeEmptyLi(ulParent);
  276. dom.remove(ul);
  277. } else if (isListNode(ulParent)) {
  278. dom.remove(ul, true);
  279. } else {
  280. ulParent.insertBefore(createNewTextBlock(li), ul);
  281. dom.remove(ul);
  282. }
  283. return true;
  284. } else if (isFirstChild(li)) {
  285. if (ulParent.nodeName == "LI") {
  286. dom.insertAfter(li, ulParent);
  287. li.appendChild(ul);
  288. removeEmptyLi(ulParent);
  289. } else if (isListNode(ulParent)) {
  290. ulParent.insertBefore(li, ul);
  291. } else {
  292. ulParent.insertBefore(createNewTextBlock(li), ul);
  293. dom.remove(li);
  294. }
  295. return true;
  296. } else if (isLastChild(li)) {
  297. if (ulParent.nodeName == "LI") {
  298. dom.insertAfter(li, ulParent);
  299. } else if (isListNode(ulParent)) {
  300. dom.insertAfter(li, ul);
  301. } else {
  302. dom.insertAfter(createNewTextBlock(li), ul);
  303. dom.remove(li);
  304. }
  305. return true;
  306. }
  307. if (ulParent.nodeName == 'LI') {
  308. ul = ulParent;
  309. newBlock = createNewTextBlock(li, 'LI');
  310. } else if (isListNode(ulParent)) {
  311. newBlock = createNewTextBlock(li, 'LI');
  312. } else {
  313. newBlock = createNewTextBlock(li);
  314. }
  315. splitList(ul, li, newBlock);
  316. normalizeList(ul.parentNode);
  317. return true;
  318. }
  319. function indent(li) {
  320. var sibling, newList, listStyle;
  321. function mergeLists(from, to) {
  322. var node;
  323. if (isListNode(from)) {
  324. while ((node = li.lastChild.firstChild)) {
  325. to.appendChild(node);
  326. }
  327. dom.remove(from);
  328. }
  329. }
  330. if (li.nodeName == 'DT') {
  331. dom.rename(li, 'DD');
  332. return true;
  333. }
  334. sibling = li.previousSibling;
  335. if (sibling && isListNode(sibling)) {
  336. sibling.appendChild(li);
  337. return true;
  338. }
  339. if (sibling && sibling.nodeName == 'LI' && isListNode(sibling.lastChild)) {
  340. sibling.lastChild.appendChild(li);
  341. mergeLists(li.lastChild, sibling.lastChild);
  342. return true;
  343. }
  344. sibling = li.nextSibling;
  345. if (sibling && isListNode(sibling)) {
  346. sibling.insertBefore(li, sibling.firstChild);
  347. return true;
  348. }
  349. /*if (sibling && sibling.nodeName == 'LI' && isListNode(li.lastChild)) {
  350. return false;
  351. }*/
  352. sibling = li.previousSibling;
  353. if (sibling && sibling.nodeName == 'LI') {
  354. newList = dom.create(li.parentNode.nodeName);
  355. listStyle = dom.getStyle(li.parentNode, 'listStyleType');
  356. if (listStyle) {
  357. dom.setStyle(newList, 'listStyleType', listStyle);
  358. }
  359. sibling.appendChild(newList);
  360. newList.appendChild(li);
  361. mergeLists(li.lastChild, newList);
  362. return true;
  363. }
  364. return false;
  365. }
  366. function indentSelection() {
  367. var listElements = getSelectedListItems();
  368. if (listElements.length) {
  369. var bookmark = createBookmark(selection.getRng(true));
  370. for (var i = 0; i < listElements.length; i++) {
  371. if (!indent(listElements[i]) && i === 0) {
  372. break;
  373. }
  374. }
  375. moveToBookmark(bookmark);
  376. editor.nodeChanged();
  377. return true;
  378. }
  379. }
  380. function outdentSelection() {
  381. var listElements = getSelectedListItems();
  382. if (listElements.length) {
  383. var bookmark = createBookmark(selection.getRng(true));
  384. var i, y, root = editor.getBody();
  385. i = listElements.length;
  386. while (i--) {
  387. var node = listElements[i].parentNode;
  388. while (node && node != root) {
  389. y = listElements.length;
  390. while (y--) {
  391. if (listElements[y] === node) {
  392. listElements.splice(i, 1);
  393. break;
  394. }
  395. }
  396. node = node.parentNode;
  397. }
  398. }
  399. for (i = 0; i < listElements.length; i++) {
  400. if (!outdent(listElements[i]) && i === 0) {
  401. break;
  402. }
  403. }
  404. moveToBookmark(bookmark);
  405. editor.nodeChanged();
  406. return true;
  407. }
  408. }
  409. function applyList(listName, detail) {
  410. var rng = selection.getRng(true), bookmark, listItemName = 'LI';
  411. if (dom.getContentEditable(selection.getNode()) === "false") {
  412. return;
  413. }
  414. listName = listName.toUpperCase();
  415. if (listName == 'DL') {
  416. listItemName = 'DT';
  417. }
  418. function getSelectedTextBlocks() {
  419. var textBlocks = [], root = editor.getBody();
  420. function getEndPointNode(start) {
  421. var container, offset;
  422. container = rng[start ? 'startContainer' : 'endContainer'];
  423. offset = rng[start ? 'startOffset' : 'endOffset'];
  424. // Resolve node index
  425. if (container.nodeType == 1) {
  426. container = container.childNodes[Math.min(offset, container.childNodes.length - 1)] || container;
  427. }
  428. while (container.parentNode != root) {
  429. if (isTextBlock(container)) {
  430. return container;
  431. }
  432. if (/^(TD|TH)$/.test(container.parentNode.nodeName)) {
  433. return container;
  434. }
  435. container = container.parentNode;
  436. }
  437. return container;
  438. }
  439. var startNode = getEndPointNode(true);
  440. var endNode = getEndPointNode();
  441. var block, siblings = [];
  442. for (var node = startNode; node; node = node.nextSibling) {
  443. siblings.push(node);
  444. if (node == endNode) {
  445. break;
  446. }
  447. }
  448. tinymce.each(siblings, function(node) {
  449. if (isTextBlock(node)) {
  450. textBlocks.push(node);
  451. block = null;
  452. return;
  453. }
  454. if (dom.isBlock(node) || isBr(node)) {
  455. if (isBr(node)) {
  456. dom.remove(node);
  457. }
  458. block = null;
  459. return;
  460. }
  461. var nextSibling = node.nextSibling;
  462. if (tinymce.dom.BookmarkManager.isBookmarkNode(node)) {
  463. if (isTextBlock(nextSibling) || (!nextSibling && node.parentNode == root)) {
  464. block = null;
  465. return;
  466. }
  467. }
  468. if (!block) {
  469. block = dom.create('p');
  470. node.parentNode.insertBefore(block, node);
  471. textBlocks.push(block);
  472. }
  473. block.appendChild(node);
  474. });
  475. return textBlocks;
  476. }
  477. bookmark = createBookmark(rng);
  478. tinymce.each(getSelectedTextBlocks(), function(block) {
  479. var listBlock, sibling;
  480. var hasCompatibleStyle = function (sib) {
  481. var sibStyle = dom.getStyle(sib, 'list-style-type');
  482. var detailStyle = detail ? detail['list-style-type'] : '';
  483. detailStyle = detailStyle === null ? '' : detailStyle;
  484. return sibStyle === detailStyle;
  485. };
  486. sibling = block.previousSibling;
  487. if (sibling && isListNode(sibling) && sibling.nodeName == listName && hasCompatibleStyle(sibling)) {
  488. listBlock = sibling;
  489. block = dom.rename(block, listItemName);
  490. sibling.appendChild(block);
  491. } else {
  492. listBlock = dom.create(listName);
  493. block.parentNode.insertBefore(listBlock, block);
  494. listBlock.appendChild(block);
  495. block = dom.rename(block, listItemName);
  496. }
  497. updateListStyle(listBlock, detail);
  498. mergeWithAdjacentLists(listBlock);
  499. });
  500. moveToBookmark(bookmark);
  501. }
  502. var updateListStyle = function (el, detail) {
  503. dom.setStyle(el, 'list-style-type', detail ? detail['list-style-type'] : null);
  504. };
  505. function removeList() {
  506. var bookmark = createBookmark(selection.getRng(true)), root = editor.getBody();
  507. tinymce.each(getSelectedListItems(), function(li) {
  508. var node, rootList;
  509. if (isEditorBody(li.parentNode)) {
  510. return;
  511. }
  512. if (isEmpty(li)) {
  513. outdent(li);
  514. return;
  515. }
  516. for (node = li; node && node != root; node = node.parentNode) {
  517. if (isListNode(node)) {
  518. rootList = node;
  519. }
  520. }
  521. splitList(rootList, li);
  522. });
  523. moveToBookmark(bookmark);
  524. }
  525. function toggleList(listName, detail) {
  526. var parentList = dom.getParent(selection.getStart(), 'OL,UL,DL');
  527. if (isEditorBody(parentList)) {
  528. return;
  529. }
  530. if (parentList) {
  531. if (parentList.nodeName == listName) {
  532. removeList(listName);
  533. } else {
  534. var bookmark = createBookmark(selection.getRng(true));
  535. updateListStyle(parentList, detail);
  536. mergeWithAdjacentLists(dom.rename(parentList, listName));
  537. moveToBookmark(bookmark);
  538. }
  539. } else {
  540. applyList(listName, detail);
  541. }
  542. }
  543. function queryListCommandState(listName) {
  544. return function() {
  545. var parentList = dom.getParent(editor.selection.getStart(), 'UL,OL,DL');
  546. return parentList && parentList.nodeName == listName;
  547. };
  548. }
  549. function isBogusBr(node) {
  550. if (!isBr(node)) {
  551. return false;
  552. }
  553. if (dom.isBlock(node.nextSibling) && !isBr(node.previousSibling)) {
  554. return true;
  555. }
  556. return false;
  557. }
  558. self.backspaceDelete = function(isForward) {
  559. function findNextCaretContainer(rng, isForward) {
  560. var node = rng.startContainer, offset = rng.startOffset;
  561. var nonEmptyBlocks, walker;
  562. if (node.nodeType == 3 && (isForward ? offset < node.data.length : offset > 0)) {
  563. return node;
  564. }
  565. nonEmptyBlocks = editor.schema.getNonEmptyElements();
  566. if (node.nodeType == 1) {
  567. node = tinymce.dom.RangeUtils.getNode(node, offset);
  568. }
  569. walker = new tinymce.dom.TreeWalker(node, editor.getBody());
  570. // Delete at <li>|<br></li> then jump over the bogus br
  571. if (isForward) {
  572. if (isBogusBr(node)) {
  573. walker.next();
  574. }
  575. }
  576. while ((node = walker[isForward ? 'next' : 'prev2']())) {
  577. if (node.nodeName == 'LI' && !node.hasChildNodes()) {
  578. return node;
  579. }
  580. if (nonEmptyBlocks[node.nodeName]) {
  581. return node;
  582. }
  583. if (node.nodeType == 3 && node.data.length > 0) {
  584. return node;
  585. }
  586. }
  587. }
  588. function mergeLiElements(fromElm, toElm) {
  589. var node, listNode, ul = fromElm.parentNode;
  590. if (!isChildOfBody(fromElm) || !isChildOfBody(toElm)) {
  591. return;
  592. }
  593. if (isListNode(toElm.lastChild)) {
  594. listNode = toElm.lastChild;
  595. }
  596. if (ul == toElm.lastChild) {
  597. if (isBr(ul.previousSibling)) {
  598. dom.remove(ul.previousSibling);
  599. }
  600. }
  601. node = toElm.lastChild;
  602. if (node && isBr(node) && fromElm.hasChildNodes()) {
  603. dom.remove(node);
  604. }
  605. if (isEmpty(toElm, true)) {
  606. dom.$(toElm).empty();
  607. }
  608. if (!isEmpty(fromElm, true)) {
  609. while ((node = fromElm.firstChild)) {
  610. toElm.appendChild(node);
  611. }
  612. }
  613. if (listNode) {
  614. toElm.appendChild(listNode);
  615. }
  616. dom.remove(fromElm);
  617. if (isEmpty(ul) && !isEditorBody(ul)) {
  618. dom.remove(ul);
  619. }
  620. }
  621. if (selection.isCollapsed()) {
  622. var li = dom.getParent(selection.getStart(), 'LI'), ul, rng, otherLi;
  623. if (li) {
  624. ul = li.parentNode;
  625. if (isEditorBody(ul) && dom.isEmpty(ul)) {
  626. return true;
  627. }
  628. rng = selection.getRng(true);
  629. otherLi = dom.getParent(findNextCaretContainer(rng, isForward), 'LI');
  630. if (otherLi && otherLi != li) {
  631. var bookmark = createBookmark(rng);
  632. if (isForward) {
  633. mergeLiElements(otherLi, li);
  634. } else {
  635. mergeLiElements(li, otherLi);
  636. }
  637. moveToBookmark(bookmark);
  638. return true;
  639. } else if (!otherLi) {
  640. if (!isForward && removeList(ul.nodeName)) {
  641. return true;
  642. }
  643. }
  644. }
  645. }
  646. };
  647. editor.on('BeforeExecCommand', function(e) {
  648. var cmd = e.command.toLowerCase(), isHandled;
  649. if (cmd == "indent") {
  650. if (indentSelection()) {
  651. isHandled = true;
  652. }
  653. } else if (cmd == "outdent") {
  654. if (outdentSelection()) {
  655. isHandled = true;
  656. }
  657. }
  658. if (isHandled) {
  659. editor.fire('ExecCommand', {command: e.command});
  660. e.preventDefault();
  661. return true;
  662. }
  663. });
  664. editor.addCommand('InsertUnorderedList', function(ui, detail) {
  665. toggleList('UL', detail);
  666. });
  667. editor.addCommand('InsertOrderedList', function(ui, detail) {
  668. toggleList('OL', detail);
  669. });
  670. editor.addCommand('InsertDefinitionList', function(ui, detail) {
  671. toggleList('DL', detail);
  672. });
  673. editor.addQueryStateHandler('InsertUnorderedList', queryListCommandState('UL'));
  674. editor.addQueryStateHandler('InsertOrderedList', queryListCommandState('OL'));
  675. editor.addQueryStateHandler('InsertDefinitionList', queryListCommandState('DL'));
  676. editor.on('keydown', function(e) {
  677. // Check for tab but not ctrl/cmd+tab since it switches browser tabs
  678. if (e.keyCode != 9 || tinymce.util.VK.metaKeyPressed(e)) {
  679. return;
  680. }
  681. if (editor.dom.getParent(editor.selection.getStart(), 'LI,DT,DD')) {
  682. e.preventDefault();
  683. if (e.shiftKey) {
  684. outdentSelection();
  685. } else {
  686. indentSelection();
  687. }
  688. }
  689. });
  690. });
  691. editor.addButton('indent', {
  692. icon: 'indent',
  693. title: 'Increase indent',
  694. cmd: 'Indent',
  695. onPostRender: function() {
  696. var ctrl = this;
  697. editor.on('nodechange', function() {
  698. var blocks = editor.selection.getSelectedBlocks();
  699. var disable = false;
  700. for (var i = 0, l = blocks.length; !disable && i < l; i++) {
  701. var tag = blocks[i].nodeName;
  702. disable = (tag == 'LI' && isFirstChild(blocks[i]) || tag == 'UL' || tag == 'OL' || tag == 'DD');
  703. }
  704. ctrl.disabled(disable);
  705. });
  706. }
  707. });
  708. editor.on('keydown', function(e) {
  709. if (e.keyCode == tinymce.util.VK.BACKSPACE) {
  710. if (self.backspaceDelete()) {
  711. e.preventDefault();
  712. }
  713. } else if (e.keyCode == tinymce.util.VK.DELETE) {
  714. if (self.backspaceDelete(true)) {
  715. e.preventDefault();
  716. }
  717. }
  718. });
  719. });