Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public normalizeRect function #2482

Merged
merged 4 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNodeOfType } from 'roosterjs-content-model-dom';
import { isNodeOfType, normalizeRect } from 'roosterjs-content-model-dom';
import { Rect } from 'roosterjs-content-model-types';

/**
Expand Down Expand Up @@ -59,16 +59,3 @@ export function getPositionRect(container: Node, offset: number): Rect | null {

return null;
}

function normalizeRect(clientRect: DOMRect): Rect | null {
const { left, right, top, bottom } =
clientRect || <DOMRect>{ left: 0, right: 0, top: 0, bottom: 0 };
return left === 0 && right === 0 && top === 0 && bottom === 0
? null
: {
left: Math.round(left),
right: Math.round(right),
top: Math.round(top),
bottom: Math.round(bottom),
};
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { normalizeRect } from 'roosterjs-content-model-dom';
import type { GetVisibleViewport, Rect } from 'roosterjs-content-model-types';

/**
Expand Down Expand Up @@ -55,16 +56,3 @@ function getIntersectedRect(elements: HTMLElement[], additionalRects: Rect[] = [

return result.top < result.bottom && result.left < result.right ? result : null;
}

function normalizeRect(clientRect: DOMRect): Rect | null {
const { left, right, top, bottom } =
clientRect || <DOMRect>{ left: 0, right: 0, top: 0, bottom: 0 };
return left === 0 && right === 0 && top === 0 && bottom === 0
? null
: {
left: Math.round(left),
right: Math.round(right),
top: Math.round(top),
bottom: Math.round(bottom),
};
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Rect } from 'roosterjs-content-model-types';

/**
* @internal
* A ClientRect of all 0 is possible. i.e. chrome returns a ClientRect of 0 when the cursor is on an empty p
* We validate that and only return a rect when the passed in ClientRect is valid
* @param clientRect Client rect object normally retrieved from getBoundingClientRect function
*/
export function normalizeRect(clientRect: DOMRect): Rect | null {
const { left, right, top, bottom } =
Expand Down
1 change: 1 addition & 0 deletions packages/roosterjs-content-model-dom/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export {
} from './domUtils/entityUtils';
export { reuseCachedElement } from './domUtils/reuseCachedElement';
export { isWhiteSpacePreserved } from './domUtils/isWhiteSpacePreserved';
export { normalizeRect } from './domUtils/normalizeRect';

export { createBr } from './modelApi/creators/createBr';
export { createListItem } from './modelApi/creators/createListItem';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { normalizeRect } from '../../lib/domUtils/normalizeRect';

describe('normalizeRect', () => {
it('valid rect - all have positive values', () => {
const rect: DOMRect = {
left: 1,
right: 2,
top: 3,
bottom: 4,
} as any;

const result = normalizeRect(rect);

expect(result).toEqual({
left: 1,
right: 2,
top: 3,
bottom: 4,
});
});

it('valid rect - some have 0 values', () => {
const rect: DOMRect = {
left: 1,
right: 0,
top: 0,
bottom: 0,
} as any;

const result = normalizeRect(rect);

expect(result).toEqual({
left: 1,
right: 0,
top: 0,
bottom: 0,
});
});

it('invalid rect', () => {
const rect: DOMRect = {
left: 0,
right: 0,
top: 0,
bottom: 0,
} as any;

const result = normalizeRect(rect);

expect(result).toBeNull();
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { normalizeRect } from './normalizeRect';
import { normalizeRect } from 'roosterjs-content-model-dom';
import type { Rect } from 'roosterjs-content-model-types';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { isNodeOfType } from 'roosterjs-content-model-dom';
import { normalizeRect } from '../pluginUtils/Rect/normalizeRect';
import { isNodeOfType, normalizeRect } from 'roosterjs-content-model-dom';
import { TableEditor } from './editors/TableEditor';
import type { EditorPlugin, IEditor, PluginEvent, Rect } from 'roosterjs-content-model-types';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { createTableInserter } from './features/TableInserter';
import { createTableMover } from './features/TableMover';
import { createTableResizer } from './features/TableResizer';
import { disposeTableEditFeature } from './features/TableEditFeature';
import { isNodeOfType } from 'roosterjs-content-model-dom';
import { normalizeRect } from '../../pluginUtils/Rect/normalizeRect';
import { isNodeOfType, normalizeRect } from 'roosterjs-content-model-dom';
import type { TableEditFeature } from './features/TableEditFeature';
import type { IEditor, TableSelection } from 'roosterjs-content-model-types';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createElement } from '../../../pluginUtils/CreateElement/createElement';
import { DragAndDropHelper } from '../../../pluginUtils/DragAndDrop/DragAndDropHelper';
import { isElementOfType } from 'roosterjs-content-model-dom';
import { normalizeRect } from '../../../pluginUtils/Rect/normalizeRect';
import { isElementOfType, normalizeRect } from 'roosterjs-content-model-dom';
import {
getFirstSelectedTable,
MIN_ALLOWED_TABLE_CELL_WIDTH,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createElement } from '../../../pluginUtils/CreateElement/createElement';
import { getIntersectedRect } from '../../../pluginUtils/Rect/getIntersectedRect';
import { isElementOfType } from 'roosterjs-content-model-dom';
import { normalizeRect } from '../../../pluginUtils/Rect/normalizeRect';
import { isElementOfType, normalizeRect } from 'roosterjs-content-model-dom';
import {
formatTableWithContentModel,
insertTableColumn,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createElement } from '../../../pluginUtils/CreateElement/createElement';
import { DragAndDropHelper } from '../../../pluginUtils/DragAndDrop/DragAndDropHelper';
import { isNodeOfType } from 'roosterjs-content-model-dom';
import { normalizeRect } from '../../../pluginUtils/Rect/normalizeRect';
import { isNodeOfType, normalizeRect } from 'roosterjs-content-model-dom';
import type { DragAndDropHandler } from '../../../pluginUtils/DragAndDrop/DragAndDropHandler';
import type { IEditor, Rect } from 'roosterjs-content-model-types';
import type { TableEditFeature } from './TableEditFeature';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { createElement } from '../../../pluginUtils/CreateElement/createElement';
import { DragAndDropHelper } from '../../../pluginUtils/DragAndDrop/DragAndDropHelper';
import { getFirstSelectedTable, normalizeTable } from 'roosterjs-content-model-core';
import { isNodeOfType } from 'roosterjs-content-model-dom';
import { normalizeRect } from '../../../pluginUtils/Rect/normalizeRect';
import { isNodeOfType, normalizeRect } from 'roosterjs-content-model-dom';
import type { ContentModelTable, IEditor, Rect } from 'roosterjs-content-model-types';
import type { TableEditFeature } from './TableEditFeature';

Expand Down
Loading