plugin.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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.9.1 (2021-08-27)
  8. */
  9. (function () {
  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. return {
  20. get: get,
  21. set: set
  22. };
  23. };
  24. var global$4 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  25. var __assign = function () {
  26. __assign = Object.assign || function __assign(t) {
  27. for (var s, i = 1, n = arguments.length; i < n; i++) {
  28. s = arguments[i];
  29. for (var p in s)
  30. if (Object.prototype.hasOwnProperty.call(s, p))
  31. t[p] = s[p];
  32. }
  33. return t;
  34. };
  35. return __assign.apply(this, arguments);
  36. };
  37. var global$3 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  38. var global$2 = tinymce.util.Tools.resolve('tinymce.html.DomParser');
  39. var global$1 = tinymce.util.Tools.resolve('tinymce.html.Node');
  40. var global = tinymce.util.Tools.resolve('tinymce.html.Serializer');
  41. var shouldHideInSourceView = function (editor) {
  42. return editor.getParam('fullpage_hide_in_source_view');
  43. };
  44. var getDefaultXmlPi = function (editor) {
  45. return editor.getParam('fullpage_default_xml_pi');
  46. };
  47. var getDefaultEncoding = function (editor) {
  48. return editor.getParam('fullpage_default_encoding');
  49. };
  50. var getDefaultFontFamily = function (editor) {
  51. return editor.getParam('fullpage_default_font_family');
  52. };
  53. var getDefaultFontSize = function (editor) {
  54. return editor.getParam('fullpage_default_font_size');
  55. };
  56. var getDefaultTextColor = function (editor) {
  57. return editor.getParam('fullpage_default_text_color');
  58. };
  59. var getDefaultTitle = function (editor) {
  60. return editor.getParam('fullpage_default_title');
  61. };
  62. var getDefaultDocType = function (editor) {
  63. return editor.getParam('fullpage_default_doctype', '<!DOCTYPE html>');
  64. };
  65. var getProtect = function (editor) {
  66. return editor.getParam('protect');
  67. };
  68. var parseHeader = function (editor, head) {
  69. return global$2({
  70. validate: false,
  71. root_name: '#document'
  72. }, editor.schema).parse(head, { format: 'xhtml' });
  73. };
  74. var htmlToData = function (editor, head) {
  75. var headerFragment = parseHeader(editor, head);
  76. var data = {};
  77. var elm, matches;
  78. var getAttr = function (elm, name) {
  79. var value = elm.attr(name);
  80. return value || '';
  81. };
  82. data.fontface = getDefaultFontFamily(editor);
  83. data.fontsize = getDefaultFontSize(editor);
  84. elm = headerFragment.firstChild;
  85. if (elm.type === 7) {
  86. data.xml_pi = true;
  87. matches = /encoding="([^"]+)"/.exec(elm.value);
  88. if (matches) {
  89. data.docencoding = matches[1];
  90. }
  91. }
  92. elm = headerFragment.getAll('#doctype')[0];
  93. if (elm) {
  94. data.doctype = '<!DOCTYPE' + elm.value + '>';
  95. }
  96. elm = headerFragment.getAll('title')[0];
  97. if (elm && elm.firstChild) {
  98. data.title = elm.firstChild.value;
  99. }
  100. global$3.each(headerFragment.getAll('meta'), function (meta) {
  101. var name = meta.attr('name');
  102. var httpEquiv = meta.attr('http-equiv');
  103. var matches;
  104. if (name) {
  105. data[name.toLowerCase()] = meta.attr('content');
  106. } else if (httpEquiv === 'Content-Type') {
  107. matches = /charset\s*=\s*(.*)\s*/gi.exec(meta.attr('content'));
  108. if (matches) {
  109. data.docencoding = matches[1];
  110. }
  111. }
  112. });
  113. elm = headerFragment.getAll('html')[0];
  114. if (elm) {
  115. data.langcode = getAttr(elm, 'lang') || getAttr(elm, 'xml:lang');
  116. }
  117. data.stylesheets = [];
  118. global$3.each(headerFragment.getAll('link'), function (link) {
  119. if (link.attr('rel') === 'stylesheet') {
  120. data.stylesheets.push(link.attr('href'));
  121. }
  122. });
  123. elm = headerFragment.getAll('body')[0];
  124. if (elm) {
  125. data.langdir = getAttr(elm, 'dir');
  126. data.style = getAttr(elm, 'style');
  127. data.visited_color = getAttr(elm, 'vlink');
  128. data.link_color = getAttr(elm, 'link');
  129. data.active_color = getAttr(elm, 'alink');
  130. }
  131. return data;
  132. };
  133. var dataToHtml = function (editor, data, head) {
  134. var headElement, elm;
  135. var dom = editor.dom;
  136. var setAttr = function (elm, name, value) {
  137. elm.attr(name, value ? value : undefined);
  138. };
  139. var addHeadNode = function (node) {
  140. if (headElement.firstChild) {
  141. headElement.insert(node, headElement.firstChild);
  142. } else {
  143. headElement.append(node);
  144. }
  145. };
  146. var headerFragment = parseHeader(editor, head);
  147. headElement = headerFragment.getAll('head')[0];
  148. if (!headElement) {
  149. elm = headerFragment.getAll('html')[0];
  150. headElement = new global$1('head', 1);
  151. if (elm.firstChild) {
  152. elm.insert(headElement, elm.firstChild, true);
  153. } else {
  154. elm.append(headElement);
  155. }
  156. }
  157. elm = headerFragment.firstChild;
  158. if (data.xml_pi) {
  159. var value = 'version="1.0"';
  160. if (data.docencoding) {
  161. value += ' encoding="' + data.docencoding + '"';
  162. }
  163. if (elm.type !== 7) {
  164. elm = new global$1('xml', 7);
  165. headerFragment.insert(elm, headerFragment.firstChild, true);
  166. }
  167. elm.value = value;
  168. } else if (elm && elm.type === 7) {
  169. elm.remove();
  170. }
  171. elm = headerFragment.getAll('#doctype')[0];
  172. if (data.doctype) {
  173. if (!elm) {
  174. elm = new global$1('#doctype', 10);
  175. if (data.xml_pi) {
  176. headerFragment.insert(elm, headerFragment.firstChild);
  177. } else {
  178. addHeadNode(elm);
  179. }
  180. }
  181. elm.value = data.doctype.substring(9, data.doctype.length - 1);
  182. } else if (elm) {
  183. elm.remove();
  184. }
  185. elm = null;
  186. global$3.each(headerFragment.getAll('meta'), function (meta) {
  187. if (meta.attr('http-equiv') === 'Content-Type') {
  188. elm = meta;
  189. }
  190. });
  191. if (data.docencoding) {
  192. if (!elm) {
  193. elm = new global$1('meta', 1);
  194. elm.attr('http-equiv', 'Content-Type');
  195. elm.shortEnded = true;
  196. addHeadNode(elm);
  197. }
  198. elm.attr('content', 'text/html; charset=' + data.docencoding);
  199. } else if (elm) {
  200. elm.remove();
  201. }
  202. elm = headerFragment.getAll('title')[0];
  203. if (data.title) {
  204. if (!elm) {
  205. elm = new global$1('title', 1);
  206. addHeadNode(elm);
  207. } else {
  208. elm.empty();
  209. }
  210. elm.append(new global$1('#text', 3)).value = data.title;
  211. } else if (elm) {
  212. elm.remove();
  213. }
  214. global$3.each('keywords,description,author,copyright,robots'.split(','), function (name) {
  215. var nodes = headerFragment.getAll('meta');
  216. var i, meta;
  217. var value = data[name];
  218. for (i = 0; i < nodes.length; i++) {
  219. meta = nodes[i];
  220. if (meta.attr('name') === name) {
  221. if (value) {
  222. meta.attr('content', value);
  223. } else {
  224. meta.remove();
  225. }
  226. return;
  227. }
  228. }
  229. if (value) {
  230. elm = new global$1('meta', 1);
  231. elm.attr('name', name);
  232. elm.attr('content', value);
  233. elm.shortEnded = true;
  234. addHeadNode(elm);
  235. }
  236. });
  237. var currentStyleSheetsMap = {};
  238. global$3.each(headerFragment.getAll('link'), function (stylesheet) {
  239. if (stylesheet.attr('rel') === 'stylesheet') {
  240. currentStyleSheetsMap[stylesheet.attr('href')] = stylesheet;
  241. }
  242. });
  243. global$3.each(data.stylesheets, function (stylesheet) {
  244. if (!currentStyleSheetsMap[stylesheet]) {
  245. elm = new global$1('link', 1);
  246. elm.attr({
  247. rel: 'stylesheet',
  248. text: 'text/css',
  249. href: stylesheet
  250. });
  251. elm.shortEnded = true;
  252. addHeadNode(elm);
  253. }
  254. delete currentStyleSheetsMap[stylesheet];
  255. });
  256. global$3.each(currentStyleSheetsMap, function (stylesheet) {
  257. stylesheet.remove();
  258. });
  259. elm = headerFragment.getAll('body')[0];
  260. if (elm) {
  261. setAttr(elm, 'dir', data.langdir);
  262. setAttr(elm, 'style', data.style);
  263. setAttr(elm, 'vlink', data.visited_color);
  264. setAttr(elm, 'link', data.link_color);
  265. setAttr(elm, 'alink', data.active_color);
  266. dom.setAttribs(editor.getBody(), {
  267. style: data.style,
  268. dir: data.dir,
  269. vLink: data.visited_color,
  270. link: data.link_color,
  271. aLink: data.active_color
  272. });
  273. }
  274. elm = headerFragment.getAll('html')[0];
  275. if (elm) {
  276. setAttr(elm, 'lang', data.langcode);
  277. setAttr(elm, 'xml:lang', data.langcode);
  278. }
  279. if (!headElement.firstChild) {
  280. headElement.remove();
  281. }
  282. var html = global({
  283. validate: false,
  284. indent: true,
  285. indent_before: 'head,html,body,meta,title,script,link,style',
  286. indent_after: 'head,html,body,meta,title,script,link,style'
  287. }).serialize(headerFragment);
  288. return html.substring(0, html.indexOf('</body>'));
  289. };
  290. var open = function (editor, headState) {
  291. var data = htmlToData(editor, headState.get());
  292. var defaultData = {
  293. title: '',
  294. keywords: '',
  295. description: '',
  296. robots: '',
  297. author: '',
  298. docencoding: ''
  299. };
  300. var initialData = __assign(__assign({}, defaultData), data);
  301. editor.windowManager.open({
  302. title: 'Metadata and Document Properties',
  303. size: 'normal',
  304. body: {
  305. type: 'panel',
  306. items: [
  307. {
  308. name: 'title',
  309. type: 'input',
  310. label: 'Title'
  311. },
  312. {
  313. name: 'keywords',
  314. type: 'input',
  315. label: 'Keywords'
  316. },
  317. {
  318. name: 'description',
  319. type: 'input',
  320. label: 'Description'
  321. },
  322. {
  323. name: 'robots',
  324. type: 'input',
  325. label: 'Robots'
  326. },
  327. {
  328. name: 'author',
  329. type: 'input',
  330. label: 'Author'
  331. },
  332. {
  333. name: 'docencoding',
  334. type: 'input',
  335. label: 'Encoding'
  336. }
  337. ]
  338. },
  339. buttons: [
  340. {
  341. type: 'cancel',
  342. name: 'cancel',
  343. text: 'Cancel'
  344. },
  345. {
  346. type: 'submit',
  347. name: 'save',
  348. text: 'Save',
  349. primary: true
  350. }
  351. ],
  352. initialData: initialData,
  353. onSubmit: function (api) {
  354. var nuData = api.getData();
  355. var headHtml = dataToHtml(editor, global$3.extend(data, nuData), headState.get());
  356. headState.set(headHtml);
  357. api.close();
  358. }
  359. });
  360. };
  361. var register$1 = function (editor, headState) {
  362. editor.addCommand('mceFullPageProperties', function () {
  363. open(editor, headState);
  364. });
  365. };
  366. var protectHtml = function (protect, html) {
  367. global$3.each(protect, function (pattern) {
  368. html = html.replace(pattern, function (str) {
  369. return '<!--mce:protected ' + escape(str) + '-->';
  370. });
  371. });
  372. return html;
  373. };
  374. var unprotectHtml = function (html) {
  375. return html.replace(/<!--mce:protected ([\s\S]*?)-->/g, function (a, m) {
  376. return unescape(m);
  377. });
  378. };
  379. var each = global$3.each;
  380. var low = function (s) {
  381. return s.replace(/<\/?[A-Z]+/g, function (a) {
  382. return a.toLowerCase();
  383. });
  384. };
  385. var handleSetContent = function (editor, headState, footState, evt) {
  386. var startPos, endPos, content, styles = '';
  387. var dom = editor.dom;
  388. if (evt.selection) {
  389. return;
  390. }
  391. content = protectHtml(getProtect(editor), evt.content);
  392. if (evt.format === 'raw' && headState.get()) {
  393. return;
  394. }
  395. if (evt.source_view && shouldHideInSourceView(editor)) {
  396. return;
  397. }
  398. if (content.length === 0 && !evt.source_view) {
  399. content = global$3.trim(headState.get()) + '\n' + global$3.trim(content) + '\n' + global$3.trim(footState.get());
  400. }
  401. content = content.replace(/<(\/?)BODY/gi, '<$1body');
  402. startPos = content.indexOf('<body');
  403. if (startPos !== -1) {
  404. startPos = content.indexOf('>', startPos);
  405. headState.set(low(content.substring(0, startPos + 1)));
  406. endPos = content.indexOf('</body', startPos);
  407. if (endPos === -1) {
  408. endPos = content.length;
  409. }
  410. evt.content = global$3.trim(content.substring(startPos + 1, endPos));
  411. footState.set(low(content.substring(endPos)));
  412. } else {
  413. headState.set(getDefaultHeader(editor));
  414. footState.set('\n</body>\n</html>');
  415. }
  416. var headerFragment = parseHeader(editor, headState.get());
  417. each(headerFragment.getAll('style'), function (node) {
  418. if (node.firstChild) {
  419. styles += node.firstChild.value;
  420. }
  421. });
  422. var bodyElm = headerFragment.getAll('body')[0];
  423. if (bodyElm) {
  424. dom.setAttribs(editor.getBody(), {
  425. style: bodyElm.attr('style') || '',
  426. dir: bodyElm.attr('dir') || '',
  427. vLink: bodyElm.attr('vlink') || '',
  428. link: bodyElm.attr('link') || '',
  429. aLink: bodyElm.attr('alink') || ''
  430. });
  431. }
  432. dom.remove('fullpage_styles');
  433. var headElm = editor.getDoc().getElementsByTagName('head')[0];
  434. if (styles) {
  435. var styleElm = dom.add(headElm, 'style', { id: 'fullpage_styles' });
  436. styleElm.appendChild(document.createTextNode(styles));
  437. }
  438. var currentStyleSheetsMap = {};
  439. global$3.each(headElm.getElementsByTagName('link'), function (stylesheet) {
  440. if (stylesheet.rel === 'stylesheet' && stylesheet.getAttribute('data-mce-fullpage')) {
  441. currentStyleSheetsMap[stylesheet.href] = stylesheet;
  442. }
  443. });
  444. global$3.each(headerFragment.getAll('link'), function (stylesheet) {
  445. var href = stylesheet.attr('href');
  446. if (!href) {
  447. return true;
  448. }
  449. if (!currentStyleSheetsMap[href] && stylesheet.attr('rel') === 'stylesheet') {
  450. dom.add(headElm, 'link', {
  451. 'rel': 'stylesheet',
  452. 'text': 'text/css',
  453. href: href,
  454. 'data-mce-fullpage': '1'
  455. });
  456. }
  457. delete currentStyleSheetsMap[href];
  458. });
  459. global$3.each(currentStyleSheetsMap, function (stylesheet) {
  460. stylesheet.parentNode.removeChild(stylesheet);
  461. });
  462. };
  463. var getDefaultHeader = function (editor) {
  464. var header = '', value, styles = '';
  465. if (getDefaultXmlPi(editor)) {
  466. var piEncoding = getDefaultEncoding(editor);
  467. header += '<?xml version="1.0" encoding="' + (piEncoding ? piEncoding : 'ISO-8859-1') + '" ?>\n';
  468. }
  469. header += getDefaultDocType(editor);
  470. header += '\n<html>\n<head>\n';
  471. if (value = getDefaultTitle(editor)) {
  472. header += '<title>' + value + '</title>\n';
  473. }
  474. if (value = getDefaultEncoding(editor)) {
  475. header += '<meta http-equiv="Content-Type" content="text/html; charset=' + value + '" />\n';
  476. }
  477. if (value = getDefaultFontFamily(editor)) {
  478. styles += 'font-family: ' + value + ';';
  479. }
  480. if (value = getDefaultFontSize(editor)) {
  481. styles += 'font-size: ' + value + ';';
  482. }
  483. if (value = getDefaultTextColor(editor)) {
  484. styles += 'color: ' + value + ';';
  485. }
  486. header += '</head>\n<body' + (styles ? ' style="' + styles + '"' : '') + '>\n';
  487. return header;
  488. };
  489. var handleGetContent = function (editor, head, foot, evt) {
  490. if (evt.format === 'html' && !evt.selection && (!evt.source_view || !shouldHideInSourceView(editor))) {
  491. evt.content = unprotectHtml(global$3.trim(head) + '\n' + global$3.trim(evt.content) + '\n' + global$3.trim(foot));
  492. }
  493. };
  494. var setup = function (editor, headState, footState) {
  495. editor.on('BeforeSetContent', function (evt) {
  496. handleSetContent(editor, headState, footState, evt);
  497. });
  498. editor.on('GetContent', function (evt) {
  499. handleGetContent(editor, headState.get(), footState.get(), evt);
  500. });
  501. };
  502. var register = function (editor) {
  503. editor.ui.registry.addButton('fullpage', {
  504. tooltip: 'Metadata and document properties',
  505. icon: 'document-properties',
  506. onAction: function () {
  507. editor.execCommand('mceFullPageProperties');
  508. }
  509. });
  510. editor.ui.registry.addMenuItem('fullpage', {
  511. text: 'Metadata and document properties',
  512. icon: 'document-properties',
  513. onAction: function () {
  514. editor.execCommand('mceFullPageProperties');
  515. }
  516. });
  517. };
  518. function Plugin () {
  519. global$4.add('fullpage', function (editor) {
  520. console.warn('The fullpage plugin has been deprecated and marked for removal in TinyMCE 6.0');
  521. var headState = Cell(''), footState = Cell('');
  522. register$1(editor, headState);
  523. register(editor);
  524. setup(editor, headState, footState);
  525. });
  526. }
  527. Plugin();
  528. }());