@fileverse-dev/fortune-react 1.1.54 → 1.1.55-smooth-scroll-1

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,8 +1,9 @@
1
- import React, { useRef, useEffect, useContext, useCallback } from "react";
2
- import { Canvas, updateContextWithCanvas, updateContextWithSheetData, handleGlobalWheel, initFreeze, cellFadeAnimator } from "@fileverse-dev/fortune-core";
1
+ import React, { useRef, useEffect, useContext } from "react";
2
+ import { Canvas, updateContextWithCanvas, updateContextWithSheetData, initFreeze, cellFadeAnimator } from "@fileverse-dev/fortune-core";
3
3
  import "./index.css";
4
4
  import WorkbookContext from "../../context";
5
5
  import SheetOverlay from "../SheetOverlay";
6
+ import { useSmoothScroll } from "./use-smooth-scroll";
6
7
  var Sheet = function Sheet(_a) {
7
8
  var _b, _c, _d;
8
9
  var sheet = _a.sheet;
@@ -149,25 +150,7 @@ var Sheet = function Sheet(_a) {
149
150
  return cellFadeAnimator.setOnTick(null);
150
151
  };
151
152
  }, [context, refs.canvas, refs.globalCache.freezen, setContext, sheet.id]);
152
- var onWheel = useCallback(function (e) {
153
- var _a, _b;
154
- var functionDetailsEl = document.getElementById("function-details");
155
- var formulaSearchEl = document.getElementById("luckysheet-formula-search-c");
156
- var isMouseOverFunctionDetails = functionDetailsEl === null || functionDetailsEl === void 0 ? void 0 : functionDetailsEl.matches(":hover");
157
- var isMouseOverFormulaSearch = formulaSearchEl === null || formulaSearchEl === void 0 ? void 0 : formulaSearchEl.matches(":hover");
158
- if (functionDetailsEl && isMouseOverFunctionDetails || formulaSearchEl && isMouseOverFormulaSearch || ((_b = (_a = refs === null || refs === void 0 ? void 0 : refs.globalCache) === null || _a === void 0 ? void 0 : _a.searchDialog) === null || _b === void 0 ? void 0 : _b.mouseEnter)) return;
159
- setContext(function (draftCtx) {
160
- handleGlobalWheel(draftCtx, e, refs.globalCache, refs.scrollbarX.current, refs.scrollbarY.current);
161
- });
162
- e.preventDefault();
163
- }, [refs.globalCache, refs.scrollbarX, refs.scrollbarY, setContext]);
164
- useEffect(function () {
165
- var container = containerRef.current;
166
- container === null || container === void 0 ? void 0 : container.addEventListener("wheel", onWheel);
167
- return function () {
168
- container === null || container === void 0 ? void 0 : container.removeEventListener("wheel", onWheel);
169
- };
170
- }, [onWheel]);
153
+ useSmoothScroll(containerRef);
171
154
  return /*#__PURE__*/React.createElement("div", {
172
155
  ref: containerRef,
173
156
  className: "fortune-sheet-container"
@@ -0,0 +1,2 @@
1
+ import { RefObject } from "react";
2
+ export declare const useSmoothScroll: (scrollContainerRef: RefObject<HTMLDivElement | null>) => void;
@@ -0,0 +1,85 @@
1
+ import { useContext, useEffect } from "react";
2
+ import WorkbookContext from "../../context";
3
+ export var useSmoothScroll = function useSmoothScroll(scrollContainerRef) {
4
+ var _a = useContext(WorkbookContext),
5
+ context = _a.context,
6
+ refs = _a.refs;
7
+ function attachSmoothWheelScroll(scrollContainer, moveScrollBy, getPixelScale) {
8
+ if (getPixelScale === void 0) {
9
+ getPixelScale = function getPixelScale() {
10
+ return window.devicePixelRatio || 1;
11
+ };
12
+ }
13
+ var queuedXPixels = 0;
14
+ var queuedYPixels = 0;
15
+ var animationFrameId = 0;
16
+ var MAX_PIXEL_DELTA_PER_FRAME = 1200;
17
+ function applyQueuedScroll() {
18
+ animationFrameId = 0;
19
+ if (Math.abs(queuedXPixels) < 0.5 && Math.abs(queuedYPixels) < 0.5) {
20
+ queuedXPixels = 0;
21
+ queuedYPixels = 0;
22
+ return;
23
+ }
24
+ var xPixels = Math.max(-MAX_PIXEL_DELTA_PER_FRAME, Math.min(MAX_PIXEL_DELTA_PER_FRAME, queuedXPixels));
25
+ var yPixels = Math.max(-MAX_PIXEL_DELTA_PER_FRAME, Math.min(MAX_PIXEL_DELTA_PER_FRAME, queuedYPixels));
26
+ moveScrollBy(xPixels, yPixels);
27
+ queuedXPixels = 0;
28
+ queuedYPixels = 0;
29
+ }
30
+ function handleWheelEvent(event) {
31
+ var _a, _b;
32
+ event.preventDefault();
33
+ var functionDetailsElement = document.getElementById("function-details");
34
+ var formulaSearchElement = document.getElementById("luckysheet-formula-search-c");
35
+ var isPointerOverFunctionDetails = functionDetailsElement === null || functionDetailsElement === void 0 ? void 0 : functionDetailsElement.matches(":hover");
36
+ var isPointerOverFormulaSearch = formulaSearchElement === null || formulaSearchElement === void 0 ? void 0 : formulaSearchElement.matches(":hover");
37
+ var isPointerInSearchDialog = !!((_b = (_a = refs === null || refs === void 0 ? void 0 : refs.globalCache) === null || _a === void 0 ? void 0 : _a.searchDialog) === null || _b === void 0 ? void 0 : _b.mouseEnter);
38
+ var hasFilterContextMenuOpen = context.filterContextMenu != null;
39
+ if (functionDetailsElement && isPointerOverFunctionDetails || formulaSearchElement && isPointerOverFormulaSearch || isPointerInSearchDialog || hasFilterContextMenuOpen) {
40
+ return;
41
+ }
42
+ var scaleFactor = getPixelScale();
43
+ queuedXPixels += event.deltaX * scaleFactor;
44
+ queuedYPixels += event.deltaY * scaleFactor;
45
+ if (!animationFrameId) animationFrameId = requestAnimationFrame(applyQueuedScroll);
46
+ }
47
+ scrollContainer.addEventListener("wheel", handleWheelEvent, {
48
+ passive: false
49
+ });
50
+ return function () {
51
+ scrollContainer.removeEventListener("wheel", handleWheelEvent);
52
+ if (animationFrameId) cancelAnimationFrame(animationFrameId);
53
+ };
54
+ }
55
+ var makeScrollbarsMoveByPixels = function makeScrollbarsMoveByPixels(horizontalScrollbarEl, verticalScrollbarEl) {
56
+ return function (xPixels, yPixels) {
57
+ var maxScrollLeft = horizontalScrollbarEl.scrollWidth - horizontalScrollbarEl.clientWidth;
58
+ var maxScrollTop = verticalScrollbarEl.scrollHeight - verticalScrollbarEl.clientHeight;
59
+ var targetScrollLeft = Math.max(0, Math.min(maxScrollLeft, horizontalScrollbarEl.scrollLeft + xPixels));
60
+ var targetScrollTop = Math.max(0, Math.min(maxScrollTop, verticalScrollbarEl.scrollTop + yPixels));
61
+ var didScrollX = targetScrollLeft !== horizontalScrollbarEl.scrollLeft;
62
+ var didScrollY = targetScrollTop !== verticalScrollbarEl.scrollTop;
63
+ if (didScrollX) {
64
+ horizontalScrollbarEl.scrollLeft = targetScrollLeft;
65
+ }
66
+ if (didScrollY) {
67
+ verticalScrollbarEl.scrollTop = targetScrollTop;
68
+ }
69
+ };
70
+ };
71
+ function routeWheelScrollToScrollbars(sheetContext, scrollContainerEl, horizontalScrollbarEl, verticalScrollbarEl) {
72
+ var moveScrollbarsByPixels = makeScrollbarsMoveByPixels(horizontalScrollbarEl, verticalScrollbarEl);
73
+ return attachSmoothWheelScroll(scrollContainerEl, moveScrollbarsByPixels, function () {
74
+ return (window.devicePixelRatio || 1) * sheetContext.zoomRatio;
75
+ });
76
+ }
77
+ useEffect(function () {
78
+ var scrollContainerEl = scrollContainerRef.current;
79
+ var horizontalScrollbarEl = refs.scrollbarX.current;
80
+ var verticalScrollbarEl = refs.scrollbarY.current;
81
+ if (!scrollContainerEl || !horizontalScrollbarEl || !verticalScrollbarEl) return function () {};
82
+ var unmountScrollHandler = routeWheelScrollToScrollbars(context, scrollContainerEl, horizontalScrollbarEl, verticalScrollbarEl);
83
+ return unmountScrollHandler;
84
+ }, [context.zoomRatio]);
85
+ };
@@ -1,6 +1,5 @@
1
1
  export declare function moveCursorToEnd(editableDiv: HTMLDivElement): void;
2
2
  export declare function getCursorPosition(editableDiv: HTMLDivElement): number;
3
- export declare function setCursorPosition(editableDiv: HTMLDivElement, pos: number): void;
4
3
  export declare function isLetterNumberPattern(str: string): boolean;
5
4
  export declare function removeLastSpan(htmlString: string): string;
6
5
  export declare function incrementColumn(cell: string): string;
@@ -18,31 +18,6 @@ export function getCursorPosition(editableDiv) {
18
18
  preRange.setEnd(range.endContainer, range.endOffset);
19
19
  return preRange.toString().length;
20
20
  }
21
- export function setCursorPosition(editableDiv, pos) {
22
- editableDiv.focus();
23
- var selection = window.getSelection();
24
- if (!selection) return;
25
- var range = document.createRange();
26
- var charIndex = 0;
27
- var nodeStack = [editableDiv];
28
- var node;
29
- while (node = nodeStack.pop()) {
30
- if (node.nodeType === Node.TEXT_NODE) {
31
- var textNode = node;
32
- var nextCharIndex = charIndex + textNode.length;
33
- if (pos <= nextCharIndex) {
34
- range.setStart(textNode, pos - charIndex);
35
- range.collapse(true);
36
- break;
37
- }
38
- charIndex = nextCharIndex;
39
- } else {
40
- nodeStack.push.apply(nodeStack, Array.from(node.childNodes).reverse());
41
- }
42
- }
43
- selection.removeAllRanges();
44
- selection.addRange(range);
45
- }
46
21
  export function isLetterNumberPattern(str) {
47
22
  var regex = /^[a-zA-Z]+\d+$/;
48
23
  return regex.test(str);
@@ -10,6 +10,7 @@ var _fortuneCore = require("@fileverse-dev/fortune-core");
10
10
  require("./index.css");
11
11
  var _context = _interopRequireDefault(require("../../context"));
12
12
  var _SheetOverlay = _interopRequireDefault(require("../SheetOverlay"));
13
+ var _useSmoothScroll = require("./use-smooth-scroll");
13
14
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
14
15
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
15
16
  var Sheet = function Sheet(_a) {
@@ -158,25 +159,7 @@ var Sheet = function Sheet(_a) {
158
159
  return _fortuneCore.cellFadeAnimator.setOnTick(null);
159
160
  };
160
161
  }, [context, refs.canvas, refs.globalCache.freezen, setContext, sheet.id]);
161
- var onWheel = (0, _react.useCallback)(function (e) {
162
- var _a, _b;
163
- var functionDetailsEl = document.getElementById("function-details");
164
- var formulaSearchEl = document.getElementById("luckysheet-formula-search-c");
165
- var isMouseOverFunctionDetails = functionDetailsEl === null || functionDetailsEl === void 0 ? void 0 : functionDetailsEl.matches(":hover");
166
- var isMouseOverFormulaSearch = formulaSearchEl === null || formulaSearchEl === void 0 ? void 0 : formulaSearchEl.matches(":hover");
167
- if (functionDetailsEl && isMouseOverFunctionDetails || formulaSearchEl && isMouseOverFormulaSearch || ((_b = (_a = refs === null || refs === void 0 ? void 0 : refs.globalCache) === null || _a === void 0 ? void 0 : _a.searchDialog) === null || _b === void 0 ? void 0 : _b.mouseEnter)) return;
168
- setContext(function (draftCtx) {
169
- (0, _fortuneCore.handleGlobalWheel)(draftCtx, e, refs.globalCache, refs.scrollbarX.current, refs.scrollbarY.current);
170
- });
171
- e.preventDefault();
172
- }, [refs.globalCache, refs.scrollbarX, refs.scrollbarY, setContext]);
173
- (0, _react.useEffect)(function () {
174
- var container = containerRef.current;
175
- container === null || container === void 0 ? void 0 : container.addEventListener("wheel", onWheel);
176
- return function () {
177
- container === null || container === void 0 ? void 0 : container.removeEventListener("wheel", onWheel);
178
- };
179
- }, [onWheel]);
162
+ (0, _useSmoothScroll.useSmoothScroll)(containerRef);
180
163
  return /*#__PURE__*/_react.default.createElement("div", {
181
164
  ref: containerRef,
182
165
  className: "fortune-sheet-container"
@@ -0,0 +1,2 @@
1
+ import { RefObject } from "react";
2
+ export declare const useSmoothScroll: (scrollContainerRef: RefObject<HTMLDivElement | null>) => void;
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useSmoothScroll = void 0;
7
+ var _react = require("react");
8
+ var _context = _interopRequireDefault(require("../../context"));
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
+ var useSmoothScroll = exports.useSmoothScroll = function useSmoothScroll(scrollContainerRef) {
11
+ var _a = (0, _react.useContext)(_context.default),
12
+ context = _a.context,
13
+ refs = _a.refs;
14
+ function attachSmoothWheelScroll(scrollContainer, moveScrollBy, getPixelScale) {
15
+ if (getPixelScale === void 0) {
16
+ getPixelScale = function getPixelScale() {
17
+ return window.devicePixelRatio || 1;
18
+ };
19
+ }
20
+ var queuedXPixels = 0;
21
+ var queuedYPixels = 0;
22
+ var animationFrameId = 0;
23
+ var MAX_PIXEL_DELTA_PER_FRAME = 1200;
24
+ function applyQueuedScroll() {
25
+ animationFrameId = 0;
26
+ if (Math.abs(queuedXPixels) < 0.5 && Math.abs(queuedYPixels) < 0.5) {
27
+ queuedXPixels = 0;
28
+ queuedYPixels = 0;
29
+ return;
30
+ }
31
+ var xPixels = Math.max(-MAX_PIXEL_DELTA_PER_FRAME, Math.min(MAX_PIXEL_DELTA_PER_FRAME, queuedXPixels));
32
+ var yPixels = Math.max(-MAX_PIXEL_DELTA_PER_FRAME, Math.min(MAX_PIXEL_DELTA_PER_FRAME, queuedYPixels));
33
+ moveScrollBy(xPixels, yPixels);
34
+ queuedXPixels = 0;
35
+ queuedYPixels = 0;
36
+ }
37
+ function handleWheelEvent(event) {
38
+ var _a, _b;
39
+ event.preventDefault();
40
+ var functionDetailsElement = document.getElementById("function-details");
41
+ var formulaSearchElement = document.getElementById("luckysheet-formula-search-c");
42
+ var isPointerOverFunctionDetails = functionDetailsElement === null || functionDetailsElement === void 0 ? void 0 : functionDetailsElement.matches(":hover");
43
+ var isPointerOverFormulaSearch = formulaSearchElement === null || formulaSearchElement === void 0 ? void 0 : formulaSearchElement.matches(":hover");
44
+ var isPointerInSearchDialog = !!((_b = (_a = refs === null || refs === void 0 ? void 0 : refs.globalCache) === null || _a === void 0 ? void 0 : _a.searchDialog) === null || _b === void 0 ? void 0 : _b.mouseEnter);
45
+ var hasFilterContextMenuOpen = context.filterContextMenu != null;
46
+ if (functionDetailsElement && isPointerOverFunctionDetails || formulaSearchElement && isPointerOverFormulaSearch || isPointerInSearchDialog || hasFilterContextMenuOpen) {
47
+ return;
48
+ }
49
+ var scaleFactor = getPixelScale();
50
+ queuedXPixels += event.deltaX * scaleFactor;
51
+ queuedYPixels += event.deltaY * scaleFactor;
52
+ if (!animationFrameId) animationFrameId = requestAnimationFrame(applyQueuedScroll);
53
+ }
54
+ scrollContainer.addEventListener("wheel", handleWheelEvent, {
55
+ passive: false
56
+ });
57
+ return function () {
58
+ scrollContainer.removeEventListener("wheel", handleWheelEvent);
59
+ if (animationFrameId) cancelAnimationFrame(animationFrameId);
60
+ };
61
+ }
62
+ var makeScrollbarsMoveByPixels = function makeScrollbarsMoveByPixels(horizontalScrollbarEl, verticalScrollbarEl) {
63
+ return function (xPixels, yPixels) {
64
+ var maxScrollLeft = horizontalScrollbarEl.scrollWidth - horizontalScrollbarEl.clientWidth;
65
+ var maxScrollTop = verticalScrollbarEl.scrollHeight - verticalScrollbarEl.clientHeight;
66
+ var targetScrollLeft = Math.max(0, Math.min(maxScrollLeft, horizontalScrollbarEl.scrollLeft + xPixels));
67
+ var targetScrollTop = Math.max(0, Math.min(maxScrollTop, verticalScrollbarEl.scrollTop + yPixels));
68
+ var didScrollX = targetScrollLeft !== horizontalScrollbarEl.scrollLeft;
69
+ var didScrollY = targetScrollTop !== verticalScrollbarEl.scrollTop;
70
+ if (didScrollX) {
71
+ horizontalScrollbarEl.scrollLeft = targetScrollLeft;
72
+ }
73
+ if (didScrollY) {
74
+ verticalScrollbarEl.scrollTop = targetScrollTop;
75
+ }
76
+ };
77
+ };
78
+ function routeWheelScrollToScrollbars(sheetContext, scrollContainerEl, horizontalScrollbarEl, verticalScrollbarEl) {
79
+ var moveScrollbarsByPixels = makeScrollbarsMoveByPixels(horizontalScrollbarEl, verticalScrollbarEl);
80
+ return attachSmoothWheelScroll(scrollContainerEl, moveScrollbarsByPixels, function () {
81
+ return (window.devicePixelRatio || 1) * sheetContext.zoomRatio;
82
+ });
83
+ }
84
+ (0, _react.useEffect)(function () {
85
+ var scrollContainerEl = scrollContainerRef.current;
86
+ var horizontalScrollbarEl = refs.scrollbarX.current;
87
+ var verticalScrollbarEl = refs.scrollbarY.current;
88
+ if (!scrollContainerEl || !horizontalScrollbarEl || !verticalScrollbarEl) return function () {};
89
+ var unmountScrollHandler = routeWheelScrollToScrollbars(context, scrollContainerEl, horizontalScrollbarEl, verticalScrollbarEl);
90
+ return unmountScrollHandler;
91
+ }, [context.zoomRatio]);
92
+ };
@@ -1,6 +1,5 @@
1
1
  export declare function moveCursorToEnd(editableDiv: HTMLDivElement): void;
2
2
  export declare function getCursorPosition(editableDiv: HTMLDivElement): number;
3
- export declare function setCursorPosition(editableDiv: HTMLDivElement, pos: number): void;
4
3
  export declare function isLetterNumberPattern(str: string): boolean;
5
4
  export declare function removeLastSpan(htmlString: string): string;
6
5
  export declare function incrementColumn(cell: string): string;
@@ -11,7 +11,6 @@ exports.incrementRow = incrementRow;
11
11
  exports.isLetterNumberPattern = isLetterNumberPattern;
12
12
  exports.moveCursorToEnd = moveCursorToEnd;
13
13
  exports.removeLastSpan = removeLastSpan;
14
- exports.setCursorPosition = setCursorPosition;
15
14
  function moveCursorToEnd(editableDiv) {
16
15
  editableDiv.focus();
17
16
  var range = document.createRange();
@@ -32,31 +31,6 @@ function getCursorPosition(editableDiv) {
32
31
  preRange.setEnd(range.endContainer, range.endOffset);
33
32
  return preRange.toString().length;
34
33
  }
35
- function setCursorPosition(editableDiv, pos) {
36
- editableDiv.focus();
37
- var selection = window.getSelection();
38
- if (!selection) return;
39
- var range = document.createRange();
40
- var charIndex = 0;
41
- var nodeStack = [editableDiv];
42
- var node;
43
- while (node = nodeStack.pop()) {
44
- if (node.nodeType === Node.TEXT_NODE) {
45
- var textNode = node;
46
- var nextCharIndex = charIndex + textNode.length;
47
- if (pos <= nextCharIndex) {
48
- range.setStart(textNode, pos - charIndex);
49
- range.collapse(true);
50
- break;
51
- }
52
- charIndex = nextCharIndex;
53
- } else {
54
- nodeStack.push.apply(nodeStack, Array.from(node.childNodes).reverse());
55
- }
56
- }
57
- selection.removeAllRanges();
58
- selection.addRange(range);
59
- }
60
34
  function isLetterNumberPattern(str) {
61
35
  var regex = /^[a-zA-Z]+\d+$/;
62
36
  return regex.test(str);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fileverse-dev/fortune-react",
3
- "version": "1.1.54",
3
+ "version": "1.1.55-smooth-scroll-1",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "module": "es/index.js",
@@ -16,7 +16,7 @@
16
16
  "tsc": "tsc"
17
17
  },
18
18
  "dependencies": {
19
- "@fileverse-dev/fortune-core": "1.1.54",
19
+ "@fileverse-dev/fortune-core": "1.1.55-smooth-scroll-1",
20
20
  "@fileverse/ui": "^4.1.7-patch-21",
21
21
  "@tippyjs/react": "^4.2.6",
22
22
  "@types/regenerator-runtime": "^0.13.6",
@@ -1,9 +0,0 @@
1
- import React from "react";
2
- interface DraggableDivProps {
3
- children?: React.ReactNode;
4
- className?: string;
5
- initialX?: number;
6
- initialY?: number;
7
- }
8
- export declare function DraggableDiv({ children, className, initialX, initialY }: DraggableDivProps): React.JSX.Element;
9
- export {};
@@ -1,129 +0,0 @@
1
- "use client";
2
-
3
- import React from "react";
4
- import { useState, useRef, useCallback } from "react";
5
- import { cn } from "@fileverse/ui";
6
- export function DraggableDiv(_a) {
7
- var children = _a.children,
8
- className = _a.className,
9
- _b = _a.initialX,
10
- initialX = _b === void 0 ? 100 : _b,
11
- _c = _a.initialY,
12
- initialY = _c === void 0 ? 100 : _c;
13
- var _d = useState({
14
- x: initialX,
15
- y: initialY
16
- }),
17
- position = _d[0],
18
- setPosition = _d[1];
19
- var _e = useState(false),
20
- isDragging = _e[0],
21
- setIsDragging = _e[1];
22
- var _f = useState({
23
- x: 0,
24
- y: 0
25
- }),
26
- dragStart = _f[0],
27
- setDragStart = _f[1];
28
- var _g = useState({
29
- x: 0,
30
- y: 0
31
- }),
32
- dragOffset = _g[0],
33
- setDragOffset = _g[1];
34
- var divRef = useRef(null);
35
- var handleMouseDown = useCallback(function (e) {
36
- e.preventDefault();
37
- setIsDragging(true);
38
- setDragStart({
39
- x: e.clientX,
40
- y: e.clientY
41
- });
42
- setDragOffset({
43
- x: e.clientX,
44
- y: e.clientY
45
- });
46
- }, [position.x, position.y]);
47
- var handleMouseMove = useCallback(function (e) {
48
- var _a, _b;
49
- if (!isDragging) return;
50
- e.preventDefault();
51
- var newX = e.clientX - dragOffset.x;
52
- var newY = e.clientY - dragOffset.y;
53
- var divWidth = ((_a = divRef.current) === null || _a === void 0 ? void 0 : _a.offsetWidth) || 0;
54
- var divHeight = ((_b = divRef.current) === null || _b === void 0 ? void 0 : _b.offsetHeight) || 0;
55
- var maxX = window.innerWidth - divWidth;
56
- var maxY = window.innerHeight - divHeight;
57
- setPosition({
58
- x: Math.max(-divWidth + 50, Math.min(newX, maxX)),
59
- y: Math.max(-divHeight + 50, Math.min(newY, maxY))
60
- });
61
- }, [isDragging, dragOffset]);
62
- var handleMouseUp = useCallback(function () {
63
- setIsDragging(false);
64
- }, []);
65
- React.useEffect(function () {
66
- if (isDragging) {
67
- document.addEventListener("mousemove", handleMouseMove);
68
- document.addEventListener("mouseup", handleMouseUp);
69
- return function () {
70
- document.removeEventListener("mousemove", handleMouseMove);
71
- document.removeEventListener("mouseup", handleMouseUp);
72
- };
73
- }
74
- }, [isDragging, handleMouseMove, handleMouseUp]);
75
- var handleTouchStart = useCallback(function (e) {
76
- e.preventDefault();
77
- var touch = e.touches[0];
78
- setIsDragging(true);
79
- setDragStart({
80
- x: touch.clientX,
81
- y: touch.clientY
82
- });
83
- setDragOffset({
84
- x: touch.clientX - position.x,
85
- y: touch.clientY - position.y
86
- });
87
- }, [position.x, position.y]);
88
- var handleTouchMove = useCallback(function (e) {
89
- var _a, _b;
90
- if (!isDragging) return;
91
- e.preventDefault();
92
- var touch = e.touches[0];
93
- var newX = touch.clientX - dragOffset.x;
94
- var newY = touch.clientY - dragOffset.y;
95
- var divWidth = ((_a = divRef.current) === null || _a === void 0 ? void 0 : _a.offsetWidth) || 0;
96
- var divHeight = ((_b = divRef.current) === null || _b === void 0 ? void 0 : _b.offsetHeight) || 0;
97
- var maxX = window.innerWidth - divWidth;
98
- var maxY = window.innerHeight - divHeight;
99
- setPosition({
100
- x: Math.max(-divWidth + 50, Math.min(newX, maxX)),
101
- y: Math.max(-divHeight + 50, Math.min(newY, maxY))
102
- });
103
- }, [isDragging, dragOffset]);
104
- var handleTouchEnd = useCallback(function () {
105
- setIsDragging(false);
106
- }, []);
107
- React.useEffect(function () {
108
- if (isDragging) {
109
- document.addEventListener("touchmove", handleTouchMove, {
110
- passive: false
111
- });
112
- document.addEventListener("touchend", handleTouchEnd);
113
- return function () {
114
- document.removeEventListener("touchmove", handleTouchMove);
115
- document.removeEventListener("touchend", handleTouchEnd);
116
- };
117
- }
118
- }, [isDragging, handleTouchMove, handleTouchEnd]);
119
- return /*#__PURE__*/React.createElement("div", {
120
- ref: divRef,
121
- className: cn("absolute select-none touch-none transition-shadow duration-200", isDragging ? "cursor-grabbing shadow-2xl z-50" : "cursor-grab shadow-lg", className),
122
- style: {
123
- left: "".concat(position.x, "px"),
124
- top: "".concat(position.y, "px")
125
- },
126
- onMouseDown: handleMouseDown,
127
- onTouchStart: handleTouchStart
128
- }, children);
129
- }
@@ -1,9 +0,0 @@
1
- import React from "react";
2
- interface DraggableDivProps {
3
- children?: React.ReactNode;
4
- className?: string;
5
- initialX?: number;
6
- initialY?: number;
7
- }
8
- export declare function DraggableDiv({ children, className, initialX, initialY }: DraggableDivProps): React.JSX.Element;
9
- export {};
@@ -1,135 +0,0 @@
1
- "use strict";
2
- "use client";
3
-
4
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.DraggableDiv = DraggableDiv;
9
- var _react = _interopRequireWildcard(require("react"));
10
- var _ui = require("@fileverse/ui");
11
- function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
12
- function DraggableDiv(_a) {
13
- var children = _a.children,
14
- className = _a.className,
15
- _b = _a.initialX,
16
- initialX = _b === void 0 ? 100 : _b,
17
- _c = _a.initialY,
18
- initialY = _c === void 0 ? 100 : _c;
19
- var _d = (0, _react.useState)({
20
- x: initialX,
21
- y: initialY
22
- }),
23
- position = _d[0],
24
- setPosition = _d[1];
25
- var _e = (0, _react.useState)(false),
26
- isDragging = _e[0],
27
- setIsDragging = _e[1];
28
- var _f = (0, _react.useState)({
29
- x: 0,
30
- y: 0
31
- }),
32
- dragStart = _f[0],
33
- setDragStart = _f[1];
34
- var _g = (0, _react.useState)({
35
- x: 0,
36
- y: 0
37
- }),
38
- dragOffset = _g[0],
39
- setDragOffset = _g[1];
40
- var divRef = (0, _react.useRef)(null);
41
- var handleMouseDown = (0, _react.useCallback)(function (e) {
42
- e.preventDefault();
43
- setIsDragging(true);
44
- setDragStart({
45
- x: e.clientX,
46
- y: e.clientY
47
- });
48
- setDragOffset({
49
- x: e.clientX,
50
- y: e.clientY
51
- });
52
- }, [position.x, position.y]);
53
- var handleMouseMove = (0, _react.useCallback)(function (e) {
54
- var _a, _b;
55
- if (!isDragging) return;
56
- e.preventDefault();
57
- var newX = e.clientX - dragOffset.x;
58
- var newY = e.clientY - dragOffset.y;
59
- var divWidth = ((_a = divRef.current) === null || _a === void 0 ? void 0 : _a.offsetWidth) || 0;
60
- var divHeight = ((_b = divRef.current) === null || _b === void 0 ? void 0 : _b.offsetHeight) || 0;
61
- var maxX = window.innerWidth - divWidth;
62
- var maxY = window.innerHeight - divHeight;
63
- setPosition({
64
- x: Math.max(-divWidth + 50, Math.min(newX, maxX)),
65
- y: Math.max(-divHeight + 50, Math.min(newY, maxY))
66
- });
67
- }, [isDragging, dragOffset]);
68
- var handleMouseUp = (0, _react.useCallback)(function () {
69
- setIsDragging(false);
70
- }, []);
71
- _react.default.useEffect(function () {
72
- if (isDragging) {
73
- document.addEventListener("mousemove", handleMouseMove);
74
- document.addEventListener("mouseup", handleMouseUp);
75
- return function () {
76
- document.removeEventListener("mousemove", handleMouseMove);
77
- document.removeEventListener("mouseup", handleMouseUp);
78
- };
79
- }
80
- }, [isDragging, handleMouseMove, handleMouseUp]);
81
- var handleTouchStart = (0, _react.useCallback)(function (e) {
82
- e.preventDefault();
83
- var touch = e.touches[0];
84
- setIsDragging(true);
85
- setDragStart({
86
- x: touch.clientX,
87
- y: touch.clientY
88
- });
89
- setDragOffset({
90
- x: touch.clientX - position.x,
91
- y: touch.clientY - position.y
92
- });
93
- }, [position.x, position.y]);
94
- var handleTouchMove = (0, _react.useCallback)(function (e) {
95
- var _a, _b;
96
- if (!isDragging) return;
97
- e.preventDefault();
98
- var touch = e.touches[0];
99
- var newX = touch.clientX - dragOffset.x;
100
- var newY = touch.clientY - dragOffset.y;
101
- var divWidth = ((_a = divRef.current) === null || _a === void 0 ? void 0 : _a.offsetWidth) || 0;
102
- var divHeight = ((_b = divRef.current) === null || _b === void 0 ? void 0 : _b.offsetHeight) || 0;
103
- var maxX = window.innerWidth - divWidth;
104
- var maxY = window.innerHeight - divHeight;
105
- setPosition({
106
- x: Math.max(-divWidth + 50, Math.min(newX, maxX)),
107
- y: Math.max(-divHeight + 50, Math.min(newY, maxY))
108
- });
109
- }, [isDragging, dragOffset]);
110
- var handleTouchEnd = (0, _react.useCallback)(function () {
111
- setIsDragging(false);
112
- }, []);
113
- _react.default.useEffect(function () {
114
- if (isDragging) {
115
- document.addEventListener("touchmove", handleTouchMove, {
116
- passive: false
117
- });
118
- document.addEventListener("touchend", handleTouchEnd);
119
- return function () {
120
- document.removeEventListener("touchmove", handleTouchMove);
121
- document.removeEventListener("touchend", handleTouchEnd);
122
- };
123
- }
124
- }, [isDragging, handleTouchMove, handleTouchEnd]);
125
- return /*#__PURE__*/_react.default.createElement("div", {
126
- ref: divRef,
127
- className: (0, _ui.cn)("absolute select-none touch-none transition-shadow duration-200", isDragging ? "cursor-grabbing shadow-2xl z-50" : "cursor-grab shadow-lg", className),
128
- style: {
129
- left: "".concat(position.x, "px"),
130
- top: "".concat(position.y, "px")
131
- },
132
- onMouseDown: handleMouseDown,
133
- onTouchStart: handleTouchStart
134
- }, children);
135
- }