import { TreeNode } from './../providers/BaseProvider' ; import { V2ex , SoV2exSource } from './../v2ex' ; import * as vscode from 'vscode' ; import topicItemClick from './topicItemClick' ; import dayjs = require ( 'dayjs' ) ; /**上次的搜索结果 */ var _lastSearchList = undefined ; returns export default async function search ( ) { if ( _lastSearchList ) { showQuickPick ( _lastSearchList ) ; return ; } showInputBox ( ) ; } async function showInputBox ( ) { let q = await vscode . window . showInputBox ( { placeHolder : '搜索帖子' , prompt : '请输入查询的关键字' } ) ; if ( q === undefined ) { return ; } q = ( q || '' ) . trim ( ) ; if ( ! q . length ) { return ; } const searchList = await V2ex . search ( q , 0 , 50 ) ; console . log ( `<${ q }>搜索到${ searchList . length }条结果` ) ; if ( searchList . length <= 0 ) { await vscode . window . showInformationMessage ( '没有找到相关内容' ) ; return ; } _lastSearchList = searchList ; showQuickPick ( searchList ) ; } async function showQuickPick ( searchList ) { const items = searchList . map ( ( s , i ) => { const dt = dayjs ( s . created ) . format ( 'YYYY-MM-DD' ) ; return { topicId : s . id , title : s . title , label : `${ i + 1 }. ${ s . title }` , description : `@${ s . member } ${ dt }` , detail : s . content } ; } ) ; const select = await vscode . window . showQuickPick ( items , { matchOnDescription : true , matchOnDetail : true , placeHolder : '搜索结果来自 sov2ex.com' } ) ; if ( select === undefined ) { _lastSearchList = undefined ; return ; } const node = new TreeNode ( select . title , false ) ; node . link = `https://www.v2ex.com/t/${ select . topicId }` ; topicItemClick ( node ) ; }