-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow dropping urls, whole IIIF manifests, or images onto mirador
- Loading branch information
Showing
8 changed files
with
416 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { handleDrop } from '../../../src/components/IIIFDropTarget'; | ||
|
||
const monitor = jest.fn(); | ||
|
||
describe('handleDrop', () => { | ||
let onDrop; | ||
|
||
beforeEach(() => { | ||
onDrop = jest.fn(); | ||
}); | ||
|
||
it('handles url lists', () => { | ||
const item = { | ||
urls: [ | ||
'http://example.com/?manifest=http://example.com/iiif/1.json', | ||
'http://example.com/?manifest=http://example.com/iiif/2.json&canvas=url', | ||
], | ||
}; | ||
const props = { onDrop }; | ||
|
||
handleDrop(item, monitor, props); | ||
expect(onDrop).toHaveBeenCalledWith({ canvasId: null, manifestId: 'http://example.com/iiif/1.json' }, props, monitor); | ||
expect(onDrop).toHaveBeenCalledWith({ canvasId: 'url', manifestId: 'http://example.com/iiif/2.json' }, props, monitor); | ||
}); | ||
|
||
it('handles manifests', () => { | ||
const file = new File(['{ "data": 123 }'], 'manifest.json', { type: 'application/json' }); | ||
const item = { | ||
files: [ | ||
file, | ||
], | ||
}; | ||
|
||
const props = { onDrop }; | ||
|
||
const promise = handleDrop(item, monitor, props); | ||
|
||
return promise.then(() => { | ||
expect(onDrop).toHaveBeenCalledWith( | ||
expect.objectContaining({ manifestJson: '{ "data": 123 }' }), | ||
props, | ||
monitor, | ||
); | ||
}); | ||
}); | ||
|
||
// jsdom doesn't load images. | ||
xit('handles images by fabricating a temporary manifest', () => { | ||
const arrayBuffer = Uint8Array.from(window.atob('R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='), c => c.charCodeAt(0)); | ||
const file = new File(arrayBuffer, 'image.gif', { type: 'image/gif' }); | ||
const item = { | ||
files: [ | ||
file, | ||
], | ||
}; | ||
|
||
const props = { onDrop }; | ||
|
||
const promise = handleDrop(item, monitor, props); | ||
|
||
return promise.then(() => { | ||
expect(onDrop).toHaveBeenCalledWith( | ||
expect.objectContaining({ manifestJson: '{}' }), | ||
props, | ||
monitor, | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { v4 as uuid } from 'uuid'; | ||
import { NativeTypes } from 'react-dnd-html5-backend'; | ||
import { useDrop } from 'react-dnd'; | ||
|
||
/** */ | ||
export const handleDrop = (item, monitor, props) => { | ||
const { onDrop } = props; | ||
|
||
if (item.urls) { | ||
item.urls.forEach((str) => { | ||
const url = new URL(str); | ||
const manifestId = url.searchParams.get('manifest'); | ||
const canvasId = url.searchParams.get('canvas'); | ||
|
||
if (manifestId) onDrop({ canvasId, manifestId }, props, monitor); | ||
}); | ||
} | ||
|
||
if (item.files) { | ||
const manifestFiles = item.files.filter(f => f.type === 'application/json'); | ||
const manifestPromises = manifestFiles.map(file => ( | ||
new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.addEventListener('load', () => { | ||
const manifestJson = reader.result; | ||
const manifestId = uuid(); | ||
|
||
if (manifestJson) onDrop({ manifestId, manifestJson }, props, monitor); | ||
resolve(); | ||
}); | ||
reader.readAsText(file); | ||
}) | ||
)); | ||
|
||
const imageFiles = item.files.filter(({ type }) => type.startsWith('image/')); | ||
|
||
let imagePromise; | ||
|
||
if (imageFiles.length > 0) { | ||
const id = uuid(); | ||
const imageData = imageFiles.map(file => ( | ||
new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
console.log(file); | ||
reader.addEventListener('load', () => { | ||
const image = new Image(); | ||
image.addEventListener('load', () => { | ||
resolve({ | ||
height: image.height, | ||
name: file.name, | ||
type: file.type, | ||
url: reader.result, | ||
width: image.width, | ||
}); | ||
}); | ||
image.src = reader.result; | ||
}); | ||
reader.readAsDataURL(file); | ||
}) | ||
)); | ||
|
||
imagePromise = Promise.all(imageData).then((images) => { | ||
const manifestJson = { | ||
'@context': 'http://iiif.io/api/presentation/3/context.json', | ||
id, | ||
items: images.map(({ | ||
name, type, width, height, url, | ||
}, index) => ({ | ||
height, | ||
id: `${id}/canvas/${index}`, | ||
items: [ | ||
{ | ||
id: `${id}/canvas/${index}/1`, | ||
items: [{ | ||
body: { | ||
format: type, | ||
id: url, | ||
type: 'Image', | ||
}, | ||
height, | ||
id: `${id}/canvas/${index}/1/image`, | ||
motivation: 'painting', | ||
target: `${id}/canvas/${index}/1`, | ||
type: 'Annotation', | ||
width, | ||
}], | ||
type: 'AnnotationPage', | ||
}, | ||
], | ||
label: name, | ||
type: 'Canvas', | ||
width, | ||
})), | ||
label: images[0].name, | ||
type: 'Manifest', | ||
}; | ||
|
||
const manifestId = uuid(); | ||
if (manifestJson) onDrop({ manifestId, manifestJson }, props, monitor); | ||
}); | ||
} | ||
|
||
return Promise.all([...manifestPromises, imagePromise]); | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
/** */ | ||
export const IIIFDropTarget = (props) => { | ||
const { children, onDrop } = props; | ||
const [{ canDrop, isOver }, drop] = useDrop({ | ||
accept: [NativeTypes.URL, NativeTypes.FILE], | ||
collect: monitor => ({ | ||
canDrop: monitor.canDrop(), | ||
isOver: monitor.isOver(), | ||
}), | ||
/** */ | ||
drop(item, monitor) { | ||
if (!onDrop) return; | ||
handleDrop(item, monitor, props); | ||
}, | ||
}); | ||
// TODO: give some indication the app receives drops | ||
const isActive = canDrop && isOver; // eslint-disable-line no-unused-vars | ||
|
||
return ( | ||
<div ref={drop}> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
IIIFDropTarget.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
onDrop: PropTypes.func.isRequired, | ||
}; |
Oops, something went wrong.