|
1 | 1 | import { takeEvery, select } from 'redux-saga/effects'; |
2 | 2 | import ActionTypes from 'mirador/dist/es/src/state/actions/action-types'; |
3 | | -import {getCanvasIndex} from "mirador/dist/es/src/state/selectors"; |
| 3 | +import { getCanvasIndex } from 'mirador/dist/es/src/state/selectors'; |
4 | 4 |
|
| 5 | +/** |
| 6 | + * Extract id from the url. |
| 7 | + * URLs are in the format of "https://domain-name/server/iiif/392363fe-015f-41e6-8cdd-b5c754605787/canvas/03c322b7-0182-44fa-8dc3-5d2efcece237" |
| 8 | + */ |
| 9 | +const extractCanvasId = (canvasUrl) => canvasUrl.split('/').pop(); |
5 | 10 |
|
6 | 11 | /** This will be called every time the SET_CANVAS action is dispatched */ |
7 | 12 | const onCanvasChange = function* (action) { |
8 | 13 | const { windowId, canvasId } = action; |
9 | 14 | if (windowId && canvasId) { |
10 | 15 | const canvasIndex = yield select(getCanvasIndex, { windowId }); |
11 | | - // IDs are in the format of "https://dspaceglam7dev.4science.cloud/server/iiif/392363fe-015f-41e6-8cdd-b5c754605787/canvas/03c322b7-0182-44fa-8dc3-5d2efcece237" |
12 | | - const id = action.canvasId.split('/').pop(); |
| 16 | + const canvasId = extractCanvasId(action.canvasId); |
| 17 | + |
13 | 18 | const message = { |
14 | 19 | type: 'update-url', |
15 | 20 | // index here starts from 0, whilst for setting the index it starts from 1. |
16 | 21 | canvasIndex: canvasIndex + 1, |
17 | | - canvasId: id |
| 22 | + canvasId: canvasId |
18 | 23 | }; |
19 | | - if (id && id !== 'undefined' && typeof window !== 'undefined') { |
| 24 | + |
| 25 | + const isValidId = canvasId && canvasId !== 'undefined'; |
| 26 | + const isBrowserEnvironment = typeof window !== 'undefined'; |
| 27 | + |
| 28 | + if (isValidId && isBrowserEnvironment) { |
20 | 29 | window.parent.postMessage(message, '*'); |
21 | 30 | } |
22 | 31 | } |
|
0 commit comments