Skip to content

Commit b6c0dc0

Browse files
committed
proper newline/br handling
1 parent fca2362 commit b6c0dc0

10 files changed

+39
-23
lines changed

examples/features.mymind

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"root": {
33
"id": "ujfdpxoz",
4-
"text": "My Mind\nFeatures",
4+
"text": "My Mind<br/>Features",
55
"layout": "map",
66
"children": [
77
{

my-mind.js

+19-11
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ MM.Item.prototype.updateSubtree = function(isSubChild) {
510510
}
511511

512512
MM.Item.prototype.setText = function(text) {
513-
this._dom.text.innerHTML = text.replace(/\n/g, "<br/>");
513+
this._dom.text.innerHTML = text;
514514
this._findLinks(this._dom.text);
515515
return this.update();
516516
}
@@ -520,7 +520,7 @@ MM.Item.prototype.getId = function() {
520520
}
521521

522522
MM.Item.prototype.getText = function() {
523-
return this._dom.text.innerHTML.replace(/<br\s*\/?>/g, "\n");
523+
return this._dom.text.innerHTML;
524524
}
525525

526526
MM.Item.prototype.collapse = function() {
@@ -1069,7 +1069,7 @@ MM.Map.prototype.getRoot = function() {
10691069

10701070
MM.Map.prototype.getName = function() {
10711071
var name = this._root.getText();
1072-
return name.replace(/\n/g, " ").replace(/<.*?>/g, "").trim();
1072+
return MM.Format.br2nl(name).replace(/\n/g, " ").replace(/<.*?>/g, "").trim();
10731073
}
10741074

10751075
MM.Map.prototype.getId = function() {
@@ -2790,6 +2790,14 @@ MM.Format.getByMime = function(mime) {
27902790

27912791
MM.Format.to = function(data) {}
27922792
MM.Format.from = function(data) {}
2793+
2794+
MM.Format.nl2br = function(str) {
2795+
return str.replace(/\n/g, "<br/>");
2796+
}
2797+
2798+
MM.Format.br2nl = function(str) {
2799+
return str.replace(/<br\s*\/?>/g, "\n");
2800+
}
27932801
MM.Format.JSON = Object.create(MM.Format, {
27942802
id: {value: "json"},
27952803
label: {value: "Native (JSON)"},
@@ -2798,7 +2806,7 @@ MM.Format.JSON = Object.create(MM.Format, {
27982806
});
27992807

28002808
MM.Format.JSON.to = function(data) {
2801-
return JSON.stringify(data, null, 2) + "\n";
2809+
return JSON.stringify(data, null, "\t") + "\n";
28022810
}
28032811

28042812
MM.Format.JSON.from = function(data) {
@@ -2852,7 +2860,7 @@ MM.Format.FreeMind._serializeItem = function(doc, json) {
28522860

28532861
MM.Format.FreeMind._serializeAttributes = function(doc, json) {
28542862
var elm = doc.createElement("node");
2855-
elm.setAttribute("TEXT", json.text);
2863+
elm.setAttribute("TEXT", MM.Format.br2nl(json.text));
28562864
elm.setAttribute("ID", json.id);
28572865

28582866
if (json.side) { elm.setAttribute("POSITION", json.side); }
@@ -2878,7 +2886,7 @@ MM.Format.FreeMind._parseNode = function(node, parent) {
28782886
MM.Format.FreeMind._parseAttributes = function(node, parent) {
28792887
var json = {
28802888
children: [],
2881-
text: node.getAttribute("TEXT") || "",
2889+
text: MM.Format.nl2br(node.getAttribute("TEXT") || ""),
28822890
id: node.getAttribute("ID")
28832891
};
28842892

@@ -2924,7 +2932,7 @@ MM.Format.MMA = Object.create(MM.Format.FreeMind, {
29242932
MM.Format.MMA._parseAttributes = function(node, parent) {
29252933
var json = {
29262934
children: [],
2927-
text: node.getAttribute("title") || "",
2935+
text: MM.Format.nl2br(node.getAttribute("title") || ""),
29282936
shape: "box"
29292937
};
29302938

@@ -2954,7 +2962,7 @@ MM.Format.MMA._parseAttributes = function(node, parent) {
29542962

29552963
MM.Format.MMA._serializeAttributes = function(doc, json) {
29562964
var elm = doc.createElement("node");
2957-
elm.setAttribute("title", json.text);
2965+
elm.setAttribute("title", MM.Format.br2nl(json.text));
29582966
elm.setAttribute("expand", json.collapsed ? "false" : "true");
29592967

29602968
if (json.side) { elm.setAttribute("direction", json.side == "left" ? "0" : "1"); }
@@ -2993,7 +3001,7 @@ MM.Format.Mup.from = function(data) {
29933001

29943002
MM.Format.Mup._MupToMM = function(item) {
29953003
var json = {
2996-
text: item.title,
3004+
text: MM.Format.nl2br(item.title),
29973005
id: item.id,
29983006
shape: "box"
29993007
}
@@ -3031,7 +3039,7 @@ MM.Format.Mup._MupToMM = function(item) {
30313039
MM.Format.Mup._MMtoMup = function(item, side) {
30323040
var result = {
30333041
id: item.id,
3034-
title: item.text,
3042+
title: MM.Format.br2nl(item.text),
30353043
attr: {}
30363044
}
30373045
if (item.color) {
@@ -3103,7 +3111,7 @@ MM.Format.Plaintext._serializeItem = function(item, depth) {
31033111
}, this);
31043112

31053113
var prefix = new Array(depth+1).join("\t");
3106-
lines.unshift(prefix + item.text.replace(/\n/g, "<br/>"))
3114+
lines.unshift(prefix + item.text.replace(/\n/g, ""));
31073115

31083116
return lines.join("\n") + (depth ? "" : "\n");
31093117
}

src/format.freemind.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ MM.Format.FreeMind._serializeItem = function(doc, json) {
4646

4747
MM.Format.FreeMind._serializeAttributes = function(doc, json) {
4848
var elm = doc.createElement("node");
49-
elm.setAttribute("TEXT", json.text);
49+
elm.setAttribute("TEXT", MM.Format.br2nl(json.text));
5050
elm.setAttribute("ID", json.id);
5151

5252
if (json.side) { elm.setAttribute("POSITION", json.side); }
@@ -72,7 +72,7 @@ MM.Format.FreeMind._parseNode = function(node, parent) {
7272
MM.Format.FreeMind._parseAttributes = function(node, parent) {
7373
var json = {
7474
children: [],
75-
text: node.getAttribute("TEXT") || "",
75+
text: MM.Format.nl2br(node.getAttribute("TEXT") || ""),
7676
id: node.getAttribute("ID")
7777
};
7878

src/format.js

+8
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,11 @@ MM.Format.getByMime = function(mime) {
1616

1717
MM.Format.to = function(data) {}
1818
MM.Format.from = function(data) {}
19+
20+
MM.Format.nl2br = function(str) {
21+
return str.replace(/\n/g, "<br/>");
22+
}
23+
24+
MM.Format.br2nl = function(str) {
25+
return str.replace(/<br\s*\/?>/g, "\n");
26+
}

src/format.json.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ MM.Format.JSON = Object.create(MM.Format, {
66
});
77

88
MM.Format.JSON.to = function(data) {
9-
return JSON.stringify(data, null, 2) + "\n";
9+
return JSON.stringify(data, null, "\t") + "\n";
1010
}
1111

1212
MM.Format.JSON.from = function(data) {

src/format.mma.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ MM.Format.MMA = Object.create(MM.Format.FreeMind, {
77
MM.Format.MMA._parseAttributes = function(node, parent) {
88
var json = {
99
children: [],
10-
text: node.getAttribute("title") || "",
10+
text: MM.Format.nl2br(node.getAttribute("title") || ""),
1111
shape: "box"
1212
};
1313

@@ -37,7 +37,7 @@ MM.Format.MMA._parseAttributes = function(node, parent) {
3737

3838
MM.Format.MMA._serializeAttributes = function(doc, json) {
3939
var elm = doc.createElement("node");
40-
elm.setAttribute("title", json.text);
40+
elm.setAttribute("title", MM.Format.br2nl(json.text));
4141
elm.setAttribute("expand", json.collapsed ? "false" : "true");
4242

4343
if (json.side) { elm.setAttribute("direction", json.side == "left" ? "0" : "1"); }

src/format.mup.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ MM.Format.Mup.from = function(data) {
2323

2424
MM.Format.Mup._MupToMM = function(item) {
2525
var json = {
26-
text: item.title,
26+
text: MM.Format.nl2br(item.title),
2727
id: item.id,
2828
shape: "box"
2929
}
@@ -61,7 +61,7 @@ MM.Format.Mup._MupToMM = function(item) {
6161
MM.Format.Mup._MMtoMup = function(item, side) {
6262
var result = {
6363
id: item.id,
64-
title: item.text,
64+
title: MM.Format.br2nl(item.text),
6565
attr: {}
6666
}
6767
if (item.color) {

src/format.plaintext.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ MM.Format.Plaintext._serializeItem = function(item, depth) {
4444
}, this);
4545

4646
var prefix = new Array(depth+1).join("\t");
47-
lines.unshift(prefix + item.text.replace(/\n/g, "<br/>"))
47+
lines.unshift(prefix + item.text.replace(/\n/g, ""));
4848

4949
return lines.join("\n") + (depth ? "" : "\n");
5050
}

src/item.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ MM.Item.prototype.updateSubtree = function(isSubChild) {
221221
}
222222

223223
MM.Item.prototype.setText = function(text) {
224-
this._dom.text.innerHTML = text.replace(/\n/g, "<br/>");
224+
this._dom.text.innerHTML = text;
225225
this._findLinks(this._dom.text);
226226
return this.update();
227227
}
@@ -231,7 +231,7 @@ MM.Item.prototype.getId = function() {
231231
}
232232

233233
MM.Item.prototype.getText = function() {
234-
return this._dom.text.innerHTML.replace(/<br\s*\/?>/g, "\n");
234+
return this._dom.text.innerHTML;
235235
}
236236

237237
MM.Item.prototype.collapse = function() {

src/map.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ MM.Map.prototype.getRoot = function() {
195195

196196
MM.Map.prototype.getName = function() {
197197
var name = this._root.getText();
198-
return name.replace(/\n/g, " ").replace(/<.*?>/g, "").trim();
198+
return MM.Format.br2nl(name).replace(/\n/g, " ").replace(/<.*?>/g, "").trim();
199199
}
200200

201201
MM.Map.prototype.getId = function() {

0 commit comments

Comments
 (0)