plugin.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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.0.1 (2019-02-21)
  8. */
  9. (function () {
  10. var fullpage = (function () {
  11. 'use strict';
  12. var Cell = function (initial) {
  13. var value = initial;
  14. var get = function () {
  15. return value;
  16. };
  17. var set = function (v) {
  18. value = v;
  19. };
  20. var clone = function () {
  21. return Cell(get());
  22. };
  23. return {
  24. get: get,
  25. set: set,
  26. clone: clone
  27. };
  28. };
  29. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  30. var global$1 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  31. var global$2 = tinymce.util.Tools.resolve('tinymce.html.DomParser');
  32. var global$3 = tinymce.util.Tools.resolve('tinymce.html.Node');
  33. var global$4 = tinymce.util.Tools.resolve('tinymce.html.Serializer');
  34. var shouldHideInSourceView = function (editor) {
  35. return editor.getParam('fullpage_hide_in_source_view');
  36. };
  37. var getDefaultXmlPi = function (editor) {
  38. return editor.getParam('fullpage_default_xml_pi');
  39. };
  40. var getDefaultEncoding = function (editor) {
  41. return editor.getParam('fullpage_default_encoding');
  42. };
  43. var getDefaultFontFamily = function (editor) {
  44. return editor.getParam('fullpage_default_font_family');
  45. };
  46. var getDefaultFontSize = function (editor) {
  47. return editor.getParam('fullpage_default_font_size');
  48. };
  49. var getDefaultTextColor = function (editor) {
  50. return editor.getParam('fullpage_default_text_color');
  51. };
  52. var getDefaultTitle = function (editor) {
  53. return editor.getParam('fullpage_default_title');
  54. };
  55. var getDefaultDocType = function (editor) {
  56. return editor.getParam('fullpage_default_doctype', '<!DOCTYPE html>');
  57. };
  58. var Settings = {
  59. shouldHideInSourceView: shouldHideInSourceView,
  60. getDefaultXmlPi: getDefaultXmlPi,
  61. getDefaultEncoding: getDefaultEncoding,
  62. getDefaultFontFamily: getDefaultFontFamily,
  63. getDefaultFontSize: getDefaultFontSize,
  64. getDefaultTextColor: getDefaultTextColor,
  65. getDefaultTitle: getDefaultTitle,
  66. getDefaultDocType: getDefaultDocType
  67. };
  68. var parseHeader = function (head) {
  69. return global$2({
  70. validate: false,
  71. root_name: '#document'
  72. }).parse(head);
  73. };
  74. var htmlToData = function (editor, head) {
  75. var headerFragment = parseHeader(head);
  76. var data = {};
  77. var elm, matches;
  78. function getAttr(elm, name) {
  79. var value = elm.attr(name);
  80. return value || '';
  81. }
  82. data.fontface = Settings.getDefaultFontFamily(editor);
  83. data.fontsize = Settings.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$1.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$1.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 headerFragment, headElement, html, elm, value;
  135. var dom = editor.dom;
  136. function setAttr(elm, name, value) {
  137. elm.attr(name, value ? value : undefined);
  138. }
  139. function addHeadNode(node) {
  140. if (headElement.firstChild) {
  141. headElement.insert(node, headElement.firstChild);
  142. } else {
  143. headElement.append(node);
  144. }
  145. }
  146. headerFragment = parseHeader(head);
  147. headElement = headerFragment.getAll('head')[0];
  148. if (!headElement) {
  149. elm = headerFragment.getAll('html')[0];
  150. headElement = new global$3('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. value = 'version="1.0"';
  160. if (data.docencoding) {
  161. value += ' encoding="' + data.docencoding + '"';
  162. }
  163. if (elm.type !== 7) {
  164. elm = new global$3('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$3('#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$1.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$3('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$3('title', 1);
  206. addHeadNode(elm);
  207. } else {
  208. elm.empty();
  209. }
  210. elm.append(new global$3('#text', 3)).value = data.title;
  211. } else if (elm) {
  212. elm.remove();
  213. }
  214. global$1.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$3('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$1.each(headerFragment.getAll('link'), function (stylesheet) {
  239. if (stylesheet.attr('rel') === 'stylesheet') {
  240. currentStyleSheetsMap[stylesheet.attr('href')] = stylesheet;
  241. }
  242. });
  243. global$1.each(data.stylesheets, function (stylesheet) {
  244. if (!currentStyleSheetsMap[stylesheet]) {
  245. elm = new global$3('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$1.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. html = global$4({
  283. validate: false,
  284. indent: true,
  285. apply_source_formatting: true,
  286. indent_before: 'head,html,body,meta,title,script,link,style',
  287. indent_after: 'head,html,body,meta,title,script,link,style'
  288. }).serialize(headerFragment);
  289. return html.substring(0, html.indexOf('</body>'));
  290. };
  291. var Parser = {
  292. parseHeader: parseHeader,
  293. htmlToData: htmlToData,
  294. dataToHtml: dataToHtml
  295. };
  296. var hasOwnProperty = Object.prototype.hasOwnProperty;
  297. var shallow = function (old, nu) {
  298. return nu;
  299. };
  300. var baseMerge = function (merger) {
  301. return function () {
  302. var objects = new Array(arguments.length);
  303. for (var i = 0; i < objects.length; i++)
  304. objects[i] = arguments[i];
  305. if (objects.length === 0)
  306. throw new Error('Can\'t merge zero objects');
  307. var ret = {};
  308. for (var j = 0; j < objects.length; j++) {
  309. var curObject = objects[j];
  310. for (var key in curObject)
  311. if (hasOwnProperty.call(curObject, key)) {
  312. ret[key] = merger(ret[key], curObject[key]);
  313. }
  314. }
  315. return ret;
  316. };
  317. };
  318. var merge = baseMerge(shallow);
  319. var open = function (editor, headState) {
  320. var data = Parser.htmlToData(editor, headState.get());
  321. var defaultData = {
  322. title: '',
  323. keywords: '',
  324. description: '',
  325. robots: '',
  326. author: '',
  327. docencoding: ''
  328. };
  329. var initialData = merge(defaultData, data);
  330. editor.windowManager.open({
  331. title: 'Metadata and Document Properties',
  332. size: 'normal',
  333. body: {
  334. type: 'panel',
  335. items: [
  336. {
  337. name: 'title',
  338. type: 'input',
  339. label: 'Title'
  340. },
  341. {
  342. name: 'keywords',
  343. type: 'input',
  344. label: 'Keywords'
  345. },
  346. {
  347. name: 'description',
  348. type: 'input',
  349. label: 'Description'
  350. },
  351. {
  352. name: 'robots',
  353. type: 'input',
  354. label: 'Robots'
  355. },
  356. {
  357. name: 'author',
  358. type: 'input',
  359. label: 'Author'
  360. },
  361. {
  362. name: 'docencoding',
  363. type: 'input',
  364. label: 'Encoding'
  365. }
  366. ]
  367. },
  368. buttons: [
  369. {
  370. type: 'cancel',
  371. name: 'cancel',
  372. text: 'Cancel'
  373. },
  374. {
  375. type: 'submit',
  376. name: 'save',
  377. text: 'Save',
  378. primary: true
  379. }
  380. ],
  381. initialData: initialData,
  382. onSubmit: function (api) {
  383. var nuData = api.getData();
  384. var headHtml = Parser.dataToHtml(editor, global$1.extend(data, nuData), headState.get());
  385. headState.set(headHtml);
  386. api.close();
  387. }
  388. });
  389. };
  390. var Dialog = { open: open };
  391. var register = function (editor, headState) {
  392. editor.addCommand('mceFullPageProperties', function () {
  393. Dialog.open(editor, headState);
  394. });
  395. };
  396. var Commands = { register: register };
  397. var protectHtml = function (protect, html) {
  398. global$1.each(protect, function (pattern) {
  399. html = html.replace(pattern, function (str) {
  400. return '<!--mce:protected ' + escape(str) + '-->';
  401. });
  402. });
  403. return html;
  404. };
  405. var unprotectHtml = function (html) {
  406. return html.replace(/<!--mce:protected ([\s\S]*?)-->/g, function (a, m) {
  407. return unescape(m);
  408. });
  409. };
  410. var Protect = {
  411. protectHtml: protectHtml,
  412. unprotectHtml: unprotectHtml
  413. };
  414. var each = global$1.each;
  415. var low = function (s) {
  416. return s.replace(/<\/?[A-Z]+/g, function (a) {
  417. return a.toLowerCase();
  418. });
  419. };
  420. var handleSetContent = function (editor, headState, footState, evt) {
  421. var startPos, endPos, content, headerFragment, styles = '';
  422. var dom = editor.dom;
  423. var elm;
  424. if (evt.selection) {
  425. return;
  426. }
  427. content = Protect.protectHtml(editor.settings.protect, evt.content);
  428. if (evt.format === 'raw' && headState.get()) {
  429. return;
  430. }
  431. if (evt.source_view && Settings.shouldHideInSourceView(editor)) {
  432. return;
  433. }
  434. if (content.length === 0 && !evt.source_view) {
  435. content = global$1.trim(headState.get()) + '\n' + global$1.trim(content) + '\n' + global$1.trim(footState.get());
  436. }
  437. content = content.replace(/<(\/?)BODY/gi, '<$1body');
  438. startPos = content.indexOf('<body');
  439. if (startPos !== -1) {
  440. startPos = content.indexOf('>', startPos);
  441. headState.set(low(content.substring(0, startPos + 1)));
  442. endPos = content.indexOf('</body', startPos);
  443. if (endPos === -1) {
  444. endPos = content.length;
  445. }
  446. evt.content = global$1.trim(content.substring(startPos + 1, endPos));
  447. footState.set(low(content.substring(endPos)));
  448. } else {
  449. headState.set(getDefaultHeader(editor));
  450. footState.set('\n</body>\n</html>');
  451. }
  452. headerFragment = Parser.parseHeader(headState.get());
  453. each(headerFragment.getAll('style'), function (node) {
  454. if (node.firstChild) {
  455. styles += node.firstChild.value;
  456. }
  457. });
  458. elm = headerFragment.getAll('body')[0];
  459. if (elm) {
  460. dom.setAttribs(editor.getBody(), {
  461. style: elm.attr('style') || '',
  462. dir: elm.attr('dir') || '',
  463. vLink: elm.attr('vlink') || '',
  464. link: elm.attr('link') || '',
  465. aLink: elm.attr('alink') || ''
  466. });
  467. }
  468. dom.remove('fullpage_styles');
  469. var headElm = editor.getDoc().getElementsByTagName('head')[0];
  470. if (styles) {
  471. dom.add(headElm, 'style', { id: 'fullpage_styles' }, styles);
  472. elm = dom.get('fullpage_styles');
  473. if (elm.styleSheet) {
  474. elm.styleSheet.cssText = styles;
  475. }
  476. }
  477. var currentStyleSheetsMap = {};
  478. global$1.each(headElm.getElementsByTagName('link'), function (stylesheet) {
  479. if (stylesheet.rel === 'stylesheet' && stylesheet.getAttribute('data-mce-fullpage')) {
  480. currentStyleSheetsMap[stylesheet.href] = stylesheet;
  481. }
  482. });
  483. global$1.each(headerFragment.getAll('link'), function (stylesheet) {
  484. var href = stylesheet.attr('href');
  485. if (!href) {
  486. return true;
  487. }
  488. if (!currentStyleSheetsMap[href] && stylesheet.attr('rel') === 'stylesheet') {
  489. dom.add(headElm, 'link', {
  490. 'rel': 'stylesheet',
  491. 'text': 'text/css',
  492. 'href': href,
  493. 'data-mce-fullpage': '1'
  494. });
  495. }
  496. delete currentStyleSheetsMap[href];
  497. });
  498. global$1.each(currentStyleSheetsMap, function (stylesheet) {
  499. stylesheet.parentNode.removeChild(stylesheet);
  500. });
  501. };
  502. var getDefaultHeader = function (editor) {
  503. var header = '', value, styles = '';
  504. if (Settings.getDefaultXmlPi(editor)) {
  505. var piEncoding = Settings.getDefaultEncoding(editor);
  506. header += '<?xml version="1.0" encoding="' + (piEncoding ? piEncoding : 'ISO-8859-1') + '" ?>\n';
  507. }
  508. header += Settings.getDefaultDocType(editor);
  509. header += '\n<html>\n<head>\n';
  510. if (value = Settings.getDefaultTitle(editor)) {
  511. header += '<title>' + value + '</title>\n';
  512. }
  513. if (value = Settings.getDefaultEncoding(editor)) {
  514. header += '<meta http-equiv="Content-Type" content="text/html; charset=' + value + '" />\n';
  515. }
  516. if (value = Settings.getDefaultFontFamily(editor)) {
  517. styles += 'font-family: ' + value + ';';
  518. }
  519. if (value = Settings.getDefaultFontSize(editor)) {
  520. styles += 'font-size: ' + value + ';';
  521. }
  522. if (value = Settings.getDefaultTextColor(editor)) {
  523. styles += 'color: ' + value + ';';
  524. }
  525. header += '</head>\n<body' + (styles ? ' style="' + styles + '"' : '') + '>\n';
  526. return header;
  527. };
  528. var handleGetContent = function (editor, head, foot, evt) {
  529. if (!evt.selection && (!evt.source_view || !Settings.shouldHideInSourceView(editor))) {
  530. evt.content = Protect.unprotectHtml(global$1.trim(head) + '\n' + global$1.trim(evt.content) + '\n' + global$1.trim(foot));
  531. }
  532. };
  533. var setup = function (editor, headState, footState) {
  534. editor.on('BeforeSetContent', function (evt) {
  535. handleSetContent(editor, headState, footState, evt);
  536. });
  537. editor.on('GetContent', function (evt) {
  538. handleGetContent(editor, headState.get(), footState.get(), evt);
  539. });
  540. };
  541. var FilterContent = { setup: setup };
  542. var register$1 = function (editor) {
  543. editor.ui.registry.addButton('fullpage', {
  544. tooltip: 'Metadata and document properties',
  545. icon: 'document-properties',
  546. onAction: function () {
  547. editor.execCommand('mceFullPageProperties');
  548. }
  549. });
  550. editor.ui.registry.addMenuItem('fullpage', {
  551. text: 'Metadata and document properties',
  552. icon: 'document-properties',
  553. onAction: function () {
  554. editor.execCommand('mceFullPageProperties');
  555. }
  556. });
  557. };
  558. var Buttons = { register: register$1 };
  559. global.add('fullpage', function (editor) {
  560. var headState = Cell(''), footState = Cell('');
  561. Commands.register(editor, headState);
  562. Buttons.register(editor);
  563. FilterContent.setup(editor, headState, footState);
  564. });
  565. function Plugin () {
  566. }
  567. return Plugin;
  568. }());
  569. })();