| 12 | * 文本加载器实现 |
| 13 | */ |
| 14 | export class TextLoader implements IAssetLoader<ITextAsset> { |
| 15 | readonly supportedType = AssetType.Text; |
| 16 | readonly supportedExtensions = ['.txt', '.text', '.md', '.csv', '.xml', '.html', '.css', '.js', '.ts']; |
| 17 | readonly contentType: AssetContentType = 'text'; |
| 18 | |
| 19 | /** |
| 20 | * Parse text from content. |
| 21 | * 从内容解析文本。 |
| 22 | */ |
| 23 | async parse(content: IAssetContent, _context: IAssetParseContext): Promise<ITextAsset> { |
| 24 | if (!content.text) { |
| 25 | throw new Error('Text content is empty'); |
| 26 | } |
| 27 | |
| 28 | return { |
| 29 | content: content.text, |
| 30 | encoding: this.detectEncoding(content.text) |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Detect text encoding |
| 36 | * 检测文本编码 |
| 37 | */ |
| 38 | private detectEncoding(content: string): 'utf8' | 'utf16' | 'ascii' { |
| 39 | for (let i = 0; i < content.length; i++) { |
| 40 | const charCode = content.charCodeAt(i); |
| 41 | if (charCode > 127) { |
| 42 | return charCode > 255 ? 'utf16' : 'utf8'; |
| 43 | } |
| 44 | } |
| 45 | return 'ascii'; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Dispose loaded asset |
| 50 | * 释放已加载的资产 |
| 51 | */ |
| 52 | dispose(asset: ITextAsset): void { |
| 53 | // 清空文本内容 | Clear text content |
| 54 | asset.content = ''; |
| 55 | } |
| 56 | } |
nothing calls this directly
no outgoing calls
no test coverage detected