Zotero Plugin Toolkit

repository·main·Indexed 41 days ago

https://github.com/windingwind/zotero-plugin-toolkit

An npm package providing a collection of helper APIs and type-safe tools to simplify the development of Zotero plugins. It supports Zotero 6, 7, and 8 (version 5.1.0+), offering specialized utilities such as MenuManager, KeyboardManager, DialogHelper, and ReaderTool to handle common tasks like UI construction, lifecycle management, and Zotero API interaction.

Tokens
14.3K
Snippets
45
Records
62
Agent score
51%

What's inside Zotero Plugin Toolkit

  1. Overview of Zotero Plugin Toolkit features

    main

    Zotero Plugin Toolkit provides modern, simple, consistent, and type-safe APIs for developing Zotero plugins. It includes a suite of specialized tools to handle common plugin development tasks:

    • BasicTool: Access global objects, environment-aware logging, and cross-version DOM compatibility.
    • UITool: Build UI elements with automatic cleanup and lifecycle binding.
    • ReaderTool: Interact with Zotero Reader instances (PDF tabs) to read, observe, and control them.
    • ExtraFieldTool: Parse and manage structured data within the Zotero 'Extra' field.
    • MenuManager: Register custom menus and popups with auto-cleanup.
    • KeyboardManager: Add keyboard shortcuts with conflict-free registration.
    • PromptManager: Add prompt panel commands with minimal boilerplate.
    • FieldHookManager: Hook into Zotero field rendering for custom logic.
    • DialogHelper: Create modern dialogs with data binding and layout control.
    • ClipboardHelper: Safely copy text, HTML, or images to the clipboard.
    • ProgressWindowHelper: Display non-blocking progress windows for task management.
    • VirtualizedTableHelper: Create performant tables similar to Zotero's item/collection trees.
    • PatchHelper: Safely patch and restore Zotero built-in methods.
    • GuideHelper: Build interactive step-by-step onboarding guides.
    • LargePrefHelper: Store large structured data safely in Zotero preferences.
  2. Import the Zotero Plugin Toolkit

    main

    You can import the toolkit in two ways depending on your bundle size requirements:

    1. Full Import: Import the ZoteroToolkit main class to access all modules.
    2. Modular Import (Recommended): Import only the specific tools you need (e.g., BasicTool, ClipboardHelper, UITool) to keep your plugin lightweight.
    // Option 1: Import all modules via the main class
    import { ZoteroToolkit } from "zotero-plugin-toolkit";
    const ztoolkit = new ZoteroToolkit();
    
    // Option 2: Import only what you need (Recommended)
    import { BasicTool, ClipboardHelper, UITool } from "zotero-plugin-toolkit";
    const basic = new BasicTool();
    const ui = new UITool();
  3. Install zotero-plugin-toolkit via npm

    main

    Install zotero-plugin-toolkit from npm to access a collection of APIs designed for Zotero plugin development.

    Compatibility Note:

    • Zotero 8 is supported.
    • Ensure you use version 5.1.0 or later of the toolkit for Zotero 8 support.
    npm install zotero-plugin-toolkit
  4. Test the toolkit locally in a plugin

    main

    You can test your local changes by installing the toolkit directly into a Zotero plugin project or a playground.

    Using the Zotero Plugin Template as a playground:

    1. Build the toolkit using npm run build.
    2. In your playground repository folder, run npm install /path/to/this/repo (replacing /path/to/this/repo with the actual local path to the toolkit).

    The playground will use your local build. If you rebuild the toolkit, the playground will automatically use the updated version without needing to run npm install again.

    npm install /path/to/this/repo
  5. Manage UI elements with UITool

    main

    The UITool class provides a high-level API for creating, managing, and automatically cleaning up DOM elements (HTML, XUL, and SVG).

    In bootstrap plugins, elements added to the Zotero UI must be manually removed when the plugin exits to prevent memory leaks or UI clutter. UITool solves this by maintaining an internal elementCache. When you call unregisterAll(), all elements created via createElement (that have enableElementRecord set to true, which is the default) are automatically removed from the DOM.

    Key lifecycle methods:

    • unregisterAll(): Removes all elements tracked by the toolkit instance.
    // Example of automatic cleanup
    const ui = new UITool(options);
    const myDiv = ui.createElement(document, "div", { id: "my-plugin-div" });
    
    // Later, when the plugin is disabling/exiting:
    ui.unregisterAll(); // myDiv is automatically removed from the DOM