import { TopicDetail } from './../v2ex' ; import { TreeNode } from '../providers/BaseProvider' ; import { LoginRequiredError , AccountRestrictedError } from './../error' ; import { V2ex } from '../v2ex' ; import * as vscode from 'vscode' ; import G from '../global' ; import * as path from 'path' ; import Config from '../config' ; const yaml = require ( 'js-yaml' ) ; /**
 * 存放话题页面的panels
 * key：话题的链接
 * value：panel
 */ const panels = { } ; param title function _getTitle ( title ) { return title . length <= 15 ? title : title . slice ( 0 , 15 ) + '...' ; } param id param label function _createPanel ( id , label ) { const panel = vscode . window . createWebviewPanel ( id , _getTitle ( label ) , vscode . ViewColumn . One , { enableScripts : true , retainContextWhenHidden : true , enableFindWidget : true , } ) ; panel . iconPath = vscode . Uri . file ( path . join ( G . context ! . extensionPath , 'resources/favicon.png' ) ) ; panels [ id ] = panel ; panel . onDidDispose ( ( ) => { delete panels [ id ] ; } ) ; return panel ; } param item export default function topicItemClick ( item ) { let panel = panels [ item . link ] ; if ( panel ) { panel . reveal ( ) ; return ; } if ( ! Config . openInNewTab ( ) ) { Object . values ( panels ) . forEach ( ( p ) => { p . dispose ( ) ; } ) ; } panel = _createPanel ( item . link , item . label ! ) ; panel . webview . onDidReceiveMessage ( ( message ) => { const topic = message . __topic ; switch ( message . command ) { case 'setTitle' : panel . title = _getTitle ( message . title ) ; break ; case 'browseImage' : _openLargeImage ( message . src ) ; break ; case 'openTopic' : { const item = new TreeNode ( message . link . split ( '.com' ) [ 1 ] , false ) ; item . link = message . link ; topicItemClick ( item ) ; } break ; case 'login' : vscode . commands . executeCommand ( 'v2ex.login' ) ; break ; case 'refresh' : loadTopicInPanel ( panel , item . link ) ; break ; case 'collect' : { vscode . window . withProgress ( { title : '正在收藏' , location : vscode . ProgressLocation . Notification , } , async ( ) => { await V2ex . collectTopic ( topic . id , topic . collectParamT || '' ) ; loadTopicInPanel ( panel , item . link ) ; } ) ; } break ; case 'cancelCollect' : { vscode . window . withProgress ( { title : '正在取消收藏' , location : vscode . ProgressLocation . Notification , } , async ( ) => { await V2ex . cancelCollectTopic ( topic . id , topic . collectParamT || '' ) ; loadTopicInPanel ( panel , item . link ) ; } ) ; } break ; case 'thank' : { vscode . window . withProgress ( { title : '发送感谢' , location : vscode . ProgressLocation . Notification , } , async ( ) => { await V2ex . thankTopic ( topic . id , topic . once ) ; loadTopicInPanel ( panel , item . link ) ; } ) ; } break ; case 'postReply' : { const { content } = message ; vscode . window . withProgress ( { title : '正在提交回复' , location : vscode . ProgressLocation . Notification , } , async ( ) => { await V2ex . postReply ( topic . link , content , topic . once ) ; loadTopicInPanel ( panel , item . link ) ; } ) ; } break ; default : break ; } } ) ; loadTopicInPanel ( panel , item . link ) ; } param panel param topicLink function loadTopicInPanel ( panel , topicLink ) { panel . webview . html = V2ex . renderPage ( 'loading.html' , { contextPath : G . getWebViewContextPath ( panel . webview ) , } ) ; V2ex . getTopicDetail ( topicLink ) . then ( ( detail ) => { try { panel . webview . html = V2ex . renderPage ( 'topic.html' , { topic : detail , topicYml : yaml . safeDump ( detail ) , contextPath : G . getWebViewContextPath ( panel . webview ) , } ) ; } catch ( err ) { console . log ( err ) ; } } ) . catch ( ( err ) => { console . error ( err ) ; if ( err instanceof LoginRequiredError ) { panel . webview . html = V2ex . renderPage ( 'error.html' , { contextPath : G . getWebViewContextPath ( panel . webview ) , message : err . message , showLogin : true , showRefresh : true , } ) ; } else if ( err instanceof AccountRestrictedError ) { panel . webview . html = V2ex . renderPage ( 'error.html' , { contextPath : G . getWebViewContextPath ( panel . webview ) , message : err . message , showRefresh : false , } ) ; } else { panel . webview . html = V2ex . renderPage ( 'error.html' , { contextPath : G . getWebViewContextPath ( panel . webview ) , message : err . message , showRefresh : true , } ) ; } } ) ; } param imageSrc function _openLargeImage ( imageSrc ) { let panel = panels [ imageSrc ] ; if ( panel ) { panel . reveal ( ) ; return ; } console . log ( '打开大图：' , imageSrc ) ; panel = _createPanel ( imageSrc , '查看图片' ) ; panel . webview . html = V2ex . renderPage ( 'browseImage.html' , { imageSrc : imageSrc , } ) ; }