Skip to content

Commit

Permalink
Add support for removing the initial state (#264)
Browse files Browse the repository at this point in the history
* Add support for removing the initial state

* Update Studio

* Update Studio

* Update Studio

* Make it possible to move a state to the root

* Fix an issue with reparenting

* Update Studio

* Update Studio
  • Loading branch information
Andarist authored Dec 5, 2022
1 parent a998e90 commit 42efe01
Show file tree
Hide file tree
Showing 7 changed files with 1,196 additions and 1,049 deletions.
1,037 changes: 0 additions & 1,037 deletions apps/extension/client/bundled-editor/assets/index.6be67cf0.js

This file was deleted.

1,037 changes: 1,037 additions & 0 deletions apps/extension/client/bundled-editor/assets/index.93e42c6d.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/extension/client/bundled-editor/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Stately Studio</title>
<script type="module" crossorigin src="./assets/index.6be67cf0.js"></script>
<script type="module" crossorigin src="./assets/index.93e42c6d.js"></script>
</head>
<body>
<div id="root"></div>
Expand Down
28 changes: 22 additions & 6 deletions packages/machine-extractor/src/MachineExtractResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ export type MachineEdit =
| { type: 'remove_state'; path: string[] }
| { type: 'rename_state'; path: string[]; name: string }
| { type: 'reparent_state'; path: string[]; newParentPath: string[] }
| { type: 'set_initial_state'; path: string[]; initialState: string }
| {
type: 'set_initial_state';
path: string[];
initialState?: string | undefined;
}
| { type: 'set_state_id'; path: string[]; id?: string }
| {
type: 'set_state_type';
Expand Down Expand Up @@ -947,9 +951,6 @@ export class MachineExtractResult {
if (edit.path.length === 0) {
throw new Error(`Root state can't be moved.`);
}
if (edit.newParentPath.length === 0) {
throw new Error(`State can't be moved to the root.`);
}
if (
arePathsEqual(
edit.path,
Expand Down Expand Up @@ -1127,7 +1128,16 @@ export class MachineExtractResult {
edit.path,
);

setProperty(stateObj, 'initial', b.stringLiteral(edit.initialState));
if (typeof edit.initialState === 'string') {
setProperty(
stateObj,
'initial',
b.stringLiteral(edit.initialState),
);
} else {
removeProperty(stateObj, 'initial');
}

break;
}
case 'set_state_id': {
Expand Down Expand Up @@ -2388,8 +2398,14 @@ function getBestTargetDescriptor(
return `.${targetPath.slice(sourcePath.length).join('.')}`;
}

const isTargetingAncestor = arePathsEqual(
sourcePath.slice(0, targetPath.length),
targetPath,
);

if (
// this transition targets a state within the renamed sibling
!isTargetingAncestor &&
// this transition might target a state within the renamed sibling
// we can't just update the segment to the empty string as that would result in targeting own descendant
targetPath[0] !== '' &&
arePathsEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,6 @@ describe('rename_state', () => {
})
`);

debugger;
expect(
modifiableMachine.modify([
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import { extractMachinesFromFile } from '../../extractMachinesFromFile';
const getModifiableMachine = (code: string) =>
extractMachinesFromFile(outdent.string(code))!.machines[0]!;

// TODO: change *some* targets within the moved state
// TODO: change some targets targeting the moved state (or its decendants)
// TODO: remove initial if it got moved?

describe('reparent_state', () => {
it('should be possible to move a state to a sibling of the parent', () => {
const modifiableMachine = getModifiableMachine(`
Expand Down Expand Up @@ -690,4 +686,74 @@ describe('reparent_state', () => {
}"
`);
});

it(`should be possible to move a state to the root`, () => {
const modifiableMachine = getModifiableMachine(`
createMachine({
states: {
a: {
states: {
a1: {},
}
},
}
})
`);

expect(
modifiableMachine.modify([
{
type: 'reparent_state',
path: ['a', 'a1'],
newParentPath: [],
},
]).configEdit.newText,
).toMatchInlineSnapshot(`
"{
states: {
a: {},
a1: {}
}
}"
`);
});

it(`should update the target descriptor correctly when moving a state to lie within the target`, () => {
const modifiableMachine = getModifiableMachine(`
createMachine({
states: {
a: {
on: {
FOO: 'b'
}
},
b: {}
}
})
`);

expect(
modifiableMachine.modify([
{
type: 'reparent_state',
path: ['a'],
newParentPath: ['b'],
},
]).configEdit.newText,
).toMatchInlineSnapshot(`
"{
states: {
b: {
states: {
a: {
on: {
FOO: "#(machine).b"
}
}
}
}
}
}"
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,70 @@ describe('set_initial_state', () => {
}"
`);
});

it('should be able to remove an initial state from a root', () => {
const modifiableMachine = getModifiableMachine(`
createMachine({
initial: 'foo',
states: {
foo: {},
bar: {},
},
})
`);

expect(
modifiableMachine.modify([
{
type: 'set_initial_state',
path: [],
initialState: undefined,
},
]).configEdit.newText,
).toMatchInlineSnapshot(`
"{
states: {
foo: {},
bar: {},
}
}"
`);
});

it('should be able to remove an initial state from a nested state', () => {
const modifiableMachine = getModifiableMachine(`
createMachine({
initial: 'foo',
states: {
foo: {
initial: 'bar',
states: {
bar: {},
}
},
},
})
`);

expect(
modifiableMachine.modify([
{
type: 'set_initial_state',
path: ['foo'],
initialState: undefined,
},
]).configEdit.newText,
).toMatchInlineSnapshot(`
"{
initial: 'foo',
states: {
foo: {
states: {
bar: {},
}
},
},
}"
`);
});
});

0 comments on commit 42efe01

Please sign in to comment.