Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaroldi committed Nov 5, 2024
1 parent 6370fde commit c172ca4
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 526 deletions.
22 changes: 22 additions & 0 deletions demo/scripts/controlsV2/demoButtons/cutButton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { RibbonButton } from 'roosterjs-react';

/**
* Key of localized strings of Cut button
*/
export type CutButtonStringKey = 'buttonNameCut';

/**
* "Cut" button on the format ribbon
*/
export const cutButton: RibbonButton<CutButtonStringKey> = {
key: 'buttonNameCut',
unlocalizedText: ' Cut',
iconName: 'ClearNight',
onClick: editor => {
const selection = editor.getDOMSelection();
if (selection) {
document.execCommand('cut');
}
return true;
},
};
5 changes: 4 additions & 1 deletion packages/roosterjs-content-model-api/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@ 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 { queryContentModel, QueryContentModelOptions } from './modelApi/common/queryContentModel';
export {
queryContentModelBlocks,
QueryContentModelOptions,
} from './modelApi/common/queryContentModelBlocks';

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import type {
ContentModelBlockType,
ReadonlyContentModelBlock,
ReadonlyContentModelBlockGroup,
ReadonlyContentModelTable,
} 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
*/
export function queryContentModelBlocks<T extends ReadonlyContentModelBlock>(
group: ReadonlyContentModelBlockGroup,
options: QueryContentModelOptions<T>
): T[] {
const { blockType, filter, findFirstOnly } = options;
const type = blockType || 'Paragraph';

return queryContentModelBlocksInternal<T>(group, type, filter, findFirstOnly);
}

function queryContentModelBlocksInternal<T extends ReadonlyContentModelBlock>(
group: ReadonlyContentModelBlockGroup,
type: ContentModelBlockType,
filter?: (element: T) => element is T,
findFirstOnly?: boolean
): T[] {
const elements: T[] = [];
for (let i = 0; i < group.blocks.length; i++) {
if (findFirstOnly && elements.length > 0) {
return elements;
}
const block = group.blocks[i];
switch (block.blockType) {
case 'BlockGroup':
if (isBlockType<T>(block, type) && (!filter || filter(block))) {
elements.push(block);
}
const blockGroupsResults = queryContentModelBlocksInternal<T>(
block,
type,
filter,
findFirstOnly
);
elements.push(...blockGroupsResults);
break;
case 'Table':
if (isBlockType<T>(block, type) && (!filter || filter(block))) {
elements.push(block);
}
const tableResults = searchInTables(block, type, filter, findFirstOnly);
elements.push(...tableResults);
break;
case 'Divider':
case 'Entity':
case 'Paragraph':
if (isBlockType<T>(block, type) && (!filter || filter(block))) {
elements.push(block);
}
break;
}
}
return elements;
}

function isBlockType<T extends ReadonlyContentModelBlock>(
block: ReadonlyContentModelBlock,
type: string
): block is T {
return block.blockType == type;
}

function searchInTables<T extends ReadonlyContentModelBlock>(
table: ReadonlyContentModelTable,
type: ContentModelBlockType,
filter?: (element: T) => element is T,
findFirstOnly?: boolean
): T[] {
const blocks: T[] = [];
for (const row of table.rows) {
for (const cell of row.cells) {
const items = queryContentModelBlocksInternal<T>(cell, type, filter, findFirstOnly);
blocks.push(...items);
}
}
return blocks;
}
Loading

0 comments on commit c172ca4

Please sign in to comment.