@pie-lib/plot 5.0.8-next.20261102105711 → 5.0.8-next.20261102164954

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,11 @@
1
1
  import React from 'react';
2
2
  import PropTypes from 'prop-types';
3
3
  export declare const deltaFn: (scale: any, snap: any, val: any) => (delta: any) => number;
4
+ /**
5
+ * A read-only graph provides `true` to stop every mark under it from being dragged. Unlike the
6
+ * `disabled` prop it leaves the marks as they look: they only stop moving.
7
+ */
8
+ export declare const DragLockContext: React.Context<boolean>;
4
9
  /**
5
10
  * Creates a Component that is draggable, within a bounded grid.
6
11
  * @param {*} opts
@@ -8,20 +13,11 @@ export declare const deltaFn: (scale: any, snap: any, val: any) => (delta: any)
8
13
  export declare const gridDraggable: (opts: any) => (Comp: any) => {
9
14
  new (props: any): {
10
15
  nodeRef: React.RefObject<unknown>;
16
+ isLocked: any;
11
17
  grid: any;
12
18
  onStart: any;
13
19
  position: any;
14
- tiny: any;
15
20
  getScaledBounds: any;
16
- /**
17
- * Retrieves the coordinates of a mouse or touch event relative to an SVG element.
18
- * This method has been overwritten from the d3-selection library's clientPoint to handle touch events and improve clarity.
19
- * @param {Element} node - The SVG element.
20
- * @param {Event} event - The mouse or touch event.
21
- * @returns {Array} - An array containing the coordinates [x, y] relative to the SVG element.
22
- */
23
- getClientPoint: any;
24
- skipDragOutsideOfBounds: any;
25
21
  onDrag: any;
26
22
  getDelta: any;
27
23
  applyDelta: any;
@@ -55,6 +51,7 @@ export declare const gridDraggable: (opts: any) => (Comp: any) => {
55
51
  componentWillUpdate?(nextProps: Readonly<{}>, nextState: Readonly<{}>, nextContext: any): void;
56
52
  UNSAFE_componentWillUpdate?(nextProps: Readonly<{}>, nextState: Readonly<{}>, nextContext: any): void;
57
53
  };
54
+ contextType: React.Context<boolean>;
58
55
  propTypes: {
59
56
  disabled: PropTypes.Requireable<boolean>;
60
57
  onDragStart: PropTypes.Requireable<(...args: any[]) => any>;
@@ -87,5 +84,4 @@ export declare const gridDraggable: (opts: any) => (Comp: any) => {
87
84
  }>>>;
88
85
  }>>>;
89
86
  };
90
- contextType?: React.Context<any> | undefined;
91
87
  };
