Mirador Documentation

repository·main·Indexed 20 days ago

https://github.com/projectmirador/mirador

An open-source, web-based 'multi-up' viewer for complex digital objects supporting zoom-pan-rotate functionality, image comparison, and annotations. Mirador 4.1.0 features a Redux-based architecture for managing state via actions and a store, a plugin system for extensibility, and support for internationalization via react-i18next. It can be embedded as a standalone application or integrated into React environments using the App and AppProviders components.

Tokens
4.5K
Snippets
23
Records
28
Agent score
60%

What's inside mirador

  1. Manage Mirador state with actions and store

    main

    Mirador uses a Redux-like pattern to manage state and perform operations. You interact with the viewer instance via actions to dispatch changes to the store.

    Common tasks include:

    • Adding a window: store.dispatch(actions.addWindow())
    • Focusing a window: store.dispatch(actions.focusWindow('window-id'))
    • Checking current state: store.getState()
    // Add a window
    store.dispatch(actions.addWindow());
    
    // Focus a specific window
    store.dispatch(actions.focusWindow('window-1'));
    
    // Check current state
    const state = store.getState();
  2. Add a new language translation to Mirador

    main

    Mirador uses react-i18next for internationalization. To add a new language, you must create a new locale directory using the ISO language code, translate the strings from the English source, update the i18n configuration, and register the language in the project settings.

    1. Create the translation file

    Create a directory for the target language and copy the English translation file as a template. For example, to add French (fr):

    $ mkdir src/locales/fr && cp src/locales/en/translation.json src/locales/fr/translation.json

    2. Translate the strings

    Open src/locales/[lang]/translation.json and translate the values.

    CRITICAL: Do not modify any strings contained within double curly braces {{}}, as these are interpolation placeholders (e.g., {{label}}).

    3. Update i18n configuration

    Update the src/i18n.js file to include the new language configuration.

    4. Register the language in settings

    To make the language appear in the UI Language selection dropdown and allow implementers to opt-in/out, add the locale and its native name to the availableLanguages object in src/config/settings.js.

  3. Embed Mirador via CDN

    main

    To embed Mirador directly in an HTML document using the UMD build, include the script tag from unpkg.

    Warning: Using @latest may cause breaking changes when Mirador transitions between major versions (e.g., v3 to v4). Pin your version in production.

    Latest version:

    <script src="https://unpkg.com/mirador@latest/dist/mirador.min.js"></script>

    Pinned version (Recommended for production):

    <script src="https://unpkg.com/mirador@^3/dist/mirador.min.js"></script>
    <script src="https://unpkg.com/mirador@latest/dist/mirador.min.js"></script>
  4. Install Mirador

    main

    You can install Mirador using a JavaScript package manager like npm or yarn. For advanced integrations involving plugins, it is recommended to use Vite with the ESM version of the packages.

    If you want to embed Mirador in an HTML page without customization, use the UMD build via a CDN. For production environments, it is recommended to pin the version (e.g., to version 3) to avoid breaking changes when the latest tag updates.

    $ npm install mirador 
    
    # or
    
    $ yarn add mirador
  5. Run Mirador local development environment

    main

    To develop Mirador locally, ensure Node.js is installed. Follow these steps:

    1. Install dependencies: npm install
    2. Start the project: npm start
    3. Access the application at http://127.0.0.1:4444/
    $ npm install
    $ npm start
  6. Customize Drag and Drop behavior via dndManager

    main

    The AppProviders component includes a MaybeDndProvider that manages the drag-and-drop context. You can control this behavior using the dndManager prop:

    1. Default Behavior: If dndManager is undefined, Mirador uses react-dnd-multi-backend with HTML5toTouch options, providing a robust experience across mouse and touch devices.
    2. Custom Manager: If you pass a specific object to dndManager, Mirador will use that object as the value for the DndContext.Provider.
    3. Disable Drag and Drop: If you pass false to dndManager, the drag-and-drop context is completely omitted from the component tree.
    // To disable drag and drop
    <AppProviders dndManager={false} ... />
    
    // To use a custom manager
    <AppProviders dndManager={myCustomDndManager} ... />
  7. Inject custom translations into Mirador

    main

    Mirador uses StoreAwareI18nextProvider to manage translations. You can inject additional translation bundles by passing a translations object to AppProviders.

    The object should follow this structure:

    {
      "language_code": { "translation": { "key": "value" } }
    }

    When the language prop changes, the provider automatically calls i18n.changeLanguage(). When the translations object is updated, it uses i18n.addResourceBundle to merge the new resources into the existing instance.

  8. Configure available languages in settings.js

    main

    To enable a new language in the Mirador UI dropdown and allow for installation-level configuration, add the language code and its native name to the availableLanguages object in src/config/settings.js.

    // src/config/settings.js
    ...
    availableLanguages: {
        de: 'Deutsch',
        en: 'English',
        fr: 'Français',
        // Add your new language here
    },
  9. Configure the image fallback

    main

    When an image fails to load, Mirador automatically displays a fallback placeholder. You can customize this by providing a fallbackImage URL in your configuration object.

    Error messages can be customized by providing a translation for the imageFailedToLoad key. Detailed error information is logged to the console.

    const config = {
      fallbackImage: 'https://example.com/custom-fallback.jpg',
    };
  10. Instantiate Mirador in a web page

    main

    To start using Mirador in your application, use the Mirador.viewer() method. You must provide an id which corresponds to the CSS selector of the element where Mirador should be instantiated.

    The returned instance provides access to actions (for dispatching commands) and store (for managing state).

    var miradorInstance = Mirador.viewer({
      id: 'mirador' // id selector where Mirador should be instantiated
    });
    
    // miradorInstance contains { actions, store }
  11. Build configuration for Mirador UMD bundle

    main

    Mirador is built using Vite to produce a Universal Module Definition (UMD) bundle. The build process targets ./src/index.js as the entry point and generates a minified file named mirador.min.js. The bundle is configured with exports: 'named' to ensure named exports are available in the UMD environment. Sourcemaps are enabled for debugging.

    // The resulting UMD bundle will be available under the global name 'Mirador'
    // and the file will be named 'mirador.min.js'
  12. Run tests and linting

    main

    Mirador uses Vitest for testing and includes linting/size checks.

    • Run all tests: npm test
    • Run tests with Vitest UI: npm test -- --ui
    • Run a specific test file with UI: npx vitest <path-to-file> --ui
    • Run linting: npm run lint
    • Debug tests: npm run test:debug
    # Run tests
    $ npm test
    
    # Run tests with UI
    $ npm test -- --ui
    
    # Run linting
    $ npm run lint
    
    # Debug tests
    $ npm run test:debug