@@ -2,6 +2,7 @@ import * as vscode from 'vscode';
22import * as fs from 'fs' ;
33import * as path from 'path' ;
44import { localize } from './i18n' ;
5+ import { SUPPORTED_CONVERT_EXTENSIONS } from './constants' ;
56
67export interface ProjectConfig {
78 enabled : boolean ;
@@ -116,7 +117,7 @@ export class ProjectManager {
116117 }
117118
118119 /**
119- * 检查项目中是否有可转换的文档文件
120+ * 检查项目中是否有可转换的文档文件(扫描根目录及一级子目录)
120121 */
121122 hasConvertibleFiles ( rootPath ?: string ) : boolean {
122123 const workspaceFolders = vscode . workspace . workspaceFolders ;
@@ -125,14 +126,31 @@ export class ProjectManager {
125126 }
126127
127128 const targetPath = rootPath || workspaceFolders [ 0 ] . uri . fsPath ;
128- const supportedExtensions = [ '.docx' , '.xlsx' , '.pptx' , '.pdf' ] ;
129-
129+
130130 try {
131- const files = fs . readdirSync ( targetPath ) ;
132- return files . some ( file => {
133- const ext = path . extname ( file ) . toLowerCase ( ) ;
134- return supportedExtensions . includes ( ext ) ;
135- } ) ;
131+ const entries = fs . readdirSync ( targetPath , { withFileTypes : true } ) ;
132+ for ( const entry of entries ) {
133+ if ( entry . isFile ( ) ) {
134+ const ext = path . extname ( entry . name ) . toLowerCase ( ) ;
135+ if ( SUPPORTED_CONVERT_EXTENSIONS . includes ( ext ) ) {
136+ return true ;
137+ }
138+ } else if ( entry . isDirectory ( ) && ! entry . name . startsWith ( '.' ) ) {
139+ // Scan one level of subdirectories, skipping hidden folders
140+ try {
141+ const subEntries = fs . readdirSync ( path . join ( targetPath , entry . name ) ) ;
142+ for ( const subEntry of subEntries ) {
143+ const ext = path . extname ( subEntry ) . toLowerCase ( ) ;
144+ if ( SUPPORTED_CONVERT_EXTENSIONS . includes ( ext ) ) {
145+ return true ;
146+ }
147+ }
148+ } catch {
149+ // Skip directories we can't read
150+ }
151+ }
152+ }
153+ return false ;
136154 } catch ( error ) {
137155 console . error ( 'Error checking for convertible files:' , error ) ;
138156 return false ;
@@ -177,16 +195,8 @@ export class ProjectManager {
177195 ) ;
178196
179197 if ( choice === convertNowLabel ) {
180- // 触发文件夹转换命令
181198 await vscode . commands . executeCommand ( 'documentConverter.convertFolder' , vscode . Uri . file ( rootPath ) ) ;
182199 }
183- } else if ( ! showConvertPrompt ) {
184- // 当 showConvertPrompt 为 false 时,不显示任何消息,由调用方处理
185- // 这避免了重复的消息提示
186- } else {
187- vscode . window . showInformationMessage (
188- localize ( 'project.info.enabledReady' )
189- ) ;
190200 }
191201
192202 return true ;
@@ -297,23 +307,7 @@ export class ProjectManager {
297307
298308 switch ( choice ) {
299309 case enableLabel :
300- // 用户已经在第一个弹窗中表达了启用意图,直接启用并自动转换,无需再次询问
301- const enabled = await this . enableForProject ( undefined , false ) ;
302- if ( enabled ) {
303- const workspaceFolders = vscode . workspace . workspaceFolders ;
304- if ( workspaceFolders && this . hasConvertibleFiles ( workspaceFolders [ 0 ] . uri . fsPath ) ) {
305- // 直接执行转换,不再询问
306- await vscode . commands . executeCommand ( 'documentConverter.convertFolder' , workspaceFolders [ 0 ] . uri ) ;
307- vscode . window . showInformationMessage (
308- localize ( 'project.info.conversionStarted' )
309- ) ;
310- } else {
311- vscode . window . showInformationMessage (
312- localize ( 'project.info.enabledReady' )
313- ) ;
314- }
315- }
316- return enabled ;
310+ return await this . enableForProject ( undefined , true ) ;
317311 case dontAskAgainLabel :
318312 await this . saveWorkspaceProjectConfig (
319313 vscode . workspace . workspaceFolders ! [ 0 ] . uri . fsPath ,
0 commit comments