Add a highlight.js loaded hook

This event allows plugins to listen for the loading of the HLJS library
so that they can install custom language definitions or otherwise
change highlighting behavior.

Change-Id: Id3e55e3d7bc1421a5b8564846901ab9b69146cbd
This commit is contained in:
Peter Marshall
2019-03-11 15:02:56 +01:00
parent e951478c60
commit d1f3642ce3
6 changed files with 39 additions and 0 deletions

View File

@@ -28,6 +28,7 @@
POST_REVERT: 'postrevert',
ANNOTATE_DIFF: 'annotatediff',
ADMIN_MENU_LINKS: 'admin-menu-links',
HIGHLIGHTJS_LOADED: 'highlightjs-loaded',
};
const Element = {
@@ -69,6 +70,9 @@
case EventType.LABEL_CHANGE:
this._handleLabelChange(detail);
break;
case EventType.HIGHLIGHTJS_LOADED:
this._handleHighlightjsLoaded(detail);
break;
default:
console.warn('handleEvent called with unsupported event type:',
type);
@@ -188,6 +192,16 @@
}
},
_handleHighlightjsLoaded(detail) {
for (const cb of this._getEventCallbacks(EventType.HIGHLIGHTJS_LOADED)) {
try {
cb(detail.hljs);
} catch (err) {
console.error(err);
}
}
},
modifyRevertMsg(change, revertMsg, origMsg) {
for (const cb of this._getEventCallbacks(EventType.REVERT)) {
try {

View File

@@ -300,6 +300,17 @@ limitations under the License.
assert.isTrue(errorStub.calledTwice);
});
test('highlightjs-loaded event', done => {
const testHljs = {_number: 42};
plugin.on(element.EventType.HIGHLIGHTJS_LOADED, throwErrFn);
plugin.on(element.EventType.HIGHLIGHTJS_LOADED, hljs => {
assert.deepEqual(hljs, testHljs);
assert.isTrue(errorStub.calledOnce);
done();
});
element.handleEvent(element.EventType.HIGHLIGHTJS_LOADED, {hljs: testHljs});
});
test('versioning', () => {
const callback = sandbox.spy();
Gerrit.install(callback, '0.0pre-alpha');