editor.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <FlexCol height="100vh">
  3. <sp-editor
  4. :toolbar-config="{
  5. excludeKeys: ['direction', 'date', 'lineHeight', 'letterSpacing', 'listCheck'],
  6. iconSize: '18px'
  7. }"
  8. :maxlength="maxLength"
  9. @init="initEditor"
  10. @input="inputOver"
  11. @upinImage="upinImage"
  12. @overMax="overMax"
  13. />
  14. <FlexRow align="center" :padding="18" :gap="15">
  15. <Button :innerStyle="{flex:1}" type="danger" @click="cancel">取消</Button>
  16. <Button :innerStyle="{flex:1}" type="primary" @click="save">保存</Button>
  17. </FlexRow>
  18. <XBarSpace />
  19. </FlexCol>
  20. </template>
  21. <script setup lang="ts">
  22. import { ref } from 'vue';
  23. import { confirm } from '@/components/utils/DialogAction';
  24. import { back, backAndCallOnPageBack } from '@/components/utils/PageAction';
  25. import { showError } from '@/common/composeabe/ErrorDisplay';
  26. import spEditor from '@/uni_modules/sp-editor/components/sp-editor/sp-editor.vue';
  27. import XBarSpace from '@/components/layout/space/XBarSpace.vue';
  28. import Button from '@/components/basic/Button.vue';
  29. import CommonContent from '@/api/CommonContent';
  30. import FlexCol from '@/components/layout/FlexCol.vue';
  31. import FlexRow from '@/components/layout/FlexRow.vue';
  32. const maxLength = ref(-1);
  33. function cancel() {
  34. confirm({
  35. title: '提示',
  36. content: '是否放弃编辑?',
  37. }).then((res) => {
  38. if (res)
  39. back();
  40. })
  41. }
  42. function save() {
  43. uni.setStorage({
  44. key: 'editorContent',
  45. data: currentContent,
  46. success: () => backAndCallOnPageBack('editor', {}),
  47. fail: (e) => showError(e),
  48. })
  49. }
  50. let currentContent = '';
  51. /**
  52. * 获取输入内容
  53. */
  54. function inputOver(e: { html: string; text: string; }) {
  55. // 可以在此处获取到编辑器已编辑的内容
  56. currentContent = e.html;
  57. }
  58. /**
  59. * 超出最大内容限制
  60. * @param {Object} e {html,text} 内容的html文本,和text文本
  61. */
  62. function overMax(e: { html: string; text: string; }) {
  63. // 若设置了最大字数限制,可在此处触发超出限制的回调
  64. }
  65. function initEditor(editor: any) {
  66. uni.getStorage({
  67. key: 'editorMaxLength',
  68. success: (success) => {
  69. maxLength.value = parseInt(success.data);
  70. },
  71. })
  72. uni.getStorage({
  73. key: 'editorContent',
  74. success: (success) => {
  75. editor.setContents({
  76. html: success.data
  77. })
  78. },
  79. })
  80. }
  81. /**
  82. * 直接运行示例工程插入图片无法正常显示的看这里
  83. * 因为插件默认采用云端存储图片的方式
  84. * 以$emit('upinImage', tempFiles, this.editorCtx)的方式回调
  85. * @param {Object} tempFiles
  86. * @param {Object} editorCtx
  87. */
  88. function upinImage(tempFiles: any, editorCtx: any) {
  89. let path;
  90. // #ifdef MP-WEIXIN
  91. path = tempFiles[0].tempFilePath;
  92. // #endif
  93. // #ifndef MP-WEIXIN
  94. path = tempFiles[0].path;
  95. // #endif
  96. console.log('==== upinImage :', path);
  97. CommonContent.uploadFile(path, 'image', 'file').then((res) => {
  98. editorCtx.insertImage({
  99. src: res.fullurl,
  100. width: '80%',
  101. success: function () {}
  102. })
  103. }).catch((e) => showError(e));
  104. }
  105. </script>