Skip to content

Support dropping plain text and image files - #3429

Open
Julia Roldi (juliaroldi) wants to merge 4 commits into
masterfrom
u/juliaroldi/drop-external-text-image
Open

Support dropping plain text and image files#3429
Julia Roldi (juliaroldi) wants to merge 4 commits into
masterfrom
u/juliaroldi/drop-external-text-image

Conversation

@juliaroldi

@juliaroldi Julia Roldi (juliaroldi) commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Support dropping external plain text and single image files in the editor while preserving the existing HTML drop behavior and forbidden-element cleanup.

Extract plain-text DOM conversion into the shared textToFragment utility so paste and drag-and-drop use the same whitespace, tab, and multiline handling.

DragAndDropPlainText DragAndDropImage

How to test

  1. Run yarn test:fast --testPathPattern="textToFragment|DragAndDropPlugin|handleDroppedExternalContent".
  2. Drag plain text, HTML content, and a single image file into the editor and verify each is inserted at the drop position. Confirm forbidden HTML elements remain excluded.

@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://microsoft.github.io/roosterjs/pr-preview/pr-3429/

Built to branch gh-pages at 2026-08-04 21:57 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends RoosterJS Content Model drag-and-drop handling to support dropping external plain text and single image files, while preserving existing HTML drop behavior (including forbidden-element cleanup). It also extracts the plain-text-to-DOM conversion logic into a shared textToFragment utility so paste and drag-and-drop share identical whitespace/tab/multiline handling.

Changes:

  • Extend external drop handling to support text/plain drops and single-image file drops.
  • Extract plain-text conversion into textToFragment and reuse it from paste and drag-and-drop code paths.
  • Add/adjust unit tests to validate new drop behaviors and textToFragment conversion rules.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/roosterjs-content-model-plugins/lib/dragAndDrop/utils/handleDroppedExternalContent.ts Adds plain text + single image file drop support by producing a dropped content model from DataTransfer.
packages/roosterjs-content-model-plugins/lib/dragAndDrop/DragAndDropPlugin.ts Routes external drops through the updated handleDroppedExternalContent (no longer pre-filtered by HTML only).
packages/roosterjs-content-model-plugins/test/dragAndDrop/utils/handleDroppedExternalContentTest.ts Updates existing tests and adds coverage for plain text and image file drops.
packages/roosterjs-content-model-plugins/test/dragAndDrop/DragAndDropPluginTest.ts Updates plugin tests to reflect that external drop handler is called regardless of HTML presence.
packages/roosterjs-content-model-dom/lib/domUtils/textToFragment.ts Introduces shared plain-text-to-DocumentFragment conversion utility (whitespace/tab/multiline handling).
packages/roosterjs-content-model-dom/lib/index.ts Re-exports textToFragment from the DOM package barrel.
packages/roosterjs-content-model-dom/test/domUtils/textToFragmentTest.ts Adds unit tests for textToFragment conversion behavior.
packages/roosterjs-content-model-core/lib/command/paste/createPasteFragment.ts Reuses textToFragment for paste-as-plain-text fragment creation (removes duplicated logic).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +66 to +89
if (types.some(type => type === 'text/html')) {
const html = dataTransfer.getData('text/html');
if (html) {
const parsedHtml = editor.getDOMCreator().htmlToDOM(html);
cleanForbiddenElements(parsedHtml, forbiddenElements);
return domToContentModel(parsedHtml.body, createDomToModelContext());
}
} else if (types.some(type => type === 'text/plain')) {
const text = dataTransfer.getData('text/plain');
if (text) {
const textFragment = textToFragment(text, editor.getDocument());
return domToContentModel(textFragment, createDomToModelContext());
}
} else if (types.some(type => type === 'Files')) {
const files = dataTransfer.files;
const file = files?.length === 1 ? files[0] : undefined;
if (file?.type.startsWith('image/')) {
const model = createContentModelDocument();
const paragraph = createParagraph();
paragraph.segments.push(createImage(URL.createObjectURL(file)));
model.blocks.push(paragraph);
return model;
}
}
Comment on lines +85 to +86
paragraph.segments.push(createImage(URL.createObjectURL(file)));
model.blocks.push(paragraph);
return undefined;
}

if (types.some(type => type === 'text/html')) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, can we reuse the paste code to get model when drop?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants