Skip to content

Commit 83c65ec

Browse files
committed
Patch: reduced bundle size by 300kb
1 parent ffebf83 commit 83c65ec

File tree

6 files changed

+37
-32
lines changed

6 files changed

+37
-32
lines changed

src/components/workspace/crosshairs.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { memo, useEffect, useState } from "react";
2-
import * as d3 from "d3";
2+
import { pointer } from "d3";
33
import { ids } from "@/constants";
44

55
export const Crosshairs = ({ render }) => {
@@ -8,9 +8,9 @@ export const Crosshairs = ({ render }) => {
88
const [enabled, setEnabled] = useState(false);
99

1010
const move = (e: Event) => {
11-
const pointer = d3.pointer(e);
12-
setX(pointer[0]);
13-
setY(pointer[1]);
11+
const ptr = pointer(e);
12+
setX(ptr[0]);
13+
setY(ptr[1]);
1414
};
1515

1616
useEffect(() => {

src/components/workspace/cursor.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect, useState } from "react";
22
import { useSelector } from "react-redux";
3-
import * as d3 from "d3";
3+
import { pointer } from "d3";
44
import { ids } from "@/constants";
55
import { resizeCursors } from "@/hooks/interactions";
66
import { isWithinBounds } from "@/utils";
@@ -16,9 +16,9 @@ export const Cursor = () => {
1616
const Cursor = useSelector((state: any) => state.editor.cursor);
1717

1818
const move = (e) => {
19-
const pointer = d3.pointer(e);
20-
const x = pointer[0];
21-
const y = pointer[1];
19+
const ptr = pointer(e);
20+
const x = ptr[0];
21+
const y = ptr[1];
2222
const workspace = document.getElementById(ids.workspace)?.getBoundingClientRect();
2323
const zoomControls = document.getElementById(ids.zoomControls)?.getBoundingClientRect();
2424
const panControls = document.getElementById(ids.panControls)?.getBoundingClientRect();

src/components/workspace/elements/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { memo, useEffect, useMemo, useRef } from "react";
2-
import * as d3 from "d3";
2+
import { select } from "d3";
33
import { default as isEqual } from "lodash/isEqual";
44
import { twMerge } from "tailwind-merge";
55
import { dataAttributes } from "@/constants";
@@ -41,7 +41,7 @@ export const Element: React.FC<IElementProps> = ({
4141

4242
useEffect(() => {
4343
if (!ref.current || consumer.mode !== "designer") return;
44-
const node = d3.select(ref.current);
44+
const node = select(ref.current);
4545
if (type === ElementType.Seat) {
4646
handleSeatDrag(node);
4747
} else if (type === ElementType.Text) {

src/components/workspace/elements/utils.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as d3 from "d3";
1+
import { drag, select } from "d3";
22
import { dataAttributes } from "@/constants";
33
import { resizeCursors } from "@/hooks/interactions";
44
import { IPopulatedSeat } from "@/types";
@@ -28,8 +28,8 @@ export const elements = {
2828
[ElementType.Image]: Image
2929
};
3030

31-
export const handleDrag = d3.drag().on("drag", function (event) {
32-
const me = d3.select(this);
31+
export const handleDrag = drag().on("drag", function (event) {
32+
const me = select(this);
3333
const controls = d3Extended.selectById(`${me.attr("id")}-controls`);
3434
const x = +me.attr("x") + event.dx;
3535
const y = +me.attr("y") + event.dy;
@@ -40,8 +40,8 @@ export const handleDrag = d3.drag().on("drag", function (event) {
4040
controls.attr("cy", center.y);
4141
});
4242

43-
export const handleSeatDrag = d3.drag().on("drag", function (event) {
44-
const me = d3.select(this);
43+
export const handleSeatDrag = drag().on("drag", function (event) {
44+
const me = select(this);
4545

4646
const x = +me.attr("cx") + event.dx;
4747
const y = +me.attr("cy") + event.dy;
@@ -58,23 +58,23 @@ export const handleSeatDrag = d3.drag().on("drag", function (event) {
5858
label.attr("y", +label.attr("y") + event.dy);
5959
});
6060

61-
export const handleTextDrag = d3.drag().on("drag", function (event) {
62-
const me = d3.select(this);
61+
export const handleTextDrag = drag().on("drag", function (event) {
62+
const me = select(this);
6363
me.attr("x", +me.attr("x") + event.dx);
6464
me.attr("y", +me.attr("y") + event.dy);
6565
});
6666

67-
export const handleShapeDrag = d3.drag().on("drag", function (event) {
68-
const me = d3.select(this);
67+
export const handleShapeDrag = drag().on("drag", function (event) {
68+
const me = select(this);
6969
if (resizeCursors.includes(me.style("cursor"))) return;
7070
const x = +me.attr("x") + event.dx;
7171
const y = +me.attr("y") + event.dy;
7272
me.attr("x", x);
7373
me.attr("y", y);
7474
});
7575

76-
export const handlePolylineDrag = d3.drag().on("drag", function (event) {
77-
const me = d3.select(this);
76+
export const handlePolylineDrag = drag().on("drag", function (event) {
77+
const me = select(this);
7878
const points = me
7979
.attr("points")
8080
.split(" ")

src/utils/d3.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as d3 from "d3";
1+
import { pointer, select, selectAll, selection, zoom, zoomIdentity, zoomTransform } from "d3";
22

33
declare module "d3" {
44
interface Selection<GElement extends d3.BaseType, Datum, PElement extends d3.BaseType, PDatum> {
@@ -9,13 +9,13 @@ declare module "d3" {
99
}
1010
}
1111

12-
d3.selection.prototype.moveToFront = function () {
12+
selection.prototype.moveToFront = function () {
1313
return this.each(function () {
1414
this.parentNode.appendChild(this);
1515
});
1616
};
1717

18-
d3.selection.prototype.moveToBack = function () {
18+
selection.prototype.moveToBack = function () {
1919
return this.each(function () {
2020
const firstChild = this.parentNode.firstChild;
2121
if (firstChild) {
@@ -24,24 +24,29 @@ d3.selection.prototype.moveToBack = function () {
2424
});
2525
};
2626

27-
d3.selection.prototype.map = function (callback) {
27+
selection.prototype.map = function (callback) {
2828
const results = [];
2929
this.each(function (_, i) {
30-
results.push(callback(d3.select(this), i));
30+
results.push(callback(select(this), i));
3131
});
3232
return results;
3333
};
3434

35-
d3.selection.prototype.forEach = function (callback) {
35+
selection.prototype.forEach = function (callback) {
3636
this.each(function (_, i) {
37-
callback(d3.select(this), i);
37+
callback(select(this), i);
3838
});
3939
};
4040

4141
export const d3Extended = {
42-
...d3,
42+
pointer,
43+
select,
44+
selectAll,
45+
zoom,
46+
zoomIdentity,
47+
zoomTransform,
4348
selectById(id: string): d3.Selection<Element, {}, HTMLElement, any> {
44-
return d3.select(`[id='${id}']`);
49+
return select(`[id='${id}']`);
4550
},
4651
getNodeCenter(node: any) {
4752
if (node.attr("cx")) {

src/utils/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as d3 from "d3";
1+
import { zoomTransform } from "d3";
22
import { ids, selectors } from "@/constants";
33

44
export * from "./d3";
@@ -35,7 +35,7 @@ export const getRelativeWorkspaceClickCoords = (e: any) => {
3535

3636
export const getRelativeClickCoordsWithTransform = (e: any) => {
3737
const coords = getRelativeWorkspaceClickCoords(e);
38-
const transform = d3.zoomTransform(document.querySelector(selectors.workspaceGroup));
38+
const transform = zoomTransform(document.querySelector(selectors.workspaceGroup));
3939
return {
4040
x: (coords.x - transform.x) / transform.k,
4141
y: (coords.y - transform.y) / transform.k

0 commit comments

Comments
 (0)