@eternalheart/ngx-file-preview

中文文档
1.0.12发布于 2025/05/21MITTypeScriptES Module近 30 天下载 2233

A powerful Angular file preview component library supporting multiple file formats including images, videos, PDFs, Office documents, text files and more.

原始使用文档
文档目录28 节
  1. Preview Examples
  2. Default List View
  3. Custom Template (Example, fully customizable)
  4. Preview Effects for Different File Types
  5. Features
  6. Installation
  7. Internationalization (i18n)
  8. Language Configuration
  9. Register New Language Pack
  10. Using i18n Pipe(For developers)
  11. Configuration
  12. 1. Font Icon Configuration and Resources
  13. 2. Module Imports
  14. Usage
  15. 1. Directive Method
  16. 2. Component Method
  17. File Configuration
  18. PreviewFile Interface
  19. Supported File Types ( PreviewType )
  20. API Reference
  21. PreviewListComponent
  22. PreviewDirective
  23. PreviewEvent Types
  24. Keyboard Shortcuts
  25. Development Guide
  26. Contribution Guidelines
  27. License
  28. Acknowledgments

NGX File Preview 版本 下载量 playground github

中文 · English

NGX File Preview is a powerful Angular file preview component library that supports previewing a wide variety of file formats and offers flexible customization options, providing an efficient and user-friendly solution for developers.

Preview Examples

Default List View

Default List View

Custom Template (Example, fully customizable)

Custom Template

Preview Effects for Different File Types

File TypePreview Effect
ImageImage Preview-dark Image Preview-light
VideoVideo Preview-dark Video Preview-light
AudioAudio Preview-dark Audio Preview-light
PPTPPT Preview-dark PPT Preview-light
WordWord Preview-dark Word Preview-light
ExcelExcel Preview-dark Excel Preview-light
TextText Preview-dark Text Preview-light
MarkdownMarkdown Preview-dark Markdown Preview-light
ZipZip Preview-dark Zip Preview-light
UnknownUnknown Preview-dark Unknown Preview-light

Features

  • Support for Multiple File Formats: Fully compatible with image, PDF, PPT, Word, Excel, text, markdown, audio, video, and many other common file types.
  • Intuitive User Experience: Provides clear indicators for unknown file types and supports user-friendly interactions for different file formats.
  • Dark Mode and Light Mode: Adapts to various use cases with visual preferences for both dark and light modes, including auto mode switching.
  • Flexible Usage: Supports both directive-based and component-based usage, offering flexibility to meet different development requirements.
  • Lightweight Design: Optimized for performance and easy integration into any project, ensuring smooth and efficient operation.
  • Keyboard Shortcut Support: Increases efficiency by allowing easy navigation and closing of the preview with keyboard shortcuts.
  • Internationalization Support: Built-in i18n support with easy language pack registration and switching capabilities.

Installation

npm install @eternalheart/ngx-file-preview --save docx-preview hls.js pptx-preview xlsx ngx-extended-pdf-viewer markdown-it highlight.js

Internationalization (i18n)

Language Configuration

You can specify the language when using the directive:

<div [ngxFilePreview]="file" lang="en">Click to preview</div>

Register New Language Pack

Register a custom language pack using I18nUtils.register:

import { I18nUtils } from '@eternalheart/ngx-file-preview';

// Register a new language pack
I18nUtils.register('fr', {
  preview: {
    error: {
      noFiles: 'Aucun fichier à afficher'
    },
    toolbar: {
      zoomIn: 'Zoom avant',
      zoomOut: 'Zoom arrière'
      // ... other translations
    }
  }
  // ... more translation keys
});

Using i18n Pipe(For developers)

Use the i18n pipe in templates to translate text:

{{ 'preview.toolbar.zoomIn' | i18n }}
// With parameters 
// Use ${0} as a numeric placeholder in the corresponding text. The number of placeholders is unlimited, but you must pass the corresponding number of arguments when using them.
// example: list.total ==> "共${0}个文件"
{{ 'list.total' | i18n:filesCount }}

Configuration

1. Font Icon Configuration and Resources

Add the necessary assets , styles and scripts to your angular.json file:

{
  "projects": {
    "your-app": {
      "architect": {
        "build": {
          "options": {
            "assets": [
              {
                "glob": "**/*",
                "input": "node_modules/ngx-extended-pdf-viewer/assets",
                "output": "assets"
              }
            ],
            "styles": [
              "node_modules/@eternalheart/ngx-file-preview/src/assets/icon/font/nfp.css"
            ],
            "scripts": [
              "node_modules/@eternalheart/ngx-file-preview/src/assets/icon/color/nfp.js"
            ]
          }
        }
      }
    }
  }
}

2. Module Imports

Import the required components in your Angular module:

import {
  PreviewDirective,
  PreviewListComponent,
  PreviewModalComponent
} from '@eternalheart/ngx-file-preview';

@Component({
  imports: [
    PreviewDirective,
    PreviewListComponent,
  ]
})

Usage

1. Directive Method

The simplest usage method: Apply the directive directly to an element:

import {PreviewDirective, PreviewEvent} from '@eternalheart/ngx-file-preview';

@Component({
  imports: [PreviewDirective],
  template: `
    <!-- Use themeMode to specify the display mode (dark/light). By default, it automatically switches based on time, but can also be manually toggled inside the preview page -->
    <div [ngxFilePreview]="file" themeMode="light">Click to preview a single file</div>
    <div [ngxFilePreview]="file" themeMode="dark" (previewEvent)="handlePreviewEvent($event)">Click to preview a single file</div>
    <div [ngxFilePreview]="file" themeMode="auto" [autoConfig]="{dark: {start: 19, end: 7}}">Click to preview a single file</div>
    <div [ngxFilePreview]="files">Click to preview multiple files</div>
  `
})
export class YourComponent {
  file: PreviewFile = {
    name: 'example.jpg',
    type: 'image',
    url: 'path/to/file.jpg'
  };

