editor.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <template>
  2. <view class="container">
  3. <view class="page-body">
  4. <form @submit="formSubmit" @reset="formReset">
  5. <view class="select-input">
  6. <input class="input" style="display:none;" />
  7. <text>标题:</text><input class="input" name="title" v-model="title" placeholder="请输入标题" />
  8. </view>
  9. <view class="head-wrapper">
  10. <view class='wrapper'>
  11. <view class='toolbar' @tap="format" style="height: 120px;overflow-y: auto;">
  12. <view :class="formats.bold ? 'ql-active' : ''" class="iconfont icon-zitijiacu"
  13. data-name="bold">
  14. </view>
  15. <view :class="formats.italic ? 'ql-active' : ''" class="iconfont icon-zitixieti"
  16. data-name="italic"></view>
  17. <view :class="formats.underline ? 'ql-active' : ''" class="iconfont icon-zitixiahuaxian"
  18. data-name="underline"></view>
  19. <view :class="formats.strike ? 'ql-active' : ''" class="iconfont icon-zitishanchuxian"
  20. data-name="strike"></view>
  21. <view :class="formats.align === 'left' ? 'ql-active' : ''" class="iconfont icon-zuoduiqi"
  22. data-name="align" data-value="left"></view>
  23. <view :class="formats.align === 'center' ? 'ql-active' : ''"
  24. class="iconfont icon-juzhongduiqi" data-name="align" data-value="center"></view>
  25. <view :class="formats.align === 'right' ? 'ql-active' : ''" class="iconfont icon-youduiqi"
  26. data-name="align" data-value="right"></view>
  27. <view :class="formats.align === 'justify' ? 'ql-active' : ''"
  28. class="iconfont icon-zuoyouduiqi" data-name="align" data-value="justify"></view>
  29. <view :class="formats.lineHeight ? 'ql-active' : ''" class="iconfont icon-line-height"
  30. data-name="lineHeight" data-value="2"></view>
  31. <view :class="formats.letterSpacing ? 'ql-active' : ''"
  32. class="iconfont icon-Character-Spacing" data-name="letterSpacing" data-value="2em">
  33. </view>
  34. <view :class="formats.marginTop ? 'ql-active' : ''"
  35. class="iconfont icon-722bianjiqi_duanqianju" data-name="marginTop" data-value="20px">
  36. </view>
  37. <view :class="formats.previewarginBottom ? 'ql-active' : ''"
  38. class="iconfont icon-723bianjiqi_duanhouju" data-name="marginBottom" data-value="20px">
  39. </view>
  40. <view class="iconfont icon-clearedformat" @tap="removeFormat"></view>
  41. <view :class="formats.fontFamily ? 'ql-active' : ''" class="iconfont icon-font"
  42. data-name="fontFamily" data-value="Pacifico"></view>
  43. <view :class="formats.fontSize === '24px' ? 'ql-active' : ''" class="iconfont icon-fontsize"
  44. data-name="fontSize" data-value="24px"></view>
  45. <view :class="formats.color === '#0000ff' ? 'ql-active' : ''"
  46. class="iconfont icon-text_color" data-name="color" data-value="#0000ff"></view>
  47. <view :class="formats.backgroundColor === '#00ff00' ? 'ql-active' : ''"
  48. class="iconfont icon-fontbgcolor" data-name="backgroundColor" data-value="#00ff00">
  49. </view>
  50. <view class="iconfont icon-date" @tap="insertDate"></view>
  51. <view class="iconfont icon--checklist" data-name="list" data-value="check"></view>
  52. <view :class="formats.list === 'ordered' ? 'ql-active' : ''"
  53. class="iconfont icon-youxupailie" data-name="list" data-value="ordered"></view>
  54. <view :class="formats.list === 'bullet' ? 'ql-active' : ''" class="iconfont icon-wuxupailie"
  55. data-name="list" data-value="bullet"></view>
  56. <view class="iconfont icon-undo" @tap="undo"></view>
  57. <view class="iconfont icon-redo" @tap="redo"></view>
  58. <view class="iconfont icon-outdent" data-name="indent" data-value="-1"></view>
  59. <view class="iconfont icon-indent" data-name="indent" data-value="+1"></view>
  60. <view class="iconfont icon-fengexian" @tap="insertDivider"></view>
  61. <view class="iconfont icon-charutupian" @tap="insertImage"></view>
  62. <view :class="formats.header === 1 ? 'ql-active' : ''" class="iconfont icon-format-header-1"
  63. data-name="header" :data-value="1"></view>
  64. <view :class="formats.script === 'sub' ? 'ql-active' : ''" class="iconfont icon-zitixiabiao"
  65. data-name="script" data-value="sub"></view>
  66. <view :class="formats.script === 'super' ? 'ql-active' : ''"
  67. class="iconfont icon-zitishangbiao" data-name="script" data-value="super"></view>
  68. <view class="iconfont icon-shanchu" @tap="clear"></view>
  69. <view :class="formats.direction === 'rtl' ? 'ql-active' : ''"
  70. class="iconfont icon-direction-rtl" data-name="direction" data-value="rtl"></view>
  71. </view>
  72. <view class="editor-wrapper">
  73. <editor id="editor" class="ql-container" placeholder="开始输入..." showImgSize
  74. showImgToolbar showImgResize @statuschange="onStatusChange" :read-only="readOnly"
  75. @ready="onEditorReady">
  76. </editor>
  77. </view>
  78. </view>
  79. </view>
  80. <!-- <button>提交</button> -->
  81. <view class="btn-area">
  82. <button type="primary" formType="submit">确认</button>
  83. </view>
  84. </form>
  85. </view>
  86. </view>
  87. </template>
  88. <script>
  89. export default {
  90. data() {
  91. return {
  92. readOnly: false,
  93. title:'',
  94. formats: {}
  95. }
  96. },
  97. methods: {
  98. formSubmit: function(e) {
  99. console.log('form发生了submit事件,携带数据为:' + JSON.stringify(e.detail.value.title))
  100. var title = this.title;
  101. this.editorCtx.getContents({
  102. success: (res) => {
  103. console.log(res.html)
  104. var note = res.html;
  105. var that = this;
  106. // var id = uni.getStorageSync("globalUser").id;
  107. var token = uni.getStorageSync("token");
  108. uni.request({
  109. url: this.serverurl + '/TNotice/addNotice',
  110. data: {
  111. "title": title,
  112. "note":note
  113. },
  114. header: {
  115. 'token': token
  116. },
  117. method: "POST",
  118. success: (res) => {
  119. uni.showModal({
  120. title: "提示",
  121. content: res.data.message,
  122. success: (res) => {
  123. }
  124. })
  125. },
  126. });
  127. },
  128. fail: function(error) {
  129. console.log(error)
  130. }
  131. })
  132. },
  133. readOnlyChange() {
  134. this.readOnly = !this.readOnly
  135. },
  136. onEditorReady() {
  137. uni.createSelectorQuery().select('#editor').context((res) => {
  138. this.editorCtx = res.context
  139. }).exec()
  140. },
  141. undo() {
  142. this.editorCtx.undo()
  143. },
  144. redo() {
  145. this.editorCtx.redo()
  146. },
  147. format(e) {
  148. let {
  149. name,
  150. value
  151. } = e.target.dataset
  152. if (!name) return
  153. console.log('format', name, value)
  154. this.editorCtx.format(name, value)
  155. },
  156. onStatusChange(e) {
  157. const formats = e.detail
  158. this.formats = formats
  159. },
  160. insertDivider() {
  161. this.editorCtx.insertDivider({
  162. success: function() {
  163. console.log('insert divider success')
  164. }
  165. })
  166. },
  167. clear() {
  168. this.editorCtx.clear({
  169. success: function(res) {
  170. console.log("clear success")
  171. }
  172. })
  173. },
  174. removeFormat() {
  175. this.editorCtx.removeFormat()
  176. },
  177. insertDate() {
  178. const date = new Date()
  179. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
  180. this.editorCtx.insertText({
  181. text: formatDate
  182. })
  183. },
  184. insertImage() {
  185. uni.chooseImage({
  186. count: 1,
  187. success: (res) => {
  188. this.editorCtx.insertImage({
  189. src: res.tempFilePaths[0],
  190. alt: '图像',
  191. success: function() {
  192. console.log('insert image success')
  193. }
  194. })
  195. }
  196. })
  197. }
  198. },
  199. }
  200. </script>
  201. <style lang="scss">
  202. @import "./editor-icon.css";
  203. .page-body {
  204. height: calc(100vh - var(--window-top) - var(--status-bar-height));
  205. }
  206. .head-wrapper {
  207. border-style: solid;
  208. border-width: 2px;
  209. margin: 10px;
  210. }
  211. .wrapper {
  212. height: 100%;
  213. }
  214. .editor-wrapper {
  215. height: calc(100vh - var(--window-top) - var(--status-bar-height) - 50px - 240px);
  216. background: #fff;
  217. }
  218. .iconfont {
  219. display: inline-block;
  220. padding: 8px 8px;
  221. width: 24px;
  222. height: 24px;
  223. cursor: pointer;
  224. font-size: 20px;
  225. }
  226. .toolbar {
  227. box-sizing: border-box;
  228. border-bottom: 0;
  229. font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
  230. }
  231. .ql-container {
  232. box-sizing: border-box;
  233. padding: 12px 15px;
  234. width: 100%;
  235. min-height: 30vh;
  236. height: 100%;
  237. margin-top: 20px;
  238. font-size: 16px;
  239. line-height: 1.5;
  240. }
  241. .ql-active {
  242. color: #06c;
  243. }
  244. .btn-area {
  245. padding: 50upx;
  246. padding-top: 20upx;
  247. padding-bottom: 0upx;
  248. button {
  249. background-color: #007AFF;
  250. }
  251. }
  252. .select-input {
  253. display: flex;
  254. flex-direction: row;
  255. justify-content: flex-start;
  256. font-size: 16px;
  257. margin: 30upx 0 50upx 30upx;
  258. input {
  259. padding-left: 20upx;
  260. // padding-top: 10upx;
  261. background-color: #FFFFFF;
  262. width: 500upx;
  263. height: 50upx;
  264. box-shadow: 0upx 0upx 20upx #D3D3D3;
  265. border-radius: 5upx;
  266. }
  267. }
  268. </style>