import shortid from 'shortid' ; import { Operation } from 'slate' ; const fieldForOperation = { insert_node : 'node' , split_node : 'properties' , } ; function isValidOperation ( operationType ) { return operationType in fieldForOperation ; } const applyIdToProperty = ( property ) => { const { children } = property ; const newId = shortid ( ) ; const op = { ... property , id : newId , ... ( children ? { children : children . map ( ( child ) => applyIdToProperty ( child ) ) } : { } ) , } ; return op ; } ; export const applyIdOnOperation = ( operation ) => { const { type } = operation ; if ( isValidOperation ( type ) ) { const propertyToChange = fieldForOperation [ type ] ; const ops = operation [ propertyToChange ] ; return { ... operation , [ propertyToChange ] : applyIdToProperty ( ops ) , } ; } return operation ; } ;