-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(html-exporter): lazy loaded JS and CSS files work (#1362)
* fix(html-exporter): lazy loaded JS and CSS files work * refactor(html-exporter): separated overrides into own JS file * fix(h5p-html-exporrter): getLibraryFilePath has fallback * refactor(h5p-html-exporter): load file override encapsulated * fix(html-exporter): added library.json vor H5P.Timeline * fix(h5p-html-exporter): accept versioned paths in further resources * fix(h5p-html-exporter): corrected filePath * docs(h5p-html-exporter): added docs
- Loading branch information
Showing
4 changed files
with
87 additions
and
20 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
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,68 @@ | ||
/** | ||
* This file is injected into the HTML bundle (in a minified form) if there were | ||
* any files in the library directories which weren't already included. | ||
*/ | ||
|
||
(function () { | ||
// By providing a custom implementation of H5P.ContentType we can inject the | ||
// resource files when a H5P content type generates the path to a file. | ||
H5P.ContentType = function (isRootLibrary) { | ||
function ContentType() {} | ||
ContentType.prototype = new H5P.EventDispatcher(); | ||
ContentType.prototype.isRoot = function () { | ||
return isRootLibrary; | ||
}; | ||
ContentType.prototype.getLibraryFilePath = function (filePath) { | ||
return ( | ||
furtherH5PInlineResources[ | ||
this.libraryInfo.versionedNameNoSpaces + '/' + filePath | ||
] || filePath | ||
); | ||
}; | ||
return ContentType; | ||
}; | ||
|
||
// We override setting <script src="..."/> behavior to be able to inject scripts | ||
// that don't use H5P.ContentType.getLibraryFilePath. | ||
var nativeSetScriptAttribute = HTMLScriptElement.prototype.setAttribute; | ||
HTMLScriptElement.prototype.setAttribute = function (name, value) { | ||
if (name === 'src') { | ||
var match = value.match(/^.\/libraries\/([^?]+)\??.*$/) | ||
if (match) { | ||
const file = match[1]; | ||
if (furtherH5PInlineResources[file]) { | ||
value = furtherH5PInlineResources[file]; | ||
} | ||
} | ||
} | ||
nativeSetScriptAttribute.call(this, name, value); | ||
}; | ||
Object.defineProperty(HTMLScriptElement.prototype, 'src', { | ||
// also apply the modification if the src is set with the src property | ||
set(value) { | ||
this.setAttribute('src', value); | ||
} | ||
}); | ||
|
||
// We override setting <link href="..."/> to be able to inject styles that don't | ||
// use H5P.ContentType.getLibraryFilePath. | ||
var nativeSetLinkAttribute = HTMLLinkElement.prototype.setAttribute; | ||
HTMLLinkElement.prototype.setAttribute = function (name, value) { | ||
if (name === 'href') { | ||
var match = value.match(/^.\/libraries\/([^?]+)\??.*$/) | ||
if (match) { | ||
const file = match[1]; | ||
if (furtherH5PInlineResources[file]) { | ||
value = furtherH5PInlineResources[file]; | ||
} | ||
} | ||
} | ||
nativeSetLinkAttribute.call(this, name, value); | ||
}; | ||
Object.defineProperty(HTMLLinkElement.prototype, 'src', { | ||
// also apply the modification if the src is set with the src property | ||
set(value) { | ||
this.setAttribute('src', value); | ||
} | ||
}); | ||
})(); |