editor.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. if(note == "<p><br></p>") {
  109. uni.showToast({
  110. title: '公告内容不为空',
  111. duration: 3000,
  112. icon: 'error'
  113. });
  114. return;
  115. } else {
  116. uni.request({
  117. url: this.serverurl + '/TNotice/addNotice',
  118. data: {
  119. "title": title,
  120. "note":note
  121. },
  122. header: {
  123. 'token': token
  124. },
  125. method: "POST",
  126. success: (res) => {
  127. uni.showModal({
  128. title: "提示",
  129. content: res.data.message,
  130. success: (res) => {
  131. }
  132. })
  133. },
  134. });
  135. }
  136. },
  137. fail: function(error) {
  138. console.log(error)
  139. }
  140. })
  141. },
  142. readOnlyChange() {
  143. this.readOnly = !this.readOnly
  144. },
  145. onEditorReady() {
  146. uni.createSelectorQuery().select('#editor').context((res) => {
  147. this.editorCtx = res.context
  148. }).exec()
  149. },
  150. undo() {
  151. this.editorCtx.undo()
  152. },
  153. redo() {
  154. this.editorCtx.redo()
  155. },
  156. format(e) {
  157. let {
  158. name,
  159. value
  160. } = e.target.dataset
  161. if (!name) return
  162. console.log('format', name, value)
  163. this.editorCtx.format(name, value)
  164. },
  165. onStatusChange(e) {
  166. const formats = e.detail
  167. this.formats = formats
  168. },
  169. insertDivider() {
  170. this.editorCtx.insertDivider({
  171. success: function() {
  172. console.log('insert divider success')
  173. }
  174. })
  175. },
  176. clear() {
  177. this.editorCtx.clear({
  178. success: function(res) {
  179. console.log("clear success")
  180. }
  181. })
  182. },
  183. removeFormat() {
  184. this.editorCtx.removeFormat()
  185. },
  186. insertDate() {
  187. const date = new Date()
  188. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
  189. this.editorCtx.insertText({
  190. text: formatDate
  191. })
  192. },
  193. insertImage() {
  194. uni.chooseImage({
  195. count: 1,
  196. success: (res) => {
  197. this.editorCtx.insertImage({
  198. src: res.tempFilePaths[0],
  199. alt: '图像',
  200. success: function() {
  201. console.log('insert image success')
  202. }
  203. })
  204. }
  205. })
  206. }
  207. },
  208. }
  209. </script>
  210. <style lang="scss">
  211. @import "./editor-icon.css";
  212. .page-body {
  213. height: calc(100vh - var(--window-top) - var(--status-bar-height));
  214. }
  215. .head-wrapper {
  216. border-style: solid;
  217. border-width: 2px;
  218. margin: 10px;
  219. }
  220. .wrapper {
  221. height: 100%;
  222. }
  223. .editor-wrapper {
  224. height: calc(100vh - var(--window-top) - var(--status-bar-height) - 50px - 240px);
  225. background: #fff;
  226. }
  227. .iconfont {
  228. display: inline-block;
  229. padding: 8px 8px;
  230. width: 24px;
  231. height: 24px;
  232. cursor: pointer;
  233. font-size: 20px;
  234. }
  235. .toolbar {
  236. box-sizing: border-box;
  237. border-bottom: 0;
  238. font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
  239. }
  240. .ql-container {
  241. box-sizing: border-box;
  242. padding: 12px 15px;
  243. width: 100%;
  244. min-height: 30vh;
  245. height: 100%;
  246. margin-top: 20px;
  247. font-size: 16px;
  248. line-height: 1.5;
  249. }
  250. .ql-active {
  251. color: #06c;
  252. }
  253. .btn-area {
  254. padding: 50upx;
  255. padding-top: 20upx;
  256. padding-bottom: 0upx;
  257. button {
  258. background-color: #007AFF;
  259. }
  260. }
  261. .select-input {
  262. display: flex;
  263. flex-direction: row;
  264. justify-content: flex-start;
  265. font-size: 16px;
  266. margin: 30upx 0 50upx 30upx;
  267. input {
  268. padding-left: 20upx;
  269. // padding-top: 10upx;
  270. background-color: #FFFFFF;
  271. width: 500upx;
  272. height: 50upx;
  273. box-shadow: 0upx 0upx 20upx #D3D3D3;
  274. border-radius: 5upx;
  275. }
  276. }
  277. </style>