|
@@ -1,13 +1,13 @@
|
|
|
import * as React from 'react';
|
|
|
import * as Icons from '@tabler/icons';
|
|
|
-import {Button, Divider, Group, Image, SimpleGrid, Space, Text, Title,} from '@mantine/core';
|
|
|
+import {Button, Divider, Group, Image, SimpleGrid, Space, Text, Title, Stack} from '@mantine/core';
|
|
|
|
|
|
import {PrimitiveRegistry} from 'onion/primitive_delta';
|
|
|
import {PrimitiveValue} from 'onion/types';
|
|
|
import {mockUuid} from 'onion/test_helpers';
|
|
|
import {Actionblock, Resultblock} from './blocks';
|
|
|
import pdImage from './assets/pd.svg';
|
|
|
-import {newVersionedModel, undoButtonHelpText, VersionedModelState,} from '../versioned_model/single_model';
|
|
|
+import {newOnion, undoButtonHelpText, VersionedModelState} from '../versioned_model/single_model2';
|
|
|
import {InfoHoverCard} from '../info_hover_card';
|
|
|
|
|
|
export const demo_PD_description =
|
|
@@ -89,118 +89,39 @@ export const demo_PD_description =
|
|
|
|
|
|
|
|
|
export function getDemoPD() {
|
|
|
+ const primitiveRegistry = new PrimitiveRegistry();
|
|
|
+ const generateUUID = mockUuid();
|
|
|
const generateBranchID = mockUuid();
|
|
|
|
|
|
- const model = newVersionedModel({readonly: false});
|
|
|
-
|
|
|
- const initialState: [PrimitiveValue, VersionedModelState, any][] = [
|
|
|
- [generateBranchID().value, model.initialState, model],
|
|
|
- ];
|
|
|
+ const onion = newOnion({readonly: false, primitiveRegistry});
|
|
|
|
|
|
+ // Functional component
|
|
|
return function () {
|
|
|
- const [globalState, setGlobalState] = React.useState<[PrimitiveValue, VersionedModelState, any][]>(initialState);
|
|
|
-
|
|
|
- const getSetBranchState = i => {
|
|
|
- return callback => {
|
|
|
- setGlobalState(prevGlobalState => {
|
|
|
- const copy = prevGlobalState.slice();
|
|
|
- const [branchName, prevBranchState, m] = copy[i];
|
|
|
- copy[i] = [branchName, callback(prevBranchState), m];
|
|
|
- return copy;
|
|
|
- });
|
|
|
- };
|
|
|
- }
|
|
|
-
|
|
|
- return <div style={{minWidth: 1300}}>{globalState.map(([branchName, branchState, {
|
|
|
- getCurrentVersion,
|
|
|
- getReducer,
|
|
|
- getReactComponents
|
|
|
- }], i) => {
|
|
|
- const setBranchState = getSetBranchState(i);
|
|
|
-
|
|
|
- const reducer = getReducer(setBranchState);
|
|
|
-
|
|
|
- const components = getReactComponents(branchState, setBranchState, {
|
|
|
- onUserEdit: reducer.createAndGotoNewVersion,
|
|
|
- onUndoClicked: reducer.undo,
|
|
|
- onRedoClicked: reducer.redo,
|
|
|
- onVersionClicked: reducer.gotoVersion,
|
|
|
- onMerge: reducer.appendVersions,
|
|
|
- });
|
|
|
-
|
|
|
- const unionClicked = () => {
|
|
|
- const [mergeWithBranchName, mergeWithBranchState, {getReducer: getOtherReducer}] = globalState[i - 1];
|
|
|
- const otherReducer = getOtherReducer(getSetBranchState(i - 1));
|
|
|
- const addRecursive = ([version, delta, _]) => {
|
|
|
- if (version.parents.length > 0) {
|
|
|
- addRecursive(version.parents[0])
|
|
|
- }
|
|
|
- otherReducer.addDeltasAndVersion(delta.deltas, delta.getDescription(), version.hash);
|
|
|
- }
|
|
|
- addRecursive(getCurrentVersion().parents[0]);
|
|
|
- setGlobalState(prevGlobalState => [
|
|
|
- ...prevGlobalState.slice(0, i),
|
|
|
- ...prevGlobalState.slice(i + 1),
|
|
|
- ]);
|
|
|
- }
|
|
|
-
|
|
|
- const cloneClicked = () => {
|
|
|
- {/*const newBranchName = prompt("Branch name: (ESC to cancel)", "branch");*/}
|
|
|
- const newBranchName = generateBranchID().value;
|
|
|
- if (newBranchName === null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- if (globalState.some(([existingBranchName]) => existingBranchName === newBranchName)) {
|
|
|
- alert("Branch with this name already exists!");
|
|
|
- return;
|
|
|
- }
|
|
|
- const newModel = newVersionedModel({readonly: false});
|
|
|
- const newBranchState = newModel.initialState;
|
|
|
-
|
|
|
- setGlobalState(prevGlobalState => [
|
|
|
- ...prevGlobalState.slice(0, i + 1),
|
|
|
- [newBranchName, newBranchState, newModel],
|
|
|
- ...prevGlobalState.slice(i + 1),
|
|
|
- ]);
|
|
|
-
|
|
|
- const setNewBranchState = getSetBranchState(i + 1);
|
|
|
-
|
|
|
- const reducer = newModel.getReducer(setNewBranchState);
|
|
|
- const compositeDeltas = [...getCurrentVersion()].reverse();
|
|
|
- compositeDeltas.forEach((c: any) => {
|
|
|
- reducer.createAndGotoNewVersion(c.deltas, c.getDescription());
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- return <div key={JSON.stringify(branchName)}>
|
|
|
- {i > 0 ? <><Space h="md"/><Divider my="sm"/></> : <></>}
|
|
|
- <SimpleGrid cols={3}>
|
|
|
- <div>
|
|
|
- <Text>State</Text>
|
|
|
- {components.graphStateComponent}
|
|
|
- </div>
|
|
|
- <div>
|
|
|
- <Text>History</Text>
|
|
|
- {components.historyComponentWithMerge}
|
|
|
- </div>
|
|
|
- <div>
|
|
|
- <Text>Deltas (L0)</Text>
|
|
|
- {components.deltaGraphL0Component}
|
|
|
- </div>
|
|
|
- </SimpleGrid>
|
|
|
- <Space h="md"/>
|
|
|
- <Group position="center">
|
|
|
- <Button onClick={unionClicked} compact disabled={i === 0}
|
|
|
- leftIcon={<Icons.IconChevronUp/>}>Union </Button>
|
|
|
- {components.undoButton}
|
|
|
- {components.redoButton}
|
|
|
- <InfoHoverCard>
|
|
|
- {undoButtonHelpText}
|
|
|
- </InfoHoverCard>
|
|
|
- <Button onClick={cloneClicked} compact rightIcon={<Icons.IconChevronDown/>}>Clone</Button>
|
|
|
- </Group>
|
|
|
- </div>
|
|
|
- })
|
|
|
- }</div>;
|
|
|
+ const {state, reducer, components} = onion.useOnion(reducer => ({}));
|
|
|
+
|
|
|
+ return <div style={{minWidth: 1300}}>
|
|
|
+ <SimpleGrid cols={3}>
|
|
|
+ <Stack>
|
|
|
+ <Title order={4}>State</Title>
|
|
|
+ {components.graphStateComponent}
|
|
|
+ <Group position="center">
|
|
|
+ {components.undoButton}
|
|
|
+ {components.redoButton}
|
|
|
+ <InfoHoverCard>
|
|
|
+ {undoButtonHelpText}
|
|
|
+ </InfoHoverCard>
|
|
|
+ </Group>
|
|
|
+ </Stack>
|
|
|
+ <Stack>
|
|
|
+ <Title order={4}>History</Title>
|
|
|
+ {components.historyComponentWithMerge}
|
|
|
+ </Stack>
|
|
|
+ <Stack>
|
|
|
+ <Title order={4}>Deltas</Title>
|
|
|
+ {components.deltaGraphL0Component}
|
|
|
+ </Stack>
|
|
|
+ </SimpleGrid>
|
|
|
+ <Space h="md"/>
|
|
|
+ </div>;
|
|
|
}
|
|
|
}
|