editor.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <view class="container">
  3. <view class="page-body">
  4. <view class='wrapper'>
  5. <view class='toolbar' @tap="format" style="height: 120px;overflow-y: auto;">
  6. <view :class="formats.bold ? 'ql-active' : ''" class="iconfont icon-zitijiacu" data-name="bold">
  7. </view>
  8. <view :class="formats.italic ? 'ql-active' : ''" class="iconfont icon-zitixieti" data-name="italic">
  9. </view>
  10. <view :class="formats.underline ? 'ql-active' : ''" class="iconfont icon-zitixiahuaxian"
  11. data-name="underline"></view>
  12. <view :class="formats.strike ? 'ql-active' : ''" class="iconfont icon-zitishanchuxian"
  13. data-name="strike"></view>
  14. <!-- #ifndef MP-BAIDU -->
  15. <view :class="formats.align === 'left' ? 'ql-active' : ''" class="iconfont icon-zuoduiqi"
  16. data-name="align" data-value="left"></view>
  17. <!-- #endif -->
  18. <view :class="formats.align === 'center' ? 'ql-active' : ''" class="iconfont icon-juzhongduiqi"
  19. data-name="align" data-value="center"></view>
  20. <view :class="formats.align === 'right' ? 'ql-active' : ''" class="iconfont icon-youduiqi"
  21. data-name="align" data-value="right"></view>
  22. <view :class="formats.align === 'justify' ? 'ql-active' : ''" class="iconfont icon-zuoyouduiqi"
  23. data-name="align" data-value="justify"></view>
  24. <!-- #ifndef MP-BAIDU -->
  25. <view :class="formats.lineHeight ? 'ql-active' : ''" class="iconfont icon-line-height"
  26. data-name="lineHeight" data-value="2"></view>
  27. <view :class="formats.letterSpacing ? 'ql-active' : ''" class="iconfont icon-Character-Spacing"
  28. data-name="letterSpacing" data-value="2em"></view>
  29. <view :class="formats.marginTop ? 'ql-active' : ''" class="iconfont icon-722bianjiqi_duanqianju"
  30. data-name="marginTop" data-value="20px"></view>
  31. <view :class="formats.marginBottom ? 'ql-active' : ''" class="iconfont icon-723bianjiqi_duanhouju"
  32. data-name="marginBottom" data-value="20px"></view>
  33. <!-- #endif -->
  34. <view class="iconfont icon-clearedformat" @tap="removeFormat"></view>
  35. <!-- #ifndef MP-BAIDU -->
  36. <view :class="formats.fontFamily ? 'ql-active' : ''" class="iconfont icon-font"
  37. data-name="fontFamily" data-value="Pacifico"></view>
  38. <view :class="formats.fontSize === '24px' ? 'ql-active' : ''" class="iconfont icon-fontsize"
  39. data-name="fontSize" data-value="24px"></view>
  40. <!-- #endif -->
  41. <view :class="formats.color === '#0000ff' ? 'ql-active' : ''" class="iconfont icon-text_color"
  42. data-name="color" data-value="#0000ff"></view>
  43. <view :class="formats.backgroundColor === '#00ff00' ? 'ql-active' : ''"
  44. class="iconfont icon-fontbgcolor" data-name="backgroundColor" data-value="#00ff00"></view>
  45. <view class="iconfont icon-date" @tap="insertDate"></view>
  46. <view class="iconfont icon--checklist" data-name="list" data-value="check"></view>
  47. <view :class="formats.list === 'ordered' ? 'ql-active' : ''" class="iconfont icon-youxupailie"
  48. data-name="list" data-value="ordered"></view>
  49. <view :class="formats.list === 'bullet' ? 'ql-active' : ''" class="iconfont icon-wuxupailie"
  50. data-name="list" data-value="bullet"></view>
  51. <view class="iconfont icon-undo" @tap="undo"></view>
  52. <view class="iconfont icon-redo" @tap="redo"></view>
  53. <view class="iconfont icon-outdent" data-name="indent" data-value="-1"></view>
  54. <view class="iconfont icon-indent" data-name="indent" data-value="+1"></view>
  55. <view class="iconfont icon-fengexian" @tap="insertDivider"></view>
  56. <view class="iconfont icon-charutupian" @tap="insertImage"></view>
  57. <view :class="formats.header === 1 ? 'ql-active' : ''" class="iconfont icon-format-header-1"
  58. data-name="header" :data-value="1"></view>
  59. <view :class="formats.script === 'sub' ? 'ql-active' : ''" class="iconfont icon-zitixiabiao"
  60. data-name="script" data-value="sub"></view>
  61. <view :class="formats.script === 'super' ? 'ql-active' : ''" class="iconfont icon-zitishangbiao"
  62. data-name="script" data-value="super"></view>
  63. <view class="iconfont icon-shanchu" @tap="clear"></view>
  64. <view :class="formats.direction === 'rtl' ? 'ql-active' : ''" class="iconfont icon-direction-rtl"
  65. data-name="direction" data-value="rtl"></view>
  66. </view>
  67. <view class="editor-wrapper">
  68. <editor id="editor" class="ql-container" placeholder="开始输入..." show-img-size show-img-toolbar
  69. show-img-resize @statuschange="onStatusChange" :read-only="readOnly" @ready="onEditorReady">
  70. </editor>
  71. </view>
  72. </view>
  73. </view>
  74. </view>
  75. </template>
  76. <script>
  77. export default {
  78. props:{
  79. // readOnly:{
  80. // type: Boolean,
  81. // default () {
  82. // return false
  83. // }
  84. // },
  85. },
  86. data() {
  87. return {
  88. readOnly: false,
  89. formats: {}
  90. }
  91. },
  92. onLoad() {
  93. // #ifndef MP-BAIDU
  94. uni.loadFontFace({
  95. family: 'Pacifico',
  96. source: 'url("https://sungd.github.io/Pacifico.ttf")'
  97. })
  98. // #endif
  99. },
  100. methods: {
  101. readOnlyChange() {
  102. this.readOnly = !this.readOnly
  103. },
  104. onEditorReady() {
  105. // #ifdef MP-BAIDU
  106. this.editorCtx = requireDynamicLib('editorLib').createEditorContext('editor');
  107. // #endif
  108. // #ifdef APP-PLUS || MP-WEIXIN || H5
  109. uni.createSelectorQuery().select('#editor').context((res) => {
  110. this.editorCtx = res.context
  111. }).exec()
  112. // #endif
  113. },
  114. undo() {
  115. this.editorCtx.undo()
  116. },
  117. redo() {
  118. this.editorCtx.redo()
  119. },
  120. format(e) {
  121. let {
  122. name,
  123. value
  124. } = e.target.dataset
  125. if (!name) return
  126. // console.log('format', name, value)
  127. this.editorCtx.format(name, value)
  128. },
  129. onStatusChange(e) {
  130. const formats = e.detail
  131. this.formats = formats
  132. },
  133. insertDivider() {
  134. this.editorCtx.insertDivider({
  135. success: function() {
  136. console.log('insert divider success')
  137. }
  138. })
  139. },
  140. clear() {
  141. uni.showModal({
  142. title: '清空编辑器',
  143. content: '确定清空编辑器全部内容?',
  144. success: res => {
  145. if (res.confirm) {
  146. this.editorCtx.clear({
  147. success: function(res) {
  148. console.log("clear success")
  149. }
  150. })
  151. }
  152. }
  153. })
  154. },
  155. removeFormat() {
  156. this.editorCtx.removeFormat()
  157. },
  158. insertDate() {
  159. const date = new Date()
  160. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
  161. this.editorCtx.insertText({
  162. text: formatDate
  163. })
  164. },
  165. insertImage() {
  166. uni.chooseImage({
  167. count: 1,
  168. success: (res) => {
  169. this.editorCtx.insertImage({
  170. src: res.tempFilePaths[0],
  171. alt: '图像',
  172. success: function() {
  173. console.log('insert image success')
  174. }
  175. })
  176. }
  177. })
  178. }
  179. }
  180. }
  181. </script>
  182. <style>
  183. @import "@/static/editor-icon.css";
  184. .page-body {
  185. height: calc(100vh - var(--window-top) - var(--status-bar-height));
  186. }
  187. .wrapper {
  188. height: 100%;
  189. }
  190. .editor-wrapper {
  191. height: calc(100vh - var(--window-top) - var(--status-bar-height) - 140px);
  192. background: #fff;
  193. }
  194. .iconfont {
  195. display: inline-block;
  196. padding: 8px 8px;
  197. width: 24px;
  198. height: 24px;
  199. cursor: pointer;
  200. font-size: 20px;
  201. }
  202. .toolbar {
  203. box-sizing: border-box;
  204. border-bottom: 0;
  205. font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
  206. }
  207. .ql-container {
  208. box-sizing: border-box;
  209. width: 100%;
  210. min-height: 30vh;
  211. height: 100%;
  212. font-size: 16px;
  213. line-height: 1.5;
  214. }
  215. .ql-active {
  216. color: #06c;
  217. }
  218. </style>