Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 103 additions & 21 deletions src/jsmind.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,40 @@ export default class jsMind {
}
return this.mind.get_node(node);
}
/**
* Add node data to the mind map without triggering UI refresh.
* @private
* @param {import('./jsmind.node.js').Node} parent_node
* @param {string} node_id
* @param {string} topic
* @param {Record<string, any>=} data
* @param {('left'|'center'|'right'|'-1'|'0'|'1'|number)=} direction
* @returns {import('./jsmind.node.js').Node|null}
*/
_add_node_data(parent_node, node_id, topic, data, direction) {
var dir = Direction.of(direction);
if (dir === undefined) {
dir = this.layout.calculate_next_child_direction(parent_node);
}
var node = this.mind.add_node(parent_node, node_id, topic, data, dir);
if (!!node) {
this.view.add_node(node);
this.view.reset_node_custom_style(node);
}
return node;
}

/**
* Refresh UI after node changes.
* @private
* @param {import('./jsmind.node.js').Node} parent_node
*/
_refresh_node_ui(parent_node) {
this.layout.layout();
this.view.show(false);
this.expand_node(parent_node);
}

/**
* Add a new node to the mind map.
* @param {string | import('./jsmind.node.js').Node} parent_node
Expand All @@ -424,30 +458,78 @@ export default class jsMind {
* @returns {import('./jsmind.node.js').Node|null}
*/
add_node(parent_node, node_id, topic, data, direction) {
if (this.get_editable()) {
var the_parent_node = this.get_node(parent_node);
var dir = Direction.of(direction);
if (dir === undefined) {
dir = this.layout.calculate_next_child_direction(the_parent_node);
}
var node = this.mind.add_node(the_parent_node, node_id, topic, data, dir);
if (!!node) {
this.view.add_node(node);
this.layout.layout();
this.view.show(false);
this.view.reset_node_custom_style(node);
this.expand_node(the_parent_node);
this.invoke_event_handle(EventType.edit, {
evt: 'add_node',
data: [the_parent_node.id, node_id, topic, data, dir],
node: node_id,
});
}
return node;
} else {
if (!this.get_editable()) {
logger.error('fail, this mind map is not editable');
return null;
}

var the_parent_node = this.get_node(parent_node);
if (!the_parent_node) {
logger.error('parent node not found');
return null;
}

var node = this._add_node_data(the_parent_node, node_id, topic, data, direction);
if (!!node) {
this._refresh_node_ui(the_parent_node);
this.invoke_event_handle(EventType.edit, {
evt: 'add_node',
data: [the_parent_node.id, node_id, topic, data, Direction.of(direction)],
node: node_id,
});
}
return node;
}

/**
* Add multiple nodes to the mind map with optimized performance.
Comment thread
UmbraCi marked this conversation as resolved.
* @param {string | import('./jsmind.node.js').Node} parent_node - Parent node for all new nodes
* @param {Array<{node_id: string, topic: string, data?: Record<string, any>, direction?: ('left'|'center'|'right'|'-1'|'0'|'1'|number)}>} nodes_data - Array of node data objects
* @returns {Array<import('./jsmind.node.js').Node|null>} Array of created nodes
*/
add_nodes(parent_node, nodes_data) {
Comment thread
UmbraCi marked this conversation as resolved.
if (!this.get_editable()) {
logger.error('fail, this mind map is not editable');
return [];
}

var the_parent_node = this.get_node(parent_node);
if (!the_parent_node) {
logger.error('parent node not found');
return [];
}

if (!Array.isArray(nodes_data) || nodes_data.length === 0) {
logger.warn('nodes_data should be a non-empty array');
return [];
}

var created_nodes = [];

// Batch create node data without triggering UI refresh
for (var i = 0; i < nodes_data.length; i++) {
var node_data = nodes_data[i];
var node = this._add_node_data(
the_parent_node,
node_data.node_id,
node_data.topic,
node_data.data,
node_data.direction
);
created_nodes.push(node);
}

// Refresh UI once after all nodes are added
if (!!created_nodes.length) {
this._refresh_node_ui(the_parent_node);
this.invoke_event_handle(EventType.edit, {
evt: 'add_nodes',
data: [the_parent_node.id, nodes_data],
nodes: created_nodes.filter(node => node !== null).map(node => node.id),
});
}

return created_nodes;
}
/**
* Insert a node before target node.
Expand Down
Loading