MDL-70990 core_editor: Replace EDITOR_CONTENT_RESTORED event

The legacy M.core.event.EDITOR_CONTENT_RESTORED event has been replaced
with a new core_editor/events::editorContentRestored native DOM event.

The new event can be listened to at any point in the DOM using the
following syntax:

```
import {eventTypes} from 'core_editor/events';

document.addEventListener(eventTypes.editorContentRestored, handler);
```

A backward-compatabibility layer is included to ensure that any legacy
jQuery event is still called with the same arguments.

This legacy bridge will be removed after Moodle 4.3.
This commit is contained in:
Andrew Nicols 2021-02-24 17:41:35 +08:00
parent a1ccefe2ac
commit a44cee78f1
11 changed files with 111 additions and 32 deletions

View File

@ -1,2 +1,2 @@
define ("core/event",["exports","core_form/events","jquery","core/yui"],function(a,b,c,d){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.default=void 0;c=e(c);d=e(d);function e(a){return a&&a.__esModule?a:{default:a}}var f=function(){var a=c.default.Deferred();d.default.use("event","moodle-core-event",function(){a.resolve(window.M.core.event)});return a.promise()},g=function(a){a=(0,c.default)(a);d.default.use("event","moodle-core-event",function(b){(0,c.default)(document).trigger(M.core.event.FILTER_CONTENT_UPDATED,[a]);var d=new b.NodeList(a.get());b.fire(M.core.event.FILTER_CONTENT_UPDATED,{nodes:d})})},h=function(){d.default.use("event","moodle-core-event",function(a){(0,c.default)(document).trigger(M.core.event.EDITOR_CONTENT_RESTORED);a.fire(M.core.event.EDITOR_CONTENT_RESTORED)})},i={Events:{FORM_FIELD_VALIDATION:"core_form-field-validation"},getLegacyEvents:f,notifyEditorContentRestored:h,notifyFilterContentUpdated:g,notifyFormSubmitAjax:function getRenamedLegacyFunction(a,b,c,d){return function(){window.console.warn("The core/event::".concat(a,"() function has been moved to ").concat(b,"::").concat(c,". ")+"Please update your code to use the new module.");return d.apply(void 0,arguments)}}("notifyFormSubmitAjax","core_form/events","notifyFormSubmittedByJavascript",b.notifyFormSubmittedByJavascript)};a.default=i;return a.default});
define ("core/event",["exports","core_editor/events","core_form/events","jquery","core/yui"],function(a,b,c,d,e){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.default=void 0;d=f(d);e=f(e);function f(a){return a&&a.__esModule?a:{default:a}}var g=function(){var a=d.default.Deferred();e.default.use("event","moodle-core-event",function(){a.resolve(window.M.core.event)});return a.promise()},h=function(a){a=(0,d.default)(a);e.default.use("event","moodle-core-event",function(b){(0,d.default)(document).trigger(M.core.event.FILTER_CONTENT_UPDATED,[a]);var c=new b.NodeList(a.get());b.fire(M.core.event.FILTER_CONTENT_UPDATED,{nodes:c})})},i=function(a,b,c,d){return function(){window.console.warn("The core/event::".concat(a,"() function has been moved to ").concat(b,"::").concat(c,". ")+"Please update your code to use the new module.");return d.apply(void 0,arguments)}},j={Events:{FORM_FIELD_VALIDATION:"core_form-field-validation"},getLegacyEvents:g,notifyEditorContentRestored:i("notifyEditorContentRestored","core_editor/events","notifyEditorContentRestored",b.notifyEditorContentRestored),notifyFilterContentUpdated:h,notifyFormSubmitAjax:i("notifyFormSubmitAjax","core_form/events","notifyFormSubmittedByJavascript",c.notifyFormSubmittedByJavascript)};a.default=j;return a.default});
//# sourceMappingURL=event.min.js.map

File diff suppressed because one or more lines are too long

View File

