Edit TextBlock of Node on Create

Is it possible to edit a textblock of a node after create? Here is what I have tried (see three commented out attempts below:

function createNode(location) {
    var newNode = {text: "test", fields:[{name:"", isUnderline:false}], location: location};
    var model = fileDiagram.model;
    fileDiagram.commit(function() {
        node = model.addNodeData(newNode);
    var node = fileDiagram.findNodeForData(newNode);
    node.isSelected = true;

    // ATTEMPT 1: code to try to edit the node on creation
    // fileDiagram.commandHandler.doKeyDown("q");
    // fileDiagram.commandHandler.editTextBlock(node.findObject("ENTITY_TYPE"));

    // ATTEMPT 2
    // fileDiagram.toolManager.textEditingTool.textBlock = node;
    // fileDiagram.toolManager.textEditingTool.doActivate();

    // ATTEMPT 3
    // fileDiagram.toolManager.textEditingTool.textBlock = node.findObject("ENTITY_TYPE");
    // fileDiagram.toolManager.textEditingTool.doStart();

I have also swapped doStart and doActivate between attempts 2 and 3. Thanks in advance for your help.

Here’s an example:

Hmmm. It appears the code you are referencing is this:

myDiagram.addDiagramListener("ExternalObjectsDropped", function(e) {
    // stop any ongoing text editing
    if (myDiagram.currentTool instanceof go.TextEditingTool) {
    // expand any "macros"
    // start editing the first node that was dropped after ungrouping
    var tb = myDiagram.selection.first().findObject('TEXT');
    if (tb) myDiagram.commandHandler.editTextBlock(tb);

However, I can’t seem to get that to work. I have tried changing the listener to PartCreated, but that doesn’t call anything. In previous attempts, I have tried this:

var tb = node.findObject("ENTITY_TYPE");
if (tb) fileDiagram.commandHandler.editTextBlock(tb);

…but this seems to freeze the screen.

The code in the initial question is called using a function. I have several things call this function (i.e. context-menu, keyboard shortcuts, double-clicking the background) which creates the node. This is how I set this up for when the diagram is created.

doubleClick: function(e) { createNode(e.documentPoint); },

Any other thoughts?

Calling CommandHandler.editTextBlock is what you want to do. Perhaps try calling it in a setTimeout function.

How bizarre. That is exactly what I am doing. I tried adding setTimeout and it still freezes the browser when the timeout is issued.

And the user can edit the text of an existing node without any problems? Odd. I don’t know what could cause that. What’s running when it “freezes”?

Yes. I just ran it with the debug file and this is the error I get:

go-debug.js:12 Uncaught Error: CommandHandler.editTextBlock value is not an instance of TextBlock: Shape(Rectangle)#1154
at v (go-debug.js:12)
at Ba (go-debug.js:13)
at w (go-debug.js:12)
at LocalStorageCommandHandler.Sk.editTextBlock (go-debug.js:882)
at createNode (main.js:10037)
at doubleClick (main.js:10759)
at zf (go-debug.js:309)
at qh.vf.standardMouseClick (go-debug.js:307)
at qh.clickSelectingTool.standardMouseClick (main.js:10755)
at qh.doMouseUp (go-debug.js:485)

Ah, well, that’s the problem then.

So, I am not quite sure how to fix this. Do I need to pass the commandhandler in the function or something?

findObject("ENTITY_TYPE") isn’t returning a TextBlock – it’s returning a Shape.

Awww…okay. That worked. Such a simple mistake. Thank you!