plugin.js 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757
  1. /**
  2. * TinyMCE version 6.0.3 (2022-05-25)
  3. */
  4. (function () {
  5. 'use strict';
  6. var global$6 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  7. const hasProto = (v, constructor, predicate) => {
  8. var _a;
  9. if (predicate(v, constructor.prototype)) {
  10. return true;
  11. } else {
  12. return ((_a = v.constructor) === null || _a === void 0 ? void 0 : _a.name) === constructor.name;
  13. }
  14. };
  15. const typeOf = x => {
  16. const t = typeof x;
  17. if (x === null) {
  18. return 'null';
  19. } else if (t === 'object' && Array.isArray(x)) {
  20. return 'array';
  21. } else if (t === 'object' && hasProto(x, String, (o, proto) => proto.isPrototypeOf(o))) {
  22. return 'string';
  23. } else {
  24. return t;
  25. }
  26. };
  27. const isType$1 = type => value => typeOf(value) === type;
  28. const isSimpleType = type => value => typeof value === type;
  29. const isString = isType$1('string');
  30. const isObject = isType$1('object');
  31. const isArray = isType$1('array');
  32. const isBoolean = isSimpleType('boolean');
  33. const isNullable = a => a === null || a === undefined;
  34. const isNonNullable = a => !isNullable(a);
  35. const isFunction = isSimpleType('function');
  36. const isNumber = isSimpleType('number');
  37. const noop = () => {
  38. };
  39. const constant = value => {
  40. return () => {
  41. return value;
  42. };
  43. };
  44. const tripleEquals = (a, b) => {
  45. return a === b;
  46. };
  47. const not = f => t => !f(t);
  48. const never = constant(false);
  49. class Optional {
  50. constructor(tag, value) {
  51. this.tag = tag;
  52. this.value = value;
  53. }
  54. static some(value) {
  55. return new Optional(true, value);
  56. }
  57. static none() {
  58. return Optional.singletonNone;
  59. }
  60. fold(onNone, onSome) {
  61. if (this.tag) {
  62. return onSome(this.value);
  63. } else {
  64. return onNone();
  65. }
  66. }
  67. isSome() {
  68. return this.tag;
  69. }
  70. isNone() {
  71. return !this.tag;
  72. }
  73. map(mapper) {
  74. if (this.tag) {
  75. return Optional.some(mapper(this.value));
  76. } else {
  77. return Optional.none();
  78. }
  79. }
  80. bind(binder) {
  81. if (this.tag) {
  82. return binder(this.value);
  83. } else {
  84. return Optional.none();
  85. }
  86. }
  87. exists(predicate) {
  88. return this.tag && predicate(this.value);
  89. }
  90. forall(predicate) {
  91. return !this.tag || predicate(this.value);
  92. }
  93. filter(predicate) {
  94. if (!this.tag || predicate(this.value)) {
  95. return this;
  96. } else {
  97. return Optional.none();
  98. }
  99. }
  100. getOr(replacement) {
  101. return this.tag ? this.value : replacement;
  102. }
  103. or(replacement) {
  104. return this.tag ? this : replacement;
  105. }
  106. getOrThunk(thunk) {
  107. return this.tag ? this.value : thunk();
  108. }
  109. orThunk(thunk) {
  110. return this.tag ? this : thunk();
  111. }
  112. getOrDie(message) {
  113. if (!this.tag) {
  114. throw new Error(message !== null && message !== void 0 ? message : 'Called getOrDie on None');
  115. } else {
  116. return this.value;
  117. }
  118. }
  119. static from(value) {
  120. return isNonNullable(value) ? Optional.some(value) : Optional.none();
  121. }
  122. getOrNull() {
  123. return this.tag ? this.value : null;
  124. }
  125. getOrUndefined() {
  126. return this.value;
  127. }
  128. each(worker) {
  129. if (this.tag) {
  130. worker(this.value);
  131. }
  132. }
  133. toArray() {
  134. return this.tag ? [this.value] : [];
  135. }
  136. toString() {
  137. return this.tag ? `some(${ this.value })` : 'none()';
  138. }
  139. }
  140. Optional.singletonNone = new Optional(false);
  141. const nativeSlice = Array.prototype.slice;
  142. const nativeIndexOf = Array.prototype.indexOf;
  143. const nativePush = Array.prototype.push;
  144. const rawIndexOf = (ts, t) => nativeIndexOf.call(ts, t);
  145. const contains$1 = (xs, x) => rawIndexOf(xs, x) > -1;
  146. const exists = (xs, pred) => {
  147. for (let i = 0, len = xs.length; i < len; i++) {
  148. const x = xs[i];
  149. if (pred(x, i)) {
  150. return true;
  151. }
  152. }
  153. return false;
  154. };
  155. const map = (xs, f) => {
  156. const len = xs.length;
  157. const r = new Array(len);
  158. for (let i = 0; i < len; i++) {
  159. const x = xs[i];
  160. r[i] = f(x, i);
  161. }
  162. return r;
  163. };
  164. const each$1 = (xs, f) => {
  165. for (let i = 0, len = xs.length; i < len; i++) {
  166. const x = xs[i];
  167. f(x, i);
  168. }
  169. };
  170. const filter$1 = (xs, pred) => {
  171. const r = [];
  172. for (let i = 0, len = xs.length; i < len; i++) {
  173. const x = xs[i];
  174. if (pred(x, i)) {
  175. r.push(x);
  176. }
  177. }
  178. return r;
  179. };
  180. const groupBy = (xs, f) => {
  181. if (xs.length === 0) {
  182. return [];
  183. } else {
  184. let wasType = f(xs[0]);
  185. const r = [];
  186. let group = [];
  187. for (let i = 0, len = xs.length; i < len; i++) {
  188. const x = xs[i];
  189. const type = f(x);
  190. if (type !== wasType) {
  191. r.push(group);
  192. group = [];
  193. }
  194. wasType = type;
  195. group.push(x);
  196. }
  197. if (group.length !== 0) {
  198. r.push(group);
  199. }
  200. return r;
  201. }
  202. };
  203. const foldl = (xs, f, acc) => {
  204. each$1(xs, (x, i) => {
  205. acc = f(acc, x, i);
  206. });
  207. return acc;
  208. };
  209. const findUntil = (xs, pred, until) => {
  210. for (let i = 0, len = xs.length; i < len; i++) {
  211. const x = xs[i];
  212. if (pred(x, i)) {
  213. return Optional.some(x);
  214. } else if (until(x, i)) {
  215. break;
  216. }
  217. }
  218. return Optional.none();
  219. };
  220. const find = (xs, pred) => {
  221. return findUntil(xs, pred, never);
  222. };
  223. const flatten = xs => {
  224. const r = [];
  225. for (let i = 0, len = xs.length; i < len; ++i) {
  226. if (!isArray(xs[i])) {
  227. throw new Error('Arr.flatten item ' + i + ' was not an array, input: ' + xs);
  228. }
  229. nativePush.apply(r, xs[i]);
  230. }
  231. return r;
  232. };
  233. const bind = (xs, f) => flatten(map(xs, f));
  234. const reverse = xs => {
  235. const r = nativeSlice.call(xs, 0);
  236. r.reverse();
  237. return r;
  238. };
  239. const get$1 = (xs, i) => i >= 0 && i < xs.length ? Optional.some(xs[i]) : Optional.none();
  240. const head = xs => get$1(xs, 0);
  241. const last = xs => get$1(xs, xs.length - 1);
  242. const unique = (xs, comparator) => {
  243. const r = [];
  244. const isDuplicated = isFunction(comparator) ? x => exists(r, i => comparator(i, x)) : x => contains$1(r, x);
  245. for (let i = 0, len = xs.length; i < len; i++) {
  246. const x = xs[i];
  247. if (!isDuplicated(x)) {
  248. r.push(x);
  249. }
  250. }
  251. return r;
  252. };
  253. const ELEMENT = 1;
  254. const fromHtml = (html, scope) => {
  255. const doc = scope || document;
  256. const div = doc.createElement('div');
  257. div.innerHTML = html;
  258. if (!div.hasChildNodes() || div.childNodes.length > 1) {
  259. const message = 'HTML does not have a single root node';
  260. console.error(message, html);
  261. throw new Error(message);
  262. }
  263. return fromDom(div.childNodes[0]);
  264. };
  265. const fromTag = (tag, scope) => {
  266. const doc = scope || document;
  267. const node = doc.createElement(tag);
  268. return fromDom(node);
  269. };
  270. const fromText = (text, scope) => {
  271. const doc = scope || document;
  272. const node = doc.createTextNode(text);
  273. return fromDom(node);
  274. };
  275. const fromDom = node => {
  276. if (node === null || node === undefined) {
  277. throw new Error('Node cannot be null or undefined');
  278. }
  279. return { dom: node };
  280. };
  281. const fromPoint = (docElm, x, y) => Optional.from(docElm.dom.elementFromPoint(x, y)).map(fromDom);
  282. const SugarElement = {
  283. fromHtml,
  284. fromTag,
  285. fromText,
  286. fromDom,
  287. fromPoint
  288. };
  289. const is$2 = (element, selector) => {
  290. const dom = element.dom;
  291. if (dom.nodeType !== ELEMENT) {
  292. return false;
  293. } else {
  294. const elem = dom;
  295. if (elem.matches !== undefined) {
  296. return elem.matches(selector);
  297. } else if (elem.msMatchesSelector !== undefined) {
  298. return elem.msMatchesSelector(selector);
  299. } else if (elem.webkitMatchesSelector !== undefined) {
  300. return elem.webkitMatchesSelector(selector);
  301. } else if (elem.mozMatchesSelector !== undefined) {
  302. return elem.mozMatchesSelector(selector);
  303. } else {
  304. throw new Error('Browser lacks native selectors');
  305. }
  306. }
  307. };
  308. const eq = (e1, e2) => e1.dom === e2.dom;
  309. const contains = (e1, e2) => {
  310. const d1 = e1.dom;
  311. const d2 = e2.dom;
  312. return d1 === d2 ? false : d1.contains(d2);
  313. };
  314. const is$1 = is$2;
  315. typeof window !== 'undefined' ? window : Function('return this;')();
  316. const name = element => {
  317. const r = element.dom.nodeName;
  318. return r.toLowerCase();
  319. };
  320. const type = element => element.dom.nodeType;
  321. const isType = t => element => type(element) === t;
  322. const isElement = isType(ELEMENT);
  323. const isTag = tag => e => isElement(e) && name(e) === tag;
  324. const parent = element => Optional.from(element.dom.parentNode).map(SugarElement.fromDom);
  325. const nextSibling = element => Optional.from(element.dom.nextSibling).map(SugarElement.fromDom);
  326. const children = element => map(element.dom.childNodes, SugarElement.fromDom);
  327. const child = (element, index) => {
  328. const cs = element.dom.childNodes;
  329. return Optional.from(cs[index]).map(SugarElement.fromDom);
  330. };
  331. const firstChild = element => child(element, 0);
  332. const lastChild = element => child(element, element.dom.childNodes.length - 1);
  333. const before$1 = (marker, element) => {
  334. const parent$1 = parent(marker);
  335. parent$1.each(v => {
  336. v.dom.insertBefore(element.dom, marker.dom);
  337. });
  338. };
  339. const after = (marker, element) => {
  340. const sibling = nextSibling(marker);
  341. sibling.fold(() => {
  342. const parent$1 = parent(marker);
  343. parent$1.each(v => {
  344. append$1(v, element);
  345. });
  346. }, v => {
  347. before$1(v, element);
  348. });
  349. };
  350. const append$1 = (parent, element) => {
  351. parent.dom.appendChild(element.dom);
  352. };
  353. const before = (marker, elements) => {
  354. each$1(elements, x => {
  355. before$1(marker, x);
  356. });
  357. };
  358. const append = (parent, elements) => {
  359. each$1(elements, x => {
  360. append$1(parent, x);
  361. });
  362. };
  363. const empty = element => {
  364. element.dom.textContent = '';
  365. each$1(children(element), rogue => {
  366. remove(rogue);
  367. });
  368. };
  369. const remove = element => {
  370. const dom = element.dom;
  371. if (dom.parentNode !== null) {
  372. dom.parentNode.removeChild(dom);
  373. }
  374. };
  375. var global$5 = tinymce.util.Tools.resolve('tinymce.dom.RangeUtils');
  376. var global$4 = tinymce.util.Tools.resolve('tinymce.dom.TreeWalker');
  377. var global$3 = tinymce.util.Tools.resolve('tinymce.util.VK');
  378. const keys = Object.keys;
  379. const each = (obj, f) => {
  380. const props = keys(obj);
  381. for (let k = 0, len = props.length; k < len; k++) {
  382. const i = props[k];
  383. const x = obj[i];
  384. f(x, i);
  385. }
  386. };
  387. const objAcc = r => (x, i) => {
  388. r[i] = x;
  389. };
  390. const internalFilter = (obj, pred, onTrue, onFalse) => {
  391. const r = {};
  392. each(obj, (x, i) => {
  393. (pred(x, i) ? onTrue : onFalse)(x, i);
  394. });
  395. return r;
  396. };
  397. const filter = (obj, pred) => {
  398. const t = {};
  399. internalFilter(obj, pred, objAcc(t), noop);
  400. return t;
  401. };
  402. const rawSet = (dom, key, value) => {
  403. if (isString(value) || isBoolean(value) || isNumber(value)) {
  404. dom.setAttribute(key, value + '');
  405. } else {
  406. console.error('Invalid call to Attribute.set. Key ', key, ':: Value ', value, ':: Element ', dom);
  407. throw new Error('Attribute value was not simple');
  408. }
  409. };
  410. const setAll = (element, attrs) => {
  411. const dom = element.dom;
  412. each(attrs, (v, k) => {
  413. rawSet(dom, k, v);
  414. });
  415. };
  416. const clone$1 = element => foldl(element.dom.attributes, (acc, attr) => {
  417. acc[attr.name] = attr.value;
  418. return acc;
  419. }, {});
  420. const clone = (original, isDeep) => SugarElement.fromDom(original.dom.cloneNode(isDeep));
  421. const deep = original => clone(original, true);
  422. const shallowAs = (original, tag) => {
  423. const nu = SugarElement.fromTag(tag);
  424. const attributes = clone$1(original);
  425. setAll(nu, attributes);
  426. return nu;
  427. };
  428. const mutate = (original, tag) => {
  429. const nu = shallowAs(original, tag);
  430. after(original, nu);
  431. const children$1 = children(original);
  432. append(nu, children$1);
  433. remove(original);
  434. return nu;
  435. };
  436. var global$2 = tinymce.util.Tools.resolve('tinymce.dom.DOMUtils');
  437. var global$1 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  438. const matchNodeName = name => node => node && node.nodeName.toLowerCase() === name;
  439. const matchNodeNames = regex => node => node && regex.test(node.nodeName);
  440. const isTextNode = node => node && node.nodeType === 3;
  441. const isListNode = matchNodeNames(/^(OL|UL|DL)$/);
  442. const isOlUlNode = matchNodeNames(/^(OL|UL)$/);
  443. const isOlNode = matchNodeName('ol');
  444. const isListItemNode = matchNodeNames(/^(LI|DT|DD)$/);
  445. const isDlItemNode = matchNodeNames(/^(DT|DD)$/);
  446. const isTableCellNode = matchNodeNames(/^(TH|TD)$/);
  447. const isBr = matchNodeName('br');
  448. const isFirstChild = node => node.parentNode.firstChild === node;
  449. const isTextBlock = (editor, node) => node && !!editor.schema.getTextBlockElements()[node.nodeName];
  450. const isBlock = (node, blockElements) => node && node.nodeName in blockElements;
  451. const isBogusBr = (dom, node) => {
  452. if (!isBr(node)) {
  453. return false;
  454. }
  455. return dom.isBlock(node.nextSibling) && !isBr(node.previousSibling);
  456. };
  457. const isEmpty$1 = (dom, elm, keepBookmarks) => {
  458. const empty = dom.isEmpty(elm);
  459. if (keepBookmarks && dom.select('span[data-mce-type=bookmark]', elm).length > 0) {
  460. return false;
  461. }
  462. return empty;
  463. };
  464. const isChildOfBody = (dom, elm) => dom.isChildOf(elm, dom.getRoot());
  465. const option = name => editor => editor.options.get(name);
  466. const register$3 = editor => {
  467. const registerOption = editor.options.register;
  468. registerOption('lists_indent_on_tab', {
  469. processor: 'boolean',
  470. default: true
  471. });
  472. };
  473. const shouldIndentOnTab = option('lists_indent_on_tab');
  474. const getForcedRootBlock = option('forced_root_block');
  475. const getForcedRootBlockAttrs = option('forced_root_block_attrs');
  476. const createTextBlock = (editor, contentNode) => {
  477. const dom = editor.dom;
  478. const blockElements = editor.schema.getBlockElements();
  479. const fragment = dom.createFragment();
  480. const blockName = getForcedRootBlock(editor);
  481. const blockAttrs = getForcedRootBlockAttrs(editor);
  482. let node, textBlock, hasContentNode;
  483. textBlock = dom.create(blockName, blockAttrs);
  484. if (!isBlock(contentNode.firstChild, blockElements)) {
  485. fragment.appendChild(textBlock);
  486. }
  487. while (node = contentNode.firstChild) {
  488. const nodeName = node.nodeName;
  489. if (!hasContentNode && (nodeName !== 'SPAN' || node.getAttribute('data-mce-type') !== 'bookmark')) {
  490. hasContentNode = true;
  491. }
  492. if (isBlock(node, blockElements)) {
  493. fragment.appendChild(node);
  494. textBlock = null;
  495. } else {
  496. if (!textBlock) {
  497. textBlock = dom.create(blockName, blockAttrs);
  498. fragment.appendChild(textBlock);
  499. }
  500. textBlock.appendChild(node);
  501. }
  502. }
  503. if (!hasContentNode) {
  504. textBlock.appendChild(dom.create('br', { 'data-mce-bogus': '1' }));
  505. }
  506. return fragment;
  507. };
  508. const DOM$2 = global$2.DOM;
  509. const splitList = (editor, list, li) => {
  510. const removeAndKeepBookmarks = targetNode => {
  511. global$1.each(bookmarks, node => {
  512. targetNode.parentNode.insertBefore(node, li.parentNode);
  513. });
  514. DOM$2.remove(targetNode);
  515. };
  516. const bookmarks = DOM$2.select('span[data-mce-type="bookmark"]', list);
  517. const newBlock = createTextBlock(editor, li);
  518. const tmpRng = DOM$2.createRng();
  519. tmpRng.setStartAfter(li);
  520. tmpRng.setEndAfter(list);
  521. const fragment = tmpRng.extractContents();
  522. for (let node = fragment.firstChild; node; node = node.firstChild) {
  523. if (node.nodeName === 'LI' && editor.dom.isEmpty(node)) {
  524. DOM$2.remove(node);
  525. break;
  526. }
  527. }
  528. if (!editor.dom.isEmpty(fragment)) {
  529. DOM$2.insertAfter(fragment, list);
  530. }
  531. DOM$2.insertAfter(newBlock, list);
  532. if (isEmpty$1(editor.dom, li.parentNode)) {
  533. removeAndKeepBookmarks(li.parentNode);
  534. }
  535. DOM$2.remove(li);
  536. if (isEmpty$1(editor.dom, list)) {
  537. DOM$2.remove(list);
  538. }
  539. };
  540. const isDescriptionDetail = isTag('dd');
  541. const isDescriptionTerm = isTag('dt');
  542. const outdentDlItem = (editor, item) => {
  543. if (isDescriptionDetail(item)) {
  544. mutate(item, 'dt');
  545. } else if (isDescriptionTerm(item)) {
  546. parent(item).each(dl => splitList(editor, dl.dom, item.dom));
  547. }
  548. };
  549. const indentDlItem = item => {
  550. if (isDescriptionTerm(item)) {
  551. mutate(item, 'dd');
  552. }
  553. };
  554. const dlIndentation = (editor, indentation, dlItems) => {
  555. if (indentation === 'Indent') {
  556. each$1(dlItems, indentDlItem);
  557. } else {
  558. each$1(dlItems, item => outdentDlItem(editor, item));
  559. }
  560. };
  561. const getNormalizedPoint = (container, offset) => {
  562. if (isTextNode(container)) {
  563. return {
  564. container,
  565. offset
  566. };
  567. }
  568. const node = global$5.getNode(container, offset);
  569. if (isTextNode(node)) {
  570. return {
  571. container: node,
  572. offset: offset >= container.childNodes.length ? node.data.length : 0
  573. };
  574. } else if (node.previousSibling && isTextNode(node.previousSibling)) {
  575. return {
  576. container: node.previousSibling,
  577. offset: node.previousSibling.data.length
  578. };
  579. } else if (node.nextSibling && isTextNode(node.nextSibling)) {
  580. return {
  581. container: node.nextSibling,
  582. offset: 0
  583. };
  584. }
  585. return {
  586. container,
  587. offset
  588. };
  589. };
  590. const normalizeRange = rng => {
  591. const outRng = rng.cloneRange();
  592. const rangeStart = getNormalizedPoint(rng.startContainer, rng.startOffset);
  593. outRng.setStart(rangeStart.container, rangeStart.offset);
  594. const rangeEnd = getNormalizedPoint(rng.endContainer, rng.endOffset);
  595. outRng.setEnd(rangeEnd.container, rangeEnd.offset);
  596. return outRng;
  597. };
  598. const getParentList = (editor, node) => {
  599. const selectionStart = node || editor.selection.getStart(true);
  600. return editor.dom.getParent(selectionStart, 'OL,UL,DL', getClosestListRootElm(editor, selectionStart));
  601. };
  602. const isParentListSelected = (parentList, selectedBlocks) => parentList && selectedBlocks.length === 1 && selectedBlocks[0] === parentList;
  603. const findSubLists = parentList => filter$1(parentList.querySelectorAll('ol,ul,dl'), isListNode);
  604. const getSelectedSubLists = editor => {
  605. const parentList = getParentList(editor);
  606. const selectedBlocks = editor.selection.getSelectedBlocks();
  607. if (isParentListSelected(parentList, selectedBlocks)) {
  608. return findSubLists(parentList);
  609. } else {
  610. return filter$1(selectedBlocks, elm => {
  611. return isListNode(elm) && parentList !== elm;
  612. });
  613. }
  614. };
  615. const findParentListItemsNodes = (editor, elms) => {
  616. const listItemsElms = global$1.map(elms, elm => {
  617. const parentLi = editor.dom.getParent(elm, 'li,dd,dt', getClosestListRootElm(editor, elm));
  618. return parentLi ? parentLi : elm;
  619. });
  620. return unique(listItemsElms);
  621. };
  622. const getSelectedListItems = editor => {
  623. const selectedBlocks = editor.selection.getSelectedBlocks();
  624. return filter$1(findParentListItemsNodes(editor, selectedBlocks), isListItemNode);
  625. };
  626. const getSelectedDlItems = editor => filter$1(getSelectedListItems(editor), isDlItemNode);
  627. const getClosestListRootElm = (editor, elm) => {
  628. const parentTableCell = editor.dom.getParents(elm, 'TD,TH');
  629. return parentTableCell.length > 0 ? parentTableCell[0] : editor.getBody();
  630. };
  631. const findLastParentListNode = (editor, elm) => {
  632. const parentLists = editor.dom.getParents(elm, 'ol,ul', getClosestListRootElm(editor, elm));
  633. return last(parentLists);
  634. };
  635. const getSelectedLists = editor => {
  636. const firstList = findLastParentListNode(editor, editor.selection.getStart());
  637. const subsequentLists = filter$1(editor.selection.getSelectedBlocks(), isOlUlNode);
  638. return firstList.toArray().concat(subsequentLists);
  639. };
  640. const getSelectedListRoots = editor => {
  641. const selectedLists = getSelectedLists(editor);
  642. return getUniqueListRoots(editor, selectedLists);
  643. };
  644. const getUniqueListRoots = (editor, lists) => {
  645. const listRoots = map(lists, list => findLastParentListNode(editor, list).getOr(list));
  646. return unique(listRoots);
  647. };
  648. const is = (lhs, rhs, comparator = tripleEquals) => lhs.exists(left => comparator(left, rhs));
  649. const lift2 = (oa, ob, f) => oa.isSome() && ob.isSome() ? Optional.some(f(oa.getOrDie(), ob.getOrDie())) : Optional.none();
  650. const fromElements = (elements, scope) => {
  651. const doc = scope || document;
  652. const fragment = doc.createDocumentFragment();
  653. each$1(elements, element => {
  654. fragment.appendChild(element.dom);
  655. });
  656. return SugarElement.fromDom(fragment);
  657. };
  658. const fireListEvent = (editor, action, element) => editor.dispatch('ListMutation', {
  659. action,
  660. element
  661. });
  662. const blank = r => s => s.replace(r, '');
  663. const trim = blank(/^\s+|\s+$/g);
  664. const isNotEmpty = s => s.length > 0;
  665. const isEmpty = s => !isNotEmpty(s);
  666. const isSupported = dom => dom.style !== undefined && isFunction(dom.style.getPropertyValue);
  667. const internalSet = (dom, property, value) => {
  668. if (!isString(value)) {
  669. console.error('Invalid call to CSS.set. Property ', property, ':: Value ', value, ':: Element ', dom);
  670. throw new Error('CSS value must be a string: ' + value);
  671. }
  672. if (isSupported(dom)) {
  673. dom.style.setProperty(property, value);
  674. }
  675. };
  676. const set = (element, property, value) => {
  677. const dom = element.dom;
  678. internalSet(dom, property, value);
  679. };
  680. const joinSegment = (parent, child) => {
  681. append$1(parent.item, child.list);
  682. };
  683. const joinSegments = segments => {
  684. for (let i = 1; i < segments.length; i++) {
  685. joinSegment(segments[i - 1], segments[i]);
  686. }
  687. };
  688. const appendSegments = (head$1, tail) => {
  689. lift2(last(head$1), head(tail), joinSegment);
  690. };
  691. const createSegment = (scope, listType) => {
  692. const segment = {
  693. list: SugarElement.fromTag(listType, scope),
  694. item: SugarElement.fromTag('li', scope)
  695. };
  696. append$1(segment.list, segment.item);
  697. return segment;
  698. };
  699. const createSegments = (scope, entry, size) => {
  700. const segments = [];
  701. for (let i = 0; i < size; i++) {
  702. segments.push(createSegment(scope, entry.listType));
  703. }
  704. return segments;
  705. };
  706. const populateSegments = (segments, entry) => {
  707. for (let i = 0; i < segments.length - 1; i++) {
  708. set(segments[i].item, 'list-style-type', 'none');
  709. }
  710. last(segments).each(segment => {
  711. setAll(segment.list, entry.listAttributes);
  712. setAll(segment.item, entry.itemAttributes);
  713. append(segment.item, entry.content);
  714. });
  715. };
  716. const normalizeSegment = (segment, entry) => {
  717. if (name(segment.list) !== entry.listType) {
  718. segment.list = mutate(segment.list, entry.listType);
  719. }
  720. setAll(segment.list, entry.listAttributes);
  721. };
  722. const createItem = (scope, attr, content) => {
  723. const item = SugarElement.fromTag('li', scope);
  724. setAll(item, attr);
  725. append(item, content);
  726. return item;
  727. };
  728. const appendItem = (segment, item) => {
  729. append$1(segment.list, item);
  730. segment.item = item;
  731. };
  732. const writeShallow = (scope, cast, entry) => {
  733. const newCast = cast.slice(0, entry.depth);
  734. last(newCast).each(segment => {
  735. const item = createItem(scope, entry.itemAttributes, entry.content);
  736. appendItem(segment, item);
  737. normalizeSegment(segment, entry);
  738. });
  739. return newCast;
  740. };
  741. const writeDeep = (scope, cast, entry) => {
  742. const segments = createSegments(scope, entry, entry.depth - cast.length);
  743. joinSegments(segments);
  744. populateSegments(segments, entry);
  745. appendSegments(cast, segments);
  746. return cast.concat(segments);
  747. };
  748. const composeList = (scope, entries) => {
  749. const cast = foldl(entries, (cast, entry) => {
  750. return entry.depth > cast.length ? writeDeep(scope, cast, entry) : writeShallow(scope, cast, entry);
  751. }, []);
  752. return head(cast).map(segment => segment.list);
  753. };
  754. const isList = el => is$1(el, 'OL,UL');
  755. const hasFirstChildList = el => firstChild(el).exists(isList);
  756. const hasLastChildList = el => lastChild(el).exists(isList);
  757. const isIndented = entry => entry.depth > 0;
  758. const isSelected = entry => entry.isSelected;
  759. const cloneItemContent = li => {
  760. const children$1 = children(li);
  761. const content = hasLastChildList(li) ? children$1.slice(0, -1) : children$1;
  762. return map(content, deep);
  763. };
  764. const createEntry = (li, depth, isSelected) => parent(li).filter(isElement).map(list => ({
  765. depth,
  766. dirty: false,
  767. isSelected,
  768. content: cloneItemContent(li),
  769. itemAttributes: clone$1(li),
  770. listAttributes: clone$1(list),
  771. listType: name(list)
  772. }));
  773. const indentEntry = (indentation, entry) => {
  774. switch (indentation) {
  775. case 'Indent':
  776. entry.depth++;
  777. break;
  778. case 'Outdent':
  779. entry.depth--;
  780. break;
  781. case 'Flatten':
  782. entry.depth = 0;
  783. }
  784. entry.dirty = true;
  785. };
  786. const cloneListProperties = (target, source) => {
  787. target.listType = source.listType;
  788. target.listAttributes = { ...source.listAttributes };
  789. };
  790. const cleanListProperties = entry => {
  791. entry.listAttributes = filter(entry.listAttributes, (_value, key) => key !== 'start');
  792. };
  793. const closestSiblingEntry = (entries, start) => {
  794. const depth = entries[start].depth;
  795. const matches = entry => entry.depth === depth && !entry.dirty;
  796. const until = entry => entry.depth < depth;
  797. return findUntil(reverse(entries.slice(0, start)), matches, until).orThunk(() => findUntil(entries.slice(start + 1), matches, until));
  798. };
  799. const normalizeEntries = entries => {
  800. each$1(entries, (entry, i) => {
  801. closestSiblingEntry(entries, i).fold(() => {
  802. if (entry.dirty) {
  803. cleanListProperties(entry);
  804. }
  805. }, matchingEntry => cloneListProperties(entry, matchingEntry));
  806. });
  807. return entries;
  808. };
  809. const Cell = initial => {
  810. let value = initial;
  811. const get = () => {
  812. return value;
  813. };
  814. const set = v => {
  815. value = v;
  816. };
  817. return {
  818. get,
  819. set
  820. };
  821. };
  822. const parseItem = (depth, itemSelection, selectionState, item) => firstChild(item).filter(isList).fold(() => {
  823. itemSelection.each(selection => {
  824. if (eq(selection.start, item)) {
  825. selectionState.set(true);
  826. }
  827. });
  828. const currentItemEntry = createEntry(item, depth, selectionState.get());
  829. itemSelection.each(selection => {
  830. if (eq(selection.end, item)) {
  831. selectionState.set(false);
  832. }
  833. });
  834. const childListEntries = lastChild(item).filter(isList).map(list => parseList(depth, itemSelection, selectionState, list)).getOr([]);
  835. return currentItemEntry.toArray().concat(childListEntries);
  836. }, list => parseList(depth, itemSelection, selectionState, list));
  837. const parseList = (depth, itemSelection, selectionState, list) => bind(children(list), element => {
  838. const parser = isList(element) ? parseList : parseItem;
  839. const newDepth = depth + 1;
  840. return parser(newDepth, itemSelection, selectionState, element);
  841. });
  842. const parseLists = (lists, itemSelection) => {
  843. const selectionState = Cell(false);
  844. const initialDepth = 0;
  845. return map(lists, list => ({
  846. sourceList: list,
  847. entries: parseList(initialDepth, itemSelection, selectionState, list)
  848. }));
  849. };
  850. const outdentedComposer = (editor, entries) => {
  851. const normalizedEntries = normalizeEntries(entries);
  852. return map(normalizedEntries, entry => {
  853. const content = fromElements(entry.content);
  854. return SugarElement.fromDom(createTextBlock(editor, content.dom));
  855. });
  856. };
  857. const indentedComposer = (editor, entries) => {
  858. const normalizedEntries = normalizeEntries(entries);
  859. return composeList(editor.contentDocument, normalizedEntries).toArray();
  860. };
  861. const composeEntries = (editor, entries) => bind(groupBy(entries, isIndented), entries => {
  862. const groupIsIndented = head(entries).exists(isIndented);
  863. return groupIsIndented ? indentedComposer(editor, entries) : outdentedComposer(editor, entries);
  864. });
  865. const indentSelectedEntries = (entries, indentation) => {
  866. each$1(filter$1(entries, isSelected), entry => indentEntry(indentation, entry));
  867. };
  868. const getItemSelection = editor => {
  869. const selectedListItems = map(getSelectedListItems(editor), SugarElement.fromDom);
  870. return lift2(find(selectedListItems, not(hasFirstChildList)), find(reverse(selectedListItems), not(hasFirstChildList)), (start, end) => ({
  871. start,
  872. end
  873. }));
  874. };
  875. const listIndentation = (editor, lists, indentation) => {
  876. const entrySets = parseLists(lists, getItemSelection(editor));
  877. each$1(entrySets, entrySet => {
  878. indentSelectedEntries(entrySet.entries, indentation);
  879. const composedLists = composeEntries(editor, entrySet.entries);
  880. each$1(composedLists, composedList => {
  881. fireListEvent(editor, indentation === 'Indent' ? 'IndentList' : 'OutdentList', composedList.dom);
  882. });
  883. before(entrySet.sourceList, composedLists);
  884. remove(entrySet.sourceList);
  885. });
  886. };
  887. const selectionIndentation = (editor, indentation) => {
  888. const lists = map(getSelectedListRoots(editor), SugarElement.fromDom);
  889. const dlItems = map(getSelectedDlItems(editor), SugarElement.fromDom);
  890. let isHandled = false;
  891. if (lists.length || dlItems.length) {
  892. const bookmark = editor.selection.getBookmark();
  893. listIndentation(editor, lists, indentation);
  894. dlIndentation(editor, indentation, dlItems);
  895. editor.selection.moveToBookmark(bookmark);
  896. editor.selection.setRng(normalizeRange(editor.selection.getRng()));
  897. editor.nodeChanged();
  898. isHandled = true;
  899. }
  900. return isHandled;
  901. };
  902. const indentListSelection = editor => selectionIndentation(editor, 'Indent');
  903. const outdentListSelection = editor => selectionIndentation(editor, 'Outdent');
  904. const flattenListSelection = editor => selectionIndentation(editor, 'Flatten');
  905. var global = tinymce.util.Tools.resolve('tinymce.dom.BookmarkManager');
  906. const DOM$1 = global$2.DOM;
  907. const createBookmark = rng => {
  908. const bookmark = {};
  909. const setupEndPoint = start => {
  910. let container = rng[start ? 'startContainer' : 'endContainer'];
  911. let offset = rng[start ? 'startOffset' : 'endOffset'];
  912. if (container.nodeType === 1) {
  913. const offsetNode = DOM$1.create('span', { 'data-mce-type': 'bookmark' });
  914. if (container.hasChildNodes()) {
  915. offset = Math.min(offset, container.childNodes.length - 1);
  916. if (start) {
  917. container.insertBefore(offsetNode, container.childNodes[offset]);
  918. } else {
  919. DOM$1.insertAfter(offsetNode, container.childNodes[offset]);
  920. }
  921. } else {
  922. container.appendChild(offsetNode);
  923. }
  924. container = offsetNode;
  925. offset = 0;
  926. }
  927. bookmark[start ? 'startContainer' : 'endContainer'] = container;
  928. bookmark[start ? 'startOffset' : 'endOffset'] = offset;
  929. };
  930. setupEndPoint(true);
  931. if (!rng.collapsed) {
  932. setupEndPoint();
  933. }
  934. return bookmark;
  935. };
  936. const resolveBookmark = bookmark => {
  937. const restoreEndPoint = start => {
  938. let node;
  939. const nodeIndex = container => {
  940. let node = container.parentNode.firstChild, idx = 0;
  941. while (node) {
  942. if (node === container) {
  943. return idx;
  944. }
  945. if (node.nodeType !== 1 || node.getAttribute('data-mce-type') !== 'bookmark') {
  946. idx++;
  947. }
  948. node = node.nextSibling;
  949. }
  950. return -1;
  951. };
  952. let container = node = bookmark[start ? 'startContainer' : 'endContainer'];
  953. let offset = bookmark[start ? 'startOffset' : 'endOffset'];
  954. if (!container) {
  955. return;
  956. }
  957. if (container.nodeType === 1) {
  958. offset = nodeIndex(container);
  959. container = container.parentNode;
  960. DOM$1.remove(node);
  961. if (!container.hasChildNodes() && DOM$1.isBlock(container)) {
  962. container.appendChild(DOM$1.create('br'));
  963. }
  964. }
  965. bookmark[start ? 'startContainer' : 'endContainer'] = container;
  966. bookmark[start ? 'startOffset' : 'endOffset'] = offset;
  967. };
  968. restoreEndPoint(true);
  969. restoreEndPoint();
  970. const rng = DOM$1.createRng();
  971. rng.setStart(bookmark.startContainer, bookmark.startOffset);
  972. if (bookmark.endContainer) {
  973. rng.setEnd(bookmark.endContainer, bookmark.endOffset);
  974. }
  975. return normalizeRange(rng);
  976. };
  977. const listToggleActionFromListName = listName => {
  978. switch (listName) {
  979. case 'UL':
  980. return 'ToggleUlList';
  981. case 'OL':
  982. return 'ToggleOlList';
  983. case 'DL':
  984. return 'ToggleDLList';
  985. }
  986. };
  987. const isCustomList = list => /\btox\-/.test(list.className);
  988. const listState = (editor, listName, activate) => {
  989. const nodeChangeHandler = e => {
  990. const inList = findUntil(e.parents, isListNode, isTableCellNode).filter(list => list.nodeName === listName && !isCustomList(list)).isSome();
  991. activate(inList);
  992. };
  993. const parents = editor.dom.getParents(editor.selection.getNode());
  994. nodeChangeHandler({ parents });
  995. editor.on('NodeChange', nodeChangeHandler);
  996. return () => editor.off('NodeChange', nodeChangeHandler);
  997. };
  998. const updateListStyle = (dom, el, detail) => {
  999. const type = detail['list-style-type'] ? detail['list-style-type'] : null;
  1000. dom.setStyle(el, 'list-style-type', type);
  1001. };
  1002. const setAttribs = (elm, attrs) => {
  1003. global$1.each(attrs, (value, key) => {
  1004. elm.setAttribute(key, value);
  1005. });
  1006. };
  1007. const updateListAttrs = (dom, el, detail) => {
  1008. setAttribs(el, detail['list-attributes']);
  1009. global$1.each(dom.select('li', el), li => {
  1010. setAttribs(li, detail['list-item-attributes']);
  1011. });
  1012. };
  1013. const updateListWithDetails = (dom, el, detail) => {
  1014. updateListStyle(dom, el, detail);
  1015. updateListAttrs(dom, el, detail);
  1016. };
  1017. const removeStyles = (dom, element, styles) => {
  1018. global$1.each(styles, style => dom.setStyle(element, style, ''));
  1019. };
  1020. const getEndPointNode = (editor, rng, start, root) => {
  1021. let container = rng[start ? 'startContainer' : 'endContainer'];
  1022. const offset = rng[start ? 'startOffset' : 'endOffset'];
  1023. if (container.nodeType === 1) {
  1024. container = container.childNodes[Math.min(offset, container.childNodes.length - 1)] || container;
  1025. }
  1026. if (!start && isBr(container.nextSibling)) {
  1027. container = container.nextSibling;
  1028. }
  1029. while (container.parentNode !== root) {
  1030. if (isTextBlock(editor, container)) {
  1031. return container;
  1032. }
  1033. if (/^(TD|TH)$/.test(container.parentNode.nodeName)) {
  1034. return container;
  1035. }
  1036. container = container.parentNode;
  1037. }
  1038. return container;
  1039. };
  1040. const getSelectedTextBlocks = (editor, rng, root) => {
  1041. const textBlocks = [];
  1042. const dom = editor.dom;
  1043. const startNode = getEndPointNode(editor, rng, true, root);
  1044. const endNode = getEndPointNode(editor, rng, false, root);
  1045. let block;
  1046. const siblings = [];
  1047. for (let node = startNode; node; node = node.nextSibling) {
  1048. siblings.push(node);
  1049. if (node === endNode) {
  1050. break;
  1051. }
  1052. }
  1053. global$1.each(siblings, node => {
  1054. if (isTextBlock(editor, node)) {
  1055. textBlocks.push(node);
  1056. block = null;
  1057. return;
  1058. }
  1059. if (dom.isBlock(node) || isBr(node)) {
  1060. if (isBr(node)) {
  1061. dom.remove(node);
  1062. }
  1063. block = null;
  1064. return;
  1065. }
  1066. const nextSibling = node.nextSibling;
  1067. if (global.isBookmarkNode(node)) {
  1068. if (isListNode(nextSibling) || isTextBlock(editor, nextSibling) || !nextSibling && node.parentNode === root) {
  1069. block = null;
  1070. return;
  1071. }
  1072. }
  1073. if (!block) {
  1074. block = dom.create('p');
  1075. node.parentNode.insertBefore(block, node);
  1076. textBlocks.push(block);
  1077. }
  1078. block.appendChild(node);
  1079. });
  1080. return textBlocks;
  1081. };
  1082. const hasCompatibleStyle = (dom, sib, detail) => {
  1083. const sibStyle = dom.getStyle(sib, 'list-style-type');
  1084. let detailStyle = detail ? detail['list-style-type'] : '';
  1085. detailStyle = detailStyle === null ? '' : detailStyle;
  1086. return sibStyle === detailStyle;
  1087. };
  1088. const applyList = (editor, listName, detail) => {
  1089. const rng = editor.selection.getRng();
  1090. let listItemName = 'LI';
  1091. const root = getClosestListRootElm(editor, editor.selection.getStart(true));
  1092. const dom = editor.dom;
  1093. if (dom.getContentEditable(editor.selection.getNode()) === 'false') {
  1094. return;
  1095. }
  1096. listName = listName.toUpperCase();
  1097. if (listName === 'DL') {
  1098. listItemName = 'DT';
  1099. }
  1100. const bookmark = createBookmark(rng);
  1101. const selectedTextBlocks = getSelectedTextBlocks(editor, rng, root);
  1102. global$1.each(selectedTextBlocks, block => {
  1103. let listBlock;
  1104. const sibling = block.previousSibling;
  1105. const parent = block.parentNode;
  1106. if (!isListItemNode(parent)) {
  1107. if (sibling && isListNode(sibling) && sibling.nodeName === listName && hasCompatibleStyle(dom, sibling, detail)) {
  1108. listBlock = sibling;
  1109. block = dom.rename(block, listItemName);
  1110. sibling.appendChild(block);
  1111. } else {
  1112. listBlock = dom.create(listName);
  1113. block.parentNode.insertBefore(listBlock, block);
  1114. listBlock.appendChild(block);
  1115. block = dom.rename(block, listItemName);
  1116. }
  1117. removeStyles(dom, block, [
  1118. 'margin',
  1119. 'margin-right',
  1120. 'margin-bottom',
  1121. 'margin-left',
  1122. 'margin-top',
  1123. 'padding',
  1124. 'padding-right',
  1125. 'padding-bottom',
  1126. 'padding-left',
  1127. 'padding-top'
  1128. ]);
  1129. updateListWithDetails(dom, listBlock, detail);
  1130. mergeWithAdjacentLists(editor.dom, listBlock);
  1131. }
  1132. });
  1133. editor.selection.setRng(resolveBookmark(bookmark));
  1134. };
  1135. const isValidLists = (list1, list2) => {
  1136. return list1 && list2 && isListNode(list1) && list1.nodeName === list2.nodeName;
  1137. };
  1138. const hasSameListStyle = (dom, list1, list2) => {
  1139. const targetStyle = dom.getStyle(list1, 'list-style-type', true);
  1140. const style = dom.getStyle(list2, 'list-style-type', true);
  1141. return targetStyle === style;
  1142. };
  1143. const hasSameClasses = (elm1, elm2) => {
  1144. return elm1.className === elm2.className;
  1145. };
  1146. const shouldMerge = (dom, list1, list2) => {
  1147. return isValidLists(list1, list2) && hasSameListStyle(dom, list1, list2) && hasSameClasses(list1, list2);
  1148. };
  1149. const mergeWithAdjacentLists = (dom, listBlock) => {
  1150. let sibling, node;
  1151. sibling = listBlock.nextSibling;
  1152. if (shouldMerge(dom, listBlock, sibling)) {
  1153. while (node = sibling.firstChild) {
  1154. listBlock.appendChild(node);
  1155. }
  1156. dom.remove(sibling);
  1157. }
  1158. sibling = listBlock.previousSibling;
  1159. if (shouldMerge(dom, listBlock, sibling)) {
  1160. while (node = sibling.lastChild) {
  1161. listBlock.insertBefore(node, listBlock.firstChild);
  1162. }
  1163. dom.remove(sibling);
  1164. }
  1165. };
  1166. const updateList$1 = (editor, list, listName, detail) => {
  1167. if (list.nodeName !== listName) {
  1168. const newList = editor.dom.rename(list, listName);
  1169. updateListWithDetails(editor.dom, newList, detail);
  1170. fireListEvent(editor, listToggleActionFromListName(listName), newList);
  1171. } else {
  1172. updateListWithDetails(editor.dom, list, detail);
  1173. fireListEvent(editor, listToggleActionFromListName(listName), list);
  1174. }
  1175. };
  1176. const toggleMultipleLists = (editor, parentList, lists, listName, detail) => {
  1177. const parentIsList = isListNode(parentList);
  1178. if (parentIsList && parentList.nodeName === listName && !hasListStyleDetail(detail)) {
  1179. flattenListSelection(editor);
  1180. } else {
  1181. applyList(editor, listName, detail);
  1182. const bookmark = createBookmark(editor.selection.getRng());
  1183. const allLists = parentIsList ? [
  1184. parentList,
  1185. ...lists
  1186. ] : lists;
  1187. global$1.each(allLists, elm => {
  1188. updateList$1(editor, elm, listName, detail);
  1189. });
  1190. editor.selection.setRng(resolveBookmark(bookmark));
  1191. }
  1192. };
  1193. const hasListStyleDetail = detail => {
  1194. return 'list-style-type' in detail;
  1195. };
  1196. const toggleSingleList = (editor, parentList, listName, detail) => {
  1197. if (parentList === editor.getBody()) {
  1198. return;
  1199. }
  1200. if (parentList) {
  1201. if (parentList.nodeName === listName && !hasListStyleDetail(detail) && !isCustomList(parentList)) {
  1202. flattenListSelection(editor);
  1203. } else {
  1204. const bookmark = createBookmark(editor.selection.getRng());
  1205. updateListWithDetails(editor.dom, parentList, detail);
  1206. const newList = editor.dom.rename(parentList, listName);
  1207. mergeWithAdjacentLists(editor.dom, newList);
  1208. editor.selection.setRng(resolveBookmark(bookmark));
  1209. applyList(editor, listName, detail);
  1210. fireListEvent(editor, listToggleActionFromListName(listName), newList);
  1211. }
  1212. } else {
  1213. applyList(editor, listName, detail);
  1214. fireListEvent(editor, listToggleActionFromListName(listName), parentList);
  1215. }
  1216. };
  1217. const toggleList = (editor, listName, _detail) => {
  1218. const parentList = getParentList(editor);
  1219. const selectedSubLists = getSelectedSubLists(editor);
  1220. const detail = isObject(_detail) ? _detail : {};
  1221. if (selectedSubLists.length > 0) {
  1222. toggleMultipleLists(editor, parentList, selectedSubLists, listName, detail);
  1223. } else {
  1224. toggleSingleList(editor, parentList, listName, detail);
  1225. }
  1226. };
  1227. const DOM = global$2.DOM;
  1228. const normalizeList = (dom, list) => {
  1229. const parentNode = list.parentNode;
  1230. if (parentNode.nodeName === 'LI' && parentNode.firstChild === list) {
  1231. const sibling = parentNode.previousSibling;
  1232. if (sibling && sibling.nodeName === 'LI') {
  1233. sibling.appendChild(list);
  1234. if (isEmpty$1(dom, parentNode)) {
  1235. DOM.remove(parentNode);
  1236. }
  1237. } else {
  1238. DOM.setStyle(parentNode, 'listStyleType', 'none');
  1239. }
  1240. }
  1241. if (isListNode(parentNode)) {
  1242. const sibling = parentNode.previousSibling;
  1243. if (sibling && sibling.nodeName === 'LI') {
  1244. sibling.appendChild(list);
  1245. }
  1246. }
  1247. };
  1248. const normalizeLists = (dom, element) => {
  1249. const lists = global$1.grep(dom.select('ol,ul', element));
  1250. global$1.each(lists, list => {
  1251. normalizeList(dom, list);
  1252. });
  1253. };
  1254. const findNextCaretContainer = (editor, rng, isForward, root) => {
  1255. let node = rng.startContainer;
  1256. const offset = rng.startOffset;
  1257. if (isTextNode(node) && (isForward ? offset < node.data.length : offset > 0)) {
  1258. return node;
  1259. }
  1260. const nonEmptyBlocks = editor.schema.getNonEmptyElements();
  1261. if (node.nodeType === 1) {
  1262. node = global$5.getNode(node, offset);
  1263. }
  1264. const walker = new global$4(node, root);
  1265. if (isForward) {
  1266. if (isBogusBr(editor.dom, node)) {
  1267. walker.next();
  1268. }
  1269. }
  1270. while (node = walker[isForward ? 'next' : 'prev2']()) {
  1271. if (node.nodeName === 'LI' && !node.hasChildNodes()) {
  1272. return node;
  1273. }
  1274. if (nonEmptyBlocks[node.nodeName]) {
  1275. return node;
  1276. }
  1277. if (isTextNode(node) && node.data.length > 0) {
  1278. return node;
  1279. }
  1280. }
  1281. };
  1282. const hasOnlyOneBlockChild = (dom, elm) => {
  1283. const childNodes = elm.childNodes;
  1284. return childNodes.length === 1 && !isListNode(childNodes[0]) && dom.isBlock(childNodes[0]);
  1285. };
  1286. const unwrapSingleBlockChild = (dom, elm) => {
  1287. if (hasOnlyOneBlockChild(dom, elm)) {
  1288. dom.remove(elm.firstChild, true);
  1289. }
  1290. };
  1291. const moveChildren = (dom, fromElm, toElm) => {
  1292. let node;
  1293. const targetElm = hasOnlyOneBlockChild(dom, toElm) ? toElm.firstChild : toElm;
  1294. unwrapSingleBlockChild(dom, fromElm);
  1295. if (!isEmpty$1(dom, fromElm, true)) {
  1296. while (node = fromElm.firstChild) {
  1297. targetElm.appendChild(node);
  1298. }
  1299. }
  1300. };
  1301. const mergeLiElements = (dom, fromElm, toElm) => {
  1302. let listNode;
  1303. const ul = fromElm.parentNode;
  1304. if (!isChildOfBody(dom, fromElm) || !isChildOfBody(dom, toElm)) {
  1305. return;
  1306. }
  1307. if (isListNode(toElm.lastChild)) {
  1308. listNode = toElm.lastChild;
  1309. }
  1310. if (ul === toElm.lastChild) {
  1311. if (isBr(ul.previousSibling)) {
  1312. dom.remove(ul.previousSibling);
  1313. }
  1314. }
  1315. const node = toElm.lastChild;
  1316. if (node && isBr(node) && fromElm.hasChildNodes()) {
  1317. dom.remove(node);
  1318. }
  1319. if (isEmpty$1(dom, toElm, true)) {
  1320. empty(SugarElement.fromDom(toElm));
  1321. }
  1322. moveChildren(dom, fromElm, toElm);
  1323. if (listNode) {
  1324. toElm.appendChild(listNode);
  1325. }
  1326. const contains$1 = contains(SugarElement.fromDom(toElm), SugarElement.fromDom(fromElm));
  1327. const nestedLists = contains$1 ? dom.getParents(fromElm, isListNode, toElm) : [];
  1328. dom.remove(fromElm);
  1329. each$1(nestedLists, list => {
  1330. if (isEmpty$1(dom, list) && list !== dom.getRoot()) {
  1331. dom.remove(list);
  1332. }
  1333. });
  1334. };
  1335. const mergeIntoEmptyLi = (editor, fromLi, toLi) => {
  1336. empty(SugarElement.fromDom(toLi));
  1337. mergeLiElements(editor.dom, fromLi, toLi);
  1338. editor.selection.setCursorLocation(toLi, 0);
  1339. };
  1340. const mergeForward = (editor, rng, fromLi, toLi) => {
  1341. const dom = editor.dom;
  1342. if (dom.isEmpty(toLi)) {
  1343. mergeIntoEmptyLi(editor, fromLi, toLi);
  1344. } else {
  1345. const bookmark = createBookmark(rng);
  1346. mergeLiElements(dom, fromLi, toLi);
  1347. editor.selection.setRng(resolveBookmark(bookmark));
  1348. }
  1349. };
  1350. const mergeBackward = (editor, rng, fromLi, toLi) => {
  1351. const bookmark = createBookmark(rng);
  1352. mergeLiElements(editor.dom, fromLi, toLi);
  1353. const resolvedBookmark = resolveBookmark(bookmark);
  1354. editor.selection.setRng(resolvedBookmark);
  1355. };
  1356. const backspaceDeleteFromListToListCaret = (editor, isForward) => {
  1357. const dom = editor.dom, selection = editor.selection;
  1358. const selectionStartElm = selection.getStart();
  1359. const root = getClosestListRootElm(editor, selectionStartElm);
  1360. const li = dom.getParent(selection.getStart(), 'LI', root);
  1361. if (li) {
  1362. const ul = li.parentNode;
  1363. if (ul === editor.getBody() && isEmpty$1(dom, ul)) {
  1364. return true;
  1365. }
  1366. const rng = normalizeRange(selection.getRng());
  1367. const otherLi = dom.getParent(findNextCaretContainer(editor, rng, isForward, root), 'LI', root);
  1368. if (otherLi && otherLi !== li) {
  1369. editor.undoManager.transact(() => {
  1370. if (isForward) {
  1371. mergeForward(editor, rng, otherLi, li);
  1372. } else {
  1373. if (isFirstChild(li)) {
  1374. outdentListSelection(editor);
  1375. } else {
  1376. mergeBackward(editor, rng, li, otherLi);
  1377. }
  1378. }
  1379. });
  1380. return true;
  1381. } else if (!otherLi) {
  1382. if (!isForward && rng.startOffset === 0 && rng.endOffset === 0) {
  1383. editor.undoManager.transact(() => {
  1384. flattenListSelection(editor);
  1385. });
  1386. return true;
  1387. }
  1388. }
  1389. }
  1390. return false;
  1391. };
  1392. const removeBlock = (dom, block, root) => {
  1393. const parentBlock = dom.getParent(block.parentNode, dom.isBlock, root);
  1394. dom.remove(block);
  1395. if (parentBlock && dom.isEmpty(parentBlock)) {
  1396. dom.remove(parentBlock);
  1397. }
  1398. };
  1399. const backspaceDeleteIntoListCaret = (editor, isForward) => {
  1400. const dom = editor.dom;
  1401. const selectionStartElm = editor.selection.getStart();
  1402. const root = getClosestListRootElm(editor, selectionStartElm);
  1403. const block = dom.getParent(selectionStartElm, dom.isBlock, root);
  1404. if (block && dom.isEmpty(block)) {
  1405. const rng = normalizeRange(editor.selection.getRng());
  1406. const otherLi = dom.getParent(findNextCaretContainer(editor, rng, isForward, root), 'LI', root);
  1407. if (otherLi) {
  1408. editor.undoManager.transact(() => {
  1409. removeBlock(dom, block, root);
  1410. mergeWithAdjacentLists(dom, otherLi.parentNode);
  1411. editor.selection.select(otherLi, true);
  1412. editor.selection.collapse(isForward);
  1413. });
  1414. return true;
  1415. }
  1416. }
  1417. return false;
  1418. };
  1419. const backspaceDeleteCaret = (editor, isForward) => {
  1420. return backspaceDeleteFromListToListCaret(editor, isForward) || backspaceDeleteIntoListCaret(editor, isForward);
  1421. };
  1422. const backspaceDeleteRange = editor => {
  1423. const selectionStartElm = editor.selection.getStart();
  1424. const root = getClosestListRootElm(editor, selectionStartElm);
  1425. const startListParent = editor.dom.getParent(selectionStartElm, 'LI,DT,DD', root);
  1426. if (startListParent || getSelectedListItems(editor).length > 0) {
  1427. editor.undoManager.transact(() => {
  1428. editor.execCommand('Delete');
  1429. normalizeLists(editor.dom, editor.getBody());
  1430. });
  1431. return true;
  1432. }
  1433. return false;
  1434. };
  1435. const backspaceDelete = (editor, isForward) => {
  1436. return editor.selection.isCollapsed() ? backspaceDeleteCaret(editor, isForward) : backspaceDeleteRange(editor);
  1437. };
  1438. const setup$1 = editor => {
  1439. editor.on('keydown', e => {
  1440. if (e.keyCode === global$3.BACKSPACE) {
  1441. if (backspaceDelete(editor, false)) {
  1442. e.preventDefault();
  1443. }
  1444. } else if (e.keyCode === global$3.DELETE) {
  1445. if (backspaceDelete(editor, true)) {
  1446. e.preventDefault();
  1447. }
  1448. }
  1449. });
  1450. };
  1451. const get = editor => ({
  1452. backspaceDelete: isForward => {
  1453. backspaceDelete(editor, isForward);
  1454. }
  1455. });
  1456. const updateList = (editor, update) => {
  1457. const parentList = getParentList(editor);
  1458. editor.undoManager.transact(() => {
  1459. if (isObject(update.styles)) {
  1460. editor.dom.setStyles(parentList, update.styles);
  1461. }
  1462. if (isObject(update.attrs)) {
  1463. each(update.attrs, (v, k) => editor.dom.setAttrib(parentList, k, v));
  1464. }
  1465. });
  1466. };
  1467. const parseAlphabeticBase26 = str => {
  1468. const chars = reverse(trim(str).split(''));
  1469. const values = map(chars, (char, i) => {
  1470. const charValue = char.toUpperCase().charCodeAt(0) - 'A'.charCodeAt(0) + 1;
  1471. return Math.pow(26, i) * charValue;
  1472. });
  1473. return foldl(values, (sum, v) => sum + v, 0);
  1474. };
  1475. const composeAlphabeticBase26 = value => {
  1476. value--;
  1477. if (value < 0) {
  1478. return '';
  1479. } else {
  1480. const remainder = value % 26;
  1481. const quotient = Math.floor(value / 26);
  1482. const rest = composeAlphabeticBase26(quotient);
  1483. const char = String.fromCharCode('A'.charCodeAt(0) + remainder);
  1484. return rest + char;
  1485. }
  1486. };
  1487. const isUppercase = str => /^[A-Z]+$/.test(str);
  1488. const isLowercase = str => /^[a-z]+$/.test(str);
  1489. const isNumeric = str => /^[0-9]+$/.test(str);
  1490. const deduceListType = start => {
  1491. if (isNumeric(start)) {
  1492. return 2;
  1493. } else if (isUppercase(start)) {
  1494. return 0;
  1495. } else if (isLowercase(start)) {
  1496. return 1;
  1497. } else if (isEmpty(start)) {
  1498. return 3;
  1499. } else {
  1500. return 4;
  1501. }
  1502. };
  1503. const parseStartValue = start => {
  1504. switch (deduceListType(start)) {
  1505. case 2:
  1506. return Optional.some({
  1507. listStyleType: Optional.none(),
  1508. start
  1509. });
  1510. case 0:
  1511. return Optional.some({
  1512. listStyleType: Optional.some('upper-alpha'),
  1513. start: parseAlphabeticBase26(start).toString()
  1514. });
  1515. case 1:
  1516. return Optional.some({
  1517. listStyleType: Optional.some('lower-alpha'),
  1518. start: parseAlphabeticBase26(start).toString()
  1519. });
  1520. case 3:
  1521. return Optional.some({
  1522. listStyleType: Optional.none(),
  1523. start: ''
  1524. });
  1525. case 4:
  1526. return Optional.none();
  1527. }
  1528. };
  1529. const parseDetail = detail => {
  1530. const start = parseInt(detail.start, 10);
  1531. if (is(detail.listStyleType, 'upper-alpha')) {
  1532. return composeAlphabeticBase26(start);
  1533. } else if (is(detail.listStyleType, 'lower-alpha')) {
  1534. return composeAlphabeticBase26(start).toLowerCase();
  1535. } else {
  1536. return detail.start;
  1537. }
  1538. };
  1539. const open = editor => {
  1540. const currentList = getParentList(editor);
  1541. if (!isOlNode(currentList)) {
  1542. return;
  1543. }
  1544. editor.windowManager.open({
  1545. title: 'List Properties',
  1546. body: {
  1547. type: 'panel',
  1548. items: [{
  1549. type: 'input',
  1550. name: 'start',
  1551. label: 'Start list at number',
  1552. inputMode: 'numeric'
  1553. }]
  1554. },
  1555. initialData: {
  1556. start: parseDetail({
  1557. start: editor.dom.getAttrib(currentList, 'start', '1'),
  1558. listStyleType: Optional.some(editor.dom.getStyle(currentList, 'list-style-type'))
  1559. })
  1560. },
  1561. buttons: [
  1562. {
  1563. type: 'cancel',
  1564. name: 'cancel',
  1565. text: 'Cancel'
  1566. },
  1567. {
  1568. type: 'submit',
  1569. name: 'save',
  1570. text: 'Save',
  1571. primary: true
  1572. }
  1573. ],
  1574. onSubmit: api => {
  1575. const data = api.getData();
  1576. parseStartValue(data.start).each(detail => {
  1577. editor.execCommand('mceListUpdate', false, {
  1578. attrs: { start: detail.start === '1' ? '' : detail.start },
  1579. styles: { 'list-style-type': detail.listStyleType.getOr('') }
  1580. });
  1581. });
  1582. api.close();
  1583. }
  1584. });
  1585. };
  1586. const queryListCommandState = (editor, listName) => () => {
  1587. const parentList = getParentList(editor);
  1588. return parentList && parentList.nodeName === listName;
  1589. };
  1590. const registerDialog = editor => {
  1591. editor.addCommand('mceListProps', () => {
  1592. open(editor);
  1593. });
  1594. };
  1595. const register$2 = editor => {
  1596. editor.on('BeforeExecCommand', e => {
  1597. const cmd = e.command.toLowerCase();
  1598. if (cmd === 'indent') {
  1599. indentListSelection(editor);
  1600. } else if (cmd === 'outdent') {
  1601. outdentListSelection(editor);
  1602. }
  1603. });
  1604. editor.addCommand('InsertUnorderedList', (ui, detail) => {
  1605. toggleList(editor, 'UL', detail);
  1606. });
  1607. editor.addCommand('InsertOrderedList', (ui, detail) => {
  1608. toggleList(editor, 'OL', detail);
  1609. });
  1610. editor.addCommand('InsertDefinitionList', (ui, detail) => {
  1611. toggleList(editor, 'DL', detail);
  1612. });
  1613. editor.addCommand('RemoveList', () => {
  1614. flattenListSelection(editor);
  1615. });
  1616. registerDialog(editor);
  1617. editor.addCommand('mceListUpdate', (ui, detail) => {
  1618. if (isObject(detail)) {
  1619. updateList(editor, detail);
  1620. }
  1621. });
  1622. editor.addQueryStateHandler('InsertUnorderedList', queryListCommandState(editor, 'UL'));
  1623. editor.addQueryStateHandler('InsertOrderedList', queryListCommandState(editor, 'OL'));
  1624. editor.addQueryStateHandler('InsertDefinitionList', queryListCommandState(editor, 'DL'));
  1625. };
  1626. const setupTabKey = editor => {
  1627. editor.on('keydown', e => {
  1628. if (e.keyCode !== global$3.TAB || global$3.metaKeyPressed(e)) {
  1629. return;
  1630. }
  1631. editor.undoManager.transact(() => {
  1632. if (e.shiftKey ? outdentListSelection(editor) : indentListSelection(editor)) {
  1633. e.preventDefault();
  1634. }
  1635. });
  1636. });
  1637. };
  1638. const setup = editor => {
  1639. if (shouldIndentOnTab(editor)) {
  1640. setupTabKey(editor);
  1641. }
  1642. setup$1(editor);
  1643. };
  1644. const register$1 = editor => {
  1645. const exec = command => () => editor.execCommand(command);
  1646. if (!editor.hasPlugin('advlist')) {
  1647. editor.ui.registry.addToggleButton('numlist', {
  1648. icon: 'ordered-list',
  1649. active: false,
  1650. tooltip: 'Numbered list',
  1651. onAction: exec('InsertOrderedList'),
  1652. onSetup: api => listState(editor, 'OL', api.setActive)
  1653. });
  1654. editor.ui.registry.addToggleButton('bullist', {
  1655. icon: 'unordered-list',
  1656. active: false,
  1657. tooltip: 'Bullet list',
  1658. onAction: exec('InsertUnorderedList'),
  1659. onSetup: api => listState(editor, 'UL', api.setActive)
  1660. });
  1661. }
  1662. };
  1663. const register = editor => {
  1664. const listProperties = {
  1665. text: 'List properties...',
  1666. icon: 'ordered-list',
  1667. onAction: () => editor.execCommand('mceListProps'),
  1668. onSetup: api => listState(editor, 'OL', api.setEnabled)
  1669. };
  1670. editor.ui.registry.addMenuItem('listprops', listProperties);
  1671. editor.ui.registry.addContextMenu('lists', {
  1672. update: node => {
  1673. const parentList = getParentList(editor, node);
  1674. return isOlNode(parentList) ? ['listprops'] : [];
  1675. }
  1676. });
  1677. };
  1678. var Plugin = () => {
  1679. global$6.add('lists', editor => {
  1680. register$3(editor);
  1681. if (editor.hasPlugin('rtc', true) === false) {
  1682. setup(editor);
  1683. register$2(editor);
  1684. } else {
  1685. registerDialog(editor);
  1686. }
  1687. register$1(editor);
  1688. register(editor);
  1689. return get(editor);
  1690. });
  1691. };
  1692. Plugin();
  1693. })();