plugin.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /**
  2. * plugin.js
  3. *
  4. * Copyright, Moxiecode Systems AB
  5. * Released under LGPL License.
  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(img.clientWidth, 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 applyPreview(items) {
  35. tinymce.each(items, function(item) {
  36. item.textStyle = function() {
  37. return editor.formatter.getCssText({inline: 'img', classes: [item.value]});
  38. };
  39. });
  40. return items;
  41. }
  42. function createImageList(callback) {
  43. return function() {
  44. var imageList = editor.settings.image_list;
  45. if (typeof(imageList) == "string") {
  46. tinymce.util.XHR.send({
  47. url: imageList,
  48. success: function(text) {
  49. callback(tinymce.util.JSON.parse(text));
  50. }
  51. });
  52. } else {
  53. callback(imageList);
  54. }
  55. };
  56. }
  57. function showDialog(imageList) {
  58. var win, data = {}, dom = editor.dom, imgElm = editor.selection.getNode();
  59. var width, height, imageListCtrl, classListCtrl;
  60. function buildValues(listSettingName, dataItemName, defaultItems) {
  61. var selectedItem, items = [];
  62. tinymce.each(editor.settings[listSettingName] || defaultItems, function(target) {
  63. var item = {
  64. text: target.text || target.title,
  65. value: target.value
  66. };
  67. items.push(item);
  68. if (data[dataItemName] === target.value || (!selectedItem && target.selected)) {
  69. selectedItem = item;
  70. }
  71. });
  72. if (selectedItem && !data[dataItemName]) {
  73. data[dataItemName] = selectedItem.value;
  74. selectedItem.selected = true;
  75. }
  76. return items;
  77. }
  78. function buildImageList() {
  79. var imageListItems = [{text: 'None', value: ''}];
  80. tinymce.each(imageList, function(image) {
  81. imageListItems.push({
  82. text: image.text || image.title,
  83. value: editor.convertURL(image.value || image.url, 'src'),
  84. menu: image.menu
  85. });
  86. });
  87. return imageListItems;
  88. }
  89. function recalcSize() {
  90. var widthCtrl, heightCtrl, newWidth, newHeight;
  91. widthCtrl = win.find('#width')[0];
  92. heightCtrl = win.find('#height')[0];
  93. newWidth = widthCtrl.value();
  94. newHeight = heightCtrl.value();
  95. if (win.find('#constrain')[0].checked() && width && height && newWidth && newHeight) {
  96. if (width != newWidth) {
  97. newHeight = Math.round((newWidth / width) * newHeight);
  98. heightCtrl.value(newHeight);
  99. } else {
  100. newWidth = Math.round((newHeight / height) * newWidth);
  101. widthCtrl.value(newWidth);
  102. }
  103. }
  104. width = newWidth;
  105. height = newHeight;
  106. }
  107. function onSubmitForm() {
  108. function waitLoad(imgElm) {
  109. function selectImage() {
  110. imgElm.onload = imgElm.onerror = null;
  111. editor.selection.select(imgElm);
  112. editor.nodeChanged();
  113. }
  114. imgElm.onload = function() {
  115. if (!data.width && !data.height) {
  116. dom.setAttribs(imgElm, {
  117. width: imgElm.clientWidth,
  118. height: imgElm.clientHeight
  119. });
  120. }
  121. selectImage();
  122. };
  123. imgElm.onerror = selectImage;
  124. }
  125. updateStyle();
  126. recalcSize();
  127. data = tinymce.extend(data, win.toJSON());
  128. if (!data.alt) {
  129. data.alt = '';
  130. }
  131. if (data.width === '') {
  132. data.width = null;
  133. }
  134. if (data.height === '') {
  135. data.height = null;
  136. }
  137. if (data.style === '') {
  138. data.style = null;
  139. }
  140. data = {
  141. src: data.src,
  142. alt: data.alt,
  143. width: data.width,
  144. height: data.height,
  145. style: data.style,
  146. "class": data["class"]
  147. };
  148. if (!data["class"]) {
  149. delete data["class"];
  150. }
  151. editor.undoManager.transact(function() {
  152. if (!data.src) {
  153. if (imgElm) {
  154. dom.remove(imgElm);
  155. editor.focus();
  156. editor.nodeChanged();
  157. }
  158. return;
  159. }
  160. if (!imgElm) {
  161. data.id = '__mcenew';
  162. editor.focus();
  163. editor.selection.setContent(dom.createHTML('img', data));
  164. imgElm = dom.get('__mcenew');
  165. dom.setAttrib(imgElm, 'id', null);
  166. } else {
  167. dom.setAttribs(imgElm, data);
  168. }
  169. waitLoad(imgElm);
  170. });
  171. }
  172. function removePixelSuffix(value) {
  173. if (value) {
  174. value = value.replace(/px$/, '');
  175. }
  176. return value;
  177. }
  178. function srcChange() {
  179. if (imageListCtrl) {
  180. imageListCtrl.value(editor.convertURL(this.value(), 'src'));
  181. }
  182. getImageSize(this.value(), function(data) {
  183. if (data.width && data.height) {
  184. width = data.width;
  185. height = data.height;
  186. win.find('#width').value(width);
  187. win.find('#height').value(height);
  188. }
  189. });
  190. }
  191. width = dom.getAttrib(imgElm, 'width');
  192. height = dom.getAttrib(imgElm, 'height');
  193. if (imgElm.nodeName == 'IMG' && !imgElm.getAttribute('data-mce-object') && !imgElm.getAttribute('data-mce-placeholder')) {
  194. data = {
  195. src: dom.getAttrib(imgElm, 'src'),
  196. alt: dom.getAttrib(imgElm, 'alt'),
  197. "class": dom.getAttrib(imgElm, 'class'),
  198. width: width,
  199. height: height
  200. };
  201. } else {
  202. imgElm = null;
  203. }
  204. if (imageList) {
  205. imageListCtrl = {
  206. type: 'listbox',
  207. label: 'Image list',
  208. values: buildImageList(),
  209. value: data.src && editor.convertURL(data.src, 'src'),
  210. onselect: function(e) {
  211. var altCtrl = win.find('#alt');
  212. if (!altCtrl.value() || (e.lastControl && altCtrl.value() == e.lastControl.text())) {
  213. altCtrl.value(e.control.text());
  214. }
  215. win.find('#src').value(e.control.value());
  216. },
  217. onPostRender: function() {
  218. imageListCtrl = this;
  219. }
  220. };
  221. }
  222. if (editor.settings.image_class_list) {
  223. classListCtrl = {
  224. name: 'class',
  225. type: 'listbox',
  226. label: 'Class',
  227. values: applyPreview(buildValues('image_class_list', 'class'))
  228. };
  229. }
  230. // General settings shared between simple and advanced dialogs
  231. var generalFormItems = [
  232. {name: 'src', type: 'filepicker', filetype: 'image', label: 'Source', autofocus: true, onchange: srcChange},
  233. imageListCtrl
  234. ];
  235. if (editor.settings.image_description !== false) {
  236. generalFormItems.push({name: 'alt', type: 'textbox', label: 'Image description'});
  237. }
  238. if (editor.settings.image_dimensions !== false) {
  239. generalFormItems.push({
  240. type: 'container',
  241. label: 'Dimensions',
  242. layout: 'flex',
  243. direction: 'row',
  244. align: 'center',
  245. spacing: 5,
  246. items: [
  247. {name: 'width', type: 'textbox', maxLength: 5, size: 3, onchange: recalcSize, ariaLabel: 'Width'},
  248. {type: 'label', text: 'x'},
  249. {name: 'height', type: 'textbox', maxLength: 5, size: 3, onchange: recalcSize, ariaLabel: 'Height'},
  250. {name: 'constrain', type: 'checkbox', checked: true, text: 'Constrain proportions'}
  251. ]
  252. });
  253. }
  254. generalFormItems.push(classListCtrl);
  255. function updateStyle() {
  256. function addPixelSuffix(value) {
  257. if (value.length > 0 && /^[0-9]+$/.test(value)) {
  258. value += 'px';
  259. }
  260. return value;
  261. }
  262. if (!editor.settings.image_advtab) {
  263. return;
  264. }
  265. var data = win.toJSON();
  266. var css = dom.parseStyle(data.style);
  267. delete css.margin;
  268. css['margin-top'] = css['margin-bottom'] = addPixelSuffix(data.vspace);
  269. css['margin-left'] = css['margin-right'] = addPixelSuffix(data.hspace);
  270. css['border-width'] = addPixelSuffix(data.border);
  271. win.find('#style').value(dom.serializeStyle(dom.parseStyle(dom.serializeStyle(css))));
  272. }
  273. if (editor.settings.image_advtab) {
  274. // Parse styles from img
  275. if (imgElm) {
  276. data.hspace = removePixelSuffix(imgElm.style.marginLeft || imgElm.style.marginRight);
  277. data.vspace = removePixelSuffix(imgElm.style.marginTop || imgElm.style.marginBottom);
  278. data.border = removePixelSuffix(imgElm.style.borderWidth);
  279. data.style = editor.dom.serializeStyle(editor.dom.parseStyle(editor.dom.getAttrib(imgElm, 'style')));
  280. }
  281. // Advanced dialog shows general+advanced tabs
  282. win = editor.windowManager.open({
  283. title: 'Insert/edit image',
  284. data: data,
  285. bodyType: 'tabpanel',
  286. body: [
  287. {
  288. title: 'General',
  289. type: 'form',
  290. items: generalFormItems
  291. },
  292. {
  293. title: 'Advanced',
  294. type: 'form',
  295. pack: 'start',
  296. items: [
  297. {
  298. label: 'Style',
  299. name: 'style',
  300. type: 'textbox'
  301. },
  302. {
  303. type: 'form',
  304. layout: 'grid',
  305. packV: 'start',
  306. columns: 2,
  307. padding: 0,
  308. alignH: ['left', 'right'],
  309. defaults: {
  310. type: 'textbox',
  311. maxWidth: 50,
  312. onchange: updateStyle
  313. },
  314. items: [
  315. {label: 'Vertical space', name: 'vspace'},
  316. {label: 'Horizontal space', name: 'hspace'},
  317. {label: 'Border', name: 'border'}
  318. ]
  319. }
  320. ]
  321. }
  322. ],
  323. onSubmit: onSubmitForm
  324. });
  325. } else {
  326. // Simple default dialog
  327. win = editor.windowManager.open({
  328. title: 'Insert/edit image',
  329. data: data,
  330. body: generalFormItems,
  331. onSubmit: onSubmitForm
  332. });
  333. }
  334. }
  335. editor.addButton('image', {
  336. icon: 'image',
  337. tooltip: 'Insert/edit image',
  338. onclick: createImageList(showDialog),
  339. stateSelector: 'img:not([data-mce-object],[data-mce-placeholder])'
  340. });
  341. editor.addMenuItem('image', {
  342. icon: 'image',
  343. text: 'Insert image',
  344. onclick: createImageList(showDialog),
  345. context: 'insert',
  346. prependToContext: true
  347. });
  348. });