refactor: centralize context-menu attribute and element helpers

Replace scattered inline encodeURIComponent(JSON.stringify(...)) and
manual oncontextmenu/dataset assignments with unified helpers added to
renderFormatMethods:

  - encodeContextMenuModel(context): safe JSON serialization for data-model
  - getContextMenuAttrs(context, tooltip): HTML attribute string for
    template literals (used in 5 files)
  - getContextMenuProps(context, tooltip): props object for imperative
    element assignment (used in missingBrowserMethods)
  - setContextMenuElement(element, context, tooltip): sets or clears
    data-model, data-tooltip, and oncontextmenu on a DOM element

Reduces duplication across 5 modules, adds try/catch around JSON
serialization, and removes 14 lines of repeated oncontextmenu wiring.
This commit is contained in:
Dariusz L
2026-06-24 23:19:02 +02:00
parent b5d10fa5cc
commit fb8a65758b
6 changed files with 73 additions and 52 deletions
@@ -881,8 +881,7 @@ export const missingBrowserMethods = {
};
};
const getLocalPathContextAttrs = (context, tooltip = 'Right-click for options') => {
if (!context) return '';
return ` data-model="${this.escapeHtml(encodeURIComponent(JSON.stringify(context)))}" oncontextmenu="window.MLOpenContextMenu(event, this)" data-tooltip="${this.escapeHtml(tooltip)}"`;
return this.getContextMenuAttrs(context, tooltip);
};
const makeModelEntry = (model, index, preferredCategory = getPreferredModelCategory()) => {
const category = getModelCategory(model);
@@ -1542,10 +1541,7 @@ export const missingBrowserMethods = {
};
if (contextMenuModel) {
notificationProps["data-model"] = encodeURIComponent(JSON.stringify(contextMenuModel));
notificationProps.oncontextmenu = (event) => {
window.MLOpenContextMenu?.(event, event.currentTarget);
};
Object.assign(notificationProps, this.getContextMenuProps?.(contextMenuModel) || {});
}
if (type === 'success') {