index.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <template>
  2. <div class="boxs">
  3. <!-- <el-upload
  4. class="avatar-uploader"
  5. name="file"
  6. action=""
  7. :show-file-list="false"
  8. :data="uploadData"
  9. :http-request="uploadHttpRequest"
  10. >
  11. </el-upload> -->
  12. <quill-editor v-model="content" ref="myQuillEditor" :options="editorOption" @blur="saveHtml" @change="saveHtml1" @focus="getFocus"></quill-editor>
  13. <!-- 富文本内容 -->
  14. </div>
  15. </template>
  16. <script>
  17. import { quillEditor } from "vue-quill-editor";
  18. import ImageBlot from './reviewClass'
  19. // 工具栏配置
  20. // const toolbarOptions = [
  21. // ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
  22. // ["blockquote", "code-block"], // 引用 代码块
  23. // [{ header: 1 }, { header: 2 }], // 1、2 级标题
  24. // [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
  25. // [{ script: "sub" }, { script: "super" }], // 上标/下标
  26. // [{ indent: "-1" }, { indent: "+1" }], // 缩进
  27. // // [{'direction': 'rtl'}], // 文本方向
  28. // [{ size: ["small", false, "large", "huge"] }], // 字体大小
  29. // [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
  30. // [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
  31. // [{ font: [] }], // 字体种类
  32. // [{ align: [] }], // 对齐方式
  33. // ["clean"], // 清除文本格式
  34. // ["link", "image", "video"], // 链接、图片、视频
  35. // ];
  36. export default {
  37. props:{
  38. value:{
  39. type:String,
  40. default:(value)=>{
  41. if(!value){
  42. return ""
  43. }
  44. }
  45. },
  46. placeholder:{
  47. type:String,
  48. default:()=>{
  49. return "请输入"
  50. }
  51. },
  52. filed:{
  53. type:String,
  54. default:()=>{
  55. return ''
  56. }
  57. },
  58. keys:{
  59. type:String,
  60. default:()=>{
  61. const len = 6
  62. const codeList = []
  63. const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz0123456789'
  64. const charsLen = chars.length
  65. for (let i = 0; i < len; i++) {
  66. codeList.push(chars.charAt(Math.floor(Math.random() * charsLen)))
  67. }
  68. return codeList.join('')
  69. }
  70. }
  71. },
  72. data() {
  73. return {
  74. uploadData: {}, // 图片文件
  75. content: this.value||"", // 内容
  76. // content: null,
  77. editorOption: {
  78. placeholder: this.placeholder,
  79. key:this.keys,
  80. // modules: {
  81. // toolbar: {
  82. // container: toolbarOptions,
  83. // handlers: {
  84. // image: function (value) {
  85. // if (value) {
  86. // // 触发input框选择图片文件
  87. // document.querySelector(".avatar-uploader input").click();
  88. // } else {
  89. // this.quill.format("image", false);
  90. // }
  91. // },
  92. // },
  93. // },
  94. // },
  95. },
  96. };
  97. },
  98. watch:{
  99. value(val){
  100. var a = this.content.replace(/<p>|<\/p>/g,'')
  101. if(a!=val){
  102. this.content = val
  103. }
  104. },
  105. },
  106. computed:{
  107. quill(){
  108. return this.$refs.myQuillEditor.quill
  109. }
  110. },
  111. mounted() {
  112. // let quill = this.$refs.myQuillEditor.quill
  113. // this.quill.enable(false)
  114. // this.$nextTick(()=>{
  115. this.quill.enable(true)
  116. // })
  117. this.$forceUpdate()
  118. this.quill.root.addEventListener('paste', evt => {
  119. if (evt.clipboardData && evt.clipboardData.files && evt.clipboardData.files.length) {
  120. evt.preventDefault();
  121. [].forEach.call(evt.clipboardData.files,async file => {
  122. if (!file.type.match(/^image\/(gif|jpe?g|a?png|bmp)/i)) {
  123. return
  124. }
  125. // 获取光标所在位置
  126. let length = this.quill.selection.savedRange.index
  127. var str = await this.fileToBase64(file)
  128. this.quill.insertEmbed(length, 'image',{
  129. url:str,
  130. width:'',
  131. height:'',
  132. style:'width:80px;height: 80px;border: 1px solid #f9f6f675;vertical-align:middle'
  133. })
  134. this.quill.setSelection(length + 2)
  135. })
  136. }
  137. }, false)
  138. },
  139. methods: {
  140. // 将file文件上传转化为base64进行显示
  141. fileToBase64(file) {
  142. return new Promise((resolve, reject) => {
  143. ///FileReader类就是专门用来读文件的
  144. const reader = new FileReader()
  145. //开始读文件
  146. reader.readAsDataURL(file)
  147. reader.onload = () => resolve(reader.result)
  148. // 失败返回失败的信息
  149. reader.onerror = error => reject(error)
  150. })
  151. },
  152. //失去焦点
  153. saveHtml(event) {
  154. // this.quill.enable(false)
  155. // console.log("this.content", this.content);
  156. // this.content = this.content.replace(/<p>|<\/p>/g,'')
  157. // this.$emit('input',this.content)
  158. // this.$nextTick(()=>{
  159. // this.quill.enable(true)
  160. // })
  161. // this.$emit('value',{
  162. // filed:this.filed,
  163. // content:this.content
  164. // })
  165. },
  166. //值变化
  167. saveHtml1(event) {
  168. var a = this.content.replace(/<p>|<\/p>/g,'')
  169. this.$emit('input',a)
  170. },
  171. getFocus(){
  172. this.quill.enable(true)
  173. },
  174. },
  175. };
  176. </script>
  177. <style lang="scss">
  178. .boxs {
  179. /* line-height: normal !important;
  180. height: 500px;
  181. width: 700px;
  182. margin: 20px auto; */
  183. .ql-toolbar{
  184. display: none;
  185. }
  186. .quill-editor{
  187. border:1px solid #dcdfe6;
  188. border-radius: 5px;
  189. .ql-container
  190. {
  191. border:0;
  192. .ql-editor{
  193. display: flex;
  194. align-items: end;
  195. }
  196. }
  197. }
  198. .ql-editor.ql-blank::before {
  199. font-style: normal;
  200. }
  201. }
  202. </style>