import { BoardDoc , CardId , BoardDocCard } from '.' ; import { AddCardArgs } from './Board' ; import { parseDocumentLink } from '../../../ShareLink' ; import ContentTypes from '../../../ContentTypes' ; import { gridCellsToPixels , snapPositionToGrid , snapDimensionToGrid , Dimension , Position , } from './BoardGrid' ; import { boundPosition } from './BoardBoundary' ; import { Selection } from './BoardSelection' ; import uuid = require ( 'uuid' ) ; export const addCardForContent = ( b , { position , dimension , url } ) => { const id = uuid ( ) as CardId ; const { type } = parseDocumentLink ( url ) ; const { component = { } } = ContentTypes . lookup ( { type , context : 'board' } ) as any ; if ( ! dimension ) dimension = { width : gridCellsToPixels ( component . defaultWidth ) , height : gridCellsToPixels ( component . defaultHeight ) } ; const { x , y } = snapPositionToGrid ( position ) ; const { width , height } = snapDimensionToGrid ( dimension ) ; const newCard = { url , x , y } ; if ( width ) { newCard . width = width ; } if ( height ) { newCard . height = height ; } b . cards [ id ] = newCard ; return id ; } ; export const moveCardsBy = ( b , selected , offset ) => { selected . forEach ( ( id ) => { const position = { x : b . cards [ id ] . x + offset . x , y : b . cards [ id ] . y + offset . y } ; const size = { width : b . cards [ id ] . width || 0 , height : b . cards [ id ] . height || 0 } ; const boundedPosition = boundPosition ( position , size ) ; const newPosition = snapPositionToGrid ( boundedPosition ) ; const cardPosition = { x : b . cards [ id ] . x , y : b . cards [ id ] . y } ; if ( newPosition . x === cardPosition . x && newPosition . y === cardPosition . y ) { return ; } const card = b . cards [ id ] ; card . x = newPosition . x ; card . y = newPosition . y ; } ) ; } ; export const cardResized = ( b , id , dimension ) => { const snapDimension = snapDimensionToGrid ( dimension ) ; const cardDimension = { width : b . cards [ id ] . width , height : b . cards [ id ] . height } ; if ( snapDimension . width === cardDimension . width && snapDimension . height === cardDimension . height ) { return ; } const card = b . cards [ id ] ; card . width = snapDimension . width ; card . height = snapDimension . height ; } ; export const deleteCards = ( b , ids ) => { ids . forEach ( ( id ) => delete b . cards [ id ] ) ; } ; export const changeBackgroundColor = ( b , color ) => { b . backgroundColor = color . hex ; } ; interface DeleteCards { type : 'DeleteCards' ; selection : Selection < CardId > ; } interface MoveCardsBy { type : 'MoveCardsBy' ; selection : Selection < CardId > ; distance : Position ; } interface AddCardForContent extends AddCardArgs { type : 'AddCardForContent' ; selectOnly ? : boolean ; } interface ChangeBackgroundColor { type : 'ChangeBackgroundColor' ; color : any ; } export type BoardDocManipulationAction = DeleteCards | MoveCardsBy | AddCardForContent | ChangeBackgroundColor ;