@@ -1131,25 +1131,36 @@ define(function (require, exports, module) {
11311131 // In the meantime, pass null for node so new item is placed
11321132 // relative to the selection
11331133 node = selection ;
1134-
1135- // Open the node before creating the new child
1136- _projectTree . jstree ( "open_node" , node ) ;
1137-
1138- // Create the node and open the editor
1139- _projectTree . jstree ( "create" , node , position , { data : initialName } , null , skipRename ) ;
1140-
1141- if ( ! skipRename ) {
1142- var $renameInput = _projectTree . find ( ".jstree-rename-input" ) ;
1143-
1144- $renameInput . on ( "keydown" , function ( event ) {
1145- // Listen for escape key on keydown, so we can remove the node in the create.jstree handler above
1146- if ( event . keyCode === KeyEvent . DOM_VK_ESCAPE ) {
1147-
1148- escapeKeyPressed = true ;
1149- }
1150- } ) ;
1134+
1135+ function createNode ( ) {
1136+ // Create the node and open the editor
1137+ _projectTree . jstree ( "create" , node , position , { data : initialName } , null , skipRename ) ;
1138+
1139+ if ( ! skipRename ) {
1140+ var $renameInput = _projectTree . find ( ".jstree-rename-input" ) ;
1141+
1142+ $renameInput . on ( "keydown" , function ( event ) {
1143+ // Listen for escape key on keydown, so we can remove the node in the create.jstree handler above
1144+ if ( event . keyCode === KeyEvent . DOM_VK_ESCAPE ) {
1145+
1146+ escapeKeyPressed = true ;
1147+ }
1148+ } ) ;
1149+
1150+ ViewUtils . scrollElementIntoView ( _projectTree , $renameInput , true ) ;
1151+ }
1152+ }
11511153
1152- ViewUtils . scrollElementIntoView ( _projectTree , $renameInput , true ) ;
1154+ // There is a race condition in jstree if "open_node" and "create" are called in rapid
1155+ // succession and the node was not yet loaded. To avoid it, first open the node and wait
1156+ // for the open_node event before trying to create the new one. See #2085 for more details.
1157+ if ( wasNodeOpen ) {
1158+ createNode ( ) ;
1159+ } else {
1160+ _projectTree . one ( "open_node.jstree" , createNode ) ;
1161+
1162+ // Open the node before creating the new child
1163+ _projectTree . jstree ( "open_node" , node ) ;
11531164 }
11541165
11551166 return result . promise ( ) ;
0 commit comments