@ -22,6 +22,7 @@
* @since 3.0
*/
import {notifyEditorContentRestored} from 'core_editor/events';
import {notifyFormSubmittedByJavascript} from 'core_form/events';
// These are only imported for legacy.
@ -68,22 +69,6 @@ const notifyFilterContentUpdated = nodes => {
});
};
/**
* Trigger an event using both JQuery and YUI
* This event alerts the world that the editor has restored some content.
*
* @method notifyEditorContentRestored
*/
const notifyEditorContentRestored = () => {
Y.use('event', 'moodle-core-event', function(Y) {
// Trigger it the JQuery way.
$(document).trigger(M.core.event.EDITOR_CONTENT_RESTORED);
// And again for YUI.
Y.fire(M.core.event.EDITOR_CONTENT_RESTORED);
});
};
/**
* Get a curried function to warn that a function has been moved and renamed
*
@ -105,7 +90,14 @@ const getRenamedLegacyFunction = (oldFunctionName, newModule, newFunctionName, n
export default {
Events,
getLegacyEvents,
notifyEditorContentRestored,
notifyEditorContentRestored: getRenamedLegacyFunction(
'notifyEditorContentRestored',
'core_editor/events',
'notifyEditorContentRestored',
notifyEditorContentRestored
),
notifyFilterContentUpdated,
notifyFormSubmitAjax: getRenamedLegacyFunction(

2
lib/editor/amd/build/events.min.js vendored Normal file
View File

@ -0,0 +1,2 @@
define ("core_editor/events",["exports","core/event_dispatcher","jquery","core/yui"],function(a,b,c,d){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.notifyEditorContentRestored=a.eventTypes=void 0;c=e(c);d=e(d);function e(a){return a&&a.__esModule?a:{default:a}}var f={editorContentRestored:"core_editor/contentRestored"};a.eventTypes=f;a.notifyEditorContentRestored=function notifyEditorContentRestored(a){if(!a){window.console.warn("The HTMLElement representing the editor that was modified should be provided to notifyEditorContentRestored.")}return(0,b.dispatchEvent)(f.editorContentRestored,{},a||document)};var g=!1;if(!g){d.default.use("event","moodle-core-event",function(){document.addEventListener(f.editorContentRestored,function(){(0,c.default)(document).trigger(M.core.event.EDITOR_CONTENT_RESTORED);d.default.fire(M.core.event.EDITOR_CONTENT_RESTORED)})});g=!0}});
//# sourceMappingURL=events.min.js.map

View File

@ -0,0 +1 @@
{"version":3,"sources":["../src/events.js"],"names":["eventTypes","editorContentRestored","notifyEditorContentRestored","editor","window","console","warn","document","legacyEventsRegistered","Y","use","addEventListener","trigger","M","core","event","EDITOR_CONTENT_RESTORED","fire"],"mappings":"uNAwBA,OACA,O,mDAQO,GAAMA,CAAAA,CAAU,CAAG,CAMtBC,qBAAqB,CAAE,6BAND,CAAnB,C,6CAiBoC,QAA9BC,CAAAA,2BAA8B,CAAAC,CAAM,CAAI,CACjD,GAAI,CAACA,CAAL,CAAa,CACTC,MAAM,CAACC,OAAP,CAAeC,IAAf,gHAGH,CACD,MAAO,oBACHN,CAAU,CAACC,qBADR,CAEH,EAFG,CAGHE,CAAM,EAAII,QAHP,CAKV,C,CAED,GAAIC,CAAAA,CAAsB,GAA1B,CACA,GAAI,CAACA,CAAL,CAA6B,CAKzBC,UAAEC,GAAF,CAAM,OAAN,CAAe,mBAAf,CAAoC,UAAM,CAEtCH,QAAQ,CAACI,gBAAT,CAA0BX,CAAU,CAACC,qBAArC,CAA4D,UAAM,CAE9D,cAAOM,QAAP,EAAiBK,OAAjB,CAAyBC,CAAC,CAACC,IAAF,CAAOC,KAAP,CAAaC,uBAAtC,EAGAP,UAAEQ,IAAF,CAAOJ,CAAC,CAACC,IAAF,CAAOC,KAAP,CAAaC,uBAApB,CACH,CAND,CAOH,CATD,EAWAR,CAAsB,GACzB,C","sourcesContent":["// This file is part of Moodle - http://moodle.org/ //\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Javascript events for the `core_editor` subsystem.\n *\n * @module core_editor/events\n * @copyright 2021 Andrew Nicols <andrew@nicols.co.uk>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n * @since 4.0\n */\n\nimport {dispatchEvent} from 'core/event_dispatcher';\nimport jQuery from 'jquery';\nimport Y from 'core/yui';\n\n/**\n * Events for the `core_editor` subsystem.\n *\n * @constant\n * @property {String} editorContentRestored See {@link event:editorContentRestored}\n */\nexport const eventTypes = {\n /**\n * An event triggered when an editor restores auto-saved content.\n *\n * @event editorContentRestored\n */\n editorContentRestored: 'core_editor/contentRestored',\n};\n\n/**\n * Trigger an event to indicate that editor content was restored.\n *\n * @method notifyEditorContentRestored\n * @param {HTMLElement|null} editor The element that was modified\n * @returns {CustomEvent}\n * @fires editorContentRestored\n */\nexport const notifyEditorContentRestored = editor => {\n if (!editor) {\n window.console.warn(\n `The HTMLElement representing the editor that was modified should be provided to notifyEditorContentRestored.`\n );\n }\n return dispatchEvent(\n eventTypes.editorContentRestored,\n {},\n editor || document\n );\n};\n\nlet legacyEventsRegistered = false;\nif (!legacyEventsRegistered) {\n // The following event triggers are legacy and will be removed in the future.\n // The following approach provides a backwards-compatability layer for the new events.\n // Code should be updated to make use of native events.\n\n Y.use('event', 'moodle-core-event', () => {\n // Provide a backwards-compatability layer for YUI Events.\n document.addEventListener(eventTypes.editorContentRestored, () => {\n // Trigger a legacy AMD event.\n jQuery(document).trigger(M.core.event.EDITOR_CONTENT_RESTORED);\n\n // Trigger a legacy YUI event.\n Y.fire(M.core.event.EDITOR_CONTENT_RESTORED);\n });\n });\n\n legacyEventsRegistered = true;\n}\n"],"file":"events.min.js"}

View File

@ -0,0 +1,82 @@
// This file is part of Moodle - http://moodle.org/ //
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Javascript events for the `core_editor` subsystem.
*
* @module core_editor/events
* @copyright 2021 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 4.0
*/
import {dispatchEvent} from 'core/event_dispatcher';
import jQuery from 'jquery';
import Y from 'core/yui';
/**
* Events for the `core_editor` subsystem.
*
* @constant
* @property {String} editorContentRestored See {@link event:editorContentRestored}
*/
export const eventTypes = {
/**
* An event triggered when an editor restores auto-saved content.
*
* @event editorContentRestored
*/
editorContentRestored: 'core_editor/contentRestored',
};
/**
* Trigger an event to indicate that editor content was restored.
*
* @method notifyEditorContentRestored
* @param {HTMLElement|null} editor The element that was modified
* @returns {CustomEvent}
* @fires editorContentRestored
*/
export const notifyEditorContentRestored = editor => {
if (!editor) {
window.console.warn(
`The HTMLElement representing the editor that was modified should be provided to notifyEditorContentRestored.`
);
}
return dispatchEvent(
eventTypes.editorContentRestored,
{},
editor || document
);
};
let legacyEventsRegistered = false;
if (!legacyEventsRegistered) {
// The following event triggers are legacy and will be removed in the future.
// The following approach provides a backwards-compatability layer for the new events.
// Code should be updated to make use of native events.
Y.use('event', 'moodle-core-event', () => {
// Provide a backwards-compatability layer for YUI Events.
document.addEventListener(eventTypes.editorContentRestored, () => {
// Trigger a legacy AMD event.
jQuery(document).trigger(M.core.event.EDITOR_CONTENT_RESTORED);
// Trigger a legacy YUI event.
Y.fire(M.core.event.EDITOR_CONTENT_RESTORED);
});
});
legacyEventsRegistered = true;
}

View File

@ -979,9 +979,9 @@ EditorAutosave.prototype = {
NOTIFY_INFO, RECOVER_MESSAGE_TIMEOUT);
// Fire an event that the editor content has changed.
require(['core/event'], function(event) {
event.notifyEditorContentRestored();
});
require(['core_editor/events'], function(editorEvents) {
editorEvents.notifyEditorContentRestored(this.editor.getDOMNode());
}.bind(this));
return this;
},

