Skip to content

Instantly share code, notes, and snippets.

@happylinks
Last active November 29, 2019 09:30
Show Gist options
  • Select an option

  • Save happylinks/e962d7b847a9684e63614b8d0fc0a735 to your computer and use it in GitHub Desktop.

Select an option

Save happylinks/e962d7b847a9684e63614b8d0fc0a735 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const layerMachine = Machine(
{
id: 'layer',
initial: 'idle',
states: {
idle: {
on: {
MOUSE_OVER: 'hovered',
},
},
hovered: {
on: {
MOUSE_OUT: 'idle',
MOUSE_DOWN: 'editing',
},
},
editing: {
on: {
CLICK: 'focused',
},
entry: ['hideLayer'],
},
focused: {
on: {
/* ESCAPE: */
BLUR: [
{
target: 'saving',
cond: 'contentValid',
actions: ['repositionLayer', 'showLayer', 'updateContent'],
},
{ target: 'idle' },
],
CHANGE: {
actions: assign({
editorState: (context, event) => {
if (event.type === 'CHANGE') {
return (context.editorState = event.editorState);
} else {
return context.editorState;
}
},
}),
},
},
entry: ['trackFocused'],
},
saving: {
invoke: {
id: 'updateCopy',
src: (context) =>
context.updateCopy(context.id, context.value),
onDone: {
target: 'success',
},
onError: {
target: 'failure',
},
},
entry: ['trackSaved'],
},
success: {
on: {
'': 'idle',
},
},
failure: {
on: {
'': 'idle',
},
},
},
},
{
actions: {
trackSaved: (context) => {
context.trackEvent({
name: `CopyMode.Text.Saved`,
});
},
trackFocused: (context) => {
context.trackEvent({
name: `CopyMode.Text.Focused`,
});
},
},
guards: {
contentValid: (context) => {
let editorContent = ''; // context.editorState
context.content !== editorContent;
},
},
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment