-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Standalone editor: Remove dependencies to enums Part 2 (#2153)
* Standalone editor: Remove dependencies to enums Part 2 * fix build
- Loading branch information
1 parent
e55b4d6
commit 88bdecf
Showing
41 changed files
with
903 additions
and
134 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
3 changes: 2 additions & 1 deletion
3
...odel/roosterjs-content-model-editor/lib/editor/corePlugins/ContentModelCopyPastePlugin.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
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
19 changes: 19 additions & 0 deletions
19
...s-content-model-editor/lib/editor/plugins/PastePlugin/pasteSourceValidations/constants.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,19 @@ | ||
/** | ||
* @internal | ||
*/ | ||
export const enum PastePropertyNames { | ||
/** | ||
* Node attribute used to identify if the content is from Google Sheets. | ||
*/ | ||
GOOGLE_SHEET_NODE_NAME = 'google-sheets-html-origin', | ||
|
||
/** | ||
* Name of the HTMLMeta Property that provides the Office App Source of the pasted content | ||
*/ | ||
PROG_ID_NAME = 'ProgId', | ||
|
||
/** | ||
* Name of the HTMLMeta Property that identifies pated content as from Excel Desktop | ||
*/ | ||
EXCEL_DESKTOP_ATTRIBUTE_NAME = 'xmlns:x', | ||
} |
25 changes: 25 additions & 0 deletions
25
...ditor/lib/editor/plugins/PastePlugin/pasteSourceValidations/documentContainWacElements.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 { GetSourceFunction } from './getPasteSource'; | ||
|
||
const WORD_ONLINE_TABLE_TEMP_ELEMENT_CLASSES = [ | ||
'TableInsertRowGapBlank', | ||
'TableColumnResizeHandle', | ||
'TableCellTopBorderHandle', | ||
'TableCellLeftBorderHandle', | ||
'TableHoverColumnHandle', | ||
'TableHoverRowHandle', | ||
]; | ||
|
||
const WAC_IDENTIFY_SELECTOR = | ||
'ul[class^="BulletListStyle"]>.OutlineElement,ol[class^="NumberListStyle"]>.OutlineElement,span.WACImageContainer,' + | ||
WORD_ONLINE_TABLE_TEMP_ELEMENT_CLASSES.map(c => `table div[class^="${c}"]`).join(','); | ||
|
||
/** | ||
* @internal | ||
* Check whether the fragment provided contain Wac Elements | ||
* @param props Properties related to the PasteEvent | ||
* @returns | ||
*/ | ||
export const documentContainWacElements: GetSourceFunction = props => { | ||
const { fragment } = props; | ||
return !!fragment.querySelector(WAC_IDENTIFY_SELECTOR); | ||
}; |
77 changes: 77 additions & 0 deletions
77
...tent-model-editor/lib/editor/plugins/PastePlugin/pasteSourceValidations/getPasteSource.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,77 @@ | ||
import { documentContainWacElements } from './documentContainWacElements'; | ||
import { isExcelDesktopDocument } from './isExcelDesktopDocument'; | ||
import { isExcelOnlineDocument } from './isExcelOnlineDocument'; | ||
import { isGoogleSheetDocument } from './isGoogleSheetDocument'; | ||
import { isPowerPointDesktopDocument } from './isPowerPointDesktopDocument'; | ||
import { isWordDesktopDocument } from './isWordDesktopDocument'; | ||
import { shouldConvertToSingleImage } from './shouldConvertToSingleImage'; | ||
import type { BeforePasteEvent, ClipboardData } from 'roosterjs-editor-types'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export type GetSourceInputParams = { | ||
htmlAttributes: Record<string, string>; | ||
fragment: DocumentFragment; | ||
shouldConvertSingleImage: boolean; | ||
clipboardData: ClipboardData; | ||
}; | ||
|
||
/** | ||
* @internal | ||
* Represent the types of sources to handle in the Paste Plugin | ||
*/ | ||
export type KnownPasteSourceType = | ||
| 'wordDesktop' | ||
| 'excelDesktop' | ||
| 'excelOnline' | ||
| 'powerPointDesktop' | ||
| 'googleSheets' | ||
| 'wacComponents' | ||
| 'default' | ||
| 'singleImage'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export type GetSourceFunction = (props: GetSourceInputParams) => boolean; | ||
|
||
const getSourceFunctions = new Map<KnownPasteSourceType, GetSourceFunction>([ | ||
['wordDesktop', isWordDesktopDocument], | ||
['excelDesktop', isExcelDesktopDocument], | ||
['excelOnline', isExcelOnlineDocument], | ||
['powerPointDesktop', isPowerPointDesktopDocument], | ||
['wacComponents', documentContainWacElements], | ||
['googleSheets', isGoogleSheetDocument], | ||
['singleImage', shouldConvertToSingleImage], | ||
]); | ||
|
||
/** | ||
* @internal | ||
* This function tries to get the source of the Pasted content | ||
* @param event the before paste event | ||
* @param shouldConvertSingleImage Whether convert single image is enabled. | ||
* @returns The Type of pasted content, if no type found will return {KnownSourceType.Default} | ||
*/ | ||
export function getPasteSource( | ||
event: BeforePasteEvent, | ||
shouldConvertSingleImage: boolean | ||
): KnownPasteSourceType { | ||
const { htmlAttributes, clipboardData, fragment } = event; | ||
|
||
let result: KnownPasteSourceType | null = null; | ||
const param: GetSourceInputParams = { | ||
htmlAttributes, | ||
fragment, | ||
shouldConvertSingleImage, | ||
clipboardData, | ||
}; | ||
|
||
getSourceFunctions.forEach((func, key) => { | ||
if (!result && func(param)) { | ||
result = key; | ||
} | ||
}); | ||
|
||
return result ?? 'default'; | ||
} |
16 changes: 16 additions & 0 deletions
16
...el-editor/lib/editor/plugins/PastePlugin/pasteSourceValidations/isExcelDesktopDocument.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,16 @@ | ||
import { PastePropertyNames } from './constants'; | ||
import type { GetSourceFunction } from './getPasteSource'; | ||
|
||
const EXCEL_ATTRIBUTE_VALUE = 'urn:schemas-microsoft-com:office:excel'; | ||
|
||
/** | ||
* @internal | ||
* Checks whether the Array provided contains strings that identify Excel Desktop documents | ||
* @param props Properties related to the PasteEvent | ||
* @returns | ||
*/ | ||
export const isExcelDesktopDocument: GetSourceFunction = props => { | ||
const { htmlAttributes } = props; | ||
// The presence of this attribute confirms its origin from Excel Desktop | ||
return htmlAttributes[PastePropertyNames.EXCEL_DESKTOP_ATTRIBUTE_NAME] == EXCEL_ATTRIBUTE_VALUE; | ||
}; |
20 changes: 20 additions & 0 deletions
20
...del-editor/lib/editor/plugins/PastePlugin/pasteSourceValidations/isExcelOnlineDocument.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,20 @@ | ||
import { PastePropertyNames } from './constants'; | ||
import type { GetSourceFunction } from './getPasteSource'; | ||
|
||
// Excel Desktop also has this attribute | ||
const EXCEL_ONLINE_ATTRIBUTE_VALUE = 'Excel.Sheet'; | ||
|
||
/** | ||
* @internal | ||
* Checks whether the Array provided contains strings that identify Excel Online documents | ||
* @param props Properties related to the PasteEvent | ||
* @returns | ||
*/ | ||
export const isExcelOnlineDocument: GetSourceFunction = props => { | ||
const { htmlAttributes } = props; | ||
// The presence of Excel.Sheet confirms its origin from Excel, the absence of EXCEL_DESKTOP_ATTRIBUTE_NAME confirms it is from the Online version | ||
return ( | ||
htmlAttributes[PastePropertyNames.PROG_ID_NAME] == EXCEL_ONLINE_ATTRIBUTE_VALUE && | ||
htmlAttributes[PastePropertyNames.EXCEL_DESKTOP_ATTRIBUTE_NAME] == undefined | ||
); | ||
}; |
13 changes: 13 additions & 0 deletions
13
...del-editor/lib/editor/plugins/PastePlugin/pasteSourceValidations/isGoogleSheetDocument.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,13 @@ | ||
import { PastePropertyNames } from './constants'; | ||
import type { GetSourceFunction } from './getPasteSource'; | ||
|
||
/** | ||
* @internal | ||
* Checks whether the fragment provided contain elements from Google sheets | ||
* @param props Properties related to the PasteEvent | ||
* @returns | ||
*/ | ||
export const isGoogleSheetDocument: GetSourceFunction = props => { | ||
const { fragment } = props; | ||
return !!fragment.querySelector(PastePropertyNames.GOOGLE_SHEET_NODE_NAME); | ||
}; |
14 changes: 14 additions & 0 deletions
14
...itor/lib/editor/plugins/PastePlugin/pasteSourceValidations/isPowerPointDesktopDocument.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,14 @@ | ||
import { PastePropertyNames } from './constants'; | ||
import type { GetSourceFunction } from './getPasteSource'; | ||
|
||
const POWERPOINT_ATTRIBUTE_VALUE = 'PowerPoint.Slide'; | ||
|
||
/** | ||
* @internal | ||
* Checks whether the Array provided contains strings that identify Power Point Desktop documents | ||
* @param props Properties related to the PasteEvent | ||
* @returns | ||
*/ | ||
export const isPowerPointDesktopDocument: GetSourceFunction = props => { | ||
return props.htmlAttributes[PastePropertyNames.PROG_ID_NAME] == POWERPOINT_ATTRIBUTE_VALUE; | ||
}; |
20 changes: 20 additions & 0 deletions
20
...del-editor/lib/editor/plugins/PastePlugin/pasteSourceValidations/isWordDesktopDocument.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,20 @@ | ||
import { PastePropertyNames } from './constants'; | ||
import type { GetSourceFunction } from './getPasteSource'; | ||
|
||
const WORD_ATTRIBUTE_NAME = 'xmlns:w'; | ||
const WORD_ATTRIBUTE_VALUE = 'urn:schemas-microsoft-com:office:word'; | ||
const WORD_PROG_ID = 'Word.Document'; | ||
|
||
/** | ||
* @internal | ||
* Checks whether the Array provided contains strings that identify Word Desktop documents | ||
* @param props Properties related to the PasteEvent | ||
* @returns | ||
*/ | ||
export const isWordDesktopDocument: GetSourceFunction = props => { | ||
const { htmlAttributes } = props; | ||
return ( | ||
htmlAttributes[WORD_ATTRIBUTE_NAME] == WORD_ATTRIBUTE_VALUE || | ||
htmlAttributes[PastePropertyNames.PROG_ID_NAME] == WORD_PROG_ID | ||
); | ||
}; |
Oops, something went wrong.