vue-quill.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <PageWrapper title="富文本" content="富文本,用于展示图文信息,比如商品详情,文章详情等...">
  3. <el-card shadow="never" class="mt-2 proCard">
  4. <QuillEditor
  5. ref="quillEditor"
  6. :options="options"
  7. v-model:content="myContent"
  8. style="height: 350px"
  9. @ready="readyQuill"
  10. class="quillEditor"
  11. />
  12. <el-space>
  13. <el-button @click="addText">增加文本</el-button>
  14. <el-button @click="addImg">增加图片</el-button>
  15. <el-button @click="getHtml">获取HTML</el-button>
  16. </el-space>
  17. </el-card>
  18. <el-card shadow="never" class="mt-3 proCard" title="HTML 内容">
  19. <el-input
  20. v-model="myContentHtml"
  21. type="textarea"
  22. placeholder="html"
  23. :autosize="{
  24. minRows: 3,
  25. maxRows: 6,
  26. }"
  27. />
  28. </el-card>
  29. </PageWrapper>
  30. </template>
  31. <script lang="ts" setup>
  32. import { ref, reactive, onMounted } from 'vue';
  33. import { QuillEditor } from '@vueup/vue-quill';
  34. import '@vueup/vue-quill/dist/vue-quill.snow.css';
  35. const quillEditor = ref();
  36. const myContent = ref(
  37. '<h4>Naive Ui Admin 是一个基于 vue3,vite2,TypeScript 的中后台解决方案</h4>',
  38. );
  39. const myContentHtml = ref(
  40. '<h4>Naive Ui Admin 是一个基于 vue3,vite2,TypeScript 的中后台解决方案</h4>',
  41. );
  42. const options = reactive({
  43. modules: {
  44. toolbar: [
  45. ['bold', 'italic', 'underline', 'strike'], // toggled buttons
  46. ['blockquote', 'code-block'],
  47. [{ header: 1 }, { header: 2 }], // custom button values
  48. [{ list: 'ordered' }, { list: 'bullet' }],
  49. [{ script: 'sub' }, { script: 'super' }], // superscript/subscript
  50. [{ indent: '-1' }, { indent: '+1' }], // outdent/indent
  51. [{ direction: 'rtl' }], // text direction
  52. [{ size: ['small', false, 'large', 'huge'] }], // custom dropdown
  53. [{ header: [1, 2, 3, 4, 5, 6, false] }],
  54. [{ color: [] }, { background: [] }], // dropdown with defaults from theme
  55. [{ font: [] }],
  56. [{ align: [] }],
  57. ['clean'],
  58. ['image'],
  59. ],
  60. },
  61. theme: 'snow',
  62. placeholder: '输入您喜欢的内容吧!',
  63. });
  64. function readyQuill() {
  65. console.log('Quill准备好了');
  66. }
  67. function getHtml() {
  68. myContentHtml.value = getHtmlVal();
  69. }
  70. function addText() {
  71. const html = getHtmlVal() + '新增加的内容';
  72. quillEditor.value.setHTML(html);
  73. }
  74. function addImg() {
  75. const html =
  76. getHtmlVal() +
  77. '<img style="width:100px" src="https://www.baidu.com/img/flexible/logo/pc/result.png"/>';
  78. quillEditor.value.setHTML(html);
  79. }
  80. function getHtmlVal() {
  81. return quillEditor.value.getHTML();
  82. }
  83. onMounted(() => {
  84. quillEditor.value.setHTML(myContent.value);
  85. });
  86. </script>
  87. <style lang="scss">
  88. .ql-toolbar.ql-snow {
  89. border-top: none;
  90. border-left: none;
  91. border-right: none;
  92. border-bottom: 1px solid #eee;
  93. margin-top: -10px;
  94. }
  95. .ql-container.ql-snow {
  96. border: none;
  97. }
  98. </style>