The current Field Widget Action works very well but it has been discussed in the latest AI Initiative Sprint Review that it will be a better UX if the triggering could be done directly on the MDX editor.

So the task is to create a custom button on the MDX Toolbar. The button should open a window to aid with uploading a file or entering a web address which will be used by the Document Loader to fetch the content.

Think of one or more fields in that form to be used in case the user requires an authenticated call to an API endpoint.

Look into whether this functionality should live inside the Document Loader module or a separate submodule (not all use cases of Document Loader might include MDX).

CommentFileSizeAuthor
#9 mdx1.gif5.16 MBunqunq
#6 mdx.gif4.68 MBunqunq
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

unqunq created an issue. See original summary.

ahmad khader’s picture

I think this ticket should be to provide a function in a DocumentLoaderManager service to convert getToolInputSchema into Drupal fields, similar to how AI tool + explorer works.

I think all the logic is on the explorer form; we could move it into the service.

ahmad khader’s picture

This way, we could fix both of the issues in one singular code logic, and other integrations in the future will be bulletproof as well.

ahmad khader’s picture

Also, I think the best approach is to have MDX logic in a sub-module similar to fwa and tool modules. We could also include ck-editor integration as well

unqunq’s picture

StatusFileSize
new4.68 MB

Good points. I have a version of the MDX integration in progress in a submodule.

Here is the first working version:
MDX integration

We've had a chat in Slack on #document_loader about improving this by updating the modal form and using a different icon.

The CKEditor should be a separate ticket - needs to go in its own submodule as well.

unqunq’s picture

Assigned: Unassigned » unqunq

unqunq’s picture

StatusFileSize
new5.16 MB

An updated demo:

Demo of MDX integration for document loader.

unqunq’s picture

unqunq’s picture

Assigned: unqunq » Unassigned
Status: Active » Needs review
bbruno’s picture

Status: Needs review » Needs work

I really like this feature, super useful! I've left some comments regarding the JS/MDXEditor part of the implementation :)

ahmad khader’s picture

Tys, bbruno and nick for the work.
I fixed all issues mentioned by bbruno. Additionally, I fixed;

1. Button stays visible in diff/source mode

Because the "Load document" button is imperatively injected into the DOM (outside React), it was not removed when the MDX editor switches to diff or source mode — the DiffSourceToggleWrapper only manages its own React children. Fixed by adding a second MutationObserver on the toolbar that detects mode changes (by checking for the toolbarTitleMode class) and hides the button with display: none when in an alternate mode. Uses a disconnect/reconnect pattern to avoid infinite loops when mutating the DOM inside the observer callback.

2. Button appears first instead of last when switching back to rich-text mode

When switching from diff/source back to rich-text mode, React re-renders all toolbar children as new DOM nodes. Our imperatively injected button was still at its old position, which put it before the newly rendered buttons — making it appear first in the toolbar. Fixed by re-positioning the button with toolbar.insertBefore(button, toolbar.lastElementChild) each time rich-text mode is restored, so it always sits just before the diff/source toggle group at the end.

3. Tooltip styling doesn't match native toolbar tooltips

The native MDX editor toolbar buttons use @radix-ui/react-tooltip (via a TooltipWrap component) which renders styled floating tooltips via React portals. Since our button is injected imperatively outside React, it cannot use the Radix tooltip component. A plain title attribute produces a browser-native tooltip that looks completely different. Fixed by creating a custom DOM-based tooltip (.dl-mdx-tooltip) that appears on mouseenter with a 100ms delay (matching Radix's delayDuration), positioned 10px above the button (matching Radix's sideOffset), using the same CSS custom properties as the native _tooltipContent class: --baseText for background, --baseBase for text color, --radius-medium for border-radius, --text-xs for font-size. The tooltip is dismissed on mouseleave and on click.

ahmad khader’s picture

Status: Needs work » Needs review

ahmad khader’s picture

I created a separate branch that dynamically loads the loaders, meaning you can load context from anywhere you want, not only files, and it includes the media widget, which I think is more user-friendly than what we currently have.

kristen pol’s picture