h5p-webcomponents
This package provides plain HTML 5 Web Components that you can use in your project to insert H5P players or editors without having to worry about the details of setting up H5P, loading scripts and styles etc. They only work in conjunction with a custom H5P server (e.g. one using @lumieducation/h5p-server) that provides endpoints that do these things:
    get the required data about content (one for playing, one for editing)
    save content created in the editor
    serve all AJAX endpoints required by the H5P core
It is recommended to checkout the rest example that uses @lumieducation/h5p-server to see how the component can be used in an application.
If you are looking for a solution to get the H5P player working without any server-side components, you should check out h5p-standalone by Tunapanda.

Browser support

The components deliberately avoid using component libraries like Polymer in order to reduce the number of third party dependencies of your project. However, this also means that your users need to use a browser that supports all the web component features used in this library (ES6 classes, window.customElements and custom HTML templates).
As of December 2020 all major browsers that are still supported by their vendors will work, but if you still need to support legacy browsers like IE 11 or older Edge versions (before it was based on Chromium), you will have to use polyfills to get the web components to work. You also have to transpile the library into ES5. See this page for more details.
The component also uses some modern JavaScript features like async/await and optional chaining. These features are supported by all cutting-edge modern browsers as of December 2020. Use a transpiler if you have to support older browsers.

Usage

Install the component with npm or yarn:
1
$ npm install @lumieducation/h5p-webcomponents
Copied!
Then, import the component in your JavaScript code and register the h5p-player or h5p-editor tag globally:
1
import { H5PPlayerComponent, H5PEditorComponent } from '@lumieducation/h5p-webcomponents';
2
window.customElements.define('h5p-player', H5PPlayerComponent);
3
window.customElements.define('h5p-editor', H5PEditorComponent);
Copied!
There is also a convenience function that you can use instead of importing the components manually:
1
import { defineElements } from '@lumieducation/h5p-webcomponents';
2
defineElements('h5p-player'); // only registers the player component
3
defineElements('h5p-editor'); // only registers the editor component
4
defineElements('h5p-player', 'h5p-editor'); // registers player and editor component
5
defineElements(); // registers player and editor component
Copied!
Then, you can insert the components into your DOM:
Option 1: Insert it directly in HTML markup:
1
<h5p-player id='player' content-id='XXXX'></h5p-player>
2
<h5p-editor id='editor' content-id='XXXX'></h5p-editor>
Copied!
and later set the loadContentCallback (and saveContentCallback for the editor) attribute from your JavaScript code.
1
document.getElementById('player').loadContentCallback = async (contentId) => { /** retrieve content model from server and return it as Promise **/ };
2
document.getElementById('editor').loadContentCallback = async (contentId) => { /** retrieve content model from server and return it as Promise **/ };
3
document.getElementById('editor').saveContentCallback = async (contentId, requestBody) => { /** save content on server **/ };
Copied!
Option 2: Create the component in JavaScript:
1
const h5pPlayer = document.createElement('h5p-player');
2
h5pPlayer.setAttribute('content-id', 'XXXX');
3
h5pPlayer.loadContentCallback = async (contentId) => { /** retrieve content model from server and return it as Promise **/ };
4
someOtherElement.appendChild(h5pPlayer);
5
6
const h5pEditor = document.createElement('h5p-editor');
7
h5pEditor.setAttribute('content-id', 'XXXX');
8
h5pEditor.loadContentCallback = async (contentId) => { /** retrieve content model from server and return it as Promise **/ };
9
h5pEditor.saveContentCallback = async (contentId, requestBody) => { /** save content on server **/ };
10
someOtherElement.appendChild(h5pEditor);
Copied!

Initializing and refreshing content

The components automatically (re-)load data from the server by calling loadContentCallback that you must set as a property of the DOM element (see above for examples). The components won't work without the callback.
The content is automatically loaded from the server after both of these conditions have been fulfilled (in any order):
1) loadContentCallback is set 2) content-id is set
You can change the value of content-id and it will discard the old content and display the new one. You can also safely remove the component from the DOM.
If you want to use the editor to create new content, you can set the content-id to the string new. Caution: If the newly created content is saved later, then its contentId will be undefined in saveContententCallback, not new!

Saving content in the editor

To save the user's changes in the editor, you have to execute the component's save() method:
1
try {
2
const { contentId, metadata } = await h5pEditor.save();
3
// do something with the return values or ignore them
4
} catch error {
5
// report errors
6
}
Copied!
The component will perform some client-side validation of the entered data and call saveContentCallback (see below) with all the data needed to save the content.

Callbacks

You must provide these callbacks for the components to work:

H5PPlayerComponent

loadContentCallback

1
loadContentCallback = async (contentId: string) => Promise<IPlayerModel>
2
/** see types.ts in @lumieducation/h5p-server for details how IPlayerModel looks
3
like **/
Copied!
You have to set loadContentCallback to a function that retrieves the necessary data from the backend. It returns a promise of data that follows the structure of IPlayerModel in types.ts in @lumieducation/h5p-server. If there is an error, the callback should throw an error object with the error message in the message property.
If you use @lumieducation/h5p-server you will get the necessary information by using a renderer that simply returns the player model if you call H5PPlayer.render(...):
1
h5pPlayerOnServer.setRenderer(model => model);
2
const playerModel = await h5pPlayerOnServer.render(contentId, user);
3
// send playerModel to client and return it in loadContentCallback
Copied!

