Move getDocumentSource to core and expose pasteSource on BeforePasteEvent#3382
Move getDocumentSource to core and expose pasteSource on BeforePasteEvent#3382BryanValverdeU wants to merge 2 commits into
Conversation
…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>
|
There was a problem hiding this comment.
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
KnownPasteSourceTypetoroosterjs-content-model-types, exported it, and extendedBeforePasteEvent+PasteTypeOrGetterto carry the detected paste source. - Updated core paste flow to compute paste source once (
getPasteSourceinpaste.ts) and pass it togeneratePasteOptionFromPluginsand paste-type getters. - Updated plugin + test code to consume
event.pasteSourceand 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.
|
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 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:
And allow things to be overwritten from the PastePlugin. |
Summary
Moves paste-source detection out of
roosterjs-content-model-pluginsand intoroosterjs-content-model-core, and exposes the detected source onBeforePasteEventso 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
pasteSourceValidationsfolder (getDocumentSource, all validators,constants) and their tests fromroosterjs-content-model-pluginstoroosterjs-content-model-core(core cannot depend on plugins).KnownPasteSourceTypetoroosterjs-content-model-typesand export it.readonly pasteSourceproperty toBeforePasteEventand apasteSourceargument to thePasteTypeOrGettercallback.paste.tsnow computes the paste source (newgetPasteSourcehelper) and passes it to the paste-type getter and togeneratePasteOptionFromPlugins, which sets it on the event.PastePluginconsumesevent.pasteSourceinstead of callinggetDocumentSource; uses a localGOOGLE_SHEET_NODE_NAMEconstant.documentContainWacElementsself-contained and drop now-unused WAC selector constants fromWacComponents/constants.ts.event.pasteSourceand repoint spies to the core module.Validation
yarn eslint- passyarn test:fast- 5890 passing (33 skipped)yarn build- passNotes
BeforePasteEvent.pasteSourceis optional to match existing optional props and avoid breaking event mocks; core always populates it.