File diff suppressed because one or more lines are too long

View File

@ -971,9 +971,9 @@ EditorAutosave.prototype = {
NOTIFY_INFO, RECOVER_MESSAGE_TIMEOUT);
// Fire an event that the editor content has changed.
require(['core/event'], function(event) {
event.notifyEditorContentRestored();
});
require(['core_editor/events'], function(editorEvents) {
editorEvents.notifyEditorContentRestored(this.editor.getDOMNode());
}.bind(this));
return this;
},

View File

@ -216,9 +216,9 @@ EditorAutosave.prototype = {
NOTIFY_INFO, RECOVER_MESSAGE_TIMEOUT);
// Fire an event that the editor content has changed.
require(['core/event'], function(event) {
event.notifyEditorContentRestored();
});
require(['core_editor/events'], function(editorEvents) {
editorEvents.notifyEditorContentRestored(this.editor.getDOMNode());
}.bind(this));
return this;
},

View File

@ -8,6 +8,8 @@ information provided here is intended especially for developers.
- Course nodes added will also be appended and appear within the 'More' option/page.
* The core/event events have been renamed and now fire native events, in addition to the legacy YUI and jQuery events.
The following events have been renamed:
- The EDITOR_CONTENT_RESTORED event has been replaced with a new native event in the `core_editor/events` AMD module
eventTypes.editorContentRestored.
- The FORM_FIELD_VALIDATION event has been replaced with a new native event in the `core_form/events` AMD module
eventTypes.formFieldValidationFailed.
- The FORM_SUBMIT_AJAX event has been replaced with a new native event in the `core_form/events` AMD module