imgDialog.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import tinymce from "tinymce/tinymce";
  2. let getAlt;
  3. let getSrc;
  4. let getHeight;
  5. let getWidth;
  6. let getLists;
  7. function SRCString (node) {
  8. let str = node.src
  9. return str;
  10. }
  11. function ALTString (node) {
  12. let str = node.alt
  13. return str;
  14. }
  15. function Width (node) {
  16. let str = node.width
  17. return str;
  18. }
  19. function Height (node) {
  20. let str = node.height
  21. return str;
  22. }
  23. function lists (node) {
  24. let html = '';
  25. node.forEach(ele=>{
  26. html += `<img src="${ele}" alt="假装有图" style="width: 33%;height:auto;padding: 10px">`
  27. })
  28. return html;
  29. }
  30. function clickImg (){
  31. let toclick = document.getElementById('gallery').getElementsByTagName('img');
  32. for (let i = 0; i < toclick.length; i++) {
  33. toclick[i].addEventListener('click',function (e) {
  34. getSrc = e.target.src;
  35. let imgSrc = document.getElementById('img');
  36. imgSrc.src = getSrc
  37. },false)
  38. }
  39. }
  40. function dialog (){
  41. return {
  42. title: '图片',
  43. size:'large',
  44. body: {
  45. type: 'panel',
  46. items: [
  47. {
  48. type: 'bar',
  49. items: [
  50. {
  51. type: 'input',
  52. name: 'desdata',
  53. label: '图片描述'
  54. },
  55. {
  56. type: 'sizeinput',
  57. name: 'size',
  58. label: 'Dimensions'
  59. }
  60. ]
  61. },
  62. {
  63. type: 'htmlpanel',
  64. html: `<div style="display: flex;max-height: 450px;overflow-y: scroll">
  65. <div style="width: 400px;">
  66. <img id="img" src="${getSrc}" alt="假装有图" style="width: 400px;min-width: 400px">
  67. </div>
  68. <div style="flex:auto;min-width: 400px;max-height:450px;height: auto;border: 1px solid rgba(0,0,0,0.5);margin: 0 20px">
  69. <div id="gallery" style="display: flex;justify-content: space-between;flex-wrap:wrap;width: 100%;height: 100%;overflow-y: scroll">
  70. <img src="${getSrc}" alt="" style="width: 33%;padding: 10px">
  71. <img src="${getSrc}" alt="" style="width: 33%;height: auto;padding: 10px">
  72. ${getLists}
  73. </div>
  74. </div>
  75. <div>`
  76. },
  77. ]
  78. },
  79. buttons: [
  80. {
  81. type: 'cancel',
  82. name: 'closeButton',
  83. text: '取消'
  84. },
  85. {
  86. type: 'submit',
  87. name: 'submitButton',
  88. text: '保存',
  89. primary: true
  90. }
  91. ],
  92. initialData: {
  93. desdata:`${getAlt}`,
  94. size:{
  95. width: `${getWidth}`,
  96. height: `${getHeight}`,
  97. },
  98. },
  99. onSubmit: function (api) {
  100. var data = api.getData();
  101. tinymce.activeEditor.execCommand('mceInsertContent', false, `<img src="${getSrc}" alt="${data.desdata}" style="width: ${data.size.width}px;height: ${data.size.height}px;">`);
  102. api.close();
  103. },
  104. }
  105. }
  106. tinymce.PluginManager.add("imgdialog", function(editor) {
  107. editor.ui.registry.addMenuItem("imgdialog", {
  108. icon: 'image',
  109. text: "图片管理",
  110. onAction: function () {
  111. getAlt = ALTString(editor.selection.getNode());
  112. getSrc = SRCString(editor.selection.getNode());
  113. getHeight = Height(editor.selection.getNode());
  114. getWidth = Width(editor.selection.getNode());
  115. getLists = lists(['1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg','7.jpg']);
  116. editor.windowManager.open(dialog())
  117. clickImg();
  118. }
  119. });
  120. //只有选中图片,右击时才会出现弹窗按钮
  121. editor.ui.registry.addContextMenu('imgdialog', {
  122. update: function (element) {
  123. return !element.src ? '' : 'imgdialog';
  124. }
  125. });
  126. });