An Origin object is the return value for the useOrigin hook and gives us the progress of an upload including the current URL for the uploading or uploaded file.
export type OriginUploading = {
url: string
status: "uploading"
sentBytes: number
totalBytes: number
eventEmitter: EventEmitter<OriginEventTypes>
finishPromise: Promise<Origin>
}
export type OriginUploaded = {
url: string
status: "complete"
}
export type OriginError = {
url: string
status: "error"
message: string
}
export type Origin = OriginUploading | OriginUploaded | OriginError
useOrigin(originKey: string) => OriginThe useOrigin hook takes an originKey and returns an Origin object.
For example:
function SimpleAttachment({
attributes,
element,
children,
}: RenderElementProps) {
const origin = useOrigin(element.originKey)
return (
<div {...attributes}>
<div>
<a href={origin.url}>Link to File</a>
</div>
<div>Upload Status: {origin.status}</div>
{children}
</div>
)
}
originKeyThe originKey is a string which represents an Origin in one of two ways:
If it is a URL, then it is converted to an OriginUploaded object.
For example, if the originKey is https://www.portive.com/ then the Origin would be:
const origin = {
url: "https://www.portive.com/",
status: "complete",
}
When a document is saved by calling the editor.portive.save method or normalized by calling the editor.portive.normalize method, all the originKey values are returned as URL strings.
When a file is uploaded, the originKey is set to a random alphanumeric string. We add that random originKey string with the value for an OriginUploading object. During the upload, the OriginUploading object is updated to reflect the amount the file has been uploaded. If there is an error, it is set to an OriginError object. When the file successfully uploaded, it is set to an OriginComplete object.
url: stringThe value of url is either:
createObjectURLIn any state, the url value can be used as the src for an img tags such as <img src={origin.src} /> and is useful for showing a preview of an image while the image is still uploading.
status: "uploading" | "complete" | "error"Indicates the current upload status:
uploading: File is currently an in progress uploadcomplete: File has completed uploaderror: There was an error during uploading. See message for why.Properties only found when status is uploading.
sentBytes: numberThe number of bytes sent so far during the upload.
totalBytes: numberThe total number of bytes in the file that need to be uploaded.
eventEmitter: EventEmitter3An instance of EventEmitter3 which has the same API as Node.js EventEmitter but works in the browser.
It emits three event types where the event object is an Origin object.
progress: Updating the progress of an uploadcomplete: Upload completederror: Error during upload🌞 Typically
eventEmitterisn't be used directly and is an internal implementation detail; however, you can attach event listeners toeventEmitterto get the progress of an upload.
finishPromise: Promise<Origin>A Promise that resolves with an Origin when uploading is completed.
🌞 Typically
finishPromiseisn't be used directly and is an internal implementation detail; however, you canawaitthisPromiseto wait for a file to finish uploading.
message: stringThe reason the upload could not be completed as a string.