Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaroldi committed Nov 6, 2024
1 parent 1f3422c commit 3a00c66
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 59 deletions.
5 changes: 1 addition & 4 deletions packages/roosterjs-content-model-api/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,4 @@ export { setModelIndentation } from './modelApi/block/setModelIndentation';
export { matchLink } from './modelApi/link/matchLink';
export { promoteLink } from './modelApi/link/promoteLink';
export { getListAnnounceData } from './modelApi/list/getListAnnounceData';
export {
queryContentModelBlocks,
QueryContentModelOptions,
} from './modelApi/common/queryContentModelBlocks';
export { queryContentModelBlocks } from './modelApi/common/queryContentModelBlocks';
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,19 @@ import type {
ReadonlyContentModelBlockGroup,
} from 'roosterjs-content-model-types';

/**
* Options for queryContentModel
*/
export interface QueryContentModelOptions<T extends ReadonlyContentModelBlock> {
/**
* The type of block to query @default 'Paragraph'
*/
blockType?: ContentModelBlockType;

/**
* Optional selector to filter the blocks
*/
filter?: (element: T) => element is T;

/**
* True to return the first block only, false to return all blocks
*/
findFirstOnly?: boolean;
}

/**
* Query content model blocks
* @param group The block group to query
* @param options The query option
* @param blockType The type of block to query @default 'Paragraph'
* @param filter Optional selector to filter the blocks
* @param findFirstOnly True to return the first block only, false to return all blocks
*/
export function queryContentModelBlocks<T extends ReadonlyContentModelBlock>(
group: ReadonlyContentModelBlockGroup,
options: QueryContentModelOptions<T>
blockType?: ContentModelBlockType,
filter?: (element: T) => element is T,
findFirstOnly?: boolean
): T[] {
const { blockType, filter, findFirstOnly } = options;
const type = blockType || 'Paragraph';
const elements: T[] = [];
for (let i = 0; i < group.blocks.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('queryContentModelBlocksBlocks', () => {
};

// Act
const result = queryContentModelBlocks(group, {});
const result = queryContentModelBlocks(group);

// Assert
expect(result).toEqual([]);
Expand All @@ -36,7 +36,7 @@ describe('queryContentModelBlocksBlocks', () => {
};

// Act
const result = queryContentModelBlocks(group, { blockType: 'Table' });
const result = queryContentModelBlocks(group, 'Table');

// Assert
expect(result).toEqual([]);
Expand Down Expand Up @@ -318,9 +318,7 @@ describe('queryContentModelBlocksBlocks', () => {
];

// Act
const result = queryContentModelBlocks<ReadonlyContentModelTable>(group, {
blockType: 'Table',
});
const result = queryContentModelBlocks<ReadonlyContentModelTable>(group, 'Table');

// Assert
expect(result).toEqual(expected);
Expand Down Expand Up @@ -377,10 +375,11 @@ describe('queryContentModelBlocksBlocks', () => {
};

// Act
const result = queryContentModelBlocks<ReadonlyContentModelParagraph>(group, {
blockType: 'Paragraph',
filter: (block): block is ReadonlyContentModelParagraph => block.segments.length == 2,
});
const result = queryContentModelBlocks<ReadonlyContentModelParagraph>(
group,
'Paragraph',
(block): block is ReadonlyContentModelParagraph => block.segments.length == 2
);

// Assert
expect(result).toEqual([paragraph]);
Expand Down Expand Up @@ -765,9 +764,7 @@ describe('queryContentModelBlocksBlocks', () => {
},
},
];
const result = queryContentModelBlocks<ReadonlyContentModelTable>(model, {
blockType: 'Table',
});
const result = queryContentModelBlocks<ReadonlyContentModelTable>(model, 'Table');
expect(result).toEqual(expected);
});

Expand Down Expand Up @@ -905,9 +902,7 @@ describe('queryContentModelBlocksBlocks', () => {
};

const expected: ReadonlyContentModelTable[] = [table];
const result = queryContentModelBlocks<ReadonlyContentModelTable>(model, {
blockType: 'Table',
});
const result = queryContentModelBlocks<ReadonlyContentModelTable>(model, 'Table');
expect(result).toEqual(expected);
});

Expand Down Expand Up @@ -1167,11 +1162,11 @@ describe('queryContentModelBlocksBlocks', () => {
},
];

const result = queryContentModelBlocks<ReadonlyContentModelListItem>(model, {
blockType: 'BlockGroup',
filter: (block): block is ReadonlyContentModelListItem =>
block.blockGroupType == 'ListItem',
});
const result = queryContentModelBlocks<ReadonlyContentModelListItem>(
model,
'BlockGroup',
(block): block is ReadonlyContentModelListItem => block.blockGroupType == 'ListItem'
);
expect(result).toEqual(listExpected);
});

Expand Down Expand Up @@ -1456,19 +1451,19 @@ describe('queryContentModelBlocksBlocks', () => {
format: {},
},
};
const result = queryContentModelBlocks<ReadonlyContentModelParagraph>(model, {
findFirstOnly: true,
filter: (
block: ReadonlyContentModelParagraph
): block is ReadonlyContentModelParagraph => {
const result = queryContentModelBlocks<ReadonlyContentModelParagraph>(
model,
'Paragraph',
(block: ReadonlyContentModelParagraph): block is ReadonlyContentModelParagraph => {
for (const segment of block.segments) {
if (segment.segmentType == 'Image' && segment.dataset.isEditing) {
return true;
}
}
return false;
},
});
true /* findFirstOnly */
);
expect(result).toEqual([imageAndParagraph]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export function findEditingImage(
imageId?: string
): ImageAndParagraph | null {
let imageAndParagraph: ImageAndParagraph | null = null;
queryContentModelBlocks<ReadonlyContentModelParagraph>(group, {
filter: (
paragraph: ReadonlyContentModelParagraph
): paragraph is ReadonlyContentModelParagraph => {
queryContentModelBlocks<ReadonlyContentModelParagraph>(
group,
'Paragraph',
(paragraph: ReadonlyContentModelParagraph): paragraph is ReadonlyContentModelParagraph => {
for (const segment of paragraph.segments) {
if (
segment.segmentType == 'Image' &&
Expand All @@ -28,8 +28,8 @@ export function findEditingImage(
}
return false;
},
findFirstOnly: true,
});
true /*findFirstOnly*/
);

return imageAndParagraph;
}

0 comments on commit 3a00c66

Please sign in to comment.