@@ -13,13 +13,14 @@ import u from "debug";
13
13
  var d = /* @__PURE__ */ e(n(), 1), f = u("pie-lib:plot:grid-draggable"), p = (e, t, n) => (r) => {
14
14
  let i = r + e(0);
15
15
  return t(n + e.invert(i)).toFixed(4) * 1e3 / 1e3;
16
- }, m = (e) => (n) => {
16
+ }, m = a.createContext(!1), h = (e) => (n) => {
17
17
  (0, d.default)(!!e && l(e.fromDelta) && l(e.bounds) && l(e.anchorPoint), "You must supply an object with: { anchorPoint: Function, fromDelta: Function, bounds: Function }");
18
18
  let u = ({ dragNodeRef: e, ...t }) => /* @__PURE__ */ c("g", {
19
19
  ref: e,
20
20
  children: /* @__PURE__ */ c(n, { ...t })
21
21
  });
22
22
  return class extends a.Component {
23
+ static contextType = m;
23
24
  static propTypes = {
24
25
  disabled: o.bool,
25
26
  onDragStart: o.func,
@@ -31,11 +32,9 @@ var d = /* @__PURE__ */ e(n(), 1), f = u("pie-lib:plot:grid-draggable"), p = (e,
31
32
  };
32
33
  nodeRef = a.createRef();
33
34
  constructor(e) {
34
- super(e), this.state = {
35
- startX: null,
36
- startY: null
37
- };
35
+ super(e), this.state = { dragging: !1 };
38
36
  }
37
+ isLocked = () => !!(this.props.disabled || this.context);
39
38
  grid = () => {
40
39
  let { graphProps: e } = this.props, { scale: t, domain: n, range: r } = e;
41
40
  return {
@@ -45,96 +44,76 @@ var d = /* @__PURE__ */ e(n(), 1), f = u("pie-lib:plot:grid-draggable"), p = (e,
45
44
  };
46
45
  onStart = (e) => {
47
46
  let { onDragStart: t } = this.props;
48
- document.activeElement && document.activeElement.blur(), this._didDrag = !1, this.setState({
49
- startX: e.clientX,
50
- startY: e.clientY
51
- });
47
+ document.activeElement && document.activeElement.blur(), this._didDrag = !1, this._startProps = this.props, this._bounds = this.getScaledBounds(this.props), this._total = {
48
+ x: 0,
49
+ y: 0
50
+ }, this.setState({ dragging: !0 });
52
51
  let n = e.target, r = (e) => {
53
52
  n.removeEventListener("click", r, !0), this._didDrag && (e.stopPropagation(), e.preventDefault());
54
53
  };
55
- n.addEventListener("click", r, !0), t && t();
54
+ n.addEventListener("click", r, !0), this._reportedStart = !!t && !this.isLocked(), this._reportedStart && t();
56
55
  };
57
- position = () => {
58
- let { x: t, y: n } = e.anchorPoint(this.props), { graphProps: r } = this.props, { scale: i, snap: a } = r;
56
+ position = (t = this.props) => {
57
+ let { x: n, y: r } = e.anchorPoint(t), { graphProps: i } = this.props, { scale: a, snap: o } = i;
59
58
  return {
60
59
  anchorPoint: {
61
- x: t,
62
- y: n
60
+ x: n,
61
+ y: r
63
62
  },
64
- x: p(i.x, a.x, t),
65
- y: p(i.y, a.y, n)
63
+ x: p(a.x, o.x, n),
64
+ y: p(a.y, o.y, r)
66
65
  };
67
66
  };
68
- tiny = (e, t) => {
69
- let n = e.toUpperCase(), r = t[`client${n}`], i = this.state[`start${n}`], a = Math.abs(r - i), o = a < Math.abs(this.grid()[e]) / 10;
70
- return f("[tiny] key: ", e, "delta: ", a, "out: ", o), o;
71
- };
72
- getScaledBounds = () => {
73
- let t = e.bounds(this.props, this.props.graphProps);
74
- f("bounds: ", t);
75
- let n = this.grid(), r = {
76
- left: t.left * n.x,
77
- right: t.right * n.x,
78
- top: t.top * n.y,
79
- bottom: t.bottom * n.y
80
- }, i = Math.abs(n.y);
81
- return r = {
82
- ...r,
83
- top: Math.min(0, r.top) - i,
84
- bottom: Math.abs(r.bottom) + i
85
- }, f("[getScaledBounds]: ", r), r;
86
- };
87
- getClientPoint = (e, t) => {
88
- if (!e || !t) return null;
89
- let n = e.ownerSVGElement || e;
90
- if (n && n.createSVGPoint) {
91
- let r = n.createSVGPoint();
92
- if (t.touches && t.touches.length > 0) {
93
- let e = t.touches[0];
94
- r.x = e.clientX, r.y = e.clientY;
95
- } else r.x = t.clientX, r.y = t.clientY;
96
- return e.getScreenCTM ? (r = r.matrixTransform(e.getScreenCTM().inverse()), [r.x, r.y]) : null;
97
- }
98
- let r = e.getBoundingClientRect();
99
- return r ? [t.clientX - r.left - e.clientLeft, t.clientY - r.top - e.clientTop] : null;
100
- };
101
- skipDragOutsideOfBounds = (e, t, n) => {
102
- let r = n.getRootNode(), i = this.getClientPoint(r, t);
103
- if (i === null) return !0;
104
- let [a, o] = i, { scale: s, domain: c, range: l } = n, u = s.x.invert(a), d = s.y.invert(o), f = e.deltaX > 0 && u < c.min || e.deltaX < 0 && u > c.max, p = e.deltaY > 0 && d > l.max || e.deltaY < 0 && d < l.min;
105
- return f || p;
67
+ getScaledBounds = (t = this.props) => {
68
+ let n = e.bounds(t, t.graphProps);
69
+ f("bounds: ", n);
70
+ let { scale: r } = t.graphProps, i = {
71
+ x: r.x(1) - r.x(0),
72
+ y: r.y(1) - r.y(0)
73
+ }, a = {
74
+ left: n.left * i.x,
75
+ right: n.right * i.x,
76
+ top: n.top * i.y,
77
+ bottom: n.bottom * i.y
78
+ };
79
+ return a = {
80
+ left: Math.min(0, a.left),
81
+ right: Math.max(0, a.right),
82
+ top: Math.min(0, a.top),
83
+ bottom: Math.abs(a.bottom)
84
+ }, f("[getScaledBounds]: ", a), a;
106
85
  };
107
86
  onDrag = (e, t) => {
108
- let { onDrag: n, graphProps: r, disabled: i } = this.props;
109
- if ((Math.abs(t.deltaX) > 1 || Math.abs(t.deltaY) > 1) && (this._didDrag = !0), !n || i) return;
110
- let a = this.getScaledBounds();
111
- if (t.deltaX < 0 && t.deltaX < a.left || t.deltaX > 0 && t.deltaX > a.right || t.deltaY < 0 && t.deltaY < a.top || t.deltaY > 0 && t.deltaY > a.bottom || this.skipDragOutsideOfBounds(t, e, r)) return;
112
- let o = this.applyDelta({
113
- x: t.deltaX,
114
- y: t.deltaY
115
- });
116
- (o !== void 0 || o !== null) && n(o);
87
+ let { onDrag: n } = this.props;
88
+ if ((Math.abs(t.deltaX) > 1 || Math.abs(t.deltaY) > 1) && (this._didDrag = !0), !n || this.isLocked()) return;
89
+ let r = this._startProps || this.props, i = this._total || {
90
+ x: 0,
91
+ y: 0
92
+ };
93
+ i.x += t.deltaX, i.y += t.deltaY, this._total = i;
94
+ let a = this._bounds || this.getScaledBounds(r), o = (e, t, n) => Math.min(Math.max(e, t), n), s = this.applyDelta({
95
+ x: o(i.x, a.left, a.right),
96
+ y: o(i.y, a.top, a.bottom)
97
+ }, r);
98
+ s != null && n(s);
117
99
  };
118
- getDelta = (e) => {
119
- let t = this.position(), n = {
120
- x: t.x(e.x),
121
- y: t.y(e.y)
100
+ getDelta = (e, t) => {
101
+ let n = this.position(t), i = {
102
+ x: n.x(e.x),
103
+ y: n.y(e.y)
122
104
  };
123
- return r(t.anchorPoint, n);
105
+ return r(n.anchorPoint, i);
124
106
  };
125
- applyDelta = (t) => {
126
- let n = this.getDelta(t);
127
- return f("[applyDelta] delta:", n), e.fromDelta(this.props, n);
107
+ applyDelta = (t, n = this.props) => {
108
+ let r = this.getDelta(t, n);
109
+ return f("[applyDelta] delta:", r), e.fromDelta(n, r);
128
110
  };
129
111
  onStop = (e, t) => {
130
112
  f("[onStop] dd:", t);
131
113
  let { onDragStop: n, onClick: r, disabled: i } = this.props;
132
- if (n && !i && n(), f("[onStop] lastX/Y: ", t.lastX, t.lastY), !this._didDrag) {
114
+ if (n && this._reportedStart && n(), this._reportedStart = !1, this._startProps = null, this._bounds = null, this._total = null, f("[onStop] lastX/Y: ", t.lastX, t.lastY), !this._didDrag) {
133
115
  if (!i && typeof e?.stopPropagation == "function" && e.stopPropagation(), r) {
134
- f("call onClick"), this.setState({
135
- startX: null,
136
- startY: null
137
- });
116
+ f("call onClick"), this.setState({ dragging: !1 });
138
117
  let { graphProps: t } = this.props, { scale: n, snap: i } = t;
139
118
  try {
140
119
  let [t, a] = s(e, e.target), o = n.x.invert(t), c = n.y.invert(a);
@@ -146,17 +125,11 @@ var d = /* @__PURE__ */ e(n(), 1), f = u("pie-lib:plot:grid-draggable"), p = (e,
146
125
  r({});
147
126
  }
148
127
  }
149
- this.setState({
150
- startX: null,
151
- startY: null
152
- });
153
- } else this.setState({
154
- startX: null,
155
- startY: null
156
- });
128
+ this.setState({ dragging: !1 });
129
+ } else this.setState({ dragging: !1 });
157
130
  };
158
131
  render() {
159
- let { disabled: t, onClick: n, ...r } = this.props, a = this.grid(), o = (e) => (e.nativeEvent || e).preventDefault(), s = this.state ? !!this.state.startX : !1;
132
+ let { disabled: t, onClick: n, ...r } = this.props, a = this.grid(), o = (e) => (e.nativeEvent || e).preventDefault(), s = !!this.state?.dragging;
160
133
  return /* @__PURE__ */ c(i, {
161
134
  nodeRef: this.nodeRef,
162
135
  onMouseDown: o,
@@ -177,4 +150,4 @@ var d = /* @__PURE__ */ e(n(), 1), f = u("pie-lib:plot:grid-draggable"), p = (e,
177
150
  };
178
151
  };
179
152
  //#endregion
180
- export { p as deltaFn, m as gridDraggable };
153
+ export { m as DragLockContext, p as deltaFn, h as gridDraggable };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import Root from './root.js';
2
2
  import Draggable from './draggable.js';
3
- import { gridDraggable } from './grid-draggable.js';
3
+ import { DragLockContext, gridDraggable } from './grid-draggable.js';
4
4
  import * as utils from './utils.js';
5
5
  import * as trig from './trig.js';
6
6
  import * as types from './types.js';
7
7
  import { create as createGraphProps } from './graph-props.js';
8
8
  import * as keyboard from './keyboard.js';
9
- export { Root, Draggable, gridDraggable, utils, trig, types, createGraphProps, keyboard };
9
+ export { Root, Draggable, DragLockContext, gridDraggable, utils, trig, types, createGraphProps, keyboard };
package/dist/index.js CHANGED
@@ -2,8 +2,8 @@ import { types_exports as e } from "./types.js";
2
2
  import { utils_exports as t } from "./utils.js";
3
3
  import { Root as n } from "./root.js";
4
4
  import r from "./draggable.js";
5
- import { gridDraggable as i } from "./grid-draggable.js";
6
- import { trig_exports as a } from "./trig.js";
7
- import { create as o } from "./graph-props.js";
8
- import { keyboard_exports as s } from "./keyboard.js";
9
- export { r as Draggable, n as Root, o as createGraphProps, i as gridDraggable, s as keyboard, a as trig, e as types, t as utils };
5
+ import { DragLockContext as i, gridDraggable as a } from "./grid-draggable.js";
6
+ import { trig_exports as o } from "./trig.js";
7
+ import { create as s } from "./graph-props.js";
8
+ import { keyboard_exports as c } from "./keyboard.js";
9
+ export { i as DragLockContext, r as Draggable, n as Root, s as createGraphProps, a as gridDraggable, c as keyboard, o as trig, e as types, t as utils };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pie-lib/plot",
3
- "version": "5.0.8-next.20261102105711",
3
+ "version": "5.0.8-next.20261102164954",
4
4
  "description": "Some underlying components for building charts/graphs",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,8 +14,8 @@
14
14
  "@mui/icons-material": "^7.3.4",
15
15
  "@mui/material": "^7.3.4",
16
16
  "@pie-element/shared-utils": "0.1.7-next.20261027193325",
17
- "@pie-lib/editable-html-tip-tap": "3.1.3-next.20261102105711",
18
- "@pie-lib/render-ui": "8.0.8-next.20261027193325",
17
+ "@pie-lib/editable-html-tip-tap": "3.1.3-next.20261102164954",
18
+ "@pie-lib/render-ui": "8.0.8-next.20261102164954",
19
19
  "assert": "^2.1.0",
20
20
  "d3-scale": "^4.0.2",
21
21
  "d3-selection": "^3.0.0",