plugin.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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.10.2 (2021-11-17)
  8. */
  9. (function () {
  10. 'use strict';
  11. var global$7 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var global$6 = tinymce.util.Tools.resolve('tinymce.util.VK');
  13. var typeOf = function (x) {
  14. var t = typeof x;
  15. if (x === null) {
  16. return 'null';
  17. } else if (t === 'object' && (Array.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'Array')) {
  18. return 'array';
  19. } else if (t === 'object' && (String.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'String')) {
  20. return 'string';
  21. } else {
  22. return t;
  23. }
  24. };
  25. var isType = function (type) {
  26. return function (value) {
  27. return typeOf(value) === type;
  28. };
  29. };
  30. var isSimpleType = function (type) {
  31. return function (value) {
  32. return typeof value === type;
  33. };
  34. };
  35. var eq = function (t) {
  36. return function (a) {
  37. return t === a;
  38. };
  39. };
  40. var isString = isType('string');
  41. var isArray = isType('array');
  42. var isNull = eq(null);
  43. var isBoolean = isSimpleType('boolean');
  44. var isFunction = isSimpleType('function');
  45. var noop = function () {
  46. };
  47. var constant = function (value) {
  48. return function () {
  49. return value;
  50. };
  51. };
  52. var identity = function (x) {
  53. return x;
  54. };
  55. var tripleEquals = function (a, b) {
  56. return a === b;
  57. };
  58. var never = constant(false);
  59. var always = constant(true);
  60. var none = function () {
  61. return NONE;
  62. };
  63. var NONE = function () {
  64. var call = function (thunk) {
  65. return thunk();
  66. };
  67. var id = identity;
  68. var me = {
  69. fold: function (n, _s) {
  70. return n();
  71. },
  72. isSome: never,
  73. isNone: always,
  74. getOr: id,
  75. getOrThunk: call,
  76. getOrDie: function (msg) {
  77. throw new Error(msg || 'error: getOrDie called on none.');
  78. },
  79. getOrNull: constant(null),
  80. getOrUndefined: constant(undefined),
  81. or: id,
  82. orThunk: call,
  83. map: none,
  84. each: noop,
  85. bind: none,
  86. exists: never,
  87. forall: always,
  88. filter: function () {
  89. return none();
  90. },
  91. toArray: function () {
  92. return [];
  93. },
  94. toString: constant('none()')
  95. };
  96. return me;
  97. }();
  98. var some = function (a) {
  99. var constant_a = constant(a);
  100. var self = function () {
  101. return me;
  102. };
  103. var bind = function (f) {
  104. return f(a);
  105. };
  106. var me = {
  107. fold: function (n, s) {
  108. return s(a);
  109. },
  110. isSome: always,
  111. isNone: never,
  112. getOr: constant_a,
  113. getOrThunk: constant_a,
  114. getOrDie: constant_a,
  115. getOrNull: constant_a,
  116. getOrUndefined: constant_a,
  117. or: self,
  118. orThunk: self,
  119. map: function (f) {
  120. return some(f(a));
  121. },
  122. each: function (f) {
  123. f(a);
  124. },
  125. bind: bind,
  126. exists: bind,
  127. forall: bind,
  128. filter: function (f) {
  129. return f(a) ? me : NONE;
  130. },
  131. toArray: function () {
  132. return [a];
  133. },
  134. toString: function () {
  135. return 'some(' + a + ')';
  136. }
  137. };
  138. return me;
  139. };
  140. var from = function (value) {
  141. return value === null || value === undefined ? NONE : some(value);
  142. };
  143. var Optional = {
  144. some: some,
  145. none: none,
  146. from: from
  147. };
  148. var nativeIndexOf = Array.prototype.indexOf;
  149. var nativePush = Array.prototype.push;
  150. var rawIndexOf = function (ts, t) {
  151. return nativeIndexOf.call(ts, t);
  152. };
  153. var contains = function (xs, x) {
  154. return rawIndexOf(xs, x) > -1;
  155. };
  156. var map = function (xs, f) {
  157. var len = xs.length;
  158. var r = new Array(len);
  159. for (var i = 0; i < len; i++) {
  160. var x = xs[i];
  161. r[i] = f(x, i);
  162. }
  163. return r;
  164. };
  165. var each$1 = function (xs, f) {
  166. for (var i = 0, len = xs.length; i < len; i++) {
  167. var x = xs[i];
  168. f(x, i);
  169. }
  170. };
  171. var foldl = function (xs, f, acc) {
  172. each$1(xs, function (x, i) {
  173. acc = f(acc, x, i);
  174. });
  175. return acc;
  176. };
  177. var flatten = function (xs) {
  178. var r = [];
  179. for (var i = 0, len = xs.length; i < len; ++i) {
  180. if (!isArray(xs[i])) {
  181. throw new Error('Arr.flatten item ' + i + ' was not an array, input: ' + xs);
  182. }
  183. nativePush.apply(r, xs[i]);
  184. }
  185. return r;
  186. };
  187. var bind = function (xs, f) {
  188. return flatten(map(xs, f));
  189. };
  190. var findMap = function (arr, f) {
  191. for (var i = 0; i < arr.length; i++) {
  192. var r = f(arr[i], i);
  193. if (r.isSome()) {
  194. return r;
  195. }
  196. }
  197. return Optional.none();
  198. };
  199. var is = function (lhs, rhs, comparator) {
  200. if (comparator === void 0) {
  201. comparator = tripleEquals;
  202. }
  203. return lhs.exists(function (left) {
  204. return comparator(left, rhs);
  205. });
  206. };
  207. var cat = function (arr) {
  208. var r = [];
  209. var push = function (x) {
  210. r.push(x);
  211. };
  212. for (var i = 0; i < arr.length; i++) {
  213. arr[i].each(push);
  214. }
  215. return r;
  216. };
  217. var someIf = function (b, a) {
  218. return b ? Optional.some(a) : Optional.none();
  219. };
  220. var assumeExternalTargets = function (editor) {
  221. var externalTargets = editor.getParam('link_assume_external_targets', false);
  222. if (isBoolean(externalTargets) && externalTargets) {
  223. return 1;
  224. } else if (isString(externalTargets) && (externalTargets === 'http' || externalTargets === 'https')) {
  225. return externalTargets;
  226. }
  227. return 0;
  228. };
  229. var hasContextToolbar = function (editor) {
  230. return editor.getParam('link_context_toolbar', false, 'boolean');
  231. };
  232. var getLinkList = function (editor) {
  233. return editor.getParam('link_list');
  234. };
  235. var getDefaultLinkTarget = function (editor) {
  236. return editor.getParam('default_link_target');
  237. };
  238. var getTargetList = function (editor) {
  239. return editor.getParam('target_list', true);
  240. };
  241. var getRelList = function (editor) {
  242. return editor.getParam('rel_list', [], 'array');
  243. };
  244. var getLinkClassList = function (editor) {
  245. return editor.getParam('link_class_list', [], 'array');
  246. };
  247. var shouldShowLinkTitle = function (editor) {
  248. return editor.getParam('link_title', true, 'boolean');
  249. };
  250. var allowUnsafeLinkTarget = function (editor) {
  251. return editor.getParam('allow_unsafe_link_target', false, 'boolean');
  252. };
  253. var useQuickLink = function (editor) {
  254. return editor.getParam('link_quicklink', false, 'boolean');
  255. };
  256. var getDefaultLinkProtocol = function (editor) {
  257. return editor.getParam('link_default_protocol', 'http', 'string');
  258. };
  259. var global$5 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  260. var getValue = function (item) {
  261. return isString(item.value) ? item.value : '';
  262. };
  263. var getText = function (item) {
  264. if (isString(item.text)) {
  265. return item.text;
  266. } else if (isString(item.title)) {
  267. return item.title;
  268. } else {
  269. return '';
  270. }
  271. };
  272. var sanitizeList = function (list, extractValue) {
  273. var out = [];
  274. global$5.each(list, function (item) {
  275. var text = getText(item);
  276. if (item.menu !== undefined) {
  277. var items = sanitizeList(item.menu, extractValue);
  278. out.push({
  279. text: text,
  280. items: items
  281. });
  282. } else {
  283. var value = extractValue(item);
  284. out.push({
  285. text: text,
  286. value: value
  287. });
  288. }
  289. });
  290. return out;
  291. };
  292. var sanitizeWith = function (extracter) {
  293. if (extracter === void 0) {
  294. extracter = getValue;
  295. }
  296. return function (list) {
  297. return Optional.from(list).map(function (list) {
  298. return sanitizeList(list, extracter);
  299. });
  300. };
  301. };
  302. var sanitize = function (list) {
  303. return sanitizeWith(getValue)(list);
  304. };
  305. var createUi = function (name, label) {
  306. return function (items) {
  307. return {
  308. name: name,
  309. type: 'listbox',
  310. label: label,
  311. items: items
  312. };
  313. };
  314. };
  315. var ListOptions = {
  316. sanitize: sanitize,
  317. sanitizeWith: sanitizeWith,
  318. createUi: createUi,
  319. getValue: getValue
  320. };
  321. var __assign = function () {
  322. __assign = Object.assign || function __assign(t) {
  323. for (var s, i = 1, n = arguments.length; i < n; i++) {
  324. s = arguments[i];
  325. for (var p in s)
  326. if (Object.prototype.hasOwnProperty.call(s, p))
  327. t[p] = s[p];
  328. }
  329. return t;
  330. };
  331. return __assign.apply(this, arguments);
  332. };
  333. var keys = Object.keys;
  334. var hasOwnProperty = Object.hasOwnProperty;
  335. var each = function (obj, f) {
  336. var props = keys(obj);
  337. for (var k = 0, len = props.length; k < len; k++) {
  338. var i = props[k];
  339. var x = obj[i];
  340. f(x, i);
  341. }
  342. };
  343. var objAcc = function (r) {
  344. return function (x, i) {
  345. r[i] = x;
  346. };
  347. };
  348. var internalFilter = function (obj, pred, onTrue, onFalse) {
  349. var r = {};
  350. each(obj, function (x, i) {
  351. (pred(x, i) ? onTrue : onFalse)(x, i);
  352. });
  353. return r;
  354. };
  355. var filter = function (obj, pred) {
  356. var t = {};
  357. internalFilter(obj, pred, objAcc(t), noop);
  358. return t;
  359. };
  360. var has = function (obj, key) {
  361. return hasOwnProperty.call(obj, key);
  362. };
  363. var hasNonNullableKey = function (obj, key) {
  364. return has(obj, key) && obj[key] !== undefined && obj[key] !== null;
  365. };
  366. var global$4 = tinymce.util.Tools.resolve('tinymce.dom.TreeWalker');
  367. var global$3 = tinymce.util.Tools.resolve('tinymce.util.URI');
  368. var isAnchor = function (elm) {
  369. return elm && elm.nodeName.toLowerCase() === 'a';
  370. };
  371. var isLink = function (elm) {
  372. return isAnchor(elm) && !!getHref(elm);
  373. };
  374. var collectNodesInRange = function (rng, predicate) {
  375. if (rng.collapsed) {
  376. return [];
  377. } else {
  378. var contents = rng.cloneContents();
  379. var walker = new global$4(contents.firstChild, contents);
  380. var elements = [];
  381. var current = contents.firstChild;
  382. do {
  383. if (predicate(current)) {
  384. elements.push(current);
  385. }
  386. } while (current = walker.next());
  387. return elements;
  388. }
  389. };
  390. var hasProtocol = function (url) {
  391. return /^\w+:/i.test(url);
  392. };
  393. var getHref = function (elm) {
  394. var href = elm.getAttribute('data-mce-href');
  395. return href ? href : elm.getAttribute('href');
  396. };
  397. var applyRelTargetRules = function (rel, isUnsafe) {
  398. var rules = ['noopener'];
  399. var rels = rel ? rel.split(/\s+/) : [];
  400. var toString = function (rels) {
  401. return global$5.trim(rels.sort().join(' '));
  402. };
  403. var addTargetRules = function (rels) {
  404. rels = removeTargetRules(rels);
  405. return rels.length > 0 ? rels.concat(rules) : rules;
  406. };
  407. var removeTargetRules = function (rels) {
  408. return rels.filter(function (val) {
  409. return global$5.inArray(rules, val) === -1;
  410. });
  411. };
  412. var newRels = isUnsafe ? addTargetRules(rels) : removeTargetRules(rels);
  413. return newRels.length > 0 ? toString(newRels) : '';
  414. };
  415. var trimCaretContainers = function (text) {
  416. return text.replace(/\uFEFF/g, '');
  417. };
  418. var getAnchorElement = function (editor, selectedElm) {
  419. selectedElm = selectedElm || editor.selection.getNode();
  420. if (isImageFigure(selectedElm)) {
  421. return editor.dom.select('a[href]', selectedElm)[0];
  422. } else {
  423. return editor.dom.getParent(selectedElm, 'a[href]');
  424. }
  425. };
  426. var getAnchorText = function (selection, anchorElm) {
  427. var text = anchorElm ? anchorElm.innerText || anchorElm.textContent : selection.getContent({ format: 'text' });
  428. return trimCaretContainers(text);
  429. };
  430. var hasLinks = function (elements) {
  431. return global$5.grep(elements, isLink).length > 0;
  432. };
  433. var hasLinksInSelection = function (rng) {
  434. return collectNodesInRange(rng, isLink).length > 0;
  435. };
  436. var isOnlyTextSelected = function (editor) {
  437. var inlineTextElements = editor.schema.getTextInlineElements();
  438. var isElement = function (elm) {
  439. return elm.nodeType === 1 && !isAnchor(elm) && !has(inlineTextElements, elm.nodeName.toLowerCase());
  440. };
  441. var elements = collectNodesInRange(editor.selection.getRng(), isElement);
  442. return elements.length === 0;
  443. };
  444. var isImageFigure = function (elm) {
  445. return elm && elm.nodeName === 'FIGURE' && /\bimage\b/i.test(elm.className);
  446. };
  447. var getLinkAttrs = function (data) {
  448. var attrs = [
  449. 'title',
  450. 'rel',
  451. 'class',
  452. 'target'
  453. ];
  454. return foldl(attrs, function (acc, key) {
  455. data[key].each(function (value) {
  456. acc[key] = value.length > 0 ? value : null;
  457. });
  458. return acc;
  459. }, { href: data.href });
  460. };
  461. var handleExternalTargets = function (href, assumeExternalTargets) {
  462. if ((assumeExternalTargets === 'http' || assumeExternalTargets === 'https') && !hasProtocol(href)) {
  463. return assumeExternalTargets + '://' + href;
  464. }
  465. return href;
  466. };
  467. var applyLinkOverrides = function (editor, linkAttrs) {
  468. var newLinkAttrs = __assign({}, linkAttrs);
  469. if (!(getRelList(editor).length > 0) && allowUnsafeLinkTarget(editor) === false) {
  470. var newRel = applyRelTargetRules(newLinkAttrs.rel, newLinkAttrs.target === '_blank');
  471. newLinkAttrs.rel = newRel ? newRel : null;
  472. }
  473. if (Optional.from(newLinkAttrs.target).isNone() && getTargetList(editor) === false) {
  474. newLinkAttrs.target = getDefaultLinkTarget(editor);
  475. }
  476. newLinkAttrs.href = handleExternalTargets(newLinkAttrs.href, assumeExternalTargets(editor));
  477. return newLinkAttrs;
  478. };
  479. var updateLink = function (editor, anchorElm, text, linkAttrs) {
  480. text.each(function (text) {
  481. if (has(anchorElm, 'innerText')) {
  482. anchorElm.innerText = text;
  483. } else {
  484. anchorElm.textContent = text;
  485. }
  486. });
  487. editor.dom.setAttribs(anchorElm, linkAttrs);
  488. editor.selection.select(anchorElm);
  489. };
  490. var createLink = function (editor, selectedElm, text, linkAttrs) {
  491. if (isImageFigure(selectedElm)) {
  492. linkImageFigure(editor, selectedElm, linkAttrs);
  493. } else {
  494. text.fold(function () {
  495. editor.execCommand('mceInsertLink', false, linkAttrs);
  496. }, function (text) {
  497. editor.insertContent(editor.dom.createHTML('a', linkAttrs, editor.dom.encode(text)));
  498. });
  499. }
  500. };
  501. var linkDomMutation = function (editor, attachState, data) {
  502. var selectedElm = editor.selection.getNode();
  503. var anchorElm = getAnchorElement(editor, selectedElm);
  504. var linkAttrs = applyLinkOverrides(editor, getLinkAttrs(data));
  505. editor.undoManager.transact(function () {
  506. if (data.href === attachState.href) {
  507. attachState.attach();
  508. }
  509. if (anchorElm) {
  510. editor.focus();
  511. updateLink(editor, anchorElm, data.text, linkAttrs);
  512. } else {
  513. createLink(editor, selectedElm, data.text, linkAttrs);
  514. }
  515. });
  516. };
  517. var unlinkSelection = function (editor) {
  518. var dom = editor.dom, selection = editor.selection;
  519. var bookmark = selection.getBookmark();
  520. var rng = selection.getRng().cloneRange();
  521. var startAnchorElm = dom.getParent(rng.startContainer, 'a[href]', editor.getBody());
  522. var endAnchorElm = dom.getParent(rng.endContainer, 'a[href]', editor.getBody());
  523. if (startAnchorElm) {
  524. rng.setStartBefore(startAnchorElm);
  525. }
  526. if (endAnchorElm) {
  527. rng.setEndAfter(endAnchorElm);
  528. }
  529. selection.setRng(rng);
  530. editor.execCommand('unlink');
  531. selection.moveToBookmark(bookmark);
  532. };
  533. var unlinkDomMutation = function (editor) {
  534. editor.undoManager.transact(function () {
  535. var node = editor.selection.getNode();
  536. if (isImageFigure(node)) {
  537. unlinkImageFigure(editor, node);
  538. } else {
  539. unlinkSelection(editor);
  540. }
  541. editor.focus();
  542. });
  543. };
  544. var unwrapOptions = function (data) {
  545. var cls = data.class, href = data.href, rel = data.rel, target = data.target, text = data.text, title = data.title;
  546. return filter({
  547. class: cls.getOrNull(),
  548. href: href,
  549. rel: rel.getOrNull(),
  550. target: target.getOrNull(),
  551. text: text.getOrNull(),
  552. title: title.getOrNull()
  553. }, function (v, _k) {
  554. return isNull(v) === false;
  555. });
  556. };
  557. var sanitizeData = function (editor, data) {
  558. var href = data.href;
  559. return __assign(__assign({}, data), { href: global$3.isDomSafe(href, 'a', editor.settings) ? href : '' });
  560. };
  561. var link = function (editor, attachState, data) {
  562. var sanitizedData = sanitizeData(editor, data);
  563. editor.hasPlugin('rtc', true) ? editor.execCommand('createlink', false, unwrapOptions(sanitizedData)) : linkDomMutation(editor, attachState, sanitizedData);
  564. };
  565. var unlink = function (editor) {
  566. editor.hasPlugin('rtc', true) ? editor.execCommand('unlink') : unlinkDomMutation(editor);
  567. };
  568. var unlinkImageFigure = function (editor, fig) {
  569. var img = editor.dom.select('img', fig)[0];
  570. if (img) {
  571. var a = editor.dom.getParents(img, 'a[href]', fig)[0];
  572. if (a) {
  573. a.parentNode.insertBefore(img, a);
  574. editor.dom.remove(a);
  575. }
  576. }
  577. };
  578. var linkImageFigure = function (editor, fig, attrs) {
  579. var img = editor.dom.select('img', fig)[0];
  580. if (img) {
  581. var a = editor.dom.create('a', attrs);
  582. img.parentNode.insertBefore(a, img);
  583. a.appendChild(img);
  584. }
  585. };
  586. var isListGroup = function (item) {
  587. return hasNonNullableKey(item, 'items');
  588. };
  589. var findTextByValue = function (value, catalog) {
  590. return findMap(catalog, function (item) {
  591. if (isListGroup(item)) {
  592. return findTextByValue(value, item.items);
  593. } else {
  594. return someIf(item.value === value, item);
  595. }
  596. });
  597. };
  598. var getDelta = function (persistentText, fieldName, catalog, data) {
  599. var value = data[fieldName];
  600. var hasPersistentText = persistentText.length > 0;
  601. return value !== undefined ? findTextByValue(value, catalog).map(function (i) {
  602. return {
  603. url: {
  604. value: i.value,
  605. meta: {
  606. text: hasPersistentText ? persistentText : i.text,
  607. attach: noop
  608. }
  609. },
  610. text: hasPersistentText ? persistentText : i.text
  611. };
  612. }) : Optional.none();
  613. };
  614. var findCatalog = function (catalogs, fieldName) {
  615. if (fieldName === 'link') {
  616. return catalogs.link;
  617. } else if (fieldName === 'anchor') {
  618. return catalogs.anchor;
  619. } else {
  620. return Optional.none();
  621. }
  622. };
  623. var init = function (initialData, linkCatalog) {
  624. var persistentData = {
  625. text: initialData.text,
  626. title: initialData.title
  627. };
  628. var getTitleFromUrlChange = function (url) {
  629. return someIf(persistentData.title.length <= 0, Optional.from(url.meta.title).getOr(''));
  630. };
  631. var getTextFromUrlChange = function (url) {
  632. return someIf(persistentData.text.length <= 0, Optional.from(url.meta.text).getOr(url.value));
  633. };
  634. var onUrlChange = function (data) {
  635. var text = getTextFromUrlChange(data.url);
  636. var title = getTitleFromUrlChange(data.url);
  637. if (text.isSome() || title.isSome()) {
  638. return Optional.some(__assign(__assign({}, text.map(function (text) {
  639. return { text: text };
  640. }).getOr({})), title.map(function (title) {
  641. return { title: title };
  642. }).getOr({})));
  643. } else {
  644. return Optional.none();
  645. }
  646. };
  647. var onCatalogChange = function (data, change) {
  648. var catalog = findCatalog(linkCatalog, change.name).getOr([]);
  649. return getDelta(persistentData.text, change.name, catalog, data);
  650. };
  651. var onChange = function (getData, change) {
  652. var name = change.name;
  653. if (name === 'url') {
  654. return onUrlChange(getData());
  655. } else if (contains([
  656. 'anchor',
  657. 'link'
  658. ], name)) {
  659. return onCatalogChange(getData(), change);
  660. } else if (name === 'text' || name === 'title') {
  661. persistentData[name] = getData()[name];
  662. return Optional.none();
  663. } else {
  664. return Optional.none();
  665. }
  666. };
  667. return { onChange: onChange };
  668. };
  669. var DialogChanges = {
  670. init: init,
  671. getDelta: getDelta
  672. };
  673. var global$2 = tinymce.util.Tools.resolve('tinymce.util.Delay');
  674. var global$1 = tinymce.util.Tools.resolve('tinymce.util.Promise');
  675. var delayedConfirm = function (editor, message, callback) {
  676. var rng = editor.selection.getRng();
  677. global$2.setEditorTimeout(editor, function () {
  678. editor.windowManager.confirm(message, function (state) {
  679. editor.selection.setRng(rng);
  680. callback(state);
  681. });
  682. });
  683. };
  684. var tryEmailTransform = function (data) {
  685. var url = data.href;
  686. var suggestMailTo = url.indexOf('@') > 0 && url.indexOf('/') === -1 && url.indexOf('mailto:') === -1;
  687. return suggestMailTo ? Optional.some({
  688. message: 'The URL you entered seems to be an email address. Do you want to add the required mailto: prefix?',
  689. preprocess: function (oldData) {
  690. return __assign(__assign({}, oldData), { href: 'mailto:' + url });
  691. }
  692. }) : Optional.none();
  693. };
  694. var tryProtocolTransform = function (assumeExternalTargets, defaultLinkProtocol) {
  695. return function (data) {
  696. var url = data.href;
  697. var suggestProtocol = assumeExternalTargets === 1 && !hasProtocol(url) || assumeExternalTargets === 0 && /^\s*www(\.|\d\.)/i.test(url);
  698. return suggestProtocol ? Optional.some({
  699. message: 'The URL you entered seems to be an external link. Do you want to add the required ' + defaultLinkProtocol + ':// prefix?',
  700. preprocess: function (oldData) {
  701. return __assign(__assign({}, oldData), { href: defaultLinkProtocol + '://' + url });
  702. }
  703. }) : Optional.none();
  704. };
  705. };
  706. var preprocess = function (editor, data) {
  707. return findMap([
  708. tryEmailTransform,
  709. tryProtocolTransform(assumeExternalTargets(editor), getDefaultLinkProtocol(editor))
  710. ], function (f) {
  711. return f(data);
  712. }).fold(function () {
  713. return global$1.resolve(data);
  714. }, function (transform) {
  715. return new global$1(function (callback) {
  716. delayedConfirm(editor, transform.message, function (state) {
  717. callback(state ? transform.preprocess(data) : data);
  718. });
  719. });
  720. });
  721. };
  722. var DialogConfirms = { preprocess: preprocess };
  723. var getAnchors = function (editor) {
  724. var anchorNodes = editor.dom.select('a:not([href])');
  725. var anchors = bind(anchorNodes, function (anchor) {
  726. var id = anchor.name || anchor.id;
  727. return id ? [{
  728. text: id,
  729. value: '#' + id
  730. }] : [];
  731. });
  732. return anchors.length > 0 ? Optional.some([{
  733. text: 'None',
  734. value: ''
  735. }].concat(anchors)) : Optional.none();
  736. };
  737. var AnchorListOptions = { getAnchors: getAnchors };
  738. var getClasses = function (editor) {
  739. var list = getLinkClassList(editor);
  740. if (list.length > 0) {
  741. return ListOptions.sanitize(list);
  742. }
  743. return Optional.none();
  744. };
  745. var ClassListOptions = { getClasses: getClasses };
  746. var global = tinymce.util.Tools.resolve('tinymce.util.XHR');
  747. var parseJson = function (text) {
  748. try {
  749. return Optional.some(JSON.parse(text));
  750. } catch (err) {
  751. return Optional.none();
  752. }
  753. };
  754. var getLinks = function (editor) {
  755. var extractor = function (item) {
  756. return editor.convertURL(item.value || item.url, 'href');
  757. };
  758. var linkList = getLinkList(editor);
  759. return new global$1(function (callback) {
  760. if (isString(linkList)) {
  761. global.send({
  762. url: linkList,
  763. success: function (text) {
  764. return callback(parseJson(text));
  765. },
  766. error: function (_) {
  767. return callback(Optional.none());
  768. }
  769. });
  770. } else if (isFunction(linkList)) {
  771. linkList(function (output) {
  772. return callback(Optional.some(output));
  773. });
  774. } else {
  775. callback(Optional.from(linkList));
  776. }
  777. }).then(function (optItems) {
  778. return optItems.bind(ListOptions.sanitizeWith(extractor)).map(function (items) {
  779. if (items.length > 0) {
  780. var noneItem = [{
  781. text: 'None',
  782. value: ''
  783. }];
  784. return noneItem.concat(items);
  785. } else {
  786. return items;
  787. }
  788. });
  789. });
  790. };
  791. var LinkListOptions = { getLinks: getLinks };
  792. var getRels = function (editor, initialTarget) {
  793. var list = getRelList(editor);
  794. if (list.length > 0) {
  795. var isTargetBlank_1 = is(initialTarget, '_blank');
  796. var enforceSafe = allowUnsafeLinkTarget(editor) === false;
  797. var safeRelExtractor = function (item) {
  798. return applyRelTargetRules(ListOptions.getValue(item), isTargetBlank_1);
  799. };
  800. var sanitizer = enforceSafe ? ListOptions.sanitizeWith(safeRelExtractor) : ListOptions.sanitize;
  801. return sanitizer(list);
  802. }
  803. return Optional.none();
  804. };
  805. var RelOptions = { getRels: getRels };
  806. var fallbacks = [
  807. {
  808. text: 'Current window',
  809. value: ''
  810. },
  811. {
  812. text: 'New window',
  813. value: '_blank'
  814. }
  815. ];
  816. var getTargets = function (editor) {
  817. var list = getTargetList(editor);
  818. if (isArray(list)) {
  819. return ListOptions.sanitize(list).orThunk(function () {
  820. return Optional.some(fallbacks);
  821. });
  822. } else if (list === false) {
  823. return Optional.none();
  824. }
  825. return Optional.some(fallbacks);
  826. };
  827. var TargetOptions = { getTargets: getTargets };
  828. var nonEmptyAttr = function (dom, elem, name) {
  829. var val = dom.getAttrib(elem, name);
  830. return val !== null && val.length > 0 ? Optional.some(val) : Optional.none();
  831. };
  832. var extractFromAnchor = function (editor, anchor) {
  833. var dom = editor.dom;
  834. var onlyText = isOnlyTextSelected(editor);
  835. var text = onlyText ? Optional.some(getAnchorText(editor.selection, anchor)) : Optional.none();
  836. var url = anchor ? Optional.some(dom.getAttrib(anchor, 'href')) : Optional.none();
  837. var target = anchor ? Optional.from(dom.getAttrib(anchor, 'target')) : Optional.none();
  838. var rel = nonEmptyAttr(dom, anchor, 'rel');
  839. var linkClass = nonEmptyAttr(dom, anchor, 'class');
  840. var title = nonEmptyAttr(dom, anchor, 'title');
  841. return {
  842. url: url,
  843. text: text,
  844. title: title,
  845. target: target,
  846. rel: rel,
  847. linkClass: linkClass
  848. };
  849. };
  850. var collect = function (editor, linkNode) {
  851. return LinkListOptions.getLinks(editor).then(function (links) {
  852. var anchor = extractFromAnchor(editor, linkNode);
  853. return {
  854. anchor: anchor,
  855. catalogs: {
  856. targets: TargetOptions.getTargets(editor),
  857. rels: RelOptions.getRels(editor, anchor.target),
  858. classes: ClassListOptions.getClasses(editor),
  859. anchor: AnchorListOptions.getAnchors(editor),
  860. link: links
  861. },
  862. optNode: Optional.from(linkNode),
  863. flags: { titleEnabled: shouldShowLinkTitle(editor) }
  864. };
  865. });
  866. };
  867. var DialogInfo = { collect: collect };
  868. var handleSubmit = function (editor, info) {
  869. return function (api) {
  870. var data = api.getData();
  871. if (!data.url.value) {
  872. unlink(editor);
  873. api.close();
  874. return;
  875. }
  876. var getChangedValue = function (key) {
  877. return Optional.from(data[key]).filter(function (value) {
  878. return !is(info.anchor[key], value);
  879. });
  880. };
  881. var changedData = {
  882. href: data.url.value,
  883. text: getChangedValue('text'),
  884. target: getChangedValue('target'),
  885. rel: getChangedValue('rel'),
  886. class: getChangedValue('linkClass'),
  887. title: getChangedValue('title')
  888. };
  889. var attachState = {
  890. href: data.url.value,
  891. attach: data.url.meta !== undefined && data.url.meta.attach ? data.url.meta.attach : noop
  892. };
  893. DialogConfirms.preprocess(editor, changedData).then(function (pData) {
  894. link(editor, attachState, pData);
  895. });
  896. api.close();
  897. };
  898. };
  899. var collectData = function (editor) {
  900. var anchorNode = getAnchorElement(editor);
  901. return DialogInfo.collect(editor, anchorNode);
  902. };
  903. var getInitialData = function (info, defaultTarget) {
  904. var anchor = info.anchor;
  905. var url = anchor.url.getOr('');
  906. return {
  907. url: {
  908. value: url,
  909. meta: { original: { value: url } }
  910. },
  911. text: anchor.text.getOr(''),
  912. title: anchor.title.getOr(''),
  913. anchor: url,
  914. link: url,
  915. rel: anchor.rel.getOr(''),
  916. target: anchor.target.or(defaultTarget).getOr(''),
  917. linkClass: anchor.linkClass.getOr('')
  918. };
  919. };
  920. var makeDialog = function (settings, onSubmit, editor) {
  921. var urlInput = [{
  922. name: 'url',
  923. type: 'urlinput',
  924. filetype: 'file',
  925. label: 'URL'
  926. }];
  927. var displayText = settings.anchor.text.map(function () {
  928. return {
  929. name: 'text',
  930. type: 'input',
  931. label: 'Text to display'
  932. };
  933. }).toArray();
  934. var titleText = settings.flags.titleEnabled ? [{
  935. name: 'title',
  936. type: 'input',
  937. label: 'Title'
  938. }] : [];
  939. var defaultTarget = Optional.from(getDefaultLinkTarget(editor));
  940. var initialData = getInitialData(settings, defaultTarget);
  941. var catalogs = settings.catalogs;
  942. var dialogDelta = DialogChanges.init(initialData, catalogs);
  943. var body = {
  944. type: 'panel',
  945. items: flatten([
  946. urlInput,
  947. displayText,
  948. titleText,
  949. cat([
  950. catalogs.anchor.map(ListOptions.createUi('anchor', 'Anchors')),
  951. catalogs.rels.map(ListOptions.createUi('rel', 'Rel')),
  952. catalogs.targets.map(ListOptions.createUi('target', 'Open link in...')),
  953. catalogs.link.map(ListOptions.createUi('link', 'Link list')),
  954. catalogs.classes.map(ListOptions.createUi('linkClass', 'Class'))
  955. ])
  956. ])
  957. };
  958. return {
  959. title: 'Insert/Edit Link',
  960. size: 'normal',
  961. body: body,
  962. buttons: [
  963. {
  964. type: 'cancel',
  965. name: 'cancel',
  966. text: 'Cancel'
  967. },
  968. {
  969. type: 'submit',
  970. name: 'save',
  971. text: 'Save',
  972. primary: true
  973. }
  974. ],
  975. initialData: initialData,
  976. onChange: function (api, _a) {
  977. var name = _a.name;
  978. dialogDelta.onChange(api.getData, { name: name }).each(function (newData) {
  979. api.setData(newData);
  980. });
  981. },
  982. onSubmit: onSubmit
  983. };
  984. };
  985. var open$1 = function (editor) {
  986. var data = collectData(editor);
  987. data.then(function (info) {
  988. var onSubmit = handleSubmit(editor, info);
  989. return makeDialog(info, onSubmit, editor);
  990. }).then(function (spec) {
  991. editor.windowManager.open(spec);
  992. });
  993. };
  994. var appendClickRemove = function (link, evt) {
  995. document.body.appendChild(link);
  996. link.dispatchEvent(evt);
  997. document.body.removeChild(link);
  998. };
  999. var open = function (url) {
  1000. var link = document.createElement('a');
  1001. link.target = '_blank';
  1002. link.href = url;
  1003. link.rel = 'noreferrer noopener';
  1004. var evt = document.createEvent('MouseEvents');
  1005. evt.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  1006. appendClickRemove(link, evt);
  1007. };
  1008. var getLink = function (editor, elm) {
  1009. return editor.dom.getParent(elm, 'a[href]');
  1010. };
  1011. var getSelectedLink = function (editor) {
  1012. return getLink(editor, editor.selection.getStart());
  1013. };
  1014. var hasOnlyAltModifier = function (e) {
  1015. return e.altKey === true && e.shiftKey === false && e.ctrlKey === false && e.metaKey === false;
  1016. };
  1017. var gotoLink = function (editor, a) {
  1018. if (a) {
  1019. var href = getHref(a);
  1020. if (/^#/.test(href)) {
  1021. var targetEl = editor.$(href);
  1022. if (targetEl.length) {
  1023. editor.selection.scrollIntoView(targetEl[0], true);
  1024. }
  1025. } else {
  1026. open(a.href);
  1027. }
  1028. }
  1029. };
  1030. var openDialog = function (editor) {
  1031. return function () {
  1032. open$1(editor);
  1033. };
  1034. };
  1035. var gotoSelectedLink = function (editor) {
  1036. return function () {
  1037. gotoLink(editor, getSelectedLink(editor));
  1038. };
  1039. };
  1040. var setupGotoLinks = function (editor) {
  1041. editor.on('click', function (e) {
  1042. var link = getLink(editor, e.target);
  1043. if (link && global$6.metaKeyPressed(e)) {
  1044. e.preventDefault();
  1045. gotoLink(editor, link);
  1046. }
  1047. });
  1048. editor.on('keydown', function (e) {
  1049. var link = getSelectedLink(editor);
  1050. if (link && e.keyCode === 13 && hasOnlyAltModifier(e)) {
  1051. e.preventDefault();
  1052. gotoLink(editor, link);
  1053. }
  1054. });
  1055. };
  1056. var toggleState = function (editor, toggler) {
  1057. editor.on('NodeChange', toggler);
  1058. return function () {
  1059. return editor.off('NodeChange', toggler);
  1060. };
  1061. };
  1062. var toggleActiveState = function (editor) {
  1063. return function (api) {
  1064. var updateState = function () {
  1065. return api.setActive(!editor.mode.isReadOnly() && getAnchorElement(editor, editor.selection.getNode()) !== null);
  1066. };
  1067. updateState();
  1068. return toggleState(editor, updateState);
  1069. };
  1070. };
  1071. var toggleEnabledState = function (editor) {
  1072. return function (api) {
  1073. var updateState = function () {
  1074. return api.setDisabled(getAnchorElement(editor, editor.selection.getNode()) === null);
  1075. };
  1076. updateState();
  1077. return toggleState(editor, updateState);
  1078. };
  1079. };
  1080. var toggleUnlinkState = function (editor) {
  1081. return function (api) {
  1082. var hasLinks$1 = function (parents) {
  1083. return hasLinks(parents) || hasLinksInSelection(editor.selection.getRng());
  1084. };
  1085. var parents = editor.dom.getParents(editor.selection.getStart());
  1086. api.setDisabled(!hasLinks$1(parents));
  1087. return toggleState(editor, function (e) {
  1088. return api.setDisabled(!hasLinks$1(e.parents));
  1089. });
  1090. };
  1091. };
  1092. var register = function (editor) {
  1093. editor.addCommand('mceLink', function () {
  1094. if (useQuickLink(editor)) {
  1095. editor.fire('contexttoolbar-show', { toolbarKey: 'quicklink' });
  1096. } else {
  1097. openDialog(editor)();
  1098. }
  1099. });
  1100. };
  1101. var setup = function (editor) {
  1102. editor.addShortcut('Meta+K', '', function () {
  1103. editor.execCommand('mceLink');
  1104. });
  1105. };
  1106. var setupButtons = function (editor) {
  1107. editor.ui.registry.addToggleButton('link', {
  1108. icon: 'link',
  1109. tooltip: 'Insert/edit link',
  1110. onAction: openDialog(editor),
  1111. onSetup: toggleActiveState(editor)
  1112. });
  1113. editor.ui.registry.addButton('openlink', {
  1114. icon: 'new-tab',
  1115. tooltip: 'Open link',
  1116. onAction: gotoSelectedLink(editor),
  1117. onSetup: toggleEnabledState(editor)
  1118. });
  1119. editor.ui.registry.addButton('unlink', {
  1120. icon: 'unlink',
  1121. tooltip: 'Remove link',
  1122. onAction: function () {
  1123. return unlink(editor);
  1124. },
  1125. onSetup: toggleUnlinkState(editor)
  1126. });
  1127. };
  1128. var setupMenuItems = function (editor) {
  1129. editor.ui.registry.addMenuItem('openlink', {
  1130. text: 'Open link',
  1131. icon: 'new-tab',
  1132. onAction: gotoSelectedLink(editor),
  1133. onSetup: toggleEnabledState(editor)
  1134. });
  1135. editor.ui.registry.addMenuItem('link', {
  1136. icon: 'link',
  1137. text: 'Link...',
  1138. shortcut: 'Meta+K',
  1139. onAction: openDialog(editor)
  1140. });
  1141. editor.ui.registry.addMenuItem('unlink', {
  1142. icon: 'unlink',
  1143. text: 'Remove link',
  1144. onAction: function () {
  1145. return unlink(editor);
  1146. },
  1147. onSetup: toggleUnlinkState(editor)
  1148. });
  1149. };
  1150. var setupContextMenu = function (editor) {
  1151. var inLink = 'link unlink openlink';
  1152. var noLink = 'link';
  1153. editor.ui.registry.addContextMenu('link', {
  1154. update: function (element) {
  1155. return hasLinks(editor.dom.getParents(element, 'a')) ? inLink : noLink;
  1156. }
  1157. });
  1158. };
  1159. var setupContextToolbars = function (editor) {
  1160. var collapseSelectionToEnd = function (editor) {
  1161. editor.selection.collapse(false);
  1162. };
  1163. var onSetupLink = function (buttonApi) {
  1164. var node = editor.selection.getNode();
  1165. buttonApi.setDisabled(!getAnchorElement(editor, node));
  1166. return noop;
  1167. };
  1168. var getLinkText = function (value) {
  1169. var anchor = getAnchorElement(editor);
  1170. var onlyText = isOnlyTextSelected(editor);
  1171. if (!anchor && onlyText) {
  1172. var text = getAnchorText(editor.selection, anchor);
  1173. return Optional.some(text.length > 0 ? text : value);
  1174. } else {
  1175. return Optional.none();
  1176. }
  1177. };
  1178. editor.ui.registry.addContextForm('quicklink', {
  1179. launch: {
  1180. type: 'contextformtogglebutton',
  1181. icon: 'link',
  1182. tooltip: 'Link',
  1183. onSetup: toggleActiveState(editor)
  1184. },
  1185. label: 'Link',
  1186. predicate: function (node) {
  1187. return !!getAnchorElement(editor, node) && hasContextToolbar(editor);
  1188. },
  1189. initValue: function () {
  1190. var elm = getAnchorElement(editor);
  1191. return !!elm ? getHref(elm) : '';
  1192. },
  1193. commands: [
  1194. {
  1195. type: 'contextformtogglebutton',
  1196. icon: 'link',
  1197. tooltip: 'Link',
  1198. primary: true,
  1199. onSetup: function (buttonApi) {
  1200. var node = editor.selection.getNode();
  1201. buttonApi.setActive(!!getAnchorElement(editor, node));
  1202. return toggleActiveState(editor)(buttonApi);
  1203. },
  1204. onAction: function (formApi) {
  1205. var value = formApi.getValue();
  1206. var text = getLinkText(value);
  1207. var attachState = {
  1208. href: value,
  1209. attach: noop
  1210. };
  1211. link(editor, attachState, {
  1212. href: value,
  1213. text: text,
  1214. title: Optional.none(),
  1215. rel: Optional.none(),
  1216. target: Optional.none(),
  1217. class: Optional.none()
  1218. });
  1219. collapseSelectionToEnd(editor);
  1220. formApi.hide();
  1221. }
  1222. },
  1223. {
  1224. type: 'contextformbutton',
  1225. icon: 'unlink',
  1226. tooltip: 'Remove link',
  1227. onSetup: onSetupLink,
  1228. onAction: function (formApi) {
  1229. unlink(editor);
  1230. formApi.hide();
  1231. }
  1232. },
  1233. {
  1234. type: 'contextformbutton',
  1235. icon: 'new-tab',
  1236. tooltip: 'Open link',
  1237. onSetup: onSetupLink,
  1238. onAction: function (formApi) {
  1239. gotoSelectedLink(editor)();
  1240. formApi.hide();
  1241. }
  1242. }
  1243. ]
  1244. });
  1245. };
  1246. function Plugin () {
  1247. global$7.add('link', function (editor) {
  1248. setupButtons(editor);
  1249. setupMenuItems(editor);
  1250. setupContextMenu(editor);
  1251. setupContextToolbars(editor);
  1252. setupGotoLinks(editor);
  1253. register(editor);
  1254. setup(editor);
  1255. });
  1256. }
  1257. Plugin();
  1258. }());