import { BlockType } from './../constants' ; import { IBlock , IBlockData } from '@core/typings' ; import { get , isString } from 'lodash' ; import { ancestorOf } from './ancestorOf' ; import { BlockManager } from './BlockManager' ; export function getPageIdx ( ) { return 'content' ; } export function getChildIdx ( idx , index ) { return `${ idx }.children.[${ index }]` ; } export function getNodeIdxClassName ( idx ) { return `node-idx-${ idx }` ; } export function getNodeTypeClassName ( type ) { return `node-type-${ type }` ; } export function getNodeIdxFromClassName ( classList ) { return Array . from ( classList ) . find ( ( item ) => item . includes ( 'node-idx-' ) ) ?. replace ( 'node-idx-' , '' ) ; } export function getNodeTypeFromClassName ( classList ) { return Array . from ( isString ( classList ) ? classList . split ( ' ' ) : classList ) . find ( ( item ) => item . includes ( 'node-type-' ) ) ?. replace ( 'node-type-' , '' ) as BlockType ; } export const getIndexByIdx = ( idx ) => { return Number ( /\.\[(\d+)\]$/ . exec ( idx ) ?. [ 1 ] ) || 0 ; } ; export const getParentIdx = ( idx ) => { if ( idx === getPageIdx ( ) ) return undefined ; return /(.*)\.children\.\[\d+\]$/ . exec ( idx ) ?. [ 1 ] ; } ; export const getValueByIdx = < T extends IBlockData > ( values , idx ) : T | null => { return get ( values , idx ) ; } ; export const getParentByIdx = < T extends IBlockData = IBlockData > ( values , idx ) : T | null => { return get ( values , getParentIdx ( idx ) || '' ) ; } ; export const getSiblingIdx = ( sourceIndex , num ) => { return sourceIndex . replace ( /\[(\d+)\]$/ , ( _ , index ) => { if ( Number ( index ) + num < 0 ) return '[0]' ; return `[${ Number ( index ) + num }]` ; } ) ; } ; export const getParentByType = < T extends IBlockData > ( context , idx , type ) : T | null => { if ( ! idx ) return null ; let parentIdx = getParentIdx ( idx ) ; while ( parentIdx ) { const parent = get ( context , parentIdx ) as T ; if ( parent && parent . type === type ) return parent ; parentIdx = getParentIdx ( idx ) ; } return null ; } ; export const getSameParent = ( values , idx , dragType ) : { parent : IBlockData ; parentIdx : string ; } | null => { let parentIdx = idx ; const block = BlockManager . getBlockByType ( dragType ) ; if ( ! block ) return null ; while ( parentIdx ) { const parent = get ( values , parentIdx ) as IBlockData ; if ( ancestorOf ( block . type , parent . type ) > 0 ) { return { parent , parentIdx , } ; } parentIdx = getParentIdx ( parentIdx ) ; } return null ; } ; export const getParenRelativeByType = < T extends IBlockData > ( context , idx , type ) : { parentIdx : string ; insertIndex : number ; parent : IBlockData ; } | null => { let prevIdx = '' ; let parentIdx = idx ; while ( parentIdx ) { const parent = get ( context , parentIdx ) as T ; if ( parent && parent . type === type ) { return { insertIndex : prevIdx ? getIndexByIdx ( prevIdx ) : parent . children . length - 1 , parentIdx , parent , } ; } else { prevIdx = parentIdx ; parentIdx = getParentIdx ( parentIdx ) ; } } return null ; } ; export const getValidChildBlocks = ( type ) : IBlock [ ] => { return BlockManager . getBlocks ( ) . filter ( ( item ) => item . validParentType . includes ( type ) ) ; } ;