@foblex/platform

中文文档
1.0.4发布于 2024/09/25MITTypeScriptESM + CJS近 30 天下载 35.4万

An Angular library providing a set of services for supporting server-side rendering (SSR) and other features.

原始使用文档
文档目录12 节
  1. Features
  2. Installation
  3. Usage
  4. PlatformService
  5. BrowserService
  6. Window & LocalStorage Injection Tokens
  7. API
  8. PlatformService
  9. BrowserService
  10. Window Injection Token (F_WINDOW)
  11. LocalStorage Injection Token (F_LOCAL_STORAGE)
  12. License

Angular Platform & Browser Utility Library

This Angular library provides a set of services to assist with platform detection, server-side rendering (SSR) support, and browser utilities. The library offers services to determine browser and operating system details, manage browser-specific objects like window and localStorage, and work with DOM-related tasks such as converting units to pixels.

Features

  • Platform Detection: Detects browser types (e.g., Edge, Firefox, Safari) and operating systems (e.g., Windows, MacOS, iOS, Android).

  • Server-Side Rendering (SSR) Compatibility: Provides mock implementations of window and localStorage for SSR environments.

  • Browser Services: Utilities for interacting with window, document, and converting CSS units to pixels.

Installation

To install the library in your Angular project, run:

npm install @foblex/platform

Usage

PlatformService

The PlatformService allows you to detect the browser, operating system, and other platform-related properties.

Example

import { PlatformService } from '@foblex/platform';

@Component({
  selector: 'app-root',
  template: `<h1>Platform Detection</h1>`
})
export class AppComponent {
  constructor(private platformService: PlatformService) {
    console.log('Is Browser:', this.platformService.isBrowser);
    console.log('Operating System:', this.platformService.getOS());
  }
}

BrowserService

The BrowserService provides easy access to browser-related objects like window, localStorage, and document. It includes a utility method to convert various CSS units to pixel values.

Example

import { BrowserService } from '@foblex/platform';

@Component({
  selector: 'app-root',
  template: `<h1>Browser Utilities</h1>`
})
export class AppComponent {
  constructor(private browserService: BrowserService) {
    console.log('Document Title:', this.browserService.getTitle());
    console.log('Pixels from 50%:', this.browserService.toPixels('50%', 800, 600, '16px'));
  }

  setTitle(title: string): void {
    this.browserService.setTitle(title);
  }
}

Window & LocalStorage Injection Tokens

This library provides injectable tokens (F_WINDOW and F_LOCAL_STORAGE) that abstract away window and localStorage for use in both browser and server-side environments.

Example

import { Component, Inject } from '@angular/core';
import { F_WINDOW, F_LOCAL_STORAGE } from '@foblex/platform';

@Component({
  selector: 'app-root',
  template: `<h1>SSR Safe Window & LocalStorage</h1>`
})
export class AppComponent {
  constructor(
    @Inject(F_WINDOW) private windowService: WindowService,
    @Inject(F_LOCAL_STORAGE) private localStorageService: LocalStorageService
  ) {
    console.log('Window Inner Width:', this.windowService.innerWidth);
    this.localStorageService.setItem('key', 'value');
  }
}

API

PlatformService

  • isBrowser: boolean - Determines if the current platform is a browser.
  • EDGE: boolean - Detects Microsoft Edge browser.
  • TRIDENT: boolean - Detects Internet Explorer (Trident engine).
  • BLINK: boolean - Detects Blink engine (used by Chrome).
  • WEBKIT: boolean - Detects WebKit engine (used by Safari).
  • IOS: boolean - Detects iOS devices.
  • FIREFOX: boolean - Detects Mozilla Firefox.
  • ANDROID: boolean - Detects Android devices.
  • SAFARI: boolean - Detects Safari browser.
  • getOS(): EOperationSystem | undefined - Returns the operating system.

BrowserService

  • window: IWindowService - Provides access to window or a server-safe mock.
  • localStorage: ILocalStorage - Provides access to localStorage or a server-safe mock.
  • document: Document - Provides access to the document object.
  • toPixels(value: string, clientWidth: number, clientHeight: number, fontSize: string): number - Converts CSS units (%, em, rem, vh, vw) to pixels.

Window Injection Token (F_WINDOW)

Injects window object or a server-safe mock of window.

LocalStorage Injection Token (F_LOCAL_STORAGE)

Injects localStorage object or a server-safe mock of localStorage.

License

MIT License

版本与发布记录

@foblex/platform 共发布 5 个版本,首次发布于 2024/09/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.4latest2024/09/252 年前
1.0.32024/09/132 年前
1.0.22024/09/122 年前
1.0.12024/09/112 年前
1.0.02024/09/112 年前

关于 @foblex/platform 的常见问题

@foblex/platform 是什么?

An Angular library providing a set of services for supporting server-side rendering (SSR) and other features.

如何安装 @foblex/platform?

在项目目录运行 npm install @foblex/platform,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@angular/common(>=12.0.0)、@angular/core(>=12.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@foblex/platform 支持 TypeScript 吗?

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

@foblex/platform 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@foblex/platform 可以商用吗?

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

@foblex/platform 的下载量有多少?

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

@foblex/platform 还在维护吗?

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