H5PEditorComponent

loadContentCallback

1
loadContentCallback = async (contentId?: string) => Promise<
2
IEditorModel /** see types.ts in @lumieducation/h5p-server for details **/ & {
3
library?: string;
4
metadata?: IContentMetadata;
5
params?: any;
6
}>
Copied!
This callback is executed when the component needs to load data for a content id. The callback must create a request to an endpoint on the server, which retrieves all necessary information. The server-side implementation of the endpoint using @lumieducation/h5p-server has to combine the results of H5PEditor.render(...) and H5PEditor.getContent(...). The render must be set to simply return the editor model like this:
1
h5pEditorOnServer.setRenderer(model => model);
Copied!
Notes:
    contentId can be undefined if the editor is used to create new content
    The library, metadata and params property of the returned object must
    only be defined if contentId is defined.
    The callback should throw an error with a message in the message property if
    something goes wrong.

saveContentCallback

1
saveContentCallback = async (
2
contentId: string,
3
requestBody: {
4
library: string;
5
params: {
6
params: any;
7
metadata: any;
8
}
9
) => Promise<{ contentId: string; metadata: IContentMetadata }>
Copied!
This callback is executed when the editor was told to save its content. You have to reach out to the server and persist the changes. When using @lumieducation/h5p-server, the server-side endpoint should call H5PEditor.saveOrUpdateContentReturnMetaData(...) and then return the result to the client, which returns the result as the return value of saveContentCallback.
Note: contentId can be undefined, if the user is creating new content.

Events

The components emit a few events using the standard dispatchEvent method of the component. You can listen to them by adding an event listener with addEventListener.

H5PPlayerComponent

initialized

This event is emitted by this component when the H5P player has initialized. The event includes the contentId in event.detail.contentId. Note: The event is emitted by H5P at a point when all the scripts but possibly not all resources (images etc.) might be loaded. You should only use the event as an indication of initialization state.

xAPI

This is an event that is emitted by this component when an action is triggered on or inside the H5P content through the H5P player. The event includes the statement in event.detail.statement,context in event.detail.context, and event in event.detail.event.

H5PEditorComponent

editorloaded

This event is emitted when the H5P editor has been fully loaded and the editor is usable. It is advisable to block user interaction with the component until this event has been fired. The event includes the contentId in event.detail.contentId and an ubername of the main content type in event.detail.ubername.

saved

This event is emitted when saving was successful. The respective contentId can be found in event.detail.contentId and the content metadata in event.detail.metadata.
Note: You can also simply use the return value of the save() method instead of subscribing to this event.

save-error

This event is emitted when there was an error while saving the content. A more detailed message can be found in event.detail.message.
Note: You can also simply catch errors by wrapping the save() method in a try {...} catch {...} block instead of subscribing to this event.

Executing underlying H5P functionality

The H5PPlayerComponent offers properties and methods that can be used to do things with the underlying "core" H5P data structures and objects:

h5pInstance

This property is the object found in H5P.instances for the contentId of the object. Contains things like the parameters of the content, its metadata and structures created by the content type's JavaScript.
The object is only available after the initialized event was fired. Important: This object is only partially typed and there are more properties and methods on it!

h5pObject

H5P has a global "H5P" namespace that is often used like this:
1
H5P.init(); // initialize H5P
2
H5P.externalDispatcher.on('xAPI', myCallback);
3
const dialog = new H5P.Dialog(...);
Copied!
The problem you'll face when you try to use this namespace is that you typically want to operate on the object inside the H5P iframe if a content type requires iframe embedding. The h5pObject property solves this problem: It contains the correct global H5P namespace regardless of whether there's an iframe or not.
You can use it like this:
1
const H5Pns = myPlayerComponent.h5pObject;
2
H5Pns.externalDispatcher.on('xAPI', myCallback);
3
const dialog = new H5Pns.Dialog(...);
Copied!
The property is only available after the initialized event was fired. Important: This object is only partially typed and there are more properties and methods on it!

getCopyrightHtml

You can get the copyright notice of the content by calling this method. Returns undefined if there is not copyright information. Returns HTML code that you must display somewhere.

hasCopyrightInformation

Returns true if there is copyright information to be displayed.

resize

Signals the H5P content inside the component to resize itself according to the dimensions of the container. Can be called if you have changed the size of the container in an unusual fashion (e.g. by scaling it with CSS transform) and you need to resize the content manually. The component already automatically calls the resize function when its own size is changed, so this will rarely be the case.

showCopyright

Shows the copyright information in a window overlaying the H5P content.

Support

This work obtained financial support for development from the German BMBF-sponsored research project "lea.online -" (FKN: 41200147).
Read more about them at the following websites:
Last modified 27d ago