plugin.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /**
  2. * TinyMCE version 8.0.2 (2025-08-14)
  3. */
  4. (function () {
  5. 'use strict';
  6. var global$1 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  7. /* eslint-disable @typescript-eslint/no-wrapper-object-types */
  8. const isSimpleType = (type) => (value) => typeof value === type;
  9. const eq = (t) => (a) => t === a;
  10. const isNull = eq(null);
  11. const isUndefined = eq(undefined);
  12. const isNullable = (a) => a === null || a === undefined;
  13. const isNonNullable = (a) => !isNullable(a);
  14. const isFunction = isSimpleType('function');
  15. const noop = () => { };
  16. const constant = (value) => {
  17. return () => {
  18. return value;
  19. };
  20. };
  21. const never = constant(false);
  22. /**
  23. * The `Optional` type represents a value (of any type) that potentially does
  24. * not exist. Any `Optional<T>` can either be a `Some<T>` (in which case the
  25. * value does exist) or a `None` (in which case the value does not exist). This
  26. * module defines a whole lot of FP-inspired utility functions for dealing with
  27. * `Optional` objects.
  28. *
  29. * Comparison with null or undefined:
  30. * - We don't get fancy null coalescing operators with `Optional`
  31. * - We do get fancy helper functions with `Optional`
  32. * - `Optional` support nesting, and allow for the type to still be nullable (or
  33. * another `Optional`)
  34. * - There is no option to turn off strict-optional-checks like there is for
  35. * strict-null-checks
  36. */
  37. class Optional {
  38. // The internal representation has a `tag` and a `value`, but both are
  39. // private: able to be console.logged, but not able to be accessed by code
  40. constructor(tag, value) {
  41. this.tag = tag;
  42. this.value = value;
  43. }
  44. // --- Identities ---
  45. /**
  46. * Creates a new `Optional<T>` that **does** contain a value.
  47. */
  48. static some(value) {
  49. return new Optional(true, value);
  50. }
  51. /**
  52. * Create a new `Optional<T>` that **does not** contain a value. `T` can be
  53. * any type because we don't actually have a `T`.
  54. */
  55. static none() {
  56. return Optional.singletonNone;
  57. }
  58. /**
  59. * Perform a transform on an `Optional` type. Regardless of whether this
  60. * `Optional` contains a value or not, `fold` will return a value of type `U`.
  61. * If this `Optional` does not contain a value, the `U` will be created by
  62. * calling `onNone`. If this `Optional` does contain a value, the `U` will be
  63. * created by calling `onSome`.
  64. *
  65. * For the FP enthusiasts in the room, this function:
  66. * 1. Could be used to implement all of the functions below
  67. * 2. Forms a catamorphism
  68. */
  69. fold(onNone, onSome) {
  70. if (this.tag) {
  71. return onSome(this.value);
  72. }
  73. else {
  74. return onNone();
  75. }
  76. }
  77. /**
  78. * Determine if this `Optional` object contains a value.
  79. */
  80. isSome() {
  81. return this.tag;
  82. }
  83. /**
  84. * Determine if this `Optional` object **does not** contain a value.
  85. */
  86. isNone() {
  87. return !this.tag;
  88. }
  89. // --- Functor (name stolen from Haskell / maths) ---
  90. /**
  91. * Perform a transform on an `Optional` object, **if** there is a value. If
  92. * you provide a function to turn a T into a U, this is the function you use
  93. * to turn an `Optional<T>` into an `Optional<U>`. If this **does** contain
  94. * a value then the output will also contain a value (that value being the
  95. * output of `mapper(this.value)`), and if this **does not** contain a value
  96. * then neither will the output.
  97. */
  98. map(mapper) {
  99. if (this.tag) {
  100. return Optional.some(mapper(this.value));
  101. }
  102. else {
  103. return Optional.none();
  104. }
  105. }
  106. // --- Monad (name stolen from Haskell / maths) ---
  107. /**
  108. * Perform a transform on an `Optional` object, **if** there is a value.
  109. * Unlike `map`, here the transform itself also returns an `Optional`.
  110. */
  111. bind(binder) {
  112. if (this.tag) {
  113. return binder(this.value);
  114. }
  115. else {
  116. return Optional.none();
  117. }
  118. }
  119. // --- Traversable (name stolen from Haskell / maths) ---
  120. /**
  121. * For a given predicate, this function finds out if there **exists** a value
  122. * inside this `Optional` object that meets the predicate. In practice, this
  123. * means that for `Optional`s that do not contain a value it returns false (as
  124. * no predicate-meeting value exists).
  125. */
  126. exists(predicate) {
  127. return this.tag && predicate(this.value);
  128. }
  129. /**
  130. * For a given predicate, this function finds out if **all** the values inside
  131. * this `Optional` object meet the predicate. In practice, this means that
  132. * for `Optional`s that do not contain a value it returns true (as all 0
  133. * objects do meet the predicate).
  134. */
  135. forall(predicate) {
  136. return !this.tag || predicate(this.value);
  137. }
  138. filter(predicate) {
  139. if (!this.tag || predicate(this.value)) {
  140. return this;
  141. }
  142. else {
  143. return Optional.none();
  144. }
  145. }
  146. // --- Getters ---
  147. /**
  148. * Get the value out of the inside of the `Optional` object, using a default
  149. * `replacement` value if the provided `Optional` object does not contain a
  150. * value.
  151. */
  152. getOr(replacement) {
  153. return this.tag ? this.value : replacement;
  154. }
  155. /**
  156. * Get the value out of the inside of the `Optional` object, using a default
  157. * `replacement` value if the provided `Optional` object does not contain a
  158. * value. Unlike `getOr`, in this method the `replacement` object is also
  159. * `Optional` - meaning that this method will always return an `Optional`.
  160. */
  161. or(replacement) {
  162. return this.tag ? this : replacement;
  163. }
  164. /**
  165. * Get the value out of the inside of the `Optional` object, using a default
  166. * `replacement` value if the provided `Optional` object does not contain a
  167. * value. Unlike `getOr`, in this method the `replacement` value is
  168. * "thunked" - that is to say that you don't pass a value to `getOrThunk`, you
  169. * pass a function which (if called) will **return** the `value` you want to
  170. * use.
  171. */
  172. getOrThunk(thunk) {
  173. return this.tag ? this.value : thunk();
  174. }
  175. /**
  176. * Get the value out of the inside of the `Optional` object, using a default
  177. * `replacement` value if the provided Optional object does not contain a
  178. * value.
  179. *
  180. * Unlike `or`, in this method the `replacement` value is "thunked" - that is
  181. * to say that you don't pass a value to `orThunk`, you pass a function which
  182. * (if called) will **return** the `value` you want to use.
  183. *
  184. * Unlike `getOrThunk`, in this method the `replacement` value is also
  185. * `Optional`, meaning that this method will always return an `Optional`.
  186. */
  187. orThunk(thunk) {
  188. return this.tag ? this : thunk();
  189. }
  190. /**
  191. * Get the value out of the inside of the `Optional` object, throwing an
  192. * exception if the provided `Optional` object does not contain a value.
  193. *
  194. * WARNING:
  195. * You should only be using this function if you know that the `Optional`
  196. * object **is not** empty (otherwise you're throwing exceptions in production
  197. * code, which is bad).
  198. *
  199. * In tests this is more acceptable.
  200. *
  201. * Prefer other methods to this, such as `.each`.
  202. */
  203. getOrDie(message) {
  204. if (!this.tag) {
  205. throw new Error(message !== null && message !== void 0 ? message : 'Called getOrDie on None');
  206. }
  207. else {
  208. return this.value;
  209. }
  210. }
  211. // --- Interop with null and undefined ---
  212. /**
  213. * Creates an `Optional` value from a nullable (or undefined-able) input.
  214. * Null, or undefined, is converted to `None`, and anything else is converted
  215. * to `Some`.
  216. */
  217. static from(value) {
  218. return isNonNullable(value) ? Optional.some(value) : Optional.none();
  219. }
  220. /**
  221. * Converts an `Optional` to a nullable type, by getting the value if it
  222. * exists, or returning `null` if it does not.
  223. */
  224. getOrNull() {
  225. return this.tag ? this.value : null;
  226. }
  227. /**
  228. * Converts an `Optional` to an undefined-able type, by getting the value if
  229. * it exists, or returning `undefined` if it does not.
  230. */
  231. getOrUndefined() {
  232. return this.value;
  233. }
  234. // --- Utilities ---
  235. /**
  236. * If the `Optional` contains a value, perform an action on that value.
  237. * Unlike the rest of the methods on this type, `.each` has side-effects. If
  238. * you want to transform an `Optional<T>` **into** something, then this is not
  239. * the method for you. If you want to use an `Optional<T>` to **do**
  240. * something, then this is the method for you - provided you're okay with not
  241. * doing anything in the case where the `Optional` doesn't have a value inside
  242. * it. If you're not sure whether your use-case fits into transforming
  243. * **into** something or **doing** something, check whether it has a return
  244. * value. If it does, you should be performing a transform.
  245. */
  246. each(worker) {
  247. if (this.tag) {
  248. worker(this.value);
  249. }
  250. }
  251. /**
  252. * Turn the `Optional` object into an array that contains all of the values
  253. * stored inside the `Optional`. In practice, this means the output will have
  254. * either 0 or 1 elements.
  255. */
  256. toArray() {
  257. return this.tag ? [this.value] : [];
  258. }
  259. /**
  260. * Turn the `Optional` object into a string for debugging or printing. Not
  261. * recommended for production code, but good for debugging. Also note that
  262. * these days an `Optional` object can be logged to the console directly, and
  263. * its inner value (if it exists) will be visible.
  264. */
  265. toString() {
  266. return this.tag ? `some(${this.value})` : 'none()';
  267. }
  268. }
  269. // Sneaky optimisation: every instance of Optional.none is identical, so just
  270. // reuse the same object
  271. Optional.singletonNone = new Optional(false);
  272. const nativeSlice = Array.prototype.slice;
  273. const exists = (xs, pred) => {
  274. for (let i = 0, len = xs.length; i < len; i++) {
  275. const x = xs[i];
  276. if (pred(x, i)) {
  277. return true;
  278. }
  279. }
  280. return false;
  281. };
  282. const map$1 = (xs, f) => {
  283. // pre-allocating array size when it's guaranteed to be known
  284. // http://jsperf.com/push-allocated-vs-dynamic/22
  285. const len = xs.length;
  286. const r = new Array(len);
  287. for (let i = 0; i < len; i++) {
  288. const x = xs[i];
  289. r[i] = f(x, i);
  290. }
  291. return r;
  292. };
  293. // Unwound implementing other functions in terms of each.
  294. // The code size is roughly the same, and it should allow for better optimisation.
  295. // const each = function<T, U>(xs: T[], f: (x: T, i?: number, xs?: T[]) => void): void {
  296. const each$1 = (xs, f) => {
  297. for (let i = 0, len = xs.length; i < len; i++) {
  298. const x = xs[i];
  299. f(x, i);
  300. }
  301. };
  302. isFunction(Array.from) ? Array.from : (x) => nativeSlice.call(x);
  303. // There are many variations of Object iteration that are faster than the 'for-in' style:
  304. // http://jsperf.com/object-keys-iteration/107
  305. //
  306. // Use the native keys if it is available (IE9+), otherwise fall back to manually filtering
  307. const keys = Object.keys;
  308. const hasOwnProperty = Object.hasOwnProperty;
  309. const each = (obj, f) => {
  310. const props = keys(obj);
  311. for (let k = 0, len = props.length; k < len; k++) {
  312. const i = props[k];
  313. const x = obj[i];
  314. f(x, i);
  315. }
  316. };
  317. const map = (obj, f) => {
  318. return tupleMap(obj, (x, i) => ({
  319. k: i,
  320. v: f(x, i)
  321. }));
  322. };
  323. const tupleMap = (obj, f) => {
  324. const r = {};
  325. each(obj, (x, i) => {
  326. const tuple = f(x, i);
  327. r[tuple.k] = tuple.v;
  328. });
  329. return r;
  330. };
  331. const has = (obj, key) => hasOwnProperty.call(obj, key);
  332. const Cell = (initial) => {
  333. let value = initial;
  334. const get = () => {
  335. return value;
  336. };
  337. const set = (v) => {
  338. value = v;
  339. };
  340. return {
  341. get,
  342. set
  343. };
  344. };
  345. const shallow = (old, nu) => {
  346. return nu;
  347. };
  348. const baseMerge = (merger) => {
  349. return (...objects) => {
  350. if (objects.length === 0) {
  351. throw new Error(`Can't merge zero objects`);
  352. }
  353. const ret = {};
  354. for (let j = 0; j < objects.length; j++) {
  355. const curObject = objects[j];
  356. for (const key in curObject) {
  357. if (has(curObject, key)) {
  358. ret[key] = merger(ret[key], curObject[key]);
  359. }
  360. }
  361. }
  362. return ret;
  363. };
  364. };
  365. const merge = baseMerge(shallow);
  366. const singleton = (doRevoke) => {
  367. const subject = Cell(Optional.none());
  368. const revoke = () => subject.get().each(doRevoke);
  369. const clear = () => {
  370. revoke();
  371. subject.set(Optional.none());
  372. };
  373. const isSet = () => subject.get().isSome();
  374. const get = () => subject.get();
  375. const set = (s) => {
  376. revoke();
  377. subject.set(Optional.some(s));
  378. };
  379. return {
  380. clear,
  381. isSet,
  382. get,
  383. set
  384. };
  385. };
  386. const value = () => {
  387. const subject = singleton(noop);
  388. const on = (f) => subject.get().each(f);
  389. return {
  390. ...subject,
  391. on
  392. };
  393. };
  394. const checkRange = (str, substr, start) => substr === '' || str.length >= substr.length && str.substr(start, start + substr.length) === substr;
  395. const contains = (str, substr, start = 0, end) => {
  396. const idx = str.indexOf(substr, start);
  397. if (idx !== -1) {
  398. return isUndefined(end) ? true : idx + substr.length <= end;
  399. }
  400. else {
  401. return false;
  402. }
  403. };
  404. /** Does 'str' start with 'prefix'?
  405. * Note: all strings start with the empty string.
  406. * More formally, for all strings x, startsWith(x, "").
  407. * This is so that for all strings x and y, startsWith(y + x, y)
  408. */
  409. const startsWith = (str, prefix) => {
  410. return checkRange(str, prefix, 0);
  411. };
  412. // Run a function fn after rate ms. If another invocation occurs
  413. // during the time it is waiting, reschedule the function again
  414. // with the new arguments.
  415. const last = (fn, rate) => {
  416. let timer = null;
  417. const cancel = () => {
  418. if (!isNull(timer)) {
  419. clearTimeout(timer);
  420. timer = null;
  421. }
  422. };
  423. const throttle = (...args) => {
  424. cancel();
  425. timer = setTimeout(() => {
  426. timer = null;
  427. fn.apply(null, args);
  428. }, rate);
  429. };
  430. return {
  431. cancel,
  432. throttle
  433. };
  434. };
  435. const insertEmoticon = (editor, ch) => {
  436. editor.insertContent(ch);
  437. };
  438. var global = tinymce.util.Tools.resolve('tinymce.Resource');
  439. const DEFAULT_ID = 'tinymce.plugins.emoticons';
  440. const option = (name) => (editor) => editor.options.get(name);
  441. const register$2 = (editor, pluginUrl) => {
  442. const registerOption = editor.options.register;
  443. registerOption('emoticons_database', {
  444. processor: 'string',
  445. default: 'emojis'
  446. });
  447. registerOption('emoticons_database_url', {
  448. processor: 'string',
  449. default: `${pluginUrl}/js/${getEmojiDatabase(editor)}${editor.suffix}.js`
  450. });
  451. registerOption('emoticons_database_id', {
  452. processor: 'string',
  453. default: DEFAULT_ID
  454. });
  455. registerOption('emoticons_append', {
  456. processor: 'object',
  457. default: {}
  458. });
  459. registerOption('emoticons_images_url', {
  460. processor: 'string',
  461. default: 'https://cdnjs.cloudflare.com/ajax/libs/twemoji/15.1.0/72x72/'
  462. });
  463. };
  464. const getEmojiDatabase = option('emoticons_database');
  465. const getEmojiDatabaseUrl = option('emoticons_database_url');
  466. const getEmojiDatabaseId = option('emoticons_database_id');
  467. const getAppendedEmoji = option('emoticons_append');
  468. const getEmojiImageUrl = option('emoticons_images_url');
  469. const ALL_CATEGORY = 'All';
  470. const categoryNameMap = {
  471. symbols: 'Symbols',
  472. people: 'People',
  473. animals_and_nature: 'Animals and Nature',
  474. food_and_drink: 'Food and Drink',
  475. activity: 'Activity',
  476. travel_and_places: 'Travel and Places',
  477. objects: 'Objects',
  478. flags: 'Flags',
  479. user: 'User Defined'
  480. };
  481. const translateCategory = (categories, name) => has(categories, name) ? categories[name] : name;
  482. const getUserDefinedEmoji = (editor) => {
  483. const userDefinedEmoticons = getAppendedEmoji(editor);
  484. return map(userDefinedEmoticons, (value) =>
  485. // Set some sane defaults for the custom emoji entry
  486. ({ keywords: [], category: 'user', ...value }));
  487. };
  488. // TODO: Consider how to share this loading across different editors
  489. const initDatabase = (editor, databaseUrl, databaseId) => {
  490. const categories = value();
  491. const all = value();
  492. const emojiImagesUrl = getEmojiImageUrl(editor);
  493. const getEmoji = (lib) => {
  494. // Note: This is a little hacky, but the database doesn't provide a way for us to tell what sort of database is being used
  495. if (startsWith(lib.char, '<img')) {
  496. return lib.char.replace(/src="([^"]+)"/, (match, url) => `src="${emojiImagesUrl}${url}"`);
  497. }
  498. else {
  499. return lib.char;
  500. }
  501. };
  502. const processEmojis = (emojis) => {
  503. const cats = {};
  504. const everything = [];
  505. each(emojis, (lib, title) => {
  506. const entry = {
  507. // Omitting fitzpatrick_scale
  508. title,
  509. keywords: lib.keywords,
  510. char: getEmoji(lib),
  511. category: translateCategory(categoryNameMap, lib.category)
  512. };
  513. const current = cats[entry.category] !== undefined ? cats[entry.category] : [];
  514. cats[entry.category] = current.concat([entry]);
  515. everything.push(entry);
  516. });
  517. categories.set(cats);
  518. all.set(everything);
  519. };
  520. editor.on('init', () => {
  521. global.load(databaseId, databaseUrl).then((emojis) => {
  522. const userEmojis = getUserDefinedEmoji(editor);
  523. processEmojis(merge(emojis, userEmojis));
  524. }, (err) => {
  525. // eslint-disable-next-line no-console
  526. console.log(`Failed to load emojis: ${err}`);
  527. categories.set({});
  528. all.set([]);
  529. });
  530. });
  531. const listCategory = (category) => {
  532. if (category === ALL_CATEGORY) {
  533. return listAll();
  534. }
  535. return categories.get().bind((cats) => Optional.from(cats[category])).getOr([]);
  536. };
  537. const listAll = () => all.get().getOr([]);
  538. const listCategories = () =>
  539. // TODO: Category key order should be adjusted to match the standard
  540. [ALL_CATEGORY].concat(keys(categories.get().getOr({})));
  541. const waitForLoad = () => {
  542. if (hasLoaded()) {
  543. return Promise.resolve(true);
  544. }
  545. else {
  546. return new Promise((resolve, reject) => {
  547. let numRetries = 15;
  548. const interval = setInterval(() => {
  549. if (hasLoaded()) {
  550. clearInterval(interval);
  551. resolve(true);
  552. }
  553. else {
  554. numRetries--;
  555. if (numRetries < 0) {
  556. // eslint-disable-next-line no-console
  557. console.log('Could not load emojis from url: ' + databaseUrl);
  558. clearInterval(interval);
  559. reject(false);
  560. }
  561. }
  562. }, 100);
  563. });
  564. }
  565. };
  566. const hasLoaded = () => categories.isSet() && all.isSet();
  567. return {
  568. listCategories,
  569. hasLoaded,
  570. waitForLoad,
  571. listAll,
  572. listCategory
  573. };
  574. };
  575. const emojiMatches = (emoji, lowerCasePattern) => contains(emoji.title.toLowerCase(), lowerCasePattern) ||
  576. exists(emoji.keywords, (k) => contains(k.toLowerCase(), lowerCasePattern));
  577. const emojisFrom = (list, pattern, maxResults) => {
  578. const matches = [];
  579. const lowerCasePattern = pattern.toLowerCase();
  580. const reachedLimit = maxResults.fold(() => never, (max) => (size) => size >= max);
  581. for (let i = 0; i < list.length; i++) {
  582. // TODO: more intelligent search by showing title matches at the top, keyword matches after that (use two arrays and concat at the end)
  583. if (pattern.length === 0 || emojiMatches(list[i], lowerCasePattern)) {
  584. matches.push({
  585. value: list[i].char,
  586. text: list[i].title,
  587. icon: list[i].char
  588. });
  589. if (reachedLimit(matches.length)) {
  590. break;
  591. }
  592. }
  593. }
  594. return matches;
  595. };
  596. const patternName = 'pattern';
  597. const open = (editor, database) => {
  598. const initialState = {
  599. pattern: '',
  600. results: emojisFrom(database.listAll(), '', Optional.some(300))
  601. };
  602. const currentTab = Cell(ALL_CATEGORY);
  603. const scan = (dialogApi) => {
  604. const dialogData = dialogApi.getData();
  605. const category = currentTab.get();
  606. const candidates = database.listCategory(category);
  607. const results = emojisFrom(candidates, dialogData[patternName], category === ALL_CATEGORY ? Optional.some(300) : Optional.none());
  608. dialogApi.setData({
  609. results
  610. });
  611. };
  612. const updateFilter = last((dialogApi) => {
  613. scan(dialogApi);
  614. }, 200);
  615. const searchField = {
  616. label: 'Search',
  617. type: 'input',
  618. name: patternName
  619. };
  620. const resultsField = {
  621. type: 'collection',
  622. name: 'results'
  623. // TODO TINY-3229 implement collection columns properly
  624. // columns: 'auto'
  625. };
  626. const getInitialState = () => {
  627. const body = {
  628. type: 'tabpanel',
  629. // All tabs have the same fields.
  630. tabs: map$1(database.listCategories(), (cat) => ({
  631. title: cat,
  632. name: cat,
  633. items: [searchField, resultsField]
  634. }))
  635. };
  636. return {
  637. title: 'Emojis',
  638. size: 'normal',
  639. body,
  640. initialData: initialState,
  641. onTabChange: (dialogApi, details) => {
  642. currentTab.set(details.newTabName);
  643. updateFilter.throttle(dialogApi);
  644. },
  645. onChange: updateFilter.throttle,
  646. onAction: (dialogApi, actionData) => {
  647. if (actionData.name === 'results') {
  648. insertEmoticon(editor, actionData.value);
  649. dialogApi.close();
  650. }
  651. },
  652. buttons: [
  653. {
  654. type: 'cancel',
  655. text: 'Close',
  656. primary: true
  657. }
  658. ]
  659. };
  660. };
  661. const dialogApi = editor.windowManager.open(getInitialState());
  662. dialogApi.focus(patternName);
  663. if (!database.hasLoaded()) {
  664. dialogApi.block('Loading emojis...');
  665. database.waitForLoad().then(() => {
  666. dialogApi.redial(getInitialState());
  667. updateFilter.throttle(dialogApi);
  668. dialogApi.focus(patternName);
  669. dialogApi.unblock();
  670. }).catch((_err) => {
  671. dialogApi.redial({
  672. title: 'Emojis',
  673. body: {
  674. type: 'panel',
  675. items: [
  676. {
  677. type: 'alertbanner',
  678. level: 'error',
  679. icon: 'warning',
  680. text: 'Could not load emojis'
  681. }
  682. ]
  683. },
  684. buttons: [
  685. {
  686. type: 'cancel',
  687. text: 'Close',
  688. primary: true
  689. }
  690. ],
  691. initialData: {
  692. pattern: '',
  693. results: []
  694. }
  695. });
  696. dialogApi.focus(patternName);
  697. dialogApi.unblock();
  698. });
  699. }
  700. };
  701. const register$1 = (editor, database) => {
  702. editor.addCommand('mceEmoticons', () => open(editor, database));
  703. };
  704. const setup = (editor) => {
  705. editor.on('PreInit', () => {
  706. editor.parser.addAttributeFilter('data-emoticon', (nodes) => {
  707. each$1(nodes, (node) => {
  708. node.attr('data-mce-resize', 'false');
  709. node.attr('data-mce-placeholder', '1');
  710. });
  711. });
  712. });
  713. };
  714. const init = (editor, database) => {
  715. editor.ui.registry.addAutocompleter('emoticons', {
  716. trigger: ':',
  717. columns: 'auto',
  718. minChars: 2,
  719. fetch: (pattern, maxResults) => database.waitForLoad().then(() => {
  720. const candidates = database.listAll();
  721. return emojisFrom(candidates, pattern, Optional.some(maxResults));
  722. }),
  723. onAction: (autocompleteApi, rng, value) => {
  724. editor.selection.setRng(rng);
  725. editor.insertContent(value);
  726. autocompleteApi.hide();
  727. }
  728. });
  729. };
  730. const onSetupEditable = (editor) => (api) => {
  731. const nodeChanged = () => {
  732. api.setEnabled(editor.selection.isEditable());
  733. };
  734. editor.on('NodeChange', nodeChanged);
  735. nodeChanged();
  736. return () => {
  737. editor.off('NodeChange', nodeChanged);
  738. };
  739. };
  740. const register = (editor) => {
  741. const onAction = () => editor.execCommand('mceEmoticons');
  742. editor.ui.registry.addButton('emoticons', {
  743. tooltip: 'Emojis',
  744. icon: 'emoji',
  745. onAction,
  746. onSetup: onSetupEditable(editor)
  747. });
  748. editor.ui.registry.addMenuItem('emoticons', {
  749. text: 'Emojis...',
  750. icon: 'emoji',
  751. onAction,
  752. onSetup: onSetupEditable(editor)
  753. });
  754. };
  755. /**
  756. * This class contains all core logic for the emoticons plugin.
  757. *
  758. * @class tinymce.emoticons.Plugin
  759. * @private
  760. */
  761. var Plugin = () => {
  762. global$1.add('emoticons', (editor, pluginUrl) => {
  763. register$2(editor, pluginUrl);
  764. const databaseUrl = getEmojiDatabaseUrl(editor);
  765. const databaseId = getEmojiDatabaseId(editor);
  766. const database = initDatabase(editor, databaseUrl, databaseId);
  767. register$1(editor, database);
  768. register(editor);
  769. init(editor, database);
  770. setup(editor);
  771. return {
  772. getAllEmojis: () => database.waitForLoad().then(() => database.listAll())
  773. };
  774. });
  775. };
  776. Plugin();
  777. /** *****
  778. * DO NOT EXPORT ANYTHING
  779. *
  780. * IF YOU DO ROLLUP WILL LEAVE A GLOBAL ON THE PAGE
  781. *******/
  782. })();