plugin.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. /**
  2. * Copyright (c) Tiny Technologies, Inc. All rights reserved.
  3. * Licensed under the LGPL or a commercial license.
  4. * For LGPL see License.txt in the project root for license information.
  5. * For commercial licenses see https://www.tiny.cloud/
  6. *
  7. * Version: 5.2.0 (2020-02-13)
  8. */
  9. (function (domGlobals) {
  10. 'use strict';
  11. var Cell = function (initial) {
  12. var value = initial;
  13. var get = function () {
  14. return value;
  15. };
  16. var set = function (v) {
  17. value = v;
  18. };
  19. var clone = function () {
  20. return Cell(get());
  21. };
  22. return {
  23. get: get,
  24. set: set,
  25. clone: clone
  26. };
  27. };
  28. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  29. var __assign = function () {
  30. __assign = Object.assign || function __assign(t) {
  31. for (var s, i = 1, n = arguments.length; i < n; i++) {
  32. s = arguments[i];
  33. for (var p in s)
  34. if (Object.prototype.hasOwnProperty.call(s, p))
  35. t[p] = s[p];
  36. }
  37. return t;
  38. };
  39. return __assign.apply(this, arguments);
  40. };
  41. function __spreadArrays() {
  42. for (var s = 0, i = 0, il = arguments.length; i < il; i++)
  43. s += arguments[i].length;
  44. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  45. for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  46. r[k] = a[j];
  47. return r;
  48. }
  49. var noop = function () {
  50. };
  51. var constant = function (value) {
  52. return function () {
  53. return value;
  54. };
  55. };
  56. var identity = function (x) {
  57. return x;
  58. };
  59. var die = function (msg) {
  60. return function () {
  61. throw new Error(msg);
  62. };
  63. };
  64. var never = constant(false);
  65. var always = constant(true);
  66. var none = function () {
  67. return NONE;
  68. };
  69. var NONE = function () {
  70. var eq = function (o) {
  71. return o.isNone();
  72. };
  73. var call = function (thunk) {
  74. return thunk();
  75. };
  76. var id = function (n) {
  77. return n;
  78. };
  79. var me = {
  80. fold: function (n, s) {
  81. return n();
  82. },
  83. is: never,
  84. isSome: never,
  85. isNone: always,
  86. getOr: id,
  87. getOrThunk: call,
  88. getOrDie: function (msg) {
  89. throw new Error(msg || 'error: getOrDie called on none.');
  90. },
  91. getOrNull: constant(null),
  92. getOrUndefined: constant(undefined),
  93. or: id,
  94. orThunk: call,
  95. map: none,
  96. each: noop,
  97. bind: none,
  98. exists: never,
  99. forall: always,
  100. filter: none,
  101. equals: eq,
  102. equals_: eq,
  103. toArray: function () {
  104. return [];
  105. },
  106. toString: constant('none()')
  107. };
  108. if (Object.freeze) {
  109. Object.freeze(me);
  110. }
  111. return me;
  112. }();
  113. var some = function (a) {
  114. var constant_a = constant(a);
  115. var self = function () {
  116. return me;
  117. };
  118. var bind = function (f) {
  119. return f(a);
  120. };
  121. var me = {
  122. fold: function (n, s) {
  123. return s(a);
  124. },
  125. is: function (v) {
  126. return a === v;
  127. },
  128. isSome: always,
  129. isNone: never,
  130. getOr: constant_a,
  131. getOrThunk: constant_a,
  132. getOrDie: constant_a,
  133. getOrNull: constant_a,
  134. getOrUndefined: constant_a,
  135. or: self,
  136. orThunk: self,
  137. map: function (f) {
  138. return some(f(a));
  139. },
  140. each: function (f) {
  141. f(a);
  142. },
  143. bind: bind,
  144. exists: bind,
  145. forall: bind,
  146. filter: function (f) {
  147. return f(a) ? me : NONE;
  148. },
  149. toArray: function () {
  150. return [a];
  151. },
  152. toString: function () {
  153. return 'some(' + a + ')';
  154. },
  155. equals: function (o) {
  156. return o.is(a);
  157. },
  158. equals_: function (o, elementEq) {
  159. return o.fold(never, function (b) {
  160. return elementEq(a, b);
  161. });
  162. }
  163. };
  164. return me;
  165. };
  166. var from = function (value) {
  167. return value === null || value === undefined ? NONE : some(value);
  168. };
  169. var Option = {
  170. some: some,
  171. none: none,
  172. from: from
  173. };
  174. var typeOf = function (x) {
  175. if (x === null) {
  176. return 'null';
  177. }
  178. var t = typeof x;
  179. if (t === 'object' && (Array.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'Array')) {
  180. return 'array';
  181. }
  182. if (t === 'object' && (String.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'String')) {
  183. return 'string';
  184. }
  185. return t;
  186. };
  187. var isType = function (type) {
  188. return function (value) {
  189. return typeOf(value) === type;
  190. };
  191. };
  192. var isString = isType('string');
  193. var isObject = isType('object');
  194. var isArray = isType('array');
  195. var isFunction = isType('function');
  196. var nativeSlice = Array.prototype.slice;
  197. var nativeIndexOf = Array.prototype.indexOf;
  198. var rawIndexOf = function (ts, t) {
  199. return nativeIndexOf.call(ts, t);
  200. };
  201. var contains = function (xs, x) {
  202. return rawIndexOf(xs, x) > -1;
  203. };
  204. var map = function (xs, f) {
  205. var len = xs.length;
  206. var r = new Array(len);
  207. for (var i = 0; i < len; i++) {
  208. var x = xs[i];
  209. r[i] = f(x, i);
  210. }
  211. return r;
  212. };
  213. var each = function (xs, f) {
  214. for (var i = 0, len = xs.length; i < len; i++) {
  215. var x = xs[i];
  216. f(x, i);
  217. }
  218. };
  219. var eachr = function (xs, f) {
  220. for (var i = xs.length - 1; i >= 0; i--) {
  221. var x = xs[i];
  222. f(x, i);
  223. }
  224. };
  225. var filter = function (xs, pred) {
  226. var r = [];
  227. for (var i = 0, len = xs.length; i < len; i++) {
  228. var x = xs[i];
  229. if (pred(x, i)) {
  230. r.push(x);
  231. }
  232. }
  233. return r;
  234. };
  235. var foldr = function (xs, f, acc) {
  236. eachr(xs, function (x) {
  237. acc = f(acc, x);
  238. });
  239. return acc;
  240. };
  241. var foldl = function (xs, f, acc) {
  242. each(xs, function (x) {
  243. acc = f(acc, x);
  244. });
  245. return acc;
  246. };
  247. var find = function (xs, pred) {
  248. for (var i = 0, len = xs.length; i < len; i++) {
  249. var x = xs[i];
  250. if (pred(x, i)) {
  251. return Option.some(x);
  252. }
  253. }
  254. return Option.none();
  255. };
  256. var forall = function (xs, pred) {
  257. for (var i = 0, len = xs.length; i < len; ++i) {
  258. var x = xs[i];
  259. if (pred(x, i) !== true) {
  260. return false;
  261. }
  262. }
  263. return true;
  264. };
  265. var sort = function (xs, comparator) {
  266. var copy = nativeSlice.call(xs, 0);
  267. copy.sort(comparator);
  268. return copy;
  269. };
  270. var head = function (xs) {
  271. return xs.length === 0 ? Option.none() : Option.some(xs[0]);
  272. };
  273. var from$1 = isFunction(Array.from) ? Array.from : function (x) {
  274. return nativeSlice.call(x);
  275. };
  276. var keys = Object.keys;
  277. var hasOwnProperty = Object.hasOwnProperty;
  278. var get = function (obj, key) {
  279. return has(obj, key) ? Option.from(obj[key]) : Option.none();
  280. };
  281. var has = function (obj, key) {
  282. return hasOwnProperty.call(obj, key);
  283. };
  284. var generate = function (cases) {
  285. if (!isArray(cases)) {
  286. throw new Error('cases must be an array');
  287. }
  288. if (cases.length === 0) {
  289. throw new Error('there must be at least one case');
  290. }
  291. var constructors = [];
  292. var adt = {};
  293. each(cases, function (acase, count) {
  294. var keys$1 = keys(acase);
  295. if (keys$1.length !== 1) {
  296. throw new Error('one and only one name per case');
  297. }
  298. var key = keys$1[0];
  299. var value = acase[key];
  300. if (adt[key] !== undefined) {
  301. throw new Error('duplicate key detected:' + key);
  302. } else if (key === 'cata') {
  303. throw new Error('cannot have a case named cata (sorry)');
  304. } else if (!isArray(value)) {
  305. throw new Error('case arguments must be an array');
  306. }
  307. constructors.push(key);
  308. adt[key] = function () {
  309. var argLength = arguments.length;
  310. if (argLength !== value.length) {
  311. throw new Error('Wrong number of arguments to case ' + key + '. Expected ' + value.length + ' (' + value + '), got ' + argLength);
  312. }
  313. var args = new Array(argLength);
  314. for (var i = 0; i < args.length; i++) {
  315. args[i] = arguments[i];
  316. }
  317. var match = function (branches) {
  318. var branchKeys = keys(branches);
  319. if (constructors.length !== branchKeys.length) {
  320. throw new Error('Wrong number of arguments to match. Expected: ' + constructors.join(',') + '\nActual: ' + branchKeys.join(','));
  321. }
  322. var allReqd = forall(constructors, function (reqKey) {
  323. return contains(branchKeys, reqKey);
  324. });
  325. if (!allReqd) {
  326. throw new Error('Not all branches were specified when using match. Specified: ' + branchKeys.join(', ') + '\nRequired: ' + constructors.join(', '));
  327. }
  328. return branches[key].apply(null, args);
  329. };
  330. return {
  331. fold: function () {
  332. if (arguments.length !== cases.length) {
  333. throw new Error('Wrong number of arguments to fold. Expected ' + cases.length + ', got ' + arguments.length);
  334. }
  335. var target = arguments[count];
  336. return target.apply(null, args);
  337. },
  338. match: match,
  339. log: function (label) {
  340. domGlobals.console.log(label, {
  341. constructors: constructors,
  342. constructor: key,
  343. params: args
  344. });
  345. }
  346. };
  347. };
  348. });
  349. return adt;
  350. };
  351. var Adt = { generate: generate };
  352. var comparison = Adt.generate([
  353. {
  354. bothErrors: [
  355. 'error1',
  356. 'error2'
  357. ]
  358. },
  359. {
  360. firstError: [
  361. 'error1',
  362. 'value2'
  363. ]
  364. },
  365. {
  366. secondError: [
  367. 'value1',
  368. 'error2'
  369. ]
  370. },
  371. {
  372. bothValues: [
  373. 'value1',
  374. 'value2'
  375. ]
  376. }
  377. ]);
  378. var partition = function (results) {
  379. var errors = [];
  380. var values = [];
  381. each(results, function (result) {
  382. result.fold(function (err) {
  383. errors.push(err);
  384. }, function (value) {
  385. values.push(value);
  386. });
  387. });
  388. return {
  389. errors: errors,
  390. values: values
  391. };
  392. };
  393. var value = function (o) {
  394. var is = function (v) {
  395. return o === v;
  396. };
  397. var or = function (opt) {
  398. return value(o);
  399. };
  400. var orThunk = function (f) {
  401. return value(o);
  402. };
  403. var map = function (f) {
  404. return value(f(o));
  405. };
  406. var mapError = function (f) {
  407. return value(o);
  408. };
  409. var each = function (f) {
  410. f(o);
  411. };
  412. var bind = function (f) {
  413. return f(o);
  414. };
  415. var fold = function (_, onValue) {
  416. return onValue(o);
  417. };
  418. var exists = function (f) {
  419. return f(o);
  420. };
  421. var forall = function (f) {
  422. return f(o);
  423. };
  424. var toOption = function () {
  425. return Option.some(o);
  426. };
  427. return {
  428. is: is,
  429. isValue: always,
  430. isError: never,
  431. getOr: constant(o),
  432. getOrThunk: constant(o),
  433. getOrDie: constant(o),
  434. or: or,
  435. orThunk: orThunk,
  436. fold: fold,
  437. map: map,
  438. mapError: mapError,
  439. each: each,
  440. bind: bind,
  441. exists: exists,
  442. forall: forall,
  443. toOption: toOption
  444. };
  445. };
  446. var error = function (message) {
  447. var getOrThunk = function (f) {
  448. return f();
  449. };
  450. var getOrDie = function () {
  451. return die(String(message))();
  452. };
  453. var or = function (opt) {
  454. return opt;
  455. };
  456. var orThunk = function (f) {
  457. return f();
  458. };
  459. var map = function (f) {
  460. return error(message);
  461. };
  462. var mapError = function (f) {
  463. return error(f(message));
  464. };
  465. var bind = function (f) {
  466. return error(message);
  467. };
  468. var fold = function (onError, _) {
  469. return onError(message);
  470. };
  471. return {
  472. is: never,
  473. isValue: never,
  474. isError: always,
  475. getOr: identity,
  476. getOrThunk: getOrThunk,
  477. getOrDie: getOrDie,
  478. or: or,
  479. orThunk: orThunk,
  480. fold: fold,
  481. map: map,
  482. mapError: mapError,
  483. each: noop,
  484. bind: bind,
  485. exists: never,
  486. forall: always,
  487. toOption: Option.none
  488. };
  489. };
  490. var fromOption = function (opt, err) {
  491. return opt.fold(function () {
  492. return error(err);
  493. }, value);
  494. };
  495. var Result = {
  496. value: value,
  497. error: error,
  498. fromOption: fromOption
  499. };
  500. var isInlinePattern = function (pattern) {
  501. return pattern.type === 'inline-command' || pattern.type === 'inline-format';
  502. };
  503. var isBlockPattern = function (pattern) {
  504. return pattern.type === 'block-command' || pattern.type === 'block-format';
  505. };
  506. var sortPatterns = function (patterns) {
  507. return sort(patterns, function (a, b) {
  508. if (a.start.length === b.start.length) {
  509. return 0;
  510. }
  511. return a.start.length > b.start.length ? -1 : 1;
  512. });
  513. };
  514. var normalizePattern = function (pattern) {
  515. var err = function (message) {
  516. return Result.error({
  517. message: message,
  518. pattern: pattern
  519. });
  520. };
  521. var formatOrCmd = function (name, onFormat, onCommand) {
  522. if (pattern.format !== undefined) {
  523. var formats = void 0;
  524. if (isArray(pattern.format)) {
  525. if (!forall(pattern.format, isString)) {
  526. return err(name + ' pattern has non-string items in the `format` array');
  527. }
  528. formats = pattern.format;
  529. } else if (isString(pattern.format)) {
  530. formats = [pattern.format];
  531. } else {
  532. return err(name + ' pattern has non-string `format` parameter');
  533. }
  534. return Result.value(onFormat(formats));
  535. } else if (pattern.cmd !== undefined) {
  536. if (!isString(pattern.cmd)) {
  537. return err(name + ' pattern has non-string `cmd` parameter');
  538. }
  539. return Result.value(onCommand(pattern.cmd, pattern.value));
  540. } else {
  541. return err(name + ' pattern is missing both `format` and `cmd` parameters');
  542. }
  543. };
  544. if (!isObject(pattern)) {
  545. return err('Raw pattern is not an object');
  546. }
  547. if (!isString(pattern.start)) {
  548. return err('Raw pattern is missing `start` parameter');
  549. }
  550. if (pattern.end !== undefined) {
  551. if (!isString(pattern.end)) {
  552. return err('Inline pattern has non-string `end` parameter');
  553. }
  554. if (pattern.start.length === 0 && pattern.end.length === 0) {
  555. return err('Inline pattern has empty `start` and `end` parameters');
  556. }
  557. var start_1 = pattern.start;
  558. var end_1 = pattern.end;
  559. if (end_1.length === 0) {
  560. end_1 = start_1;
  561. start_1 = '';
  562. }
  563. return formatOrCmd('Inline', function (format) {
  564. return {
  565. type: 'inline-format',
  566. start: start_1,
  567. end: end_1,
  568. format: format
  569. };
  570. }, function (cmd, value) {
  571. return {
  572. type: 'inline-command',
  573. start: start_1,
  574. end: end_1,
  575. cmd: cmd,
  576. value: value
  577. };
  578. });
  579. } else if (pattern.replacement !== undefined) {
  580. if (!isString(pattern.replacement)) {
  581. return err('Replacement pattern has non-string `replacement` parameter');
  582. }
  583. if (pattern.start.length === 0) {
  584. return err('Replacement pattern has empty `start` parameter');
  585. }
  586. return Result.value({
  587. type: 'inline-command',
  588. start: '',
  589. end: pattern.start,
  590. cmd: 'mceInsertContent',
  591. value: pattern.replacement
  592. });
  593. } else {
  594. if (pattern.start.length === 0) {
  595. return err('Block pattern has empty `start` parameter');
  596. }
  597. return formatOrCmd('Block', function (formats) {
  598. return {
  599. type: 'block-format',
  600. start: pattern.start,
  601. format: formats[0]
  602. };
  603. }, function (command, commandValue) {
  604. return {
  605. type: 'block-command',
  606. start: pattern.start,
  607. cmd: command,
  608. value: commandValue
  609. };
  610. });
  611. }
  612. };
  613. var denormalizePattern = function (pattern) {
  614. if (pattern.type === 'block-command') {
  615. return {
  616. start: pattern.start,
  617. cmd: pattern.cmd,
  618. value: pattern.value
  619. };
  620. } else if (pattern.type === 'block-format') {
  621. return {
  622. start: pattern.start,
  623. format: pattern.format
  624. };
  625. } else if (pattern.type === 'inline-command') {
  626. if (pattern.cmd === 'mceInsertContent' && pattern.start === '') {
  627. return {
  628. start: pattern.end,
  629. replacement: pattern.value
  630. };
  631. } else {
  632. return {
  633. start: pattern.start,
  634. end: pattern.end,
  635. cmd: pattern.cmd,
  636. value: pattern.value
  637. };
  638. }
  639. } else if (pattern.type === 'inline-format') {
  640. return {
  641. start: pattern.start,
  642. end: pattern.end,
  643. format: pattern.format.length === 1 ? pattern.format[0] : pattern.format
  644. };
  645. }
  646. };
  647. var createPatternSet = function (patterns) {
  648. return {
  649. inlinePatterns: filter(patterns, isInlinePattern),
  650. blockPatterns: sortPatterns(filter(patterns, isBlockPattern))
  651. };
  652. };
  653. var get$1 = function (patternsState) {
  654. var setPatterns = function (newPatterns) {
  655. var normalized = partition(map(newPatterns, normalizePattern));
  656. if (normalized.errors.length > 0) {
  657. var firstError = normalized.errors[0];
  658. throw new Error(firstError.message + ':\n' + JSON.stringify(firstError.pattern, null, 2));
  659. }
  660. patternsState.set(createPatternSet(normalized.values));
  661. };
  662. var getPatterns = function () {
  663. return __spreadArrays(map(patternsState.get().inlinePatterns, denormalizePattern), map(patternsState.get().blockPatterns, denormalizePattern));
  664. };
  665. return {
  666. setPatterns: setPatterns,
  667. getPatterns: getPatterns
  668. };
  669. };
  670. var Api = { get: get$1 };
  671. var Global = typeof domGlobals.window !== 'undefined' ? domGlobals.window : Function('return this;')();
  672. var error$1 = function () {
  673. var args = [];
  674. for (var _i = 0; _i < arguments.length; _i++) {
  675. args[_i] = arguments[_i];
  676. }
  677. var console = Global.console;
  678. if (console) {
  679. if (console.error) {
  680. console.error.apply(console, args);
  681. } else {
  682. console.log.apply(console, args);
  683. }
  684. }
  685. };
  686. var defaultPatterns = [
  687. {
  688. start: '*',
  689. end: '*',
  690. format: 'italic'
  691. },
  692. {
  693. start: '**',
  694. end: '**',
  695. format: 'bold'
  696. },
  697. {
  698. start: '#',
  699. format: 'h1'
  700. },
  701. {
  702. start: '##',
  703. format: 'h2'
  704. },
  705. {
  706. start: '###',
  707. format: 'h3'
  708. },
  709. {
  710. start: '####',
  711. format: 'h4'
  712. },
  713. {
  714. start: '#####',
  715. format: 'h5'
  716. },
  717. {
  718. start: '######',
  719. format: 'h6'
  720. },
  721. {
  722. start: '1. ',
  723. cmd: 'InsertOrderedList'
  724. },
  725. {
  726. start: '* ',
  727. cmd: 'InsertUnorderedList'
  728. },
  729. {
  730. start: '- ',
  731. cmd: 'InsertUnorderedList'
  732. }
  733. ];
  734. var getPatternSet = function (editorSettings) {
  735. var patterns = get(editorSettings, 'textpattern_patterns').getOr(defaultPatterns);
  736. if (!isArray(patterns)) {
  737. error$1('The setting textpattern_patterns should be an array');
  738. return {
  739. inlinePatterns: [],
  740. blockPatterns: []
  741. };
  742. }
  743. var normalized = partition(map(patterns, normalizePattern));
  744. each(normalized.errors, function (err) {
  745. return error$1(err.message, err.pattern);
  746. });
  747. return createPatternSet(normalized.values);
  748. };
  749. var getForcedRootBlock = function (editor) {
  750. var block = editor.getParam('forced_root_block', 'p');
  751. if (block === false) {
  752. return '';
  753. } else if (block === true) {
  754. return 'p';
  755. } else {
  756. return block;
  757. }
  758. };
  759. var global$1 = tinymce.util.Tools.resolve('tinymce.util.Delay');
  760. var global$2 = tinymce.util.Tools.resolve('tinymce.util.VK');
  761. var zeroWidth = '\uFEFF';
  762. var nbsp = '\xA0';
  763. var global$3 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  764. var global$4 = tinymce.util.Tools.resolve('tinymce.dom.DOMUtils');
  765. var global$5 = tinymce.util.Tools.resolve('tinymce.dom.TextSeeker');
  766. var point = function (container, offset) {
  767. return {
  768. container: container,
  769. offset: offset
  770. };
  771. };
  772. var isText = function (node) {
  773. return node.nodeType === domGlobals.Node.TEXT_NODE;
  774. };
  775. var cleanEmptyNodes = function (dom, node, isRoot) {
  776. if (node && dom.isEmpty(node) && !isRoot(node)) {
  777. var parent_1 = node.parentNode;
  778. dom.remove(node);
  779. cleanEmptyNodes(dom, parent_1, isRoot);
  780. }
  781. };
  782. var deleteRng = function (dom, rng, isRoot, clean) {
  783. if (clean === void 0) {
  784. clean = true;
  785. }
  786. var startParent = rng.startContainer.parentNode;
  787. var endParent = rng.endContainer.parentNode;
  788. rng.deleteContents();
  789. if (clean && !isRoot(rng.startContainer)) {
  790. if (isText(rng.startContainer) && rng.startContainer.data.length === 0) {
  791. dom.remove(rng.startContainer);
  792. }
  793. if (isText(rng.endContainer) && rng.endContainer.data.length === 0) {
  794. dom.remove(rng.endContainer);
  795. }
  796. cleanEmptyNodes(dom, startParent, isRoot);
  797. if (startParent !== endParent) {
  798. cleanEmptyNodes(dom, endParent, isRoot);
  799. }
  800. }
  801. };
  802. var isBlockFormatName = function (name, formatter) {
  803. var formatSet = formatter.get(name);
  804. return isArray(formatSet) && head(formatSet).exists(function (format) {
  805. return has(format, 'block');
  806. });
  807. };
  808. var isReplacementPattern = function (pattern) {
  809. return pattern.start.length === 0;
  810. };
  811. var getParentBlock = function (editor, rng) {
  812. var parentBlockOpt = Option.from(editor.dom.getParent(rng.startContainer, editor.dom.isBlock));
  813. if (getForcedRootBlock(editor) === '') {
  814. return parentBlockOpt.orThunk(function () {
  815. return Option.some(editor.getBody());
  816. });
  817. } else {
  818. return parentBlockOpt;
  819. }
  820. };
  821. var DOM = global$4.DOM;
  822. var alwaysNext = function (startNode) {
  823. return function (node) {
  824. return startNode === node ? -1 : 0;
  825. };
  826. };
  827. var isBoundary = function (dom) {
  828. return function (node) {
  829. return dom.isBlock(node) || contains([
  830. 'BR',
  831. 'IMG',
  832. 'HR',
  833. 'INPUT'
  834. ], node.nodeName) || dom.getContentEditable(node) === 'false';
  835. };
  836. };
  837. var textBefore = function (node, offset, rootNode) {
  838. if (isText(node) && offset >= 0) {
  839. return Option.some(point(node, offset));
  840. } else {
  841. var textSeeker = global$5(DOM);
  842. return Option.from(textSeeker.backwards(node, offset, alwaysNext(node), rootNode)).map(function (prev) {
  843. return point(prev.container, prev.container.data.length);
  844. });
  845. }
  846. };
  847. var textAfter = function (node, offset, rootNode) {
  848. if (isText(node) && offset >= node.length) {
  849. return Option.some(point(node, offset));
  850. } else {
  851. var textSeeker = global$5(DOM);
  852. return Option.from(textSeeker.forwards(node, offset, alwaysNext(node), rootNode)).map(function (prev) {
  853. return point(prev.container, 0);
  854. });
  855. }
  856. };
  857. var scanLeft = function (node, offset, rootNode) {
  858. if (!isText(node)) {
  859. return Option.none();
  860. }
  861. var text = node.textContent;
  862. if (offset >= 0 && offset <= text.length) {
  863. return Option.some(point(node, offset));
  864. } else {
  865. var textSeeker = global$5(DOM);
  866. return Option.from(textSeeker.backwards(node, offset, alwaysNext(node), rootNode)).bind(function (prev) {
  867. var prevText = prev.container.data;
  868. return scanLeft(prev.container, offset + prevText.length, rootNode);
  869. });
  870. }
  871. };
  872. var scanRight = function (node, offset, rootNode) {
  873. if (!isText(node)) {
  874. return Option.none();
  875. }
  876. var text = node.textContent;
  877. if (offset <= text.length) {
  878. return Option.some(point(node, offset));
  879. } else {
  880. var textSeeker = global$5(DOM);
  881. return Option.from(textSeeker.forwards(node, offset, alwaysNext(node), rootNode)).bind(function (next) {
  882. return scanRight(next.container, offset - text.length, rootNode);
  883. });
  884. }
  885. };
  886. var repeatLeft = function (dom, node, offset, process, rootNode) {
  887. var search = global$5(dom, isBoundary(dom));
  888. return Option.from(search.backwards(node, offset, process, rootNode));
  889. };
  890. var generatePath = function (root, node, offset) {
  891. if (isText(node) && (offset < 0 || offset > node.data.length)) {
  892. return [];
  893. }
  894. var p = [offset];
  895. var current = node;
  896. while (current !== root && current.parentNode) {
  897. var parent_1 = current.parentNode;
  898. for (var i = 0; i < parent_1.childNodes.length; i++) {
  899. if (parent_1.childNodes[i] === current) {
  900. p.push(i);
  901. break;
  902. }
  903. }
  904. current = parent_1;
  905. }
  906. return current === root ? p.reverse() : [];
  907. };
  908. var generatePathRange = function (root, startNode, startOffset, endNode, endOffset) {
  909. var start = generatePath(root, startNode, startOffset);
  910. var end = generatePath(root, endNode, endOffset);
  911. return {
  912. start: start,
  913. end: end
  914. };
  915. };
  916. var resolvePath = function (root, path) {
  917. var nodePath = path.slice();
  918. var offset = nodePath.pop();
  919. return foldl(nodePath, function (optNode, index) {
  920. return optNode.bind(function (node) {
  921. return Option.from(node.childNodes[index]);
  922. });
  923. }, Option.some(root)).bind(function (node) {
  924. if (isText(node) && offset >= 0 && offset <= node.data.length) {
  925. return Option.some({
  926. node: node,
  927. offset: offset
  928. });
  929. } else {
  930. return Option.some({
  931. node: node,
  932. offset: offset
  933. });
  934. }
  935. });
  936. };
  937. var resolvePathRange = function (root, range) {
  938. return resolvePath(root, range.start).bind(function (_a) {
  939. var startNode = _a.node, startOffset = _a.offset;
  940. return resolvePath(root, range.end).map(function (_a) {
  941. var endNode = _a.node, endOffset = _a.offset;
  942. var rng = domGlobals.document.createRange();
  943. rng.setStart(startNode, startOffset);
  944. rng.setEnd(endNode, endOffset);
  945. return rng;
  946. });
  947. });
  948. };
  949. var generatePathRangeFromRange = function (root, range) {
  950. return generatePathRange(root, range.startContainer, range.startOffset, range.endContainer, range.endOffset);
  951. };
  952. var stripPattern = function (dom, block, pattern) {
  953. var firstTextNode = textAfter(block, 0, block);
  954. firstTextNode.each(function (spot) {
  955. var node = spot.container;
  956. scanRight(node, pattern.start.length, block).each(function (end) {
  957. var rng = dom.createRng();
  958. rng.setStart(node, 0);
  959. rng.setEnd(end.container, end.offset);
  960. deleteRng(dom, rng, function (e) {
  961. return e === block;
  962. });
  963. });
  964. });
  965. };
  966. var applyPattern = function (editor, match) {
  967. var dom = editor.dom;
  968. var pattern = match.pattern;
  969. var rng = resolvePathRange(dom.getRoot(), match.range).getOrDie('Unable to resolve path range');
  970. getParentBlock(editor, rng).each(function (block) {
  971. if (pattern.type === 'block-format') {
  972. if (isBlockFormatName(pattern.format, editor.formatter)) {
  973. editor.undoManager.transact(function () {
  974. stripPattern(editor.dom, block, pattern);
  975. editor.formatter.apply(pattern.format);
  976. });
  977. }
  978. } else if (pattern.type === 'block-command') {
  979. editor.undoManager.transact(function () {
  980. stripPattern(editor.dom, block, pattern);
  981. editor.execCommand(pattern.cmd, false, pattern.value);
  982. });
  983. }
  984. });
  985. return true;
  986. };
  987. var findPattern = function (patterns, text) {
  988. var nuText = text.replace(nbsp, ' ');
  989. return find(patterns, function (pattern) {
  990. if (text.indexOf(pattern.start) !== 0 && nuText.indexOf(pattern.start) !== 0) {
  991. return false;
  992. }
  993. return true;
  994. });
  995. };
  996. var findPatterns = function (editor, patterns) {
  997. var dom = editor.dom;
  998. var rng = editor.selection.getRng();
  999. return getParentBlock(editor, rng).filter(function (block) {
  1000. var forcedRootBlock = getForcedRootBlock(editor);
  1001. var matchesForcedRootBlock = forcedRootBlock === '' && dom.is(block, 'body') || dom.is(block, forcedRootBlock);
  1002. return block !== null && matchesForcedRootBlock;
  1003. }).bind(function (block) {
  1004. var blockText = block.textContent;
  1005. var matchedPattern = findPattern(patterns, blockText);
  1006. return matchedPattern.map(function (pattern) {
  1007. if (global$3.trim(blockText).length === pattern.start.length) {
  1008. return [];
  1009. }
  1010. return [{
  1011. pattern: pattern,
  1012. range: generatePathRange(dom.getRoot(), block, 0, block, 0)
  1013. }];
  1014. });
  1015. }).getOr([]);
  1016. };
  1017. var applyMatches = function (editor, matches) {
  1018. if (matches.length === 0) {
  1019. return;
  1020. }
  1021. var bookmark = editor.selection.getBookmark();
  1022. each(matches, function (match) {
  1023. return applyPattern(editor, match);
  1024. });
  1025. editor.selection.moveToBookmark(bookmark);
  1026. };
  1027. var unique = 0;
  1028. var generate$1 = function (prefix) {
  1029. var date = new Date();
  1030. var time = date.getTime();
  1031. var random = Math.floor(Math.random() * 1000000000);
  1032. unique++;
  1033. return prefix + '_' + random + unique + String(time);
  1034. };
  1035. var checkRange = function (str, substr, start) {
  1036. if (substr === '') {
  1037. return true;
  1038. }
  1039. if (str.length < substr.length) {
  1040. return false;
  1041. }
  1042. var x = str.substr(start, start + substr.length);
  1043. return x === substr;
  1044. };
  1045. var endsWith = function (str, suffix) {
  1046. return checkRange(str, suffix, str.length - suffix.length);
  1047. };
  1048. var newMarker = function (dom, id) {
  1049. return dom.create('span', {
  1050. 'data-mce-type': 'bookmark',
  1051. 'id': id
  1052. });
  1053. };
  1054. var rangeFromMarker = function (dom, marker) {
  1055. var rng = dom.createRng();
  1056. rng.setStartAfter(marker.start);
  1057. rng.setEndBefore(marker.end);
  1058. return rng;
  1059. };
  1060. var createMarker = function (dom, markerPrefix, pathRange) {
  1061. var rng = resolvePathRange(dom.getRoot(), pathRange).getOrDie('Unable to resolve path range');
  1062. var startNode = rng.startContainer;
  1063. var endNode = rng.endContainer;
  1064. var textEnd = rng.endOffset === 0 ? endNode : endNode.splitText(rng.endOffset);
  1065. var textStart = rng.startOffset === 0 ? startNode : startNode.splitText(rng.startOffset);
  1066. return {
  1067. prefix: markerPrefix,
  1068. end: textEnd.parentNode.insertBefore(newMarker(dom, markerPrefix + '-end'), textEnd),
  1069. start: textStart.parentNode.insertBefore(newMarker(dom, markerPrefix + '-start'), textStart)
  1070. };
  1071. };
  1072. var removeMarker = function (dom, marker, isRoot) {
  1073. cleanEmptyNodes(dom, dom.get(marker.prefix + '-end'), isRoot);
  1074. cleanEmptyNodes(dom, dom.get(marker.prefix + '-start'), isRoot);
  1075. };
  1076. var matchesPattern = function (dom, block, patternContent) {
  1077. return function (element, offset) {
  1078. var text = element.data;
  1079. var searchText = text.substring(0, offset);
  1080. var startEndIndex = searchText.lastIndexOf(patternContent.charAt(patternContent.length - 1));
  1081. var startIndex = searchText.lastIndexOf(patternContent);
  1082. if (startIndex !== -1) {
  1083. return startIndex + patternContent.length;
  1084. } else if (startEndIndex !== -1) {
  1085. return startEndIndex + 1;
  1086. } else {
  1087. return -1;
  1088. }
  1089. };
  1090. };
  1091. var findPatternStartFromSpot = function (dom, pattern, block, spot) {
  1092. var startPattern = pattern.start;
  1093. var startSpot = repeatLeft(dom, spot.container, spot.offset, matchesPattern(dom, block, startPattern), block);
  1094. return startSpot.bind(function (spot) {
  1095. if (spot.offset >= startPattern.length) {
  1096. var rng = dom.createRng();
  1097. rng.setStart(spot.container, spot.offset - startPattern.length);
  1098. rng.setEnd(spot.container, spot.offset);
  1099. return Option.some(rng);
  1100. } else {
  1101. var offset = spot.offset - startPattern.length;
  1102. return scanLeft(spot.container, offset, block).map(function (nextSpot) {
  1103. var rng = dom.createRng();
  1104. rng.setStart(nextSpot.container, nextSpot.offset);
  1105. rng.setEnd(spot.container, spot.offset);
  1106. return rng;
  1107. }).filter(function (rng) {
  1108. return rng.toString() === startPattern;
  1109. }).orThunk(function () {
  1110. return findPatternStartFromSpot(dom, pattern, block, point(spot.container, 0));
  1111. });
  1112. }
  1113. });
  1114. };
  1115. var findPatternStart = function (dom, pattern, node, offset, block, requireGap) {
  1116. if (requireGap === void 0) {
  1117. requireGap = false;
  1118. }
  1119. if (pattern.start.length === 0 && !requireGap) {
  1120. var rng = dom.createRng();
  1121. rng.setStart(node, offset);
  1122. rng.setEnd(node, offset);
  1123. return Option.some(rng);
  1124. }
  1125. return textBefore(node, offset, block).bind(function (spot) {
  1126. var start = findPatternStartFromSpot(dom, pattern, block, spot);
  1127. return start.bind(function (startRange) {
  1128. if (requireGap) {
  1129. if (startRange.endContainer === spot.container && startRange.endOffset === spot.offset) {
  1130. return Option.none();
  1131. } else if (spot.offset === 0 && startRange.endContainer.textContent.length === startRange.endOffset) {
  1132. return Option.none();
  1133. }
  1134. }
  1135. return Option.some(startRange);
  1136. });
  1137. });
  1138. };
  1139. var findPattern$1 = function (editor, block, details) {
  1140. var dom = editor.dom;
  1141. var root = dom.getRoot();
  1142. var pattern = details.pattern;
  1143. var endNode = details.position.container;
  1144. var endOffset = details.position.offset;
  1145. return scanLeft(endNode, endOffset - details.pattern.end.length, block).bind(function (spot) {
  1146. var endPathRng = generatePathRange(root, spot.container, spot.offset, endNode, endOffset);
  1147. if (isReplacementPattern(pattern)) {
  1148. return Option.some({
  1149. matches: [{
  1150. pattern: pattern,
  1151. startRng: endPathRng,
  1152. endRng: endPathRng
  1153. }],
  1154. position: spot
  1155. });
  1156. } else {
  1157. var resultsOpt = findPatternsRec(editor, details.remainingPatterns, spot.container, spot.offset, block);
  1158. var results_1 = resultsOpt.getOr({
  1159. matches: [],
  1160. position: spot
  1161. });
  1162. var pos = results_1.position;
  1163. var start = findPatternStart(dom, pattern, pos.container, pos.offset, block, resultsOpt.isNone());
  1164. return start.map(function (startRng) {
  1165. var startPathRng = generatePathRangeFromRange(root, startRng);
  1166. return {
  1167. matches: results_1.matches.concat([{
  1168. pattern: pattern,
  1169. startRng: startPathRng,
  1170. endRng: endPathRng
  1171. }]),
  1172. position: point(startRng.startContainer, startRng.startOffset)
  1173. };
  1174. });
  1175. }
  1176. });
  1177. };
  1178. var findPatternsRec = function (editor, patterns, node, offset, block) {
  1179. var dom = editor.dom;
  1180. return textBefore(node, offset, dom.getRoot()).bind(function (endSpot) {
  1181. var rng = dom.createRng();
  1182. rng.setStart(block, 0);
  1183. rng.setEnd(node, offset);
  1184. var text = rng.toString();
  1185. for (var i = 0; i < patterns.length; i++) {
  1186. var pattern = patterns[i];
  1187. if (!endsWith(text, pattern.end)) {
  1188. continue;
  1189. }
  1190. var patternsWithoutCurrent = patterns.slice();
  1191. patternsWithoutCurrent.splice(i, 1);
  1192. var result = findPattern$1(editor, block, {
  1193. pattern: pattern,
  1194. remainingPatterns: patternsWithoutCurrent,
  1195. position: endSpot
  1196. });
  1197. if (result.isSome()) {
  1198. return result;
  1199. }
  1200. }
  1201. return Option.none();
  1202. });
  1203. };
  1204. var applyPattern$1 = function (editor, pattern, patternRange) {
  1205. editor.selection.setRng(patternRange);
  1206. if (pattern.type === 'inline-format') {
  1207. each(pattern.format, function (format) {
  1208. editor.formatter.apply(format);
  1209. });
  1210. } else {
  1211. editor.execCommand(pattern.cmd, false, pattern.value);
  1212. }
  1213. };
  1214. var applyReplacementPattern = function (editor, pattern, marker, isRoot) {
  1215. var markerRange = rangeFromMarker(editor.dom, marker);
  1216. deleteRng(editor.dom, markerRange, isRoot);
  1217. applyPattern$1(editor, pattern, markerRange);
  1218. };
  1219. var applyPatternWithContent = function (editor, pattern, startMarker, endMarker, isRoot) {
  1220. var dom = editor.dom;
  1221. var markerEndRange = rangeFromMarker(dom, endMarker);
  1222. var markerStartRange = rangeFromMarker(dom, startMarker);
  1223. deleteRng(dom, markerStartRange, isRoot);
  1224. deleteRng(dom, markerEndRange, isRoot);
  1225. var patternMarker = {
  1226. prefix: startMarker.prefix,
  1227. start: startMarker.end,
  1228. end: endMarker.start
  1229. };
  1230. var patternRange = rangeFromMarker(dom, patternMarker);
  1231. applyPattern$1(editor, pattern, patternRange);
  1232. };
  1233. var addMarkers = function (dom, matches) {
  1234. var markerPrefix = generate$1('mce_textpattern');
  1235. var matchesWithEnds = foldr(matches, function (acc, match) {
  1236. var endMarker = createMarker(dom, markerPrefix + ('_end' + acc.length), match.endRng);
  1237. return acc.concat([__assign(__assign({}, match), { endMarker: endMarker })]);
  1238. }, []);
  1239. return foldr(matchesWithEnds, function (acc, match) {
  1240. var idx = matchesWithEnds.length - acc.length - 1;
  1241. var startMarker = isReplacementPattern(match.pattern) ? match.endMarker : createMarker(dom, markerPrefix + ('_start' + idx), match.startRng);
  1242. return acc.concat([__assign(__assign({}, match), { startMarker: startMarker })]);
  1243. }, []);
  1244. };
  1245. var findPatterns$1 = function (editor, patterns, space) {
  1246. var rng = editor.selection.getRng();
  1247. if (rng.collapsed === false) {
  1248. return [];
  1249. }
  1250. return getParentBlock(editor, rng).bind(function (block) {
  1251. var offset = rng.startOffset - (space ? 1 : 0);
  1252. return findPatternsRec(editor, patterns, rng.startContainer, offset, block);
  1253. }).fold(function () {
  1254. return [];
  1255. }, function (result) {
  1256. return result.matches;
  1257. });
  1258. };
  1259. var applyMatches$1 = function (editor, matches) {
  1260. if (matches.length === 0) {
  1261. return;
  1262. }
  1263. var dom = editor.dom;
  1264. var bookmark = editor.selection.getBookmark();
  1265. var matchesWithMarkers = addMarkers(dom, matches);
  1266. each(matchesWithMarkers, function (match) {
  1267. var block = dom.getParent(match.startMarker.start, dom.isBlock);
  1268. var isRoot = function (node) {
  1269. return node === block;
  1270. };
  1271. if (isReplacementPattern(match.pattern)) {
  1272. applyReplacementPattern(editor, match.pattern, match.endMarker, isRoot);
  1273. } else {
  1274. applyPatternWithContent(editor, match.pattern, match.startMarker, match.endMarker, isRoot);
  1275. }
  1276. removeMarker(dom, match.endMarker, isRoot);
  1277. removeMarker(dom, match.startMarker, isRoot);
  1278. });
  1279. editor.selection.moveToBookmark(bookmark);
  1280. };
  1281. var handleEnter = function (editor, patternSet) {
  1282. if (!editor.selection.isCollapsed()) {
  1283. return false;
  1284. }
  1285. var inlineMatches = findPatterns$1(editor, patternSet.inlinePatterns, false);
  1286. var blockMatches = findPatterns(editor, patternSet.blockPatterns);
  1287. if (blockMatches.length > 0 || inlineMatches.length > 0) {
  1288. editor.undoManager.add();
  1289. editor.undoManager.extra(function () {
  1290. editor.execCommand('mceInsertNewLine');
  1291. }, function () {
  1292. editor.insertContent(zeroWidth);
  1293. applyMatches$1(editor, inlineMatches);
  1294. applyMatches(editor, blockMatches);
  1295. var range = editor.selection.getRng();
  1296. var spot = textBefore(range.startContainer, range.startOffset, editor.dom.getRoot());
  1297. editor.execCommand('mceInsertNewLine');
  1298. spot.each(function (s) {
  1299. var node = s.container;
  1300. if (node.data.charAt(s.offset - 1) === zeroWidth) {
  1301. node.deleteData(s.offset - 1, 1);
  1302. cleanEmptyNodes(editor.dom, node.parentNode, function (e) {
  1303. return e === editor.dom.getRoot();
  1304. });
  1305. }
  1306. });
  1307. });
  1308. return true;
  1309. }
  1310. return false;
  1311. };
  1312. var handleInlineKey = function (editor, patternSet) {
  1313. var inlineMatches = findPatterns$1(editor, patternSet.inlinePatterns, true);
  1314. if (inlineMatches.length > 0) {
  1315. editor.undoManager.transact(function () {
  1316. applyMatches$1(editor, inlineMatches);
  1317. });
  1318. }
  1319. };
  1320. var checkKeyEvent = function (codes, event, predicate) {
  1321. for (var i = 0; i < codes.length; i++) {
  1322. if (predicate(codes[i], event)) {
  1323. return true;
  1324. }
  1325. }
  1326. };
  1327. var checkKeyCode = function (codes, event) {
  1328. return checkKeyEvent(codes, event, function (code, event) {
  1329. return code === event.keyCode && global$2.modifierPressed(event) === false;
  1330. });
  1331. };
  1332. var checkCharCode = function (chars, event) {
  1333. return checkKeyEvent(chars, event, function (chr, event) {
  1334. return chr.charCodeAt(0) === event.charCode;
  1335. });
  1336. };
  1337. var KeyHandler = {
  1338. handleEnter: handleEnter,
  1339. handleInlineKey: handleInlineKey,
  1340. checkCharCode: checkCharCode,
  1341. checkKeyCode: checkKeyCode
  1342. };
  1343. var setup = function (editor, patternsState) {
  1344. var charCodes = [
  1345. ',',
  1346. '.',
  1347. ';',
  1348. ':',
  1349. '!',
  1350. '?'
  1351. ];
  1352. var keyCodes = [32];
  1353. editor.on('keydown', function (e) {
  1354. if (e.keyCode === 13 && !global$2.modifierPressed(e)) {
  1355. if (KeyHandler.handleEnter(editor, patternsState.get())) {
  1356. e.preventDefault();
  1357. }
  1358. }
  1359. }, true);
  1360. editor.on('keyup', function (e) {
  1361. if (KeyHandler.checkKeyCode(keyCodes, e)) {
  1362. KeyHandler.handleInlineKey(editor, patternsState.get());
  1363. }
  1364. });
  1365. editor.on('keypress', function (e) {
  1366. if (KeyHandler.checkCharCode(charCodes, e)) {
  1367. global$1.setEditorTimeout(editor, function () {
  1368. KeyHandler.handleInlineKey(editor, patternsState.get());
  1369. });
  1370. }
  1371. });
  1372. };
  1373. var Keyboard = { setup: setup };
  1374. function Plugin () {
  1375. global.add('textpattern', function (editor) {
  1376. var patternsState = Cell(getPatternSet(editor.settings));
  1377. Keyboard.setup(editor, patternsState);
  1378. return Api.get(patternsState);
  1379. });
  1380. }
  1381. Plugin();
  1382. }(window));