Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions js/openseadragon_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,6 @@
});
}

return viewer;
};
})(once);
21 changes: 21 additions & 0 deletions modules/openseadragon_bookmark_url/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# OpenSeadragon Bookmark URL

Enables the OpenSeadragon Bookmark URL plugin that tracks OpenSeadragon zoom level and coordinates to allow sharing of a display via URL.

This module assumes you have added a string field to a media type that can hold the bookmark fragment, e.g.
`file` with `field_bookmark_url`.

## Requirements
- [OpenSeadragon](https://www.drupal.org/project/openseadragon)

## Installation

Install as you would normally install a contributed Drupal module. For further information, see
[Installing Drupal Modules](https://www.drupal.org/docs/extending-drupal/installing-modules).

## Configuration
Currently, no UI exists for configuration.

Create an openseadragon_bookmark_url.settings.yml file and set `bookmark_url_bundle` to the media type, e.g. `file`, and `bookmark_url_field` to the field. The value from the specific media and field are inject into the Drupal settings available to javascript.
* `bookmark_url_bundle`: `file`
* `bookmark_url_field`: `field_bookmark_url`
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Default configuration for the OpenSeadragon Bookmark URL submodule.
# These values are intentionally empty; an administrator must choose the
# appropriate content type and field for their site via the settings form.
bookmark_url_bundle: ''
bookmark_url_field: ''
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Schema for openseadragon_bookmark_url.settings config object.

openseadragon_bookmark_url.settings:
type: config_object
label: 'OpenSeadragon Bookmark URL settings'
mapping:
bookmark_url_bundle:
type: string
label: 'Content type (bundle) machine name'
constraints:
# Must be a valid (media) bundle machine name or empty string.
Regex:
pattern: '/^[a-z0-9_]*$/'
message: 'The bundle must be a valid machine name (lowercase letters, digits, and underscores only).'

bookmark_url_field:
type: string
label: 'Field machine name that stores the bookmark URL'
constraints:
Regex:
pattern: '/^[a-z0-9_]*$/'
message: 'The field name must be a valid machine name (lowercase letters, digits, and underscores only).'
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*jslint browser: true*/
/*global OpenSeadragon, Drupal*/
/**
* @file
* Attaches openseadragon-bookmark-url to OpenSeadragon viewer.
*/
(function($, Drupal, drupalSettings, once) {
'use strict';

/**
* The DOM element that represents the Singleton Instance of this class.
* @type {string}
*/
var base = '#openseadragon-viewer';

/**
* Invoke bookmark url plugin.
*/
Drupal.behaviors.openSeadragonBookmarkURL = {
attach: function(context, settings) {
Object.keys(settings.openseadragon).forEach(function(osdViewerId) {
// Use custom element #id if set.
base = '#' + osdViewerId;
once('openSeadragonViewerBookmarkURL', base, context).forEach(function () {
// Load bookmark url, but without page tracking, to allow hash to be set.
Drupal.openSeadragonViewer[base].bookmarkUrl({trackPage: false});
if (window.location.hash === '') {
var hash = settings.openseadragon[osdViewerId].openseadragonBookmarkUrl;
if (hash !== '') {
window.location.hash = hash;
}
}
// With hash set or not, once OSD loaded, begin page tracking.
Drupal.openSeadragonViewer[base].addOnceHandler('open', function(){
Drupal.openSeadragonViewer[base].bookmarkUrl({trackPage: true});
});
});
});
}
};

})($, Drupal, drupalSettings, once);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure that referencing what I assume is supposed to be jQuery as $ here is safe. On the other hand, it doesn't seem to be used, so it probably shouldn't be here in the first place? drupalSettings doesn't appear to be used directly, either.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, yeah, the attempt to use $ globally should cause warnings about unresolvable symbols, given Drupal loads jQuery with .noConflict() by default.

Somewhat related: If the aliasing of jQuery to $ isn't necessary (because jQuery isn't actually being used), then the function wrapping the behavior definition as a whole isn't necessary.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: 'OpenSeadragon Bookmark URL'
type: module
description: 'Wrapper for https://github.com/openseadragon/bookmark-url library'
package: Media
core_version_requirement: ^10 || ^11
dependencies:
- openseadragon:openseadragon
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
plugin:
remote: https://github.com/openseadragon/bookmark-url
version: "0.0.5"
license:
name: BSD-3-Clause
url: https://github.com/openseadragon/bookmark-url/blob/master/LICENSE
gpl-compatible: true
js:
https://cdn.jsdelivr.net/gh/openseadragon/bookmark-url@13c534c6ad7078fa44675f23185b75ee760d659c/openseadragon-bookmark-url.js:
type: external
minified: false
attributes:
integrity: "sha256-RDZWd2DzG9Asb3Pf3U0UrC0wKEGVJaC3D00Z9mqN63w="
crossorigin: anonymous
openseadragon_bookmark_url:
js:
js/drupal-openseadragon-bookmark-url.js: {}
dependencies:
- core/drupal
- core/once
- core/drupalSettings
- openseadragon/openseadragon
- openseadragon_bookmark_url/plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* @file
* Attach bookmark-url.js.
*/

/**
* Attach openseadragon_bookmark_url js if openseadragon_formatter used.
*
* Implements hook_preprocess_HOOK().
*
* {@inheritDoc}
*/
function openseadragon_bookmark_url_preprocess_field(&$variables) {
if (isset($variables['items'])) {
foreach ($variables['items'] as $item) {
if (isset($item['content']['#theme']) && $item['content']['#theme'] == 'openseadragon_formatter') {
$variables['#attached']['library'][] = 'openseadragon_bookmark_url/openseadragon_bookmark_url';

// Inject bookmark URL into settings for later reference in js.
$config = \Drupal::config('openseadragon_bookmark_url.settings');
$bookmark_url_bundle = $config->get('bookmark_url_bundle');
$bookmark_url_field = $config->get('bookmark_url_field');
if (isset($item['content']['#entity'])) {
$entity = $item['content']['#entity'];
if ($entity->bundle() == $bookmark_url_bundle) {
$openseadragon_viewer_id = 'openseadragon-viewer-' . $entity->id();
if ($entity->hasField($bookmark_url_field)) {
$fragment = $entity->get($bookmark_url_field)->value;
$variables['#attached']['drupalSettings']['openseadragon'][$openseadragon_viewer_id]['openseadragonBookmarkUrl'] = $fragment;
}
}
}
}
}
}
}
Comment on lines +1 to +38

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<?php
/**
* @file
* Attach bookmark-url.js.
*/
/**
* Attach openseadragon-bookmark-url.js to all pages.
*
* @todo It would be good to target this better, e.g. load only when OSD is loaded.
*
* {@inheritDoc}
*/
function openseadragon_bookmark_url_page_attachments(array &$page) {
$page['#attached']['library'][] = 'openseadragon_bookmark_url/openseadragon_bookmark_url';
}

Can we just use libraries-extend on openseadragon/openseadragon in this module's libraries file?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT library-extends is only available for themes, not modules https://www.drupal.org/docs/develop/theming-drupal/adding-assets-css-js-to-a-drupal-theme-via-librariesyml#s-libraries-extend. I tried using openseadragon_bookmark_url_library_info_alter() to declare a dependency of openseadragon on openseadragon_bookmark_url but that causes a circular dependency.

For now, I've used openseadragon_bookmark_url_preprocess_field() to check if a field withopenseadragon_formatter is being used.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah - I guess modules have to use hook_library_info_alter and can't leverage library-extends.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
openseadragon_bookmark_url.config:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is used? If so can we drop this services file and modules/openseadragon_bookmark_url/src/BookmarkUrlConfig.php

class: Drupal\openseadragon_bookmark_url\BookmarkUrlConfig
arguments:
- '@config.factory'
62 changes: 62 additions & 0 deletions modules/openseadragon_bookmark_url/src/BookmarkUrlConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Drupal\openseadragon_bookmark_url;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;

/**
* Typed accessor service for openseadragon_bookmark_url.settings.
*
* Usage (from a service or controller):
* @code
* $config = \Drupal::service('openseadragon_bookmark_url.config');
* $bundle = $config->getBundle();
* $field = $config->getField();
* @endcode
*/
final class BookmarkUrlConfig {

private const CONFIG_NAME = 'openseadragon_bookmark_url.settings';

/**
* The openseadragon bookmark url config.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
private ImmutableConfig $config;

/**
* Constructs a BookmarkUrlConfig instance.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The Drupal config factory service.
*/
public function __construct(ConfigFactoryInterface $configFactory) {
$this->config = $configFactory->get(self::CONFIG_NAME);
}

/**
* Returns the configured node bundle machine name, or an empty string.
*/
public function getBundle(): string {
return $this->config->get('bookmark_url_bundle') ?? '';
}

/**
* Returns the configured field machine name, or an empty string.
*/
public function getField(): string {
return $this->config->get('bookmark_url_field') ?? '';
}

/**
* Returns TRUE when both bundle and field have been set by an administrator.
*/
public function isConfigured(): bool {
return $this->getBundle() !== '' && $this->getField() !== '';
}

}