Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __tests__/src/components/Window.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MosaicWindowContext } from 'react-mosaic-component2';
import { MosaicWindowContext } from 'react-mosaic-component';
import { render, screen } from '@tests/utils/test-utils';

import { Window } from '../../../src/components/Window';
Expand Down
6 changes: 3 additions & 3 deletions __tests__/src/components/WorkspaceMosaic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('WorkspaceMosaic', () => {
const updateWorkspaceMosaicLayout = vi.fn();
const props = {
classes: {},
layout: { direction: 'row', first: '1', second: '2' },
layout: { type: 'split', direction: 'row', children: ['1', '2'] },
updateWorkspaceMosaicLayout,
windowIds,
workspaceId: 'foo',
Expand Down Expand Up @@ -89,13 +89,13 @@ describe('WorkspaceMosaic', () => {
it('when the new and old layouts are the same', () => {
const updateWorkspaceMosaicLayout = vi.fn();
wrapper = createWrapper({
layout: { direction: 'row', first: '1', second: '2' },
layout: { type: 'split', direction: 'row', children: ['1', '2'] },
updateWorkspaceMosaicLayout,
windowIds,
});

wrapper.rerender(
<WorkspaceMosaic classes={{}} windowIds={windowIds} layout={{ direction: 'row', first: '1', second: '2' }} workspaceId="foo" updateWorkspaceMosaicLayout={updateWorkspaceMosaicLayout} />,
<WorkspaceMosaic classes={{}} windowIds={windowIds} layout={{ type: 'split', direction: 'row', children: ['1', '2'] }} workspaceId="foo" updateWorkspaceMosaicLayout={updateWorkspaceMosaicLayout} />,
);

expect(updateWorkspaceMosaicLayout).toHaveBeenCalledTimes(0);
Expand Down
24 changes: 12 additions & 12 deletions __tests__/src/lib/MosaicLayout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,36 @@ describe('MosaicLayout', () => {
expect(instance.layout).toEqual('foo');
instance.addWindows(['bar']);
expect(instance.layout).toEqual({
type: 'split',
direction: 'row',
first: 'foo',
second: 'bar',
children: ['foo', 'bar'],
});
});
it('case 3 windows: adds to the top right', () => {
expect(instance.layout).toEqual('foo');
instance.addWindows(['bar', 'bat', 'bark']);
expect(instance.layout).toEqual({
type: 'split',
direction: 'row',
first: 'foo',
second: {
children: ['foo', {
type: 'split',
direction: 'column',
first: {
children: [{
type: 'split',
direction: 'row',
first: 'bat',
second: 'bark',
},
second: 'bar',
},
children: ['bat', 'bark'],
}, 'bar'],
}],
});
});
});
describe('removeWindows', () => {
let instance;
beforeEach(() => {
instance = new MosaicLayout({ first: 'foo', second: 'bar' });
instance = new MosaicLayout({ type: 'split', direction: 'row', children: ['foo', 'bar'] });
});
it('case 1 window: returns a single window', () => {
instance.removeWindows(['bar'], { bar: ['second'] });
instance.removeWindows(['bar'], { bar: [1] });
expect(instance.layout).toEqual('foo');
});
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"react-full-screen": "^1.1.1",
"react-image": "^4.0.1",
"react-intersection-observer": "^10.0.0",
"react-mosaic-component2": "^7.0.0",
"react-mosaic-component": "7.0.0-beta0",
"react-redux": "^8.0.0 || ^9.0.0",
"react-resize-observer": "^1.1.1",
"react-rnd": "~10.5.3",
Expand Down
2 changes: 1 addition & 1 deletion src/components/Window.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useContext, useCallback } from 'react';
import PropTypes from 'prop-types';
import { styled } from '@mui/material/styles';
import Paper from '@mui/material/Paper';
import { MosaicWindowContext } from 'react-mosaic-component2';
import { MosaicWindowContext } from 'react-mosaic-component';
import { ErrorBoundary } from 'react-error-boundary';
import { useTranslation } from 'react-i18next';
import ns from '../config/css-ns';
Expand Down
24 changes: 13 additions & 11 deletions src/components/WorkspaceMosaic.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { styled } from '@mui/material/styles';
import GlobalStyles from '@mui/material/GlobalStyles';
import { DndContext } from 'react-dnd';
import {
Mosaic, MosaicWindow, getLeaves, createBalancedTreeFromLeaves,
} from 'react-mosaic-component2';
Mosaic, MosaicWindow, getLeaves, createBalancedTreeFromLeaves, convertLegacyToNary
} from 'react-mosaic-component';
import difference from 'lodash/difference';
import isEqual from 'lodash/isEqual';
import classNames from 'classnames';
Expand Down Expand Up @@ -78,19 +78,20 @@ const determineWorkspaceLayout = (currentLayout, windowIds, currentWindowPaths =
export function WorkspaceMosaic({
layout = undefined, updateWorkspaceMosaicLayout, windowIds = [], workspaceId,
}) {
const toolbarControls = [];
const additionalControls = [];

const windowPaths = useRef({});
const dndContext = useContext(DndContext);

// Convert legacy layout format to new format if needed
const normalizedLayout = layout ? convertLegacyToNary(layout) : layout;

useEffect(() => {
const leaveKeys = getLeaves(layout);
const leaveKeys = getLeaves(normalizedLayout);
// Handle some trivial layout cases:
// 1. No layout
// 2. No windows
// 3. Not enough windows to create a layout
if (!layout || windowIds.length === 0 || leaveKeys.length < 2) {
if (!normalizedLayout || windowIds.length === 0 || leaveKeys.length < 2) {
updateWorkspaceMosaicLayout(createBalancedTreeFromLeaves(windowIds));

return undefined;
Expand All @@ -101,12 +102,14 @@ export function WorkspaceMosaic({
return undefined;
}

const newLayout = determineWorkspaceLayout(layout, windowIds, windowPaths.current);
const newLayout = determineWorkspaceLayout(normalizedLayout, windowIds, windowPaths.current);

if (!isEqual(newLayout, layout)) updateWorkspaceMosaicLayout(newLayout);
if (!isEqual(newLayout, normalizedLayout)) updateWorkspaceMosaicLayout(newLayout);

return undefined;
}, [layout, windowIds, windowPaths, updateWorkspaceMosaicLayout]);
}, [normalizedLayout, windowIds, windowPaths, updateWorkspaceMosaicLayout]);

const toolbarControls = [];

/**
* Render a tile (Window) in the Mosaic.
Expand All @@ -118,7 +121,6 @@ export function WorkspaceMosaic({
return (
<MosaicWindow
toolbarControls={toolbarControls}
additionalControls={additionalControls}
path={path}
windowId={id}
renderPreview={RenderPreview}
Expand All @@ -144,7 +146,7 @@ export function WorkspaceMosaic({
<StyledMosaic
dragAndDropManager={dndContext.dragDropManager}
renderTile={tileRenderer}
initialValue={layout || createBalancedTreeFromLeaves(windowIds)}
initialValue={normalizedLayout || createBalancedTreeFromLeaves(windowIds)}
onChange={mosaicChange}
className={classNames('mirador-mosaic')}
zeroStateView={<ZeroStateView />}
Expand Down
15 changes: 6 additions & 9 deletions src/lib/MosaicLayout.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
createRemoveUpdate, updateTree,
getNodeAtPath, getOtherDirection, getPathToCorner, Corner,
} from 'react-mosaic-component2';
} from 'react-mosaic-component';
import dropRight from 'lodash/dropRight';

/** */
Expand Down Expand Up @@ -38,22 +38,19 @@ export default class MosaicLayout {
const parent = this.pathToParent(path);
const destination = this.nodeAtPath(path);
const direction = parent ? getOtherDirection(parent.direction) : 'row';
let first;
let second;
let children;
if (direction === 'row') {
first = destination;
second = addedWindowIds[i];
children = [destination, addedWindowIds[i]];
} else {
first = addedWindowIds[i];
second = destination;
children = [addedWindowIds[i], destination];
}
const update = {
path,
spec: {
$set: {
type: 'split',
direction,
first,
second,
children,
},
},
};
Expand Down
Loading