editor.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. data() {
  79. return {
  80. readOnly: false,
  81. formats: {}
  82. }
  83. },
  84. onLoad() {
  85. // #ifndef MP-BAIDU
  86. uni.loadFontFace({
  87. family: 'Pacifico',
  88. source: 'url("https://sungd.github.io/Pacifico.ttf")'
  89. })
  90. // #endif
  91. },
  92. methods: {
  93. readOnlyChange() {
  94. this.readOnly = !this.readOnly
  95. },
  96. onEditorReady() {
  97. // #ifdef MP-BAIDU
  98. this.editorCtx = requireDynamicLib('editorLib').createEditorContext('editor');
  99. // #endif
  100. // #ifdef APP-PLUS || MP-WEIXIN || H5
  101. uni.createSelectorQuery().select('#editor').context((res) => {
  102. this.editorCtx = res.context
  103. }).exec()
  104. // #endif
  105. },
  106. undo() {
  107. this.editorCtx.undo()
  108. },
  109. redo() {
  110. this.editorCtx.redo()
  111. },
  112. format(e) {
  113. let {
  114. name,
  115. value
  116. } = e.target.dataset
  117. if (!name) return
  118. // console.log('format', name, value)
  119. this.editorCtx.format(name, value)
  120. },
  121. onStatusChange(e) {
  122. const formats = e.detail
  123. this.formats = formats
  124. },
  125. insertDivider() {
  126. this.editorCtx.insertDivider({
  127. success: function() {
  128. console.log('insert divider success')
  129. }
  130. })
  131. },
  132. clear() {
  133. uni.showModal({
  134. title: '清空编辑器',
  135. content: '确定清空编辑器全部内容?',
  136. success: res => {
  137. if (res.confirm) {
  138. this.editorCtx.clear({
  139. success: function(res) {
  140. console.log("clear success")
  141. }
  142. })
  143. }
  144. }
  145. })
  146. },
  147. removeFormat() {
  148. this.editorCtx.removeFormat()
  149. },
  150. insertDate() {
  151. const date = new Date()
  152. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
  153. this.editorCtx.insertText({
  154. text: formatDate
  155. })
  156. },
  157. insertImage() {
  158. uni.chooseImage({
  159. count: 1,
  160. success: (res) => {
  161. this.editorCtx.insertImage({
  162. src: res.tempFilePaths[0],
  163. alt: '图像',
  164. success: function() {
  165. console.log('insert image success')
  166. }
  167. })
  168. }
  169. })
  170. }
  171. }
  172. }
  173. </script>
  174. <style>
  175. @import "@/static/editor-icon.css";
  176. .page-body {
  177. height: calc(100vh - var(--window-top) - var(--status-bar-height));
  178. }
  179. .wrapper {
  180. height: 100%;
  181. }
  182. .editor-wrapper {
  183. height: calc(100vh - var(--window-top) - var(--status-bar-height) - 140px);
  184. background: #fff;
  185. }
  186. .iconfont {
  187. display: inline-block;
  188. padding: 8px 8px;
  189. width: 24px;
  190. height: 24px;
  191. cursor: pointer;
  192. font-size: 20px;
  193. }
  194. .toolbar {
  195. box-sizing: border-box;
  196. border-bottom: 0;
  197. font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
  198. }
  199. .ql-container {
  200. box-sizing: border-box;
  201. width: 100%;
  202. min-height: 30vh;
  203. height: 100%;
  204. font-size: 16px;
  205. line-height: 1.5;
  206. }
  207. .ql-active {
  208. color: #06c;
  209. }
  210. </style>