Skip to content

Commit

Permalink
[Bugfix] support folded field in freemind (#577)
Browse files Browse the repository at this point in the history
* support folded field in freemind

* fix ut
  • Loading branch information
hizzgdev authored Feb 25, 2024
1 parent 30e671c commit 52e7a37
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
27 changes: 19 additions & 8 deletions src/jsmind.format.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ export const format = {
xml_lines.push('<map version="1.0.1">');
df._build_map(mind.root, xml_lines);
xml_lines.push('</map>');
json.data = xml_lines.join(' ');
json.data = xml_lines.join('');
return json;
},

Expand Down Expand Up @@ -355,6 +355,7 @@ export const format = {
var df = format.freemind;
var node_id = xml_node.getAttribute('ID');
var node_topic = xml_node.getAttribute('TEXT');
var node_folded = xml_node.getAttribute('FOLDED');
// look for richcontent
if (node_topic == null) {
var topic_children = xml_node.childNodes;
Expand All @@ -368,7 +369,8 @@ export const format = {
}
}
var node_data = df._load_attributes(xml_node);
var node_expanded = 'expanded' in node_data ? node_data.expanded == 'true' : true;
var node_expanded =
'expanded' in node_data ? node_data.expanded == 'true' : node_folded != 'true';
delete node_data.expanded;

var node_position = xml_node.getAttribute('POSITION');
Expand Down Expand Up @@ -419,14 +421,14 @@ export const format = {
pos = node.direction === Direction.left ? 'left' : 'right';
}
xml_lines.push('<node');
xml_lines.push('ID="' + node.id + '"');
xml_lines.push(' ID="' + node.id + '"');
if (!!pos) {
xml_lines.push('POSITION="' + pos + '"');
xml_lines.push(' POSITION="' + pos + '"');
}
xml_lines.push('TEXT="' + node.topic + '">');

// store expanded status as an attribute
xml_lines.push('<attribute NAME="expanded" VALUE="' + node.expanded + '"/>');
if (!node.expanded) {
xml_lines.push(' FOLDED="true"');
}
xml_lines.push(' TEXT="' + df._escape(node.topic) + '">');

// for attributes
var node_data = node.data;
Expand All @@ -444,6 +446,15 @@ export const format = {

xml_lines.push('</node>');
},

_escape: function (text) {
return text
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/'/g, '&apos;')
.replace(/"/g, '&quot;');
},
},
text: {
example: {
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/jsmind.format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function checkMindData(mind) {
expect(node1.id).toBe('easy');
expect(node1.topic).toBe('Easy');
expect(node1.direction).toBe(Direction.left);
expect(node1.expanded).toBe(false);
expect(node1.children.length).toBe(1);

const node2 = node1.children[0];
Expand Down Expand Up @@ -86,6 +87,7 @@ const fakeMindMaps = {
id: 'easy',
topic: 'Easy',
direction: 'left',
expanded: false,
children: [{ id: 'easy1', topic: 'Easy to show', ext: 'addition data' }],
},
],
Expand All @@ -96,14 +98,14 @@ const fakeMindMaps = {
format: 'node_array',
data: [
{ id: 'root', topic: 'jsMind', isroot: true },
{ id: 'easy', topic: 'Easy', parentid: 'root', direction: 'left' },
{ id: 'easy', topic: 'Easy', parentid: 'root', direction: 'left', expanded: false },
{ id: 'easy1', topic: 'Easy to show', parentid: 'easy', ext: 'addition data' },
],
},
freemind: {
meta: { name: 'test jsmind', author: 'hizzgdev', version: 'version' },
format: 'freemind',
data: '<map version="1.0.1"> <node ID="root" TEXT="jsMind"> <attribute NAME="expanded" VALUE="true"/> <node ID="easy" POSITION="left" TEXT="Easy"> <attribute NAME="expanded" VALUE="true"/> <node ID="easy1" TEXT="Easy to show"> <attribute NAME="expanded" VALUE="true"/> <attribute NAME="ext" VALUE="addition data"/> </node> </node> </node> </map>',
data: '<map version="1.0.1"><node ID="root" TEXT="jsMind"><node ID="easy" POSITION="left" FOLDED="true" TEXT="Easy"><node ID="easy1" TEXT="Easy to show"><attribute NAME="ext" VALUE="addition data"/></node></node></node></map>',
},
text: {
meta: { name: 'test jsmind', author: 'hizzgdev', version: 'version' },
Expand Down

0 comments on commit 52e7a37

Please sign in to comment.