Skip to content

Move getDocumentSource to core and expose pasteSource on BeforePasteEvent#3382

Closed
BryanValverdeU wants to merge 2 commits into
masterfrom
u/bvalverde/movePasteSourceToCorePackage
Closed

Move getDocumentSource to core and expose pasteSource on BeforePasteEvent#3382
BryanValverdeU wants to merge 2 commits into
masterfrom
u/bvalverde/movePasteSourceToCorePackage

Conversation

@BryanValverdeU

@BryanValverdeU BryanValverdeU commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Summary

Moves paste-source detection out of roosterjs-content-model-plugins and into roosterjs-content-model-core, and exposes the detected source on BeforePasteEvent so plugins consume it from the event rather than recomputing it. Additionally add an extra param to pasteTypeGetter, so we can consume the paste source and change the paste type if the contents are comming from a O365 document.

Changes

  • Move the pasteSourceValidations folder (getDocumentSource, all validators, constants) and their tests from roosterjs-content-model-plugins to roosterjs-content-model-core (core cannot depend on plugins).
  • Add KnownPasteSourceType to roosterjs-content-model-types and export it.
  • Add a readonly pasteSource property to BeforePasteEvent and a pasteSource argument to the PasteTypeOrGetter callback.
  • paste.ts now computes the paste source (new getPasteSource helper) and passes it to the paste-type getter and to generatePasteOptionFromPlugins, which sets it on the event.
  • PastePlugin consumes event.pasteSource instead of calling getDocumentSource; uses a local GOOGLE_SHEET_NODE_NAME constant.
  • Make documentContainWacElements self-contained and drop now-unused WAC selector constants from WacComponents/constants.ts.
  • Update tests to set event.pasteSource and repoint spies to the core module.

Validation

  • yarn eslint - pass
  • yarn test:fast - 5890 passing (33 skipped)
  • yarn build - pass

Notes

  • Adding the getter's 3rd param is not a breaking change: in TS a function with fewer params is assignable to a type expecting more, so existing 2-arg getters still compile.
  • BeforePasteEvent.pasteSource is optional to match existing optional props and avoid breaking event mocks; core always populates it.

…asteEvent

- Move pasteSourceValidations (getDocumentSource, validators, constants) and
  their tests from roosterjs-content-model-plugins to roosterjs-content-model-core
  (core cannot depend on plugins).
- Add KnownPasteSourceType to roosterjs-content-model-types and export it.
- Add readonly pasteSource to BeforePasteEvent and a pasteSource arg to the
  PasteTypeOrGetter callback.
- paste.ts now computes the paste source (getPasteSource helper) and passes it to
  the paste-type getter and to generatePasteOptionFromPlugins, which sets it on the event.
- PastePlugin consumes event.pasteSource instead of calling getDocumentSource;
  uses a local GOOGLE_SHEET_NODE_NAME constant.
- Make documentContainWacElements self-contained and drop now-unused WAC selector
  constants from WacComponents/constants.ts.
- Update tests: set event.pasteSource instead of spying getDocumentSource, repoint
  e2e/pasteTest spies to the core module, and update arg-shape assertions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions

github-actions Bot commented Jun 25, 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-3382/

Built to branch gh-pages at 2026-06-25 23: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 relocates paste-source detection (previously in roosterjs-content-model-plugins) into roosterjs-content-model-core and threads the detected source through the paste pipeline by exposing it on BeforePasteEvent and passing it to the paste-type getter callback. This aligns with the package dependency constraints (core must not depend on plugins) while avoiding repeated source detection in plugins.

Changes:

  • Added KnownPasteSourceType to roosterjs-content-model-types, exported it, and extended BeforePasteEvent + PasteTypeOrGetter to carry the detected paste source.
  • Updated core paste flow to compute paste source once (getPasteSource in paste.ts) and pass it to generatePasteOptionFromPlugins and paste-type getters.
  • Updated plugin + test code to consume event.pasteSource and moved/updated paste-source validation utilities and tests accordingly.

Reviewed changes

Copilot reviewed 25 out of 35 changed files in this pull request and generated no comments.

