-
Notifications
You must be signed in to change notification settings - Fork 169
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 event handling and related utilities
- Loading branch information
1 parent
6388b93
commit c5337af
Showing
19 changed files
with
233 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
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 { BeforePasteEvent, ClipboardData } from 'roosterjs'; | ||
|
||
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 /* handleForNativeEvemt */ | ||
); | ||
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.