You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

651 lines
16 KiB

  1. /**
  2. * plugin.js
  3. *
  4. * Released under LGPL License.
  5. * Copyright (c) 1999-2015 Ephox Corp. All rights reserved
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. /*global tinymce:true */
  11. tinymce.PluginManager.add('image', function(editor) {
  12. function getImageSize(url, callback) {
  13. var img = document.createElement('img');
  14. function done(width, height) {
  15. if (img.parentNode) {
  16. img.parentNode.removeChild(img);
  17. }
  18. callback({width: width, height: height});
  19. }
  20. img.onload = function() {
  21. done(Math.max(img.width, img.clientWidth), Math.max(img.height, img.clientHeight));
  22. };
  23. img.onerror = function() {
  24. done();
  25. };
  26. var style = img.style;
  27. style.visibility = 'hidden';
  28. style.position = 'fixed';
  29. style.bottom = style.left = 0;
  30. style.width = style.height = 'auto';
  31. document.body.appendChild(img);
  32. img.src = url;
  33. }
  34. function buildListItems(inputList, itemCallback, startItems) {
  35. function appendItems(values, output) {
  36. output = output || [];
  37. tinymce.each(values, function(item) {
  38. var menuItem = {text: item.text || item.title};
  39. if (item.menu) {
  40. menuItem.menu = appendItems(item.menu);
  41. } else {
  42. menuItem.value = item.value;
  43. itemCallback(menuItem);
  44. }
  45. output.push(menuItem);
  46. });
  47. return output;
  48. }
  49. return appendItems(inputList, startItems || []);
  50. }
  51. function createImageList(callback) {
  52. return function() {
  53. var imageList = editor.settings.image_list;
  54. if (typeof imageList == "string") {
  55. tinymce.util.XHR.send({
  56. url: imageList,
  57. success: function(text) {
  58. callback(tinymce.util.JSON.parse(text));
  59. }
  60. });
  61. } else if (typeof imageList == "function") {
  62. imageList(callback);
  63. } else {
  64. callback(imageList);
  65. }
  66. };
  67. }
  68. function showDialog(imageList) {
  69. var win, data = {}, dom = editor.dom, imgElm, figureElm;
  70. var width, height, imageListCtrl, classListCtrl, imageDimensions = editor.settings.image_dimensions !== false;
  71. function recalcSize() {
  72. var widthCtrl, heightCtrl, newWidth, newHeight;
  73. widthCtrl = win.find('#width')[0];
  74. heightCtrl = win.find('#height')[0];
  75. if (!widthCtrl || !heightCtrl) {
  76. return;
  77. }
  78. newWidth = widthCtrl.value();
  79. newHeight = heightCtrl.value();
  80. if (win.find('#constrain')[0].checked() && width && height && newWidth && newHeight) {
  81. if (width != newWidth) {
  82. newHeight = Math.round((newWidth / width) * newHeight);
  83. if (!isNaN(newHeight)) {
  84. heightCtrl.value(newHeight);
  85. }
  86. } else {
  87. newWidth = Math.round((newHeight / height) * newWidth);
  88. if (!isNaN(newWidth)) {
  89. widthCtrl.value(newWidth);
  90. }
  91. }
  92. }
  93. width = newWidth;
  94. height = newHeight;
  95. }
  96. function onSubmitForm() {
  97. var figureElm, oldImg;
  98. function waitLoad(imgElm) {
  99. function selectImage() {
  100. imgElm.onload = imgElm.onerror = null;
  101. if (editor.selection) {
  102. editor.selection.select(imgElm);
  103. editor.nodeChanged();
  104. }
  105. }
  106. imgElm.onload = function() {
  107. if (!data.width && !data.height && imageDimensions) {
  108. dom.setAttribs(imgElm, {
  109. width: imgElm.clientWidth,
  110. height: imgElm.clientHeight
  111. });
  112. //WP
  113. editor.fire( 'wpNewImageRefresh', { node: imgElm } );
  114. }
  115. selectImage();
  116. };
  117. imgElm.onerror = selectImage;
  118. }
  119. updateStyle();
  120. recalcSize();
  121. data = tinymce.extend(data, win.toJSON());
  122. var wpcaption = data.wpcaption; // WP
  123. if (!data.alt) {
  124. data.alt = '';
  125. }
  126. if (!data.title) {
  127. data.title = '';
  128. }
  129. if (data.width === '') {
  130. data.width = null;
  131. }
  132. if (data.height === '') {
  133. data.height = null;
  134. }
  135. if (!data.style) {
  136. data.style = null;
  137. }
  138. // Setup new data excluding style properties
  139. /*eslint dot-notation: 0*/
  140. data = {
  141. src: data.src,
  142. alt: data.alt,
  143. title: data.title,
  144. width: data.width,
  145. height: data.height,
  146. style: data.style,
  147. caption: data.caption,
  148. "class": data["class"]
  149. };
  150. editor.undoManager.transact(function() {
  151. // WP
  152. var eventData = { node: imgElm, data: data, wpcaption: wpcaption };
  153. editor.fire( 'wpImageFormSubmit', { imgData: eventData } );
  154. if ( eventData.cancel ) {
  155. waitLoad( eventData.node );
  156. return;
  157. }
  158. // WP end
  159. if (!data.src) {
  160. if (imgElm) {
  161. dom.remove(imgElm);
  162. editor.focus();
  163. editor.nodeChanged();
  164. }
  165. return;
  166. }
  167. if (data.title === "") {
  168. data.title = null;
  169. }
  170. if (!imgElm) {
  171. data.id = '__mcenew';
  172. editor.focus();
  173. editor.selection.setContent(dom.createHTML('img', data));
  174. imgElm = dom.get('__mcenew');
  175. dom.setAttrib(imgElm, 'id', null);
  176. } else {
  177. dom.setAttribs(imgElm, data);
  178. }
  179. editor.editorUpload.uploadImagesAuto();
  180. if (data.caption === false) {
  181. if (dom.is(imgElm.parentNode, 'figure.image')) {
  182. figureElm = imgElm.parentNode;
  183. dom.insertAfter(imgElm, figureElm);
  184. dom.remove(figureElm);
  185. }
  186. }
  187. function isTextBlock(node) {
  188. return editor.schema.getTextBlockElements()[node.nodeName];
  189. }
  190. if (data.caption === true) {
  191. if (!dom.is(imgElm.parentNode, 'figure.image')) {
  192. oldImg = imgElm;
  193. imgElm = imgElm.cloneNode(true);
  194. figureElm = dom.create('figure', {'class': 'image'});
  195. figureElm.appendChild(imgElm);
  196. figureElm.appendChild(dom.create('figcaption', {contentEditable: true}, 'Caption'));
  197. figureElm.contentEditable = false;
  198. var textBlock = dom.getParent(oldImg, isTextBlock);
  199. if (textBlock) {
  200. dom.split(textBlock, oldImg, figureElm);
  201. } else {
  202. dom.replace(figureElm, oldImg);
  203. }
  204. editor.selection.select(figureElm);
  205. }
  206. return;
  207. }
  208. waitLoad(imgElm);
  209. });
  210. }
  211. function removePixelSuffix(value) {
  212. if (value) {
  213. value = value.replace(/px$/, '');
  214. }
  215. return value;
  216. }
  217. function srcChange(e) {
  218. var srcURL, prependURL, absoluteURLPattern, meta = e.meta || {};
  219. if (imageListCtrl) {
  220. imageListCtrl.value(editor.convertURL(this.value(), 'src'));
  221. }
  222. tinymce.each(meta, function(value, key) {
  223. win.find('#' + key).value(value);
  224. });
  225. if (!meta.width && !meta.height) {
  226. srcURL = editor.convertURL(this.value(), 'src');
  227. // Pattern test the src url and make sure we haven't already prepended the url
  228. prependURL = editor.settings.image_prepend_url;
  229. absoluteURLPattern = new RegExp('^(?:[a-z]+:)?//', 'i');
  230. if (prependURL && !absoluteURLPattern.test(srcURL) && srcURL.substring(0, prependURL.length) !== prependURL) {
  231. srcURL = prependURL + srcURL;
  232. }
  233. this.value(srcURL);
  234. getImageSize(editor.documentBaseURI.toAbsolute(this.value()), function(data) {
  235. if (data.width && data.height && imageDimensions) {
  236. width = data.width;
  237. height = data.height;
  238. win.find('#width').value(width);
  239. win.find('#height').value(height);
  240. }
  241. });
  242. }
  243. }
  244. imgElm = editor.selection.getNode();
  245. figureElm = dom.getParent(imgElm, 'figure.image');
  246. if (figureElm) {
  247. imgElm = dom.select('img', figureElm)[0];
  248. }
  249. if (imgElm && (imgElm.nodeName != 'IMG' || imgElm.getAttribute('data-mce-object') || imgElm.getAttribute('data-mce-placeholder'))) {
  250. imgElm = null;
  251. }
  252. if (imgElm) {
  253. width = dom.getAttrib(imgElm, 'width');
  254. height = dom.getAttrib(imgElm, 'height');
  255. data = {
  256. src: dom.getAttrib(imgElm, 'src'),
  257. alt: dom.getAttrib(imgElm, 'alt'),
  258. title: dom.getAttrib(imgElm, 'title'),
  259. "class": dom.getAttrib(imgElm, 'class'),
  260. width: width,
  261. height: height,
  262. caption: !!figureElm
  263. };
  264. // WP
  265. editor.fire( 'wpLoadImageData', { imgData: { data: data, node: imgElm } } );
  266. }
  267. if (imageList) {
  268. imageListCtrl = {
  269. type: 'listbox',
  270. label: 'Image list',
  271. values: buildListItems(
  272. imageList,
  273. function(item) {
  274. item.value = editor.convertURL(item.value || item.url, 'src');
  275. },
  276. [{text: 'None', value: ''}]
  277. ),
  278. value: data.src && editor.convertURL(data.src, 'src'),
  279. onselect: function(e) {
  280. var altCtrl = win.find('#alt');
  281. if (!altCtrl.value() || (e.lastControl && altCtrl.value() == e.lastControl.text())) {
  282. altCtrl.value(e.control.text());
  283. }
  284. win.find('#src').value(e.control.value()).fire('change');
  285. },
  286. onPostRender: function() {
  287. /*eslint consistent-this: 0*/
  288. imageListCtrl = this;
  289. }
  290. };
  291. }
  292. if (editor.settings.image_class_list) {
  293. classListCtrl = {
  294. name: 'class',
  295. type: 'listbox',
  296. label: 'Class',
  297. values: buildListItems(
  298. editor.settings.image_class_list,
  299. function(item) {
  300. if (item.value) {
  301. item.textStyle = function() {
  302. return editor.formatter.getCssText({inline: 'img', classes: [item.value]});
  303. };
  304. }
  305. }
  306. )
  307. };
  308. }
  309. // General settings shared between simple and advanced dialogs
  310. var generalFormItems = [
  311. {
  312. name: 'src',
  313. type: 'filepicker',
  314. filetype: 'image',
  315. label: 'Source',
  316. autofocus: true,
  317. onchange: srcChange
  318. },
  319. imageListCtrl
  320. ];
  321. if (editor.settings.image_description !== false) {
  322. generalFormItems.push({name: 'alt', type: 'textbox', label: 'Image description'});
  323. }
  324. if (editor.settings.image_title) {
  325. generalFormItems.push({name: 'title', type: 'textbox', label: 'Image Title'});
  326. }
  327. if (imageDimensions) {
  328. generalFormItems.push({
  329. type: 'container',
  330. label: 'Dimensions',
  331. layout: 'flex',
  332. direction: 'row',
  333. align: 'center',
  334. spacing: 5,
  335. items: [
  336. {name: 'width', type: 'textbox', maxLength: 5, size: 3, onchange: recalcSize, ariaLabel: 'Width'},
  337. {type: 'label', text: 'x'},
  338. {name: 'height', type: 'textbox', maxLength: 5, size: 3, onchange: recalcSize, ariaLabel: 'Height'},
  339. {name: 'constrain', type: 'checkbox', checked: true, text: 'Constrain proportions'}
  340. ]
  341. });
  342. }
  343. generalFormItems.push(classListCtrl);
  344. if (editor.settings.image_caption && tinymce.Env.ceFalse) {
  345. generalFormItems.push({name: 'caption', type: 'checkbox', label: 'Caption'});
  346. }
  347. // WP
  348. editor.fire( 'wpLoadImageForm', { data: generalFormItems } );
  349. function mergeMargins(css) {
  350. if (css.margin) {
  351. var splitMargin = css.margin.split(" ");
  352. switch (splitMargin.length) {
  353. case 1: //margin: toprightbottomleft;
  354. css['margin-top'] = css['margin-top'] || splitMargin[0];
  355. css['margin-right'] = css['margin-right'] || splitMargin[0];
  356. css['margin-bottom'] = css['margin-bottom'] || splitMargin[0];
  357. css['margin-left'] = css['margin-left'] || splitMargin[0];
  358. break;
  359. case 2: //margin: topbottom rightleft;
  360. css['margin-top'] = css['margin-top'] || splitMargin[0];
  361. css['margin-right'] = css['margin-right'] || splitMargin[1];
  362. css['margin-bottom'] = css['margin-bottom'] || splitMargin[0];
  363. css['margin-left'] = css['margin-left'] || splitMargin[1];
  364. break;
  365. case 3: //margin: top rightleft bottom;
  366. css['margin-top'] = css['margin-top'] || splitMargin[0];
  367. css['margin-right'] = css['margin-right'] || splitMargin[1];
  368. css['margin-bottom'] = css['margin-bottom'] || splitMargin[2];
  369. css['margin-left'] = css['margin-left'] || splitMargin[1];
  370. break;
  371. case 4: //margin: top right bottom left;
  372. css['margin-top'] = css['margin-top'] || splitMargin[0];
  373. css['margin-right'] = css['margin-right'] || splitMargin[1];
  374. css['margin-bottom'] = css['margin-bottom'] || splitMargin[2];
  375. css['margin-left'] = css['margin-left'] || splitMargin[3];
  376. }
  377. delete css.margin;
  378. }
  379. return css;
  380. }
  381. function updateStyle() {
  382. function addPixelSuffix(value) {
  383. if (value.length > 0 && /^[0-9]+$/.test(value)) {
  384. value += 'px';
  385. }
  386. return value;
  387. }
  388. if (!editor.settings.image_advtab) {
  389. return;
  390. }
  391. var data = win.toJSON(),
  392. css = dom.parseStyle(data.style);
  393. css = mergeMargins(css);
  394. if (data.vspace) {
  395. css['margin-top'] = css['margin-bottom'] = addPixelSuffix(data.vspace);
  396. }
  397. if (data.hspace) {
  398. css['margin-left'] = css['margin-right'] = addPixelSuffix(data.hspace);
  399. }
  400. if (data.border) {
  401. css['border-width'] = addPixelSuffix(data.border);
  402. }
  403. win.find('#style').value(dom.serializeStyle(dom.parseStyle(dom.serializeStyle(css))));
  404. }
  405. function updateVSpaceHSpaceBorder() {
  406. if (!editor.settings.image_advtab) {
  407. return;
  408. }
  409. var data = win.toJSON(),
  410. css = dom.parseStyle(data.style);
  411. win.find('#vspace').value("");
  412. win.find('#hspace').value("");
  413. css = mergeMargins(css);
  414. //Move opposite equal margins to vspace/hspace field
  415. if ((css['margin-top'] && css['margin-bottom']) || (css['margin-right'] && css['margin-left'])) {
  416. if (css['margin-top'] === css['margin-bottom']) {
  417. win.find('#vspace').value(removePixelSuffix(css['margin-top']));
  418. } else {
  419. win.find('#vspace').value('');
  420. }
  421. if (css['margin-right'] === css['margin-left']) {
  422. win.find('#hspace').value(removePixelSuffix(css['margin-right']));
  423. } else {
  424. win.find('#hspace').value('');
  425. }
  426. }
  427. //Move border-width
  428. if (css['border-width']) {
  429. win.find('#border').value(removePixelSuffix(css['border-width']));
  430. }
  431. win.find('#style').value(dom.serializeStyle(dom.parseStyle(dom.serializeStyle(css))));
  432. }
  433. if (editor.settings.image_advtab) {
  434. // Parse styles from img
  435. if (imgElm) {
  436. if (imgElm.style.marginLeft && imgElm.style.marginRight && imgElm.style.marginLeft === imgElm.style.marginRight) {
  437. data.hspace = removePixelSuffix(imgElm.style.marginLeft);
  438. }
  439. if (imgElm.style.marginTop && imgElm.style.marginBottom && imgElm.style.marginTop === imgElm.style.marginBottom) {
  440. data.vspace = removePixelSuffix(imgElm.style.marginTop);
  441. }
  442. if (imgElm.style.borderWidth) {
  443. data.border = removePixelSuffix(imgElm.style.borderWidth);
  444. }
  445. data.style = editor.dom.serializeStyle(editor.dom.parseStyle(editor.dom.getAttrib(imgElm, 'style')));
  446. }
  447. // Advanced dialog shows general+advanced tabs
  448. win = editor.windowManager.open({
  449. title: 'Insert/edit image',
  450. data: data,
  451. bodyType: 'tabpanel',
  452. body: [
  453. {
  454. title: 'General',
  455. type: 'form',
  456. items: generalFormItems
  457. },
  458. {
  459. title: 'Advanced',
  460. type: 'form',
  461. pack: 'start',
  462. items: [
  463. {
  464. label: 'Style',
  465. name: 'style',
  466. type: 'textbox',
  467. onchange: updateVSpaceHSpaceBorder
  468. },
  469. {
  470. type: 'form',
  471. layout: 'grid',
  472. packV: 'start',
  473. columns: 2,
  474. padding: 0,
  475. alignH: ['left', 'right'],
  476. defaults: {
  477. type: 'textbox',
  478. maxWidth: 50,
  479. onchange: updateStyle
  480. },
  481. items: [
  482. {label: 'Vertical space', name: 'vspace'},
  483. {label: 'Horizontal space', name: 'hspace'},
  484. {label: 'Border', name: 'border'}
  485. ]
  486. }
  487. ]
  488. }
  489. ],
  490. onSubmit: onSubmitForm
  491. });
  492. } else {
  493. // Simple default dialog
  494. win = editor.windowManager.open({
  495. title: 'Insert/edit image',
  496. data: data,
  497. body: generalFormItems,
  498. onSubmit: onSubmitForm
  499. });
  500. }
  501. }
  502. editor.on('preInit', function() {
  503. function hasImageClass(node) {
  504. var className = node.attr('class');
  505. return className && /\bimage\b/.test(className);
  506. }
  507. function toggleContentEditableState(state) {
  508. return function(nodes) {
  509. var i = nodes.length, node;
  510. function toggleContentEditable(node) {
  511. node.attr('contenteditable', state ? 'true' : null);
  512. }
  513. while (i--) {
  514. node = nodes[i];
  515. if (hasImageClass(node)) {
  516. node.attr('contenteditable', state ? 'false' : null);
  517. tinymce.each(node.getAll('figcaption'), toggleContentEditable);
  518. }
  519. }
  520. };
  521. }
  522. editor.parser.addNodeFilter('figure', toggleContentEditableState(true));
  523. editor.serializer.addNodeFilter('figure', toggleContentEditableState(false));
  524. });
  525. editor.addButton('image', {
  526. icon: 'image',
  527. tooltip: 'Insert/edit image',
  528. onclick: createImageList(showDialog),
  529. stateSelector: 'img:not([data-mce-object],[data-mce-placeholder]),figure.image'
  530. });
  531. editor.addMenuItem('image', {
  532. icon: 'image',
  533. text: 'Insert/edit image',
  534. onclick: createImageList(showDialog),
  535. context: 'insert',
  536. prependToContext: true
  537. });
  538. editor.addCommand('mceImage', createImageList(showDialog));
  539. });