useFroalaEditor.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { ref,nextTick } from "vue";
  2. export function useInitFroalaEditor() {
  3. let frolaEditorContentChange=ref(false)//富文本内容是否改变
  4. let lastFocusPosition=ref(null)//最后焦点位置
  5. let imgUploadFlag=ref(true)//图片是否上传完成
  6. let options = {
  7. toolbarButtons: [
  8. "insertImage",
  9. "insertVideo",
  10. "embedly",
  11. "insertFile",
  12. "textColor",
  13. "bold",
  14. "italic",
  15. "underline",
  16. "strikeThrough",
  17. "subscript",
  18. "superscript",
  19. "fontFamily",
  20. "fontSize",
  21. "color",
  22. "inlineClass",
  23. "inlineStyle",
  24. "paragraphStyle",
  25. "lineHeight",
  26. "paragraphFormat",
  27. "align",
  28. "formatOL",
  29. "formatUL",
  30. "outdent",
  31. "indent",
  32. "quote",
  33. "insertTable",
  34. "emoticons",
  35. "fontAwesome",
  36. "specialCharacters",
  37. "insertHR",
  38. "selectAll",
  39. "clearFormatting",
  40. "html",
  41. "undo",
  42. "redo"
  43. ],
  44. height: 500,
  45. fontSize: ["6","8","10","12", "13", "14", "15", "16", "18", "20", "24", "28", "32", "36", "40"],
  46. fontSizeDefaultSelection: "16",
  47. theme: "dark", //主题
  48. placeholderText: "请输入内容",
  49. language: "zh_cn", //国际化
  50. imageUploadURL: import.meta.env.VITE_APP_API_URL + '/report/uploadImg', //上传url
  51. videoUploadURL: import.meta.env.VITE_APP_API_URL + '/report/uploadImg', //上传url
  52. fileUploadURL: import.meta.env.VITE_APP_API_URL + '/report/uploadImg', //上传url 更多上传介绍 请访问https://www.froala.com/wysiwyg-editor/docs/options
  53. imageDefaultWidth: false,
  54. quickInsertEnabled: false,
  55. // quickInsertButtons: ['image', 'ul', 'ol'], //快速插入项
  56. toolbarVisibleWithoutSelection: true, //是否开启 不选中模式
  57. toolbarSticky: false, //操作栏是否自动吸顶
  58. saveInterval: 0,
  59. charCounterCount: false,
  60. events:{
  61. 'xhr.beforeSend': function (xhr) {
  62. console.log(xhr);
  63. xhr.setRequestHeader('Authorization', sessionStorage.getItem('token')||''); // 替换为你的 Token
  64. },
  65. // 内容变化事件
  66. contentChanged:function (){
  67. frolaEditorContentChange.value=true
  68. getSelection().rangeCount&&(lastFocusPosition.value=getSelection().getRangeAt(0))
  69. },
  70. keyup:function(e,editor){
  71. nextTick(()=>{
  72. getSelection().rangeCount&&(lastFocusPosition.value=getSelection().getRangeAt(0))
  73. })
  74. },
  75. click:function(e,editor){
  76. nextTick(()=>{
  77. getSelection().rangeCount&&(lastFocusPosition.value=getSelection().getRangeAt(0))
  78. })
  79. },
  80. 'image.beforePasteUpload':function(){
  81. imgUploadFlag.value=false
  82. },
  83. 'image.beforeUpload':function(){
  84. imgUploadFlag.value=false
  85. },
  86. 'image.inserted':function(){
  87. imgUploadFlag.value=true
  88. },
  89. 'image.error':function(){
  90. imgUploadFlag.value=true
  91. },
  92. },
  93. requestHeaders: {
  94. Authorization: sessionStorage.getItem('token')||''
  95. }
  96. };
  97. /**
  98. * 初始化编辑器
  99. * @param el domid
  100. * @param opts 配置项
  101. * 由于要获取到富文本实例 要在new FroalaEditor(el,opt,callback)的callback中才能获取到
  102. * 方案一返回一个promise
  103. * 方案二直接返回富文本实例 无法在调用initFroalaEditor方法后立即执行实例上的一些方法或者读取属性
  104. * 一般写个settimeout 可以解决
  105. */
  106. const initFroalaEditor = (el,opts) => {
  107. // 方案一
  108. // let ins=null
  109. // return new Promise((resolve,reject)=>{
  110. // options.height=opts?.height??500
  111. // console.log(options);
  112. // ins=new FroalaEditor(el, options,()=>{
  113. // // console.log(ins);
  114. // resolve(ins)
  115. // })
  116. // })
  117. // 方案二
  118. options={...options,...opts}
  119. // options.height=options.height<350?350:options.height
  120. console.log(options);
  121. return new FroalaEditor(el, options);
  122. };
  123. return {
  124. lastFocusPosition,
  125. frolaEditorContentChange,
  126. imgUploadFlag,
  127. initFroalaEditor,
  128. }
  129. }