import { Result } from './Store.d' ; import { EVT } from './EVT' ; import { form } from './Settings' ; import { store } from './Store' ; import { lang } from './Lang' ; import { DateFormat } from './DateFormat' ; class FileName { constructor ( ) { window . addEventListener ( EVT . events . previewFileName , ( ) => { this . previewFileName ( ) ; } ) ; } private unsafeStr = new RegExp ( /[\u0001-\u001f\u007f-\u009f\u00ad\u0600-\u0605\u061c\u06dd\u070f\u08e2\u180e\u200b-\u200f\u202a-\u202e\u2060-\u2064\u2066-\u206f\ufdd0-\ufdef\ufeff\ufff9-\ufffb\ufffe\uffff]/g ) ; private fullWidthDict : string [ ] [ ] = [ [ '\\\\' , '＼' ] , [ '/' , '／' ] , [ ':' , '：' ] , [ '\\?' , '？' ] , [ '"' , '＂' ] , [ '<' , '＜' ] , [ '>' , '＞' ] , [ '\\*' , '＊' ] , [ '\\|' , '｜' ] , [ '~' , '～' ] , ] ; private replaceUnsafeStr ( str ) { str = str . replace ( this . unsafeStr , '' ) ; for ( let index = 0 ; index < this . fullWidthDict . length ; index ++ ) { const rule = this . fullWidthDict [ index ] ; const reg = new RegExp ( rule [ 0 ] , 'g' ) ; str = str . replace ( reg , rule [ 1 ] ) ; } return str ; } private transDate ( date ) { const date0 = new Date ( date ) ; const y = date0 . getFullYear ( ) ; const M = ( date0 . getMonth ( ) + 1 ) . toString ( ) . padStart ( 2 , '0' ) ; const d = date0 . getDate ( ) . toString ( ) . padStart ( 2 , '0' ) ; const h = date0 . getHours ( ) . toString ( ) . padStart ( 2 , '0' ) ; const m = date0 . getMinutes ( ) . toString ( ) . padStart ( 2 , '0' ) ; return `${ y }-${ M }-${ d } ${ h }-${ m }` ; } public getFileName ( data ) { let result = form . userSetName . value ; result = result || store . defaultFileName ; const cfg = { '{postid}' : { value : data . postId , safe : true , } , '{title}' : { value : data . title , safe : false , } , '{name}' : { value : data . name , safe : false , } , '{ext}' : { value : data . ext , safe : false , } , '{index}' : { value : data . index , safe : false , } , '{tags}' : { value : data . tags , safe : false , } , '{date}' : { value : DateFormat . format ( data . date , form . dateFormat . value ) , safe : false , } , '{task_date}' : { value : DateFormat . format ( store . crawlCompleteTime , form . dateFormat . value ) , prefix : '' , safe : false , } , '{fee}' : { value : data . fee , safe : true , } , '{user}' : { value : data . user , safe : false , } , '{uid}' : { value : data . uid , safe : true , } , } ; result = this . replaceUnsafeStr ( result ) ; result = result . replace ( /／/g , '/' ) ; for ( const [ key , val ] of Object . entries ( cfg ) ) { if ( result . includes ( key ) && val . value !== '' && val . value !== null ) { let once = String ( val . value ) ; if ( ! val . safe ) { once = this . replaceUnsafeStr ( once ) ; } result = result . replace ( new RegExp ( key , 'g' ) , once ) ; } } result = result . replace ( /undefined/g , '' ) . replace ( /\/{2,9}/ , '/' ) ; let tempArr = result . split ( '/' ) ; tempArr . forEach ( ( str , index , arr ) => { arr [ index ] = str . trim ( ) . replace ( /^\./g , '．' ) . replace ( /\.$/g , '．' ) ; } ) ; result = tempArr . join ( '/' ) ; if ( result . startsWith ( '/' ) ) { result = result . replace ( '/' , '' ) ; } if ( result . endsWith ( '/' ) ) { result = result . substr ( 0 , result . length - 1 ) ; } result += '.' + data . ext ; return result ; } previewFileName ( ) { if ( store . result . length === 0 ) { return alert ( lang . transl ( '_没有数据可供使用' ) ) ; } const resultArr = [ ] ; const length = store . result . length ; for ( let i = 0 ; i < length ; i ++ ) { const data = store . result [ i ] ; const fullName = this . getFileName ( data ) ; const part = fullName . split ( '/' ) ; const length = part . length ; for ( let i = 0 ; i < length ; i ++ ) { const str = part [ i ] ; if ( i < length - 1 ) { part [ i ] = `<span class="color666">${ str }</span>` ; } else { part [ i ] = `<span class="color000">${ str }</span>` ; } } const fullNameHtml = part . join ( '/' ) ; const nowResult = `<p class="result">${ fullNameHtml }</p>` ; resultArr . push ( nowResult ) ; } const result = resultArr . join ( '' ) ; EVT . fire ( EVT . events . output , result ) ; } } const fileName = new FileName ( ) ; export { fileName } ;