plugin.min.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. tinymce.PluginManager.add('o2upimgs', function(editor, url) {
  2. var pluginName='多图片上传';
  3. debugger;
  4. var o2upimgs = window["o2upimgs_"+editor.id]= {}; //扔外部公共变量,也可以扔一个自定义的位置
  5. var baseURL=tinymce.baseURL;
  6. var iframe1 = baseURL+'/plugins/o2upimgs/upfiles.html';
  7. var base64Encode = editor.getParam('base64Encode', false);
  8. var images_upload_handler = editor.getParam('images_upload_handler', undefined, 'function');
  9. if( !images_upload_handler ){
  10. //var enablePreview = editor.getParam('enablePreview', true);
  11. var localImageMaxWidth = editor.getParam('localImageMaxWidth', 2000);
  12. var reference = editor.getParam('reference');
  13. var referenceType = editor.getParam('referenceType');
  14. images_upload_handler = function(blobInfo, success, failure, image){
  15. debugger;
  16. var file = blobInfo.blob();
  17. var formData = new FormData();
  18. formData.append('file', file, file.name );
  19. MWF.xDesktop.uploadImageByScale( reference, referenceType, localImageMaxWidth, formData, file, function (responseData) {
  20. var id = responseData.data ? responseData.data.id : responseData.id;
  21. var orgid = responseData.data ? responseData.data.origId : responseData.origId;
  22. var src = MWF.xDesktop.getImageSrc( id );
  23. var width = image.naturalWidth;
  24. var height = image.naturalHeight;
  25. //按最大宽度比率缩小
  26. if( localImageMaxWidth && localImageMaxWidth < width ){
  27. height = parseInt( height * (localImageMaxWidth / width) );
  28. }
  29. width = Math.min(width, localImageMaxWidth);
  30. var attributes = {
  31. "data-id": id,
  32. "data-orgid": orgid,
  33. "data-height": ''+height,
  34. "data-width": ''+width,
  35. "data-filename": file.name || '',
  36. "style": 'max-width:100%; width:' + width + 'px',
  37. "onerror": 'MWF.xDesktop.setImageSrc()',
  38. "alt": file.name || '',
  39. "data-prv": 'true' //enablePreview ? 'true' : 'false'
  40. };
  41. if(success)success(src, attributes);
  42. }, function () {
  43. if(failure)failure();
  44. return true;
  45. });
  46. }
  47. }
  48. o2upimgs.images_upload_handler = images_upload_handler;
  49. o2upimgs.images_upload_base_path = editor.getParam('images_upload_base_path', '', 'string');
  50. o2upimgs.o2upimgs_filetype = editor.getParam('o2upimgs_filetype', '.png,.gif,.jpg,.jpeg', 'string');
  51. o2upimgs.res=[];
  52. var openDialog = function() {
  53. return editor.windowManager.openUrl({
  54. title: pluginName,
  55. size: 'large',
  56. url:iframe1,
  57. buttons: [
  58. {
  59. type: 'cancel',
  60. text: 'Close'
  61. },
  62. {
  63. type: 'custom',
  64. text: 'Save',
  65. name: 'save',
  66. primary: true
  67. },
  68. ],
  69. onAction: function (api, details) {
  70. switch (details.name) {
  71. case 'save':
  72. o2upimgs.iframeWindow.querySave(function () {
  73. var html = '';
  74. var imgs = o2upimgs.res;
  75. var len = imgs.length;
  76. for(var i=0;i<len;i++){
  77. if( imgs[i].url ){
  78. var attributes = imgs[i].attributes;
  79. var att = '';
  80. for(var key in attributes){
  81. att = att + key+'="' + attributes[key] + '" ';
  82. }
  83. html += '<img src="'+imgs[i].url+'" '+att+'/>';
  84. }
  85. }
  86. editor.insertContent(html);
  87. o2upimgs.res=[];
  88. editor.fire( 'change' );
  89. api.close();
  90. })
  91. break;
  92. default:
  93. break;
  94. }
  95. }
  96. });
  97. };
  98. editor.ui.registry.getAll().icons.o2upimgs || editor.ui.registry.addIcon('o2upimgs','<svg viewBox="0 0 1280 1024" xmlns="http://www.w3.org/2000/svg" width="24" height="24"><path d="M1126.2,779.8V87.6c0-24-22.6-86.9-83.5-86.9H83.5C14.7,0.7,0,63.7,0,87.7v692c0,36.2,29.2,89.7,83.5,89.7l959.3-1.3c51.7,0,83.5-42.5,83.5-88.3zm-1044,4V86.3h961.6V783.7H82.2v0.1z" fill="#53565A"/><path d="M603,461.6L521.1,366.3,313,629.8,227.2,546.8,102.4,716.8H972.8v-170L768.2,235.2,603.1,461.6zM284.6,358.4a105.4,105.4,0,0,0,73.5-30c19.5-19.1,30.3-45,30.2-71.8,0-56.8-45.9-103-102.4-103-56.6,0-102.4,46.1-102.4,103C183.4,313.5,228,358.4,284.6,358.4z" fill="#9598A0"/><path d="M1197.7,153.6l-0.3,669.3s13.5,113.9-67.4,113.9H153.6c0,24.1,23.9,87.2,83.5,87.2h959.3c58.3,0,83.6-49.5,83.6-89.9V240.8c-0.1-41.8-44.9-87.2-82.3-87.2z" fill="#53565A"/></svg>');
  99. editor.ui.registry.addButton('o2upimgs', {
  100. icon: 'o2upimgs',
  101. tooltip: pluginName,
  102. onAction: function() {
  103. openDialog();
  104. }
  105. });
  106. editor.ui.registry.addMenuItem('o2upimgs', {
  107. icon: 'o2upimgs',
  108. text: '图片批量上传...',
  109. onAction: function() {
  110. openDialog();
  111. }
  112. });
  113. return {
  114. getMetadata: function() {
  115. return {
  116. name: pluginName
  117. // url: "http://tinymce.ax-z.cn/more-plugins/axupimgs.php",
  118. };
  119. }
  120. };
  121. });