Skip to content

Commit

Permalink
Content Model: Fix overwrite table cell bug (#2240)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong authored Dec 4, 2023
1 parent 2bee980 commit a7bdb43
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import type { ContentModelBlockGroup, DomToModelContext } from 'roosterjs-conten
* @internal
*/
export function addSelectionMarker(group: ContentModelBlockGroup, context: DomToModelContext) {
const marker = createSelectionMarker(context.segmentFormat);
const segmentFormat = {
...context.defaultFormat,
...context.segmentFormat,
};
const marker = createSelectionMarker(segmentFormat);

addDecorators(marker, context);

addSegment(group, marker, context.blockFormat);
addSegment(group, marker, context.blockFormat, segmentFormat);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export { createListLevel } from './modelApi/creators/createListLevel';
export { addBlock } from './modelApi/common/addBlock';
export { addCode } from './modelApi/common/addDecorators';
export { addLink } from './modelApi/common/addDecorators';
export { ensureParagraph } from './modelApi/common/ensureParagraph';

export { normalizeContentModel } from './modelApi/common/normalizeContentModel';
export { isGeneralSegment } from './modelApi/common/isGeneralSegment';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
ContentModelBlockGroup,
ContentModelParagraph,
ContentModelSegment,
ContentModelSegmentFormat,
} from 'roosterjs-content-model-types';

/**
Expand All @@ -16,9 +17,10 @@ import type {
export function addSegment(
group: ContentModelBlockGroup,
newSegment: ContentModelSegment,
blockFormat?: ContentModelBlockFormat
blockFormat?: ContentModelBlockFormat,
segmentFormat?: ContentModelSegmentFormat
): ContentModelParagraph {
const paragraph = ensureParagraph(group, blockFormat);
const paragraph = ensureParagraph(group, blockFormat, segmentFormat);
const lastSegment = paragraph.segments[paragraph.segments.length - 1];

if (newSegment.segmentType == 'SelectionMarker') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@ import type {
ContentModelBlockFormat,
ContentModelBlockGroup,
ContentModelParagraph,
ContentModelSegmentFormat,
} from 'roosterjs-content-model-types';

/**
* @internal
* Ensure there is a Paragraph that can insert segments in a Content Model Block Group
* @param group The parent block group of the target paragraph
* @param blockFormat Format of the paragraph. This is only used if we need to create a new paragraph
*/
export function ensureParagraph(
group: ContentModelBlockGroup,
blockFormat?: ContentModelBlockFormat
blockFormat?: ContentModelBlockFormat,
segmentFormat?: ContentModelSegmentFormat
): ContentModelParagraph {
const lastBlock = group.blocks[group.blocks.length - 1];

if (lastBlock?.blockType == 'Paragraph') {
return lastBlock;
} else {
const paragraph = createParagraph(true, blockFormat);
const paragraph = createParagraph(true, blockFormat, segmentFormat);
addBlock(group, paragraph);

return paragraph;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ describe('childProcessor', () => {
{ segmentType: 'SelectionMarker', format: { a: 'b' } as any, isSelected: true },
],
isImplicit: true,
segmentFormat: { a: 'b' } as any,
format: {},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('addSelectionMarker', () => {
format: { fontWeight: 'bold' },
},
],
segmentFormat: { fontWeight: 'bold' },
},
],
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { deleteSelection, isModifierKey } from 'roosterjs-content-model-core';
import { normalizeContentModel } from 'roosterjs-content-model-dom';
import type { IContentModelEditor } from 'roosterjs-content-model-editor';
import type { DOMSelection } from 'roosterjs-content-model-types';

Expand Down Expand Up @@ -26,6 +27,8 @@ export function keyboardInput(editor: IContentModelEditor, rawEvent: KeyboardEve
// We have deleted something, next input should inherit the segment format from deleted content, so set pending format here
context.newPendingFormat = result.insertPoint?.marker.format;

normalizeContentModel(model);

// Do not preventDefault since we still want browser to handle the final input for now
return true;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as deleteSelection from 'roosterjs-content-model-core/lib/publicApi/selection/deleteSelection';
import * as normalizeContentModel from 'roosterjs-content-model-dom/lib/modelApi/common/normalizeContentModel';
import { IContentModelEditor } from 'roosterjs-content-model-editor';
import { keyboardInput } from '../../lib/edit/keyboardInput';
import {
Expand All @@ -14,6 +15,7 @@ describe('keyboardInput', () => {
let getDOMSelectionSpy: jasmine.Spy;
let deleteSelectionSpy: jasmine.Spy;
let mockedModel: ContentModelDocument;
let normalizeContentModelSpy: jasmine.Spy;
let mockedContext: FormatWithContentModelContext;
let formatResult: boolean | undefined;

Expand All @@ -34,6 +36,7 @@ describe('keyboardInput', () => {
});
getDOMSelectionSpy = jasmine.createSpy('getDOMSelection');
deleteSelectionSpy = spyOn(deleteSelection, 'deleteSelection');
normalizeContentModelSpy = spyOn(normalizeContentModel, 'normalizeContentModel');

editor = {
getDOMSelection: getDOMSelectionSpy,
Expand Down Expand Up @@ -69,6 +72,7 @@ describe('keyboardInput', () => {
newEntities: [],
newImages: [],
});
expect(normalizeContentModelSpy).not.toHaveBeenCalled();
});

it('Letter input, expanded selection, no modifier key, deleteSelection returns not deleted', () => {
Expand Down Expand Up @@ -100,6 +104,7 @@ describe('keyboardInput', () => {
clearModelCache: true,
skipUndoSnapshot: true,
});
expect(normalizeContentModelSpy).not.toHaveBeenCalled();
});

it('Letter input, expanded selection, no modifier key, deleteSelection returns range', () => {
Expand Down Expand Up @@ -132,6 +137,7 @@ describe('keyboardInput', () => {
skipUndoSnapshot: true,
newPendingFormat: undefined,
});
expect(normalizeContentModelSpy).toHaveBeenCalledWith(mockedModel);
});

it('Letter input, table selection, no modifier key, deleteSelection returns range', () => {
Expand Down Expand Up @@ -161,6 +167,7 @@ describe('keyboardInput', () => {
skipUndoSnapshot: true,
newPendingFormat: undefined,
});
expect(normalizeContentModelSpy).toHaveBeenCalledWith(mockedModel);
});

it('Letter input, image selection, no modifier key, deleteSelection returns range', () => {
Expand Down Expand Up @@ -190,6 +197,7 @@ describe('keyboardInput', () => {
skipUndoSnapshot: true,
newPendingFormat: undefined,
});
expect(normalizeContentModelSpy).toHaveBeenCalledWith(mockedModel);
});

it('Letter input, no selection, no modifier key, deleteSelection returns range', () => {
Expand All @@ -214,6 +222,7 @@ describe('keyboardInput', () => {
newEntities: [],
newImages: [],
});
expect(normalizeContentModelSpy).not.toHaveBeenCalled();
});

it('Letter input, expanded selection, has modifier key, deleteSelection returns range', () => {
Expand Down Expand Up @@ -244,6 +253,7 @@ describe('keyboardInput', () => {
newEntities: [],
newImages: [],
});
expect(normalizeContentModelSpy).not.toHaveBeenCalled();
});

it('Space input, table selection, no modifier key, deleteSelection returns range', () => {
Expand Down Expand Up @@ -273,6 +283,7 @@ describe('keyboardInput', () => {
skipUndoSnapshot: true,
newPendingFormat: undefined,
});
expect(normalizeContentModelSpy).toHaveBeenCalledWith(mockedModel);
});

it('Backspace input, table selection, no modifier key, deleteSelection returns range', () => {
Expand All @@ -299,6 +310,7 @@ describe('keyboardInput', () => {
newEntities: [],
newImages: [],
});
expect(normalizeContentModelSpy).not.toHaveBeenCalled();
});

it('Enter input, table selection, no modifier key, deleteSelection returns range', () => {
Expand Down Expand Up @@ -328,6 +340,7 @@ describe('keyboardInput', () => {
skipUndoSnapshot: true,
newPendingFormat: undefined,
});
expect(normalizeContentModelSpy).toHaveBeenCalledWith(mockedModel);
});

it('Letter input, expanded selection, no modifier key, deleteSelection returns range, has segment format', () => {
Expand Down Expand Up @@ -366,5 +379,6 @@ describe('keyboardInput', () => {
skipUndoSnapshot: true,
newPendingFormat: mockedFormat,
});
expect(normalizeContentModelSpy).toHaveBeenCalledWith(mockedModel);
});
});

0 comments on commit a7bdb43

Please sign in to comment.