富文本编辑器 Tiny 封装为 Vue 组件。

准备

  1. 官网 https://www.tiny.cloud/ 注册账号,申请 APIKEY(建议下载代码,发布到公司的 cdn)
  2. 获取 cdn (tinymce.min.js)

开发

TinyEditor.vue

  1. <template>
  2. <div
  3. class="editor-popup"
  4. v-if="visible"
  5. >
  6. <div
  7. class="tiny-editor"
  8. :style="style"
  9. >
  10. <div
  11. class="tips-line"
  12. v-show="!isInitFinished"
  13. >
  14. <p>初始化中...</p>
  15. </div>
  16. <div v-show="isInitFinished">
  17. <textarea
  18. ref="tinyEditor"
  19. id="tinyTextarea"
  20. name="tinyTextarea"
  21. >欢迎使用 <b>Tiny</b> 富文本编辑器</textarea>
  22. <div class="editor-actions-bar">
  23. <div
  24. class="editor-action-btn"
  25. @click="handleOk"
  26. >确定</div>
  27. <div
  28. class="editor-action-btn"
  29. @click="handleCancel"
  30. >取消</div>
  31. </div>
  32. </div>
  33. </div>
  34. </div>
  35. </template>
  36. <script>
  37. import '@/lib/tinymce-zh_CN.js'
  38. export default {
  39. name: 'TinyEditor',
  40. props: {
  41. visible: {
  42. type: Boolean,
  43. default: false
  44. },
  45. style: {
  46. type: Object,
  47. default: {
  48. width: '800px',
  49. height: '400px'
  50. }
  51. }
  52. },
  53. data() {
  54. return {
  55. // 编辑器是否初始化完成
  56. isInitFinished: false,
  57. // 编辑器实例
  58. tinymce: null,
  59. // 编辑器内容
  60. editorContent: '',
  61. // 默认配置项
  62. tinyDefault: {
  63. imageUrl: 'http://assets.dxycdn.com/gitrepo/bbs/img/bbs/logo.png'
  64. }
  65. }
  66. },
  67. methods: {
  68. handleOk() {
  69. const self = this
  70. this.tinymce.then(function(res) {
  71. self.editorContent = res[0].getContent()
  72. console.log(self.editorContent)
  73. })
  74. },
  75. handleCancel() {
  76. this.editorContent = ''
  77. this.tinymce.then(function(res) {
  78. res[0].resetContent()
  79. })
  80. this.$emit('handleCancel')
  81. }
  82. },
  83. mounted() {
  84. this.$nextTick(() => {
  85. this.tinymce = tinymce.init({
  86. // 选择器配置
  87. selector: '#tinyTextarea',
  88. // 隐藏技术支持
  89. branding: false,
  90. // 隐藏元素路径
  91. elementpath: false,
  92. // 中文语言包
  93. language: 'zh_CN',
  94. // 禁止调整编辑器大小
  95. resize: false,
  96. /**
  97. * 使用 content_css 自定义可编辑区域
  98. * http://tinymce.ax-z.cn/general/customize-ui.php
  99. */
  100. content_css: '',
  101. /**
  102. * 菜单栏配置
  103. * http://tinymce.ax-z.cn/general/basic-setup.php
  104. */
  105. menubar: false,
  106. /**
  107. * 插件配置
  108. * 参考:https://www.tiny.cloud/docs/plugins/
  109. */
  110. plugins:
  111. 'print preview searchreplace autolink directionality visualblocks visualchars fullscreen image link media template code codesample table charmap hr pagebreak nonbreaking anchor insertdatetime advlist lists wordcount imagetools textpattern help paste emoticons autosave bdmap indent2em', // 插件按需加载 字符串方式/数组方式
  112. /**
  113. * 工具栏配置
  114. * 参考:http://tinymce.ax-z.cn/general/basic-setup.php
  115. */
  116. toolbar:
  117. 'code undo redo restoredraft | forecolor backcolor bold italic underline strikethrough link | bullist numlist | alignleft aligncenter alignright alignjustify outdent indent | \
  118. styleselect fontselect fontsizeselect | table image media emoticons hr insertdatetime | preview',
  119. height: 400,
  120. fontsize_formats: '12px 14px 16px 18px 24px 36px 48px 56px 72px',
  121. font_formats:
  122. '微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Symbol=symbol;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings,zapf dingbats;知乎配置=BlinkMacSystemFont, Helvetica Neue, PingFang SC, Microsoft YaHei, Source Han Sans SC, Noto Sans CJK SC, WenQuanYi Micro Hei, sans-serif;小米配置=Helvetica Neue,Helvetica,Arial,Microsoft Yahei,Hiragino Sans GB,Heiti SC,WenQuanYi Micro Hei,sans-serif',
  123. importcss_append: true,
  124. // 自定义文件选择器的回调内容
  125. file_picker_callback: function(callback, value, meta) {
  126. if (meta.filetype === 'file') {
  127. callback(this.tinyDefault.imageUrl, { text: 'DXY' })
  128. }
  129. if (meta.filetype === 'image') {
  130. callback(this.tinyDefault.imageUrl, { alt: 'DXY' })
  131. }
  132. },
  133. // 时间日期格式
  134. template_cdate_format: '[CDATE: %m/%d/%Y : %H:%M:%S]',
  135. template_mdate_format: '[MDATE: %m/%d/%Y : %H:%M:%S]',
  136. image_caption: true,
  137. // 初始化结束后执行
  138. init_instance_callback: editor => {
  139. this.isInitFinished = true
  140. }
  141. })
  142. })
  143. }
  144. }
  145. </script>
  146. <style lang="scss" scoped>
  147. @import '@/styles/var.scss';
  148. .editor-popup {
  149. position: fixed;
  150. top: 0;
  151. right: 0;
  152. bottom: 0;
  153. left: 0;
  154. overflow: auto;
  155. margin: 0;
  156. background-color: rgba(0, 0, 0, 0.4);
  157. .tiny-editor {
  158. position: absolute;
  159. top: 50%;
  160. left: 50%;
  161. transform: translate3d(-50%, -50%, 0);
  162. background-color: #fff;
  163. .tips-line {
  164. display: flex;
  165. justify-content: center;
  166. align-items: center;
  167. width: 100%;
  168. height: 100%;
  169. }
  170. }
  171. .editor-actions-bar {
  172. display: flex;
  173. justify-content: space-between;
  174. align-items: center;
  175. border: 1px solid #fff;
  176. border-top: none;
  177. width: 100%;
  178. height: 35px;
  179. line-height: 35px;
  180. background-color: $--color-primary;
  181. box-sizing: border-box;
  182. .editor-action-btn {
  183. position: relative;
  184. width: 100%;
  185. height: 100%;
  186. text-align: center;
  187. font-weight: 500;
  188. color: #fff;
  189. cursor: pointer;
  190. &:nth-of-type(1) {
  191. border-right: 1px solid #fff;
  192. }
  193. &:hover {
  194. &::after {
  195. position: absolute;
  196. content: ' ';
  197. width: 100%;
  198. height: 100%;
  199. top: 0;
  200. left: 0;
  201. background: rgba(255, 255, 255, 0.2);
  202. }
  203. }
  204. &:active {
  205. opacity: 0.8;
  206. }
  207. }
  208. }
  209. }
  210. </style>

使用

  1. <!-- 富文本编辑器 -->
  2. <tiny-editor
  3. :visible.sync="tinyEditor.visible"
  4. @handleCancel="tinyEditor.visible = false"
  5. />

参考