import { TypeElement , TypeElemDesc , TypeElementBase , } from '@idraw/types' ; import util from '@idraw/util' ; import { _board , _data , _opts , _config , _renderer , _element , _engine , _tempData , _draw , _coreEvent , _emitChangeScreen , _emitChangeData , } from './../names' ; import { diffElementResourceChange } from './../lib/diff' ; import Core from './../index' ; import { Mode } from './../constant/static' ; const { deepClone } = util . data ; const { createUUID } = util . uuid ; export function getSelectedElements ( core ) { const elems = [ ] ; let list = [ ] ; const uuid = core [ _engine ] . temp . get ( 'selectedUUID' ) ; if ( typeof uuid === 'string' && uuid ) { list . push ( uuid ) ; } else { list = core [ _engine ] . temp . get ( 'selectedUUIDList' ) ; } list . forEach ( ( uuid ) => { const index = core [ _engine ] . helper . getElementIndexByUUID ( uuid ) ; if ( index !== null && index >= 0 ) { const elem = core [ _data ] ?. elements [ index ] ; if ( elem ) elems . push ( elem ) ; } } ) ; return deepClone ( elems ) ; } export function getElement ( core , uuid ) { let elem = null ; const index = core [ _engine ] . helper . getElementIndexByUUID ( uuid ) ; if ( index !== null && core [ _data ] . elements [ index ] ) { elem = deepClone ( core [ _data ] . elements [ index ] ) ; } return elem ; } export function getElementByIndex ( core , index ) { let elem = null ; if ( index >= 0 && core [ _data ] . elements [ index ] ) { elem = deepClone ( core [ _data ] . elements [ index ] ) ; } return elem ; } export function updateElement ( core , elem ) { const _elem = deepClone ( elem ) as TypeElement < keyof TypeElemDesc > ; const data = core [ _data ] ; const resourceChangeUUIDs = [ ] ; for ( let i = 0 ; i < data . elements . length ; i ++ ) { if ( _elem . uuid === data . elements [ i ] ?. uuid ) { const result = diffElementResourceChange ( data . elements [ i ] , _elem ) ; if ( typeof result === 'string' ) { resourceChangeUUIDs . push ( result ) ; } data . elements [ i ] = _elem ; break ; } } core [ _emitChangeData ] ( ) ; core [ _draw ] ( { resourceChangeUUIDs } ) ; } export function selectElementByIndex ( core , index , opts ? ) { if ( core [ _data ] . elements [ index ] ) { const uuid = core [ _data ] . elements [ index ] . uuid ; if ( opts ?. useMode === true ) { core [ _engine ] . temp . set ( 'mode' , Mode . SELECT_ELEMENT ) ; } else { core [ _engine ] . temp . set ( 'mode' , Mode . NULL ) ; } if ( typeof uuid === 'string' ) { core [ _engine ] . temp . set ( 'selectedUUID' , uuid ) ; core [ _engine ] . temp . set ( 'selectedUUIDList' , [ ] ) ; } core [ _draw ] ( ) ; } } export function selectElement ( core , uuid , opts ? ) { const index = core [ _engine ] . helper . getElementIndexByUUID ( uuid ) ; if ( typeof index === 'number' && index >= 0 ) { core . selectElementByIndex ( index , opts ) ; } } export function moveUpElement ( core , uuid ) { const index = core [ _engine ] . helper . getElementIndexByUUID ( uuid ) ; if ( typeof index === 'number' && index >= 0 && index < core [ _data ] . elements . length - 1 ) { const temp = core [ _data ] . elements [ index ] ; core [ _data ] . elements [ index ] = core [ _data ] . elements [ index + 1 ] ; core [ _data ] . elements [ index + 1 ] = temp ; } core [ _emitChangeData ] ( ) ; core [ _draw ] ( ) ; } export function moveDownElement ( core , uuid ) { const index = core [ _engine ] . helper . getElementIndexByUUID ( uuid ) ; if ( typeof index === 'number' && index > 0 && index < core [ _data ] . elements . length ) { const temp = core [ _data ] . elements [ index ] ; core [ _data ] . elements [ index ] = core [ _data ] . elements [ index - 1 ] ; core [ _data ] . elements [ index - 1 ] = temp ; } core [ _emitChangeData ] ( ) ; core [ _draw ] ( ) ; } export function addElement ( core , elem ) { const _elem = deepClone ( elem ) ; _elem . uuid = createUUID ( ) ; core [ _data ] . elements . push ( _elem ) ; core [ _emitChangeData ] ( ) ; core [ _draw ] ( ) ; return _elem . uuid ; } export function deleteElement ( core , uuid ) { const index = core [ _element ] . getElementIndex ( core [ _data ] , uuid ) ; if ( index >= 0 ) { core [ _data ] . elements . splice ( index , 1 ) ; core [ _emitChangeData ] ( ) ; core [ _draw ] ( ) ; } } export function insertElementBefore ( core , elem , beforeUUID ) { const index = core [ _engine ] . helper . getElementIndexByUUID ( beforeUUID ) ; if ( index !== null ) { return core . insertElementBeforeIndex ( elem , index ) ; } return null ; } export function insertElementBeforeIndex ( core , elem , index ) { const _elem = deepClone ( elem ) ; _elem . uuid = createUUID ( ) ; if ( index >= 0 ) { core [ _data ] . elements . splice ( index , 0 , _elem ) ; core [ _emitChangeData ] ( ) ; core [ _draw ] ( ) ; return _elem . uuid ; } return null ; } export function insertElementAfter ( core , elem , beforeUUID ) { const index = core [ _engine ] . helper . getElementIndexByUUID ( beforeUUID ) ; if ( index !== null ) { return core . insertElementAfterIndex ( elem , index ) ; } return null ; } export function insertElementAfterIndex ( core , elem , index ) { const _elem = deepClone ( elem ) ; _elem . uuid = createUUID ( ) ; if ( index >= 0 ) { core [ _data ] . elements . splice ( index + 1 , 0 , _elem ) ; core [ _emitChangeData ] ( ) ; core [ _draw ] ( ) ; return _elem . uuid ; } return null ; }