@vuu-ui/vuu-layout 0.5.10 → 0.5.11
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/LICENSE +201 -0
- package/cjs/index.js +20 -0
- package/cjs/index.js.map +7 -0
- package/esm/index.js +20 -0
- package/esm/index.js.map +7 -0
- package/index.css +2 -0
- package/index.css.map +7 -0
- package/package.json +13 -10
- package/src/Component.css +0 -0
- package/src/Component.tsx +0 -20
- package/src/DraggableLayout.css +0 -18
- package/src/DraggableLayout.tsx +0 -26
- package/src/__tests__/flexbox-utils.spec.js +0 -90
- package/src/chest-of-drawers/Chest.css +0 -36
- package/src/chest-of-drawers/Chest.tsx +0 -42
- package/src/chest-of-drawers/Drawer.css +0 -159
- package/src/chest-of-drawers/Drawer.tsx +0 -118
- package/src/chest-of-drawers/index.ts +0 -2
- package/src/common-types.ts +0 -9
- package/src/debug.ts +0 -16
- package/src/drag-drop/BoxModel.ts +0 -551
- package/src/drag-drop/DragState.ts +0 -219
- package/src/drag-drop/Draggable.ts +0 -282
- package/src/drag-drop/DropMenu.css +0 -71
- package/src/drag-drop/DropMenu.tsx +0 -61
- package/src/drag-drop/DropTarget.ts +0 -393
- package/src/drag-drop/DropTargetRenderer.css +0 -40
- package/src/drag-drop/DropTargetRenderer.tsx +0 -277
- package/src/drag-drop/dragDropTypes.ts +0 -47
- package/src/drag-drop/index.ts +0 -5
- package/src/editable-label/EditableLabel.css +0 -28
- package/src/editable-label/EditableLabel.tsx +0 -99
- package/src/editable-label/index.ts +0 -1
- package/src/flexbox/Flexbox.css +0 -45
- package/src/flexbox/Flexbox.tsx +0 -70
- package/src/flexbox/FlexboxLayout.tsx +0 -28
- package/src/flexbox/FluidGrid.css +0 -134
- package/src/flexbox/FluidGrid.tsx +0 -82
- package/src/flexbox/FluidGridLayout.tsx +0 -9
- package/src/flexbox/Splitter.css +0 -140
- package/src/flexbox/Splitter.tsx +0 -127
- package/src/flexbox/flexbox-utils.ts +0 -128
- package/src/flexbox/flexboxTypes.ts +0 -68
- package/src/flexbox/index.ts +0 -5
- package/src/flexbox/useResponsiveSizing.ts +0 -82
- package/src/flexbox/useSplitterResizing.ts +0 -270
- package/src/index.ts +0 -19
- package/src/layout-action.ts +0 -21
- package/src/layout-header/ActionButton.tsx +0 -23
- package/src/layout-header/Header.css +0 -8
- package/src/layout-header/Header.tsx +0 -216
- package/src/layout-header/index.ts +0 -1
- package/src/layout-provider/LayoutProvider.tsx +0 -161
- package/src/layout-provider/LayoutProviderContext.ts +0 -17
- package/src/layout-provider/index.ts +0 -3
- package/src/layout-provider/useLayoutDragDrop.ts +0 -210
- package/src/layout-reducer/flexUtils.ts +0 -276
- package/src/layout-reducer/index.ts +0 -5
- package/src/layout-reducer/insert-layout-element.ts +0 -365
- package/src/layout-reducer/layout-reducer.ts +0 -237
- package/src/layout-reducer/layoutTypes.ts +0 -159
- package/src/layout-reducer/layoutUtils.ts +0 -288
- package/src/layout-reducer/remove-layout-element.ts +0 -226
- package/src/layout-reducer/replace-layout-element.ts +0 -113
- package/src/layout-reducer/resize-flex-children.ts +0 -55
- package/src/layout-reducer/wrap-layout-element.ts +0 -307
- package/src/layout-view/View.css +0 -61
- package/src/layout-view/View.tsx +0 -143
- package/src/layout-view/ViewContext.ts +0 -30
- package/src/layout-view/index.ts +0 -5
- package/src/layout-view/useView.tsx +0 -104
- package/src/layout-view/useViewActionDispatcher.ts +0 -123
- package/src/layout-view/useViewResize.ts +0 -53
- package/src/layout-view/viewTypes.ts +0 -35
- package/src/palette/Palette.css +0 -33
- package/src/palette/Palette.tsx +0 -140
- package/src/palette/PaletteSalt.css +0 -9
- package/src/palette/PaletteSalt.tsx +0 -79
- package/src/palette/index.ts +0 -3
- package/src/placeholder/Placeholder.css +0 -10
- package/src/placeholder/Placeholder.tsx +0 -38
- package/src/placeholder/index.ts +0 -1
- package/src/registry/ComponentRegistry.ts +0 -44
- package/src/registry/index.ts +0 -1
- package/src/responsive/breakpoints.ts +0 -62
- package/src/responsive/index.ts +0 -3
- package/src/responsive/measureMinimumNodeSize.ts +0 -23
- package/src/responsive/overflowUtils.js +0 -14
- package/src/responsive/use-breakpoints.ts +0 -101
- package/src/responsive/useResizeObserver.ts +0 -154
- package/src/responsive/utils.ts +0 -37
- package/src/stack/Stack.css +0 -39
- package/src/stack/Stack.tsx +0 -173
- package/src/stack/StackLayout.tsx +0 -119
- package/src/stack/index.ts +0 -4
- package/src/stack/stackTypes.ts +0 -22
- package/src/tabs/TabPanel.css +0 -12
- package/src/tabs/TabPanel.tsx +0 -17
- package/src/tabs/index.ts +0 -1
- package/src/tools/config-wrapper/ConfigWrapper.tsx +0 -55
- package/src/tools/config-wrapper/index.ts +0 -1
- package/src/tools/devtools-box/layout-configurator.css +0 -112
- package/src/tools/devtools-box/layout-configurator.jsx +0 -369
- package/src/tools/devtools-tree/layout-tree-viewer.css +0 -15
- package/src/tools/devtools-tree/layout-tree-viewer.jsx +0 -36
- package/src/tools/index.ts +0 -4
- package/src/use-persistent-state.ts +0 -112
- package/src/utils/index.ts +0 -5
- package/src/utils/pathUtils.ts +0 -283
- package/src/utils/propUtils.ts +0 -26
- package/src/utils/refUtils.ts +0 -16
- package/src/utils/styleUtils.ts +0 -13
- package/src/utils/typeOf.ts +0 -25
- package/tsconfig-emit-types.json +0 -11
package/src/layout-action.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export const Action = {
|
|
2
|
-
ADD: 'add',
|
|
3
|
-
BLUR: 'blur',
|
|
4
|
-
BLUR_SPLITTER: 'blur-splitter',
|
|
5
|
-
DRAG_START: 'drag-start',
|
|
6
|
-
DRAG_STARTED: 'drag-started',
|
|
7
|
-
DRAG_DROP: 'drag-drop',
|
|
8
|
-
FOCUS: 'focus',
|
|
9
|
-
FOCUS_SPLITTER: 'focus-splitter',
|
|
10
|
-
INITIALIZE: 'initialize',
|
|
11
|
-
MAXIMIZE: 'maximize',
|
|
12
|
-
MINIMIZE: 'minimize',
|
|
13
|
-
REMOVE: 'remove',
|
|
14
|
-
REPLACE: 'replace',
|
|
15
|
-
RESTORE: 'restore',
|
|
16
|
-
SAVE: 'save',
|
|
17
|
-
SET_TITLE: 'set-title',
|
|
18
|
-
SPLITTER_RESIZE: 'splitter-resize',
|
|
19
|
-
SWITCH_TAB: 'switch-tab',
|
|
20
|
-
TEAR_OUT: 'tear-out'
|
|
21
|
-
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import classnames from 'classnames';
|
|
2
|
-
import { HTMLAttributes, MouseEvent } from 'react';
|
|
3
|
-
|
|
4
|
-
export interface ActionButtonProps extends Omit<HTMLAttributes<HTMLButtonElement>, 'onClick'> {
|
|
5
|
-
actionId: 'maximize' | 'minimize' | 'restore' | 'tearout';
|
|
6
|
-
iconName?: string;
|
|
7
|
-
onClick: (evt: MouseEvent, actionId: 'maximize' | 'minimize' | 'restore' | 'tearout') => void;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const ActionButton = ({ actionId, className, iconName, onClick, ...props }: ActionButtonProps) => {
|
|
11
|
-
const handleClick = (evt: MouseEvent) => {
|
|
12
|
-
onClick(evt, actionId);
|
|
13
|
-
};
|
|
14
|
-
return (
|
|
15
|
-
<button
|
|
16
|
-
{...props}
|
|
17
|
-
className={classnames('ActionButton', className)}
|
|
18
|
-
onClick={handleClick}
|
|
19
|
-
title="Close View"></button>
|
|
20
|
-
);
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export default ActionButton;
|
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
import classnames from "classnames";
|
|
2
|
-
import React, {
|
|
3
|
-
HTMLAttributes,
|
|
4
|
-
KeyboardEvent,
|
|
5
|
-
MouseEvent,
|
|
6
|
-
ReactElement,
|
|
7
|
-
useRef,
|
|
8
|
-
useState
|
|
9
|
-
} from "react";
|
|
10
|
-
import { Contribution, useViewDispatch } from "../layout-view";
|
|
11
|
-
|
|
12
|
-
import {
|
|
13
|
-
EditableLabel,
|
|
14
|
-
Toolbar,
|
|
15
|
-
ToolbarButton,
|
|
16
|
-
ToolbarField,
|
|
17
|
-
Tooltray
|
|
18
|
-
} from "@heswell/salt-lab";
|
|
19
|
-
import { CloseIcon } from "@salt-ds/icons";
|
|
20
|
-
|
|
21
|
-
import "./Header.css";
|
|
22
|
-
|
|
23
|
-
export interface HeaderProps extends HTMLAttributes<HTMLDivElement> {
|
|
24
|
-
collapsed?: boolean;
|
|
25
|
-
contributions?: Contribution[];
|
|
26
|
-
expanded?: boolean;
|
|
27
|
-
closeable?: boolean;
|
|
28
|
-
onEditTitle: (value: string) => void;
|
|
29
|
-
orientation?: "horizontal" | "vertical";
|
|
30
|
-
tearOut?: boolean;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export const Header = ({
|
|
34
|
-
className: classNameProp,
|
|
35
|
-
contributions,
|
|
36
|
-
collapsed,
|
|
37
|
-
closeable,
|
|
38
|
-
onEditTitle,
|
|
39
|
-
orientation: orientationProp = "horizontal",
|
|
40
|
-
style,
|
|
41
|
-
title = "Untitled",
|
|
42
|
-
}: HeaderProps) => {
|
|
43
|
-
const labelFieldRef = useRef<HTMLDivElement>(null);
|
|
44
|
-
const [value, setValue] = useState<string>(title);
|
|
45
|
-
const [editing, setEditing] = useState<boolean>(false);
|
|
46
|
-
|
|
47
|
-
const viewDispatch = useViewDispatch();
|
|
48
|
-
const handleClose = (evt: MouseEvent) =>
|
|
49
|
-
viewDispatch?.({ type: "remove" }, evt);
|
|
50
|
-
const classBase = "vuuHeader";
|
|
51
|
-
|
|
52
|
-
const handleTitleMouseDown = () => {
|
|
53
|
-
labelFieldRef.current?.focus();
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const handleButtonMouseDown = (evt: MouseEvent) => {
|
|
57
|
-
// do not allow drag to be initiated
|
|
58
|
-
evt.stopPropagation();
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
const orientation = collapsed || orientationProp;
|
|
62
|
-
|
|
63
|
-
const className = classnames(
|
|
64
|
-
classBase,
|
|
65
|
-
classNameProp,
|
|
66
|
-
`${classBase}-${orientation}`
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
const handleEnterEditMode = () => {
|
|
70
|
-
setEditing(true);
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const handleTitleKeyDown = (evt: KeyboardEvent<HTMLDivElement>) => {
|
|
74
|
-
if (evt.key === "Enter") {
|
|
75
|
-
setEditing(true);
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const handleExitEditMode = (
|
|
80
|
-
originalValue = "",
|
|
81
|
-
finalValue = "",
|
|
82
|
-
allowDeactivation = true,
|
|
83
|
-
editCancelled = false
|
|
84
|
-
) => {
|
|
85
|
-
setEditing(false);
|
|
86
|
-
if (editCancelled) {
|
|
87
|
-
setValue(originalValue);
|
|
88
|
-
} else if (finalValue !== originalValue) {
|
|
89
|
-
setValue(finalValue);
|
|
90
|
-
onEditTitle?.(finalValue);
|
|
91
|
-
}
|
|
92
|
-
if (allowDeactivation === false) {
|
|
93
|
-
labelFieldRef.current?.focus();
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const handleMouseDown = (e: MouseEvent) => {
|
|
98
|
-
viewDispatch?.({ type: "mousedown" }, e);
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
const toolbarItems: ReactElement[] = [];
|
|
102
|
-
const contributedItems: ReactElement[] = [];
|
|
103
|
-
const actionButtons: ReactElement[] = [];
|
|
104
|
-
|
|
105
|
-
title &&
|
|
106
|
-
toolbarItems.push(
|
|
107
|
-
<ToolbarField className="vuuHeader-title" key="title">
|
|
108
|
-
<EditableLabel
|
|
109
|
-
editing={editing}
|
|
110
|
-
key="title"
|
|
111
|
-
value={value}
|
|
112
|
-
onChange={setValue}
|
|
113
|
-
onMouseDownCapture={handleTitleMouseDown}
|
|
114
|
-
onEnterEditMode={handleEnterEditMode}
|
|
115
|
-
onExitEditMode={handleExitEditMode}
|
|
116
|
-
onKeyDown={handleTitleKeyDown}
|
|
117
|
-
ref={labelFieldRef}
|
|
118
|
-
tabIndex={0}
|
|
119
|
-
/>
|
|
120
|
-
</ToolbarField>
|
|
121
|
-
);
|
|
122
|
-
|
|
123
|
-
contributions?.forEach((contribution, i) => {
|
|
124
|
-
contributedItems.push(React.cloneElement(contribution.content, { key: i }));
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
closeable &&
|
|
128
|
-
actionButtons.push(
|
|
129
|
-
<ToolbarButton
|
|
130
|
-
key="close"
|
|
131
|
-
onClick={handleClose}
|
|
132
|
-
onMouseDown={handleButtonMouseDown}
|
|
133
|
-
>
|
|
134
|
-
<CloseIcon /> Close
|
|
135
|
-
</ToolbarButton>
|
|
136
|
-
);
|
|
137
|
-
|
|
138
|
-
contributedItems.length > 0 &&
|
|
139
|
-
toolbarItems.push(
|
|
140
|
-
<Tooltray data-align-end key="contributions">
|
|
141
|
-
{contributedItems}
|
|
142
|
-
</Tooltray>
|
|
143
|
-
);
|
|
144
|
-
|
|
145
|
-
actionButtons.length > 0 &&
|
|
146
|
-
toolbarItems.push(
|
|
147
|
-
<Tooltray data-align-end key="actions">
|
|
148
|
-
{actionButtons}
|
|
149
|
-
</Tooltray>
|
|
150
|
-
);
|
|
151
|
-
|
|
152
|
-
return (
|
|
153
|
-
<Toolbar
|
|
154
|
-
className={className}
|
|
155
|
-
orientation={orientationProp}
|
|
156
|
-
style={style}
|
|
157
|
-
onMouseDown={handleMouseDown}
|
|
158
|
-
>
|
|
159
|
-
{toolbarItems}
|
|
160
|
-
{/*
|
|
161
|
-
{collapsed === false ? (
|
|
162
|
-
<ActionButton
|
|
163
|
-
aria-label="Minimize View"
|
|
164
|
-
actionId="minimize"
|
|
165
|
-
iconName="minimize"
|
|
166
|
-
onClick={handleAction}
|
|
167
|
-
onMouseDown={handleButtonMouseDown}
|
|
168
|
-
/>
|
|
169
|
-
) : null}
|
|
170
|
-
{collapsed ? (
|
|
171
|
-
<ActionButton
|
|
172
|
-
aria-label="Restore View"
|
|
173
|
-
actionId="restore"
|
|
174
|
-
iconName="double-chevron-right"
|
|
175
|
-
onClick={handleAction}
|
|
176
|
-
onMouseDown={handleButtonMouseDown}
|
|
177
|
-
/>
|
|
178
|
-
) : null}
|
|
179
|
-
{expanded === false ? (
|
|
180
|
-
<ActionButton
|
|
181
|
-
aria-label="Maximize View"
|
|
182
|
-
actionId="maximize"
|
|
183
|
-
iconName="maximize"
|
|
184
|
-
onClick={handleAction}
|
|
185
|
-
onMouseDown={handleButtonMouseDown}
|
|
186
|
-
/>
|
|
187
|
-
) : null}
|
|
188
|
-
{expanded ? (
|
|
189
|
-
<ActionButton
|
|
190
|
-
aria-label="Restore View"
|
|
191
|
-
actionId="restore"
|
|
192
|
-
iconName="restore"
|
|
193
|
-
onClick={handleAction}
|
|
194
|
-
onMouseDown={handleButtonMouseDown}
|
|
195
|
-
/>
|
|
196
|
-
) : null}
|
|
197
|
-
{tearOut ? (
|
|
198
|
-
<ActionButton
|
|
199
|
-
aria-label="Tear out View"
|
|
200
|
-
actionId="tearout"
|
|
201
|
-
iconName="tear-out"
|
|
202
|
-
onClick={handleAction}
|
|
203
|
-
onMouseDown={handleButtonMouseDown}
|
|
204
|
-
/>
|
|
205
|
-
) : null}
|
|
206
|
-
{closeable ? (
|
|
207
|
-
<Button
|
|
208
|
-
aria-label="close"
|
|
209
|
-
data-icon
|
|
210
|
-
onClick={handleClose}
|
|
211
|
-
onMouseDown={handleButtonMouseDown}
|
|
212
|
-
/>
|
|
213
|
-
) : null} */}
|
|
214
|
-
</Toolbar>
|
|
215
|
-
);
|
|
216
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './Header';
|
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
MutableRefObject,
|
|
3
|
-
ReactElement,
|
|
4
|
-
useCallback,
|
|
5
|
-
useContext,
|
|
6
|
-
useEffect,
|
|
7
|
-
useRef,
|
|
8
|
-
useState
|
|
9
|
-
} from "react";
|
|
10
|
-
import {
|
|
11
|
-
LayoutActionType,
|
|
12
|
-
layoutFromJson,
|
|
13
|
-
LayoutJSON,
|
|
14
|
-
layoutReducer,
|
|
15
|
-
LayoutReducerAction,
|
|
16
|
-
layoutToJSON,
|
|
17
|
-
processLayoutElement
|
|
18
|
-
} from "../layout-reducer";
|
|
19
|
-
import { findTarget, getChildProp, getProps, typeOf } from "../utils";
|
|
20
|
-
import {
|
|
21
|
-
LayoutProviderContext,
|
|
22
|
-
LayoutProviderDispatch
|
|
23
|
-
} from "./LayoutProviderContext";
|
|
24
|
-
import { useLayoutDragDrop } from "./useLayoutDragDrop";
|
|
25
|
-
|
|
26
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
27
|
-
const withDropTarget = (props: any) => props.dropTarget;
|
|
28
|
-
const shouldSave = (action: LayoutReducerAction) =>
|
|
29
|
-
[
|
|
30
|
-
"drag-drop",
|
|
31
|
-
"remove",
|
|
32
|
-
"set-title",
|
|
33
|
-
"splitter-resize",
|
|
34
|
-
"switch-tab",
|
|
35
|
-
].includes(action.type);
|
|
36
|
-
|
|
37
|
-
type LayoutChangeHandler = (layout: LayoutJSON, source: string) => void;
|
|
38
|
-
|
|
39
|
-
export interface LayoutProviderProps {
|
|
40
|
-
children: ReactElement;
|
|
41
|
-
layout?: LayoutJSON;
|
|
42
|
-
onLayoutChange?: LayoutChangeHandler;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export const LayoutProviderVersion = () => {
|
|
46
|
-
const version = useLayoutProviderVersion();
|
|
47
|
-
return <div>{`Context: ${version} `}</div>;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export const LayoutProvider = (props: LayoutProviderProps): ReactElement => {
|
|
51
|
-
const { children, layout, onLayoutChange } = props;
|
|
52
|
-
const state = useRef<ReactElement | undefined>(undefined);
|
|
53
|
-
const childrenRef = useRef<ReactElement>(children);
|
|
54
|
-
|
|
55
|
-
const [, forceRefresh] = useState<unknown>(null);
|
|
56
|
-
|
|
57
|
-
const serializeState = useCallback(
|
|
58
|
-
(source) => {
|
|
59
|
-
if (onLayoutChange) {
|
|
60
|
-
const targetContainer =
|
|
61
|
-
findTarget(source, withDropTarget) || state.current;
|
|
62
|
-
const isDraggableLayout = typeOf(targetContainer) === "DraggableLayout";
|
|
63
|
-
const target = isDraggableLayout
|
|
64
|
-
? getProps(targetContainer).children[0]
|
|
65
|
-
: targetContainer;
|
|
66
|
-
const serializedModel = layoutToJSON(target);
|
|
67
|
-
onLayoutChange(serializedModel, "drag-root");
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
[onLayoutChange]
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
const dispatchLayoutAction = useCallback(
|
|
74
|
-
(action: LayoutReducerAction, suppressSave = false) => {
|
|
75
|
-
const nextState = layoutReducer(state.current as ReactElement, action);
|
|
76
|
-
if (nextState !== state.current) {
|
|
77
|
-
state.current = nextState;
|
|
78
|
-
forceRefresh({});
|
|
79
|
-
if (!suppressSave && shouldSave(action)) {
|
|
80
|
-
serializeState(nextState);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
},
|
|
84
|
-
[serializeState]
|
|
85
|
-
);
|
|
86
|
-
|
|
87
|
-
const layoutActionDispatcher: LayoutProviderDispatch = useCallback(
|
|
88
|
-
(action) => {
|
|
89
|
-
switch(action.type) {
|
|
90
|
-
case "drag-start": {
|
|
91
|
-
prepareToDragLayout(action); break
|
|
92
|
-
}
|
|
93
|
-
case "save": {
|
|
94
|
-
serializeState(state.current); break
|
|
95
|
-
}
|
|
96
|
-
default: {
|
|
97
|
-
dispatchLayoutAction(action); break
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
},
|
|
101
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
102
|
-
[dispatchLayoutAction, serializeState]
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
const prepareToDragLayout = useLayoutDragDrop(
|
|
106
|
-
state as MutableRefObject<ReactElement>,
|
|
107
|
-
layoutActionDispatcher
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
useEffect(() => {
|
|
111
|
-
if (layout) {
|
|
112
|
-
const targetContainer = findTarget(
|
|
113
|
-
state.current as never,
|
|
114
|
-
withDropTarget
|
|
115
|
-
) as ReactElement;
|
|
116
|
-
const target = getChildProp(targetContainer);
|
|
117
|
-
const newLayout = layoutFromJson(
|
|
118
|
-
layout,
|
|
119
|
-
`${targetContainer.props.path}.0`
|
|
120
|
-
);
|
|
121
|
-
const action = target
|
|
122
|
-
? {
|
|
123
|
-
type: LayoutActionType.REPLACE,
|
|
124
|
-
target,
|
|
125
|
-
replacement: newLayout,
|
|
126
|
-
}
|
|
127
|
-
: {
|
|
128
|
-
type: LayoutActionType.ADD,
|
|
129
|
-
path: targetContainer.props.path,
|
|
130
|
-
component: newLayout,
|
|
131
|
-
};
|
|
132
|
-
dispatchLayoutAction(action, true);
|
|
133
|
-
}
|
|
134
|
-
}, [dispatchLayoutAction, layout]);
|
|
135
|
-
|
|
136
|
-
if (state.current === undefined) {
|
|
137
|
-
state.current = processLayoutElement(children);
|
|
138
|
-
} else if (children !== childrenRef.current) {
|
|
139
|
-
state.current = processLayoutElement(children, state.current);
|
|
140
|
-
childrenRef.current = children;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return (
|
|
144
|
-
<LayoutProviderContext.Provider
|
|
145
|
-
value={{ dispatchLayoutProvider: layoutActionDispatcher, version: 0 }}
|
|
146
|
-
>
|
|
147
|
-
{state.current}
|
|
148
|
-
</LayoutProviderContext.Provider>
|
|
149
|
-
);
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
export const useLayoutProviderDispatch = () => {
|
|
153
|
-
const { dispatchLayoutProvider } = useContext(LayoutProviderContext);
|
|
154
|
-
return dispatchLayoutProvider;
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
export const useLayoutProviderVersion = () => {
|
|
158
|
-
console.log({ LayoutProviderContext });
|
|
159
|
-
const { version } = useContext(LayoutProviderContext);
|
|
160
|
-
return version;
|
|
161
|
-
};
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { createContext, Dispatch } from 'react';
|
|
2
|
-
import { DragStartAction, LayoutReducerAction, SaveAction } from '../layout-reducer';
|
|
3
|
-
|
|
4
|
-
const unconfiguredLayoutProviderDispatch: LayoutProviderDispatch = (action) =>
|
|
5
|
-
console.log(`dispatch ${action.type}, have you forgotten to provide a LayoutProvider ?`);
|
|
6
|
-
|
|
7
|
-
export type LayoutProviderDispatch = Dispatch<LayoutReducerAction | SaveAction | DragStartAction>;
|
|
8
|
-
|
|
9
|
-
export interface LayoutProviderContextProps {
|
|
10
|
-
dispatchLayoutProvider: LayoutProviderDispatch;
|
|
11
|
-
version: number;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export const LayoutProviderContext = createContext<LayoutProviderContextProps>({
|
|
15
|
-
dispatchLayoutProvider: unconfiguredLayoutProviderDispatch,
|
|
16
|
-
version: -1
|
|
17
|
-
});
|
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
import { MutableRefObject, ReactElement, useCallback, useRef } from "react";
|
|
2
|
-
import {
|
|
3
|
-
DragDropRect,
|
|
4
|
-
DragEndCallback,
|
|
5
|
-
Draggable,
|
|
6
|
-
DragInstructions
|
|
7
|
-
} from "../drag-drop";
|
|
8
|
-
import { DragStartAction } from "../layout-reducer";
|
|
9
|
-
import { getIntrinsicSize } from "../layout-reducer/flexUtils";
|
|
10
|
-
import { followPath } from "../utils";
|
|
11
|
-
import { LayoutProviderDispatch } from "./LayoutProviderContext";
|
|
12
|
-
|
|
13
|
-
const NO_INSTRUCTIONS = {} as DragInstructions;
|
|
14
|
-
const NO_OFFSETS: [number, number] = [0, 0];
|
|
15
|
-
|
|
16
|
-
interface CurrentDragAction extends Omit<DragStartAction, "evt" | "type"> {
|
|
17
|
-
dragContainerPath: string;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
interface DragOperation {
|
|
21
|
-
payload: ReactElement;
|
|
22
|
-
originalCSS: string;
|
|
23
|
-
dragRect: unknown;
|
|
24
|
-
dragInstructions: DragInstructions;
|
|
25
|
-
dragOffsets: [number, number];
|
|
26
|
-
targetPosition: { left: number; top: number };
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const getDragElement = (
|
|
30
|
-
rect: DragDropRect,
|
|
31
|
-
id: string,
|
|
32
|
-
dragElement?: HTMLElement
|
|
33
|
-
): [HTMLElement, string, number, number] => {
|
|
34
|
-
const wrapper = document.createElement("div");
|
|
35
|
-
wrapper.className = "vuuSimpleDraggableWrapper";
|
|
36
|
-
wrapper.classList.add(
|
|
37
|
-
"vuuSimpleDraggableWrapper",
|
|
38
|
-
"salt-theme",
|
|
39
|
-
"salt-density-medium"
|
|
40
|
-
);
|
|
41
|
-
wrapper.dataset.dragging = "true";
|
|
42
|
-
|
|
43
|
-
const div = dragElement ?? document.createElement("div");
|
|
44
|
-
div.id = id;
|
|
45
|
-
|
|
46
|
-
wrapper.appendChild(div);
|
|
47
|
-
document.body.appendChild(wrapper);
|
|
48
|
-
const cssText = `top:${rect.top}px;left:${rect.left}px;width:${rect.width}px;height:${rect.height}px;`;
|
|
49
|
-
return [wrapper, cssText, rect.left, rect.top];
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const determineDragOffsets = (
|
|
53
|
-
draggedElement: HTMLElement
|
|
54
|
-
): [number, number] => {
|
|
55
|
-
const { offsetParent } = draggedElement;
|
|
56
|
-
if (offsetParent === null) {
|
|
57
|
-
return NO_OFFSETS;
|
|
58
|
-
} else {
|
|
59
|
-
const { left: offsetLeft, top: offsetTop } =
|
|
60
|
-
offsetParent.getBoundingClientRect();
|
|
61
|
-
return [offsetLeft, offsetTop];
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
export const useLayoutDragDrop = (
|
|
66
|
-
rootLayoutRef: MutableRefObject<ReactElement>,
|
|
67
|
-
dispatch: LayoutProviderDispatch
|
|
68
|
-
) => {
|
|
69
|
-
const dragActionRef = useRef<CurrentDragAction>();
|
|
70
|
-
const dragOperationRef = useRef<DragOperation>();
|
|
71
|
-
const draggableHTMLElementRef = useRef<HTMLElement>();
|
|
72
|
-
|
|
73
|
-
const handleDrag = useCallback((x, y) => {
|
|
74
|
-
if (dragOperationRef.current && draggableHTMLElementRef.current) {
|
|
75
|
-
const {
|
|
76
|
-
dragOffsets: [offsetX, offsetY],
|
|
77
|
-
targetPosition,
|
|
78
|
-
} = dragOperationRef.current;
|
|
79
|
-
const left = typeof x === "number" ? x - offsetX : targetPosition.left;
|
|
80
|
-
const top = typeof y === "number" ? y - offsetY : targetPosition.top;
|
|
81
|
-
if (left !== targetPosition.left || top !== targetPosition.top) {
|
|
82
|
-
dragOperationRef.current.targetPosition.left = left;
|
|
83
|
-
dragOperationRef.current.targetPosition.top = top;
|
|
84
|
-
draggableHTMLElementRef.current.style.top = top + "px";
|
|
85
|
-
draggableHTMLElementRef.current.style.left = left + "px";
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}, []);
|
|
89
|
-
|
|
90
|
-
const handleDrop: DragEndCallback = useCallback((dropTarget) => {
|
|
91
|
-
if (dragOperationRef.current) {
|
|
92
|
-
const {
|
|
93
|
-
dragInstructions,
|
|
94
|
-
payload: draggedReactElement,
|
|
95
|
-
originalCSS,
|
|
96
|
-
} = dragOperationRef.current;
|
|
97
|
-
dispatch({
|
|
98
|
-
type: "drag-drop",
|
|
99
|
-
draggedReactElement,
|
|
100
|
-
dragInstructions,
|
|
101
|
-
dropTarget,
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
console.log(`[useLayoutDragDrop]`, {
|
|
105
|
-
dragInstructions,
|
|
106
|
-
});
|
|
107
|
-
if (draggableHTMLElementRef.current) {
|
|
108
|
-
if (dragInstructions.RemoveDraggableOnDragEnd) {
|
|
109
|
-
document.body.removeChild(draggableHTMLElementRef.current);
|
|
110
|
-
} else {
|
|
111
|
-
draggableHTMLElementRef.current.style.cssText = originalCSS;
|
|
112
|
-
delete draggableHTMLElementRef.current.dataset.dragging;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
dragActionRef.current = undefined;
|
|
117
|
-
dragOperationRef.current = undefined;
|
|
118
|
-
draggableHTMLElementRef.current = undefined;
|
|
119
|
-
}
|
|
120
|
-
}, [dispatch]);
|
|
121
|
-
|
|
122
|
-
const handleDragStart = useCallback(
|
|
123
|
-
(evt: MouseEvent) => {
|
|
124
|
-
if (dragActionRef.current) {
|
|
125
|
-
const {
|
|
126
|
-
payload: component,
|
|
127
|
-
dragContainerPath,
|
|
128
|
-
dragElement,
|
|
129
|
-
dragRect,
|
|
130
|
-
instructions = NO_INSTRUCTIONS,
|
|
131
|
-
path,
|
|
132
|
-
} = dragActionRef.current;
|
|
133
|
-
const { current: rootLayout } = rootLayoutRef;
|
|
134
|
-
const dragPos = { x: evt.clientX, y: evt.clientY };
|
|
135
|
-
const dragPayload = component ?? followPath(rootLayout, path, true);
|
|
136
|
-
const { id: dragPayloadId } = dragPayload.props;
|
|
137
|
-
const intrinsicSize = getIntrinsicSize(dragPayload);
|
|
138
|
-
let originalCSS = "",
|
|
139
|
-
dragCSS = "",
|
|
140
|
-
dragTransform = "";
|
|
141
|
-
|
|
142
|
-
let dragStartLeft = -1;
|
|
143
|
-
let dragStartTop = -1;
|
|
144
|
-
let dragOffsets: [number, number] = NO_OFFSETS;
|
|
145
|
-
|
|
146
|
-
let element = document.getElementById(dragPayloadId);
|
|
147
|
-
|
|
148
|
-
if (element === null) {
|
|
149
|
-
[element, dragCSS, dragStartLeft, dragStartTop] = getDragElement(
|
|
150
|
-
dragRect,
|
|
151
|
-
dragPayloadId,
|
|
152
|
-
dragElement
|
|
153
|
-
);
|
|
154
|
-
} else {
|
|
155
|
-
dragOffsets = determineDragOffsets(element);
|
|
156
|
-
const [offsetLeft, offsetTop] = dragOffsets;
|
|
157
|
-
const { width, height, left, top } = element.getBoundingClientRect();
|
|
158
|
-
dragStartLeft = left - offsetLeft;
|
|
159
|
-
dragStartTop = top - offsetTop;
|
|
160
|
-
dragCSS = `width:${width}px;height:${height}px;left:${dragStartLeft}px;top:${dragStartTop}px;z-index: 100;background-color:#ccc;opacity: 0.6;`;
|
|
161
|
-
element.dataset.dragging = "true";
|
|
162
|
-
originalCSS = element.style.cssText;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
dragTransform = Draggable.initDrag(
|
|
166
|
-
rootLayoutRef.current,
|
|
167
|
-
dragContainerPath,
|
|
168
|
-
dragRect,
|
|
169
|
-
dragPos,
|
|
170
|
-
{
|
|
171
|
-
drag: handleDrag,
|
|
172
|
-
drop: handleDrop,
|
|
173
|
-
},
|
|
174
|
-
intrinsicSize
|
|
175
|
-
// dropTargets
|
|
176
|
-
);
|
|
177
|
-
|
|
178
|
-
element.style.cssText = dragCSS + dragTransform;
|
|
179
|
-
draggableHTMLElementRef.current = element;
|
|
180
|
-
|
|
181
|
-
dragOperationRef.current = {
|
|
182
|
-
payload: dragPayload,
|
|
183
|
-
originalCSS,
|
|
184
|
-
dragRect,
|
|
185
|
-
dragOffsets,
|
|
186
|
-
dragInstructions: instructions,
|
|
187
|
-
targetPosition: { left: dragStartLeft, top: dragStartTop },
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
},
|
|
191
|
-
[handleDrag, handleDrop, rootLayoutRef]
|
|
192
|
-
);
|
|
193
|
-
|
|
194
|
-
const prepareToDrag = useCallback(
|
|
195
|
-
(action: DragStartAction) => {
|
|
196
|
-
const { evt, ...options } = action;
|
|
197
|
-
console.log(`prepare to drag`, {
|
|
198
|
-
options,
|
|
199
|
-
});
|
|
200
|
-
dragActionRef.current = {
|
|
201
|
-
...options,
|
|
202
|
-
dragContainerPath: "",
|
|
203
|
-
};
|
|
204
|
-
Draggable.handleMousedown(evt, handleDragStart, options.instructions);
|
|
205
|
-
},
|
|
206
|
-
[handleDragStart]
|
|
207
|
-
);
|
|
208
|
-
|
|
209
|
-
return prepareToDrag;
|
|
210
|
-
};
|