From 3a00c66a0417fee350589830803fc50349e14a22 Mon Sep 17 00:00:00 2001 From: "Julia Roldi (from Dev Box)" Date: Wed, 6 Nov 2024 11:41:54 -0300 Subject: [PATCH] refactor --- .../roosterjs-content-model-api/lib/index.ts | 5 +- .../common/queryContentModelBlocks.ts | 29 +++--------- .../common/queryContentModelBlocksTest.ts | 47 +++++++++---------- .../lib/imageEdit/utils/findEditingImage.ts | 12 ++--- 4 files changed, 34 insertions(+), 59 deletions(-) diff --git a/packages/roosterjs-content-model-api/lib/index.ts b/packages/roosterjs-content-model-api/lib/index.ts index 88d60893bc5..292f44d08e5 100644 --- a/packages/roosterjs-content-model-api/lib/index.ts +++ b/packages/roosterjs-content-model-api/lib/index.ts @@ -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'; diff --git a/packages/roosterjs-content-model-api/lib/modelApi/common/queryContentModelBlocks.ts b/packages/roosterjs-content-model-api/lib/modelApi/common/queryContentModelBlocks.ts index aed7d04f0f7..ebd290de2a7 100644 --- a/packages/roosterjs-content-model-api/lib/modelApi/common/queryContentModelBlocks.ts +++ b/packages/roosterjs-content-model-api/lib/modelApi/common/queryContentModelBlocks.ts @@ -4,36 +4,19 @@ import type { ReadonlyContentModelBlockGroup, } from 'roosterjs-content-model-types'; -/** - * Options for queryContentModel - */ -export interface QueryContentModelOptions { - /** - * 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( group: ReadonlyContentModelBlockGroup, - options: QueryContentModelOptions + 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++) { diff --git a/packages/roosterjs-content-model-api/test/modelApi/common/queryContentModelBlocksTest.ts b/packages/roosterjs-content-model-api/test/modelApi/common/queryContentModelBlocksTest.ts index 34c64d45c49..8a07dc41da6 100644 --- a/packages/roosterjs-content-model-api/test/modelApi/common/queryContentModelBlocksTest.ts +++ b/packages/roosterjs-content-model-api/test/modelApi/common/queryContentModelBlocksTest.ts @@ -15,7 +15,7 @@ describe('queryContentModelBlocksBlocks', () => { }; // Act - const result = queryContentModelBlocks(group, {}); + const result = queryContentModelBlocks(group); // Assert expect(result).toEqual([]); @@ -36,7 +36,7 @@ describe('queryContentModelBlocksBlocks', () => { }; // Act - const result = queryContentModelBlocks(group, { blockType: 'Table' }); + const result = queryContentModelBlocks(group, 'Table'); // Assert expect(result).toEqual([]); @@ -318,9 +318,7 @@ describe('queryContentModelBlocksBlocks', () => { ]; // Act - const result = queryContentModelBlocks(group, { - blockType: 'Table', - }); + const result = queryContentModelBlocks(group, 'Table'); // Assert expect(result).toEqual(expected); @@ -377,10 +375,11 @@ describe('queryContentModelBlocksBlocks', () => { }; // Act - const result = queryContentModelBlocks(group, { - blockType: 'Paragraph', - filter: (block): block is ReadonlyContentModelParagraph => block.segments.length == 2, - }); + const result = queryContentModelBlocks( + group, + 'Paragraph', + (block): block is ReadonlyContentModelParagraph => block.segments.length == 2 + ); // Assert expect(result).toEqual([paragraph]); @@ -765,9 +764,7 @@ describe('queryContentModelBlocksBlocks', () => { }, }, ]; - const result = queryContentModelBlocks(model, { - blockType: 'Table', - }); + const result = queryContentModelBlocks(model, 'Table'); expect(result).toEqual(expected); }); @@ -905,9 +902,7 @@ describe('queryContentModelBlocksBlocks', () => { }; const expected: ReadonlyContentModelTable[] = [table]; - const result = queryContentModelBlocks(model, { - blockType: 'Table', - }); + const result = queryContentModelBlocks(model, 'Table'); expect(result).toEqual(expected); }); @@ -1167,11 +1162,11 @@ describe('queryContentModelBlocksBlocks', () => { }, ]; - const result = queryContentModelBlocks(model, { - blockType: 'BlockGroup', - filter: (block): block is ReadonlyContentModelListItem => - block.blockGroupType == 'ListItem', - }); + const result = queryContentModelBlocks( + model, + 'BlockGroup', + (block): block is ReadonlyContentModelListItem => block.blockGroupType == 'ListItem' + ); expect(result).toEqual(listExpected); }); @@ -1456,11 +1451,10 @@ describe('queryContentModelBlocksBlocks', () => { format: {}, }, }; - const result = queryContentModelBlocks(model, { - findFirstOnly: true, - filter: ( - block: ReadonlyContentModelParagraph - ): block is ReadonlyContentModelParagraph => { + const result = queryContentModelBlocks( + model, + 'Paragraph', + (block: ReadonlyContentModelParagraph): block is ReadonlyContentModelParagraph => { for (const segment of block.segments) { if (segment.segmentType == 'Image' && segment.dataset.isEditing) { return true; @@ -1468,7 +1462,8 @@ describe('queryContentModelBlocksBlocks', () => { } return false; }, - }); + true /* findFirstOnly */ + ); expect(result).toEqual([imageAndParagraph]); }); }); diff --git a/packages/roosterjs-content-model-plugins/lib/imageEdit/utils/findEditingImage.ts b/packages/roosterjs-content-model-plugins/lib/imageEdit/utils/findEditingImage.ts index 8c20467c371..a3b257c9d94 100644 --- a/packages/roosterjs-content-model-plugins/lib/imageEdit/utils/findEditingImage.ts +++ b/packages/roosterjs-content-model-plugins/lib/imageEdit/utils/findEditingImage.ts @@ -13,10 +13,10 @@ export function findEditingImage( imageId?: string ): ImageAndParagraph | null { let imageAndParagraph: ImageAndParagraph | null = null; - queryContentModelBlocks(group, { - filter: ( - paragraph: ReadonlyContentModelParagraph - ): paragraph is ReadonlyContentModelParagraph => { + queryContentModelBlocks( + group, + 'Paragraph', + (paragraph: ReadonlyContentModelParagraph): paragraph is ReadonlyContentModelParagraph => { for (const segment of paragraph.segments) { if ( segment.segmentType == 'Image' && @@ -28,8 +28,8 @@ export function findEditingImage( } return false; }, - findFirstOnly: true, - }); + true /*findFirstOnly*/ + ); return imageAndParagraph; }