editor.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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"></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" data-name="marginBottom"
  25. 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. <view class="editor-wrapper">
  57. <editor id="editor" class="ql-container" placeholder="开始输入..." showImgSize showImgToolbar showImgResize
  58. @statuschange="onStatusChange" :read-only="readOnly" @ready="onEditorReady">
  59. </editor>
  60. </view>
  61. </view>
  62. </view>
  63. </view>
  64. </template>
  65. <script>
  66. export default {
  67. data() {
  68. return {
  69. readOnly: false,
  70. formats: {}
  71. }
  72. },
  73. methods: {
  74. readOnlyChange() {
  75. this.readOnly = !this.readOnly
  76. },
  77. onEditorReady() {
  78. uni.createSelectorQuery().select('#editor').context((res) => {
  79. this.editorCtx = res.context
  80. }).exec()
  81. },
  82. undo() {
  83. this.editorCtx.undo()
  84. },
  85. redo() {
  86. this.editorCtx.redo()
  87. },
  88. format(e) {
  89. let {
  90. name,
  91. value
  92. } = e.target.dataset
  93. if (!name) return
  94. // console.log('format', name, value)
  95. this.editorCtx.format(name, value)
  96. },
  97. onStatusChange(e) {
  98. const formats = e.detail
  99. this.formats = formats
  100. },
  101. insertDivider() {
  102. this.editorCtx.insertDivider({
  103. success: function() {
  104. console.log('insert divider success')
  105. }
  106. })
  107. },
  108. clear() {
  109. this.editorCtx.clear({
  110. success: function(res) {
  111. console.log("clear success")
  112. }
  113. })
  114. },
  115. removeFormat() {
  116. this.editorCtx.removeFormat()
  117. },
  118. insertDate() {
  119. const date = new Date()
  120. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
  121. this.editorCtx.insertText({
  122. text: formatDate
  123. })
  124. },
  125. insertImage() {
  126. uni.chooseImage({
  127. count: 1,
  128. success: (res) => {
  129. this.editorCtx.insertImage({
  130. src: res.tempFilePaths[0],
  131. alt: '图像',
  132. success: function() {
  133. console.log('insert image success')
  134. }
  135. })
  136. }
  137. })
  138. }
  139. },
  140. onLoad() {
  141. uni.loadFontFace({
  142. family: 'Pacifico',
  143. source: 'url("https://sungd.github.io/Pacifico.ttf")'
  144. })
  145. },
  146. }
  147. </script>
  148. <style>
  149. @import "./editor-icon.css";
  150. .page-body {
  151. height: calc(100vh - var(--window-top) - var(--status-bar-height));
  152. }
  153. .wrapper {
  154. height: 100%;
  155. }
  156. .editor-wrapper {
  157. height: calc(100vh - var(--window-top) - var(--status-bar-height) - 140px);
  158. background: #fff;
  159. }
  160. .iconfont {
  161. display: inline-block;
  162. padding: 8px 8px;
  163. width: 24px;
  164. height: 24px;
  165. cursor: pointer;
  166. font-size: 20px;
  167. }
  168. .toolbar {
  169. box-sizing: border-box;
  170. border-bottom: 0;
  171. font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
  172. }
  173. .ql-container {
  174. box-sizing: border-box;
  175. padding: 12px 15px;
  176. width: 100%;
  177. min-height: 30vh;
  178. height: 100%;
  179. margin-top: 20px;
  180. font-size: 16px;
  181. line-height: 1.5;
  182. }
  183. .ql-active {
  184. color: #06c;
  185. }
  186. </style>