Skip to content

Commit

Permalink
fix: 优化拖拽时插入的位置 Tencent#338
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsonliu committed Oct 30, 2022
1 parent dac503d commit e3296fb
Showing 1 changed file with 36 additions and 33 deletions.
69 changes: 36 additions & 33 deletions src/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,40 +292,43 @@ export default class Editor {
editor.on('drop', (codemirror, evt) => {
const files = evt.dataTransfer.files || [];
if (files && files.length > 0) {
for (let i = 0, needBr = false; i < files.length; i++) {
const file = files[i];
const fileType = file.type || '';
// 文本类型或者无类型的,直接读取内容,不做上传文件的操作
if (fileType === '' || /^text/i.test(fileType)) {
continue;
}
this.options.fileUpload(file, (url, params) => {
if (typeof url !== 'string') {
return;
}
// 拖拽上传文件时,强制改成没有文字选择区的状态
codemirror.setSelection(codemirror.getCursor());
const name = params.name ? params.name : file.name;
let type = '';
let poster = '';
if (/video/i.test(file.type)) {
type = '!video';
poster = params.poster ? `{poster=${params.poster}}` : '';
}
if (/audio/i.test(file.type)) {
type = '!audio';
}
if (/image/i.test(file.type)) {
type = '!';
// 增加延时,让drop的位置变成codemirror的光标位置
setTimeout(() => {
for (let i = 0, needBr = false; i < files.length; i++) {
const file = files[i];
const fileType = file.type || '';
// 文本类型或者无类型的,直接读取内容,不做上传文件的操作
if (fileType === '' || /^text/i.test(fileType)) {
continue;
}
const style = type ? handelParams(params) : '';
type = needBr ? `\n${type}` : type;
const insertValue = `${type}[${name}${style}](${url})${poster}`;
// 当批量上传文件时,每个被插入的文件中间需要加个换行,但单个上传文件的时候不需要加换行
needBr = true;
codemirror.replaceSelection(insertValue);
});
}
this.options.fileUpload(file, (url, params) => {
if (typeof url !== 'string') {
return;
}
// 拖拽上传文件时,强制改成没有文字选择区的状态
codemirror.setSelection(codemirror.getCursor());
const name = params.name ? params.name : file.name;
let type = '';
let poster = '';
if (/video/i.test(file.type)) {
type = '!video';
poster = params.poster ? `{poster=${params.poster}}` : '';
}
if (/audio/i.test(file.type)) {
type = '!audio';
}
if (/image/i.test(file.type)) {
type = '!';
}
const style = type ? handelParams(params) : '';
type = needBr ? `\n${type}` : type;
const insertValue = `${type}[${name}${style}](${url})${poster}`;
// 当批量上传文件时,每个被插入的文件中间需要加个换行,但单个上传文件的时候不需要加换行
needBr = true;
codemirror.replaceSelection(insertValue);
});
}
}, 50);
}
});

Expand Down

0 comments on commit e3296fb

Please sign in to comment.