-
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.
Add support to cursor around Block entities. (#2350)
- Loading branch information
1 parent
01e79bd
commit 7998d03
Showing
8 changed files
with
153 additions
and
11 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
40 changes: 40 additions & 0 deletions
40
...ges-content-model/roosterjs-content-model-core/lib/override/pasteCopyBlockEntityParser.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,40 @@ | ||
import { isElementOfType, isNodeOfType } from 'roosterjs-content-model-dom'; | ||
import type { | ||
ContentModelEntity, | ||
EntityInfoFormat, | ||
FormatParser, | ||
OnNodeCreated, | ||
} from 'roosterjs-content-model-types'; | ||
|
||
const BLOCK_ENTITY_CLASS = '_EBlock'; | ||
const ONE_HUNDRED_PERCENT = '100%'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const onCreateCopyEntityNode: OnNodeCreated = (model, node) => { | ||
const entityModel = model as ContentModelEntity; | ||
if ( | ||
entityModel && | ||
entityModel.wrapper && | ||
entityModel.segmentType == 'Entity' && | ||
isNodeOfType(node, 'ELEMENT_NODE') && | ||
isElementOfType(node, 'span') && | ||
node.style.width == ONE_HUNDRED_PERCENT && | ||
node.style.display == 'inline-block' | ||
) { | ||
node.classList.add(BLOCK_ENTITY_CLASS); | ||
node.style.display = 'block'; | ||
} | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const pasteBlockEntityParser: FormatParser<EntityInfoFormat> = (_, element) => { | ||
if (element.classList.contains(BLOCK_ENTITY_CLASS)) { | ||
element.style.display = 'inline-block'; | ||
element.style.width = ONE_HUNDRED_PERCENT; | ||
element.classList.remove(BLOCK_ENTITY_CLASS); | ||
} | ||
}; |
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
82 changes: 82 additions & 0 deletions
82
...ntent-model/roosterjs-content-model-core/test/overrides/pasteCopyBlockEntityParserTest.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,82 @@ | ||
import { ContentModelEntity } from 'roosterjs-content-model-types'; | ||
import { | ||
onCreateCopyEntityNode, | ||
pasteBlockEntityParser, | ||
} from '../../lib/override/pasteCopyBlockEntityParser'; | ||
|
||
describe('onCreateCopyEntityNode', () => { | ||
it('handle', () => { | ||
const span = document.createElement('span'); | ||
span.style.width = '100%'; | ||
span.style.display = 'inline-block'; | ||
const modelEntity: ContentModelEntity = { | ||
entityFormat: {}, | ||
format: {}, | ||
wrapper: span, | ||
segmentType: 'Entity', | ||
blockType: 'Entity', | ||
}; | ||
|
||
onCreateCopyEntityNode(modelEntity, span); | ||
|
||
expect(span.style.display).toEqual('block'); | ||
expect(span.classList.contains('_EBlock')).toBeTrue(); | ||
}); | ||
|
||
it('Dont handle, no 100% width', () => { | ||
const span = document.createElement('span'); | ||
span.style.display = 'inline-block'; | ||
const modelEntity: ContentModelEntity = { | ||
entityFormat: {}, | ||
format: {}, | ||
wrapper: span, | ||
segmentType: 'Entity', | ||
blockType: 'Entity', | ||
}; | ||
|
||
onCreateCopyEntityNode(modelEntity, span); | ||
|
||
expect(span.style.display).not.toEqual('block'); | ||
expect(span.classList.contains('_EBlock')).not.toBeTrue(); | ||
}); | ||
|
||
it('Dont handle, not inline block', () => { | ||
const span = document.createElement('span'); | ||
span.style.width = '100%'; | ||
const modelEntity: ContentModelEntity = { | ||
entityFormat: {}, | ||
format: {}, | ||
wrapper: span, | ||
segmentType: 'Entity', | ||
blockType: 'Entity', | ||
}; | ||
|
||
onCreateCopyEntityNode(modelEntity, span); | ||
|
||
expect(span.style.display).not.toEqual('block'); | ||
expect(span.classList.contains('_EBlock')).not.toBeTrue(); | ||
}); | ||
}); | ||
|
||
describe('pasteBlockEntityParser', () => { | ||
it('handle', () => { | ||
const span = document.createElement('span'); | ||
span.classList.add('_EBlock'); | ||
|
||
pasteBlockEntityParser({}, span, <any>{}, {}); | ||
|
||
expect(span.style.width).toEqual('100%'); | ||
expect(span.style.display).toEqual('inline-block'); | ||
expect(span.classList.contains('_EBlock')).toBeFalse(); | ||
}); | ||
|
||
it('Dont handle', () => { | ||
const span = document.createElement('span'); | ||
|
||
pasteBlockEntityParser({}, span, <any>{}, {}); | ||
|
||
expect(span.style.width).not.toEqual('100%'); | ||
expect(span.style.display).not.toEqual('inline-block'); | ||
expect(span.classList.contains('_EBlock')).toBeFalse(); | ||
}); | ||
}); |
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