2023-10-01 00:11:05 +02:00
|
|
|
import { GridStack, GridStackNode } from 'fily-publish-gridstack';
|
2023-01-20 20:24:25 +01:00
|
|
|
import { MutableRefObject, RefObject } from 'react';
|
2023-10-01 00:11:05 +02:00
|
|
|
import { Item } from '~/components/Board/context';
|
2022-12-04 17:36:30 +01:00
|
|
|
|
|
|
|
|
export const initializeGridstack = (
|
|
|
|
|
areaType: 'wrapper' | 'category' | 'sidebar',
|
|
|
|
|
wrapperRef: RefObject<HTMLDivElement>,
|
|
|
|
|
gridRef: MutableRefObject<GridStack | undefined>,
|
|
|
|
|
itemRefs: MutableRefObject<Record<string, RefObject<HTMLDivElement>>>,
|
|
|
|
|
areaId: string,
|
2023-10-01 00:11:05 +02:00
|
|
|
items: Item[],
|
2022-12-04 17:36:30 +01:00
|
|
|
isEditMode: boolean,
|
2023-01-13 18:56:00 +09:00
|
|
|
wrapperColumnCount: number,
|
2023-01-06 23:50:08 +01:00
|
|
|
shapeSize: 'sm' | 'md' | 'lg',
|
2023-01-07 23:25:13 +01:00
|
|
|
tilesWithUnknownLocation: TileWithUnknownLocation[],
|
2022-12-04 17:36:30 +01:00
|
|
|
events: {
|
|
|
|
|
onChange: (changedNode: GridStackNode) => void;
|
|
|
|
|
onAdd: (addedNode: GridStackNode) => void;
|
|
|
|
|
}
|
|
|
|
|
) => {
|
|
|
|
|
if (!wrapperRef.current) return;
|
|
|
|
|
// calculates the currently available count of columns
|
2023-01-06 22:46:07 +01:00
|
|
|
const columnCount = areaType === 'sidebar' ? 2 : wrapperColumnCount;
|
2023-01-06 23:50:08 +01:00
|
|
|
const minRow = areaType !== 'sidebar' ? 1 : Math.floor(wrapperRef.current.offsetHeight / 128);
|
2022-12-04 17:36:30 +01:00
|
|
|
// initialize gridstack
|
2022-12-22 11:45:48 +09:00
|
|
|
const newGrid = gridRef;
|
|
|
|
|
newGrid.current = GridStack.init(
|
2022-12-04 17:36:30 +01:00
|
|
|
{
|
|
|
|
|
column: columnCount,
|
2023-01-19 11:13:03 +09:00
|
|
|
margin: areaType === 'sidebar' ? 5 : 10,
|
2023-01-06 23:50:08 +01:00
|
|
|
cellHeight: 128,
|
2022-12-04 17:36:30 +01:00
|
|
|
float: true,
|
|
|
|
|
alwaysShowResizeHandle: 'mobile',
|
|
|
|
|
acceptWidgets: true,
|
|
|
|
|
disableOneColumnMode: true,
|
|
|
|
|
staticGrid: !isEditMode,
|
|
|
|
|
minRow,
|
2023-01-06 22:46:07 +01:00
|
|
|
animate: false,
|
2022-12-04 17:36:30 +01:00
|
|
|
},
|
|
|
|
|
// selector of the gridstack item (it's eather category or wrapper)
|
|
|
|
|
`.grid-stack-${areaType}[data-${areaType}='${areaId}']`
|
|
|
|
|
);
|
2022-12-22 11:45:48 +09:00
|
|
|
const grid = newGrid.current;
|
2023-10-01 00:11:05 +02:00
|
|
|
if (!grid) return;
|
2023-01-06 22:46:07 +01:00
|
|
|
// Must be used to update the column count after the initialization
|
2023-01-17 21:35:17 +01:00
|
|
|
grid.column(columnCount, 'none');
|
2022-12-04 17:36:30 +01:00
|
|
|
|
|
|
|
|
// Add listener for moving items around in a wrapper
|
|
|
|
|
grid.on('change', (_, el) => {
|
|
|
|
|
const nodes = el as GridStackNode[];
|
|
|
|
|
const firstNode = nodes.at(0);
|
|
|
|
|
if (!firstNode) return;
|
|
|
|
|
events.onChange(firstNode);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Add listener for moving items in config from one wrapper to another
|
|
|
|
|
grid.on('added', (_, el) => {
|
|
|
|
|
const nodes = el as GridStackNode[];
|
|
|
|
|
const firstNode = nodes.at(0);
|
|
|
|
|
if (!firstNode) return;
|
|
|
|
|
events.onAdd(firstNode);
|
|
|
|
|
});
|
2022-12-19 21:27:44 +01:00
|
|
|
|
2022-12-04 17:36:30 +01:00
|
|
|
grid.batchUpdate();
|
|
|
|
|
grid.removeAll(false);
|
2023-10-01 00:11:05 +02:00
|
|
|
items.forEach((item) => {
|
|
|
|
|
const ref = itemRefs.current[item.id]?.current;
|
|
|
|
|
setAttributesFromShape(ref, item);
|
|
|
|
|
ref && grid.makeWidget(ref as HTMLDivElement);
|
|
|
|
|
/*if (!item && ref) {
|
|
|
|
|
const gridItemElement = ref as GridItemHTMLElement;
|
2023-01-07 23:25:13 +01:00
|
|
|
if (gridItemElement.gridstackNode) {
|
|
|
|
|
const { x, y, w, h } = gridItemElement.gridstackNode;
|
2023-10-01 00:11:05 +02:00
|
|
|
tilesWithUnknownLocation.push({ x, y, w, h, type: 'app', id: item.id });
|
2023-01-07 23:25:13 +01:00
|
|
|
}
|
2023-10-01 00:11:05 +02:00
|
|
|
}*/
|
2023-01-07 09:45:00 +01:00
|
|
|
});
|
2022-12-04 17:36:30 +01:00
|
|
|
grid.batchUpdate(false);
|
|
|
|
|
};
|
2023-01-06 23:50:08 +01:00
|
|
|
|
2023-10-01 00:11:05 +02:00
|
|
|
function setAttributesFromShape(ref: HTMLDivElement | null, item: Item) {
|
|
|
|
|
if (!item || !ref) return;
|
|
|
|
|
ref.setAttribute('gs-x', item.x.toString());
|
|
|
|
|
ref.setAttribute('gs-y', item.y.toString());
|
|
|
|
|
ref.setAttribute('gs-w', item.width.toString());
|
|
|
|
|
ref.setAttribute('gs-h', item.height.toString());
|
2023-01-06 23:50:08 +01:00
|
|
|
}
|
2023-01-07 23:25:13 +01:00
|
|
|
|
|
|
|
|
export type TileWithUnknownLocation = {
|
|
|
|
|
x?: number;
|
|
|
|
|
y?: number;
|
|
|
|
|
w?: number;
|
|
|
|
|
h?: number;
|
|
|
|
|
type: 'app' | 'widget';
|
|
|
|
|
id: string;
|
|
|
|
|
};
|