@@ -40,6 +40,9 @@ const mapExtensionToLanguageId_local = (extension: string | null): string => {
4040 case 'fmx' :
4141 case 'ini' :
4242 return 'ini' ;
43+ case 'application/pdf' :
44+ case 'pdf' :
45+ return 'pdf' ;
4346 default : return 'plaintext' ;
4447 }
4548} ;
@@ -208,7 +211,7 @@ export const databaseService = {
208211 }
209212
210213 // Insert Documents and Versions
211- const docStmt = db . prepare ( 'INSERT INTO documents (node_id, doc_type, language_hint) VALUES (?, ?, ?)' ) ;
214+ const docStmt = db . prepare ( 'INSERT INTO documents (node_id, doc_type, language_hint, default_view_mode ) VALUES (?, ?, ?, ?)' ) ;
212215 const versionStmt = db . prepare ( 'INSERT INTO doc_versions (document_id, created_at, content_id) VALUES (?, ?, ?)' ) ;
213216 const updateDocStmt = db . prepare ( 'UPDATE documents SET current_version_id = ? WHERE document_id = ?' ) ;
214217
@@ -219,7 +222,7 @@ export const databaseService = {
219222 }
220223
221224 for ( const doc of data . documents ) {
222- const docResult = docStmt . run ( doc . node_id , doc . doc_type , doc . language_hint ) ;
225+ const docResult = docStmt . run ( doc . node_id , doc . doc_type , doc . language_hint , doc . default_view_mode ?? null ) ;
223226 const docId = Number ( docResult . lastInsertRowid ) ;
224227
225228 const versions = docVersionsByNode . get ( doc . node_id ) || [ ] ;
@@ -296,9 +299,9 @@ export const databaseService = {
296299 const originalDoc = db . prepare ( 'SELECT * FROM documents WHERE node_id = ?' ) . get ( nodeId ) as Document ;
297300 if ( originalDoc ) {
298301 const newDocResult = db . prepare ( `
299- INSERT INTO documents (node_id, doc_type, language_hint, current_version_id)
300- VALUES (?, ?, ?, NULL)
301- ` ) . run ( newNodeId , originalDoc . doc_type , originalDoc . language_hint ) ;
302+ INSERT INTO documents (node_id, doc_type, language_hint, default_view_mode, current_version_id)
303+ VALUES (?, ?, ?, ?, NULL)
304+ ` ) . run ( newNodeId , originalDoc . doc_type , originalDoc . language_hint , originalDoc . default_view_mode ) ;
302305 const newDocId = newDocResult . lastInsertRowid ;
303306
304307 // Fix: Cast the result to DocVersion array.
@@ -456,11 +459,20 @@ export const databaseService = {
456459 const maxSortOrderResult = db . prepare ( `SELECT MAX(sort_order) as max_order FROM nodes WHERE parent_id ${ currentParentId ? '= ?' : 'IS NULL' } ` ) . get ( currentParentId ) as { max_order : number | null } ;
457460 const sortOrder = ( maxSortOrderResult ?. max_order ?? - 1 ) + 1 ;
458461 const extension = file . name . split ( '.' ) . pop ( ) || null ;
459- const languageHint = mapExtensionToLanguageId_local ( extension ) ;
462+ let languageHint = mapExtensionToLanguageId_local ( extension ) ;
460463
461464 db . prepare ( `INSERT INTO nodes (node_id, parent_id, node_type, title, sort_order, created_at, updated_at) VALUES (?, ?, 'document', ?, ?, ?, ?)` ) . run ( newNodeId , currentParentId , file . name , sortOrder , now , now ) ;
462465
463- const docResult = db . prepare ( `INSERT INTO documents (node_id, doc_type, language_hint) VALUES (?, ?, ?)` ) . run ( newNodeId , 'source_code' , languageHint ) ;
466+ const trimmedContent = file . content . trim ( ) ;
467+ const isPdf = languageHint === 'pdf' || languageHint === 'application/pdf' || trimmedContent . startsWith ( 'data:application/pdf' ) ;
468+ if ( isPdf ) {
469+ languageHint = 'pdf' ;
470+ }
471+ const docType = isPdf ? 'pdf' : 'source_code' ;
472+ const defaultViewMode = isPdf ? 'preview' : null ;
473+
474+ const docResult = db . prepare ( `INSERT INTO documents (node_id, doc_type, language_hint, default_view_mode) VALUES (?, ?, ?, ?)` )
475+ . run ( newNodeId , docType , languageHint , defaultViewMode ) ;
464476 const documentId = Number ( docResult . lastInsertRowid ) ;
465477
466478 const contentId = getContentId ( file . content ) ;
0 commit comments