-
Notifications
You must be signed in to change notification settings - Fork 169
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
Implement Excel non-native paste event handling and related utilities #2950
Merged
BryanValverdeU
merged 11 commits into
master
from
u/bvalverde/fixExcelPasteWithProgramaticRetrievalOfClipboard
Feb 20, 2025
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c5337af
Implement Excel non-native event handling and related utilities
BryanValverdeU 4a00cd5
Merge branch 'master' into u/bvalverde/fixExcelPasteWithProgramaticRe…
BryanValverdeU f970dbd
Update import statement for BeforePasteEvent and ClipboardData types
BryanValverdeU 0e6b36d
Merge branch 'u/bvalverde/fixExcelPasteWithProgramaticRetrievalOfClip…
BryanValverdeU 828d8ee
Fix typo in handleExcelContentFromNotNativeEventTest for handleForNat…
BryanValverdeU 5ab9eb5
Merge branch 'master' into u/bvalverde/fixExcelPasteWithProgramaticRe…
JiuqingSong f8f1bc5
Remove deprecated Excel non-native event handling and related tests; …
BryanValverdeU bd08ec5
Merge branch 'u/bvalverde/fixExcelPasteWithProgramaticRetrievalOfClip…
BryanValverdeU 50d81c6
Fix tests in pipeline
BryanValverdeU d80354e
Merge branch 'master' into u/bvalverde/fixExcelPasteWithProgramaticRe…
BryanValverdeU c7abc51
Merge branch 'master' into u/bvalverde/fixExcelPasteWithProgramaticRe…
JiuqingSong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...s/roosterjs-content-model-plugins/lib/paste/Excel/handleExcelContentFromNotNativeEvent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { setupExcelTableHandlers } from './setupExcelTableHandlers'; | ||
import type { BeforePasteEvent } from 'roosterjs-content-model-types'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function handleExcelContentFromNotNativeEvent( | ||
event: BeforePasteEvent, | ||
allowExcelNoBorderTable: boolean | ||
) { | ||
setupExcelTableHandlers(event, allowExcelNoBorderTable, false /* handleForNativeEvent */); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/roosterjs-content-model-plugins/lib/paste/Excel/setupExcelTableHandlers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { addParser } from '../utils/addParser'; | ||
import { setProcessor } from '../utils/setProcessor'; | ||
import type { BeforePasteEvent, ElementProcessor } from 'roosterjs-content-model-types'; | ||
|
||
const DEFAULT_BORDER_STYLE = 'solid 1px #d4d4d4'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function setupExcelTableHandlers( | ||
event: BeforePasteEvent, | ||
allowExcelNoBorderTable: boolean | undefined, | ||
handleForNativeEvent: boolean | ||
) { | ||
addParser(event.domToModelOption, 'tableCell', (format, element) => { | ||
if ( | ||
!allowExcelNoBorderTable && | ||
(element.style.borderStyle === 'none' || | ||
(!handleForNativeEvent && element.style.borderStyle == '')) | ||
) { | ||
format.borderBottom = DEFAULT_BORDER_STYLE; | ||
format.borderLeft = DEFAULT_BORDER_STYLE; | ||
format.borderRight = DEFAULT_BORDER_STYLE; | ||
format.borderTop = DEFAULT_BORDER_STYLE; | ||
} | ||
}); | ||
|
||
setProcessor(event.domToModelOption, 'child', childProcessor); | ||
} | ||
|
||
/** | ||
* @internal | ||
* Exported only for unit test | ||
*/ | ||
export const childProcessor: ElementProcessor<ParentNode> = (group, element, context) => { | ||
const segmentFormat = { ...context.segmentFormat }; | ||
if ( | ||
group.blockGroupType === 'TableCell' && | ||
group.format.textColor && | ||
!context.segmentFormat.textColor | ||
) { | ||
context.segmentFormat.textColor = group.format.textColor; | ||
} | ||
|
||
context.defaultElementProcessors.child(group, element, context); | ||
|
||
if (group.blockGroupType === 'TableCell' && group.format.textColor) { | ||
context.segmentFormat = segmentFormat; | ||
delete group.format.textColor; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...roosterjs-content-model-plugins/lib/paste/pasteSourceValidations/isExcelNonNativeEvent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { GetSourceFunction, GetSourceInputParams } from './getPasteSource'; | ||
|
||
const ShadowWorkbookClipboardType = 'web data/shadow-workbook'; | ||
|
||
/** | ||
* @internal | ||
* When the clipboard content is retrieved programatically, the clipboard html does not contain the usual | ||
* attributes we use to determine if the content is from Excel. This function is used to handle that case. | ||
*/ | ||
export const isExcelNotNativeEvent: GetSourceFunction = (props: GetSourceInputParams) => { | ||
const { clipboardData } = props; | ||
|
||
return ( | ||
clipboardData.types.includes(ShadowWorkbookClipboardType) && | ||
clipboardData.htmlFirstLevelChildTags?.length == 1 && | ||
clipboardData.htmlFirstLevelChildTags[0] == 'TABLE' | ||
); | ||
}; |
25 changes: 25 additions & 0 deletions
25
packages/roosterjs-content-model-plugins/test/paste/createBeforePasteEventMock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { BeforePasteEvent, ClipboardData } from 'roosterjs-content-model-types'; | ||
|
||
export function createBeforePasteEventMock( | ||
fragment: DocumentFragment, | ||
htmlBefore: string = '' | ||
): BeforePasteEvent { | ||
return { | ||
eventType: 'beforePaste', | ||
clipboardData: <ClipboardData>{}, | ||
fragment: fragment, | ||
htmlBefore, | ||
htmlAfter: '', | ||
htmlAttributes: {}, | ||
pasteType: 'normal', | ||
domToModelOption: { | ||
additionalAllowedTags: [], | ||
additionalDisallowedTags: [], | ||
additionalFormatParsers: {}, | ||
attributeSanitizers: {}, | ||
formatParserOverride: {}, | ||
processorOverride: {}, | ||
styleSanitizers: {}, | ||
}, | ||
}; | ||
} |
38 changes: 38 additions & 0 deletions
38
...sterjs-content-model-plugins/test/paste/excel/handleExcelContentFromNotNativeEventTest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import * as addParserFile from '../../../lib/paste/utils/addParser'; | ||
import * as setProcessorFile from '../../../lib/paste/utils/setProcessor'; | ||
import * as setupExcelTableHandlersFile from '../../../lib/paste/Excel/setupExcelTableHandlers'; | ||
import { BeforePasteEvent } from 'roosterjs-content-model-types'; | ||
import { handleExcelContentFromNotNativeEvent } from '../../../lib/paste/Excel/handleExcelContentFromNotNativeEvent'; | ||
|
||
describe('handleExcelContentFromNotNativeEvent', () => { | ||
beforeEach(() => { | ||
spyOn(setupExcelTableHandlersFile, 'setupExcelTableHandlers').and.callThrough(); | ||
spyOn(addParserFile, 'addParser').and.callFake(() => {}); | ||
spyOn(setProcessorFile, 'setProcessor').and.callFake(() => {}); | ||
}); | ||
|
||
it('should handle Excel content from non-native event', () => { | ||
const fragment = document.createDocumentFragment(); | ||
const table = document.createElement('table'); | ||
fragment.appendChild(table); | ||
|
||
const event: BeforePasteEvent = { | ||
fragment, | ||
clipboardData: { | ||
types: ['web data/shadow-workbook'], | ||
} as any, | ||
} as any; | ||
|
||
const allowExcelNoBorderTable = true; | ||
|
||
handleExcelContentFromNotNativeEvent(event, allowExcelNoBorderTable); | ||
|
||
expect(setupExcelTableHandlersFile.setupExcelTableHandlers).toHaveBeenCalledWith( | ||
event, | ||
allowExcelNoBorderTable, | ||
false /* handleForNativeEvent */ | ||
); | ||
expect(setProcessorFile.setProcessor).toHaveBeenCalledTimes(1); | ||
expect(addParserFile.addParser).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...s/test/paste/validateExcelFragmentTest.ts → .../paste/excel/validateExcelFragmentTest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this exist in all cases when copy from Excel? If so, do we still need to keep 'excelDesktop'? Maybe we just need to replace 'excelDesktop' and always use the new one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the shadow workbook type exists when copying from Excel. Should be available from ctrl + v and programatically pasting
The main difference is that when pasting wIth Ctrl + V we need the current approach, (clipboard fragment does not have a table element, so we do an special case).
But when getting the clipboard programatically, the fragment does contain a table but we need to add borders to the table.
Another option is to merge both logic in excel handling, but if fragment contains a table we do not need to build the table from the rawHtml
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, then let's keep the change in this PR to keep it simple.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, just modified the logic. Now we just keep all the excel handling in a single block of the switch. I think it looks cleaner. Let me know if it is okay