Мнения, написани от Joseph Rézeau

Moodle in English -> General developer forum -> Javascript AMD/ES6 and import/export and define... ???

от Joseph Rézeau -
Снимка на Core developers Снимка на Plugin developers Снимка на Testers Снимка на Translators

Hi all fellow Moodle developers,

Here's a little conundrum for those who have nothing better to do during this end-of-year festive season. 😵‍💫

I am developing a new question type, 'guessit' which involves a few JavaScript scripts. I can get 2 functions running OK if I put them inside the same JS file. But I would like to write more then one JS file, in order to extend and debug them more easily. And I'm having problems with importing functions from one script to another. This is related to using AMD vs ES6. The Moodle dev. doc. states that new JS modules should be written ES6 syntax, not AMD. So why do my scripts require AMD syntax? Here's a simplified illustration.

Case #1 works OK
script gapsnavigation.js
/**
 * Initialize the auto-grow input functionality.
 */
export function init() {
    document.querySelectorAll('input[class*="auto-grow-input"]').forEach(function (element) {
        element.addEventListener("keydown", (event) => {
            if (event.keyCode === 32) {
                event.preventDefault(); // Prevent space from being entered
            }
        });
    });
}
---
Case #2 does not work:
script gapsnavigation.js

import { handleKeydownEvent } from './keydownHandler.js';
/**
 * Initialize the auto-grow input functionality.
 */
export function init() {
    document.querySelectorAll('input[class*="auto-grow-input"]').forEach(function (element) {
        handleKeydownEvent(element);
    });
    window.console.log('Hello, world!');
}
script keydownHandler.js
/**
 * Handles the "keydown" event to prevent spaces from being entered.
 * @param {HTMLElement} element - The input element.
 */
export function handleKeydownEvent(element) {
    element.addEventListener("keydown", (event) => {
            if (event.keyCode === 32) {
                event.preventDefault(); // Prevent space from being entered
            }
    });
}

ChatGPT told me "If your environment uses RequireJS (AMD module loader) instead of ES modules, the import/export syntax will not work directly. Instead, you need to use define to define modules and require to load them." So this is what I"ve done:

Case #3 works OK
script gapsnavigation.js

define(['./keydownHandler'], function (keydownHandler) {
    return {
        init: function () {
            document.querySelectorAll('input[class*="auto-grow-input"]').forEach(function (element) {
                keydownHandler.handleKeydownEvent(element);
            });
        }
    };
});

script keydownHandler.js

define(function () {
    return {
        handleKeydownEvent: function (element) {
            element.addEventListener("keydown", (event) => {
                if (event.keyCode === 32) {
                    event.preventDefault(); // Prevent space from being entered
                }
            });
        }
    };
});

OK, but... I don't understand why "my environment uses RequireJS (AMD module loader) instead of ES modules".😵‍💫

Explanations welcome and ... do enjoy the festive season anyway!

Снимка на Core developers Снимка на Plugin developers Снимка на Testers Снимка на Translators

Thanks for your report, German

"2) When I tried to recreate the ERROR caused by a double parenthesis, the server seemed to freeze."

It may depend on the complexity of the regular expression you entered. For instance this simple parenthesis error does trigger an error message: 

Answer 2 (50%) (men|women)) ERROR! Check your parentheses or square brackets! (men|women))

But it's quite possible that a parenthesis (or other type of) error in a complex regular expression will freeze the server. I suppose you tried with this example from the doc:

The colours of the French flag are: {1:REGEXP:%100%blue, white and red#Congratulations!~--.*(blue|red|white).*#You have not even found one of the colours of the French flag!~--.*(&&blue&&red&&white).*#You have not found all the colours of the French flag~--.*blue.*#The colour of the sky is missing!}.

By inserting an extra parenthesis in that expression I did manage to freeze my server. 😡

Thanks for reporting, anyway. I will look into this problem and try to find a more robust solution! In the meantime, I hope people using it will either only use less complex expressions OR avoid making errors.😉

Moodle in English -> Lounge -> A rose for Xmas!

от Joseph Rézeau -
Снимка на Core developers Снимка на Plugin developers Снимка на Testers Снимка на Translators

No, not a so-called Christmas roseaka hellebore, but a real rose just opened in my garden today, on Xmas Eve!.

Wishing all moodlers happy end-of-year festivities! See you all again in 2025.

Rosa Floribunda ‘Alexandra David-Neel’

Rosa Floribunda ‘Alexandra David-Neel’. 24-12-2024


Среден рейтинг: Very cool (6)