const html2nodes = require ( './Parser.js' ) ; const initData = function ( Component ) { setTimeout ( ( ) => { Component . createSelectorQuery ( ) . select ( '#contain' ) . boundingClientRect ( res => { Component . triggerEvent ( 'ready' , res ) ; } ) . exec ( ) ; Component . videoContext = [ ] ; let nodes = [ Component . selectComponent ( '#contain' ) ] ; nodes = nodes . concat ( Component . selectAllComponents ( '#contain>>>#node' ) ) ; for ( let node of nodes ) { for ( let item of node . data . nodes ) { if ( item . name == 'video' ) { Component . videoContext . push ( { id : item . attrs . id , context : wx . createVideoContext ( item . attrs . id , node ) } ) ; } else if ( item . name == 'audio' && item . attrs . autoplay ) wx . createAudioContext ( item . attrs . id , node ) . play ( ) ; } } } , 10 ) ; } ; Component ( { created ( ) { try { const Document = require ( "./document.js" ) ; this . document = new Document ( ) ; } catch ( e ) { } } , properties : { 'html' : { type : null , value : '' , observer : function ( html ) { let hideAnimation = { } , showAnimation = { } ; if ( this . data . showWithAnimation ) { hideAnimation = wx . createAnimation ( { duration : this . data . animationDuration , timingFunction : "ease" } ) . opacity ( 0 ) . step ( ) . export ( ) ; showAnimation = wx . createAnimation ( { duration : this . data . animationDuration , timingFunction : "ease" } ) . opacity ( 1 ) . step ( ) . export ( ) ; } if ( ! html ) { this . setData ( { nodes : [ ] } ) ; } else if ( typeof html == 'string' ) { html2nodes ( html , this . data . tagStyle ) . then ( res => { this . setData ( { nodes : res . nodes , controls : { imgMode : this . data . imgMode } , showAnimation , hideAnimation } , initData ( this ) ) ; if ( this . document ) this . document . init ( "nodes" , res . nodes , this ) ; if ( res . title && this . data . autosetTitle ) { wx . setNavigationBarTitle ( { title : res . title } ) ; } this . imgList = res . imgList ; this . triggerEvent ( 'parse' , res ) ; } ) . catch ( err => { this . triggerEvent ( 'error' , { source : "parse" , errMsg : err } ) ; } ) ; } else if ( html . constructor == Array ) { this . setData ( { controls : { imgMode : this . data . imgMode } , showAnimation , hideAnimation } , initData ( this ) ) ; if ( this . document ) this . document . init ( "html" , html , this ) ; this . imgList = [ ] ; } else if ( typeof html == 'object' ) { if ( ! html . nodes || html . nodes . constructor != Array ) { if ( ( html . name && html . children && html . attrs ) || ( html . type == "text" ) ) return ; this . triggerEvent ( 'error' , { source : "parse" , errMsg : "传入的nodes数组格式不正确！应该传入的类型是array，实际传入的类型是：" + typeof html . nodes } ) ; return ; } this . setData ( { controls : { imgMode : this . data . imgMode } , showAnimation , hideAnimation } , initData ( this ) ) ; if ( this . document ) this . document . init ( "html.nodes" , html . nodes , this ) ; if ( html . title && this . data . autosetTitle ) wx . setNavigationBarTitle ( { title : html . title } ) ; this . imgList = html . imgList || [ ] ; } else { this . triggerEvent ( 'error' , { source : "parse" , errMsg : "错误的html类型：" + typeof html } ) ; } } } , 'autocopy' : { type : Boolean , value : true } , 'autopause' : { type : Boolean , value : true } , 'autopreview' : { type : Boolean , value : true } , 'autosetTitle' : { type : Boolean , value : true } , 'imgMode' : { type : String , value : "default" } , 'selectable' : { type : Boolean , value : false } , 'tagStyle' : { type : Object , value : { } } , 'showWithAnimation' : { type : Boolean , value : false } , 'animationDuration' : { type : Number , value : 400 } } , methods : { tapEvent ( e ) { let src = e . detail ; if ( src . match ( /^https?:\/\/steamcn.com\/t(\d+)-(\d+)-(\d+)/ ) ) { let tid = src . match ( /^https?:\/\/steamcn.com\/t(\d+)-(\d+)-(\d+)/ ) [ 1 ] ; wx . navigateTo ( { url : `/pages/thread/thread?tid=${ tid }` } ) ; } else if ( src . match ( /^https?:\/\/steamcn.com\/forum.php\?mod=viewthread&tid=(\d+)/ ) ) { let tid = src . match ( /^https?:\/\/steamcn.com\/forum.php\?mod=viewthread&tid=(\d+)/ ) [ 1 ] ; wx . navigateTo ( { url : `/pages/thread/thread?tid=${ tid }` } ) ; } else if ( src . match ( /^https?:\/\/steamcn.com\/forum.php\?mod=redirect&goto=findpost&ptid=(\d+)/ ) ) { let tid = src . match ( /^https?:\/\/steamcn.com\/forum.php\?mod=redirect&goto=findpost&ptid=(\d+)/ ) [ 1 ] ; wx . navigateTo ( { url : `/pages/thread/thread?tid=${ tid }` } ) ; } else if ( this . data . autocopy && e . detail && /^http/ . test ( e . detail ) ) { wx . setClipboardData ( { data : e . detail , success ( ) { wx . showToast ( { title : '链接已复制' , icon : 'success' , duration : 600 } ) ; } } ) ; } this . triggerEvent ( 'linkpress' , e . detail ) ; } , errorEvent ( e ) { this . triggerEvent ( 'error' , e . detail ) ; } , previewEvent ( e ) { if ( this . data . autopreview ) { wx . previewImage ( { current : e . detail , urls : this . imgList . length ? this . imgList : [ e . detail ] , } ) ; } this . triggerEvent ( 'imgtap' , e . detail ) ; } , _playVideo ( e ) { if ( this . videoContext . length > 1 && this . data . autopause ) { for ( let video of this . videoContext ) { if ( video . id == e . detail ) continue ; video . context . pause ( ) ; } } } } } ) ;