import { ICnChar , StrokeArg } from 'cnchar-types/main' ; import { ICncharTool } from 'cnchar-types/main/tool' ; import { orders , strokeTable } from './dict' ; import initOrderToWord from './orderToWord' ; import { ICnCharOrder , TOrderArg , TStrokeOrderReturn } from 'cnchar-types/plugin/order' ; let _ ; const arg = { letter : 'letter' , shape : 'shape' , count : 'count' , name : 'name' , detail : 'detail' , array : 'array' , order : 'order' , } ; function setOrder ( key , value ? ) { _ . mapJson ( key , value , ( k , v ) => { orders [ k ] = v ; } ) ; } function main ( cnchar ) { if ( cnchar . plugins . indexOf ( 'order' ) !== - 1 ) { return ; } cnchar . plugins . push ( 'order' ) ; const _old = cnchar . _origin . stroke ; _ = cnchar . _ ; cnchar . setOrder = setOrder ; const _new = function ( sentence , ... args ) : number | Array < any > { if ( _ . has ( args , arg . order ) ) { return _order ( sentence , ... args ) ; } return _old ( sentence , ... args ) ; } ; cnchar . stroke = _new ; String . prototype . stroke = function ( ... args ) : number | Array < any > { return _new ( this as string , ... args ) ; } ; cnchar . type . stroke = arg ; cnchar . _ . order = true ; cnchar . _ . orderWithLetters = orderWithLetters ; if ( cnchar . _ . _reinitStrokeOrder ) { cnchar . _ . _reinitStrokeOrder ( ) ; delete cnchar . _ . _reinitStrokeOrder ; } initOrderToWord ( cnchar ) ; } export default function init ( cnchar ? ) { if ( typeof window === 'object' && window . CnChar ) { main ( window . CnChar ) ; } else if ( typeof cnchar !== 'undefined' ) { main ( cnchar ) ; } } function _order ( str , ... args ) { const strs = str . split ( '' ) ; _ . checkArgs ( 'stroke' , args ) ; const res = [ ] ; for ( var i = 0 ; i < strs . length ; i ++ ) { res [ i ] = orders [ strs [ i ] ] ; } return orderWithLetters ( res , strs , args ) ; } function orderWithLetters ( res , strs , args , igList = [ ] ) { igList = igList || [ ] ; if ( _ . has ( args , arg . letter ) ) { return res ; } for ( var i = 0 ; i < res . length ; i ++ ) { if ( igList . indexOf ( i ) === - 1 && typeof res [ i ] === 'string' ) { res [ i ] = getStrokeSingle ( strs [ i ] , res [ i ] as string , args ) ; } } return res ; } function getStrokeSingle ( str , order , args ) { if ( typeof order === 'undefined' ) { return str ; } var isDetail = _ . has ( args , arg . detail ) ; let name = arg . letter ; if ( ! isDetail ) { if ( _ . has ( args , arg . shape ) ) { name = arg . shape ; } else if ( _ . has ( args , arg . name ) ) { name = arg . name ; } else if ( _ . has ( args , arg . count ) ) { name = arg . count ; } } else { name = arg . detail ; } if ( name === arg . count ) { return order . length ; } if ( name === arg . letter ) { return order ; } var arr = [ ] ; for ( var i = 0 ; i < order . length ; i ++ ) { if ( isDetail ) { arr [ i ] = strokeTable [ order [ i ] ] ; } else { arr [ i ] = strokeTable [ order [ i ] ] [ name ] ; } } return arr ; } init ( ) ;