Skip to content
Snippets Groups Projects
Commit c524dcd9 authored by Jack Reed's avatar Jack Reed
Browse files

Add getSelectedTargetsAnnotations that selects annotations by multiple targets

parent 3873843a
No related branches found
No related tags found
No related merge requests found
......@@ -19,7 +19,8 @@ import {
getManifestProvider,
getManifestTitle,
getManifestThumbnail,
getSelectedCanvasAnnotations,
getSelectedTargetAnnotations,
getSelectedTargetsAnnotations,
getWindowViewType,
getIdAndLabelOfCanvases,
getCompanionWindowsOfWindow,
......@@ -325,7 +326,7 @@ describe('getDestructuredMetadata', () => {
});
});
describe('getSelectedCanvasAnnotations', () => {
describe('getSelectedTargetAnnotations', () => {
it('returns annotations for the given canvasId that have resources', () => {
const state = {
annotations: {
......@@ -337,15 +338,42 @@ describe('getSelectedCanvasAnnotations', () => {
},
};
expect(getSelectedCanvasAnnotations(state, 'abc123').length).toEqual(1);
expect(getSelectedTargetAnnotations(state, 'abc123').length).toEqual(1);
});
it('returns an empty array if there are no annotations', () => {
const state = { annotations: { xyz321: {} } };
const expected = [];
expect(getSelectedCanvasAnnotations({}, 'abc123')).toEqual(expected);
expect(getSelectedCanvasAnnotations(state, 'abc123')).toEqual(expected);
expect(getSelectedTargetAnnotations({}, 'abc123')).toEqual(expected);
expect(getSelectedTargetAnnotations(state, 'abc123')).toEqual(expected);
});
});
describe('getSelectedTargetsAnnotations', () => {
it('returns annotations for multiple canvasIds', () => {
const state = {
annotations: {
abc123: {
annoId1: { '@id': 'annoId1', json: { resources: ['aResource'] } },
annoId2: { '@id': 'annoId2' },
annoId3: { '@id': 'annoId3', json: { resources: [] } },
},
def456: {
annoId4: { '@id': 'annoId4', json: { resources: ['helloWorld'] } },
},
},
};
expect(getSelectedTargetsAnnotations(state, ['abc123', 'def456']).length).toEqual(2);
});
it('returns an empty array if there are no annotations', () => {
const state = { annotations: { xyz321: {} } };
const expected = [];
expect(getSelectedTargetsAnnotations({}, ['abc123'])).toEqual(expected);
expect(getSelectedTargetsAnnotations(state, ['abc123'])).toEqual(expected);
});
});
......
......@@ -8,7 +8,7 @@ import * as actions from '../state/actions';
import {
getCanvasLabel,
getSelectedCanvas,
getSelectedCanvasAnnotations,
getSelectedTargetsAnnotations,
} from '../state/selectors';
/**
......@@ -24,7 +24,7 @@ const mapStateToProps = ({
getSelectedCanvas({ windows, manifests }, windowId),
windows[windowId].canvasIndex,
),
annotations: getSelectedCanvasAnnotations(
annotations: getSelectedTargetsAnnotations(
{ annotations },
currentCanvases.map(canvas => canvas.id),
),
......
......@@ -4,7 +4,7 @@ import { withTranslation } from 'react-i18next';
import {
getIdAndContentOfResources,
getSelectedCanvas,
getSelectedCanvasAnnotations,
getSelectedTargetAnnotations,
getAnnotationResourcesByMotivation,
} from '../state/selectors';
import { WindowSideBarAnnotationsPanel } from '../components/WindowSideBarAnnotationsPanel';
......@@ -17,7 +17,7 @@ import { WindowSideBarAnnotationsPanel } from '../components/WindowSideBarAnnota
const mapStateToProps = (state, { windowId }) => ({
annotations: getIdAndContentOfResources(
getAnnotationResourcesByMotivation(
getSelectedCanvasAnnotations(state, getSelectedCanvas(state, windowId).id),
getSelectedTargetAnnotations(state, getSelectedCanvas(state, windowId).id),
['oa:commenting', 'sc:painting'],
),
),
......
......@@ -7,7 +7,7 @@ import * as actions from '../state/actions';
import {
getCompanionWindowForPosition,
getSelectedCanvas,
getSelectedCanvasAnnotations,
getSelectedTargetAnnotations,
getAnnotationResourcesByMotivation,
} from '../state/selectors';
import { WindowSideBarButtons } from '../components/WindowSideBarButtons';
......@@ -32,7 +32,7 @@ const mapDispatchToProps = (dispatch, { windowId }) => ({
*/
const mapStateToProps = (state, { windowId }) => ({
hasAnnotations: getAnnotationResourcesByMotivation(
getSelectedCanvasAnnotations(state, (getSelectedCanvas(state, windowId) || {}).id),
getSelectedTargetAnnotations(state, (getSelectedCanvas(state, windowId) || {}).id),
['oa:commenting', 'sc:painting'],
).length > 0,
sideBarPanel: (getCompanionWindowForPosition(state, windowId, 'left') || {}).content,
......
......@@ -126,17 +126,32 @@ export function getSelectedCanvas(state, windowId) {
}
/**
* Return the current canvas' (selected in a window) annotations
* Return annotations for an array of targets
* @param {object} state
* @param {String} windowId
* @param {Array} targets
* @return {Array}
*/
export function getSelectedTargetsAnnotations(state, targets) {
const annotations = state.annotations
&& targets.map(target => getSelectedTargetAnnotations(state, target));
if (!annotations) return [];
return flatten(annotations);
}
/**
* Return a single target's annotations
* @param {object} state
* @param {String} target
* @return {Array}
*/
export function getSelectedCanvasAnnotations(state, canvasId) {
const annotations = state.annotations && state.annotations[canvasId];
export function getSelectedTargetAnnotations(state, target) {
const annotations = state.annotations && state.annotations[target];
if (!annotations) return [];
return filter(
Object.keys(annotations).map(id => new Annotation(annotations[id].json)),
Object.keys(annotations).map(id => new Annotation(annotations[id].json, target)),
annotation => annotation
&& annotation.present(),
);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment