useFroalaEditor.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "fontFamily",
  18. "fontSize",
  19. "color",
  20. "lineHeight",
  21. "align",
  22. "formatOL",
  23. "formatUL",
  24. "outdent",
  25. "indent",
  26. "quote",
  27. "insertTable",
  28. "insertHR",
  29. "undo",
  30. "redo",
  31. ],
  32. height: 500,
  33. fontSize: ["6","8","10","12", "13", "14", "15", "16", "18", "20", "24", "28", "32", "36", "40"],
  34. fontSizeDefaultSelection: "16",
  35. theme: "dark", //主题
  36. placeholderText: "请输入内容",
  37. language: "zh_cn", //国际化
  38. imageUploadURL: import.meta.env.VITE_APP_API_URL + '/report/uploadImg', //上传url
  39. videoUploadURL: import.meta.env.VITE_APP_API_URL + '/report/uploadImg', //上传url
  40. fileUploadURL: import.meta.env.VITE_APP_API_URL + '/report/uploadImg', //上传url 更多上传介绍 请访问https://www.froala.com/wysiwyg-editor/docs/options
  41. imageDefaultWidth: false,
  42. quickInsertEnabled: false,
  43. // quickInsertButtons: ['image', 'ul', 'ol'], //快速插入项
  44. toolbarVisibleWithoutSelection: true, //是否开启 不选中模式
  45. toolbarSticky: false, //操作栏是否自动吸顶
  46. saveInterval: 0,
  47. charCounterCount: false,
  48. events:{
  49. // 内容变化事件
  50. contentChanged:function (){
  51. frolaEditorContentChange.value=true
  52. getSelection().rangeCount&&(lastFocusPosition.value=getSelection().getRangeAt(0))
  53. },
  54. keyup:function(e,editor){
  55. nextTick(()=>{
  56. getSelection().rangeCount&&(lastFocusPosition.value=getSelection().getRangeAt(0))
  57. })
  58. },
  59. click:function(e,editor){
  60. nextTick(()=>{
  61. getSelection().rangeCount&&(lastFocusPosition.value=getSelection().getRangeAt(0))
  62. })
  63. },
  64. 'image.beforePasteUpload':function(){
  65. imgUploadFlag.value=false
  66. },
  67. 'image.beforeUpload':function(){
  68. imgUploadFlag.value=false
  69. },
  70. 'image.inserted':function(){
  71. imgUploadFlag.value=true
  72. },
  73. 'image.error':function(){
  74. imgUploadFlag.value=true
  75. },
  76. }
  77. };
  78. /**
  79. * 初始化编辑器
  80. * @param el domid
  81. * @param opts 配置项
  82. * 由于要获取到富文本实例 要在new FroalaEditor(el,opt,callback)的callback中才能获取到
  83. * 方案一返回一个promise
  84. * 方案二直接返回富文本实例 无法在调用initFroalaEditor方法后立即执行实例上的一些方法或者读取属性
  85. * 一般写个settimeout 可以解决
  86. */
  87. const initFroalaEditor = (el,opts) => {
  88. // 方案一
  89. // let ins=null
  90. // return new Promise((resolve,reject)=>{
  91. // options.height=opts?.height??500
  92. // console.log(options);
  93. // ins=new FroalaEditor(el, options,()=>{
  94. // // console.log(ins);
  95. // resolve(ins)
  96. // })
  97. // })
  98. // 方案二
  99. options={...options,...opts}
  100. // options.height=options.height<350?350:options.height
  101. console.log(options);
  102. return new FroalaEditor(el, options);
  103. };
  104. return {
  105. lastFocusPosition,
  106. frolaEditorContentChange,
  107. imgUploadFlag,
  108. initFroalaEditor,
  109. }
  110. }