Skip to content

Commit

Permalink
Merge branch 'test' into u/jisong/cache8
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong committed Sep 20, 2023
2 parents 2edc202 + 984487e commit 59e034b
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { createTable } from '../../../lib/modelApi/creators/createTable';
import { createTableCell } from '../../../lib/modelApi/creators/createTableCell';
import { createText } from '../../../lib/modelApi/creators/createText';
import {
ContentModelCode,
ContentModelLink,
ContentModelListLevel,
ContentModelSegmentFormat,
ContentModelTableCellFormat,
Expand Down Expand Up @@ -188,6 +190,47 @@ describe('Creators', () => {
expect(format).toEqual({ a: 1 });
});

it('createText with decorators', () => {
const format = { a: 1 } as any;
const text = 'test';
const link: ContentModelLink = {
dataset: {},
format: {
href: 'test',
},
};
const code: ContentModelCode = {
format: { fontFamily: 'test' },
};
const result = createText(text, format, link, code);

expect(result).toEqual({
segmentType: 'Text',
format: { a: 1 } as any,
text: text,
link,
code,
});
expect(result.link).not.toBe(link);
expect(result.code).not.toBe(code);

result.link!.dataset.a = 'b';
result.link!.format.href = 'test2';

expect(link).toEqual({
dataset: {},
format: {
href: 'test',
},
});

result.code!.format.fontFamily = 'test2';

expect(code).toEqual({
format: { fontFamily: 'test' },
});
});

it('createTable', () => {
const tableModel = createTable(2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ describe('ContentModelFormatPlugin', () => {
},
cacheContentModel: () => {},
isDarkMode: () => false,
getContentModelDefaultFormat: () => ({}),
triggerPluginEvent: jasmine.createSpy('triggerPluginEvent'),
getContentModelDefaultFormat: () => ({}),
} as any) as IContentModelEditor;
const plugin = new ContentModelFormatPlugin();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { adjustWordSelection } from '../../../lib/modelApi/selection/adjustWordSelection';
import {
createContentModelDocument,
createParagraph,
createSelectionMarker,
createText,
} from 'roosterjs-content-model-dom';
import {
ContentModelBlock,
ContentModelDocument,
Expand Down Expand Up @@ -885,4 +891,84 @@ describe('adjustWordSelection', () => {
);
});
});

it('Do not modify segments array if no word is selected: marker before text', () => {
const text = createText('Word1 Word2');
const marker = createSelectionMarker();
const paragraph = createParagraph();
const model = createContentModelDocument();

paragraph.segments.push(marker, text);
model.blocks.push(paragraph);

adjustWordSelection(model, marker);

expect(model).toEqual({
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
segments: [marker, text],
format: {},
},
],
});
expect(model.blocks[0]).toBe(paragraph);
expect(paragraph.segments[0]).toBe(marker);
expect(paragraph.segments[1]).toBe(text);
});

it('Do not modify segments array if no word is selected: marker after text', () => {
const text = createText('Word1 Word2');
const marker = createSelectionMarker();
const paragraph = createParagraph();
const model = createContentModelDocument();

paragraph.segments.push(text, marker);
model.blocks.push(paragraph);

adjustWordSelection(model, marker);

expect(model).toEqual({
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
segments: [text, marker],
format: {},
},
],
});
expect(model.blocks[0]).toBe(paragraph);
expect(paragraph.segments[0]).toBe(text);
expect(paragraph.segments[1]).toBe(marker);
});

it('Do not modify segments array if no word is selected: marker between text', () => {
const text1 = createText('Word1 ');
const text2 = createText(' Word2');
const marker = createSelectionMarker();
const paragraph = createParagraph();
const model = createContentModelDocument();

paragraph.segments.push(text1, marker, text2);
model.blocks.push(paragraph);

adjustWordSelection(model, marker);

expect(model).toEqual({
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
segments: [text1, marker, text2],
format: {},
},
],
});
expect(model.blocks[0]).toBe(paragraph);
expect(paragraph.segments[0]).toBe(text1);
expect(paragraph.segments[1]).toBe(marker);
expect(paragraph.segments[2]).toBe(text2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -725,4 +725,80 @@ describe('setSelection', () => {
],
});
});

it('Update table selection', () => {
const model = createContentModelDocument();
const table = createTable(3);

const cell00 = createTableCell();
const cell01 = createTableCell();
const cell02 = createTableCell();
const cell10 = createTableCell();
const cell11 = createTableCell();
const cell12 = createTableCell();
const cell20 = createTableCell();
const cell21 = createTableCell();
const cell22 = createTableCell();

table.rows[0].cells.push(cell00, cell01, cell02);
table.rows[1].cells.push(cell10, cell11, cell12);
table.rows[2].cells.push(cell20, cell21, cell22);

cell00.isSelected = true;
cell01.isSelected = true;
cell10.isSelected = true;
cell11.isSelected = true;

model.blocks.push(table);

setSelection(model, cell11, cell22);

expect(cell00.isSelected).toBeFalsy();
expect(cell01.isSelected).toBeFalsy();
expect(cell02.isSelected).toBeFalsy();
expect(cell10.isSelected).toBeFalsy();
expect(cell11.isSelected).toBeTrue();
expect(cell12.isSelected).toBeTrue();
expect(cell20.isSelected).toBeFalsy();
expect(cell21.isSelected).toBeTrue();
expect(cell22.isSelected).toBeTrue();
});

it('Clear table selection', () => {
const model = createContentModelDocument();
const table = createTable(3);

const cell00 = createTableCell();
const cell01 = createTableCell();
const cell02 = createTableCell();
const cell10 = createTableCell();
const cell11 = createTableCell();
const cell12 = createTableCell();
const cell20 = createTableCell();
const cell21 = createTableCell();
const cell22 = createTableCell();

table.rows[0].cells.push(cell00, cell01, cell02);
table.rows[1].cells.push(cell10, cell11, cell12);
table.rows[2].cells.push(cell20, cell21, cell22);

cell00.isSelected = true;
cell01.isSelected = true;
cell10.isSelected = true;
cell11.isSelected = true;

model.blocks.push(table);

setSelection(model);

expect(cell00.isSelected).toBeFalsy();
expect(cell01.isSelected).toBeFalsy();
expect(cell02.isSelected).toBeFalsy();
expect(cell10.isSelected).toBeFalsy();
expect(cell11.isSelected).toBeFalsy();
expect(cell12.isSelected).toBeFalsy();
expect(cell20.isSelected).toBeFalsy();
expect(cell21.isSelected).toBeFalsy();
expect(cell22.isSelected).toBeFalsy();
});
});

0 comments on commit 59e034b

Please sign in to comment.