editor.vue 6.4 KB

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