Skip to content

Commit

Permalink
fix: #673 closed 修复粘贴excel只有图片的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsonliu committed Dec 25, 2023
1 parent 39e93d7 commit 9f46d5e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
21 changes: 10 additions & 11 deletions src/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ export default class Editor {
* @returns {boolean | void}
*/
handlePaste(event, clipboardData, codemirror) {
this.pasterHtml = false;
const { items } = clipboardData;
const types = clipboardData.types || [];
const codemirrorDoc = codemirror.getDoc();
Expand All @@ -279,7 +280,13 @@ export default class Editor {
return;
}
const mdStr = handleFileUploadCallback(url, params, file);
codemirrorDoc.replaceSelection(mdStr);
if (this.pasterHtml) {
const { line, ch } = codemirror.getCursor();
codemirror.setSelection({ line, ch }, { line, ch });
codemirrorDoc.replaceSelection(`\n${mdStr}`, 'end');
} else {
codemirrorDoc.replaceSelection(mdStr);
}
});
event.preventDefault();
}
Expand All @@ -291,21 +298,13 @@ export default class Editor {
if (!html || !this.options.convertWhenPaste) {
return true;
}
/**
* 这里需要处理一个特殊逻辑:
* 从excel中复制而来的内容,剪切板里会有一张图片(一个<img>元素)和一段纯文本,在这种场景下,需要丢掉图片,直接粘贴纯文本
* 与此同时,当剪切板里有图片和其他html标签时(从web页面上复制的内容),则需要走下面的html转md的逻辑
* 基于上述两个场景,才有了下面四行奇葩的代码
*/
const test = html.replace(/<(html|head|body|!)/g, '');
if (test.match(/<[a-zA-Z]/g)?.length <= 1 && /<img/.test(test)) {
return true;
}

let divObj = document.createElement('DIV');
divObj.innerHTML = html;
html = divObj.innerHTML;
const mdText = htmlParser.run(html);
if (typeof mdText === 'string' && mdText.trim().length > 0) {
this.pasterHtml = true;
const range = codemirror.listSelections();
if (codemirror.getSelections().length <= 1 && range[0] && range[0].anchor) {
const currentCursor = {};
Expand Down
35 changes: 26 additions & 9 deletions src/utils/htmlparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ const htmlParser = {
// 递归每一个子元素
tmpText = self.$dealHtml(obj.children);
}
if (obj.name === 'style') {
// 不解析样式属性,只处理行内样式
if (/(style|meta|link|script)/.test(obj.name)) {
return '';
}
if (obj.name === 'code' || obj.name === 'pre') {
Expand Down Expand Up @@ -791,23 +790,41 @@ const htmlParser = {
return `^^${str.trim().replace(/\^\^/g, '\\^\\^')}^^`;
},
convertTd(str) {
return `~|${str.trim().replace(/\n{1,}/g, '<br>')} ~|`;
return `~|${str
.trim()
.replace(/\n{1,}/g, '<br>')
.replace(/ /g, '~s~')} ~|`;
},
convertTh(str) {
if (/^\s*$/.test(str)) {
return '';
}
return `~|${str.trim().replace(/\n{1,}/g, '<br>')} ~|`;
},
convertTr(str) {
return `${str.replace(/\n/g, '')}\n`;
if (/^\s*$/.test(str)) {
return '';
}
return `${str.trim().replace(/\n/g, '')}\n`;
},
convertThead(str) {
return `${str.replace(/~\|~\|/g, '~|').replace(/~\|/g, '|')}|:--|\n`;
const $str = `${str
.trim()
.replace(/~\|[ \t]*~\|/g, '~|')
.replace(/~\|/g, '|')}\n`;
const headsCount = $str.match(/\|/g).length - 1;
return `${$str}|${':-:|'.repeat(headsCount)}\n`;
},
convertTable(str) {
const ret = `\n${str.replace(/~\|~\|/g, '~|').replace(/~\|/g, '|')}\n`.replace(/\n{2,}/g, '\n');
if (/\|:--\|/.test(ret)) {
return ret;
const $str = str.replace(/^\s+/gm, '').replace(/~s~/g, ' ');
let ret = `\n${$str.replace(/~\|[ \t]*~\|/g, '~|').replace(/~\|/g, '|')}\n`
.replace(/\n{2,}/g, '\n')
.replace(/\n[ \t]+\n/g, '\n');
if (!/\|:-:\|/.test(ret)) {
const headsCount = ret.match(/^\n[^\n]+\n/)[0].match(/\|/g).length - 1;
ret = `\n|${' |'.repeat(headsCount)}\n|${':-:|'.repeat(headsCount)}${ret}`;
}
return `\n| |\n|:--|${ret}`;
return ret;
},
convertLi(str) {
return `- ${str.replace(/^\n/, '').replace(/\n+$/, '').replace(/\n+/g, '\n\t')}\n`;
Expand Down

0 comments on commit 9f46d5e

Please sign in to comment.