@knotx/plugins-canvas 0.4.2 → 0.4.3

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.
package/dist/index.cjs CHANGED
@@ -14,19 +14,21 @@ function calculateTransform({
14
14
  scale,
15
15
  block = "nearest",
16
16
  inline = "nearest",
17
- offset = 0
17
+ offset = 0,
18
+ contentSize,
19
+ limitToBounds
18
20
  }) {
19
- var _a, _b, _c, _d, _e, _f;
20
- const nodeX = node.position.x;
21
- const nodeY = node.position.y;
22
- const nodeWidth = (_b = (_a = node.measured) == null ? void 0 : _a.width) != null ? _b : 0;
23
- const nodeHeight = (_d = (_c = node.measured) == null ? void 0 : _c.height) != null ? _d : 0;
21
+ var _a, _b, _c, _d, _e, _f, _g, _h;
22
+ const nodeX = (_a = node == null ? void 0 : node.position.x) != null ? _a : 0;
23
+ const nodeY = (_b = node == null ? void 0 : node.position.y) != null ? _b : 0;
24
+ const nodeWidth = (_d = (_c = node == null ? void 0 : node.measured) == null ? void 0 : _c.width) != null ? _d : 0;
25
+ const nodeHeight = (_f = (_e = node == null ? void 0 : node.measured) == null ? void 0 : _e.height) != null ? _f : 0;
24
26
  const viewportWidth = container.width;
25
27
  const viewportHeight = container.height;
26
28
  let positionX = -transform.positionX;
27
29
  let positionY = -transform.positionY;
28
- const offsetX = typeof offset === "number" ? offset : (_e = offset.x) != null ? _e : 0;
29
- const offsetY = typeof offset === "number" ? offset : (_f = offset.y) != null ? _f : 0;
30
+ const offsetX = typeof offset === "number" ? offset : (_g = offset.x) != null ? _g : 0;
31
+ const offsetY = typeof offset === "number" ? offset : (_h = offset.y) != null ? _h : 0;
30
32
  switch (inline) {
31
33
  case "center":
32
34
  positionX = (nodeX + nodeWidth / 2) * scale - viewportWidth / 2;
@@ -73,6 +75,14 @@ function calculateTransform({
73
75
  break;
74
76
  }
75
77
  }
78
+ if (limitToBounds && contentSize) {
79
+ if (contentSize.width * scale < viewportWidth) {
80
+ positionX = contentSize.width / 2 * scale - viewportWidth / 2;
81
+ }
82
+ if (contentSize.height * scale < viewportHeight) {
83
+ positionY = contentSize.height / 2 * scale - viewportHeight / 2;
84
+ }
85
+ }
76
86
  return {
77
87
  scale,
78
88
  positionX,
@@ -286,10 +296,12 @@ class Canvas extends (_a = core.BasePlugin, _transform_dec = [decorators.registe
286
296
  node,
287
297
  container: this.container,
288
298
  transform: this.transform,
299
+ contentSize: this.contentSize,
289
300
  scale,
290
301
  block,
291
302
  inline,
292
- offset
303
+ offset,
304
+ limitToBounds: false
293
305
  });
294
306
  (_a2 = this.ref) == null ? void 0 : _a2.setTransform(-positionX, -positionY, newScale, animationTime);
295
307
  }
@@ -312,21 +324,19 @@ class Canvas extends (_a = core.BasePlugin, _transform_dec = [decorators.registe
312
324
  render({ children } = { children: null }) {
313
325
  var _a2, _b, _c, _d;
314
326
  const defaultTransform = react.useMemo(() => {
315
- const defaultLocated = this.config.defaultLocated;
327
+ const defaultLocated = __spreadValues({}, this.config.defaultLocated);
316
328
  if (!(defaultLocated == null ? void 0 : defaultLocated.nodeId)) {
317
329
  return this.transform;
318
330
  }
319
331
  const node = this.getNode({ id: defaultLocated.nodeId });
320
- if (!node) {
321
- return this.transform;
322
- }
323
332
  defaultLocated.inline || (defaultLocated.inline = "center");
324
333
  defaultLocated.block || (defaultLocated.block = "center");
325
334
  defaultLocated.scale || (defaultLocated.scale = this.transform.scale);
326
335
  const { positionX, positionY, scale: newScale } = calculateTransform(__spreadValues({
327
336
  node,
328
337
  container: this.container,
329
- transform: this.transform
338
+ transform: this.transform,
339
+ contentSize: this.contentSize
330
340
  }, defaultLocated));
331
341
  const newTransform = {
332
342
  previousScale: this.transform.scale,
package/dist/index.d.cts CHANGED
@@ -1,33 +1,40 @@
1
1
  import { Element, BasePlugin, DOMElement, Engine } from '@knotx/core';
2
2
  import { InferParamsFromSchema } from '@knotx/decorators';
3
3
  import { MouseEvent, ReactNode } from 'react';
4
- import { ReactZoomPanPinchState, ReactZoomPanPinchProps, ReactZoomPanPinchContentRef } from 'react-zoom-pan-pinch';
4
+ import { ReactZoomPanPinchProps, ReactZoomPanPinchContentRef } from 'react-zoom-pan-pinch';
5
5
 
6
- type CanvasConfig = ReactZoomPanPinchProps & {
7
- defaultLocated?: {
8
- nodeId: string;
9
- scale?: number;
10
- block?: 'center' | 'end' | 'nearest' | 'start';
11
- inline?: 'center' | 'end' | 'nearest' | 'start';
12
- offset?: number | {
13
- x: number;
14
- y: number;
15
- };
16
- animationTime?: number;
17
- };
18
- };
6
+ interface CanvasTransformState {
7
+ previousScale: number;
8
+ scale: number;
9
+ positionX: number;
10
+ positionY: number;
11
+ }
19
12
  interface CanvasContentSize {
20
13
  width: number;
21
14
  height: number;
22
15
  }
23
- type CanvasTransformState = ReactZoomPanPinchState;
24
- type CanvasRef = ReactZoomPanPinchContentRef;
16
+ interface ScrollNodeIntoViewOptions {
17
+ nodeId: string;
18
+ scale?: number;
19
+ block?: 'center' | 'end' | 'nearest' | 'start';
20
+ inline?: 'center' | 'end' | 'nearest' | 'start';
21
+ offset?: {
22
+ x: number;
23
+ y: number;
24
+ };
25
+ animationTime?: number;
26
+ limitToBounds?: boolean;
27
+ }
25
28
  interface EdgeScrollConfig {
26
29
  enabled: boolean;
27
30
  edgeSize: number;
28
31
  maxSpeed: number;
29
32
  cornerSize?: number;
30
33
  }
34
+
35
+ type CanvasConfig = ReactZoomPanPinchProps & {
36
+ defaultLocated?: ScrollNodeIntoViewOptions;
37
+ };
31
38
  declare module '@knotx/core' {
32
39
  interface PluginData {
33
40
  canvas: {
@@ -45,17 +52,7 @@ declare module '@knotx/core' {
45
52
  }
46
53
  interface PluginTools {
47
54
  canvas: {
48
- scrollNodeIntoView: (params: {
49
- nodeId: string;
50
- scale?: number;
51
- block?: 'center' | 'end' | 'nearest' | 'start';
52
- inline?: 'center' | 'end' | 'nearest' | 'start';
53
- offset?: {
54
- x: number;
55
- y: number;
56
- };
57
- animationTime?: number;
58
- }) => void;
55
+ scrollNodeIntoView: (params: ScrollNodeIntoViewOptions) => void;
59
56
  zoomIn: (params: {
60
57
  step?: number;
61
58
  animationTime?: number;
@@ -81,7 +78,7 @@ type CanvasEventListener = (event: MouseEvent<HTMLElement>) => void;
81
78
  declare class Canvas extends BasePlugin<'canvas', CanvasConfig> {
82
79
  private config;
83
80
  name: "canvas";
84
- ref: CanvasRef | null;
81
+ ref: ReactZoomPanPinchContentRef | null;
85
82
  transform: CanvasTransformState;
86
83
  rootElement: DOMElement | null;
87
84
  contentSize: CanvasContentSize | undefined;
@@ -187,4 +184,4 @@ declare class Canvas extends BasePlugin<'canvas', CanvasConfig> {
187
184
  private isCanvasEvent;
188
185
  }
189
186
 
190
- export { Canvas, type CanvasConfig, type CanvasContentSize, type CanvasEventListener, type CanvasEventType, type CanvasRef, type CanvasTransformState, type EdgeScrollConfig };
187
+ export { Canvas, type CanvasConfig, type CanvasContentSize, type CanvasEventListener, type CanvasEventType, type CanvasTransformState, type EdgeScrollConfig, type ScrollNodeIntoViewOptions };
package/dist/index.d.mts CHANGED
@@ -1,33 +1,40 @@
1
1
  import { Element, BasePlugin, DOMElement, Engine } from '@knotx/core';
2
2
  import { InferParamsFromSchema } from '@knotx/decorators';
3
3
  import { MouseEvent, ReactNode } from 'react';
4
- import { ReactZoomPanPinchState, ReactZoomPanPinchProps, ReactZoomPanPinchContentRef } from 'react-zoom-pan-pinch';
4
+ import { ReactZoomPanPinchProps, ReactZoomPanPinchContentRef } from 'react-zoom-pan-pinch';
5
5
 
6
- type CanvasConfig = ReactZoomPanPinchProps & {
7
- defaultLocated?: {
8
- nodeId: string;
9
- scale?: number;
10
- block?: 'center' | 'end' | 'nearest' | 'start';
11
- inline?: 'center' | 'end' | 'nearest' | 'start';
12
- offset?: number | {
13
- x: number;
14
- y: number;
15
- };
16
- animationTime?: number;
17
- };
18
- };
6
+ interface CanvasTransformState {
7
+ previousScale: number;
8
+ scale: number;
9
+ positionX: number;
10
+ positionY: number;
11
+ }
19
12
  interface CanvasContentSize {
20
13
  width: number;
21
14
  height: number;
22
15
  }
23
- type CanvasTransformState = ReactZoomPanPinchState;
24
- type CanvasRef = ReactZoomPanPinchContentRef;
16
+ interface ScrollNodeIntoViewOptions {
17
+ nodeId: string;
18
+ scale?: number;
19
+ block?: 'center' | 'end' | 'nearest' | 'start';
20
+ inline?: 'center' | 'end' | 'nearest' | 'start';
21
+ offset?: {
22
+ x: number;
23
+ y: number;
24
+ };
25
+ animationTime?: number;
26
+ limitToBounds?: boolean;
27
+ }
25
28
  interface EdgeScrollConfig {
26
29
  enabled: boolean;
27
30
  edgeSize: number;
28
31
  maxSpeed: number;
29
32
  cornerSize?: number;
30
33
  }
34
+
35
+ type CanvasConfig = ReactZoomPanPinchProps & {
36
+ defaultLocated?: ScrollNodeIntoViewOptions;
37
+ };
31
38
  declare module '@knotx/core' {
32
39
  interface PluginData {
33
40
  canvas: {
@@ -45,17 +52,7 @@ declare module '@knotx/core' {
45
52
  }
46
53
  interface PluginTools {
47
54
  canvas: {
48
- scrollNodeIntoView: (params: {
49
- nodeId: string;
50
- scale?: number;
51
- block?: 'center' | 'end' | 'nearest' | 'start';
52
- inline?: 'center' | 'end' | 'nearest' | 'start';
53
- offset?: {
54
- x: number;
55
- y: number;
56
- };
57
- animationTime?: number;
58
- }) => void;
55
+ scrollNodeIntoView: (params: ScrollNodeIntoViewOptions) => void;
59
56
  zoomIn: (params: {
60
57
  step?: number;
61
58
  animationTime?: number;
@@ -81,7 +78,7 @@ type CanvasEventListener = (event: MouseEvent<HTMLElement>) => void;
81
78
  declare class Canvas extends BasePlugin<'canvas', CanvasConfig> {
82
79
  private config;
83
80
  name: "canvas";
84
- ref: CanvasRef | null;
81
+ ref: ReactZoomPanPinchContentRef | null;
85
82
  transform: CanvasTransformState;
86
83
  rootElement: DOMElement | null;
87
84
  contentSize: CanvasContentSize | undefined;
@@ -187,4 +184,4 @@ declare class Canvas extends BasePlugin<'canvas', CanvasConfig> {
187
184
  private isCanvasEvent;
188
185
  }
189
186
 
190
- export { Canvas, type CanvasConfig, type CanvasContentSize, type CanvasEventListener, type CanvasEventType, type CanvasRef, type CanvasTransformState, type EdgeScrollConfig };
187
+ export { Canvas, type CanvasConfig, type CanvasContentSize, type CanvasEventListener, type CanvasEventType, type CanvasTransformState, type EdgeScrollConfig, type ScrollNodeIntoViewOptions };
package/dist/index.d.ts CHANGED
@@ -1,33 +1,40 @@
1
1
  import { Element, BasePlugin, DOMElement, Engine } from '@knotx/core';
2
2
  import { InferParamsFromSchema } from '@knotx/decorators';
3
3
  import { MouseEvent, ReactNode } from 'react';
4
- import { ReactZoomPanPinchState, ReactZoomPanPinchProps, ReactZoomPanPinchContentRef } from 'react-zoom-pan-pinch';
4
+ import { ReactZoomPanPinchProps, ReactZoomPanPinchContentRef } from 'react-zoom-pan-pinch';
5
5
 
6
- type CanvasConfig = ReactZoomPanPinchProps & {
7
- defaultLocated?: {
8
- nodeId: string;
9
- scale?: number;
10
- block?: 'center' | 'end' | 'nearest' | 'start';
11
- inline?: 'center' | 'end' | 'nearest' | 'start';
12
- offset?: number | {
13
- x: number;
14
- y: number;
15
- };
16
- animationTime?: number;
17
- };
18
- };
6
+ interface CanvasTransformState {
7
+ previousScale: number;
8
+ scale: number;
9
+ positionX: number;
10
+ positionY: number;
11
+ }
19
12
  interface CanvasContentSize {
20
13
  width: number;
21
14
  height: number;
22
15
  }
23
- type CanvasTransformState = ReactZoomPanPinchState;
24
- type CanvasRef = ReactZoomPanPinchContentRef;
16
+ interface ScrollNodeIntoViewOptions {
17
+ nodeId: string;
18
+ scale?: number;
19
+ block?: 'center' | 'end' | 'nearest' | 'start';
20
+ inline?: 'center' | 'end' | 'nearest' | 'start';
21
+ offset?: {
22
+ x: number;
23
+ y: number;
24
+ };
25
+ animationTime?: number;
26
+ limitToBounds?: boolean;
27
+ }
25
28
  interface EdgeScrollConfig {
26
29
  enabled: boolean;
27
30
  edgeSize: number;
28
31
  maxSpeed: number;
29
32
  cornerSize?: number;
30
33
  }
34
+
35
+ type CanvasConfig = ReactZoomPanPinchProps & {
36
+ defaultLocated?: ScrollNodeIntoViewOptions;
37
+ };
31
38
  declare module '@knotx/core' {
32
39
  interface PluginData {
33
40
  canvas: {
@@ -45,17 +52,7 @@ declare module '@knotx/core' {
45
52
  }
46
53
  interface PluginTools {
47
54
  canvas: {
48
- scrollNodeIntoView: (params: {
49
- nodeId: string;
50
- scale?: number;
51
- block?: 'center' | 'end' | 'nearest' | 'start';
52
- inline?: 'center' | 'end' | 'nearest' | 'start';
53
- offset?: {
54
- x: number;
55
- y: number;
56
- };
57
- animationTime?: number;
58
- }) => void;
55
+ scrollNodeIntoView: (params: ScrollNodeIntoViewOptions) => void;
59
56
  zoomIn: (params: {
60
57
  step?: number;
61
58
  animationTime?: number;
@@ -81,7 +78,7 @@ type CanvasEventListener = (event: MouseEvent<HTMLElement>) => void;
81
78
  declare class Canvas extends BasePlugin<'canvas', CanvasConfig> {
82
79
  private config;
83
80
  name: "canvas";
84
- ref: CanvasRef | null;
81
+ ref: ReactZoomPanPinchContentRef | null;
85
82
  transform: CanvasTransformState;
86
83
  rootElement: DOMElement | null;
87
84
  contentSize: CanvasContentSize | undefined;
@@ -187,4 +184,4 @@ declare class Canvas extends BasePlugin<'canvas', CanvasConfig> {
187
184
  private isCanvasEvent;
188
185
  }
189
186
 
190
- export { Canvas, type CanvasConfig, type CanvasContentSize, type CanvasEventListener, type CanvasEventType, type CanvasRef, type CanvasTransformState, type EdgeScrollConfig };
187
+ export { Canvas, type CanvasConfig, type CanvasContentSize, type CanvasEventListener, type CanvasEventType, type CanvasTransformState, type EdgeScrollConfig, type ScrollNodeIntoViewOptions };
package/dist/index.js CHANGED
@@ -12,19 +12,21 @@ function calculateTransform({
12
12
  scale,
13
13
  block = "nearest",
14
14
  inline = "nearest",
15
- offset = 0
15
+ offset = 0,
16
+ contentSize,
17
+ limitToBounds
16
18
  }) {
17
- var _a, _b, _c, _d, _e, _f;
18
- const nodeX = node.position.x;
19
- const nodeY = node.position.y;
20
- const nodeWidth = (_b = (_a = node.measured) == null ? void 0 : _a.width) != null ? _b : 0;
21
- const nodeHeight = (_d = (_c = node.measured) == null ? void 0 : _c.height) != null ? _d : 0;
19
+ var _a, _b, _c, _d, _e, _f, _g, _h;
20
+ const nodeX = (_a = node == null ? void 0 : node.position.x) != null ? _a : 0;
21
+ const nodeY = (_b = node == null ? void 0 : node.position.y) != null ? _b : 0;
22
+ const nodeWidth = (_d = (_c = node == null ? void 0 : node.measured) == null ? void 0 : _c.width) != null ? _d : 0;
23
+ const nodeHeight = (_f = (_e = node == null ? void 0 : node.measured) == null ? void 0 : _e.height) != null ? _f : 0;
22
24
  const viewportWidth = container.width;
23
25
  const viewportHeight = container.height;
24
26
  let positionX = -transform.positionX;
25
27
  let positionY = -transform.positionY;
26
- const offsetX = typeof offset === "number" ? offset : (_e = offset.x) != null ? _e : 0;
27
- const offsetY = typeof offset === "number" ? offset : (_f = offset.y) != null ? _f : 0;
28
+ const offsetX = typeof offset === "number" ? offset : (_g = offset.x) != null ? _g : 0;
29
+ const offsetY = typeof offset === "number" ? offset : (_h = offset.y) != null ? _h : 0;
28
30
  switch (inline) {
29
31
  case "center":
30
32
  positionX = (nodeX + nodeWidth / 2) * scale - viewportWidth / 2;
@@ -71,6 +73,14 @@ function calculateTransform({
71
73
  break;
72
74
  }
73
75
  }
76
+ if (limitToBounds && contentSize) {
77
+ if (contentSize.width * scale < viewportWidth) {
78
+ positionX = contentSize.width / 2 * scale - viewportWidth / 2;
79
+ }
80
+ if (contentSize.height * scale < viewportHeight) {
81
+ positionY = contentSize.height / 2 * scale - viewportHeight / 2;
82
+ }
83
+ }
74
84
  return {
75
85
  scale,
76
86
  positionX,
@@ -284,10 +294,12 @@ class Canvas extends (_a = BasePlugin, _transform_dec = [register("transform")],
284
294
  node,
285
295
  container: this.container,
286
296
  transform: this.transform,
297
+ contentSize: this.contentSize,
287
298
  scale,
288
299
  block,
289
300
  inline,
290
- offset
301
+ offset,
302
+ limitToBounds: false
291
303
  });
292
304
  (_a2 = this.ref) == null ? void 0 : _a2.setTransform(-positionX, -positionY, newScale, animationTime);
293
305
  }
@@ -310,21 +322,19 @@ class Canvas extends (_a = BasePlugin, _transform_dec = [register("transform")],
310
322
  render({ children } = { children: null }) {
311
323
  var _a2, _b, _c, _d;
312
324
  const defaultTransform = useMemo(() => {
313
- const defaultLocated = this.config.defaultLocated;
325
+ const defaultLocated = __spreadValues({}, this.config.defaultLocated);
314
326
  if (!(defaultLocated == null ? void 0 : defaultLocated.nodeId)) {
315
327
  return this.transform;
316
328
  }
317
329
  const node = this.getNode({ id: defaultLocated.nodeId });
318
- if (!node) {
319
- return this.transform;
320
- }
321
330
  defaultLocated.inline || (defaultLocated.inline = "center");
322
331
  defaultLocated.block || (defaultLocated.block = "center");
323
332
  defaultLocated.scale || (defaultLocated.scale = this.transform.scale);
324
333
  const { positionX, positionY, scale: newScale } = calculateTransform(__spreadValues({
325
334
  node,
326
335
  container: this.container,
327
- transform: this.transform
336
+ transform: this.transform,
337
+ contentSize: this.contentSize
328
338
  }, defaultLocated));
329
339
  const newTransform = {
330
340
  previousScale: this.transform.scale,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knotx/plugins-canvas",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "Canvas Plugin for Knotx",
5
5
  "author": "boenfu",
6
6
  "license": "MIT",
@@ -29,23 +29,23 @@
29
29
  ],
30
30
  "peerDependencies": {
31
31
  "react": ">=17.0.0",
32
- "@knotx/jsx": "0.4.2"
32
+ "@knotx/jsx": "0.4.3"
33
33
  },
34
34
  "dependencies": {
35
35
  "lodash-es": "^4.17.21",
36
36
  "react-zoom-pan-pinch": "^3.7.0",
37
37
  "rxjs": "^7.8.1",
38
- "@knotx/core": "0.4.2",
39
- "@knotx/decorators": "0.4.2"
38
+ "@knotx/core": "0.4.3",
39
+ "@knotx/decorators": "0.4.3"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@types/lodash-es": "^4.17.12",
43
43
  "@types/react": "^17.0.0",
44
44
  "react": "^17.0.0",
45
- "@knotx/build-config": "0.4.2",
46
- "@knotx/eslint-config": "0.4.2",
47
- "@knotx/jsx": "0.4.2",
48
- "@knotx/typescript-config": "0.4.2"
45
+ "@knotx/build-config": "0.4.3",
46
+ "@knotx/eslint-config": "0.4.3",
47
+ "@knotx/jsx": "0.4.3",
48
+ "@knotx/typescript-config": "0.4.3"
49
49
  },
50
50
  "scripts": {
51
51
  "build": "unbuild",