leditor.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <view class="editor">
  3. <view class="cons">
  4. <view v-if="read">
  5. <u-parse :content="value"></u-parse>
  6. </view>
  7. <view v-else>
  8. <view class="toolbar" @tap="format">
  9. <view :class="formats.list === 'ordered' ? 'ql-active' : ''" class="icon" data-name="list" data-value="ordered" title="编号">&#xe7f5;</view>
  10. <view class="icon" @tap="clear" title="清空内容">&#xe8b6;</view>
  11. </view>
  12. <editor id="editor" class="ql-container" :placeholder="placeholder" showImgSize showImgToolbar showImgResize @input="editorChange" @statuschange="onStatusChange" :read-only="false" @ready="onEditorReady"></editor>
  13. </view>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. name: 'leditor',
  20. props: {
  21. value: {
  22. default: ''
  23. },
  24. read: {
  25. type: Boolean,
  26. default: false
  27. },
  28. placeholder: {
  29. type: String,
  30. default: '输入就诊记录,支持图文,快捷插入就诊日期'
  31. }
  32. },
  33. data() {
  34. return {
  35. ready: false,
  36. formats: {}
  37. };
  38. },
  39. methods: {
  40. //初始化
  41. onEditorReady() {
  42. uni.createSelectorQuery()
  43. .in(this)
  44. .select('#editor')
  45. .context((res) => {
  46. this.editorCtx = res.context;
  47. })
  48. .exec();
  49. },
  50. //设置编辑器内容
  51. setContents() {
  52. if (!this.read) {
  53. setTimeout(() => {
  54. this.editorCtx.setContents({
  55. html: this.value
  56. });
  57. }, 3000);
  58. }
  59. },
  60. //修改样式
  61. format(e) {
  62. let { name, value } = e.target.dataset;
  63. if (!name) return;
  64. this.editorCtx.format(name, value);
  65. },
  66. //插入分割线
  67. insertDivider() {
  68. this.editorCtx.insertDivider();
  69. },
  70. //插入日期
  71. insertDate() {
  72. const date = new Date();
  73. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  74. this.editorCtx.insertText({
  75. text: formatDate
  76. });
  77. },
  78. //改变编辑器内样式
  79. onStatusChange(e) {
  80. const formats = e.detail;
  81. this.formats = formats;
  82. },
  83. //清除全部内容
  84. clear() {
  85. this.editorCtx.clear();
  86. },
  87. //内容改变时
  88. editorChange: function (e) {
  89. this.$emit('input', e.detail.html);
  90. },
  91. //插入图片
  92. insertImage() {
  93. uni.chooseImage({
  94. count: 9, //默认9
  95. sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
  96. sourceType: ['album'], //从相册选择
  97. success: (chooseImageRes) => {
  98. chooseImageRes.tempFilePaths.forEach((item) => {
  99. uni.showLoading({ title: '正在上传图片...', mask: true });
  100. uni.uploadFile({
  101. url: this.http.urls.upload,
  102. filePath: item,
  103. name: 'file',
  104. header: { Authorization: this.getUser().token },
  105. success: (res) => {
  106. uni.hideLoading();
  107. let result = JSON.parse(res.data);
  108. if (result.code === 200) {
  109. this.editorCtx.insertImage({
  110. src: this.http.urls.ip + JSON.parse(res.data).fileName,
  111. alt: '图像'
  112. });
  113. } else {
  114. uni.showModal({ content: result.msg, showCancel: false });
  115. }
  116. }
  117. });
  118. });
  119. }
  120. });
  121. }
  122. }
  123. };
  124. </script>
  125. <style lang="scss">
  126. .editor {
  127. padding: 11px 0px 0px 0px;
  128. .cons {
  129. width: 100%;
  130. .toolbar {
  131. border: 1px solid #dadbde;
  132. border-bottom: 0px;
  133. background-color: whitesmoke;
  134. border-radius: 5px 5px 0px 0px;
  135. .icon {
  136. display: inline-block;
  137. cursor: pointer;
  138. font-size: 20px;
  139. padding: 6px 6px;
  140. width: 23px;
  141. height: 23px;
  142. }
  143. .ql-active {
  144. color: #06c;
  145. }
  146. }
  147. .ql-container {
  148. padding: 10px;
  149. border: 1px solid #dadbde;
  150. font-size: 15px;
  151. background-color: white;
  152. border-radius: 0px 0px 5px 5px;
  153. image {
  154. width: 100%;
  155. }
  156. }
  157. }
  158. }
  159. </style>