plugin.js 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /**
  2. * TinyMCE version 8.0.2 (2025-08-14)
  3. */
  4. (function () {
  5. 'use strict';
  6. var global$4 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  7. /* eslint-disable @typescript-eslint/no-wrapper-object-types */
  8. const hasProto = (v, constructor, predicate) => {
  9. var _a;
  10. if (predicate(v, constructor.prototype)) {
  11. return true;
  12. }
  13. else {
  14. // String-based fallback time
  15. return ((_a = v.constructor) === null || _a === void 0 ? void 0 : _a.name) === constructor.name;
  16. }
  17. };
  18. const typeOf = (x) => {
  19. const t = typeof x;
  20. if (x === null) {
  21. return 'null';
  22. }
  23. else if (t === 'object' && Array.isArray(x)) {
  24. return 'array';
  25. }
  26. else if (t === 'object' && hasProto(x, String, (o, proto) => proto.isPrototypeOf(o))) {
  27. return 'string';
  28. }
  29. else {
  30. return t;
  31. }
  32. };
  33. const isType$1 = (type) => (value) => typeOf(value) === type;
  34. const isSimpleType = (type) => (value) => typeof value === type;
  35. const isString = isType$1('string');
  36. const isBoolean = isSimpleType('boolean');
  37. const isNullable = (a) => a === null || a === undefined;
  38. const isNonNullable = (a) => !isNullable(a);
  39. const isFunction = isSimpleType('function');
  40. const isNumber = isSimpleType('number');
  41. /** Compose two unary functions. Similar to compose, but avoids using Function.prototype.apply. */
  42. const compose1 = (fbc, fab) => (a) => fbc(fab(a));
  43. const constant = (value) => {
  44. return () => {
  45. return value;
  46. };
  47. };
  48. const tripleEquals = (a, b) => {
  49. return a === b;
  50. };
  51. const never = constant(false);
  52. /**
  53. * The `Optional` type represents a value (of any type) that potentially does
  54. * not exist. Any `Optional<T>` can either be a `Some<T>` (in which case the
  55. * value does exist) or a `None` (in which case the value does not exist). This
  56. * module defines a whole lot of FP-inspired utility functions for dealing with
  57. * `Optional` objects.
  58. *
  59. * Comparison with null or undefined:
  60. * - We don't get fancy null coalescing operators with `Optional`
  61. * - We do get fancy helper functions with `Optional`
  62. * - `Optional` support nesting, and allow for the type to still be nullable (or
  63. * another `Optional`)
  64. * - There is no option to turn off strict-optional-checks like there is for
  65. * strict-null-checks
  66. */
  67. class Optional {
  68. // The internal representation has a `tag` and a `value`, but both are
  69. // private: able to be console.logged, but not able to be accessed by code
  70. constructor(tag, value) {
  71. this.tag = tag;
  72. this.value = value;
  73. }
  74. // --- Identities ---
  75. /**
  76. * Creates a new `Optional<T>` that **does** contain a value.
  77. */
  78. static some(value) {
  79. return new Optional(true, value);
  80. }
  81. /**
  82. * Create a new `Optional<T>` that **does not** contain a value. `T` can be
  83. * any type because we don't actually have a `T`.
  84. */
  85. static none() {
  86. return Optional.singletonNone;
  87. }
  88. /**
  89. * Perform a transform on an `Optional` type. Regardless of whether this
  90. * `Optional` contains a value or not, `fold` will return a value of type `U`.
  91. * If this `Optional` does not contain a value, the `U` will be created by
  92. * calling `onNone`. If this `Optional` does contain a value, the `U` will be
  93. * created by calling `onSome`.
  94. *
  95. * For the FP enthusiasts in the room, this function:
  96. * 1. Could be used to implement all of the functions below
  97. * 2. Forms a catamorphism
  98. */
  99. fold(onNone, onSome) {
  100. if (this.tag) {
  101. return onSome(this.value);
  102. }
  103. else {
  104. return onNone();
  105. }
  106. }
  107. /**
  108. * Determine if this `Optional` object contains a value.
  109. */
  110. isSome() {
  111. return this.tag;
  112. }
  113. /**
  114. * Determine if this `Optional` object **does not** contain a value.
  115. */
  116. isNone() {
  117. return !this.tag;
  118. }
  119. // --- Functor (name stolen from Haskell / maths) ---
  120. /**
  121. * Perform a transform on an `Optional` object, **if** there is a value. If
  122. * you provide a function to turn a T into a U, this is the function you use
  123. * to turn an `Optional<T>` into an `Optional<U>`. If this **does** contain
  124. * a value then the output will also contain a value (that value being the
  125. * output of `mapper(this.value)`), and if this **does not** contain a value
  126. * then neither will the output.
  127. */
  128. map(mapper) {
  129. if (this.tag) {
  130. return Optional.some(mapper(this.value));
  131. }
  132. else {
  133. return Optional.none();
  134. }
  135. }
  136. // --- Monad (name stolen from Haskell / maths) ---
  137. /**
  138. * Perform a transform on an `Optional` object, **if** there is a value.
  139. * Unlike `map`, here the transform itself also returns an `Optional`.
  140. */
  141. bind(binder) {
  142. if (this.tag) {
  143. return binder(this.value);
  144. }
  145. else {
  146. return Optional.none();
  147. }
  148. }
  149. // --- Traversable (name stolen from Haskell / maths) ---
  150. /**
  151. * For a given predicate, this function finds out if there **exists** a value
  152. * inside this `Optional` object that meets the predicate. In practice, this
  153. * means that for `Optional`s that do not contain a value it returns false (as
  154. * no predicate-meeting value exists).
  155. */
  156. exists(predicate) {
  157. return this.tag && predicate(this.value);
  158. }
  159. /**
  160. * For a given predicate, this function finds out if **all** the values inside
  161. * this `Optional` object meet the predicate. In practice, this means that
  162. * for `Optional`s that do not contain a value it returns true (as all 0
  163. * objects do meet the predicate).
  164. */
  165. forall(predicate) {
  166. return !this.tag || predicate(this.value);
  167. }
  168. filter(predicate) {
  169. if (!this.tag || predicate(this.value)) {
  170. return this;
  171. }
  172. else {
  173. return Optional.none();
  174. }
  175. }
  176. // --- Getters ---
  177. /**
  178. * Get the value out of the inside of the `Optional` object, using a default
  179. * `replacement` value if the provided `Optional` object does not contain a
  180. * value.
  181. */
  182. getOr(replacement) {
  183. return this.tag ? this.value : replacement;
  184. }
  185. /**
  186. * Get the value out of the inside of the `Optional` object, using a default
  187. * `replacement` value if the provided `Optional` object does not contain a
  188. * value. Unlike `getOr`, in this method the `replacement` object is also
  189. * `Optional` - meaning that this method will always return an `Optional`.
  190. */
  191. or(replacement) {
  192. return this.tag ? this : replacement;
  193. }
  194. /**
  195. * Get the value out of the inside of the `Optional` object, using a default
  196. * `replacement` value if the provided `Optional` object does not contain a
  197. * value. Unlike `getOr`, in this method the `replacement` value is
  198. * "thunked" - that is to say that you don't pass a value to `getOrThunk`, you
  199. * pass a function which (if called) will **return** the `value` you want to
  200. * use.
  201. */
  202. getOrThunk(thunk) {
  203. return this.tag ? this.value : thunk();
  204. }
  205. /**
  206. * Get the value out of the inside of the `Optional` object, using a default
  207. * `replacement` value if the provided Optional object does not contain a
  208. * value.
  209. *
  210. * Unlike `or`, in this method the `replacement` value is "thunked" - that is
  211. * to say that you don't pass a value to `orThunk`, you pass a function which
  212. * (if called) will **return** the `value` you want to use.
  213. *
  214. * Unlike `getOrThunk`, in this method the `replacement` value is also
  215. * `Optional`, meaning that this method will always return an `Optional`.
  216. */
  217. orThunk(thunk) {
  218. return this.tag ? this : thunk();
  219. }
  220. /**
  221. * Get the value out of the inside of the `Optional` object, throwing an
  222. * exception if the provided `Optional` object does not contain a value.
  223. *
  224. * WARNING:
  225. * You should only be using this function if you know that the `Optional`
  226. * object **is not** empty (otherwise you're throwing exceptions in production
  227. * code, which is bad).
  228. *
  229. * In tests this is more acceptable.
  230. *
  231. * Prefer other methods to this, such as `.each`.
  232. */
  233. getOrDie(message) {
  234. if (!this.tag) {
  235. throw new Error(message !== null && message !== void 0 ? message : 'Called getOrDie on None');
  236. }
  237. else {
  238. return this.value;
  239. }
  240. }
  241. // --- Interop with null and undefined ---
  242. /**
  243. * Creates an `Optional` value from a nullable (or undefined-able) input.
  244. * Null, or undefined, is converted to `None`, and anything else is converted
  245. * to `Some`.
  246. */
  247. static from(value) {
  248. return isNonNullable(value) ? Optional.some(value) : Optional.none();
  249. }
  250. /**
  251. * Converts an `Optional` to a nullable type, by getting the value if it
  252. * exists, or returning `null` if it does not.
  253. */
  254. getOrNull() {
  255. return this.tag ? this.value : null;
  256. }
  257. /**
  258. * Converts an `Optional` to an undefined-able type, by getting the value if
  259. * it exists, or returning `undefined` if it does not.
  260. */
  261. getOrUndefined() {
  262. return this.value;
  263. }
  264. // --- Utilities ---
  265. /**
  266. * If the `Optional` contains a value, perform an action on that value.
  267. * Unlike the rest of the methods on this type, `.each` has side-effects. If
  268. * you want to transform an `Optional<T>` **into** something, then this is not
  269. * the method for you. If you want to use an `Optional<T>` to **do**
  270. * something, then this is the method for you - provided you're okay with not
  271. * doing anything in the case where the `Optional` doesn't have a value inside
  272. * it. If you're not sure whether your use-case fits into transforming
  273. * **into** something or **doing** something, check whether it has a return
  274. * value. If it does, you should be performing a transform.
  275. */
  276. each(worker) {
  277. if (this.tag) {
  278. worker(this.value);
  279. }
  280. }
  281. /**
  282. * Turn the `Optional` object into an array that contains all of the values
  283. * stored inside the `Optional`. In practice, this means the output will have
  284. * either 0 or 1 elements.
  285. */
  286. toArray() {
  287. return this.tag ? [this.value] : [];
  288. }
  289. /**
  290. * Turn the `Optional` object into a string for debugging or printing. Not
  291. * recommended for production code, but good for debugging. Also note that
  292. * these days an `Optional` object can be logged to the console directly, and
  293. * its inner value (if it exists) will be visible.
  294. */
  295. toString() {
  296. return this.tag ? `some(${this.value})` : 'none()';
  297. }
  298. }
  299. // Sneaky optimisation: every instance of Optional.none is identical, so just
  300. // reuse the same object
  301. Optional.singletonNone = new Optional(false);
  302. const nativeIndexOf = Array.prototype.indexOf;
  303. const rawIndexOf = (ts, t) => nativeIndexOf.call(ts, t);
  304. const contains = (xs, x) => rawIndexOf(xs, x) > -1;
  305. const map = (xs, f) => {
  306. // pre-allocating array size when it's guaranteed to be known
  307. // http://jsperf.com/push-allocated-vs-dynamic/22
  308. const len = xs.length;
  309. const r = new Array(len);
  310. for (let i = 0; i < len; i++) {
  311. const x = xs[i];
  312. r[i] = f(x, i);
  313. }
  314. return r;
  315. };
  316. // Unwound implementing other functions in terms of each.
  317. // The code size is roughly the same, and it should allow for better optimisation.
  318. // const each = function<T, U>(xs: T[], f: (x: T, i?: number, xs?: T[]) => void): void {
  319. const each$1 = (xs, f) => {
  320. for (let i = 0, len = xs.length; i < len; i++) {
  321. const x = xs[i];
  322. f(x, i);
  323. }
  324. };
  325. const filter = (xs, pred) => {
  326. const r = [];
  327. for (let i = 0, len = xs.length; i < len; i++) {
  328. const x = xs[i];
  329. if (pred(x, i)) {
  330. r.push(x);
  331. }
  332. }
  333. return r;
  334. };
  335. const foldl = (xs, f, acc) => {
  336. each$1(xs, (x, i) => {
  337. acc = f(acc, x, i);
  338. });
  339. return acc;
  340. };
  341. // There are many variations of Object iteration that are faster than the 'for-in' style:
  342. // http://jsperf.com/object-keys-iteration/107
  343. //
  344. // Use the native keys if it is available (IE9+), otherwise fall back to manually filtering
  345. const keys = Object.keys;
  346. const each = (obj, f) => {
  347. const props = keys(obj);
  348. for (let k = 0, len = props.length; k < len; k++) {
  349. const i = props[k];
  350. const x = obj[i];
  351. f(x, i);
  352. }
  353. };
  354. /**
  355. * Adds two numbers, and wrap to a range.
  356. * If the result overflows to the right, snap to the left.
  357. * If the result overflows to the left, snap to the right.
  358. */
  359. // the division is meant to get a number between 0 and 1 for more information check this discussion: https://stackoverflow.com/questions/58285941/how-to-replace-math-random-with-crypto-getrandomvalues-and-keep-same-result
  360. const random = () => window.crypto.getRandomValues(new Uint32Array(1))[0] / 4294967295;
  361. /**
  362. * Generate a unique identifier.
  363. *
  364. * The unique portion of the identifier only contains an underscore
  365. * and digits, so that it may safely be used within HTML attributes.
  366. *
  367. * The chance of generating a non-unique identifier has been minimized
  368. * by combining the current time, a random number and a one-up counter.
  369. *
  370. * generate :: String -> String
  371. */
  372. let unique = 0;
  373. const generate = (prefix) => {
  374. const date = new Date();
  375. const time = date.getTime();
  376. const random$1 = Math.floor(random() * 1000000000);
  377. unique++;
  378. return prefix + '_' + random$1 + unique + String(time);
  379. };
  380. /**
  381. * **Is** the value stored inside this Optional object equal to `rhs`?
  382. */
  383. const is$2 = (lhs, rhs, comparator = tripleEquals) => lhs.exists((left) => comparator(left, rhs));
  384. const blank = (r) => (s) => s.replace(r, '');
  385. /** removes all leading and trailing spaces */
  386. const trim = blank(/^\s+|\s+$/g);
  387. const point = (element, offset) => ({
  388. element,
  389. offset
  390. });
  391. const fromHtml = (html, scope) => {
  392. const doc = scope || document;
  393. const div = doc.createElement('div');
  394. div.innerHTML = html;
  395. if (!div.hasChildNodes() || div.childNodes.length > 1) {
  396. const message = 'HTML does not have a single root node';
  397. // eslint-disable-next-line no-console
  398. console.error(message, html);
  399. throw new Error(message);
  400. }
  401. return fromDom(div.childNodes[0]);
  402. };
  403. const fromTag = (tag, scope) => {
  404. const doc = scope || document;
  405. const node = doc.createElement(tag);
  406. return fromDom(node);
  407. };
  408. const fromText = (text, scope) => {
  409. const doc = scope || document;
  410. const node = doc.createTextNode(text);
  411. return fromDom(node);
  412. };
  413. const fromDom = (node) => {
  414. // TODO: Consider removing this check, but left atm for safety
  415. if (node === null || node === undefined) {
  416. throw new Error('Node cannot be null or undefined');
  417. }
  418. return {
  419. dom: node
  420. };
  421. };
  422. const fromPoint = (docElm, x, y) => Optional.from(docElm.dom.elementFromPoint(x, y)).map(fromDom);
  423. // tslint:disable-next-line:variable-name
  424. const SugarElement = {
  425. fromHtml,
  426. fromTag,
  427. fromText,
  428. fromDom,
  429. fromPoint
  430. };
  431. const COMMENT = 8;
  432. const DOCUMENT = 9;
  433. const DOCUMENT_FRAGMENT = 11;
  434. const ELEMENT = 1;
  435. const TEXT = 3;
  436. const is$1 = (element, selector) => {
  437. const dom = element.dom;
  438. if (dom.nodeType !== ELEMENT) {
  439. return false;
  440. }
  441. else {
  442. const elem = dom;
  443. if (elem.matches !== undefined) {
  444. return elem.matches(selector);
  445. }
  446. else if (elem.msMatchesSelector !== undefined) {
  447. return elem.msMatchesSelector(selector);
  448. }
  449. else if (elem.webkitMatchesSelector !== undefined) {
  450. return elem.webkitMatchesSelector(selector);
  451. }
  452. else if (elem.mozMatchesSelector !== undefined) {
  453. // cast to any as mozMatchesSelector doesn't exist in TS DOM lib
  454. return elem.mozMatchesSelector(selector);
  455. }
  456. else {
  457. throw new Error('Browser lacks native selectors');
  458. } // unfortunately we can't throw this on startup :(
  459. }
  460. };
  461. const bypassSelector = (dom) =>
  462. // Only elements, documents and shadow roots support querySelector
  463. // shadow root element type is DOCUMENT_FRAGMENT
  464. dom.nodeType !== ELEMENT && dom.nodeType !== DOCUMENT && dom.nodeType !== DOCUMENT_FRAGMENT ||
  465. // IE fix for complex queries on empty nodes: http://jsfiddle.net/spyder/fv9ptr5L/
  466. dom.childElementCount === 0;
  467. const all = (selector, scope) => {
  468. const base = scope === undefined ? document : scope.dom;
  469. return bypassSelector(base) ? [] : map(base.querySelectorAll(selector), SugarElement.fromDom);
  470. };
  471. const one = (selector, scope) => {
  472. const base = scope === undefined ? document : scope.dom;
  473. return bypassSelector(base) ? Optional.none() : Optional.from(base.querySelector(selector)).map(SugarElement.fromDom);
  474. };
  475. const eq = (e1, e2) => e1.dom === e2.dom;
  476. const is = is$1;
  477. const name = (element) => {
  478. const r = element.dom.nodeName;
  479. return r.toLowerCase();
  480. };
  481. const type = (element) => element.dom.nodeType;
  482. const isType = (t) => (element) => type(element) === t;
  483. const isComment = (element) => type(element) === COMMENT || name(element) === '#comment';
  484. const isElement = isType(ELEMENT);
  485. const isText = isType(TEXT);
  486. const isDocument = isType(DOCUMENT);
  487. const isDocumentFragment = isType(DOCUMENT_FRAGMENT);
  488. /**
  489. * The document associated with the current element
  490. * NOTE: this will throw if the owner is null.
  491. */
  492. const owner = (element) => SugarElement.fromDom(element.dom.ownerDocument);
  493. /**
  494. * If the element is a document, return it. Otherwise, return its ownerDocument.
  495. * @param dos
  496. */
  497. const documentOrOwner = (dos) => isDocument(dos) ? dos : owner(dos);
  498. const parent = (element) => Optional.from(element.dom.parentNode).map(SugarElement.fromDom);
  499. const parents = (element, isRoot) => {
  500. const stop = isFunction(isRoot) ? isRoot : never;
  501. // This is used a *lot* so it needs to be performant, not recursive
  502. let dom = element.dom;
  503. const ret = [];
  504. while (dom.parentNode !== null && dom.parentNode !== undefined) {
  505. const rawParent = dom.parentNode;
  506. const p = SugarElement.fromDom(rawParent);
  507. ret.push(p);
  508. if (stop(p) === true) {
  509. break;
  510. }
  511. else {
  512. dom = rawParent;
  513. }
  514. }
  515. return ret;
  516. };
  517. const prevSibling = (element) => Optional.from(element.dom.previousSibling).map(SugarElement.fromDom);
  518. const nextSibling = (element) => Optional.from(element.dom.nextSibling).map(SugarElement.fromDom);
  519. const children = (element) => map(element.dom.childNodes, SugarElement.fromDom);
  520. const child = (element, index) => {
  521. const cs = element.dom.childNodes;
  522. return Optional.from(cs[index]).map(SugarElement.fromDom);
  523. };
  524. const firstChild = (element) => child(element, 0);
  525. /**
  526. * Is the element a ShadowRoot?
  527. *
  528. * Note: this is insufficient to test if any element is a shadow root, but it is sufficient to differentiate between
  529. * a Document and a ShadowRoot.
  530. */
  531. const isShadowRoot = (dos) => isDocumentFragment(dos) && isNonNullable(dos.dom.host);
  532. const getRootNode = (e) => SugarElement.fromDom(e.dom.getRootNode());
  533. /** If this element is in a ShadowRoot, return it. */
  534. const getShadowRoot = (e) => {
  535. const r = getRootNode(e);
  536. return isShadowRoot(r) ? Optional.some(r) : Optional.none();
  537. };
  538. /** Return the host of a ShadowRoot.
  539. *
  540. * This function will throw if Shadow DOM is unsupported in the browser, or if the host is null.
  541. * If you actually have a ShadowRoot, this shouldn't happen.
  542. */
  543. const getShadowHost = (e) => SugarElement.fromDom(e.dom.host);
  544. const before = (marker, element) => {
  545. const parent$1 = parent(marker);
  546. parent$1.each((v) => {
  547. v.dom.insertBefore(element.dom, marker.dom);
  548. });
  549. };
  550. const after$1 = (marker, element) => {
  551. const sibling = nextSibling(marker);
  552. sibling.fold(() => {
  553. const parent$1 = parent(marker);
  554. parent$1.each((v) => {
  555. append$1(v, element);
  556. });
  557. }, (v) => {
  558. before(v, element);
  559. });
  560. };
  561. const prepend = (parent, element) => {
  562. const firstChild$1 = firstChild(parent);
  563. firstChild$1.fold(() => {
  564. append$1(parent, element);
  565. }, (v) => {
  566. parent.dom.insertBefore(element.dom, v.dom);
  567. });
  568. };
  569. const append$1 = (parent, element) => {
  570. parent.dom.appendChild(element.dom);
  571. };
  572. const wrap = (element, wrapper) => {
  573. before(element, wrapper);
  574. append$1(wrapper, element);
  575. };
  576. const after = (marker, elements) => {
  577. each$1(elements, (x, i) => {
  578. const e = i === 0 ? marker : elements[i - 1];
  579. after$1(e, x);
  580. });
  581. };
  582. const append = (parent, elements) => {
  583. each$1(elements, (x) => {
  584. append$1(parent, x);
  585. });
  586. };
  587. const rawSet = (dom, key, value) => {
  588. /*
  589. * JQuery coerced everything to a string, and silently did nothing on text node/null/undefined.
  590. *
  591. * We fail on those invalid cases, only allowing numbers and booleans.
  592. */
  593. if (isString(value) || isBoolean(value) || isNumber(value)) {
  594. dom.setAttribute(key, value + '');
  595. }
  596. else {
  597. // eslint-disable-next-line no-console
  598. console.error('Invalid call to Attribute.set. Key ', key, ':: Value ', value, ':: Element ', dom);
  599. throw new Error('Attribute value was not simple');
  600. }
  601. };
  602. const set$2 = (element, key, value) => {
  603. rawSet(element.dom, key, value);
  604. };
  605. const setAll = (element, attrs) => {
  606. const dom = element.dom;
  607. each(attrs, (v, k) => {
  608. rawSet(dom, k, v);
  609. });
  610. };
  611. const get$2 = (element, key) => {
  612. const v = element.dom.getAttribute(key);
  613. // undefined is the more appropriate value for JS, and this matches JQuery
  614. return v === null ? undefined : v;
  615. };
  616. const getOpt = (element, key) => Optional.from(get$2(element, key));
  617. const remove$2 = (element, key) => {
  618. element.dom.removeAttribute(key);
  619. };
  620. const clone = (element) => foldl(element.dom.attributes, (acc, attr) => {
  621. acc[attr.name] = attr.value;
  622. return acc;
  623. }, {});
  624. const remove$1 = (element) => {
  625. const dom = element.dom;
  626. if (dom.parentNode !== null) {
  627. dom.parentNode.removeChild(dom);
  628. }
  629. };
  630. const unwrap = (wrapper) => {
  631. const children$1 = children(wrapper);
  632. if (children$1.length > 0) {
  633. after(wrapper, children$1);
  634. }
  635. remove$1(wrapper);
  636. };
  637. // some elements, such as mathml, don't have style attributes
  638. // others, such as angular elements, have style attributes that aren't a CSSStyleDeclaration
  639. const isSupported = (dom) => dom.style !== undefined && isFunction(dom.style.getPropertyValue);
  640. // Node.contains() is very, very, very good performance
  641. // http://jsperf.com/closest-vs-contains/5
  642. const inBody = (element) => {
  643. // Technically this is only required on IE, where contains() returns false for text nodes.
  644. // But it's cheap enough to run everywhere and Sugar doesn't have platform detection (yet).
  645. const dom = isText(element) ? element.dom.parentNode : element.dom;
  646. // use ownerDocument.body to ensure this works inside iframes.
  647. // Normally contains is bad because an element "contains" itself, but here we want that.
  648. if (dom === undefined || dom === null || dom.ownerDocument === null) {
  649. return false;
  650. }
  651. const doc = dom.ownerDocument;
  652. return getShadowRoot(SugarElement.fromDom(dom)).fold(() => doc.body.contains(dom), compose1(inBody, getShadowHost));
  653. };
  654. const internalSet = (dom, property, value) => {
  655. // This is going to hurt. Apologies.
  656. // JQuery coerces numbers to pixels for certain property names, and other times lets numbers through.
  657. // we're going to be explicit; strings only.
  658. if (!isString(value)) {
  659. // eslint-disable-next-line no-console
  660. console.error('Invalid call to CSS.set. Property ', property, ':: Value ', value, ':: Element ', dom);
  661. throw new Error('CSS value must be a string: ' + value);
  662. }
  663. // removed: support for dom().style[property] where prop is camel case instead of normal property name
  664. if (isSupported(dom)) {
  665. dom.style.setProperty(property, value);
  666. }
  667. };
  668. const internalRemove = (dom, property) => {
  669. /*
  670. * IE9 and above - MDN doesn't have details, but here's a couple of random internet claims
  671. *
  672. * http://help.dottoro.com/ljopsjck.php
  673. * http://stackoverflow.com/a/7901886/7546
  674. */
  675. if (isSupported(dom)) {
  676. dom.style.removeProperty(property);
  677. }
  678. };
  679. const set$1 = (element, property, value) => {
  680. const dom = element.dom;
  681. internalSet(dom, property, value);
  682. };
  683. /*
  684. * NOTE: For certain properties, this returns the "used value" which is subtly different to the "computed value" (despite calling getComputedStyle).
  685. * Blame CSS 2.0.
  686. *
  687. * https://developer.mozilla.org/en-US/docs/Web/CSS/used_value
  688. */
  689. const get$1 = (element, property) => {
  690. const dom = element.dom;
  691. /*
  692. * IE9 and above per
  693. * https://developer.mozilla.org/en/docs/Web/API/window.getComputedStyle
  694. *
  695. * Not in numerosity, because it doesn't memoize and looking this up dynamically in performance critical code would be horrendous.
  696. *
  697. * JQuery has some magic here for IE popups, but we don't really need that.
  698. * It also uses element.ownerDocument.defaultView to handle iframes but that hasn't been required since FF 3.6.
  699. */
  700. const styles = window.getComputedStyle(dom);
  701. const r = styles.getPropertyValue(property);
  702. // jquery-ism: If r is an empty string, check that the element is not in a document. If it isn't, return the raw value.
  703. // Turns out we do this a lot.
  704. return (r === '' && !inBody(element)) ? getUnsafeProperty(dom, property) : r;
  705. };
  706. // removed: support for dom().style[property] where prop is camel case instead of normal property name
  707. // empty string is what the browsers (IE11 and Chrome) return when the propertyValue doesn't exists.
  708. const getUnsafeProperty = (dom, property) => isSupported(dom) ? dom.style.getPropertyValue(property) : '';
  709. /*
  710. * Gets the raw value from the style attribute. Useful for retrieving "used values" from the DOM:
  711. * https://developer.mozilla.org/en-US/docs/Web/CSS/used_value
  712. *
  713. * Returns NONE if the property isn't set, or the value is an empty string.
  714. */
  715. const getRaw = (element, property) => {
  716. const dom = element.dom;
  717. const raw = getUnsafeProperty(dom, property);
  718. return Optional.from(raw).filter((r) => r.length > 0);
  719. };
  720. const remove = (element, property) => {
  721. const dom = element.dom;
  722. internalRemove(dom, property);
  723. if (is$2(getOpt(element, 'style').map(trim), '')) {
  724. // No more styles left, remove the style attribute as well
  725. remove$2(element, 'style');
  726. }
  727. };
  728. const NodeValue = (is, name) => {
  729. const get = (element) => {
  730. if (!is(element)) {
  731. throw new Error('Can only get ' + name + ' value of a ' + name + ' node');
  732. }
  733. return getOption(element).getOr('');
  734. };
  735. const getOption = (element) => is(element) ? Optional.from(element.dom.nodeValue) : Optional.none();
  736. const set = (element, value) => {
  737. if (!is(element)) {
  738. throw new Error('Can only set raw ' + name + ' value of a ' + name + ' node');
  739. }
  740. element.dom.nodeValue = value;
  741. };
  742. return {
  743. get,
  744. getOption,
  745. set
  746. };
  747. };
  748. const api = NodeValue(isText, 'text');
  749. const get = (element) => api.get(element);
  750. const set = (element, value) => api.set(element, value);
  751. var ClosestOrAncestor = (is, ancestor, scope, a, isRoot) => {
  752. if (is(scope, a)) {
  753. return Optional.some(scope);
  754. }
  755. else if (isFunction(isRoot) && isRoot(scope)) {
  756. return Optional.none();
  757. }
  758. else {
  759. return ancestor(scope, a, isRoot);
  760. }
  761. };
  762. const ancestor$1 = (scope, predicate, isRoot) => {
  763. let element = scope.dom;
  764. const stop = isFunction(isRoot) ? isRoot : never;
  765. while (element.parentNode) {
  766. element = element.parentNode;
  767. const el = SugarElement.fromDom(element);
  768. if (predicate(el)) {
  769. return Optional.some(el);
  770. }
  771. else if (stop(el)) {
  772. break;
  773. }
  774. }
  775. return Optional.none();
  776. };
  777. const ancestor = (scope, selector, isRoot) => ancestor$1(scope, (e) => is$1(e, selector), isRoot);
  778. const descendant = (scope, selector) => one(selector, scope);
  779. // Returns Some(closest ancestor element (sugared)) matching 'selector' up to isRoot, or None() otherwise
  780. const closest = (scope, selector, isRoot) => {
  781. const is = (element, selector) => is$1(element, selector);
  782. return ClosestOrAncestor(is, ancestor, scope, selector, isRoot);
  783. };
  784. const descendants$1 = (scope, predicate) => {
  785. let result = [];
  786. // Recurse.toArray() might help here
  787. each$1(children(scope), (x) => {
  788. if (predicate(x)) {
  789. result = result.concat([x]);
  790. }
  791. result = result.concat(descendants$1(x, predicate));
  792. });
  793. return result;
  794. };
  795. const descendants = (scope, selector) => all(selector, scope);
  796. var TagBoundaries = [
  797. 'body',
  798. 'p',
  799. 'div',
  800. 'article',
  801. 'aside',
  802. 'figcaption',
  803. 'figure',
  804. 'footer',
  805. 'header',
  806. 'nav',
  807. 'section',
  808. 'ol',
  809. 'ul',
  810. 'li',
  811. 'table',
  812. 'thead',
  813. 'tbody',
  814. 'tfoot',
  815. 'caption',
  816. 'tr',
  817. 'td',
  818. 'th',
  819. 'h1',
  820. 'h2',
  821. 'h3',
  822. 'h4',
  823. 'h5',
  824. 'h6',
  825. 'blockquote',
  826. 'pre',
  827. 'address'
  828. ];
  829. var DomUniverse = () => {
  830. const clone$1 = (element) => {
  831. return SugarElement.fromDom(element.dom.cloneNode(false));
  832. };
  833. const document = (element) => documentOrOwner(element).dom;
  834. const isBoundary = (element) => {
  835. if (!isElement(element)) {
  836. return false;
  837. }
  838. if (name(element) === 'body') {
  839. return true;
  840. }
  841. return contains(TagBoundaries, name(element));
  842. };
  843. const isEmptyTag = (element) => {
  844. if (!isElement(element)) {
  845. return false;
  846. }
  847. return contains(['br', 'img', 'hr', 'input'], name(element));
  848. };
  849. const isNonEditable = (element) => isElement(element) && get$2(element, 'contenteditable') === 'false';
  850. const comparePosition = (element, other) => {
  851. return element.dom.compareDocumentPosition(other.dom);
  852. };
  853. const copyAttributesTo = (source, destination) => {
  854. const as = clone(source);
  855. setAll(destination, as);
  856. };
  857. const isSpecial = (element) => {
  858. const tag = name(element);
  859. return contains([
  860. 'script', 'noscript', 'iframe', 'noframes', 'noembed', 'title', 'style', 'textarea', 'xmp'
  861. ], tag);
  862. };
  863. const getLanguage = (element) => isElement(element) ? getOpt(element, 'lang') : Optional.none();
  864. return {
  865. up: constant({
  866. selector: ancestor,
  867. closest: closest,
  868. predicate: ancestor$1,
  869. all: parents
  870. }),
  871. down: constant({
  872. selector: descendants,
  873. predicate: descendants$1
  874. }),
  875. styles: constant({
  876. get: get$1,
  877. getRaw: getRaw,
  878. set: set$1,
  879. remove: remove
  880. }),
  881. attrs: constant({
  882. get: get$2,
  883. set: set$2,
  884. remove: remove$2,
  885. copyTo: copyAttributesTo
  886. }),
  887. insert: constant({
  888. before: before,
  889. after: after$1,
  890. afterAll: after,
  891. append: append$1,
  892. appendAll: append,
  893. prepend: prepend,
  894. wrap: wrap
  895. }),
  896. remove: constant({
  897. unwrap: unwrap,
  898. remove: remove$1
  899. }),
  900. create: constant({
  901. nu: SugarElement.fromTag,
  902. clone: clone$1,
  903. text: SugarElement.fromText
  904. }),
  905. query: constant({
  906. comparePosition,
  907. prevSibling: prevSibling,
  908. nextSibling: nextSibling
  909. }),
  910. property: constant({
  911. children: children,
  912. name: name,
  913. parent: parent,
  914. document,
  915. isText: isText,
  916. isComment: isComment,
  917. isElement: isElement,
  918. isSpecial,
  919. getLanguage,
  920. getText: get,
  921. setText: set,
  922. isBoundary,
  923. isEmptyTag,
  924. isNonEditable
  925. }),
  926. eq: eq,
  927. is: is
  928. };
  929. };
  930. const scan = (universe, element, direction) => {
  931. // if a comment or zero-length text, scan the siblings
  932. if ((universe.property().isText(element) && universe.property().getText(element).trim().length === 0)
  933. || universe.property().isComment(element)) {
  934. return direction(element).bind((elem) => {
  935. return scan(universe, elem, direction).orThunk(() => {
  936. return Optional.some(elem);
  937. });
  938. });
  939. }
  940. else {
  941. return Optional.none();
  942. }
  943. };
  944. const toEnd = (universe, element) => {
  945. if (universe.property().isText(element)) {
  946. return universe.property().getText(element).length;
  947. }
  948. const children = universe.property().children(element);
  949. return children.length;
  950. };
  951. const freefallRtl$2 = (universe, element) => {
  952. const candidate = scan(universe, element, universe.query().prevSibling).getOr(element);
  953. if (universe.property().isText(candidate)) {
  954. return point(candidate, toEnd(universe, candidate));
  955. }
  956. const children = universe.property().children(candidate);
  957. return children.length > 0 ? freefallRtl$2(universe, children[children.length - 1]) : point(candidate, toEnd(universe, candidate));
  958. };
  959. const freefallRtl$1 = freefallRtl$2;
  960. const universe = DomUniverse();
  961. const freefallRtl = (element) => {
  962. return freefallRtl$1(universe, element);
  963. };
  964. const fireToggleAccordionEvent = (editor, element, state) => editor.dispatch('ToggledAccordion', { element, state });
  965. const fireToggleAllAccordionsEvent = (editor, elements, state) => editor.dispatch('ToggledAllAccordions', { elements, state });
  966. const accordionTag = 'details';
  967. const accordionDetailsClass = 'mce-accordion';
  968. const accordionSummaryClass = 'mce-accordion-summary';
  969. const accordionBodyWrapperClass = 'mce-accordion-body';
  970. const accordionBodyWrapperTag = 'div';
  971. var global$3 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  972. const isSummary = (node) => (node === null || node === void 0 ? void 0 : node.nodeName) === 'SUMMARY';
  973. const isDetails = (node) => (node === null || node === void 0 ? void 0 : node.nodeName) === 'DETAILS';
  974. const isOpen = (details) => details.hasAttribute('open');
  975. const isInSummary = (editor) => {
  976. const node = editor.selection.getNode();
  977. return isSummary(node) || Boolean(editor.dom.getParent(node, isSummary));
  978. };
  979. const isAtDetailsStart = (editor) => {
  980. const rng = editor.selection.getRng();
  981. return isDetails(rng.startContainer)
  982. && rng.collapsed
  983. && rng.startOffset === 0;
  984. };
  985. const isInsertAllowed = (editor) => !isInSummary(editor) && editor.dom.isEditable(editor.selection.getNode()) && !editor.mode.isReadOnly();
  986. const getSelectedDetails = (editor) => Optional.from(editor.dom.getParent(editor.selection.getNode(), isDetails));
  987. const isDetailsSelected = (editor) => getSelectedDetails(editor).isSome();
  988. const insertBogus = (element) => {
  989. element.innerHTML = '<br data-mce-bogus="1" />';
  990. return element;
  991. };
  992. const createParagraph = (editor) => insertBogus(editor.dom.create('p'));
  993. const createSummary = (editor) => insertBogus(editor.dom.create('summary'));
  994. const insertAndSelectParagraphAfter = (editor, target) => {
  995. const paragraph = createParagraph(editor);
  996. target.insertAdjacentElement('afterend', paragraph);
  997. editor.selection.setCursorLocation(paragraph, 0);
  998. };
  999. const normalizeContent = (editor, accordion) => {
  1000. if (isSummary(accordion === null || accordion === void 0 ? void 0 : accordion.lastChild)) {
  1001. const paragraph = createParagraph(editor);
  1002. accordion.appendChild(paragraph);
  1003. editor.selection.setCursorLocation(paragraph, 0);
  1004. }
  1005. };
  1006. const normalizeSummary = (editor, accordion) => {
  1007. if (!isSummary(accordion === null || accordion === void 0 ? void 0 : accordion.firstChild)) {
  1008. const summary = createSummary(editor);
  1009. accordion.prepend(summary);
  1010. editor.selection.setCursorLocation(summary, 0);
  1011. }
  1012. };
  1013. const normalizeAccordion = (editor) => (accordion) => {
  1014. normalizeContent(editor, accordion);
  1015. normalizeSummary(editor, accordion);
  1016. };
  1017. const normalizeDetails = (editor) => {
  1018. global$3.each(global$3.grep(editor.dom.select('details', editor.getBody())), normalizeAccordion(editor));
  1019. };
  1020. const insertAccordion = (editor) => {
  1021. if (!isInsertAllowed(editor)) {
  1022. return;
  1023. }
  1024. const editorBody = SugarElement.fromDom(editor.getBody());
  1025. const uid = generate('acc');
  1026. const summaryText = editor.dom.encode(editor.selection.getRng().toString() || editor.translate('Accordion summary...'));
  1027. const bodyText = editor.dom.encode(editor.translate('Accordion body...'));
  1028. const accordionSummaryHtml = `<summary class="${accordionSummaryClass}">${summaryText}</summary>`;
  1029. const accordionBodyHtml = `<${accordionBodyWrapperTag} class="${accordionBodyWrapperClass}"><p>${bodyText}</p></${accordionBodyWrapperTag}>`;
  1030. editor.undoManager.transact(() => {
  1031. editor.insertContent([
  1032. `<details data-mce-id="${uid}" class="${accordionDetailsClass}" open="open">`,
  1033. accordionSummaryHtml,
  1034. accordionBodyHtml,
  1035. `</details>`
  1036. ].join(''));
  1037. descendant(editorBody, `[data-mce-id="${uid}"]`).each((detailsElm) => {
  1038. remove$2(detailsElm, 'data-mce-id');
  1039. descendant(detailsElm, `summary`).each((summaryElm) => {
  1040. // Set the cursor location to be at the end of the summary text
  1041. const rng = editor.dom.createRng();
  1042. const des = freefallRtl(summaryElm);
  1043. rng.setStart(des.element.dom, des.offset);
  1044. rng.setEnd(des.element.dom, des.offset);
  1045. editor.selection.setRng(rng);
  1046. });
  1047. });
  1048. });
  1049. };
  1050. const toggleDetailsElement = (details, state) => {
  1051. const shouldOpen = state !== null && state !== void 0 ? state : !isOpen(details);
  1052. if (shouldOpen) {
  1053. details.setAttribute('open', 'open');
  1054. }
  1055. else {
  1056. details.removeAttribute('open');
  1057. }
  1058. return shouldOpen;
  1059. };
  1060. const toggleAccordion = (editor, state) => {
  1061. getSelectedDetails(editor).each((details) => {
  1062. fireToggleAccordionEvent(editor, details, toggleDetailsElement(details, state));
  1063. });
  1064. };
  1065. const removeAccordion = (editor) => {
  1066. if (!editor.mode.isReadOnly()) {
  1067. getSelectedDetails(editor)
  1068. .each((details) => {
  1069. const { nextSibling } = details;
  1070. if (nextSibling) {
  1071. editor.selection.select(nextSibling, true);
  1072. editor.selection.collapse(true);
  1073. }
  1074. else {
  1075. insertAndSelectParagraphAfter(editor, details);
  1076. }
  1077. details.remove();
  1078. });
  1079. }
  1080. };
  1081. const toggleAllAccordions = (editor, state) => {
  1082. const accordions = Array.from(editor.getBody().querySelectorAll('details'));
  1083. if (accordions.length === 0) {
  1084. return;
  1085. }
  1086. each$1(accordions, (accordion) => toggleDetailsElement(accordion, state !== null && state !== void 0 ? state : !isOpen(accordion)));
  1087. fireToggleAllAccordionsEvent(editor, accordions, state);
  1088. };
  1089. const register$1 = (editor) => {
  1090. editor.addCommand('InsertAccordion', () => insertAccordion(editor));
  1091. editor.addCommand('ToggleAccordion', (_ui, value) => toggleAccordion(editor, value));
  1092. editor.addCommand('ToggleAllAccordions', (_ui, value) => toggleAllAccordions(editor, value));
  1093. editor.addCommand('RemoveAccordion', () => removeAccordion(editor));
  1094. };
  1095. var global$2 = tinymce.util.Tools.resolve('tinymce.html.Node');
  1096. const getClassList = (node) => { var _a, _b; return (_b = (_a = node.attr('class')) === null || _a === void 0 ? void 0 : _a.split(' ')) !== null && _b !== void 0 ? _b : []; };
  1097. const addClasses = (node, classes) => {
  1098. const classListSet = new Set([...getClassList(node), ...classes]);
  1099. const newClassList = Array.from(classListSet);
  1100. if (newClassList.length > 0) {
  1101. node.attr('class', newClassList.join(' '));
  1102. }
  1103. };
  1104. const removeClasses = (node, classes) => {
  1105. const newClassList = filter(getClassList(node), (clazz) => !classes.has(clazz));
  1106. node.attr('class', newClassList.length > 0 ? newClassList.join(' ') : null);
  1107. };
  1108. const isAccordionDetailsNode = (node) => node.name === accordionTag && contains(getClassList(node), accordionDetailsClass);
  1109. const isAccordionBodyWrapperNode = (node) => node.name === accordionBodyWrapperTag && contains(getClassList(node), accordionBodyWrapperClass);
  1110. const getAccordionChildren = (accordionNode) => {
  1111. const children = accordionNode.children();
  1112. let summaryNode;
  1113. let wrapperNode;
  1114. const otherNodes = [];
  1115. for (let i = 0; i < children.length; i++) {
  1116. const child = children[i];
  1117. // Only want to get the first summary element
  1118. if (child.name === 'summary' && isNullable(summaryNode)) {
  1119. summaryNode = child;
  1120. }
  1121. else if (isAccordionBodyWrapperNode(child) && isNullable(wrapperNode)) {
  1122. wrapperNode = child;
  1123. }
  1124. else {
  1125. otherNodes.push(child);
  1126. }
  1127. }
  1128. return {
  1129. summaryNode,
  1130. wrapperNode,
  1131. otherNodes
  1132. };
  1133. };
  1134. const padInputNode = (node) => {
  1135. // Add br to node to ensure the cursor can be placed inside the node
  1136. // Mark as bogus so that it is converted to an nbsp on serialization
  1137. const br = new global$2('br', 1);
  1138. br.attr('data-mce-bogus', '1');
  1139. node.empty();
  1140. node.append(br);
  1141. };
  1142. const setup$2 = (editor) => {
  1143. editor.on('PreInit', () => {
  1144. const { serializer, parser } = editor;
  1145. // Purpose:
  1146. // - add mce-accordion-summary class to summary node
  1147. // - wrap details body in div and add mce-accordion-body class (TINY-9959 assists with Chrome selection issue)
  1148. parser.addNodeFilter(accordionTag, (nodes) => {
  1149. // Using a traditional for loop here as we may have to iterate over many nodes and it is the most performant way of doing so
  1150. for (let i = 0; i < nodes.length; i++) {
  1151. const node = nodes[i];
  1152. if (isAccordionDetailsNode(node)) {
  1153. const accordionNode = node;
  1154. const { summaryNode, wrapperNode, otherNodes } = getAccordionChildren(accordionNode);
  1155. const hasSummaryNode = isNonNullable(summaryNode);
  1156. const newSummaryNode = hasSummaryNode ? summaryNode : new global$2('summary', 1);
  1157. // If there is nothing in the summary, pad it with a br
  1158. // so the cursor can be put inside the accordion summary
  1159. if (isNullable(newSummaryNode.firstChild)) {
  1160. padInputNode(newSummaryNode);
  1161. }
  1162. addClasses(newSummaryNode, [accordionSummaryClass]);
  1163. if (!hasSummaryNode) {
  1164. if (isNonNullable(accordionNode.firstChild)) {
  1165. accordionNode.insert(newSummaryNode, accordionNode.firstChild, true);
  1166. }
  1167. else {
  1168. accordionNode.append(newSummaryNode);
  1169. }
  1170. }
  1171. const hasWrapperNode = isNonNullable(wrapperNode);
  1172. const newWrapperNode = hasWrapperNode ? wrapperNode : new global$2(accordionBodyWrapperTag, 1);
  1173. newWrapperNode.attr('data-mce-bogus', '1');
  1174. addClasses(newWrapperNode, [accordionBodyWrapperClass]);
  1175. if (otherNodes.length > 0) {
  1176. for (let j = 0; j < otherNodes.length; j++) {
  1177. const otherNode = otherNodes[j];
  1178. newWrapperNode.append(otherNode);
  1179. }
  1180. }
  1181. // If there is nothing in the wrapper, append a placeholder p tag
  1182. // so the cursor can be put inside the accordion body
  1183. if (isNullable(newWrapperNode.firstChild)) {
  1184. const pNode = new global$2('p', 1);
  1185. padInputNode(pNode);
  1186. newWrapperNode.append(pNode);
  1187. }
  1188. if (!hasWrapperNode) {
  1189. accordionNode.append(newWrapperNode);
  1190. }
  1191. }
  1192. }
  1193. });
  1194. // Purpose:
  1195. // - remove div wrapping details content as it is only required during editor (see TINY-9959 for details)
  1196. // - remove mce-accordion-summary class on the summary node
  1197. serializer.addNodeFilter(accordionTag, (nodes) => {
  1198. const summaryClassRemoveSet = new Set([accordionSummaryClass]);
  1199. // Using a traditional for loop here as we may have to iterate over many nodes and it is the most performant way of doing so
  1200. for (let i = 0; i < nodes.length; i++) {
  1201. const node = nodes[i];
  1202. if (isAccordionDetailsNode(node)) {
  1203. const accordionNode = node;
  1204. const { summaryNode, wrapperNode } = getAccordionChildren(accordionNode);
  1205. if (isNonNullable(summaryNode)) {
  1206. removeClasses(summaryNode, summaryClassRemoveSet);
  1207. }
  1208. if (isNonNullable(wrapperNode)) {
  1209. wrapperNode.unwrap();
  1210. }
  1211. }
  1212. }
  1213. });
  1214. });
  1215. };
  1216. var global$1 = tinymce.util.Tools.resolve('tinymce.util.VK');
  1217. const setupEnterKeyInSummary = (editor) => {
  1218. editor.on('keydown', (event) => {
  1219. if (!event.shiftKey && event.keyCode === global$1.ENTER
  1220. && isInSummary(editor) || isAtDetailsStart(editor)) {
  1221. event.preventDefault();
  1222. editor.execCommand('ToggleAccordion');
  1223. }
  1224. });
  1225. };
  1226. const setup$1 = (editor) => {
  1227. setupEnterKeyInSummary(editor);
  1228. editor.on('ExecCommand', (e) => {
  1229. const cmd = e.command.toLowerCase();
  1230. if ((cmd === 'delete' || cmd === 'forwarddelete') && isDetailsSelected(editor)) {
  1231. normalizeDetails(editor);
  1232. }
  1233. });
  1234. };
  1235. var global = tinymce.util.Tools.resolve('tinymce.Env');
  1236. const setup = (editor) => {
  1237. // TINY-10177: On Safari, clicking on the expand arrow of the `details` element sets the selection before the `summary`,
  1238. // so we override the selection to the beginning of `summary` content
  1239. if (global.browser.isSafari()) {
  1240. editor.on('click', (e) => {
  1241. if (isSummary(e.target)) {
  1242. const summary = e.target;
  1243. const rng = editor.selection.getRng();
  1244. if (rng.collapsed && rng.startContainer === summary.parentNode && rng.startOffset === 0) {
  1245. editor.selection.setCursorLocation(summary, 0);
  1246. }
  1247. }
  1248. });
  1249. }
  1250. };
  1251. const onSetup = (editor) => (buttonApi) => {
  1252. const onNodeChange = () => buttonApi.setEnabled(isInsertAllowed(editor));
  1253. editor.on('NodeChange', onNodeChange);
  1254. return () => editor.off('NodeChange', onNodeChange);
  1255. };
  1256. const register = (editor) => {
  1257. const onAction = () => editor.execCommand('InsertAccordion');
  1258. editor.ui.registry.addButton('accordion', { icon: 'accordion', tooltip: 'Insert accordion', onSetup: onSetup(editor), onAction });
  1259. editor.ui.registry.addMenuItem('accordion', { icon: 'accordion', text: 'Accordion', onSetup: onSetup(editor), onAction });
  1260. editor.ui.registry.addToggleButton('accordiontoggle', {
  1261. icon: 'accordion-toggle',
  1262. tooltip: 'Toggle accordion',
  1263. onAction: () => editor.execCommand('ToggleAccordion')
  1264. });
  1265. editor.ui.registry.addToggleButton('accordionremove', {
  1266. icon: 'remove',
  1267. tooltip: 'Delete accordion',
  1268. onAction: () => editor.execCommand('RemoveAccordion')
  1269. });
  1270. editor.ui.registry.addContextToolbar('accordion', {
  1271. predicate: (accordion) => editor.dom.is(accordion, 'details') && editor.getBody().contains(accordion) && editor.dom.isEditable(accordion.parentNode),
  1272. items: 'accordiontoggle accordionremove',
  1273. scope: 'node',
  1274. position: 'node'
  1275. });
  1276. };
  1277. var Plugin = () => {
  1278. global$4.add('accordion', (editor) => {
  1279. register(editor);
  1280. register$1(editor);
  1281. setup$1(editor);
  1282. setup$2(editor);
  1283. setup(editor);
  1284. });
  1285. };
  1286. Plugin();
  1287. /** *****
  1288. * DO NOT EXPORT ANYTHING
  1289. *
  1290. * IF YOU DO ROLLUP WILL LEAVE A GLOBAL ON THE PAGE
  1291. *******/
  1292. })();