editor.uvue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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"
  9. data-name="underline"></view>
  10. <view :class="formats.strike ? 'ql-active' : ''" class="iconfont icon-zitishanchuxian" data-name="strike">
  11. </view>
  12. <view :class="formats.align === 'left' ? 'ql-active' : ''" class="iconfont icon-zuoduiqi" data-name="align"
  13. data-value="left"></view>
  14. <view :class="formats.align === 'center' ? 'ql-active' : ''" class="iconfont icon-juzhongduiqi"
  15. data-name="align" data-value="center"></view>
  16. <view :class="formats.align === 'right' ? 'ql-active' : ''" class="iconfont icon-youduiqi" data-name="align"
  17. data-value="right"></view>
  18. <view :class="formats.align === 'justify' ? 'ql-active' : ''" class="iconfont icon-zuoyouduiqi"
  19. data-name="align" data-value="justify"></view>
  20. <view :class="formats.lineHeight ? 'ql-active' : ''" class="iconfont icon-line-height" data-name="lineHeight"
  21. data-value="2"></view>
  22. <view :class="formats.letterSpacing ? 'ql-active' : ''" class="iconfont icon-Character-Spacing"
  23. data-name="letterSpacing" data-value="2em"></view>
  24. <view :class="formats.marginTop ? 'ql-active' : ''" class="iconfont icon-722bianjiqi_duanqianju"
  25. data-name="marginTop" data-value="20px"></view>
  26. <view :class="formats.marginBottom ? 'ql-active' : ''" class="iconfont icon-723bianjiqi_duanhouju"
  27. data-name="marginBottom" data-value="20px"></view>
  28. <view :class="formats.fontFamily ? 'ql-active' : ''" class="iconfont icon-font" data-name="fontFamily"
  29. data-value="Pacifico"></view>
  30. <view :class="formats.fontSize === '24px' ? 'ql-active' : ''" class="iconfont icon-fontsize"
  31. data-name="fontSize" data-value="24px"></view>
  32. <view :class="formats.color === '#0000ff' ? 'ql-active' : ''" class="iconfont icon-text_color"
  33. data-name="color" data-value="#0000ff"></view>
  34. <view :class="formats.backgroundColor === '#00ff00' ? 'ql-active' : ''" class="iconfont icon-fontbgcolor"
  35. data-name="backgroundColor" data-value="#00ff00"></view>
  36. <view class="iconfont icon--checklist" data-name="list" data-value="check"></view>
  37. <view :class="formats.list === 'ordered' ? 'ql-active' : ''" class="iconfont icon-youxupailie"
  38. data-name="list" data-value="ordered"></view>
  39. <view :class="formats.list === 'bullet' ? 'ql-active' : ''" class="iconfont icon-wuxupailie" data-name="list"
  40. data-value="bullet"></view>
  41. <view class="iconfont icon-outdent" data-name="indent" data-value="-1"></view>
  42. <view class="iconfont icon-indent" data-name="indent" data-value="+1"></view>
  43. <view :class="formats.header === 1 ? 'ql-active' : ''" class="iconfont icon-format-header-1"
  44. data-name="header" :data-value="1"></view>
  45. <view :class="formats.script === 'sub' ? 'ql-active' : ''" class="iconfont icon-zitixiabiao"
  46. data-name="script" data-value="sub"></view>
  47. <view :class="formats.script === 'super' ? 'ql-active' : ''" class="iconfont icon-zitishangbiao"
  48. data-name="script" data-value="super"></view>
  49. <view :class="formats.direction === 'rtl' ? 'ql-active' : ''" class="iconfont icon-direction-rtl"
  50. data-name="direction" data-value="rtl"></view>
  51. <view class="iconfont icon-date" @tap="insertDate"></view>
  52. <view class="iconfont icon-fengexian" @tap="insertDivider"></view>
  53. <view class="iconfont icon-charutupian" @tap="chooseInsertImage"></view>
  54. <view class="iconfont icon-clearedformat" @tap="removeFormat"></view>
  55. <view class="iconfont icon-undo" @tap="undo"></view>
  56. <view class="iconfont icon-redo" @tap="redo"></view>
  57. <view class="iconfont icon-shanchu" @tap="clearShowModal"></view>
  58. </view>
  59. <view class="editor-wrapper">
  60. <editor id="editor" class="ql-container" placeholder="开始输入..." show-img-size show-img-toolbar show-img-resize
  61. @statuschange="onStatusChange" :read-only="readOnly" @ready="onEditorReady">
  62. </editor>
  63. </view>
  64. <view>
  65. <button @tap="getCon">控制台打印文本内容</button>
  66. </view>
  67. </view>
  68. </view>
  69. </view>
  70. </template>
  71. <script lang="uts">
  72. type Context = {
  73. id ?: string;
  74. pageId ?: number;
  75. }
  76. export default {
  77. data() {
  78. return {
  79. readOnly: false,
  80. formats: {},
  81. editorCtx: {} as Context,
  82. // 自动化测试
  83. autoTest: false,
  84. undoTest: false,
  85. redoTest: false,
  86. removeFormatTest: false,
  87. insertImageTest: false,
  88. blurTest: false
  89. }
  90. },
  91. onLoad() {
  92. uni.loadFontFace({
  93. family: 'Pacifico',
  94. source: 'url("/static/font/Pacifico-Regular.ttf")',
  95. success() {
  96. console.log('success load font')
  97. },
  98. fail() {
  99. console.log('fail load font load')
  100. }
  101. })
  102. },
  103. methods: {
  104. readOnlyChange() {
  105. this.readOnly = !this.readOnly
  106. },
  107. onEditorReady() {
  108. uni.createSelectorQuery().select('#editor').context((res) => {
  109. this.editorCtx = res.context
  110. }).exec()
  111. },
  112. // 自动化测试专用
  113. setContents(options) {
  114. this.editorCtx.setContents({
  115. delta: {
  116. ops: options
  117. },
  118. success: (res) => {
  119. console.log('setContents-success', res)
  120. },
  121. fail: (err) => {
  122. console.log(err)
  123. }
  124. })
  125. },
  126. blur() {
  127. this.editorCtx.blur({
  128. success: (res) => {
  129. console.log('编辑器失焦:', res)
  130. this.blurTest = true
  131. },
  132. fail: (err) => {
  133. console.log(err)
  134. }
  135. })
  136. },
  137. getCon() {
  138. this.editorCtx.getContents({
  139. success: (res) => {
  140. console.log('文本详情:', res)
  141. },
  142. fail: (err) => {
  143. console.log(err)
  144. }
  145. })
  146. },
  147. undo() {
  148. this.editorCtx.undo({
  149. success: (res) => {
  150. this.undoTest = true
  151. },
  152. fail: (err) => {
  153. this.undoTest = false
  154. }
  155. })
  156. },
  157. redo() {
  158. this.editorCtx.redo({
  159. success: (res) => {
  160. this.redoTest = true
  161. },
  162. fail: (err) => {
  163. this.redoTest = false
  164. }
  165. })
  166. },
  167. format(e) {
  168. let { name, value } = e.target.dataset
  169. if (!name) return
  170. // console.log('format', name, value)
  171. this.editorCtx.format(name, value)
  172. },
  173. onStatusChange(e) {
  174. const formats = e.detail
  175. this.formats = formats
  176. },
  177. insertDivider() {
  178. this.editorCtx.insertDivider({
  179. success: function () {
  180. console.log('insert divider success')
  181. }
  182. })
  183. },
  184. clear() {
  185. this.editorCtx.clear({
  186. success: function (res) {
  187. console.log("clear success")
  188. }
  189. })
  190. },
  191. clearShowModal() {
  192. uni.showModal({
  193. title: '清空编辑器',
  194. content: '确定清空编辑器全部内容?',
  195. success: res => {
  196. if (res.confirm) {
  197. this.clear()
  198. }
  199. }
  200. })
  201. },
  202. removeFormat() {
  203. this.editorCtx.removeFormat({
  204. success: (res) => {
  205. console.log('removeFormat-success', res)
  206. this.removeFormatTest = true
  207. },
  208. fail: (err) => {
  209. this.removeFormatTest = false
  210. }
  211. })
  212. },
  213. insertDate() {
  214. const date = new Date()
  215. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
  216. this.editorCtx.insertText({
  217. text: formatDate
  218. })
  219. },
  220. insertImage(image) {
  221. this.editorCtx.insertImage({
  222. src: image,
  223. alt: '图像',
  224. success: () => {
  225. console.log('insert image success')
  226. this.insertImageTest = true
  227. }
  228. })
  229. },
  230. chooseInsertImage() {
  231. uni.chooseImage({
  232. count: 1,
  233. success: (res) => {
  234. this.insertImage(res.tempFilePaths[0])
  235. }
  236. })
  237. }
  238. }
  239. }
  240. </script>
  241. <style>
  242. @import "./editor-icon.css";
  243. .page-body {
  244. height: calc(100vh - var(--window-top) - var(--status-bar-height));
  245. }
  246. .wrapper {
  247. height: 100%;
  248. }
  249. .editor-wrapper {
  250. height: calc(100vh - var(--window-top) - var(--status-bar-height) - 140px - 46px);
  251. background: #fff;
  252. }
  253. .iconfont {
  254. display: inline-block;
  255. width: 30px;
  256. height: 30px;
  257. cursor: pointer;
  258. font-size: 20px;
  259. margin: 0px 6px;
  260. align-content: center;
  261. }
  262. .toolbar {
  263. box-sizing: border-box;
  264. border-bottom: 0;
  265. font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
  266. flex-direction: row;
  267. flex-wrap: wrap;
  268. height: 140px;
  269. padding-left: 10px;
  270. }
  271. .ql-container {
  272. box-sizing: border-box;
  273. padding: 12px 15px;
  274. width: 100%;
  275. min-height: 30vh;
  276. height: 100%;
  277. font-size: 16px;
  278. line-height: 1.5;
  279. }
  280. .ql-active {
  281. color: #06c;
  282. }
  283. </style>