Can't turn off Formatted Text programmatically
Asked Answered
S

1

6

I am trying to turn off Formatted Text for every cell in my graphs programmatically so I can avoid the Not supported by viewer in an mxgraph editor (similar to Draw.io)

Here is my attempt:

graph.stopEditing();
graph.getModel().beginUpdate();

try
{
    var cells = graph.getChildCells(graph.getDefaultParent());
    cells.forEach(function (cell)
    {
        var state = graph.getView().getState(cell);
        if (state == null)
        {
            return;
        }
        if (state.style['html'] != '1') {
            return;
        }

        var label = graph.convertValueToString(state.cell);
        if (mxUtils.getValue(state.style, 'nl2Br', '1') != '0')
        {
            label = label.replace(/\n/g, '').replace(/<br\s*.?>/g, '\n');
        }

        // Removes HTML tags
        var temp = document.createElement('div');
        temp.innerHTML = label;
        label = mxUtils.extractTextWithWhitespace(temp.childNodes);

        graph.cellLabelChanged(state.cell, label);
        graph.setCellStyles('html', null);
    });

    ui.fireEvent(new mxEventObject('styleChanged', 'keys', ['html'],
        'values', ['0'], 'cells', cells));
}
finally
{
    graph.getModel().endUpdate();
}

But it doesn't seem to work (i.e. if I turn it off from the checkbox in the Format panel, it works; but this way, I still get the [Not supported by viewer] output in my SVG) What am I doing wrong about it?

Solatium answered 18/1, 2019 at 9:50 Comment(0)
P
0

Replace:

graph.setCellStyles('html', null);

with:

state.style['html'] = null;
Pytlik answered 4/2, 2019 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.