Wangeditor.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import {rand} from "../../util.js";
  2. const template=`
  3. <link href="/assets/libs/wangeditor/style.css" rel="stylesheet">
  4. <div :style="'width:'+width_">
  5. <div :id="'editor-wrapper-'+id" style="border: 1px solid #ccc;">
  6. <div :id="'toolbar-container-'+id"></div>
  7. <div :id="'editor-container-'+id" :style="'height:'+height_+';border-top: 1px solid #ccc;'"></div>
  8. </div>
  9. </div>
  10. `;
  11. function unescapeHTML(escapedHTML) {
  12. const temp = document.createElement('textarea');
  13. temp.innerHTML = escapedHTML;
  14. return temp.value;
  15. }
  16. export default {
  17. name: "Wangeditor",
  18. data: function () {
  19. return {
  20. id:'',
  21. editor:'',
  22. width_:'',
  23. height_:''
  24. }
  25. },
  26. mounted:function (){
  27. this.id=rand(1000,9999);
  28. if(this.width && (this.width.indexOf('%')>-1 || this.width.indexOf('px')>-1)){
  29. this.width_=this.width;
  30. }else{
  31. this.width_=this.width+'px';
  32. }
  33. if(this.height && (this.height.indexOf('%')>-1 || this.height.indexOf('px')>-1)){
  34. this.height_=this.height;
  35. }else{
  36. this.height_=this.height+'px';
  37. }
  38. Yunqi.use('/assets/libs/wangeditor/index.js').then(res=>{
  39. this.editor=window.wangEditor;
  40. let time=rand(100,999);
  41. setTimeout(()=>{
  42. this.createEditor();
  43. },time);
  44. });
  45. },
  46. props:{
  47. mode:'default',//or 'simple'
  48. value:'',
  49. field:{
  50. type:String,
  51. default: ''
  52. },
  53. width:{
  54. type:String,
  55. default:'100%'
  56. },
  57. height:{
  58. type:String,
  59. default:'350'
  60. }
  61. },
  62. template:template,
  63. emits:['change'],
  64. methods:{
  65. createEditor:function (){
  66. let editorConfig={
  67. placeholder:'请输入内容',
  68. MENU_CONF:{
  69. uploadImage: {
  70. customBrowseAndUpload(insertFn) {
  71. Yunqi.api.open({
  72. url: 'general/attachment/select?limit=10',
  73. title:'选择图片',
  74. icon:'fa fa-image',
  75. width:1000,
  76. height:550,
  77. close:function (imgs){
  78. if(!imgs){
  79. return;
  80. }
  81. imgs.forEach(img=>{
  82. insertFn(img,'',img);
  83. });
  84. }
  85. });
  86. }
  87. },
  88. uploadVideo:{
  89. server: '/ajax/upload'
  90. }
  91. },
  92. onChange:(editor)=>{
  93. if(this.field){
  94. this.$emit('change',{field:this.field,value:editor.getHtml()});
  95. }else{
  96. this.$emit('change',editor.getHtml());
  97. }
  98. }
  99. };
  100. let editor=this.editor.createEditor({
  101. selector: '#editor-container-'+this.id,
  102. html: this.value?unescapeHTML(this.value):'<p><br></p>',
  103. config: editorConfig,
  104. mode: this.mode
  105. });
  106. let toolbarConfig={};
  107. this.editor.createToolbar({
  108. editor,
  109. selector: '#toolbar-container-'+this.id,
  110. config: toolbarConfig,
  111. mode: this.mode
  112. })
  113. }
  114. }
  115. };