Show a summary per file
File Description
packages/roosterjs-content-model-types/lib/parameter/PasteTypeOrGetter.ts Extends paste-type getter signature to include detected paste source.
packages/roosterjs-content-model-types/lib/index.ts Re-exports KnownPasteSourceType.
packages/roosterjs-content-model-types/lib/event/BeforePasteEvent.ts Adds optional pasteSource field to the beforePaste event contract.
packages/roosterjs-content-model-types/lib/enum/KnownPasteSourceType.ts Defines the shared union type for known paste sources.
packages/roosterjs-content-model-plugins/test/paste/plugin/ContentModelPastePluginTest.ts Updates unit tests to set event.pasteSource instead of spying on source detection.
packages/roosterjs-content-model-plugins/test/paste/e2e/cmPasteFromPowerPointDesktopTest.ts Repoints getDocumentSource import to core location.
packages/roosterjs-content-model-plugins/test/paste/e2e/cmPasteFromOneNoteTest.ts Repoints getDocumentSource import to core location.
packages/roosterjs-content-model-plugins/lib/paste/WacComponents/constants.ts Removes WAC selector constants no longer needed after moving WAC detection logic.
packages/roosterjs-content-model-plugins/lib/paste/pasteSourceValidations/documentContainWacElements.ts Removes plugin-side WAC detection (moved to core).
packages/roosterjs-content-model-plugins/lib/paste/PastePlugin.ts Switches to using event.pasteSource and local Google Sheets tag constant.
packages/roosterjs-content-model-core/test/command/paste/pasteTest.ts Updates paste tests for the new pasteSource plumbing.
packages/roosterjs-content-model-core/test/command/paste/pasteSourceValidations/shouldConvertToSingleImageTest.ts Moves validator test imports to core command path.
packages/roosterjs-content-model-core/test/command/paste/pasteSourceValidations/pasteTestUtils.ts Adds shared constants/helpers for paste-source validation tests.
packages/roosterjs-content-model-core/test/command/paste/pasteSourceValidations/isWordDesktopDocumentTest.ts Updates test imports to core command path.
packages/roosterjs-content-model-core/test/command/paste/pasteSourceValidations/isPowerPointDesktopDocumentTest.ts Updates test imports to core command path.
packages/roosterjs-content-model-core/test/command/paste/pasteSourceValidations/isOneNoteDocumentTest.ts Updates test imports to core command path.
packages/roosterjs-content-model-core/test/command/paste/pasteSourceValidations/isGoogleSheetDocumentTest.ts Updates test imports to core command path.
packages/roosterjs-content-model-core/test/command/paste/pasteSourceValidations/isExcelOnlineDocumentTest.ts Updates test imports to core command path.
packages/roosterjs-content-model-core/test/command/paste/pasteSourceValidations/isExcelDesktopDocumentTest.ts Updates test imports to core command path.
packages/roosterjs-content-model-core/test/command/paste/pasteSourceValidations/getPasteSourceTest.ts Updates test imports to core command path.
packages/roosterjs-content-model-core/test/command/paste/pasteSourceValidations/documentContainWacElementsTest.ts Updates test imports to core command path.
packages/roosterjs-content-model-core/test/command/paste/generatePasteOptionFromPluginsTest.ts Updates tests to expect pasteSource on the event and new function signature.
packages/roosterjs-content-model-core/lib/command/paste/pasteSourceValidations/shouldConvertToSingleImage.ts Adds core-side validator for single-image detection.
packages/roosterjs-content-model-core/lib/command/paste/pasteSourceValidations/isWordDesktopDocument.ts Adds core-side Word Desktop detection.
packages/roosterjs-content-model-core/lib/command/paste/pasteSourceValidations/isPowerPointDesktopDocument.ts Adds core-side PowerPoint Desktop detection.
packages/roosterjs-content-model-core/lib/command/paste/pasteSourceValidations/isOneNoteDocument.ts Adds core-side OneNote Desktop detection.
packages/roosterjs-content-model-core/lib/command/paste/pasteSourceValidations/isGoogleSheetDocument.ts Adds core-side Google Sheets detection.
packages/roosterjs-content-model-core/lib/command/paste/pasteSourceValidations/isExcelOnlineDocument.ts Adds core-side Excel Online detection.
packages/roosterjs-content-model-core/lib/command/paste/pasteSourceValidations/isExcelNonNativeEvent.ts Adds core-side “Excel non-native event” detection.
packages/roosterjs-content-model-core/lib/command/paste/pasteSourceValidations/isExcelDesktopDocument.ts Adds core-side Excel Desktop detection.
packages/roosterjs-content-model-core/lib/command/paste/pasteSourceValidations/getDocumentSource.ts Hosts the moved detection orchestrator and updates shared types to use KnownPasteSourceType.
packages/roosterjs-content-model-core/lib/command/paste/pasteSourceValidations/documentContainWacElements.ts Adds core-side WAC detection logic (now self-contained).
packages/roosterjs-content-model-core/lib/command/paste/pasteSourceValidations/constants.ts Adds core-side constants used by paste-source validators.
packages/roosterjs-content-model-core/lib/command/paste/paste.ts Computes paste source once and threads it through paste-type getter and beforePaste event.
packages/roosterjs-content-model-core/lib/command/paste/generatePasteOptionFromPlugins.ts Adds pasteSource to the beforePaste event payload and new function signature.

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

@juliaroldi

Copy link
Copy Markdown
Contributor

Can we not add all this code to the roosterjs core? Maybe we can create an option in EditorOptions to pass a callback to detect the paste source? Otherwise, every time we need to detect a paste source we will need to change the roosterJs core code.

@JiuqingSong

Copy link
Copy Markdown
Collaborator

Can we not add all this code to the roosterjs core? Maybe we can create an option in EditorOptions to pass a callback to detect the paste source? Otherwise, every time we need to detect a paste source we will need to change the roosterJs core code.

I have the same feeling with @juliaroldi . Ideally this kind of code should be in plugin package. And actually the API paste itself, should be in api package. But just because the build-in paste feature also need to trigger the BeforePaste event and do proper handling, I need to put it in core package. (Now I feel at least some of the functionality can be moved out such as re-paste hanlding)

Any special reason we need to move the code into core? If we want the API paste to be smart enough, we can split it into two parts:

  1. Basic paste, only handle Ctrl+V and context menu, trigger BeforePaste event with most common options, then do basic content merge
  2. Customized paste (can be in API package or plugin package), do more advanced handling, such as repaste (change paste format), specify paste format (html, merge format, plain text, markdown, ...)

And allow things to be overwritten from the PastePlugin.

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.

4 participants