import { Machine } from 'xstate'; import { EditContext, EditSchema, EditEvent } from '../index.data'; import { guards, actions } from './helper'; import * as R from 'rambda'; import { useEventBus } from 'valor-hooks'; import { BEFORE_EXIT_EDITING, EDITOR_MACHINE_EVENT_BUS } from './constants'; function getMachine(defaultContext = {}) { return Machine({ id: 'edit-machine', context: { usage: 'editor', ...defaultContext } as EditContext, initial: 'idle', on: { 'SELECT.CELL': { target: 'selecting.cell', actions: actions.reSelect, }, 'SELECT.ROW': { target: 'selecting.row', actions: actions.reSelect, }, 'SELECT.COLUMN': { target: 'selecting.column', actions: actions.reSelect, }, 'SELECT.NONE': { target: 'idle', actions: actions.clearSelect, }, EDITING: { target: 'editing', actions: actions.activeCurrentCell, }, 'SELECT.MOVE': { target: 'selecting.cell', actions: actions.moveSelect, }, }, states: { idle: { on: { 'MOUSE.DOWN': [ { cond: guards.isRowSelected, target: 'selecting.row', actions: actions.reSelect, }, { cond: guards.isCellSelected, target: 'selecting.cell', actions: actions.reSelect, }, { cond: guards.isColumnSelected, target: 'selecting.column', actions: actions.reSelect, }, ], }, }, selecting: { id: 'selecting', on: { 'MOUSE.DOWN': [ // 当单元格已选时, 再次点击现在并不进入editing(仿excel) // { // cond: guards.isCurrentCellSelected, // target: '#editing', // actions: actions.activeCurrentCell, // }, { cond: guards.isRowSelected, target: 'selecting.row', actions: actions.reSelect, }, { cond: guards.isCellSelected, target: 'selecting.cell', actions: actions.reSelect, }, { cond: guards.isColumnSelected, target: 'selecting.column', actions: actions.reSelect, }, ], 'MOUSE.MOVE': { actions: actions.appendSelect, }, }, states: { row: {}, column: {}, cell: { // id: 'cell', // on: { // 'KEY.ARROW': { // cond: guards.isSingleCellInContext, // actions: actions.moveSelect, // }, // 'KEY.ENTER': { // cond: guards.isSingleCellInContext, // target: '#editing', // actions: actions.activeCurrentCell, // }, // }, }, }, }, editing: { id: 'editing', type: 'parallel', initial: 'default', states: { default: { on: { 'PICK.ENTER': [ { target: 'picking.on', }, ], // 'MOUSE.DOWN': [ // { // // 必须排除picking = on的情形, 否则就无法pick了 // cond: guards.pickingIsOff_and_isNotActivedCellSelected, // target: '#selecting.cell', // actions: [ // () => { // const { dispatchEvent } = useEventBus(EDITOR_MACHINE_EVENT_BUS); // dispatchEvent(BEFORE_EXIT_EDITING, 'mousedown'); // }, // actions.restoreSelectingFromEditing, // ], // }, // ], }, }, picking: { initial: 'off', states: { on: { on: { // 发选择单元格的通知 'MOUSE.DOWN': { actions: actions.rePick, }, 'MOUSE.MOVE': { actions: actions.appendPick, }, 'MOUSE.UP': { actions: actions.appendPickedAndNotify, }, 'PICK.EXIT': { target: 'off', }, }, }, off: {}, }, }, }, }, }, }); } export default getMachine;