-
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: decouple entity (#2107)
* Standalone editor: decouple entity * fix build * fix build * improve * fix build * add test
- Loading branch information
1 parent
ac0a912
commit 43a947b
Showing
44 changed files
with
765 additions
and
271 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
43 changes: 43 additions & 0 deletions
43
packages-content-model/roosterjs-content-model-dom/lib/domUtils/entityUtils.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,43 @@ | ||
import { isNodeOfType } from './isNodeOfType'; | ||
import type { ContentModelEntityFormat } from 'roosterjs-content-model-types'; | ||
|
||
const ENTITY_INFO_NAME = '_Entity'; | ||
const ENTITY_TYPE_PREFIX = '_EType_'; | ||
const ENTITY_ID_PREFIX = '_EId_'; | ||
const ENTITY_READONLY_PREFIX = '_EReadonly_'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function isEntityElement(node: Node): boolean { | ||
return isNodeOfType(node, 'ELEMENT_NODE') && node.classList.contains(ENTITY_INFO_NAME); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function parseEntityClassName( | ||
className: string, | ||
format: ContentModelEntityFormat | ||
): boolean | undefined { | ||
if (className == ENTITY_INFO_NAME) { | ||
return true; | ||
} else if (className.indexOf(ENTITY_TYPE_PREFIX) == 0) { | ||
format.entityType = className.substring(ENTITY_TYPE_PREFIX.length); | ||
} else if (className.indexOf(ENTITY_ID_PREFIX) == 0) { | ||
format.id = className.substring(ENTITY_ID_PREFIX.length); | ||
} else if (className.indexOf(ENTITY_READONLY_PREFIX) == 0) { | ||
format.isReadonly = className.substring(ENTITY_READONLY_PREFIX.length) == '1'; | ||
} | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function generateEntityClassNames(format: ContentModelEntityFormat): string { | ||
return format.isFakeEntity | ||
? '' | ||
: `${ENTITY_INFO_NAME} ${ENTITY_TYPE_PREFIX}${format.entityType ?? ''} ${ | ||
format.id ? `${ENTITY_ID_PREFIX}${format.id} ` : '' | ||
}${ENTITY_READONLY_PREFIX}${format.isReadonly ? '1' : '0'}`; | ||
} |
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
33 changes: 33 additions & 0 deletions
33
...ontent-model/roosterjs-content-model-dom/lib/formatHandlers/entity/entityFormatHandler.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,33 @@ | ||
import { generateEntityClassNames, parseEntityClassName } from '../../domUtils/entityUtils'; | ||
import type { EntityInfoFormat, IdFormat } from 'roosterjs-content-model-types'; | ||
import type { FormatHandler } from '../FormatHandler'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const entityFormatHandler: FormatHandler<EntityInfoFormat & IdFormat> = { | ||
parse: (format, element) => { | ||
let isEntity = false; | ||
|
||
element.classList.forEach(name => { | ||
isEntity = parseEntityClassName(name, format) || isEntity; | ||
}); | ||
|
||
if (!isEntity) { | ||
format.isFakeEntity = true; | ||
format.isReadonly = !element.isContentEditable; | ||
} | ||
}, | ||
|
||
apply: (format, element) => { | ||
if (!format.isFakeEntity) { | ||
element.className = generateEntityClassNames(format); | ||
} | ||
|
||
if (format.isReadonly) { | ||
element.contentEditable = 'false'; | ||
} else { | ||
element.removeAttribute('contenteditable'); | ||
} | ||
}, | ||
}; |
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
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.