plugin.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  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.1.0 (2019-10-17)
  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 = function () {
  762. return '\uFEFF';
  763. };
  764. var global$3 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  765. var global$4 = tinymce.util.Tools.resolve('tinymce.dom.TreeWalker');
  766. var isText = function (node) {
  767. return node.nodeType === domGlobals.Node.TEXT_NODE;
  768. };
  769. var cleanEmptyNodes = function (dom, node, isRoot) {
  770. if (node && dom.isEmpty(node) && !isRoot(node)) {
  771. var parent = node.parentNode;
  772. dom.remove(node);
  773. cleanEmptyNodes(dom, parent, isRoot);
  774. }
  775. };
  776. var deleteRng = function (dom, rng, isRoot, clean) {
  777. if (clean === void 0) {
  778. clean = true;
  779. }
  780. var startParent = rng.startContainer.parentNode;
  781. var endParent = rng.endContainer.parentNode;
  782. rng.deleteContents();
  783. if (clean && !isRoot(rng.startContainer)) {
  784. if (isText(rng.startContainer) && rng.startContainer.data.length === 0) {
  785. dom.remove(rng.startContainer);
  786. }
  787. if (isText(rng.endContainer) && rng.endContainer.data.length === 0) {
  788. dom.remove(rng.endContainer);
  789. }
  790. cleanEmptyNodes(dom, startParent, isRoot);
  791. if (startParent !== endParent) {
  792. cleanEmptyNodes(dom, endParent, isRoot);
  793. }
  794. }
  795. };
  796. var isBlockFormatName = function (name, formatter) {
  797. var formatSet = formatter.get(name);
  798. return isArray(formatSet) && head(formatSet).exists(function (format) {
  799. return has(format, 'block');
  800. });
  801. };
  802. var isInlinePattern$1 = function (pattern) {
  803. return has(pattern, 'end');
  804. };
  805. var isReplacementPattern = function (pattern) {
  806. return pattern.start.length === 0;
  807. };
  808. var findPattern = function (patterns, text) {
  809. return find(patterns, function (pattern) {
  810. if (text.indexOf(pattern.start) !== 0) {
  811. return false;
  812. }
  813. if (isInlinePattern$1(pattern) && pattern.end && text.lastIndexOf(pattern.end) !== text.length - pattern.end.length) {
  814. return false;
  815. }
  816. return true;
  817. });
  818. };
  819. var getParentBlock = function (editor, rng) {
  820. var parentBlockOpt = Option.from(editor.dom.getParent(rng.startContainer, editor.dom.isBlock));
  821. if (getForcedRootBlock(editor) === '') {
  822. return parentBlockOpt.orThunk(function () {
  823. return Option.some(editor.getBody());
  824. });
  825. } else {
  826. return parentBlockOpt;
  827. }
  828. };
  829. var point = function (element, offset) {
  830. return {
  831. element: element,
  832. offset: offset
  833. };
  834. };
  835. var TextWalker = function (startNode, rootNode) {
  836. var walker = new global$4(startNode, rootNode);
  837. var walk = function (direction) {
  838. var next = walker[direction]();
  839. while (next && next.nodeType !== domGlobals.Node.TEXT_NODE) {
  840. next = walker[direction]();
  841. }
  842. return next && next.nodeType === domGlobals.Node.TEXT_NODE ? Option.some(next) : Option.none();
  843. };
  844. return {
  845. next: function () {
  846. return walk('next');
  847. },
  848. prev: function () {
  849. return walk('prev');
  850. },
  851. prev2: function () {
  852. return walk('prev2');
  853. }
  854. };
  855. };
  856. var textBefore = function (node, offset, rootNode) {
  857. if (isText(node) && offset >= 0) {
  858. return Option.some(point(node, offset));
  859. } else {
  860. var textWalker = TextWalker(node, rootNode);
  861. return textWalker.prev().map(function (prev) {
  862. return point(prev, prev.data.length);
  863. });
  864. }
  865. };
  866. var scanLeft = function (node, offset, rootNode) {
  867. if (!isText(node)) {
  868. return Option.none();
  869. }
  870. var text = node.textContent;
  871. if (offset >= 0 && offset <= text.length) {
  872. return Option.some(point(node, offset));
  873. } else {
  874. var textWalker = TextWalker(node, rootNode);
  875. return textWalker.prev().bind(function (prev) {
  876. var prevText = prev.textContent;
  877. return scanLeft(prev, offset + prevText.length, rootNode);
  878. });
  879. }
  880. };
  881. var scanRight = function (node, offset, rootNode) {
  882. if (!isText(node)) {
  883. return Option.none();
  884. }
  885. var text = node.textContent;
  886. if (offset <= text.length) {
  887. return Option.some(point(node, offset));
  888. } else {
  889. var textWalker = TextWalker(node, rootNode);
  890. return textWalker.next().bind(function (next) {
  891. return scanRight(next, offset - text.length, rootNode);
  892. });
  893. }
  894. };
  895. var isBoundary = function (dom, node) {
  896. return dom.isBlock(node) || contains([
  897. 'BR',
  898. 'IMG',
  899. 'HR',
  900. 'INPUT'
  901. ], node.nodeName) || dom.getContentEditable(node) === 'false';
  902. };
  903. var outcome = Adt.generate([
  904. { aborted: [] },
  905. { edge: ['element'] },
  906. { success: ['info'] }
  907. ]);
  908. var phase = Adt.generate([
  909. { abort: [] },
  910. { kontinue: [] },
  911. { finish: ['info'] }
  912. ]);
  913. var repeat = function (dom, node, offset, process, walker, recent) {
  914. var terminate = function () {
  915. return recent.fold(outcome.aborted, outcome.edge);
  916. };
  917. var recurse = function () {
  918. var next = walker();
  919. if (next) {
  920. return repeat(dom, next, Option.none(), process, walker, Option.some(node));
  921. } else {
  922. return terminate();
  923. }
  924. };
  925. if (isBoundary(dom, node)) {
  926. return terminate();
  927. } else if (!isText(node)) {
  928. return recurse();
  929. } else {
  930. var text = node.textContent;
  931. return process(phase, node, text, offset).fold(outcome.aborted, function () {
  932. return recurse();
  933. }, outcome.success);
  934. }
  935. };
  936. var repeatLeft = function (dom, node, offset, process, rootNode) {
  937. var walker = new global$4(node, rootNode);
  938. return repeat(dom, node, Option.some(offset), process, walker.prev, Option.none());
  939. };
  940. var generatePath = function (root, node, offset) {
  941. if (isText(node) && (offset < 0 || offset > node.data.length)) {
  942. return [];
  943. }
  944. var p = [offset];
  945. var current = node;
  946. while (current !== root && current.parentNode) {
  947. var parent = current.parentNode;
  948. for (var i = 0; i < parent.childNodes.length; i++) {
  949. if (parent.childNodes[i] === current) {
  950. p.push(i);
  951. break;
  952. }
  953. }
  954. current = parent;
  955. }
  956. return current === root ? p.reverse() : [];
  957. };
  958. var generatePathRange = function (root, startNode, startOffset, endNode, endOffset) {
  959. var start = generatePath(root, startNode, startOffset);
  960. var end = generatePath(root, endNode, endOffset);
  961. return {
  962. start: start,
  963. end: end
  964. };
  965. };
  966. var resolvePath = function (root, path) {
  967. var nodePath = path.slice();
  968. var offset = nodePath.pop();
  969. return foldl(nodePath, function (optNode, index) {
  970. return optNode.bind(function (node) {
  971. return Option.from(node.childNodes[index]);
  972. });
  973. }, Option.some(root)).bind(function (node) {
  974. if (isText(node) && offset >= 0 && offset <= node.data.length) {
  975. return Option.some({
  976. node: node,
  977. offset: offset
  978. });
  979. } else {
  980. return Option.some({
  981. node: node,
  982. offset: offset
  983. });
  984. }
  985. });
  986. };
  987. var resolvePathRange = function (root, range) {
  988. return resolvePath(root, range.start).bind(function (_a) {
  989. var startNode = _a.node, startOffset = _a.offset;
  990. return resolvePath(root, range.end).map(function (_a) {
  991. var endNode = _a.node, endOffset = _a.offset;
  992. var rng = domGlobals.document.createRange();
  993. rng.setStart(startNode, startOffset);
  994. rng.setEnd(endNode, endOffset);
  995. return rng;
  996. });
  997. });
  998. };
  999. var generatePathRangeFromRange = function (root, range) {
  1000. return generatePathRange(root, range.startContainer, range.startOffset, range.endContainer, range.endOffset);
  1001. };
  1002. var stripPattern = function (dom, block, pattern) {
  1003. var firstTextNode = TextWalker(block, block).next();
  1004. firstTextNode.each(function (node) {
  1005. scanRight(node, pattern.start.length, block).each(function (end) {
  1006. var rng = dom.createRng();
  1007. rng.setStart(node, 0);
  1008. rng.setEnd(end.element, end.offset);
  1009. deleteRng(dom, rng, function (e) {
  1010. return e === block;
  1011. });
  1012. });
  1013. });
  1014. };
  1015. var applyPattern = function (editor, match) {
  1016. var dom = editor.dom;
  1017. var pattern = match.pattern;
  1018. var rng = resolvePathRange(dom.getRoot(), match.range).getOrDie('Unable to resolve path range');
  1019. getParentBlock(editor, rng).each(function (block) {
  1020. if (pattern.type === 'block-format') {
  1021. if (isBlockFormatName(pattern.format, editor.formatter)) {
  1022. editor.undoManager.transact(function () {
  1023. stripPattern(editor.dom, block, pattern);
  1024. editor.formatter.apply(pattern.format);
  1025. });
  1026. }
  1027. } else if (pattern.type === 'block-command') {
  1028. editor.undoManager.transact(function () {
  1029. stripPattern(editor.dom, block, pattern);
  1030. editor.execCommand(pattern.cmd, false, pattern.value);
  1031. });
  1032. }
  1033. });
  1034. return true;
  1035. };
  1036. var findPatterns = function (editor, patterns) {
  1037. var dom = editor.dom;
  1038. var rng = editor.selection.getRng();
  1039. return getParentBlock(editor, rng).filter(function (block) {
  1040. var forcedRootBlock = getForcedRootBlock(editor);
  1041. var matchesForcedRootBlock = forcedRootBlock === '' && dom.is(block, 'body') || dom.is(block, forcedRootBlock);
  1042. return block !== null && matchesForcedRootBlock;
  1043. }).bind(function (block) {
  1044. var blockText = block.textContent;
  1045. var matchedPattern = findPattern(patterns, blockText);
  1046. return matchedPattern.map(function (pattern) {
  1047. if (global$3.trim(blockText).length === pattern.start.length) {
  1048. return [];
  1049. }
  1050. return [{
  1051. pattern: pattern,
  1052. range: generatePathRange(dom.getRoot(), block, 0, block, 0)
  1053. }];
  1054. });
  1055. }).getOr([]);
  1056. };
  1057. var applyMatches = function (editor, matches) {
  1058. if (matches.length === 0) {
  1059. return;
  1060. }
  1061. var bookmark = editor.selection.getBookmark();
  1062. each(matches, function (match) {
  1063. return applyPattern(editor, match);
  1064. });
  1065. editor.selection.moveToBookmark(bookmark);
  1066. };
  1067. var unique = 0;
  1068. var generate$1 = function (prefix) {
  1069. var date = new Date();
  1070. var time = date.getTime();
  1071. var random = Math.floor(Math.random() * 1000000000);
  1072. unique++;
  1073. return prefix + '_' + random + unique + String(time);
  1074. };
  1075. var checkRange = function (str, substr, start) {
  1076. if (substr === '') {
  1077. return true;
  1078. }
  1079. if (str.length < substr.length) {
  1080. return false;
  1081. }
  1082. var x = str.substr(start, start + substr.length);
  1083. return x === substr;
  1084. };
  1085. var endsWith = function (str, suffix) {
  1086. return checkRange(str, suffix, str.length - suffix.length);
  1087. };
  1088. var newMarker = function (dom, id) {
  1089. return dom.create('span', {
  1090. 'data-mce-type': 'bookmark',
  1091. 'id': id
  1092. });
  1093. };
  1094. var rangeFromMarker = function (dom, marker) {
  1095. var rng = dom.createRng();
  1096. rng.setStartAfter(marker.start);
  1097. rng.setEndBefore(marker.end);
  1098. return rng;
  1099. };
  1100. var createMarker = function (dom, markerPrefix, pathRange) {
  1101. var rng = resolvePathRange(dom.getRoot(), pathRange).getOrDie('Unable to resolve path range');
  1102. var startNode = rng.startContainer;
  1103. var endNode = rng.endContainer;
  1104. var textEnd = rng.endOffset === 0 ? endNode : endNode.splitText(rng.endOffset);
  1105. var textStart = rng.startOffset === 0 ? startNode : startNode.splitText(rng.startOffset);
  1106. return {
  1107. prefix: markerPrefix,
  1108. end: textEnd.parentNode.insertBefore(newMarker(dom, markerPrefix + '-end'), textEnd),
  1109. start: textStart.parentNode.insertBefore(newMarker(dom, markerPrefix + '-start'), textStart)
  1110. };
  1111. };
  1112. var removeMarker = function (dom, marker, isRoot) {
  1113. cleanEmptyNodes(dom, dom.get(marker.prefix + '-end'), isRoot);
  1114. cleanEmptyNodes(dom, dom.get(marker.prefix + '-start'), isRoot);
  1115. };
  1116. var nodeMatchesPattern = function (dom, block, content) {
  1117. return function (phase, element, text, optOffset) {
  1118. if (element === block) {
  1119. return phase.abort();
  1120. }
  1121. var searchText = text.substring(0, optOffset.getOr(text.length));
  1122. var startEndIndex = searchText.lastIndexOf(content.charAt(content.length - 1));
  1123. var startIndex = searchText.lastIndexOf(content);
  1124. if (startIndex !== -1) {
  1125. var rng = dom.createRng();
  1126. rng.setStart(element, startIndex);
  1127. rng.setEnd(element, startIndex + content.length);
  1128. return phase.finish(rng);
  1129. } else if (startEndIndex !== -1) {
  1130. return scanLeft(element, startEndIndex + 1 - content.length, block).fold(function () {
  1131. return phase.kontinue();
  1132. }, function (spot) {
  1133. var rng = dom.createRng();
  1134. rng.setStart(spot.element, spot.offset);
  1135. rng.setEnd(element, startEndIndex + 1);
  1136. if (rng.toString() === content) {
  1137. return phase.finish(rng);
  1138. } else {
  1139. return phase.kontinue();
  1140. }
  1141. });
  1142. } else {
  1143. return phase.kontinue();
  1144. }
  1145. };
  1146. };
  1147. var findPatternStart = function (dom, pattern, node, offset, block, requireGap) {
  1148. if (requireGap === void 0) {
  1149. requireGap = false;
  1150. }
  1151. if (pattern.start.length === 0 && !requireGap) {
  1152. var rng = dom.createRng();
  1153. rng.setStart(node, offset);
  1154. rng.setEnd(node, offset);
  1155. return Option.some(rng);
  1156. }
  1157. return textBefore(node, offset, block).bind(function (spot) {
  1158. var outcome = repeatLeft(dom, spot.element, spot.offset, nodeMatchesPattern(dom, block, pattern.start), block);
  1159. var start = outcome.fold(Option.none, Option.none, Option.some);
  1160. return start.bind(function (startRange) {
  1161. if (requireGap) {
  1162. if (startRange.endContainer === spot.element && startRange.endOffset === spot.offset) {
  1163. return Option.none();
  1164. } else if (spot.offset === 0 && startRange.endContainer.textContent.length === startRange.endOffset) {
  1165. return Option.none();
  1166. }
  1167. }
  1168. return Option.some(startRange);
  1169. });
  1170. });
  1171. };
  1172. var findPattern$1 = function (editor, block, details) {
  1173. var dom = editor.dom;
  1174. var root = dom.getRoot();
  1175. var pattern = details.pattern;
  1176. var endNode = details.position.element;
  1177. var endOffset = details.position.offset;
  1178. return scanLeft(endNode, endOffset - details.pattern.end.length, block).bind(function (spot) {
  1179. var endPathRng = generatePathRange(root, spot.element, spot.offset, endNode, endOffset);
  1180. if (isReplacementPattern(pattern)) {
  1181. return Option.some({
  1182. matches: [{
  1183. pattern: pattern,
  1184. startRng: endPathRng,
  1185. endRng: endPathRng
  1186. }],
  1187. position: spot
  1188. });
  1189. } else {
  1190. var resultsOpt = findPatternsRec(editor, details.remainingPatterns, spot.element, spot.offset, block);
  1191. var results_1 = resultsOpt.getOr({
  1192. matches: [],
  1193. position: spot
  1194. });
  1195. var pos = results_1.position;
  1196. var start = findPatternStart(dom, pattern, pos.element, pos.offset, block, resultsOpt.isNone());
  1197. return start.map(function (startRng) {
  1198. var startPathRng = generatePathRangeFromRange(root, startRng);
  1199. return {
  1200. matches: results_1.matches.concat([{
  1201. pattern: pattern,
  1202. startRng: startPathRng,
  1203. endRng: endPathRng
  1204. }]),
  1205. position: point(startRng.startContainer, startRng.startOffset)
  1206. };
  1207. });
  1208. }
  1209. });
  1210. };
  1211. var findPatternsRec = function (editor, patterns, node, offset, block) {
  1212. var dom = editor.dom;
  1213. return textBefore(node, offset, dom.getRoot()).bind(function (endSpot) {
  1214. var rng = dom.createRng();
  1215. rng.setStart(block, 0);
  1216. rng.setEnd(node, offset);
  1217. var text = rng.toString();
  1218. for (var i = 0; i < patterns.length; i++) {
  1219. var pattern = patterns[i];
  1220. if (!endsWith(text, pattern.end)) {
  1221. continue;
  1222. }
  1223. var patternsWithoutCurrent = patterns.slice();
  1224. patternsWithoutCurrent.splice(i, 1);
  1225. var result = findPattern$1(editor, block, {
  1226. pattern: pattern,
  1227. remainingPatterns: patternsWithoutCurrent,
  1228. position: endSpot
  1229. });
  1230. if (result.isSome()) {
  1231. return result;
  1232. }
  1233. }
  1234. return Option.none();
  1235. });
  1236. };
  1237. var applyPattern$1 = function (editor, pattern, patternRange) {
  1238. editor.selection.setRng(patternRange);
  1239. if (pattern.type === 'inline-format') {
  1240. each(pattern.format, function (format) {
  1241. editor.formatter.apply(format);
  1242. });
  1243. } else {
  1244. editor.execCommand(pattern.cmd, false, pattern.value);
  1245. }
  1246. };
  1247. var applyReplacementPattern = function (editor, pattern, marker, isRoot) {
  1248. var markerRange = rangeFromMarker(editor.dom, marker);
  1249. deleteRng(editor.dom, markerRange, isRoot);
  1250. applyPattern$1(editor, pattern, markerRange);
  1251. };
  1252. var applyPatternWithContent = function (editor, pattern, startMarker, endMarker, isRoot) {
  1253. var dom = editor.dom;
  1254. var markerEndRange = rangeFromMarker(dom, endMarker);
  1255. var markerStartRange = rangeFromMarker(dom, startMarker);
  1256. deleteRng(dom, markerStartRange, isRoot);
  1257. deleteRng(dom, markerEndRange, isRoot);
  1258. var patternMarker = {
  1259. prefix: startMarker.prefix,
  1260. start: startMarker.end,
  1261. end: endMarker.start
  1262. };
  1263. var patternRange = rangeFromMarker(dom, patternMarker);
  1264. applyPattern$1(editor, pattern, patternRange);
  1265. };
  1266. var addMarkers = function (dom, matches) {
  1267. var markerPrefix = generate$1('mce_textpattern');
  1268. var matchesWithEnds = foldr(matches, function (acc, match) {
  1269. var endMarker = createMarker(dom, markerPrefix + ('_end' + acc.length), match.endRng);
  1270. return acc.concat([__assign(__assign({}, match), { endMarker: endMarker })]);
  1271. }, []);
  1272. return foldr(matchesWithEnds, function (acc, match) {
  1273. var idx = matchesWithEnds.length - acc.length - 1;
  1274. var startMarker = isReplacementPattern(match.pattern) ? match.endMarker : createMarker(dom, markerPrefix + ('_start' + idx), match.startRng);
  1275. return acc.concat([__assign(__assign({}, match), { startMarker: startMarker })]);
  1276. }, []);
  1277. };
  1278. var findPatterns$1 = function (editor, patterns, space) {
  1279. var rng = editor.selection.getRng();
  1280. if (rng.collapsed === false) {
  1281. return [];
  1282. }
  1283. return getParentBlock(editor, rng).bind(function (block) {
  1284. var offset = rng.startOffset - (space ? 1 : 0);
  1285. return findPatternsRec(editor, patterns, rng.startContainer, offset, block);
  1286. }).fold(function () {
  1287. return [];
  1288. }, function (result) {
  1289. return result.matches;
  1290. });
  1291. };
  1292. var applyMatches$1 = function (editor, matches) {
  1293. if (matches.length === 0) {
  1294. return;
  1295. }
  1296. var dom = editor.dom;
  1297. var bookmark = editor.selection.getBookmark();
  1298. var matchesWithMarkers = addMarkers(dom, matches);
  1299. each(matchesWithMarkers, function (match) {
  1300. var block = dom.getParent(match.startMarker.start, dom.isBlock);
  1301. var isRoot = function (node) {
  1302. return node === block;
  1303. };
  1304. if (isReplacementPattern(match.pattern)) {
  1305. applyReplacementPattern(editor, match.pattern, match.endMarker, isRoot);
  1306. } else {
  1307. applyPatternWithContent(editor, match.pattern, match.startMarker, match.endMarker, isRoot);
  1308. }
  1309. removeMarker(dom, match.endMarker, isRoot);
  1310. removeMarker(dom, match.startMarker, isRoot);
  1311. });
  1312. editor.selection.moveToBookmark(bookmark);
  1313. };
  1314. var handleEnter = function (editor, patternSet) {
  1315. if (!editor.selection.isCollapsed()) {
  1316. return false;
  1317. }
  1318. var inlineMatches = findPatterns$1(editor, patternSet.inlinePatterns, false);
  1319. var blockMatches = findPatterns(editor, patternSet.blockPatterns);
  1320. if (blockMatches.length > 0 || inlineMatches.length > 0) {
  1321. editor.undoManager.add();
  1322. editor.undoManager.extra(function () {
  1323. editor.execCommand('mceInsertNewLine');
  1324. }, function () {
  1325. editor.insertContent(zeroWidth());
  1326. applyMatches$1(editor, inlineMatches);
  1327. applyMatches(editor, blockMatches);
  1328. var range = editor.selection.getRng();
  1329. var spot = textBefore(range.startContainer, range.startOffset, editor.dom.getRoot());
  1330. editor.execCommand('mceInsertNewLine');
  1331. spot.each(function (s) {
  1332. if (s.element.data.charAt(s.offset - 1) === zeroWidth()) {
  1333. s.element.deleteData(s.offset - 1, 1);
  1334. cleanEmptyNodes(editor.dom, s.element.parentNode, function (e) {
  1335. return e === editor.dom.getRoot();
  1336. });
  1337. }
  1338. });
  1339. });
  1340. return true;
  1341. }
  1342. return false;
  1343. };
  1344. var handleInlineKey = function (editor, patternSet) {
  1345. var inlineMatches = findPatterns$1(editor, patternSet.inlinePatterns, true);
  1346. if (inlineMatches.length > 0) {
  1347. editor.undoManager.transact(function () {
  1348. applyMatches$1(editor, inlineMatches);
  1349. });
  1350. }
  1351. };
  1352. var checkKeyEvent = function (codes, event, predicate) {
  1353. for (var i = 0; i < codes.length; i++) {
  1354. if (predicate(codes[i], event)) {
  1355. return true;
  1356. }
  1357. }
  1358. };
  1359. var checkKeyCode = function (codes, event) {
  1360. return checkKeyEvent(codes, event, function (code, event) {
  1361. return code === event.keyCode && global$2.modifierPressed(event) === false;
  1362. });
  1363. };
  1364. var checkCharCode = function (chars, event) {
  1365. return checkKeyEvent(chars, event, function (chr, event) {
  1366. return chr.charCodeAt(0) === event.charCode;
  1367. });
  1368. };
  1369. var KeyHandler = {
  1370. handleEnter: handleEnter,
  1371. handleInlineKey: handleInlineKey,
  1372. checkCharCode: checkCharCode,
  1373. checkKeyCode: checkKeyCode
  1374. };
  1375. var setup = function (editor, patternsState) {
  1376. var charCodes = [
  1377. ',',
  1378. '.',
  1379. ';',
  1380. ':',
  1381. '!',
  1382. '?'
  1383. ];
  1384. var keyCodes = [32];
  1385. editor.on('keydown', function (e) {
  1386. if (e.keyCode === 13 && !global$2.modifierPressed(e)) {
  1387. if (KeyHandler.handleEnter(editor, patternsState.get())) {
  1388. e.preventDefault();
  1389. }
  1390. }
  1391. }, true);
  1392. editor.on('keyup', function (e) {
  1393. if (KeyHandler.checkKeyCode(keyCodes, e)) {
  1394. KeyHandler.handleInlineKey(editor, patternsState.get());
  1395. }
  1396. });
  1397. editor.on('keypress', function (e) {
  1398. if (KeyHandler.checkCharCode(charCodes, e)) {
  1399. global$1.setEditorTimeout(editor, function () {
  1400. KeyHandler.handleInlineKey(editor, patternsState.get());
  1401. });
  1402. }
  1403. });
  1404. };
  1405. var Keyboard = { setup: setup };
  1406. function Plugin () {
  1407. global.add('textpattern', function (editor) {
  1408. var patternsState = Cell(getPatternSet(editor.settings));
  1409. Keyboard.setup(editor, patternsState);
  1410. return Api.get(patternsState);
  1411. });
  1412. }
  1413. Plugin();
  1414. }(window));