  files: PreviewFile[] = [
    // ... file array
  ];

  handlePreviewEvent(event: PreviewEvent) {
    const {type, message, event: targetEvent} = event;
    if (type === "error") {
      console.log(message); // Handle error event
    }
    if (type === "select") {
      console.log(targetEvent); // Handle file selection event
    }
  }
}

2. Component Method

Using the Default List Template:

import {PreviewListComponent} from '@eternalheart/ngx-file-preview';

@Component({
  imports: [PreviewListComponent],
  template: `
    <ngx-preview-list [files]="files" (fileSelect)="onFileSelect($event)">
    </ngx-preview-list>
  `
})

Using a Custom Template:

@Component({
  template: `
    <ngx-preview-list [files]="files">
      <ng-template #itemTemplate 
                   let-file 
                   let-index="index"
                   let-isActive="isActive"
                   let-preview="preview">
        <div class="custom-item" 
             [class.active]="isActive"
             (click)="preview()">
          <span>{{ file.name }}</span>
          <span>{{ formatFileSize(file.size) }}</span>
        </div>
      </ng-template>
    </ngx-preview-list>
  `
})

File Configuration

PreviewFile Interface

interface PreviewFile {
  url: string;          // File URL
  name: string;         // File name
  type?: PreviewType;    // File type - Not nessary
  size?: number;        // File size (optional)
  lastModified?: number;// Last modified time (optional)
  coverUrl?: string;    // Cover image URL (for video/audio, optional)
}

Supported File Types (PreviewType)

type PreviewType =
  | 'image'   // Image
  | 'pdf'     // PDF Document
  | 'ppt'     // PPT Presentation
  | 'word'    // Word Document
  | 'txt'     // Text File
  | 'video'   // Video
  | 'excel'   // Excel Spreadsheet
  | 'audio'   // Audio
  | 'zip'     // Compressed File
  | 'unknown' // Unknown Type

API Reference

PreviewListComponent

PropertyTypeDefaultDescription
filesPreviewFile[][]List of files to preview
themeMode'light' | 'dark' | 'auto''auto'Theme mode for the preview
autoConfig{ dark: { start: number, end: number } }{ dark: { start: 19, end: 7 } }Auto theme mode configuration
langstring'zh'Internationalization language setting, 'zh' and 'en' are registered by default
(fileSelect)EventEmitter-Event emitted when a file is selected
(previewEvent)EventEmitter-Event emitted during preview actions

Template Context Variables

VariableTypeDescription
filePreviewFileCurrent file object
indexnumberCurrent file index
typestringFile type
isActivebooleanWhether the current item is selected
select() => voidMethod to select the current file
preview() => voidMethod to preview the current file

PreviewDirective

SelectorPropertyTypeDefaultDescription
[ngxFilePreview]ngxFilePreviewPreviewFile | PreviewFile[]-File(s) to preview
themeMode'light' | 'dark' | 'auto''auto'Theme mode for the preview
autoConfig{ dark: { start: number, end: number } }{ dark: { start: 19, end: 7 } }Auto theme mode configuration
langstring'zh'Internationalization language setting, 'zh' and 'en' are registered by default
(previewEvent)EventEmitter-Event emitted during preview actions

PreviewEvent Types

Event TypeDescriptionEvent Data
errorError event{ type: 'error', message: string }
selectFile selection event{ type: 'select', event: MouseEvent }

Keyboard Shortcuts

In preview mode, the following shortcuts are available:

  • ← Previous file
  • → Next file
  • Esc Close preview

Development Guide

  1. Clone the repository:
git clone https://github.com/wh131462/ngx-file-preview.git
  1. Install dependencies:
npm install
  1. Start the development server:
npm run start
  1. Build the library:
npm run build

Contribution Guidelines

Feel free to submit Issues and Pull Requests to help improve this project!

License

MIT

Acknowledgments

This project uses the following excellent open-source libraries:

We appreciate the contributions from these open-source projects to the community!

版本与发布记录

@eternalheart/ngx-file-preview 共发布 13 个版本,首次发布于 2025/01/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.12latest2025/05/211.5 年前
1.0.112025/04/121.6 年前
1.0.102025/03/251.6 年前
1.0.92025/03/221.6 年前
1.0.82025/03/201.7 年前
1.0.72025/03/191.7 年前

关于 @eternalheart/ngx-file-preview 的常见问题

@eternalheart/ngx-file-preview 是什么?

A powerful Angular file preview component library supporting multiple file formats including images, videos, PDFs, Office documents, text files and more.

如何安装 @eternalheart/ngx-file-preview?

在项目目录运行 npm install @eternalheart/ngx-file-preview,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:docx-preview(^0.3.3)、highlight.js(^11.11.1)、hls.js(^1.4.12)、markdown-it(^14.1.0)、ngx-extended-pdf-viewer(^23.0.0-alpha.6)、pptx-preview(^1.0.1)、xlsx(^0.18.5)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@eternalheart/ngx-file-preview 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@eternalheart/ngx-file-preview 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@eternalheart/ngx-file-preview 可以商用吗?

@eternalheart/ngx-file-preview 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@eternalheart/ngx-file-preview 的下载量有多少?

根据 npm 官方下载统计,@eternalheart/ngx-file-preview 在 2026/10/14 – 2026/11/12 这 30 天内被下载 2,233 次,日均约 74 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@eternalheart/ngx-file-preview 还在维护吗?

最新版本 1.0.12 发布于 2025/05/21(1.5 年前);至今共发布 13 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/01/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。