plugin.js 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182
  1. (function () {
  2. var lists = (function (domGlobals) {
  3. 'use strict';
  4. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  5. var global$1 = tinymce.util.Tools.resolve('tinymce.dom.RangeUtils');
  6. var global$2 = tinymce.util.Tools.resolve('tinymce.dom.TreeWalker');
  7. var global$3 = tinymce.util.Tools.resolve('tinymce.util.VK');
  8. var global$4 = tinymce.util.Tools.resolve('tinymce.dom.BookmarkManager');
  9. var global$5 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  10. var global$6 = tinymce.util.Tools.resolve('tinymce.dom.DOMUtils');
  11. var isTextNode = function (node) {
  12. return node && node.nodeType === 3;
  13. };
  14. var isListNode = function (node) {
  15. return node && /^(OL|UL|DL)$/.test(node.nodeName);
  16. };
  17. var isOlUlNode = function (node) {
  18. return node && /^(OL|UL)$/.test(node.nodeName);
  19. };
  20. var isListItemNode = function (node) {
  21. return node && /^(LI|DT|DD)$/.test(node.nodeName);
  22. };
  23. var isDlItemNode = function (node) {
  24. return node && /^(DT|DD)$/.test(node.nodeName);
  25. };
  26. var isTableCellNode = function (node) {
  27. return node && /^(TH|TD)$/.test(node.nodeName);
  28. };
  29. var isBr = function (node) {
  30. return node && node.nodeName === 'BR';
  31. };
  32. var isFirstChild = function (node) {
  33. return node.parentNode.firstChild === node;
  34. };
  35. var isLastChild = function (node) {
  36. return node.parentNode.lastChild === node;
  37. };
  38. var isTextBlock = function (editor, node) {
  39. return node && !!editor.schema.getTextBlockElements()[node.nodeName];
  40. };
  41. var isBlock = function (node, blockElements) {
  42. return node && node.nodeName in blockElements;
  43. };
  44. var isBogusBr = function (dom, node) {
  45. if (!isBr(node)) {
  46. return false;
  47. }
  48. if (dom.isBlock(node.nextSibling) && !isBr(node.previousSibling)) {
  49. return true;
  50. }
  51. return false;
  52. };
  53. var isEmpty = function (dom, elm, keepBookmarks) {
  54. var empty = dom.isEmpty(elm);
  55. if (keepBookmarks && dom.select('span[data-mce-type=bookmark]', elm).length > 0) {
  56. return false;
  57. }
  58. return empty;
  59. };
  60. var isChildOfBody = function (dom, elm) {
  61. return dom.isChildOf(elm, dom.getRoot());
  62. };
  63. var NodeType = {
  64. isTextNode: isTextNode,
  65. isListNode: isListNode,
  66. isOlUlNode: isOlUlNode,
  67. isDlItemNode: isDlItemNode,
  68. isListItemNode: isListItemNode,
  69. isTableCellNode: isTableCellNode,
  70. isBr: isBr,
  71. isFirstChild: isFirstChild,
  72. isLastChild: isLastChild,
  73. isTextBlock: isTextBlock,
  74. isBlock: isBlock,
  75. isBogusBr: isBogusBr,
  76. isEmpty: isEmpty,
  77. isChildOfBody: isChildOfBody
  78. };
  79. var getNormalizedPoint = function (container, offset) {
  80. if (NodeType.isTextNode(container)) {
  81. return {
  82. container: container,
  83. offset: offset
  84. };
  85. }
  86. var node = global$1.getNode(container, offset);
  87. if (NodeType.isTextNode(node)) {
  88. return {
  89. container: node,
  90. offset: offset >= container.childNodes.length ? node.data.length : 0
  91. };
  92. } else if (node.previousSibling && NodeType.isTextNode(node.previousSibling)) {
  93. return {
  94. container: node.previousSibling,
  95. offset: node.previousSibling.data.length
  96. };
  97. } else if (node.nextSibling && NodeType.isTextNode(node.nextSibling)) {
  98. return {
  99. container: node.nextSibling,
  100. offset: 0
  101. };
  102. }
  103. return {
  104. container: container,
  105. offset: offset
  106. };
  107. };
  108. var normalizeRange = function (rng) {
  109. var outRng = rng.cloneRange();
  110. var rangeStart = getNormalizedPoint(rng.startContainer, rng.startOffset);
  111. outRng.setStart(rangeStart.container, rangeStart.offset);
  112. var rangeEnd = getNormalizedPoint(rng.endContainer, rng.endOffset);
  113. outRng.setEnd(rangeEnd.container, rangeEnd.offset);
  114. return outRng;
  115. };
  116. var Range = {
  117. getNormalizedPoint: getNormalizedPoint,
  118. normalizeRange: normalizeRange
  119. };
  120. var DOM = global$6.DOM;
  121. var createBookmark = function (rng) {
  122. var bookmark = {};
  123. var setupEndPoint = function (start) {
  124. var offsetNode, container, offset;
  125. container = rng[start ? 'startContainer' : 'endContainer'];
  126. offset = rng[start ? 'startOffset' : 'endOffset'];
  127. if (container.nodeType === 1) {
  128. offsetNode = DOM.create('span', { 'data-mce-type': 'bookmark' });
  129. if (container.hasChildNodes()) {
  130. offset = Math.min(offset, container.childNodes.length - 1);
  131. if (start) {
  132. container.insertBefore(offsetNode, container.childNodes[offset]);
  133. } else {
  134. DOM.insertAfter(offsetNode, container.childNodes[offset]);
  135. }
  136. } else {
  137. container.appendChild(offsetNode);
  138. }
  139. container = offsetNode;
  140. offset = 0;
  141. }
  142. bookmark[start ? 'startContainer' : 'endContainer'] = container;
  143. bookmark[start ? 'startOffset' : 'endOffset'] = offset;
  144. };
  145. setupEndPoint(true);
  146. if (!rng.collapsed) {
  147. setupEndPoint();
  148. }
  149. return bookmark;
  150. };
  151. var resolveBookmark = function (bookmark) {
  152. function restoreEndPoint(start) {
  153. var container, offset, node;
  154. var nodeIndex = function (container) {
  155. var node = container.parentNode.firstChild, idx = 0;
  156. while (node) {
  157. if (node === container) {
  158. return idx;
  159. }
  160. if (node.nodeType !== 1 || node.getAttribute('data-mce-type') !== 'bookmark') {
  161. idx++;
  162. }
  163. node = node.nextSibling;
  164. }
  165. return -1;
  166. };
  167. container = node = bookmark[start ? 'startContainer' : 'endContainer'];
  168. offset = bookmark[start ? 'startOffset' : 'endOffset'];
  169. if (!container) {
  170. return;
  171. }
  172. if (container.nodeType === 1) {
  173. offset = nodeIndex(container);
  174. container = container.parentNode;
  175. DOM.remove(node);
  176. if (!container.hasChildNodes() && DOM.isBlock(container)) {
  177. container.appendChild(DOM.create('br'));
  178. }
  179. }
  180. bookmark[start ? 'startContainer' : 'endContainer'] = container;
  181. bookmark[start ? 'startOffset' : 'endOffset'] = offset;
  182. }
  183. restoreEndPoint(true);
  184. restoreEndPoint();
  185. var rng = DOM.createRng();
  186. rng.setStart(bookmark.startContainer, bookmark.startOffset);
  187. if (bookmark.endContainer) {
  188. rng.setEnd(bookmark.endContainer, bookmark.endOffset);
  189. }
  190. return Range.normalizeRange(rng);
  191. };
  192. var Bookmark = {
  193. createBookmark: createBookmark,
  194. resolveBookmark: resolveBookmark
  195. };
  196. var constant = function (value) {
  197. return function () {
  198. return value;
  199. };
  200. };
  201. var not = function (f) {
  202. return function () {
  203. var args = [];
  204. for (var _i = 0; _i < arguments.length; _i++) {
  205. args[_i] = arguments[_i];
  206. }
  207. return !f.apply(null, args);
  208. };
  209. };
  210. var never = constant(false);
  211. var always = constant(true);
  212. var never$1 = never;
  213. var always$1 = always;
  214. var none = function () {
  215. return NONE;
  216. };
  217. var NONE = function () {
  218. var eq = function (o) {
  219. return o.isNone();
  220. };
  221. var call = function (thunk) {
  222. return thunk();
  223. };
  224. var id = function (n) {
  225. return n;
  226. };
  227. var noop = function () {
  228. };
  229. var nul = function () {
  230. return null;
  231. };
  232. var undef = function () {
  233. return undefined;
  234. };
  235. var me = {
  236. fold: function (n, s) {
  237. return n();
  238. },
  239. is: never$1,
  240. isSome: never$1,
  241. isNone: always$1,
  242. getOr: id,
  243. getOrThunk: call,
  244. getOrDie: function (msg) {
  245. throw new Error(msg || 'error: getOrDie called on none.');
  246. },
  247. getOrNull: nul,
  248. getOrUndefined: undef,
  249. or: id,
  250. orThunk: call,
  251. map: none,
  252. ap: none,
  253. each: noop,
  254. bind: none,
  255. flatten: none,
  256. exists: never$1,
  257. forall: always$1,
  258. filter: none,
  259. equals: eq,
  260. equals_: eq,
  261. toArray: function () {
  262. return [];
  263. },
  264. toString: constant('none()')
  265. };
  266. if (Object.freeze) {
  267. Object.freeze(me);
  268. }
  269. return me;
  270. }();
  271. var some = function (a) {
  272. var constant_a = function () {
  273. return a;
  274. };
  275. var self = function () {
  276. return me;
  277. };
  278. var map = function (f) {
  279. return some(f(a));
  280. };
  281. var bind = function (f) {
  282. return f(a);
  283. };
  284. var me = {
  285. fold: function (n, s) {
  286. return s(a);
  287. },
  288. is: function (v) {
  289. return a === v;
  290. },
  291. isSome: always$1,
  292. isNone: never$1,
  293. getOr: constant_a,
  294. getOrThunk: constant_a,
  295. getOrDie: constant_a,
  296. getOrNull: constant_a,
  297. getOrUndefined: constant_a,
  298. or: self,
  299. orThunk: self,
  300. map: map,
  301. ap: function (optfab) {
  302. return optfab.fold(none, function (fab) {
  303. return some(fab(a));
  304. });
  305. },
  306. each: function (f) {
  307. f(a);
  308. },
  309. bind: bind,
  310. flatten: constant_a,
  311. exists: bind,
  312. forall: bind,
  313. filter: function (f) {
  314. return f(a) ? me : NONE;
  315. },
  316. equals: function (o) {
  317. return o.is(a);
  318. },
  319. equals_: function (o, elementEq) {
  320. return o.fold(never$1, function (b) {
  321. return elementEq(a, b);
  322. });
  323. },
  324. toArray: function () {
  325. return [a];
  326. },
  327. toString: function () {
  328. return 'some(' + a + ')';
  329. }
  330. };
  331. return me;
  332. };
  333. var from = function (value) {
  334. return value === null || value === undefined ? NONE : some(value);
  335. };
  336. var Option = {
  337. some: some,
  338. none: none,
  339. from: from
  340. };
  341. var typeOf = function (x) {
  342. if (x === null) {
  343. return 'null';
  344. }
  345. var t = typeof x;
  346. if (t === 'object' && (Array.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'Array')) {
  347. return 'array';
  348. }
  349. if (t === 'object' && (String.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'String')) {
  350. return 'string';
  351. }
  352. return t;
  353. };
  354. var isType = function (type) {
  355. return function (value) {
  356. return typeOf(value) === type;
  357. };
  358. };
  359. var isString = isType('string');
  360. var isArray = isType('array');
  361. var isBoolean = isType('boolean');
  362. var isFunction = isType('function');
  363. var isNumber = isType('number');
  364. var slice = Array.prototype.slice;
  365. var map = function (xs, f) {
  366. var len = xs.length;
  367. var r = new Array(len);
  368. for (var i = 0; i < len; i++) {
  369. var x = xs[i];
  370. r[i] = f(x, i, xs);
  371. }
  372. return r;
  373. };
  374. var each = function (xs, f) {
  375. for (var i = 0, len = xs.length; i < len; i++) {
  376. var x = xs[i];
  377. f(x, i, xs);
  378. }
  379. };
  380. var filter = function (xs, pred) {
  381. var r = [];
  382. for (var i = 0, len = xs.length; i < len; i++) {
  383. var x = xs[i];
  384. if (pred(x, i, xs)) {
  385. r.push(x);
  386. }
  387. }
  388. return r;
  389. };
  390. var groupBy = function (xs, f) {
  391. if (xs.length === 0) {
  392. return [];
  393. } else {
  394. var wasType = f(xs[0]);
  395. var r = [];
  396. var group = [];
  397. for (var i = 0, len = xs.length; i < len; i++) {
  398. var x = xs[i];
  399. var type = f(x);
  400. if (type !== wasType) {
  401. r.push(group);
  402. group = [];
  403. }
  404. wasType = type;
  405. group.push(x);
  406. }
  407. if (group.length !== 0) {
  408. r.push(group);
  409. }
  410. return r;
  411. }
  412. };
  413. var foldl = function (xs, f, acc) {
  414. each(xs, function (x) {
  415. acc = f(acc, x);
  416. });
  417. return acc;
  418. };
  419. var find = function (xs, pred) {
  420. for (var i = 0, len = xs.length; i < len; i++) {
  421. var x = xs[i];
  422. if (pred(x, i, xs)) {
  423. return Option.some(x);
  424. }
  425. }
  426. return Option.none();
  427. };
  428. var push = Array.prototype.push;
  429. var flatten = function (xs) {
  430. var r = [];
  431. for (var i = 0, len = xs.length; i < len; ++i) {
  432. if (!isArray(xs[i])) {
  433. throw new Error('Arr.flatten item ' + i + ' was not an array, input: ' + xs);
  434. }
  435. push.apply(r, xs[i]);
  436. }
  437. return r;
  438. };
  439. var bind = function (xs, f) {
  440. var output = map(xs, f);
  441. return flatten(output);
  442. };
  443. var reverse = function (xs) {
  444. var r = slice.call(xs, 0);
  445. r.reverse();
  446. return r;
  447. };
  448. var head = function (xs) {
  449. return xs.length === 0 ? Option.none() : Option.some(xs[0]);
  450. };
  451. var last = function (xs) {
  452. return xs.length === 0 ? Option.none() : Option.some(xs[xs.length - 1]);
  453. };
  454. var from$1 = isFunction(Array.from) ? Array.from : function (x) {
  455. return slice.call(x);
  456. };
  457. var Global = typeof domGlobals.window !== 'undefined' ? domGlobals.window : Function('return this;')();
  458. var path = function (parts, scope) {
  459. var o = scope !== undefined && scope !== null ? scope : Global;
  460. for (var i = 0; i < parts.length && o !== undefined && o !== null; ++i) {
  461. o = o[parts[i]];
  462. }
  463. return o;
  464. };
  465. var resolve = function (p, scope) {
  466. var parts = p.split('.');
  467. return path(parts, scope);
  468. };
  469. var unsafe = function (name, scope) {
  470. return resolve(name, scope);
  471. };
  472. var getOrDie = function (name, scope) {
  473. var actual = unsafe(name, scope);
  474. if (actual === undefined || actual === null) {
  475. throw new Error(name + ' not available on this browser');
  476. }
  477. return actual;
  478. };
  479. var Global$1 = { getOrDie: getOrDie };
  480. var htmlElement = function (scope) {
  481. return Global$1.getOrDie('HTMLElement', scope);
  482. };
  483. var isPrototypeOf = function (x) {
  484. var scope = resolve('ownerDocument.defaultView', x);
  485. return htmlElement(scope).prototype.isPrototypeOf(x);
  486. };
  487. var HTMLElement = { isPrototypeOf: isPrototypeOf };
  488. var global$7 = tinymce.util.Tools.resolve('tinymce.dom.DomQuery');
  489. var getParentList = function (editor) {
  490. var selectionStart = editor.selection.getStart(true);
  491. return editor.dom.getParent(selectionStart, 'OL,UL,DL', getClosestListRootElm(editor, selectionStart));
  492. };
  493. var isParentListSelected = function (parentList, selectedBlocks) {
  494. return parentList && selectedBlocks.length === 1 && selectedBlocks[0] === parentList;
  495. };
  496. var findSubLists = function (parentList) {
  497. return global$5.grep(parentList.querySelectorAll('ol,ul,dl'), function (elm) {
  498. return NodeType.isListNode(elm);
  499. });
  500. };
  501. var getSelectedSubLists = function (editor) {
  502. var parentList = getParentList(editor);
  503. var selectedBlocks = editor.selection.getSelectedBlocks();
  504. if (isParentListSelected(parentList, selectedBlocks)) {
  505. return findSubLists(parentList);
  506. } else {
  507. return global$5.grep(selectedBlocks, function (elm) {
  508. return NodeType.isListNode(elm) && parentList !== elm;
  509. });
  510. }
  511. };
  512. var findParentListItemsNodes = function (editor, elms) {
  513. var listItemsElms = global$5.map(elms, function (elm) {
  514. var parentLi = editor.dom.getParent(elm, 'li,dd,dt', getClosestListRootElm(editor, elm));
  515. return parentLi ? parentLi : elm;
  516. });
  517. return global$7.unique(listItemsElms);
  518. };
  519. var getSelectedListItems = function (editor) {
  520. var selectedBlocks = editor.selection.getSelectedBlocks();
  521. return global$5.grep(findParentListItemsNodes(editor, selectedBlocks), function (block) {
  522. return NodeType.isListItemNode(block);
  523. });
  524. };
  525. var getSelectedDlItems = function (editor) {
  526. return filter(getSelectedListItems(editor), NodeType.isDlItemNode);
  527. };
  528. var getClosestListRootElm = function (editor, elm) {
  529. var parentTableCell = editor.dom.getParents(elm, 'TD,TH');
  530. var root = parentTableCell.length > 0 ? parentTableCell[0] : editor.getBody();
  531. return root;
  532. };
  533. var findLastParentListNode = function (editor, elm) {
  534. var parentLists = editor.dom.getParents(elm, 'ol,ul', getClosestListRootElm(editor, elm));
  535. return last(parentLists);
  536. };
  537. var getSelectedLists = function (editor) {
  538. var firstList = findLastParentListNode(editor, editor.selection.getStart());
  539. var subsequentLists = filter(editor.selection.getSelectedBlocks(), NodeType.isOlUlNode);
  540. return firstList.toArray().concat(subsequentLists);
  541. };
  542. var getSelectedListRoots = function (editor) {
  543. var selectedLists = getSelectedLists(editor);
  544. return getUniqueListRoots(editor, selectedLists);
  545. };
  546. var getUniqueListRoots = function (editor, lists) {
  547. var listRoots = map(lists, function (list) {
  548. return findLastParentListNode(editor, list).getOr(list);
  549. });
  550. return global$7.unique(listRoots);
  551. };
  552. var isList = function (editor) {
  553. var list = getParentList(editor);
  554. return HTMLElement.isPrototypeOf(list);
  555. };
  556. var Selection = {
  557. isList: isList,
  558. getParentList: getParentList,
  559. getSelectedSubLists: getSelectedSubLists,
  560. getSelectedListItems: getSelectedListItems,
  561. getClosestListRootElm: getClosestListRootElm,
  562. getSelectedDlItems: getSelectedDlItems,
  563. getSelectedListRoots: getSelectedListRoots
  564. };
  565. var fromHtml = function (html, scope) {
  566. var doc = scope || domGlobals.document;
  567. var div = doc.createElement('div');
  568. div.innerHTML = html;
  569. if (!div.hasChildNodes() || div.childNodes.length > 1) {
  570. domGlobals.console.error('HTML does not have a single root node', html);
  571. throw new Error('HTML must have a single root node');
  572. }
  573. return fromDom(div.childNodes[0]);
  574. };
  575. var fromTag = function (tag, scope) {
  576. var doc = scope || domGlobals.document;
  577. var node = doc.createElement(tag);
  578. return fromDom(node);
  579. };
  580. var fromText = function (text, scope) {
  581. var doc = scope || domGlobals.document;
  582. var node = doc.createTextNode(text);
  583. return fromDom(node);
  584. };
  585. var fromDom = function (node) {
  586. if (node === null || node === undefined) {
  587. throw new Error('Node cannot be null or undefined');
  588. }
  589. return { dom: constant(node) };
  590. };
  591. var fromPoint = function (docElm, x, y) {
  592. var doc = docElm.dom();
  593. return Option.from(doc.elementFromPoint(x, y)).map(fromDom);
  594. };
  595. var Element = {
  596. fromHtml: fromHtml,
  597. fromTag: fromTag,
  598. fromText: fromText,
  599. fromDom: fromDom,
  600. fromPoint: fromPoint
  601. };
  602. var liftN = function (arr, f) {
  603. var r = [];
  604. for (var i = 0; i < arr.length; i++) {
  605. var x = arr[i];
  606. if (x.isSome()) {
  607. r.push(x.getOrDie());
  608. } else {
  609. return Option.none();
  610. }
  611. }
  612. return Option.some(f.apply(null, r));
  613. };
  614. var fromElements = function (elements, scope) {
  615. var doc = scope || domGlobals.document;
  616. var fragment = doc.createDocumentFragment();
  617. each(elements, function (element) {
  618. fragment.appendChild(element.dom());
  619. });
  620. return Element.fromDom(fragment);
  621. };
  622. var Immutable = function () {
  623. var fields = [];
  624. for (var _i = 0; _i < arguments.length; _i++) {
  625. fields[_i] = arguments[_i];
  626. }
  627. return function () {
  628. var values = [];
  629. for (var _i = 0; _i < arguments.length; _i++) {
  630. values[_i] = arguments[_i];
  631. }
  632. if (fields.length !== values.length) {
  633. throw new Error('Wrong number of arguments to struct. Expected "[' + fields.length + ']", got ' + values.length + ' arguments');
  634. }
  635. var struct = {};
  636. each(fields, function (name, i) {
  637. struct[name] = constant(values[i]);
  638. });
  639. return struct;
  640. };
  641. };
  642. var keys = Object.keys;
  643. var each$1 = function (obj, f) {
  644. var props = keys(obj);
  645. for (var k = 0, len = props.length; k < len; k++) {
  646. var i = props[k];
  647. var x = obj[i];
  648. f(x, i, obj);
  649. }
  650. };
  651. var node = function () {
  652. var f = Global$1.getOrDie('Node');
  653. return f;
  654. };
  655. var compareDocumentPosition = function (a, b, match) {
  656. return (a.compareDocumentPosition(b) & match) !== 0;
  657. };
  658. var documentPositionPreceding = function (a, b) {
  659. return compareDocumentPosition(a, b, node().DOCUMENT_POSITION_PRECEDING);
  660. };
  661. var documentPositionContainedBy = function (a, b) {
  662. return compareDocumentPosition(a, b, node().DOCUMENT_POSITION_CONTAINED_BY);
  663. };
  664. var Node = {
  665. documentPositionPreceding: documentPositionPreceding,
  666. documentPositionContainedBy: documentPositionContainedBy
  667. };
  668. var cached = function (f) {
  669. var called = false;
  670. var r;
  671. return function () {
  672. var args = [];
  673. for (var _i = 0; _i < arguments.length; _i++) {
  674. args[_i] = arguments[_i];
  675. }
  676. if (!called) {
  677. called = true;
  678. r = f.apply(null, args);
  679. }
  680. return r;
  681. };
  682. };
  683. var firstMatch = function (regexes, s) {
  684. for (var i = 0; i < regexes.length; i++) {
  685. var x = regexes[i];
  686. if (x.test(s)) {
  687. return x;
  688. }
  689. }
  690. return undefined;
  691. };
  692. var find$1 = function (regexes, agent) {
  693. var r = firstMatch(regexes, agent);
  694. if (!r) {
  695. return {
  696. major: 0,
  697. minor: 0
  698. };
  699. }
  700. var group = function (i) {
  701. return Number(agent.replace(r, '$' + i));
  702. };
  703. return nu(group(1), group(2));
  704. };
  705. var detect = function (versionRegexes, agent) {
  706. var cleanedAgent = String(agent).toLowerCase();
  707. if (versionRegexes.length === 0) {
  708. return unknown();
  709. }
  710. return find$1(versionRegexes, cleanedAgent);
  711. };
  712. var unknown = function () {
  713. return nu(0, 0);
  714. };
  715. var nu = function (major, minor) {
  716. return {
  717. major: major,
  718. minor: minor
  719. };
  720. };
  721. var Version = {
  722. nu: nu,
  723. detect: detect,
  724. unknown: unknown
  725. };
  726. var edge = 'Edge';
  727. var chrome = 'Chrome';
  728. var ie = 'IE';
  729. var opera = 'Opera';
  730. var firefox = 'Firefox';
  731. var safari = 'Safari';
  732. var isBrowser = function (name, current) {
  733. return function () {
  734. return current === name;
  735. };
  736. };
  737. var unknown$1 = function () {
  738. return nu$1({
  739. current: undefined,
  740. version: Version.unknown()
  741. });
  742. };
  743. var nu$1 = function (info) {
  744. var current = info.current;
  745. var version = info.version;
  746. return {
  747. current: current,
  748. version: version,
  749. isEdge: isBrowser(edge, current),
  750. isChrome: isBrowser(chrome, current),
  751. isIE: isBrowser(ie, current),
  752. isOpera: isBrowser(opera, current),
  753. isFirefox: isBrowser(firefox, current),
  754. isSafari: isBrowser(safari, current)
  755. };
  756. };
  757. var Browser = {
  758. unknown: unknown$1,
  759. nu: nu$1,
  760. edge: constant(edge),
  761. chrome: constant(chrome),
  762. ie: constant(ie),
  763. opera: constant(opera),
  764. firefox: constant(firefox),
  765. safari: constant(safari)
  766. };
  767. var windows = 'Windows';
  768. var ios = 'iOS';
  769. var android = 'Android';
  770. var linux = 'Linux';
  771. var osx = 'OSX';
  772. var solaris = 'Solaris';
  773. var freebsd = 'FreeBSD';
  774. var isOS = function (name, current) {
  775. return function () {
  776. return current === name;
  777. };
  778. };
  779. var unknown$2 = function () {
  780. return nu$2({
  781. current: undefined,
  782. version: Version.unknown()
  783. });
  784. };
  785. var nu$2 = function (info) {
  786. var current = info.current;
  787. var version = info.version;
  788. return {
  789. current: current,
  790. version: version,
  791. isWindows: isOS(windows, current),
  792. isiOS: isOS(ios, current),
  793. isAndroid: isOS(android, current),
  794. isOSX: isOS(osx, current),
  795. isLinux: isOS(linux, current),
  796. isSolaris: isOS(solaris, current),
  797. isFreeBSD: isOS(freebsd, current)
  798. };
  799. };
  800. var OperatingSystem = {
  801. unknown: unknown$2,
  802. nu: nu$2,
  803. windows: constant(windows),
  804. ios: constant(ios),
  805. android: constant(android),
  806. linux: constant(linux),
  807. osx: constant(osx),
  808. solaris: constant(solaris),
  809. freebsd: constant(freebsd)
  810. };
  811. var DeviceType = function (os, browser, userAgent) {
  812. var isiPad = os.isiOS() && /ipad/i.test(userAgent) === true;
  813. var isiPhone = os.isiOS() && !isiPad;
  814. var isAndroid3 = os.isAndroid() && os.version.major === 3;
  815. var isAndroid4 = os.isAndroid() && os.version.major === 4;
  816. var isTablet = isiPad || isAndroid3 || isAndroid4 && /mobile/i.test(userAgent) === true;
  817. var isTouch = os.isiOS() || os.isAndroid();
  818. var isPhone = isTouch && !isTablet;
  819. var iOSwebview = browser.isSafari() && os.isiOS() && /safari/i.test(userAgent) === false;
  820. return {
  821. isiPad: constant(isiPad),
  822. isiPhone: constant(isiPhone),
  823. isTablet: constant(isTablet),
  824. isPhone: constant(isPhone),
  825. isTouch: constant(isTouch),
  826. isAndroid: os.isAndroid,
  827. isiOS: os.isiOS,
  828. isWebView: constant(iOSwebview)
  829. };
  830. };
  831. var detect$1 = function (candidates, userAgent) {
  832. var agent = String(userAgent).toLowerCase();
  833. return find(candidates, function (candidate) {
  834. return candidate.search(agent);
  835. });
  836. };
  837. var detectBrowser = function (browsers, userAgent) {
  838. return detect$1(browsers, userAgent).map(function (browser) {
  839. var version = Version.detect(browser.versionRegexes, userAgent);
  840. return {
  841. current: browser.name,
  842. version: version
  843. };
  844. });
  845. };
  846. var detectOs = function (oses, userAgent) {
  847. return detect$1(oses, userAgent).map(function (os) {
  848. var version = Version.detect(os.versionRegexes, userAgent);
  849. return {
  850. current: os.name,
  851. version: version
  852. };
  853. });
  854. };
  855. var UaString = {
  856. detectBrowser: detectBrowser,
  857. detectOs: detectOs
  858. };
  859. var contains = function (str, substr) {
  860. return str.indexOf(substr) !== -1;
  861. };
  862. var normalVersionRegex = /.*?version\/\ ?([0-9]+)\.([0-9]+).*/;
  863. var checkContains = function (target) {
  864. return function (uastring) {
  865. return contains(uastring, target);
  866. };
  867. };
  868. var browsers = [
  869. {
  870. name: 'Edge',
  871. versionRegexes: [/.*?edge\/ ?([0-9]+)\.([0-9]+)$/],
  872. search: function (uastring) {
  873. return contains(uastring, 'edge/') && contains(uastring, 'chrome') && contains(uastring, 'safari') && contains(uastring, 'applewebkit');
  874. }
  875. },
  876. {
  877. name: 'Chrome',
  878. versionRegexes: [
  879. /.*?chrome\/([0-9]+)\.([0-9]+).*/,
  880. normalVersionRegex
  881. ],
  882. search: function (uastring) {
  883. return contains(uastring, 'chrome') && !contains(uastring, 'chromeframe');
  884. }
  885. },
  886. {
  887. name: 'IE',
  888. versionRegexes: [
  889. /.*?msie\ ?([0-9]+)\.([0-9]+).*/,
  890. /.*?rv:([0-9]+)\.([0-9]+).*/
  891. ],
  892. search: function (uastring) {
  893. return contains(uastring, 'msie') || contains(uastring, 'trident');
  894. }
  895. },
  896. {
  897. name: 'Opera',
  898. versionRegexes: [
  899. normalVersionRegex,
  900. /.*?opera\/([0-9]+)\.([0-9]+).*/
  901. ],
  902. search: checkContains('opera')
  903. },
  904. {
  905. name: 'Firefox',
  906. versionRegexes: [/.*?firefox\/\ ?([0-9]+)\.([0-9]+).*/],
  907. search: checkContains('firefox')
  908. },
  909. {
  910. name: 'Safari',
  911. versionRegexes: [
  912. normalVersionRegex,
  913. /.*?cpu os ([0-9]+)_([0-9]+).*/
  914. ],
  915. search: function (uastring) {
  916. return (contains(uastring, 'safari') || contains(uastring, 'mobile/')) && contains(uastring, 'applewebkit');
  917. }
  918. }
  919. ];
  920. var oses = [
  921. {
  922. name: 'Windows',
  923. search: checkContains('win'),
  924. versionRegexes: [/.*?windows\ nt\ ?([0-9]+)\.([0-9]+).*/]
  925. },
  926. {
  927. name: 'iOS',
  928. search: function (uastring) {
  929. return contains(uastring, 'iphone') || contains(uastring, 'ipad');
  930. },
  931. versionRegexes: [
  932. /.*?version\/\ ?([0-9]+)\.([0-9]+).*/,
  933. /.*cpu os ([0-9]+)_([0-9]+).*/,
  934. /.*cpu iphone os ([0-9]+)_([0-9]+).*/
  935. ]
  936. },
  937. {
  938. name: 'Android',
  939. search: checkContains('android'),
  940. versionRegexes: [/.*?android\ ?([0-9]+)\.([0-9]+).*/]
  941. },
  942. {
  943. name: 'OSX',
  944. search: checkContains('os x'),
  945. versionRegexes: [/.*?os\ x\ ?([0-9]+)_([0-9]+).*/]
  946. },
  947. {
  948. name: 'Linux',
  949. search: checkContains('linux'),
  950. versionRegexes: []
  951. },
  952. {
  953. name: 'Solaris',
  954. search: checkContains('sunos'),
  955. versionRegexes: []
  956. },
  957. {
  958. name: 'FreeBSD',
  959. search: checkContains('freebsd'),
  960. versionRegexes: []
  961. }
  962. ];
  963. var PlatformInfo = {
  964. browsers: constant(browsers),
  965. oses: constant(oses)
  966. };
  967. var detect$2 = function (userAgent) {
  968. var browsers = PlatformInfo.browsers();
  969. var oses = PlatformInfo.oses();
  970. var browser = UaString.detectBrowser(browsers, userAgent).fold(Browser.unknown, Browser.nu);
  971. var os = UaString.detectOs(oses, userAgent).fold(OperatingSystem.unknown, OperatingSystem.nu);
  972. var deviceType = DeviceType(os, browser, userAgent);
  973. return {
  974. browser: browser,
  975. os: os,
  976. deviceType: deviceType
  977. };
  978. };
  979. var PlatformDetection = { detect: detect$2 };
  980. var detect$3 = cached(function () {
  981. var userAgent = domGlobals.navigator.userAgent;
  982. return PlatformDetection.detect(userAgent);
  983. });
  984. var PlatformDetection$1 = { detect: detect$3 };
  985. var ATTRIBUTE = domGlobals.Node.ATTRIBUTE_NODE;
  986. var CDATA_SECTION = domGlobals.Node.CDATA_SECTION_NODE;
  987. var COMMENT = domGlobals.Node.COMMENT_NODE;
  988. var DOCUMENT = domGlobals.Node.DOCUMENT_NODE;
  989. var DOCUMENT_TYPE = domGlobals.Node.DOCUMENT_TYPE_NODE;
  990. var DOCUMENT_FRAGMENT = domGlobals.Node.DOCUMENT_FRAGMENT_NODE;
  991. var ELEMENT = domGlobals.Node.ELEMENT_NODE;
  992. var TEXT = domGlobals.Node.TEXT_NODE;
  993. var PROCESSING_INSTRUCTION = domGlobals.Node.PROCESSING_INSTRUCTION_NODE;
  994. var ENTITY_REFERENCE = domGlobals.Node.ENTITY_REFERENCE_NODE;
  995. var ENTITY = domGlobals.Node.ENTITY_NODE;
  996. var NOTATION = domGlobals.Node.NOTATION_NODE;
  997. var ELEMENT$1 = ELEMENT;
  998. var is = function (element, selector) {
  999. var dom = element.dom();
  1000. if (dom.nodeType !== ELEMENT$1) {
  1001. return false;
  1002. } else {
  1003. var elem = dom;
  1004. if (elem.matches !== undefined) {
  1005. return elem.matches(selector);
  1006. } else if (elem.msMatchesSelector !== undefined) {
  1007. return elem.msMatchesSelector(selector);
  1008. } else if (elem.webkitMatchesSelector !== undefined) {
  1009. return elem.webkitMatchesSelector(selector);
  1010. } else if (elem.mozMatchesSelector !== undefined) {
  1011. return elem.mozMatchesSelector(selector);
  1012. } else {
  1013. throw new Error('Browser lacks native selectors');
  1014. }
  1015. }
  1016. };
  1017. var eq = function (e1, e2) {
  1018. return e1.dom() === e2.dom();
  1019. };
  1020. var regularContains = function (e1, e2) {
  1021. var d1 = e1.dom();
  1022. var d2 = e2.dom();
  1023. return d1 === d2 ? false : d1.contains(d2);
  1024. };
  1025. var ieContains = function (e1, e2) {
  1026. return Node.documentPositionContainedBy(e1.dom(), e2.dom());
  1027. };
  1028. var browser = PlatformDetection$1.detect().browser;
  1029. var contains$1 = browser.isIE() ? ieContains : regularContains;
  1030. var is$1 = is;
  1031. var parent = function (element) {
  1032. return Option.from(element.dom().parentNode).map(Element.fromDom);
  1033. };
  1034. var children = function (element) {
  1035. return map(element.dom().childNodes, Element.fromDom);
  1036. };
  1037. var child = function (element, index) {
  1038. var cs = element.dom().childNodes;
  1039. return Option.from(cs[index]).map(Element.fromDom);
  1040. };
  1041. var firstChild = function (element) {
  1042. return child(element, 0);
  1043. };
  1044. var lastChild = function (element) {
  1045. return child(element, element.dom().childNodes.length - 1);
  1046. };
  1047. var spot = Immutable('element', 'offset');
  1048. var before = function (marker, element) {
  1049. var parent$1 = parent(marker);
  1050. parent$1.each(function (v) {
  1051. v.dom().insertBefore(element.dom(), marker.dom());
  1052. });
  1053. };
  1054. var append = function (parent, element) {
  1055. parent.dom().appendChild(element.dom());
  1056. };
  1057. var before$1 = function (marker, elements) {
  1058. each(elements, function (x) {
  1059. before(marker, x);
  1060. });
  1061. };
  1062. var append$1 = function (parent, elements) {
  1063. each(elements, function (x) {
  1064. append(parent, x);
  1065. });
  1066. };
  1067. var remove = function (element) {
  1068. var dom = element.dom();
  1069. if (dom.parentNode !== null) {
  1070. dom.parentNode.removeChild(dom);
  1071. }
  1072. };
  1073. var name = function (element) {
  1074. var r = element.dom().nodeName;
  1075. return r.toLowerCase();
  1076. };
  1077. var type = function (element) {
  1078. return element.dom().nodeType;
  1079. };
  1080. var isType$1 = function (t) {
  1081. return function (element) {
  1082. return type(element) === t;
  1083. };
  1084. };
  1085. var isElement = isType$1(ELEMENT);
  1086. var rawSet = function (dom, key, value) {
  1087. if (isString(value) || isBoolean(value) || isNumber(value)) {
  1088. dom.setAttribute(key, value + '');
  1089. } else {
  1090. domGlobals.console.error('Invalid call to Attr.set. Key ', key, ':: Value ', value, ':: Element ', dom);
  1091. throw new Error('Attribute value was not simple');
  1092. }
  1093. };
  1094. var setAll = function (element, attrs) {
  1095. var dom = element.dom();
  1096. each$1(attrs, function (v, k) {
  1097. rawSet(dom, k, v);
  1098. });
  1099. };
  1100. var clone = function (element) {
  1101. return foldl(element.dom().attributes, function (acc, attr) {
  1102. acc[attr.name] = attr.value;
  1103. return acc;
  1104. }, {});
  1105. };
  1106. var isSupported = function (dom) {
  1107. return dom.style !== undefined && isFunction(dom.style.getPropertyValue);
  1108. };
  1109. var internalSet = function (dom, property, value) {
  1110. if (!isString(value)) {
  1111. domGlobals.console.error('Invalid call to CSS.set. Property ', property, ':: Value ', value, ':: Element ', dom);
  1112. throw new Error('CSS value must be a string: ' + value);
  1113. }
  1114. if (isSupported(dom)) {
  1115. dom.style.setProperty(property, value);
  1116. }
  1117. };
  1118. var set = function (element, property, value) {
  1119. var dom = element.dom();
  1120. internalSet(dom, property, value);
  1121. };
  1122. var clone$1 = function (original, isDeep) {
  1123. return Element.fromDom(original.dom().cloneNode(isDeep));
  1124. };
  1125. var deep = function (original) {
  1126. return clone$1(original, true);
  1127. };
  1128. var shallowAs = function (original, tag) {
  1129. var nu = Element.fromTag(tag);
  1130. var attributes = clone(original);
  1131. setAll(nu, attributes);
  1132. return nu;
  1133. };
  1134. var mutate = function (original, tag) {
  1135. var nu = shallowAs(original, tag);
  1136. before(original, nu);
  1137. var children$1 = children(original);
  1138. append$1(nu, children$1);
  1139. remove(original);
  1140. return nu;
  1141. };
  1142. var joinSegment = function (parent, child) {
  1143. append(parent.item, child.list);
  1144. };
  1145. var joinSegments = function (segments) {
  1146. for (var i = 1; i < segments.length; i++) {
  1147. joinSegment(segments[i - 1], segments[i]);
  1148. }
  1149. };
  1150. var appendSegments = function (head$1, tail) {
  1151. liftN([
  1152. last(head$1),
  1153. head(tail)
  1154. ], joinSegment);
  1155. };
  1156. var createSegment = function (scope, listType) {
  1157. var segment = {
  1158. list: Element.fromTag(listType, scope),
  1159. item: Element.fromTag('li', scope)
  1160. };
  1161. append(segment.list, segment.item);
  1162. return segment;
  1163. };
  1164. var createSegments = function (scope, entry, size) {
  1165. var segments = [];
  1166. for (var i = 0; i < size; i++) {
  1167. segments.push(createSegment(scope, entry.listType));
  1168. }
  1169. return segments;
  1170. };
  1171. var populateSegments = function (segments, entry) {
  1172. for (var i = 0; i < segments.length - 1; i++) {
  1173. set(segments[i].item, 'list-style-type', 'none');
  1174. }
  1175. last(segments).each(function (segment) {
  1176. setAll(segment.list, entry.listAttributes);
  1177. setAll(segment.item, entry.itemAttributes);
  1178. append$1(segment.item, entry.content);
  1179. });
  1180. };
  1181. var normalizeSegment = function (segment, entry) {
  1182. if (name(segment.list) !== entry.listType) {
  1183. segment.list = mutate(segment.list, entry.listType);
  1184. }
  1185. setAll(segment.list, entry.listAttributes);
  1186. };
  1187. var createItem = function (scope, attr, content) {
  1188. var item = Element.fromTag('li', scope);
  1189. setAll(item, attr);
  1190. append$1(item, content);
  1191. return item;
  1192. };
  1193. var appendItem = function (segment, item) {
  1194. append(segment.list, item);
  1195. segment.item = item;
  1196. };
  1197. var writeShallow = function (scope, cast, entry) {
  1198. var newCast = cast.slice(0, entry.depth);
  1199. last(newCast).each(function (segment) {
  1200. var item = createItem(scope, entry.itemAttributes, entry.content);
  1201. appendItem(segment, item);
  1202. normalizeSegment(segment, entry);
  1203. });
  1204. return newCast;
  1205. };
  1206. var writeDeep = function (scope, cast, entry) {
  1207. var segments = createSegments(scope, entry, entry.depth - cast.length);
  1208. joinSegments(segments);
  1209. populateSegments(segments, entry);
  1210. appendSegments(cast, segments);
  1211. return cast.concat(segments);
  1212. };
  1213. var composeList = function (scope, entries) {
  1214. var cast = foldl(entries, function (cast, entry) {
  1215. return entry.depth > cast.length ? writeDeep(scope, cast, entry) : writeShallow(scope, cast, entry);
  1216. }, []);
  1217. return head(cast).map(function (segment) {
  1218. return segment.list;
  1219. });
  1220. };
  1221. var isList$1 = function (el) {
  1222. return is$1(el, 'OL,UL');
  1223. };
  1224. var hasFirstChildList = function (el) {
  1225. return firstChild(el).map(isList$1).getOr(false);
  1226. };
  1227. var hasLastChildList = function (el) {
  1228. return lastChild(el).map(isList$1).getOr(false);
  1229. };
  1230. var isIndented = function (entry) {
  1231. return entry.depth > 0;
  1232. };
  1233. var isSelected = function (entry) {
  1234. return entry.isSelected;
  1235. };
  1236. var cloneItemContent = function (li) {
  1237. var children$1 = children(li);
  1238. var content = hasLastChildList(li) ? children$1.slice(0, -1) : children$1;
  1239. return map(content, deep);
  1240. };
  1241. var createEntry = function (li, depth, isSelected) {
  1242. return parent(li).filter(isElement).map(function (list) {
  1243. return {
  1244. depth: depth,
  1245. isSelected: isSelected,
  1246. content: cloneItemContent(li),
  1247. itemAttributes: clone(li),
  1248. listAttributes: clone(list),
  1249. listType: name(list)
  1250. };
  1251. });
  1252. };
  1253. var indentEntry = function (indentation, entry) {
  1254. switch (indentation) {
  1255. case 'Indent':
  1256. entry.depth++;
  1257. break;
  1258. case 'Outdent':
  1259. entry.depth--;
  1260. break;
  1261. case 'Flatten':
  1262. entry.depth = 0;
  1263. }
  1264. };
  1265. var hasOwnProperty = Object.prototype.hasOwnProperty;
  1266. var shallow = function (old, nu) {
  1267. return nu;
  1268. };
  1269. var baseMerge = function (merger) {
  1270. return function () {
  1271. var objects = new Array(arguments.length);
  1272. for (var i = 0; i < objects.length; i++) {
  1273. objects[i] = arguments[i];
  1274. }
  1275. if (objects.length === 0) {
  1276. throw new Error('Can\'t merge zero objects');
  1277. }
  1278. var ret = {};
  1279. for (var j = 0; j < objects.length; j++) {
  1280. var curObject = objects[j];
  1281. for (var key in curObject) {
  1282. if (hasOwnProperty.call(curObject, key)) {
  1283. ret[key] = merger(ret[key], curObject[key]);
  1284. }
  1285. }
  1286. }
  1287. return ret;
  1288. };
  1289. };
  1290. var merge = baseMerge(shallow);
  1291. var cloneListProperties = function (target, source) {
  1292. target.listType = source.listType;
  1293. target.listAttributes = merge({}, source.listAttributes);
  1294. };
  1295. var previousSiblingEntry = function (entries, start) {
  1296. var depth = entries[start].depth;
  1297. for (var i = start - 1; i >= 0; i--) {
  1298. if (entries[i].depth === depth) {
  1299. return Option.some(entries[i]);
  1300. }
  1301. if (entries[i].depth < depth) {
  1302. break;
  1303. }
  1304. }
  1305. return Option.none();
  1306. };
  1307. var normalizeEntries = function (entries) {
  1308. each(entries, function (entry, i) {
  1309. previousSiblingEntry(entries, i).each(function (matchingEntry) {
  1310. cloneListProperties(entry, matchingEntry);
  1311. });
  1312. });
  1313. };
  1314. var Cell = function (initial) {
  1315. var value = initial;
  1316. var get = function () {
  1317. return value;
  1318. };
  1319. var set = function (v) {
  1320. value = v;
  1321. };
  1322. var clone = function () {
  1323. return Cell(get());
  1324. };
  1325. return {
  1326. get: get,
  1327. set: set,
  1328. clone: clone
  1329. };
  1330. };
  1331. var parseItem = function (depth, itemSelection, selectionState, item) {
  1332. return firstChild(item).filter(isList$1).fold(function () {
  1333. itemSelection.each(function (selection) {
  1334. if (eq(selection.start, item)) {
  1335. selectionState.set(true);
  1336. }
  1337. });
  1338. var currentItemEntry = createEntry(item, depth, selectionState.get());
  1339. itemSelection.each(function (selection) {
  1340. if (eq(selection.end, item)) {
  1341. selectionState.set(false);
  1342. }
  1343. });
  1344. var childListEntries = lastChild(item).filter(isList$1).map(function (list) {
  1345. return parseList(depth, itemSelection, selectionState, list);
  1346. }).getOr([]);
  1347. return currentItemEntry.toArray().concat(childListEntries);
  1348. }, function (list) {
  1349. return parseList(depth, itemSelection, selectionState, list);
  1350. });
  1351. };
  1352. var parseList = function (depth, itemSelection, selectionState, list) {
  1353. return bind(children(list), function (element) {
  1354. var parser = isList$1(element) ? parseList : parseItem;
  1355. var newDepth = depth + 1;
  1356. return parser(newDepth, itemSelection, selectionState, element);
  1357. });
  1358. };
  1359. var parseLists = function (lists, itemSelection) {
  1360. var selectionState = Cell(false);
  1361. var initialDepth = 0;
  1362. return map(lists, function (list) {
  1363. return {
  1364. sourceList: list,
  1365. entries: parseList(initialDepth, itemSelection, selectionState, list)
  1366. };
  1367. });
  1368. };
  1369. var global$8 = tinymce.util.Tools.resolve('tinymce.Env');
  1370. var createTextBlock = function (editor, contentNode) {
  1371. var dom = editor.dom;
  1372. var blockElements = editor.schema.getBlockElements();
  1373. var fragment = dom.createFragment();
  1374. var node, textBlock, blockName, hasContentNode;
  1375. if (editor.settings.forced_root_block) {
  1376. blockName = editor.settings.forced_root_block;
  1377. }
  1378. if (blockName) {
  1379. textBlock = dom.create(blockName);
  1380. if (textBlock.tagName === editor.settings.forced_root_block) {
  1381. dom.setAttribs(textBlock, editor.settings.forced_root_block_attrs);
  1382. }
  1383. if (!NodeType.isBlock(contentNode.firstChild, blockElements)) {
  1384. fragment.appendChild(textBlock);
  1385. }
  1386. }
  1387. if (contentNode) {
  1388. while (node = contentNode.firstChild) {
  1389. var nodeName = node.nodeName;
  1390. if (!hasContentNode && (nodeName !== 'SPAN' || node.getAttribute('data-mce-type') !== 'bookmark')) {
  1391. hasContentNode = true;
  1392. }
  1393. if (NodeType.isBlock(node, blockElements)) {
  1394. fragment.appendChild(node);
  1395. textBlock = null;
  1396. } else {
  1397. if (blockName) {
  1398. if (!textBlock) {
  1399. textBlock = dom.create(blockName);
  1400. fragment.appendChild(textBlock);
  1401. }
  1402. textBlock.appendChild(node);
  1403. } else {
  1404. fragment.appendChild(node);
  1405. }
  1406. }
  1407. }
  1408. }
  1409. if (!editor.settings.forced_root_block) {
  1410. fragment.appendChild(dom.create('br'));
  1411. } else {
  1412. if (!hasContentNode && (!global$8.ie || global$8.ie > 10)) {
  1413. textBlock.appendChild(dom.create('br', { 'data-mce-bogus': '1' }));
  1414. }
  1415. }
  1416. return fragment;
  1417. };
  1418. var outdentedComposer = function (editor, entries) {
  1419. return map(entries, function (entry) {
  1420. var content = fromElements(entry.content);
  1421. return Element.fromDom(createTextBlock(editor, content.dom()));
  1422. });
  1423. };
  1424. var indentedComposer = function (editor, entries) {
  1425. normalizeEntries(entries);
  1426. return composeList(editor.contentDocument, entries).toArray();
  1427. };
  1428. var composeEntries = function (editor, entries) {
  1429. return bind(groupBy(entries, isIndented), function (entries) {
  1430. var groupIsIndented = head(entries).map(isIndented).getOr(false);
  1431. return groupIsIndented ? indentedComposer(editor, entries) : outdentedComposer(editor, entries);
  1432. });
  1433. };
  1434. var indentSelectedEntries = function (entries, indentation) {
  1435. each(filter(entries, isSelected), function (entry) {
  1436. return indentEntry(indentation, entry);
  1437. });
  1438. };
  1439. var getItemSelection = function (editor) {
  1440. var selectedListItems = map(Selection.getSelectedListItems(editor), Element.fromDom);
  1441. return liftN([
  1442. find(selectedListItems, not(hasFirstChildList)),
  1443. find(reverse(selectedListItems), not(hasFirstChildList))
  1444. ], function (start, end) {
  1445. return {
  1446. start: start,
  1447. end: end
  1448. };
  1449. });
  1450. };
  1451. var listsIndentation = function (editor, lists, indentation) {
  1452. var entrySets = parseLists(lists, getItemSelection(editor));
  1453. each(entrySets, function (entrySet) {
  1454. indentSelectedEntries(entrySet.entries, indentation);
  1455. before$1(entrySet.sourceList, composeEntries(editor, entrySet.entries));
  1456. remove(entrySet.sourceList);
  1457. });
  1458. };
  1459. var DOM$1 = global$6.DOM;
  1460. var splitList = function (editor, ul, li) {
  1461. var tmpRng, fragment, bookmarks, node, newBlock;
  1462. var removeAndKeepBookmarks = function (targetNode) {
  1463. global$5.each(bookmarks, function (node) {
  1464. targetNode.parentNode.insertBefore(node, li.parentNode);
  1465. });
  1466. DOM$1.remove(targetNode);
  1467. };
  1468. bookmarks = DOM$1.select('span[data-mce-type="bookmark"]', ul);
  1469. newBlock = createTextBlock(editor, li);
  1470. tmpRng = DOM$1.createRng();
  1471. tmpRng.setStartAfter(li);
  1472. tmpRng.setEndAfter(ul);
  1473. fragment = tmpRng.extractContents();
  1474. for (node = fragment.firstChild; node; node = node.firstChild) {
  1475. if (node.nodeName === 'LI' && editor.dom.isEmpty(node)) {
  1476. DOM$1.remove(node);
  1477. break;
  1478. }
  1479. }
  1480. if (!editor.dom.isEmpty(fragment)) {
  1481. DOM$1.insertAfter(fragment, ul);
  1482. }
  1483. DOM$1.insertAfter(newBlock, ul);
  1484. if (NodeType.isEmpty(editor.dom, li.parentNode)) {
  1485. removeAndKeepBookmarks(li.parentNode);
  1486. }
  1487. DOM$1.remove(li);
  1488. if (NodeType.isEmpty(editor.dom, ul)) {
  1489. DOM$1.remove(ul);
  1490. }
  1491. };
  1492. var SplitList = { splitList: splitList };
  1493. var outdentDlItem = function (editor, item) {
  1494. if (is$1(item, 'dd')) {
  1495. mutate(item, 'dt');
  1496. } else if (is$1(item, 'dt')) {
  1497. parent(item).each(function (dl) {
  1498. return SplitList.splitList(editor, dl.dom(), item.dom());
  1499. });
  1500. }
  1501. };
  1502. var indentDlItem = function (item) {
  1503. if (is$1(item, 'dt')) {
  1504. mutate(item, 'dd');
  1505. }
  1506. };
  1507. var dlIndentation = function (editor, indentation, dlItems) {
  1508. if (indentation === 'Indent') {
  1509. each(dlItems, indentDlItem);
  1510. } else {
  1511. each(dlItems, function (item) {
  1512. return outdentDlItem(editor, item);
  1513. });
  1514. }
  1515. };
  1516. var selectionIndentation = function (editor, indentation) {
  1517. var lists = map(Selection.getSelectedListRoots(editor), Element.fromDom);
  1518. var dlItems = map(Selection.getSelectedDlItems(editor), Element.fromDom);
  1519. var isHandled = false;
  1520. if (lists.length || dlItems.length) {
  1521. var bookmark = editor.selection.getBookmark();
  1522. listsIndentation(editor, lists, indentation);
  1523. dlIndentation(editor, indentation, dlItems);
  1524. editor.selection.moveToBookmark(bookmark);
  1525. editor.selection.setRng(Range.normalizeRange(editor.selection.getRng()));
  1526. editor.nodeChanged();
  1527. isHandled = true;
  1528. }
  1529. return isHandled;
  1530. };
  1531. var indentListSelection = function (editor) {
  1532. return selectionIndentation(editor, 'Indent');
  1533. };
  1534. var outdentListSelection = function (editor) {
  1535. return selectionIndentation(editor, 'Outdent');
  1536. };
  1537. var flattenListSelection = function (editor) {
  1538. return selectionIndentation(editor, 'Flatten');
  1539. };
  1540. var updateListStyle = function (dom, el, detail) {
  1541. var type = detail['list-style-type'] ? detail['list-style-type'] : null;
  1542. dom.setStyle(el, 'list-style-type', type);
  1543. };
  1544. var setAttribs = function (elm, attrs) {
  1545. global$5.each(attrs, function (value, key) {
  1546. elm.setAttribute(key, value);
  1547. });
  1548. };
  1549. var updateListAttrs = function (dom, el, detail) {
  1550. setAttribs(el, detail['list-attributes']);
  1551. global$5.each(dom.select('li', el), function (li) {
  1552. setAttribs(li, detail['list-item-attributes']);
  1553. });
  1554. };
  1555. var updateListWithDetails = function (dom, el, detail) {
  1556. updateListStyle(dom, el, detail);
  1557. updateListAttrs(dom, el, detail);
  1558. };
  1559. var removeStyles = function (dom, element, styles) {
  1560. global$5.each(styles, function (style) {
  1561. var _a;
  1562. return dom.setStyle(element, (_a = {}, _a[style] = '', _a));
  1563. });
  1564. };
  1565. var getEndPointNode = function (editor, rng, start, root) {
  1566. var container, offset;
  1567. container = rng[start ? 'startContainer' : 'endContainer'];
  1568. offset = rng[start ? 'startOffset' : 'endOffset'];
  1569. if (container.nodeType === 1) {
  1570. container = container.childNodes[Math.min(offset, container.childNodes.length - 1)] || container;
  1571. }
  1572. if (!start && NodeType.isBr(container.nextSibling)) {
  1573. container = container.nextSibling;
  1574. }
  1575. while (container.parentNode !== root) {
  1576. if (NodeType.isTextBlock(editor, container)) {
  1577. return container;
  1578. }
  1579. if (/^(TD|TH)$/.test(container.parentNode.nodeName)) {
  1580. return container;
  1581. }
  1582. container = container.parentNode;
  1583. }
  1584. return container;
  1585. };
  1586. var getSelectedTextBlocks = function (editor, rng, root) {
  1587. var textBlocks = [], dom = editor.dom;
  1588. var startNode = getEndPointNode(editor, rng, true, root);
  1589. var endNode = getEndPointNode(editor, rng, false, root);
  1590. var block;
  1591. var siblings = [];
  1592. for (var node = startNode; node; node = node.nextSibling) {
  1593. siblings.push(node);
  1594. if (node === endNode) {
  1595. break;
  1596. }
  1597. }
  1598. global$5.each(siblings, function (node) {
  1599. if (NodeType.isTextBlock(editor, node)) {
  1600. textBlocks.push(node);
  1601. block = null;
  1602. return;
  1603. }
  1604. if (dom.isBlock(node) || NodeType.isBr(node)) {
  1605. if (NodeType.isBr(node)) {
  1606. dom.remove(node);
  1607. }
  1608. block = null;
  1609. return;
  1610. }
  1611. var nextSibling = node.nextSibling;
  1612. if (global$4.isBookmarkNode(node)) {
  1613. if (NodeType.isTextBlock(editor, nextSibling) || !nextSibling && node.parentNode === root) {
  1614. block = null;
  1615. return;
  1616. }
  1617. }
  1618. if (!block) {
  1619. block = dom.create('p');
  1620. node.parentNode.insertBefore(block, node);
  1621. textBlocks.push(block);
  1622. }
  1623. block.appendChild(node);
  1624. });
  1625. return textBlocks;
  1626. };
  1627. var hasCompatibleStyle = function (dom, sib, detail) {
  1628. var sibStyle = dom.getStyle(sib, 'list-style-type');
  1629. var detailStyle = detail ? detail['list-style-type'] : '';
  1630. detailStyle = detailStyle === null ? '' : detailStyle;
  1631. return sibStyle === detailStyle;
  1632. };
  1633. var applyList = function (editor, listName, detail) {
  1634. if (detail === void 0) {
  1635. detail = {};
  1636. }
  1637. var rng = editor.selection.getRng(true);
  1638. var bookmark;
  1639. var listItemName = 'LI';
  1640. var root = Selection.getClosestListRootElm(editor, editor.selection.getStart(true));
  1641. var dom = editor.dom;
  1642. if (dom.getContentEditable(editor.selection.getNode()) === 'false') {
  1643. return;
  1644. }
  1645. listName = listName.toUpperCase();
  1646. if (listName === 'DL') {
  1647. listItemName = 'DT';
  1648. }
  1649. bookmark = Bookmark.createBookmark(rng);
  1650. global$5.each(getSelectedTextBlocks(editor, rng, root), function (block) {
  1651. var listBlock, sibling;
  1652. sibling = block.previousSibling;
  1653. if (sibling && NodeType.isListNode(sibling) && sibling.nodeName === listName && hasCompatibleStyle(dom, sibling, detail)) {
  1654. listBlock = sibling;
  1655. block = dom.rename(block, listItemName);
  1656. sibling.appendChild(block);
  1657. } else {
  1658. listBlock = dom.create(listName);
  1659. block.parentNode.insertBefore(listBlock, block);
  1660. listBlock.appendChild(block);
  1661. block = dom.rename(block, listItemName);
  1662. }
  1663. removeStyles(dom, block, [
  1664. 'margin',
  1665. 'margin-right',
  1666. 'margin-bottom',
  1667. 'margin-left',
  1668. 'margin-top',
  1669. 'padding',
  1670. 'padding-right',
  1671. 'padding-bottom',
  1672. 'padding-left',
  1673. 'padding-top'
  1674. ]);
  1675. updateListWithDetails(dom, listBlock, detail);
  1676. mergeWithAdjacentLists(editor.dom, listBlock);
  1677. });
  1678. editor.selection.setRng(Bookmark.resolveBookmark(bookmark));
  1679. };
  1680. var isValidLists = function (list1, list2) {
  1681. return list1 && list2 && NodeType.isListNode(list1) && list1.nodeName === list2.nodeName;
  1682. };
  1683. var hasSameListStyle = function (dom, list1, list2) {
  1684. var targetStyle = dom.getStyle(list1, 'list-style-type', true);
  1685. var style = dom.getStyle(list2, 'list-style-type', true);
  1686. return targetStyle === style;
  1687. };
  1688. var hasSameClasses = function (elm1, elm2) {
  1689. return elm1.className === elm2.className;
  1690. };
  1691. var shouldMerge = function (dom, list1, list2) {
  1692. return isValidLists(list1, list2) && hasSameListStyle(dom, list1, list2) && hasSameClasses(list1, list2);
  1693. };
  1694. var mergeWithAdjacentLists = function (dom, listBlock) {
  1695. var sibling, node;
  1696. sibling = listBlock.nextSibling;
  1697. if (shouldMerge(dom, listBlock, sibling)) {
  1698. while (node = sibling.firstChild) {
  1699. listBlock.appendChild(node);
  1700. }
  1701. dom.remove(sibling);
  1702. }
  1703. sibling = listBlock.previousSibling;
  1704. if (shouldMerge(dom, listBlock, sibling)) {
  1705. while (node = sibling.lastChild) {
  1706. listBlock.insertBefore(node, listBlock.firstChild);
  1707. }
  1708. dom.remove(sibling);
  1709. }
  1710. };
  1711. var updateList = function (dom, list, listName, detail) {
  1712. if (list.nodeName !== listName) {
  1713. var newList = dom.rename(list, listName);
  1714. updateListWithDetails(dom, newList, detail);
  1715. } else {
  1716. updateListWithDetails(dom, list, detail);
  1717. }
  1718. };
  1719. var toggleMultipleLists = function (editor, parentList, lists, listName, detail) {
  1720. if (parentList.nodeName === listName && !hasListStyleDetail(detail)) {
  1721. flattenListSelection(editor);
  1722. } else {
  1723. var bookmark = Bookmark.createBookmark(editor.selection.getRng(true));
  1724. global$5.each([parentList].concat(lists), function (elm) {
  1725. updateList(editor.dom, elm, listName, detail);
  1726. });
  1727. editor.selection.setRng(Bookmark.resolveBookmark(bookmark));
  1728. }
  1729. };
  1730. var hasListStyleDetail = function (detail) {
  1731. return 'list-style-type' in detail;
  1732. };
  1733. var toggleSingleList = function (editor, parentList, listName, detail) {
  1734. if (parentList === editor.getBody()) {
  1735. return;
  1736. }
  1737. if (parentList) {
  1738. if (parentList.nodeName === listName && !hasListStyleDetail(detail)) {
  1739. flattenListSelection(editor);
  1740. } else {
  1741. var bookmark = Bookmark.createBookmark(editor.selection.getRng(true));
  1742. updateListWithDetails(editor.dom, parentList, detail);
  1743. mergeWithAdjacentLists(editor.dom, editor.dom.rename(parentList, listName));
  1744. editor.selection.setRng(Bookmark.resolveBookmark(bookmark));
  1745. }
  1746. } else {
  1747. applyList(editor, listName, detail);
  1748. }
  1749. };
  1750. var toggleList = function (editor, listName, detail) {
  1751. var parentList = Selection.getParentList(editor);
  1752. var selectedSubLists = Selection.getSelectedSubLists(editor);
  1753. detail = detail ? detail : {};
  1754. if (parentList && selectedSubLists.length > 0) {
  1755. toggleMultipleLists(editor, parentList, selectedSubLists, listName, detail);
  1756. } else {
  1757. toggleSingleList(editor, parentList, listName, detail);
  1758. }
  1759. };
  1760. var ToggleList = {
  1761. toggleList: toggleList,
  1762. mergeWithAdjacentLists: mergeWithAdjacentLists
  1763. };
  1764. var DOM$2 = global$6.DOM;
  1765. var normalizeList = function (dom, ul) {
  1766. var sibling;
  1767. var parentNode = ul.parentNode;
  1768. if (parentNode.nodeName === 'LI' && parentNode.firstChild === ul) {
  1769. sibling = parentNode.previousSibling;
  1770. if (sibling && sibling.nodeName === 'LI') {
  1771. sibling.appendChild(ul);
  1772. if (NodeType.isEmpty(dom, parentNode)) {
  1773. DOM$2.remove(parentNode);
  1774. }
  1775. } else {
  1776. DOM$2.setStyle(parentNode, 'listStyleType', 'none');
  1777. }
  1778. }
  1779. if (NodeType.isListNode(parentNode)) {
  1780. sibling = parentNode.previousSibling;
  1781. if (sibling && sibling.nodeName === 'LI') {
  1782. sibling.appendChild(ul);
  1783. }
  1784. }
  1785. };
  1786. var normalizeLists = function (dom, element) {
  1787. global$5.each(global$5.grep(dom.select('ol,ul', element)), function (ul) {
  1788. normalizeList(dom, ul);
  1789. });
  1790. };
  1791. var NormalizeLists = {
  1792. normalizeList: normalizeList,
  1793. normalizeLists: normalizeLists
  1794. };
  1795. var findNextCaretContainer = function (editor, rng, isForward, root) {
  1796. var node = rng.startContainer;
  1797. var offset = rng.startOffset;
  1798. var nonEmptyBlocks, walker;
  1799. if (node.nodeType === 3 && (isForward ? offset < node.data.length : offset > 0)) {
  1800. return node;
  1801. }
  1802. nonEmptyBlocks = editor.schema.getNonEmptyElements();
  1803. if (node.nodeType === 1) {
  1804. node = global$1.getNode(node, offset);
  1805. }
  1806. walker = new global$2(node, root);
  1807. if (isForward) {
  1808. if (NodeType.isBogusBr(editor.dom, node)) {
  1809. walker.next();
  1810. }
  1811. }
  1812. while (node = walker[isForward ? 'next' : 'prev2']()) {
  1813. if (node.nodeName === 'LI' && !node.hasChildNodes()) {
  1814. return node;
  1815. }
  1816. if (nonEmptyBlocks[node.nodeName]) {
  1817. return node;
  1818. }
  1819. if (node.nodeType === 3 && node.data.length > 0) {
  1820. return node;
  1821. }
  1822. }
  1823. };
  1824. var hasOnlyOneBlockChild = function (dom, elm) {
  1825. var childNodes = elm.childNodes;
  1826. return childNodes.length === 1 && !NodeType.isListNode(childNodes[0]) && dom.isBlock(childNodes[0]);
  1827. };
  1828. var unwrapSingleBlockChild = function (dom, elm) {
  1829. if (hasOnlyOneBlockChild(dom, elm)) {
  1830. dom.remove(elm.firstChild, true);
  1831. }
  1832. };
  1833. var moveChildren = function (dom, fromElm, toElm) {
  1834. var node, targetElm;
  1835. targetElm = hasOnlyOneBlockChild(dom, toElm) ? toElm.firstChild : toElm;
  1836. unwrapSingleBlockChild(dom, fromElm);
  1837. if (!NodeType.isEmpty(dom, fromElm, true)) {
  1838. while (node = fromElm.firstChild) {
  1839. targetElm.appendChild(node);
  1840. }
  1841. }
  1842. };
  1843. var mergeLiElements = function (dom, fromElm, toElm) {
  1844. var node, listNode;
  1845. var ul = fromElm.parentNode;
  1846. if (!NodeType.isChildOfBody(dom, fromElm) || !NodeType.isChildOfBody(dom, toElm)) {
  1847. return;
  1848. }
  1849. if (NodeType.isListNode(toElm.lastChild)) {
  1850. listNode = toElm.lastChild;
  1851. }
  1852. if (ul === toElm.lastChild) {
  1853. if (NodeType.isBr(ul.previousSibling)) {
  1854. dom.remove(ul.previousSibling);
  1855. }
  1856. }
  1857. node = toElm.lastChild;
  1858. if (node && NodeType.isBr(node) && fromElm.hasChildNodes()) {
  1859. dom.remove(node);
  1860. }
  1861. if (NodeType.isEmpty(dom, toElm, true)) {
  1862. dom.$(toElm).empty();
  1863. }
  1864. moveChildren(dom, fromElm, toElm);
  1865. if (listNode) {
  1866. toElm.appendChild(listNode);
  1867. }
  1868. var contains = contains$1(Element.fromDom(toElm), Element.fromDom(fromElm));
  1869. var nestedLists = contains ? dom.getParents(fromElm, NodeType.isListNode, toElm) : [];
  1870. dom.remove(fromElm);
  1871. each(nestedLists, function (list) {
  1872. if (NodeType.isEmpty(dom, list) && list !== dom.getRoot()) {
  1873. dom.remove(list);
  1874. }
  1875. });
  1876. };
  1877. var mergeIntoEmptyLi = function (editor, fromLi, toLi) {
  1878. editor.dom.$(toLi).empty();
  1879. mergeLiElements(editor.dom, fromLi, toLi);
  1880. editor.selection.setCursorLocation(toLi);
  1881. };
  1882. var mergeForward = function (editor, rng, fromLi, toLi) {
  1883. var dom = editor.dom;
  1884. if (dom.isEmpty(toLi)) {
  1885. mergeIntoEmptyLi(editor, fromLi, toLi);
  1886. } else {
  1887. var bookmark = Bookmark.createBookmark(rng);
  1888. mergeLiElements(dom, fromLi, toLi);
  1889. editor.selection.setRng(Bookmark.resolveBookmark(bookmark));
  1890. }
  1891. };
  1892. var mergeBackward = function (editor, rng, fromLi, toLi) {
  1893. var bookmark = Bookmark.createBookmark(rng);
  1894. mergeLiElements(editor.dom, fromLi, toLi);
  1895. var resolvedBookmark = Bookmark.resolveBookmark(bookmark);
  1896. editor.selection.setRng(resolvedBookmark);
  1897. };
  1898. var backspaceDeleteFromListToListCaret = function (editor, isForward) {
  1899. var dom = editor.dom, selection = editor.selection;
  1900. var selectionStartElm = selection.getStart();
  1901. var root = Selection.getClosestListRootElm(editor, selectionStartElm);
  1902. var li = dom.getParent(selection.getStart(), 'LI', root);
  1903. var ul, rng, otherLi;
  1904. if (li) {
  1905. ul = li.parentNode;
  1906. if (ul === editor.getBody() && NodeType.isEmpty(dom, ul)) {
  1907. return true;
  1908. }
  1909. rng = Range.normalizeRange(selection.getRng(true));
  1910. otherLi = dom.getParent(findNextCaretContainer(editor, rng, isForward, root), 'LI', root);
  1911. if (otherLi && otherLi !== li) {
  1912. if (isForward) {
  1913. mergeForward(editor, rng, otherLi, li);
  1914. } else {
  1915. mergeBackward(editor, rng, li, otherLi);
  1916. }
  1917. return true;
  1918. } else if (!otherLi) {
  1919. if (!isForward) {
  1920. flattenListSelection(editor);
  1921. return true;
  1922. }
  1923. }
  1924. }
  1925. return false;
  1926. };
  1927. var removeBlock = function (dom, block, root) {
  1928. var parentBlock = dom.getParent(block.parentNode, dom.isBlock, root);
  1929. dom.remove(block);
  1930. if (parentBlock && dom.isEmpty(parentBlock)) {
  1931. dom.remove(parentBlock);
  1932. }
  1933. };
  1934. var backspaceDeleteIntoListCaret = function (editor, isForward) {
  1935. var dom = editor.dom;
  1936. var selectionStartElm = editor.selection.getStart();
  1937. var root = Selection.getClosestListRootElm(editor, selectionStartElm);
  1938. var block = dom.getParent(selectionStartElm, dom.isBlock, root);
  1939. if (block && dom.isEmpty(block)) {
  1940. var rng = Range.normalizeRange(editor.selection.getRng(true));
  1941. var otherLi_1 = dom.getParent(findNextCaretContainer(editor, rng, isForward, root), 'LI', root);
  1942. if (otherLi_1) {
  1943. editor.undoManager.transact(function () {
  1944. removeBlock(dom, block, root);
  1945. ToggleList.mergeWithAdjacentLists(dom, otherLi_1.parentNode);
  1946. editor.selection.select(otherLi_1, true);
  1947. editor.selection.collapse(isForward);
  1948. });
  1949. return true;
  1950. }
  1951. }
  1952. return false;
  1953. };
  1954. var backspaceDeleteCaret = function (editor, isForward) {
  1955. return backspaceDeleteFromListToListCaret(editor, isForward) || backspaceDeleteIntoListCaret(editor, isForward);
  1956. };
  1957. var backspaceDeleteRange = function (editor) {
  1958. var selectionStartElm = editor.selection.getStart();
  1959. var root = Selection.getClosestListRootElm(editor, selectionStartElm);
  1960. var startListParent = editor.dom.getParent(selectionStartElm, 'LI,DT,DD', root);
  1961. if (startListParent || Selection.getSelectedListItems(editor).length > 0) {
  1962. editor.undoManager.transact(function () {
  1963. editor.execCommand('Delete');
  1964. NormalizeLists.normalizeLists(editor.dom, editor.getBody());
  1965. });
  1966. return true;
  1967. }
  1968. return false;
  1969. };
  1970. var backspaceDelete = function (editor, isForward) {
  1971. return editor.selection.isCollapsed() ? backspaceDeleteCaret(editor, isForward) : backspaceDeleteRange(editor);
  1972. };
  1973. var setup = function (editor) {
  1974. editor.on('keydown', function (e) {
  1975. if (e.keyCode === global$3.BACKSPACE) {
  1976. if (backspaceDelete(editor, false)) {
  1977. e.preventDefault();
  1978. }
  1979. } else if (e.keyCode === global$3.DELETE) {
  1980. if (backspaceDelete(editor, true)) {
  1981. e.preventDefault();
  1982. }
  1983. }
  1984. });
  1985. };
  1986. var Delete = {
  1987. setup: setup,
  1988. backspaceDelete: backspaceDelete
  1989. };
  1990. var get = function (editor) {
  1991. return {
  1992. backspaceDelete: function (isForward) {
  1993. Delete.backspaceDelete(editor, isForward);
  1994. }
  1995. };
  1996. };
  1997. var Api = { get: get };
  1998. var queryListCommandState = function (editor, listName) {
  1999. return function () {
  2000. var parentList = editor.dom.getParent(editor.selection.getStart(), 'UL,OL,DL');
  2001. return parentList && parentList.nodeName === listName;
  2002. };
  2003. };
  2004. var register = function (editor) {
  2005. editor.on('BeforeExecCommand', function (e) {
  2006. var cmd = e.command.toLowerCase();
  2007. if (cmd === 'indent') {
  2008. indentListSelection(editor);
  2009. } else if (cmd === 'outdent') {
  2010. outdentListSelection(editor);
  2011. }
  2012. });
  2013. editor.addCommand('InsertUnorderedList', function (ui, detail) {
  2014. ToggleList.toggleList(editor, 'UL', detail);
  2015. });
  2016. editor.addCommand('InsertOrderedList', function (ui, detail) {
  2017. ToggleList.toggleList(editor, 'OL', detail);
  2018. });
  2019. editor.addCommand('InsertDefinitionList', function (ui, detail) {
  2020. ToggleList.toggleList(editor, 'DL', detail);
  2021. });
  2022. editor.addCommand('RemoveList', function () {
  2023. flattenListSelection(editor);
  2024. });
  2025. editor.addQueryStateHandler('InsertUnorderedList', queryListCommandState(editor, 'UL'));
  2026. editor.addQueryStateHandler('InsertOrderedList', queryListCommandState(editor, 'OL'));
  2027. editor.addQueryStateHandler('InsertDefinitionList', queryListCommandState(editor, 'DL'));
  2028. };
  2029. var Commands = { register: register };
  2030. var shouldIndentOnTab = function (editor) {
  2031. return editor.getParam('lists_indent_on_tab', true);
  2032. };
  2033. var Settings = { shouldIndentOnTab: shouldIndentOnTab };
  2034. var setupTabKey = function (editor) {
  2035. editor.on('keydown', function (e) {
  2036. if (e.keyCode !== global$3.TAB || global$3.metaKeyPressed(e)) {
  2037. return;
  2038. }
  2039. editor.undoManager.transact(function () {
  2040. if (e.shiftKey ? outdentListSelection(editor) : indentListSelection(editor)) {
  2041. e.preventDefault();
  2042. }
  2043. });
  2044. });
  2045. };
  2046. var setup$1 = function (editor) {
  2047. if (Settings.shouldIndentOnTab(editor)) {
  2048. setupTabKey(editor);
  2049. }
  2050. Delete.setup(editor);
  2051. };
  2052. var Keyboard = { setup: setup$1 };
  2053. var findIndex = function (list, predicate) {
  2054. for (var index = 0; index < list.length; index++) {
  2055. var element = list[index];
  2056. if (predicate(element)) {
  2057. return index;
  2058. }
  2059. }
  2060. return -1;
  2061. };
  2062. var listState = function (editor, listName) {
  2063. return function (e) {
  2064. var ctrl = e.control;
  2065. editor.on('NodeChange', function (e) {
  2066. var tableCellIndex = findIndex(e.parents, NodeType.isTableCellNode);
  2067. var parents = tableCellIndex !== -1 ? e.parents.slice(0, tableCellIndex) : e.parents;
  2068. var lists = global$5.grep(parents, NodeType.isListNode);
  2069. ctrl.active(lists.length > 0 && lists[0].nodeName === listName);
  2070. });
  2071. };
  2072. };
  2073. var register$1 = function (editor) {
  2074. var hasPlugin = function (editor, plugin) {
  2075. var plugins = editor.settings.plugins ? editor.settings.plugins : '';
  2076. return global$5.inArray(plugins.split(/[ ,]/), plugin) !== -1;
  2077. };
  2078. if (!hasPlugin(editor, 'advlist')) {
  2079. editor.addButton('numlist', {
  2080. active: false,
  2081. title: 'Numbered list',
  2082. cmd: 'InsertOrderedList',
  2083. onPostRender: listState(editor, 'OL')
  2084. });
  2085. editor.addButton('bullist', {
  2086. active: false,
  2087. title: 'Bullet list',
  2088. cmd: 'InsertUnorderedList',
  2089. onPostRender: listState(editor, 'UL')
  2090. });
  2091. }
  2092. editor.addButton('indent', {
  2093. icon: 'indent',
  2094. title: 'Increase indent',
  2095. cmd: 'Indent'
  2096. });
  2097. };
  2098. var Buttons = { register: register$1 };
  2099. global.add('lists', function (editor) {
  2100. Keyboard.setup(editor);
  2101. Buttons.register(editor);
  2102. Commands.register(editor);
  2103. return Api.get(editor);
  2104. });
  2105. function Plugin () {
  2106. }
  2107. return Plugin;
  2108. }(window));
  2109. })();