ckeditor5和vuedraggle一起使用时,拖拽会导致将内容拖拽到了编辑器里面,如下:



如上图,按住拖拽时,当经过编辑器,编辑器会自动获取焦点,此时松开鼠标,会将拖拽的内容转成文字填入编辑器内。解决方案就是在拖拽时改变富文本状态,禁止编辑,拖拽结束后再恢复可编辑状态。
vuedraggle通过start和end事件监控拖拽发生,在start事件中,调用editor.enableReadOnlyMode( 'my-feature-id' )禁用编辑器,在end事件中,调用editor.disableReadOnlyMode( 'my-feature-id' )来启用编辑器。
具体代码如下:
1、在ckeditor5插件里定义如下方法并暴露出去:
01 | function disableEditor(id) { |
02 | editor.enableReadOnlyMode(id) |
04 | function enableEditor(id) { |
05 | editor.disableReadOnlyMode(id) |
2、在调用ckeditor5的页面,监听拖拽的两个事件,分别禁用和启用ckeditor5
02 | < draggable :list = "ruleForm.articleContent" handle = ".move" ghost-class = "ghost" chosen-class = "chosenClass" animation = "300" @ start = "onStart" @ end = "onEnd" item-key = "fileId" > |
03 | < template # item = "{ element }" > |
05 | < el-icon class = "move" >< el-icon-rank /></ el-icon > |
06 | < el-icon class = "close" @ click = "handleDelete(element)" >< el-icon-CloseBold /></ el-icon > |
07 | < div class = "flex flex-middle" > |
08 | < el-image class = "img" :src = "element.path" :preview-src-list = "getPreviewSrcList()" :initial-index = "0" preview-teleported /> |
12 | < el-input type = "text" v-model = "element.fileName" style = "width:100%" /> |
17 | v-model = "element.desc" |
21 | :ref="(el)=>setItemRef(el, 'editor'+tool.generateUUID())" |
23 | 'fontFamily', 'fontSize', 'bold', 'italic', 'underline', 'strikethrough', |
24 | 'fontBackgroundColor', 'fontColor', 'outdent', 'indent', 'alignment', |
30 | < label >{{ element.type===1 ? '内容' : element.type===2 ? '栏目' : '链接' }}</ label > |
31 | < el-input v-if = "element.type===1 || element.type===2" v-model = "element.title" type = "text" readonly style = "width:100%" :placeholder = "element.type===1 ? '请选择内容' : '请选择栏目'" > |
32 | < template #append>< el-button @ click = "handleChoose(element)" >选择</ el-button ></ template > |
34 | < el-input v-else v-model = "element.url" type = "text" placeholder = "请输入url,请以http://或https://开头" style = "width:100%" /> |
35 | < el-button class = "ml15" :type = "element.type===1 ? 'primary' : ''" @ click = "handleSwitch(element, 1)" >链接到内容</ el-button > |
36 | < el-button :type = "element.type===2 ? 'primary' : ''" @ click = "handleSwitch(element, 2)" >链接到栏目</ el-button > |
37 | < el-button :type = "element.type===3 ? 'primary' : ''" @ click = "handleSwitch(element, 3)" >外部地址</ el-button > |
39 | < div class = "row detail" > |
41 | < div >< span >原名称:</ span >{{ element.originalName }}</ div > |
42 | < div >< span >大小:</ span >{{ instance.proxy.$TOOL.sizeTostr(element.size) }}</ div > |
43 | < div >< span >类型:</ span >{{ element.fileType }}</ div > |
3、因为我这是动态渲染出来的多个editor,不得不这么做:
2 | const setItemRef = (el, key) => { |
4、拖拽开始和结束方法
03 | for (let key in editorRefs) { |
04 | editorRefs[key].disableEditor(key) |
09 | for (let key in editorRefs) { |
10 | editorRefs[key].enableEditor(key) |