This commit is contained in:
PavelPaliy 2024-05-03 11:30:25 +02:00 committed by GitHub
commit dee5149d19
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -109,6 +109,7 @@ interface PasteData {
* @version 2.0.0
*/
export default class Paste extends Module {
public pasteEvent;
/** If string`s length is greater than this number we don't check paste patterns */
public static readonly PATTERN_PROCESSING_MAX_LENGTH = 450;
@ -224,6 +225,30 @@ export default class Paste extends Module {
}
}
/**
* Check if elem has input class in parents html elements
*
* @param {HTMLElement} elem - pasted or dropped data transfer object
*/
public isDomElementInCdxInput(elem): boolean {
const { StylesAPI } = this.Editor;
const elements = document.querySelectorAll(`.${StylesAPI.classes.input}`);
try {
for (const i in elements) {
if (elements[i] && elements[i].contains(elem)) {
return true;
}
}
return false;
} catch (e) {
console.log(e);
return false;
}
}
/**
* Process pasted text and divide them into Blocks
*
@ -238,6 +263,29 @@ export default class Paste extends Module {
return;
}
if (this.pasteEvent && this.pasteEvent.target && this.isDomElementInCdxInput(this.pasteEvent.target)) {
const p = document.createElement('p');
p.innerText = '';
dataToInsert.forEach((item, index) => {
p.innerText = p.innerText + item.content.innerText;
if (index<dataToInsert.length-1) {
p.innerText = p.innerText + ' ';
}
});
const pasteDataObj = {
content:p,
isBlock:false,
tool: 'paragraph',
event:this.pasteEvent,
};
this.processInlinePaste(pasteDataObj);
return;
}
if (dataToInsert.length === 1) {
if (!dataToInsert[0].isBlock) {
this.processInlinePaste(dataToInsert.pop());
@ -478,7 +526,7 @@ export default class Paste extends Module {
*/
private handlePasteEvent = async (event: ClipboardEvent): Promise<void> => {
const { BlockManager, Toolbar } = this.Editor;
this.pasteEvent = event;
/**
* When someone pasting into a block, its more stable to set current block by event target, instead of relying on current block set before
*/