com-tinymce.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * @link:http://www.gdqijianshi.com/
  4. * @copyright: Copyright (c) 2020 广东七件事集团
  5. * Created by PhpStorm
  6. * Author: huangpan
  7. * Date: 2020-04-07
  8. * Time: 19:56
  9. */
  10. ?>
  11. <template id="com-tinymce">
  12. <div :class="{fullscreen:fullscreen}" class="tinymce-container" :style="{width:containerWidth}">
  13. <textarea :id="tinymceId" class="tinymce-textarea" v-model="myValue"></textarea>
  14. <div class="editor-custom-btn-container">
  15. <com-attachment v-model="imgUrl" :multiple="false" :max="1">
  16. <el-tooltip effect="dark" content="建议尺寸200*200" placement="top">
  17. <el-button style="margin-bottom: 10px;" size="mini">选择文件</el-button>
  18. </el-tooltip>
  19. </com-attachment>
  20. </div>
  21. </div>
  22. </template>
  23. <!-- <script src="https://cdn.jsdelivr.net/npm/tinymce-all-in-one@4.9.3/tinymce.min.js"></script> -->
  24. <script src="/resources/unpkg/tinymce-all-in-one@4.9.3/tinymce.min.js"></script>
  25. <script>
  26. const plugins = ['lists advlist autoresize bbcode code image preview']
  27. const toolbar = ['fontsizeselect | undo redo | bold italic underline | bullist numlist | code | preview | alignleft aligncenter alignright alignjustify']
  28. Vue.component('com-tinymce', {
  29. template: '#com-tinymce',
  30. props: {
  31. id: {
  32. type: String,
  33. default: function () {
  34. return (
  35. 'vue-tinymce-' + +new Date() + ((Math.random() * 1000).toFixed(0) + '')
  36. )
  37. }
  38. },
  39. value: {
  40. type: String,
  41. default: ''
  42. },
  43. toolbar: {
  44. type: Array,
  45. required: false,
  46. default() {
  47. return []
  48. }
  49. },
  50. menubar: {
  51. type: String,
  52. default: 'file edit insert view format table'
  53. },
  54. height: {
  55. type: [Number, String],
  56. required: false,
  57. default: 500
  58. },
  59. width: {
  60. type: [Number, String],
  61. required: false,
  62. default: 'auto'
  63. }
  64. },
  65. data() {
  66. return {
  67. hasChange: false,
  68. hasInit: false,
  69. tinymceId: this.id,
  70. fullscreen: false,
  71. languageTypeList: {
  72. en: 'en',
  73. zh: 'zh_CN',
  74. es: 'es_MX',
  75. ja: 'ja'
  76. },
  77. imgUrl: '',
  78. myValue: this.value,
  79. }
  80. },
  81. computed: {
  82. containerWidth() {
  83. const width = this.width
  84. if (/^[\d]+(\.[\d]+)?$/.test(width)) {
  85. // matches `100`, `'100'`
  86. return `${width}px`
  87. }
  88. return width
  89. }
  90. },
  91. watch: {
  92. value(val) {
  93. val = val.trim();
  94. if (!this.hasChange && this.hasInit || !val) {
  95. this.$nextTick(() =>
  96. window.tinymce.get(this.tinymceId).setContent(val || '')
  97. )
  98. }
  99. this.myValue = val;
  100. },
  101. imgUrl() {
  102. if (this.imgUrl.trim().length === 0) return
  103. tinymce
  104. .get(this.tinymceId)
  105. .insertContent(`<img class="wscnph" src="${this.imgUrl}" >`)
  106. this.imgUrl = ''
  107. },
  108. myValue(newValue) {
  109. this.$emit("input", newValue);
  110. }
  111. },
  112. mounted() {
  113. this.initTinymce()
  114. },
  115. activated() {
  116. if (tinymce) {
  117. this.initTinymce()
  118. }
  119. },
  120. deactivated() {
  121. this.destroyTinymce()
  122. },
  123. destroyed() {
  124. this.destroyTinymce()
  125. },
  126. methods: {
  127. initTinymce() {
  128. const _this = this
  129. tinymce.init({
  130. selector: `#${this.tinymceId}`,
  131. language: this.languageTypeList['zh'],
  132. height: this.height,
  133. body_class: 'panel-body ',
  134. object_resizing: false, // 是否禁用表格图片大小调整
  135. toolbar: this.toolbar.length > 0 ? this.toolbar : toolbar, // 分组工具栏控件
  136. menubar: this.menubar, // 菜单:指定应该出现哪些菜单
  137. plugins: plugins, // 插件
  138. end_container_on_empty_block: true, // enter键 分块
  139. powerpaste_word_import: 'clean', // 是否保留word粘贴样式 clean | merge
  140. code_dialog_height: 450, // 代码框高度 、宽度
  141. code_dialog_width: 450,
  142. advlist_bullet_styles: 'square', // 无序列表 有序列表
  143. advlist_number_styles: 'default',
  144. imagetools_cors_hosts: ['www.tinymce.com', 'codepen.io'],
  145. default_link_target: '_blank',
  146. link_title: false,
  147. branding: false, // 隐藏右下角技术支持
  148. fontsize_formats: '8px 10px 12px 14px 16px 18px 20px 24px 36px', // 字号选择
  149. nonbreaking_force_tab: true, // inserting nonbreaking space &nbsp; need Nonbreaking Space Plugin
  150. init_instance_callback: editor => {
  151. if (_this.value) {
  152. editor.setContent(_this.value)
  153. }
  154. _this.hasInit = true
  155. editor.on('NodeChange Change KeyUp SetContent', () => {
  156. this.hasChange = true
  157. this.$emit('input', editor.getContent())
  158. })
  159. },
  160. setup(editor) {
  161. editor.on('FullscreenStateChanged', e => {
  162. _this.fullscreen = e.state
  163. })
  164. }
  165. })
  166. },
  167. destroyTinymce() {
  168. const tinymce = tinymce.get(this.tinymceId)
  169. if (this.fullscreen) {
  170. tinymce.execCommand('mceFullScreen')
  171. }
  172. if (tinymce) {
  173. tinymce.destroy()
  174. }
  175. },
  176. setContent(value) {
  177. tinymce.get(this.tinymceId).setContent(value)
  178. },
  179. getContent() {
  180. tinymce.get(this.tinymceId).getContent()
  181. },
  182. imageSuccessCBK(arr) {
  183. const _this = this
  184. arr.forEach(v => {
  185. tinymce
  186. .get(_this.tinymceId)
  187. .insertContent(`<img class="wscnph" src="${v.url}" >`)
  188. })
  189. }
  190. }
  191. });
  192. </script>
  193. <style scoped>
  194. .tinymce-container {
  195. position: relative;
  196. line-height: normal;
  197. }
  198. .tinymce-container >>> .mce-fullscreen {
  199. z-index: 10000;
  200. }
  201. .tinymce-textarea {
  202. visibility: hidden;
  203. z-index: -1;
  204. }
  205. .editor-custom-btn-container {
  206. position: absolute;
  207. right: 4px;
  208. top: 4px;
  209. /*z-index: 2005;*/
  210. }
  211. .fullscreen .editor-custom-btn-container {
  212. z-index: 10000;
  213. position: fixed;
  214. }
  215. .editor-upload-btn {
  216. display: inline-block;
  217. }
  218. </style>