|
|
import { useRtl } from 'i18n-calypso'; |
|
|
import { get } from 'lodash'; |
|
|
import { cloneElement, Children } from 'react'; |
|
|
import { stripHTML } from 'calypso/lib/formatting'; |
|
|
import { isRTLCharacter, isLTRCharacter } from './direction'; |
|
|
|
|
|
const MAX_LENGTH_OF_TEXT_TO_EXAMINE = 100; |
|
|
|
|
|
const SPACE_CHARACTERS = { |
|
|
'\n': true, |
|
|
'\r': true, |
|
|
'\t': true, |
|
|
' ': true, |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const isSpaceCharacter = ( character ) => !! SPACE_CHARACTERS[ character ]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getTaglessIndex = ( text ) => { |
|
|
let isTagOpen = false; |
|
|
|
|
|
for ( let i = 0; i < text.length; i++ ) { |
|
|
|
|
|
if ( isSpaceCharacter( text[ i ] ) ) { |
|
|
continue; |
|
|
} |
|
|
|
|
|
if ( text[ i ] === '<' ) { |
|
|
isTagOpen = true; |
|
|
} else if ( isTagOpen && text[ i ] === '>' ) { |
|
|
isTagOpen = false; |
|
|
} else if ( ! isTagOpen ) { |
|
|
return i; |
|
|
} |
|
|
} |
|
|
|
|
|
return 0; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getContent = ( reactElement ) => { |
|
|
if ( ! reactElement ) { |
|
|
return null; |
|
|
} |
|
|
const { props } = reactElement; |
|
|
|
|
|
if ( ! props ) { |
|
|
return null; |
|
|
} |
|
|
|
|
|
|
|
|
if ( typeof props.children === 'string' ) { |
|
|
return props.children; |
|
|
} |
|
|
|
|
|
|
|
|
if ( typeof props.dangerouslySetInnerHTML === 'object' ) { |
|
|
|
|
|
|
|
|
|
|
|
const html = props.dangerouslySetInnerHTML.__html; |
|
|
if ( ! html ) { |
|
|
return ''; |
|
|
} |
|
|
|
|
|
const taglessIndex = getTaglessIndex( html ); |
|
|
const startIndex = |
|
|
taglessIndex + MAX_LENGTH_OF_TEXT_TO_EXAMINE < html.length ? taglessIndex : 0; |
|
|
|
|
|
return stripHTML( html.substring( startIndex, startIndex + MAX_LENGTH_OF_TEXT_TO_EXAMINE ) ); |
|
|
} |
|
|
|
|
|
|
|
|
if ( typeof props.value === 'string' ) { |
|
|
return props.value; |
|
|
} |
|
|
|
|
|
|
|
|
return null; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getTextMainDirection = ( text, isRtl ) => { |
|
|
let rtlCount = 0; |
|
|
let ltrCount = 0; |
|
|
|
|
|
const examinedLength = Math.min( MAX_LENGTH_OF_TEXT_TO_EXAMINE, text.length ); |
|
|
for ( let i = 0; i < examinedLength; i++ ) { |
|
|
if ( isRTLCharacter( text[ i ] ) ) { |
|
|
rtlCount++; |
|
|
} else if ( isLTRCharacter( text[ i ] ) ) { |
|
|
ltrCount++; |
|
|
} |
|
|
} |
|
|
|
|
|
if ( rtlCount + ltrCount === 0 ) { |
|
|
return isRtl ? 'rtl' : 'ltr'; |
|
|
} |
|
|
|
|
|
return rtlCount > ltrCount ? 'rtl' : 'ltr'; |
|
|
}; |
|
|
|
|
|
const getDirectionProps = ( child, direction ) => ( { |
|
|
direction: direction, |
|
|
style: Object.assign( {}, get( child, 'props.style', {} ), { |
|
|
direction: direction, |
|
|
textAlign: direction === 'rtl' ? 'right' : 'left', |
|
|
} ), |
|
|
} ); |
|
|
|
|
|
const getChildDirection = ( child, isRtl ) => { |
|
|
const childContent = getContent( child ); |
|
|
|
|
|
if ( childContent ) { |
|
|
const textMainDirection = getTextMainDirection( childContent, isRtl ); |
|
|
const userDirection = isRtl ? 'rtl' : 'ltr'; |
|
|
|
|
|
if ( textMainDirection !== userDirection ) { |
|
|
return textMainDirection; |
|
|
} |
|
|
} |
|
|
|
|
|
return null; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const setChildDirection = ( child, isRtl ) => { |
|
|
const childDirection = getChildDirection( child, isRtl ); |
|
|
|
|
|
if ( childDirection ) { |
|
|
return cloneElement( child, getDirectionProps( child, childDirection ) ); |
|
|
} |
|
|
|
|
|
if ( child?.props?.children ) { |
|
|
const children = Children.map( child.props.children, ( innerChild ) => { |
|
|
if ( ! innerChild ) { |
|
|
return innerChild; |
|
|
} |
|
|
|
|
|
if ( typeof innerChild === 'string' ) { |
|
|
return innerChild; |
|
|
} |
|
|
|
|
|
return setChildDirection( innerChild, isRtl ); |
|
|
} ); |
|
|
|
|
|
return cloneElement( child, null, children ); |
|
|
} |
|
|
|
|
|
return child; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default function AutoDirection( { children } ) { |
|
|
const isRtl = useRtl(); |
|
|
return setChildDirection( children, isRtl ); |
|
|
} |
|
|
|