Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix [Bug] #1427 #2635

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/components/modules/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
*
Expand All @@ -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());
Expand Down Expand Up @@ -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
*/
Expand Down