Skip to content

[[jsxgraph]] and [[javascript]], input-ref-... attributes block the use of JS module imports inside the block #1616

@aharjula

Description

@aharjula

Not an issue for those that don't import stuff, but those needing to import stuff and act on inputs at the same time may suffer from this.

A problem exists in the current logic for the input-ref-... shorthand. It provides those references by wrapping the whole contents of the block in a function to be called only once those references have been resolved. One cannot use import statements as those need to be defined at the top-level, and that wrapping is most definitely not top-level.

For now, the workaround is to not use the input-ref-... shorthand if one needs to do imports; basically, don't do this, it won't work:

[[javascript input-ref-ans1="ans1ref" input-ref-ans2="ans2ref"]]
import * as magic from "somewhere out there";

...
[[/javascript]]

Instead, do the ref requests on your own like this:

[[javascript]]
import * as magic from "somewhere out there";

Promise.all([
 stack_js.request_access_to_input('ans1',true),
 stack_js.request_access_to_input('ans2',true)
]).then(([ans1ref, ans2ref]) => {
...
});
[[/javascript]]

We will need to figure out if we can lift imports out of the wrapping automatically from raw code, which would make things simpler for the author or if we need to do something else that would make things less compatible with normal JS scripting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions