@foblex/drag-toolkit

中文文档
1.1.1发布于 2025/05/24MITTypeScriptESM + CJS近 30 天下载 1.7万

A TypeScript library providing foundational classes and utilities for implementing drag-and-drop functionality in Angular applications, with support for both mouse and touch events, and designed to work with or without Angular’s NgZone (zoneless applicati

原始使用文档
文档目录7 节
  1. Overview
  2. Installation
  3. Usage
  4. Creating a Custom Drag-and-Drop Component
  5. Subscribing to Events
  6. Error Handling
  7. License

Foblex Drag Toolkit

A TypeScript library providing foundational classes and utilities for implementing drag-and-drop functionality in Angular applications, with support for both mouse and touch events, and designed to work with or without Angular’s NgZone (zoneless applications). Used in Foblex Flow for drag-and-drop operations.

Overview

Foblex Drag Toolkit simplifies the implementation of drag-and-drop interactions in Angular applications by providing:

  • Unified Pointer Events: Abstract classes for handling mouse and touch events uniformly.
  • Drag-and-Drop Base Class: An abstract base class DragAndDropBase encapsulating common logic for drag-and-drop operations.
  • Event Utilities: Helper functions and classes for event handling, including support for passive event listeners.
  • Angular Zone Compatibility: Designed to work with or without Angular’s NgZone, making it suitable for zoneless applications.

By extending the DragAndDropBase class and implementing its abstract methods, developers can create custom drag-and-drop components with consistent behavior across different input types.

Installation

Install the library via npm:

  npm install @foblex/drag-toolkit

Usage

Creating a Custom Drag-and-Drop Component

Extend the DragAndDropBase class and implement the abstract methods:

import { Component, ElementRef } from '@angular/core';
import {
  DragAndDropBase,
  IPointerEvent,
  ICanRunOutsideAngular,
} from '@foblex/drag-toolkit';

@Component({
  selector: 'app-draggable',
  template: `<div #draggableElement>Drag Me!</div>`,
})
export class DraggableComponent extends DragAndDropBase {
  public hostElement: HTMLElement;
  public disabled = false;

  constructor(
    private elementRef: ElementRef,
    ngZone: ICanRunOutsideAngular
  ) {
    super(ngZone);
    this.hostElement = this.elementRef.nativeElement;
  }

  protected prepareDragSequence(event: IPointerEvent): void {
    // Initialize drag operation
  }

  protected finalizeDragSequence(): void {
    // Clean up after drag operation
  }

  protected onSelect(event: Event): void {
    // Handle text selection during drag
    event.preventDefault();
  }

  public onPointerDown(event: IPointerEvent): boolean {
    // Determine if drag should start
    return true;
  }

  public onPointerMove(event: IPointerEvent): void {
    // Handle dragging movement
  }

  public onPointerUp(event: IPointerEvent): void {
    // Handle drag completion
  }
}

Subscribing to Events

Ensure that you call subscribe and unsubscribe methods appropriately, typically in ngOnInit and ngOnDestroy:

import { AfterViewInit, OnDestroy } from '@angular/core';

export class DraggableComponent
  extends DragAndDropBase
  implements AfterViewInit, OnDestroy
{
  // ... (other code)

  ngAfterViewInit(): void {
    this.subscribe(document);
  }

  ngOnDestroy(): void {
    this.unsubscribe();
  }
}

Error Handling

  • Synthetic Events: The library checks for synthetic mouse events triggered after touch events to prevent duplicate actions.
  • Screen Reader Compatibility: Functions like isFakeMousedownFromScreenReader and isFakeTouchstartFromScreenReader are used to ignore events from screen readers, enhancing accessibility.

License

This project is licensed under the MIT License.

By using the Foblex Drag Toolkit, you can simplify the implementation of complex drag-and-drop interactions in your Angular applications, ensuring consistent behavior across different devices and input types.

版本与发布记录

@foblex/drag-toolkit 共发布 2 个版本,首次发布于 2024/09/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.1latest2025/05/241.5 年前
1.1.02024/09/142 年前

关于 @foblex/drag-toolkit 的常见问题

@foblex/drag-toolkit 是什么?

A TypeScript library providing foundational classes and utilities for implementing drag-and-drop functionality in Angular applications, with support for both mouse and touch events, and designed to work with or without Angular’s NgZone (zoneless applicati

如何安装 @foblex/drag-toolkit?

在项目目录运行 npm install @foblex/drag-toolkit,或使用 pnpm add / yarn add / bun add。

@foblex/drag-toolkit 支持 TypeScript 吗?

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

@foblex/drag-toolkit 支持 ESM 还是 CommonJS?

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

@foblex/drag-toolkit 可以商用吗?

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

@foblex/drag-toolkit 的下载量有多少?

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

@foblex/drag-toolkit 还在维护吗?

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