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

chore: remove duplicate utils offset of DOM element #1203

Merged
merged 1 commit into from
Nov 21, 2023
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
13 changes: 0 additions & 13 deletions packages/common/src/core/slickCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,19 +672,6 @@ export class Utils {

public static noop() { }

public static offset(el: HTMLElement | null) {
if (!el || !el.getBoundingClientRect) {
return undefined;
}
const box = el.getBoundingClientRect();
const docElem = document.documentElement;

return {
top: box.top + window.pageYOffset - docElem.clientTop,
left: box.left + window.pageXOffset - docElem.clientLeft
};
}

public static width(el: HTMLElement, value?: number | string): number | void {
if (!el || !el.getBoundingClientRect) { return; }
if (value === undefined) {
Expand Down
12 changes: 6 additions & 6 deletions packages/common/src/core/slickGrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ import type {
SlickPlugin,
SlickGridEventData,
} from '../interfaces';
import { createDomElement, emptyElement } from '../services/domUtilities';
import { createDomElement, emptyElement, getHtmlElementOffset } from '../services/domUtilities';

/**
* @license
Expand Down Expand Up @@ -1752,13 +1752,13 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
scroll: !this.hasFrozenColumns(), // enable auto-scroll
onStart: (e: { item: any; originalEvent: MouseEvent; }) => {
canDragScroll = !this.hasFrozenColumns() ||
Utils.offset(e.item)!.left > Utils.offset(this._viewportScrollContainerX)!.left;
getHtmlElementOffset(e.item)!.left > getHtmlElementOffset(this._viewportScrollContainerX)!.left;

if (canDragScroll && e.originalEvent.pageX > this._container.clientWidth) {
if (!(columnScrollTimer)) {
columnScrollTimer = setInterval(scrollColumnsRight, 100);
}
} else if (canDragScroll && e.originalEvent.pageX < Utils.offset(this._viewportScrollContainerX)!.left) {
} else if (canDragScroll && e.originalEvent.pageX < getHtmlElementOffset(this._viewportScrollContainerX)!.left) {
if (!(columnScrollTimer)) {
columnScrollTimer = setInterval(scrollColumnsLeft, 100);
}
Expand Down Expand Up @@ -5048,7 +5048,7 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e

if (this.hasFrozenRows) {
let rowOffset = 0;
const c = Utils.offset(Utils.parents(cellNode, '.grid-canvas')[0] as HTMLElement);
const c = getHtmlElementOffset(Utils.parents(cellNode, '.grid-canvas')[0] as HTMLElement);
const isBottom = Utils.parents(cellNode, '.grid-canvas-bottom').length;

if (isBottom) {
Expand Down Expand Up @@ -5166,8 +5166,8 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
this.activeCellNode = newCell;

if (isDefined(this.activeCellNode)) {
const activeCellOffset = Utils.offset(this.activeCellNode);
let rowOffset = Math.floor(Utils.offset(Utils.parents(this.activeCellNode, '.grid-canvas')[0] as HTMLElement)!.top);
const activeCellOffset = getHtmlElementOffset(this.activeCellNode);
let rowOffset = Math.floor(getHtmlElementOffset(Utils.parents(this.activeCellNode, '.grid-canvas')[0] as HTMLElement)!.top);
const isBottom = Utils.parents(this.activeCellNode, '.grid-canvas-bottom').length;

if (this.hasFrozenRows && isBottom) {
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/editors/longTextEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export class LongTextEditor implements Editor {
* Same goes for the top/bottom position, Most of the time positioning the editor to the "bottom" but we are clicking on a cell at the bottom of the grid then we might need to reposition to "top" instead.
* NOTE: this only applies to Inline Editing and will not have any effect when using the Composite Editor modal window.
*/
position(parentPosition: HtmlElementPosition) {
position(parentPosition: Partial<HtmlElementPosition>) {
const containerOffset = getHtmlElementOffset(this.args.container);
const containerHeight = this.args.container.offsetHeight;
const containerWidth = this.args.container.offsetWidth;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface HtmlElementPosition {
top?: number;
bottom?: number;
left?: number;
right?: number;
top: number;
bottom: number;
left: number;
right: number;
}
18 changes: 10 additions & 8 deletions packages/common/src/services/domUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,23 @@ export function getElementOffsetRelativeToParent(parentElm: HTMLElement | null,
}

/** Get HTML element offset with pure JS */
export function getHtmlElementOffset(element?: HTMLElement | null): HtmlElementPosition | undefined {
if (!element) {
export function getHtmlElementOffset(elm?: HTMLElement | null): HtmlElementPosition | undefined {
if (!elm || !elm.getBoundingClientRect) {
return undefined;
}
const rect = element?.getBoundingClientRect?.();
const box = elm.getBoundingClientRect();
const docElem = document.documentElement;

let top = 0;
let left = 0;
let bottom = 0;
let right = 0;

if (rect?.top !== undefined && rect.left !== undefined) {
top = rect.top + window.pageYOffset;
left = rect.left + window.pageXOffset;
right = rect.right;
bottom = rect.bottom;
if (box?.top !== undefined && box.left !== undefined) {
top = box.top + window.pageYOffset - docElem.clientTop;
left = box.left + window.pageXOffset - docElem.clientLeft;
right = box.right;
bottom = box.bottom;
}
return { top, left, bottom, right };
}
Expand Down