com-rich-text.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <style>
  2. .com-rich-text {
  3. line-height: normal;
  4. }
  5. .com-rich-text textarea,
  6. .com-rich-text .edui-editor {
  7. width: 100% !important;
  8. }
  9. </style>
  10. <template id="com-rich-text">
  11. <div class="com-rich-text">
  12. <textarea style="width: 100%" :id="id"></textarea>
  13. <com-attachment style="height: 0" :type="type" :simple="simpleAttachment" :open-dialog="attachmentDialogVisible" :multiple="!simpleAttachment" @closed="attachmentClosed" @selected="attachmentSelected">
  14. </com-attachment>
  15. </div>
  16. </template>
  17. <script src="/resources/ueditor/ueditor.config.js"></script>
  18. <script src="/resources/ueditor/ueditor.all.js"></script>
  19. <script>
  20. Vue.component('com-rich-text', {
  21. template: '#com-rich-text',
  22. props: {
  23. value: null,
  24. simpleAttachment: false,
  25. },
  26. data() {
  27. return {
  28. attachmentDialogVisible: false,
  29. id: 'com-rich-text-' + (Math.floor((Math.random() * 10000) + 1)),
  30. ue: null,
  31. tempContent: this.value,
  32. isInputChange: false,
  33. type: 'images',
  34. };
  35. },
  36. watch: {
  37. value(newVal, oldVal) {
  38. if (!this.isInputChange && newVal) {
  39. if (this.ue && this.ue.isReady === 1) {
  40. this.ue.setContent(newVal);
  41. } else {
  42. this.tempContent = newVal;
  43. }
  44. }
  45. if (this.isInputChange) {
  46. this.isInputChange = false;
  47. }
  48. },
  49. },
  50. mounted() {
  51. this.loadUe();
  52. },
  53. destroyed() {
  54. // 销毁实例
  55. if (this.ue) this.ue.destroy()
  56. },
  57. methods: {
  58. attachmentClosed() {
  59. this.attachmentDialogVisible = false;
  60. },
  61. attachmentSelected(e) {
  62. if (e.length) {
  63. let html = '';
  64. for (let i in e) {
  65. if(e[i].upload_type =='videos'){
  66. html += '<video src="' + e[i].url + '" style="max-width: 100%;" controls poster="" preload="metadata">1</video><span style="display:none">用于解决单个视频不能被保存问题</span><br/>';
  67. }else{
  68. html += '<img src="' + e[i].url + '" style="max-width: 100%;">';
  69. }
  70. }
  71. this.ue.execCommand('inserthtml', html);
  72. }
  73. },
  74. loadUe() {
  75. const vm = this;
  76. this.ue = UE.getEditor(this.id,{
  77. serverUrl: "index.php",
  78. imageActionName: `''&r=common%2Ffile%2Fupload`,
  79. imageFieldName: "file",
  80. serverparam:{
  81. upload_type: "images"
  82. },
  83. });
  84. this.ue.addListener('ready', editor => {
  85. if (this.tempContent) {
  86. this.ue.setContent(this.tempContent);
  87. }
  88. });
  89. this.ue.addListener('keyup', editor => {
  90. this.isInputChange = true;
  91. this.$emit('input', this.ue.getContent());
  92. });
  93. this.ue.addListener('contentChange', editor => {
  94. this.isInputChange = true;
  95. this.$emit('input', this.ue.getContent());
  96. });
  97. let self = this;
  98. UE.registerUI('appinsertimage', (editor, uiName) => {
  99. return new UE.ui.Button({
  100. name: uiName,
  101. title: '插入图片',
  102. //添加额外样式,指定icon图标,这里默认使用一个重复的icon
  103. cssRules: 'background-position: -381px 0px;',
  104. onclick() {
  105. self.ue = editor
  106. self.type = 'images'
  107. vm.attachmentDialogVisible = true;
  108. },
  109. });
  110. });
  111. UE.registerUI('appinsertvideo', (editor, uiName) => {
  112. return new UE.ui.Button({
  113. name: uiName,
  114. title: '插入视频',
  115. //添加额外样式,指定icon图标,这里默认使用一个重复的icon
  116. cssRules: 'background-position: -321px -20px;',
  117. onclick() {
  118. self.ue = editor
  119. self.type = 'videos'
  120. vm.attachmentDialogVisible = true;
  121. },
  122. });
  123. });
  124. }
  125. },
  126. });
  127. </script>