All files / src/lib/tinymce/plugins/graph2d plugin.js

66.66% Statements 8/12
30% Branches 3/10
33.33% Functions 1/3
66.66% Lines 8/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52              17x 17x             32x     32x 32x 32x 32x 32x                                                          
/**
 * Graph 2D helpers for TinyMCE (placeholder HTML, URL for the standalone editor).
 * The graph toolbar uses `tinymceGraph2dOpenUrlPlugin.js` + `windowManager.openUrl` → `/graph-editor`.
 *
 * @author EruditionTX
 */
 
export const GRAPH2D_DATA_ATTR = "data-graph2d";
export const GRAPH2D_CLASS = "graph2d-embed";
 
/**
 * Absolute URL of the graph editor route (`/graph-editor`, respecting Vite `BASE_URL`).
 * Use as TinyMCE `graph2d_url` for `openUrl` iframe dialogs.
 */
export function getGraph2dEditorUrl() {
    Iif (typeof window === "undefined") {
        return "/graph-editor";
    }
    const base = import.meta.env?.BASE_URL || "/";
    const origin = window.location.origin.replace(/\/$/, "");
    const normalizedBase = base.endsWith("/") ? base : `${base}/`;
    const path = new URL("graph-editor", `${origin}${normalizedBase}`).pathname;
    return `${origin}${path}`;
}
 
/**
 * Escapes a string for safe embedding in HTML attributes.
 */
export function escapeGraph2dHtml(s) {
    return String(s ?? "")
        .replace(/&/g, "&")
        .replace(/</g, "&lt;")
        .replace(/>/g, "&gt;")
        .replace(/"/g, "&quot;")
        .replace(/'/g, "&#39;");
}
 
/**
 * Inserts a new graph2d placeholder span into the TinyMCE editor.
 * @param {object} editor - TinyMCE editor instance
 * @param {string} encodedData - serialized graph data
 * @param {string|undefined} imageUrl - preview image data URL
 */
export function insertGraph2dPlaceholder(editor, encodedData, imageUrl) {
    const inner = imageUrl
        ? `<img src="${escapeGraph2dHtml(imageUrl)}" alt="2D Graph" style="max-width:100%;height:auto;display:inline-block;vertical-align:middle;" />`
        : "[2D Graph]";
    editor.insertContent(
        `<p><span class="${GRAPH2D_CLASS}" contenteditable="false" ${GRAPH2D_DATA_ATTR}="${escapeGraph2dHtml(encodedData)}">${inner}</span></p>`
    );
}