-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Excel non-native paste event handling and related utilities (…
…#2950) * Implement Excel non-native event handling and related utilities * Update import statement for BeforePasteEvent and ClipboardData types * Fix typo in handleExcelContentFromNotNativeEventTest for handleForNativeEvent parameter * Remove deprecated Excel non-native event handling and related tests; add new HTML template for non-native Excel content * Fix tests in pipeline --------- Co-authored-by: Jiuqing Song <jisong@microsoft.com>
- Loading branch information
1 parent
3ddc5b8
commit 7beec85
Showing
18 changed files
with
326 additions
and
105 deletions.
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
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
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: {}, | ||
}, | ||
}; | ||
} |
119 changes: 119 additions & 0 deletions
119
packages/roosterjs-content-model-plugins/test/paste/e2e/cmPasteFromExcelNonNative.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,119 @@ | ||
import * as processPastedContentFromExcel from '../../../lib/paste/Excel/processPastedContentFromExcel'; | ||
import { excelContentFromNonNativeTemplate } from './htmlTemplates/htmlFromExcelNonNative'; | ||
import { expectEqual, initEditor } from './testUtils'; | ||
import { paste } from 'roosterjs-content-model-core'; | ||
import type { ClipboardData, IEditor } from 'roosterjs-content-model-types'; | ||
|
||
describe('Paste from Excel non native', () => { | ||
let editor: IEditor = undefined!; | ||
|
||
beforeEach(() => { | ||
editor = initEditor('Paste_from_Excel_non_native'); | ||
}); | ||
|
||
afterEach(() => { | ||
document.getElementById('Paste_from_Excel_non_native')?.remove(); | ||
}); | ||
|
||
it('E2E', () => { | ||
const clipboardData = { | ||
types: ['web data/shadow-workbook', 'image/png', 'text/plain', 'text/html'], | ||
text: '\r\n', | ||
image: {}, | ||
files: [{}], | ||
rawHtml: excelContentFromNonNativeTemplate, | ||
customValues: {}, | ||
pasteNativeEvent: true, | ||
imageDataUri: null, | ||
} as ClipboardData; | ||
spyOn(processPastedContentFromExcel, 'processPastedContentFromExcel').and.callThrough(); | ||
|
||
paste(editor, clipboardData); | ||
const model = editor.getContentModelCopy('connected'); | ||
|
||
expect(processPastedContentFromExcel.processPastedContentFromExcel).toHaveBeenCalled(); | ||
|
||
expectEqual(model, { | ||
blockGroupType: 'Document', | ||
blocks: [ | ||
{ | ||
widths: jasmine.anything() as any, | ||
rows: [ | ||
{ | ||
height: jasmine.anything() as any, | ||
cells: [ | ||
{ | ||
spanAbove: false, | ||
spanLeft: false, | ||
isHeader: false, | ||
blockGroupType: 'TableCell', | ||
blocks: [], | ||
format: { | ||
borderTop: '1px solid rgb(212, 212, 212)', | ||
borderRight: '1px solid rgb(212, 212, 212)', | ||
borderBottom: '1px solid rgb(212, 212, 212)', | ||
borderLeft: '1px solid rgb(212, 212, 212)', | ||
width: '50pt', | ||
height: '14.4pt', | ||
}, | ||
dataset: {}, | ||
}, | ||
{ | ||
spanAbove: false, | ||
spanLeft: false, | ||
isHeader: false, | ||
blockGroupType: 'TableCell', | ||
blocks: [], | ||
format: { | ||
borderTop: '1px solid rgb(212, 212, 212)', | ||
borderRight: '1px solid rgb(212, 212, 212)', | ||
borderBottom: '1px solid rgb(212, 212, 212)', | ||
borderLeft: '1px solid rgb(212, 212, 212)', | ||
width: '50pt', | ||
}, | ||
dataset: {}, | ||
}, | ||
], | ||
format: {}, | ||
}, | ||
], | ||
blockType: 'Table', | ||
format: { | ||
width: '100pt', | ||
useBorderBox: true, | ||
borderCollapse: true, | ||
}, | ||
dataset: {}, | ||
}, | ||
{ | ||
segments: [ | ||
{ | ||
isSelected: true, | ||
segmentType: 'SelectionMarker', | ||
format: { | ||
backgroundColor: '', | ||
fontFamily: '', | ||
fontSize: '', | ||
fontWeight: '', | ||
italic: false, | ||
letterSpacing: '', | ||
lineHeight: '', | ||
strikethrough: false, | ||
superOrSubScriptSequence: '', | ||
textColor: '', | ||
underline: false, | ||
}, | ||
}, | ||
{ | ||
segmentType: 'Br', | ||
format: {}, | ||
}, | ||
], | ||
blockType: 'Paragraph', | ||
format: {}, | ||
}, | ||
], | ||
format: {}, | ||
}); | ||
}); | ||
}); |
5 changes: 5 additions & 0 deletions
5
...es/roosterjs-content-model-plugins/test/paste/e2e/htmlTemplates/htmlFromExcelNonNative.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,5 @@ | ||
export const excelContentFromNonNativeTemplate = `<table border="0" cellpadding="0" cellspacing="0" width="134" style="border-collapse: | ||
collapse;width:100pt"><tbody><tr height="19" style="height:14.4pt"> | ||
<td height="19" width="67" style="height:14.4pt;width:50pt"></td> | ||
<td width="67" style="width:50pt"></td> | ||
</tr></tbody></table>`; |
Oops, something went wrong.