markmap-view 0.14.5-alpha.18 → 0.14.5-alpha.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/index.js +97 -31
- package/dist/index.js +116 -57
- package/package.json +12 -11
- package/types/view.d.ts +1 -1
- package/dist/index.mjs +0 -1218
package/dist/index.mjs
DELETED
|
@@ -1,1218 +0,0 @@
|
|
|
1
|
-
/*! markmap-view v0.14.5-alpha.18+44c9639 | MIT License */
|
|
2
|
-
import { Hook, getId, addClass, walkTree, childSelector, noop } from 'markmap-common';
|
|
3
|
-
export { loadCSS, loadJS } from 'markmap-common';
|
|
4
|
-
import * as d3 from 'd3';
|
|
5
|
-
|
|
6
|
-
function _extends() {
|
|
7
|
-
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
8
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
9
|
-
var source = arguments[i];
|
|
10
|
-
for (var key in source) {
|
|
11
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
12
|
-
target[key] = source[key];
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
return target;
|
|
17
|
-
};
|
|
18
|
-
return _extends.apply(this, arguments);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function count(node) {
|
|
22
|
-
var sum = 0,
|
|
23
|
-
children = node.children,
|
|
24
|
-
i = children && children.length;
|
|
25
|
-
if (!i) sum = 1;else while (--i >= 0) sum += children[i].value;
|
|
26
|
-
node.value = sum;
|
|
27
|
-
}
|
|
28
|
-
function node_count () {
|
|
29
|
-
return this.eachAfter(count);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function node_each (callback) {
|
|
33
|
-
var node = this,
|
|
34
|
-
current,
|
|
35
|
-
next = [node],
|
|
36
|
-
children,
|
|
37
|
-
i,
|
|
38
|
-
n;
|
|
39
|
-
do {
|
|
40
|
-
current = next.reverse(), next = [];
|
|
41
|
-
while (node = current.pop()) {
|
|
42
|
-
callback(node), children = node.children;
|
|
43
|
-
if (children) for (i = 0, n = children.length; i < n; ++i) {
|
|
44
|
-
next.push(children[i]);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
} while (next.length);
|
|
48
|
-
return this;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function node_eachBefore (callback) {
|
|
52
|
-
var node = this,
|
|
53
|
-
nodes = [node],
|
|
54
|
-
children,
|
|
55
|
-
i;
|
|
56
|
-
while (node = nodes.pop()) {
|
|
57
|
-
callback(node), children = node.children;
|
|
58
|
-
if (children) for (i = children.length - 1; i >= 0; --i) {
|
|
59
|
-
nodes.push(children[i]);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return this;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function node_eachAfter (callback) {
|
|
66
|
-
var node = this,
|
|
67
|
-
nodes = [node],
|
|
68
|
-
next = [],
|
|
69
|
-
children,
|
|
70
|
-
i,
|
|
71
|
-
n;
|
|
72
|
-
while (node = nodes.pop()) {
|
|
73
|
-
next.push(node), children = node.children;
|
|
74
|
-
if (children) for (i = 0, n = children.length; i < n; ++i) {
|
|
75
|
-
nodes.push(children[i]);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
while (node = next.pop()) {
|
|
79
|
-
callback(node);
|
|
80
|
-
}
|
|
81
|
-
return this;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function node_sum (value) {
|
|
85
|
-
return this.eachAfter(function (node) {
|
|
86
|
-
var sum = +value(node.data) || 0,
|
|
87
|
-
children = node.children,
|
|
88
|
-
i = children && children.length;
|
|
89
|
-
while (--i >= 0) sum += children[i].value;
|
|
90
|
-
node.value = sum;
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function node_sort (compare) {
|
|
95
|
-
return this.eachBefore(function (node) {
|
|
96
|
-
if (node.children) {
|
|
97
|
-
node.children.sort(compare);
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function node_path (end) {
|
|
103
|
-
var start = this,
|
|
104
|
-
ancestor = leastCommonAncestor(start, end),
|
|
105
|
-
nodes = [start];
|
|
106
|
-
while (start !== ancestor) {
|
|
107
|
-
start = start.parent;
|
|
108
|
-
nodes.push(start);
|
|
109
|
-
}
|
|
110
|
-
var k = nodes.length;
|
|
111
|
-
while (end !== ancestor) {
|
|
112
|
-
nodes.splice(k, 0, end);
|
|
113
|
-
end = end.parent;
|
|
114
|
-
}
|
|
115
|
-
return nodes;
|
|
116
|
-
}
|
|
117
|
-
function leastCommonAncestor(a, b) {
|
|
118
|
-
if (a === b) return a;
|
|
119
|
-
var aNodes = a.ancestors(),
|
|
120
|
-
bNodes = b.ancestors(),
|
|
121
|
-
c = null;
|
|
122
|
-
a = aNodes.pop();
|
|
123
|
-
b = bNodes.pop();
|
|
124
|
-
while (a === b) {
|
|
125
|
-
c = a;
|
|
126
|
-
a = aNodes.pop();
|
|
127
|
-
b = bNodes.pop();
|
|
128
|
-
}
|
|
129
|
-
return c;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function node_ancestors () {
|
|
133
|
-
var node = this,
|
|
134
|
-
nodes = [node];
|
|
135
|
-
while (node = node.parent) {
|
|
136
|
-
nodes.push(node);
|
|
137
|
-
}
|
|
138
|
-
return nodes;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
function node_descendants () {
|
|
142
|
-
var nodes = [];
|
|
143
|
-
this.each(function (node) {
|
|
144
|
-
nodes.push(node);
|
|
145
|
-
});
|
|
146
|
-
return nodes;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
function node_leaves () {
|
|
150
|
-
var leaves = [];
|
|
151
|
-
this.eachBefore(function (node) {
|
|
152
|
-
if (!node.children) {
|
|
153
|
-
leaves.push(node);
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
return leaves;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function node_links () {
|
|
160
|
-
var root = this,
|
|
161
|
-
links = [];
|
|
162
|
-
root.each(function (node) {
|
|
163
|
-
if (node !== root) {
|
|
164
|
-
// Don’t include the root’s parent, if any.
|
|
165
|
-
links.push({
|
|
166
|
-
source: node.parent,
|
|
167
|
-
target: node
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
return links;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function hierarchy(data, children) {
|
|
175
|
-
var root = new Node$1(data),
|
|
176
|
-
valued = +data.value && (root.value = data.value),
|
|
177
|
-
node,
|
|
178
|
-
nodes = [root],
|
|
179
|
-
child,
|
|
180
|
-
childs,
|
|
181
|
-
i,
|
|
182
|
-
n;
|
|
183
|
-
if (children == null) children = defaultChildren;
|
|
184
|
-
while (node = nodes.pop()) {
|
|
185
|
-
if (valued) node.value = +node.data.value;
|
|
186
|
-
if ((childs = children(node.data)) && (n = childs.length)) {
|
|
187
|
-
node.children = new Array(n);
|
|
188
|
-
for (i = n - 1; i >= 0; --i) {
|
|
189
|
-
nodes.push(child = node.children[i] = new Node$1(childs[i]));
|
|
190
|
-
child.parent = node;
|
|
191
|
-
child.depth = node.depth + 1;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
return root.eachBefore(computeHeight);
|
|
196
|
-
}
|
|
197
|
-
function node_copy() {
|
|
198
|
-
return hierarchy(this).eachBefore(copyData);
|
|
199
|
-
}
|
|
200
|
-
function defaultChildren(d) {
|
|
201
|
-
return d.children;
|
|
202
|
-
}
|
|
203
|
-
function copyData(node) {
|
|
204
|
-
node.data = node.data.data;
|
|
205
|
-
}
|
|
206
|
-
function computeHeight(node) {
|
|
207
|
-
var height = 0;
|
|
208
|
-
do node.height = height; while ((node = node.parent) && node.height < ++height);
|
|
209
|
-
}
|
|
210
|
-
function Node$1(data) {
|
|
211
|
-
this.data = data;
|
|
212
|
-
this.depth = this.height = 0;
|
|
213
|
-
this.parent = null;
|
|
214
|
-
}
|
|
215
|
-
Node$1.prototype = hierarchy.prototype = {
|
|
216
|
-
constructor: Node$1,
|
|
217
|
-
count: node_count,
|
|
218
|
-
each: node_each,
|
|
219
|
-
eachAfter: node_eachAfter,
|
|
220
|
-
eachBefore: node_eachBefore,
|
|
221
|
-
sum: node_sum,
|
|
222
|
-
sort: node_sort,
|
|
223
|
-
path: node_path,
|
|
224
|
-
ancestors: node_ancestors,
|
|
225
|
-
descendants: node_descendants,
|
|
226
|
-
leaves: node_leaves,
|
|
227
|
-
links: node_links,
|
|
228
|
-
copy: node_copy
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
var version = "2.1.1";
|
|
232
|
-
|
|
233
|
-
const defaults = Object.freeze({
|
|
234
|
-
children: data => data.children,
|
|
235
|
-
nodeSize: node => node.data.size,
|
|
236
|
-
spacing: 0
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
// Create a layout function with customizable options. Per D3-style, the
|
|
240
|
-
// options can be set at any time using setter methods. The layout function
|
|
241
|
-
// will compute the tree node positions based on the options in effect at the
|
|
242
|
-
// time it is called.
|
|
243
|
-
function flextree(options) {
|
|
244
|
-
const opts = Object.assign({}, defaults, options);
|
|
245
|
-
function accessor(name) {
|
|
246
|
-
const opt = opts[name];
|
|
247
|
-
return typeof opt === 'function' ? opt : () => opt;
|
|
248
|
-
}
|
|
249
|
-
function layout(tree) {
|
|
250
|
-
const wtree = wrap(getWrapper(), tree, node => node.children);
|
|
251
|
-
wtree.update();
|
|
252
|
-
return wtree.data;
|
|
253
|
-
}
|
|
254
|
-
function getFlexNode() {
|
|
255
|
-
const nodeSize = accessor('nodeSize');
|
|
256
|
-
const spacing = accessor('spacing');
|
|
257
|
-
return class FlexNode extends hierarchy.prototype.constructor {
|
|
258
|
-
constructor(data) {
|
|
259
|
-
super(data);
|
|
260
|
-
}
|
|
261
|
-
copy() {
|
|
262
|
-
const c = wrap(this.constructor, this, node => node.children);
|
|
263
|
-
c.each(node => node.data = node.data.data);
|
|
264
|
-
return c;
|
|
265
|
-
}
|
|
266
|
-
get size() {
|
|
267
|
-
return nodeSize(this);
|
|
268
|
-
}
|
|
269
|
-
spacing(oNode) {
|
|
270
|
-
return spacing(this, oNode);
|
|
271
|
-
}
|
|
272
|
-
get nodes() {
|
|
273
|
-
return this.descendants();
|
|
274
|
-
}
|
|
275
|
-
get xSize() {
|
|
276
|
-
return this.size[0];
|
|
277
|
-
}
|
|
278
|
-
get ySize() {
|
|
279
|
-
return this.size[1];
|
|
280
|
-
}
|
|
281
|
-
get top() {
|
|
282
|
-
return this.y;
|
|
283
|
-
}
|
|
284
|
-
get bottom() {
|
|
285
|
-
return this.y + this.ySize;
|
|
286
|
-
}
|
|
287
|
-
get left() {
|
|
288
|
-
return this.x - this.xSize / 2;
|
|
289
|
-
}
|
|
290
|
-
get right() {
|
|
291
|
-
return this.x + this.xSize / 2;
|
|
292
|
-
}
|
|
293
|
-
get root() {
|
|
294
|
-
const ancs = this.ancestors();
|
|
295
|
-
return ancs[ancs.length - 1];
|
|
296
|
-
}
|
|
297
|
-
get numChildren() {
|
|
298
|
-
return this.hasChildren ? this.children.length : 0;
|
|
299
|
-
}
|
|
300
|
-
get hasChildren() {
|
|
301
|
-
return !this.noChildren;
|
|
302
|
-
}
|
|
303
|
-
get noChildren() {
|
|
304
|
-
return this.children === null;
|
|
305
|
-
}
|
|
306
|
-
get firstChild() {
|
|
307
|
-
return this.hasChildren ? this.children[0] : null;
|
|
308
|
-
}
|
|
309
|
-
get lastChild() {
|
|
310
|
-
return this.hasChildren ? this.children[this.numChildren - 1] : null;
|
|
311
|
-
}
|
|
312
|
-
get extents() {
|
|
313
|
-
return (this.children || []).reduce((acc, kid) => FlexNode.maxExtents(acc, kid.extents), this.nodeExtents);
|
|
314
|
-
}
|
|
315
|
-
get nodeExtents() {
|
|
316
|
-
return {
|
|
317
|
-
top: this.top,
|
|
318
|
-
bottom: this.bottom,
|
|
319
|
-
left: this.left,
|
|
320
|
-
right: this.right
|
|
321
|
-
};
|
|
322
|
-
}
|
|
323
|
-
static maxExtents(e0, e1) {
|
|
324
|
-
return {
|
|
325
|
-
top: Math.min(e0.top, e1.top),
|
|
326
|
-
bottom: Math.max(e0.bottom, e1.bottom),
|
|
327
|
-
left: Math.min(e0.left, e1.left),
|
|
328
|
-
right: Math.max(e0.right, e1.right)
|
|
329
|
-
};
|
|
330
|
-
}
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
function getWrapper() {
|
|
334
|
-
const FlexNode = getFlexNode();
|
|
335
|
-
const nodeSize = accessor('nodeSize');
|
|
336
|
-
const spacing = accessor('spacing');
|
|
337
|
-
return class extends FlexNode {
|
|
338
|
-
constructor(data) {
|
|
339
|
-
super(data);
|
|
340
|
-
Object.assign(this, {
|
|
341
|
-
x: 0,
|
|
342
|
-
y: 0,
|
|
343
|
-
relX: 0,
|
|
344
|
-
prelim: 0,
|
|
345
|
-
shift: 0,
|
|
346
|
-
change: 0,
|
|
347
|
-
lExt: this,
|
|
348
|
-
lExtRelX: 0,
|
|
349
|
-
lThr: null,
|
|
350
|
-
rExt: this,
|
|
351
|
-
rExtRelX: 0,
|
|
352
|
-
rThr: null
|
|
353
|
-
});
|
|
354
|
-
}
|
|
355
|
-
get size() {
|
|
356
|
-
return nodeSize(this.data);
|
|
357
|
-
}
|
|
358
|
-
spacing(oNode) {
|
|
359
|
-
return spacing(this.data, oNode.data);
|
|
360
|
-
}
|
|
361
|
-
get x() {
|
|
362
|
-
return this.data.x;
|
|
363
|
-
}
|
|
364
|
-
set x(v) {
|
|
365
|
-
this.data.x = v;
|
|
366
|
-
}
|
|
367
|
-
get y() {
|
|
368
|
-
return this.data.y;
|
|
369
|
-
}
|
|
370
|
-
set y(v) {
|
|
371
|
-
this.data.y = v;
|
|
372
|
-
}
|
|
373
|
-
update() {
|
|
374
|
-
layoutChildren(this);
|
|
375
|
-
resolveX(this);
|
|
376
|
-
return this;
|
|
377
|
-
}
|
|
378
|
-
};
|
|
379
|
-
}
|
|
380
|
-
function wrap(FlexClass, treeData, children) {
|
|
381
|
-
const _wrap = (data, parent) => {
|
|
382
|
-
const node = new FlexClass(data);
|
|
383
|
-
Object.assign(node, {
|
|
384
|
-
parent,
|
|
385
|
-
depth: parent === null ? 0 : parent.depth + 1,
|
|
386
|
-
height: 0,
|
|
387
|
-
length: 1
|
|
388
|
-
});
|
|
389
|
-
const kidsData = children(data) || [];
|
|
390
|
-
node.children = kidsData.length === 0 ? null : kidsData.map(kd => _wrap(kd, node));
|
|
391
|
-
if (node.children) {
|
|
392
|
-
Object.assign(node, node.children.reduce((hl, kid) => ({
|
|
393
|
-
height: Math.max(hl.height, kid.height + 1),
|
|
394
|
-
length: hl.length + kid.length
|
|
395
|
-
}), node));
|
|
396
|
-
}
|
|
397
|
-
return node;
|
|
398
|
-
};
|
|
399
|
-
return _wrap(treeData, null);
|
|
400
|
-
}
|
|
401
|
-
Object.assign(layout, {
|
|
402
|
-
nodeSize(arg) {
|
|
403
|
-
return arguments.length ? (opts.nodeSize = arg, layout) : opts.nodeSize;
|
|
404
|
-
},
|
|
405
|
-
spacing(arg) {
|
|
406
|
-
return arguments.length ? (opts.spacing = arg, layout) : opts.spacing;
|
|
407
|
-
},
|
|
408
|
-
children(arg) {
|
|
409
|
-
return arguments.length ? (opts.children = arg, layout) : opts.children;
|
|
410
|
-
},
|
|
411
|
-
hierarchy(treeData, children) {
|
|
412
|
-
const kids = typeof children === 'undefined' ? opts.children : children;
|
|
413
|
-
return wrap(getFlexNode(), treeData, kids);
|
|
414
|
-
},
|
|
415
|
-
dump(tree) {
|
|
416
|
-
const nodeSize = accessor('nodeSize');
|
|
417
|
-
const _dump = i0 => node => {
|
|
418
|
-
const i1 = i0 + ' ';
|
|
419
|
-
const i2 = i0 + ' ';
|
|
420
|
-
const {
|
|
421
|
-
x,
|
|
422
|
-
y
|
|
423
|
-
} = node;
|
|
424
|
-
const size = nodeSize(node);
|
|
425
|
-
const kids = node.children || [];
|
|
426
|
-
const kdumps = kids.length === 0 ? ' ' : `,${i1}children: [${i2}${kids.map(_dump(i2)).join(i2)}${i1}],${i0}`;
|
|
427
|
-
return `{ size: [${size.join(', ')}],${i1}x: ${x}, y: ${y}${kdumps}},`;
|
|
428
|
-
};
|
|
429
|
-
return _dump('\n')(tree);
|
|
430
|
-
}
|
|
431
|
-
});
|
|
432
|
-
return layout;
|
|
433
|
-
}
|
|
434
|
-
flextree.version = version;
|
|
435
|
-
const layoutChildren = (w, y = 0) => {
|
|
436
|
-
w.y = y;
|
|
437
|
-
(w.children || []).reduce((acc, kid) => {
|
|
438
|
-
const [i, lastLows] = acc;
|
|
439
|
-
layoutChildren(kid, w.y + w.ySize);
|
|
440
|
-
// The lowest vertical coordinate while extreme nodes still point
|
|
441
|
-
// in current subtree.
|
|
442
|
-
const lowY = (i === 0 ? kid.lExt : kid.rExt).bottom;
|
|
443
|
-
if (i !== 0) separate(w, i, lastLows);
|
|
444
|
-
const lows = updateLows(lowY, i, lastLows);
|
|
445
|
-
return [i + 1, lows];
|
|
446
|
-
}, [0, null]);
|
|
447
|
-
shiftChange(w);
|
|
448
|
-
positionRoot(w);
|
|
449
|
-
return w;
|
|
450
|
-
};
|
|
451
|
-
|
|
452
|
-
// Resolves the relative coordinate properties - relX and prelim --
|
|
453
|
-
// to set the final, absolute x coordinate for each node. This also sets
|
|
454
|
-
// `prelim` to 0, so that `relX` for each node is its x-coordinate relative
|
|
455
|
-
// to its parent.
|
|
456
|
-
const resolveX = (w, prevSum, parentX) => {
|
|
457
|
-
// A call to resolveX without arguments is assumed to be for the root of
|
|
458
|
-
// the tree. This will set the root's x-coord to zero.
|
|
459
|
-
if (typeof prevSum === 'undefined') {
|
|
460
|
-
prevSum = -w.relX - w.prelim;
|
|
461
|
-
parentX = 0;
|
|
462
|
-
}
|
|
463
|
-
const sum = prevSum + w.relX;
|
|
464
|
-
w.relX = sum + w.prelim - parentX;
|
|
465
|
-
w.prelim = 0;
|
|
466
|
-
w.x = parentX + w.relX;
|
|
467
|
-
(w.children || []).forEach(k => resolveX(k, sum, w.x));
|
|
468
|
-
return w;
|
|
469
|
-
};
|
|
470
|
-
|
|
471
|
-
// Process shift and change for all children, to add intermediate spacing to
|
|
472
|
-
// each child's modifier.
|
|
473
|
-
const shiftChange = w => {
|
|
474
|
-
(w.children || []).reduce((acc, child) => {
|
|
475
|
-
const [lastShiftSum, lastChangeSum] = acc;
|
|
476
|
-
const shiftSum = lastShiftSum + child.shift;
|
|
477
|
-
const changeSum = lastChangeSum + shiftSum + child.change;
|
|
478
|
-
child.relX += changeSum;
|
|
479
|
-
return [shiftSum, changeSum];
|
|
480
|
-
}, [0, 0]);
|
|
481
|
-
};
|
|
482
|
-
|
|
483
|
-
// Separates the latest child from its previous sibling
|
|
484
|
-
/* eslint-disable complexity */
|
|
485
|
-
const separate = (w, i, lows) => {
|
|
486
|
-
const lSib = w.children[i - 1];
|
|
487
|
-
const curSubtree = w.children[i];
|
|
488
|
-
let rContour = lSib;
|
|
489
|
-
let rSumMods = lSib.relX;
|
|
490
|
-
let lContour = curSubtree;
|
|
491
|
-
let lSumMods = curSubtree.relX;
|
|
492
|
-
let isFirst = true;
|
|
493
|
-
while (rContour && lContour) {
|
|
494
|
-
if (rContour.bottom > lows.lowY) lows = lows.next;
|
|
495
|
-
// How far to the left of the right side of rContour is the left side
|
|
496
|
-
// of lContour? First compute the center-to-center distance, then add
|
|
497
|
-
// the "spacing"
|
|
498
|
-
const dist = rSumMods + rContour.prelim - (lSumMods + lContour.prelim) + rContour.xSize / 2 + lContour.xSize / 2 + rContour.spacing(lContour);
|
|
499
|
-
if (dist > 0 || dist < 0 && isFirst) {
|
|
500
|
-
lSumMods += dist;
|
|
501
|
-
// Move subtree by changing relX.
|
|
502
|
-
moveSubtree(curSubtree, dist);
|
|
503
|
-
distributeExtra(w, i, lows.index, dist);
|
|
504
|
-
}
|
|
505
|
-
isFirst = false;
|
|
506
|
-
// Advance highest node(s) and sum(s) of modifiers
|
|
507
|
-
const rightBottom = rContour.bottom;
|
|
508
|
-
const leftBottom = lContour.bottom;
|
|
509
|
-
if (rightBottom <= leftBottom) {
|
|
510
|
-
rContour = nextRContour(rContour);
|
|
511
|
-
if (rContour) rSumMods += rContour.relX;
|
|
512
|
-
}
|
|
513
|
-
if (rightBottom >= leftBottom) {
|
|
514
|
-
lContour = nextLContour(lContour);
|
|
515
|
-
if (lContour) lSumMods += lContour.relX;
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
// Set threads and update extreme nodes. In the first case, the
|
|
519
|
-
// current subtree is taller than the left siblings.
|
|
520
|
-
if (!rContour && lContour) setLThr(w, i, lContour, lSumMods);
|
|
521
|
-
// In the next case, the left siblings are taller than the current subtree
|
|
522
|
-
else if (rContour && !lContour) setRThr(w, i, rContour, rSumMods);
|
|
523
|
-
};
|
|
524
|
-
/* eslint-enable complexity */
|
|
525
|
-
|
|
526
|
-
// Move subtree by changing relX.
|
|
527
|
-
const moveSubtree = (subtree, distance) => {
|
|
528
|
-
subtree.relX += distance;
|
|
529
|
-
subtree.lExtRelX += distance;
|
|
530
|
-
subtree.rExtRelX += distance;
|
|
531
|
-
};
|
|
532
|
-
const distributeExtra = (w, curSubtreeI, leftSibI, dist) => {
|
|
533
|
-
const curSubtree = w.children[curSubtreeI];
|
|
534
|
-
const n = curSubtreeI - leftSibI;
|
|
535
|
-
// Are there intermediate children?
|
|
536
|
-
if (n > 1) {
|
|
537
|
-
const delta = dist / n;
|
|
538
|
-
w.children[leftSibI + 1].shift += delta;
|
|
539
|
-
curSubtree.shift -= delta;
|
|
540
|
-
curSubtree.change -= dist - delta;
|
|
541
|
-
}
|
|
542
|
-
};
|
|
543
|
-
const nextLContour = w => {
|
|
544
|
-
return w.hasChildren ? w.firstChild : w.lThr;
|
|
545
|
-
};
|
|
546
|
-
const nextRContour = w => {
|
|
547
|
-
return w.hasChildren ? w.lastChild : w.rThr;
|
|
548
|
-
};
|
|
549
|
-
const setLThr = (w, i, lContour, lSumMods) => {
|
|
550
|
-
const firstChild = w.firstChild;
|
|
551
|
-
const lExt = firstChild.lExt;
|
|
552
|
-
const curSubtree = w.children[i];
|
|
553
|
-
lExt.lThr = lContour;
|
|
554
|
-
// Change relX so that the sum of modifier after following thread is correct.
|
|
555
|
-
const diff = lSumMods - lContour.relX - firstChild.lExtRelX;
|
|
556
|
-
lExt.relX += diff;
|
|
557
|
-
// Change preliminary x coordinate so that the node does not move.
|
|
558
|
-
lExt.prelim -= diff;
|
|
559
|
-
// Update extreme node and its sum of modifiers.
|
|
560
|
-
firstChild.lExt = curSubtree.lExt;
|
|
561
|
-
firstChild.lExtRelX = curSubtree.lExtRelX;
|
|
562
|
-
};
|
|
563
|
-
|
|
564
|
-
// Mirror image of setLThr.
|
|
565
|
-
const setRThr = (w, i, rContour, rSumMods) => {
|
|
566
|
-
const curSubtree = w.children[i];
|
|
567
|
-
const rExt = curSubtree.rExt;
|
|
568
|
-
const lSib = w.children[i - 1];
|
|
569
|
-
rExt.rThr = rContour;
|
|
570
|
-
const diff = rSumMods - rContour.relX - curSubtree.rExtRelX;
|
|
571
|
-
rExt.relX += diff;
|
|
572
|
-
rExt.prelim -= diff;
|
|
573
|
-
curSubtree.rExt = lSib.rExt;
|
|
574
|
-
curSubtree.rExtRelX = lSib.rExtRelX;
|
|
575
|
-
};
|
|
576
|
-
|
|
577
|
-
// Position root between children, taking into account their modifiers
|
|
578
|
-
const positionRoot = w => {
|
|
579
|
-
if (w.hasChildren) {
|
|
580
|
-
const k0 = w.firstChild;
|
|
581
|
-
const kf = w.lastChild;
|
|
582
|
-
const prelim = (k0.prelim + k0.relX - k0.xSize / 2 + kf.relX + kf.prelim + kf.xSize / 2) / 2;
|
|
583
|
-
Object.assign(w, {
|
|
584
|
-
prelim,
|
|
585
|
-
lExt: k0.lExt,
|
|
586
|
-
lExtRelX: k0.lExtRelX,
|
|
587
|
-
rExt: kf.rExt,
|
|
588
|
-
rExtRelX: kf.rExtRelX
|
|
589
|
-
});
|
|
590
|
-
}
|
|
591
|
-
};
|
|
592
|
-
|
|
593
|
-
// Make/maintain a linked list of the indexes of left siblings and their
|
|
594
|
-
// lowest vertical coordinate.
|
|
595
|
-
const updateLows = (lowY, index, lastLows) => {
|
|
596
|
-
// Remove siblings that are hidden by the new subtree.
|
|
597
|
-
while (lastLows !== null && lowY >= lastLows.lowY) lastLows = lastLows.next;
|
|
598
|
-
// Prepend the new subtree.
|
|
599
|
-
return {
|
|
600
|
-
lowY,
|
|
601
|
-
index,
|
|
602
|
-
next: lastLows
|
|
603
|
-
};
|
|
604
|
-
};
|
|
605
|
-
|
|
606
|
-
/*! @gera2ld/jsx-dom v2.2.2 | ISC License */
|
|
607
|
-
const VTYPE_ELEMENT = 1;
|
|
608
|
-
const VTYPE_FUNCTION = 2;
|
|
609
|
-
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
610
|
-
const XLINK_NS = 'http://www.w3.org/1999/xlink';
|
|
611
|
-
const NS_ATTRS = {
|
|
612
|
-
show: XLINK_NS,
|
|
613
|
-
actuate: XLINK_NS,
|
|
614
|
-
href: XLINK_NS
|
|
615
|
-
};
|
|
616
|
-
const isLeaf = c => typeof c === 'string' || typeof c === 'number';
|
|
617
|
-
const isElement = c => (c == null ? void 0 : c.vtype) === VTYPE_ELEMENT;
|
|
618
|
-
const isRenderFunction = c => (c == null ? void 0 : c.vtype) === VTYPE_FUNCTION;
|
|
619
|
-
function jsx(type, props) {
|
|
620
|
-
let vtype;
|
|
621
|
-
if (typeof type === 'string') vtype = VTYPE_ELEMENT;else if (typeof type === 'function') vtype = VTYPE_FUNCTION;else throw new Error('Invalid VNode type');
|
|
622
|
-
return {
|
|
623
|
-
vtype,
|
|
624
|
-
type,
|
|
625
|
-
props
|
|
626
|
-
};
|
|
627
|
-
}
|
|
628
|
-
function Fragment(props) {
|
|
629
|
-
return props.children;
|
|
630
|
-
}
|
|
631
|
-
const DEFAULT_ENV = {
|
|
632
|
-
isSvg: false
|
|
633
|
-
};
|
|
634
|
-
function insertDom(parent, nodes) {
|
|
635
|
-
if (!Array.isArray(nodes)) nodes = [nodes];
|
|
636
|
-
nodes = nodes.filter(Boolean);
|
|
637
|
-
if (nodes.length) parent.append(...nodes);
|
|
638
|
-
}
|
|
639
|
-
function mountAttributes(domElement, props, env) {
|
|
640
|
-
for (const key in props) {
|
|
641
|
-
if (key === 'key' || key === 'children' || key === 'ref') continue;
|
|
642
|
-
if (key === 'dangerouslySetInnerHTML') {
|
|
643
|
-
domElement.innerHTML = props[key].__html;
|
|
644
|
-
} else if (key === 'innerHTML' || key === 'textContent' || key === 'innerText' || key === 'value' && ['textarea', 'select'].includes(domElement.tagName)) {
|
|
645
|
-
const value = props[key];
|
|
646
|
-
if (value != null) domElement[key] = value;
|
|
647
|
-
} else if (key.startsWith('on')) {
|
|
648
|
-
domElement[key.toLowerCase()] = props[key];
|
|
649
|
-
} else {
|
|
650
|
-
setDOMAttribute(domElement, key, props[key], env.isSvg);
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
const attrMap = {
|
|
655
|
-
className: 'class',
|
|
656
|
-
labelFor: 'for'
|
|
657
|
-
};
|
|
658
|
-
function setDOMAttribute(el, attr, value, isSVG) {
|
|
659
|
-
attr = attrMap[attr] || attr;
|
|
660
|
-
if (value === true) {
|
|
661
|
-
el.setAttribute(attr, '');
|
|
662
|
-
} else if (value === false) {
|
|
663
|
-
el.removeAttribute(attr);
|
|
664
|
-
} else {
|
|
665
|
-
const namespace = isSVG ? NS_ATTRS[attr] : undefined;
|
|
666
|
-
if (namespace !== undefined) {
|
|
667
|
-
el.setAttributeNS(namespace, attr, value);
|
|
668
|
-
} else {
|
|
669
|
-
el.setAttribute(attr, value);
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
function flatten(arr) {
|
|
674
|
-
return arr.reduce((prev, item) => prev.concat(item), []);
|
|
675
|
-
}
|
|
676
|
-
function mountChildren(children, env) {
|
|
677
|
-
return Array.isArray(children) ? flatten(children.map(child => mountChildren(child, env))) : mount(children, env);
|
|
678
|
-
}
|
|
679
|
-
function mount(vnode, env = DEFAULT_ENV) {
|
|
680
|
-
if (vnode == null || typeof vnode === 'boolean') {
|
|
681
|
-
return null;
|
|
682
|
-
}
|
|
683
|
-
if (vnode instanceof Node) {
|
|
684
|
-
return vnode;
|
|
685
|
-
}
|
|
686
|
-
if (isRenderFunction(vnode)) {
|
|
687
|
-
const {
|
|
688
|
-
type,
|
|
689
|
-
props
|
|
690
|
-
} = vnode;
|
|
691
|
-
if (type === Fragment) {
|
|
692
|
-
const node = document.createDocumentFragment();
|
|
693
|
-
if (props.children) {
|
|
694
|
-
const children = mountChildren(props.children, env);
|
|
695
|
-
insertDom(node, children);
|
|
696
|
-
}
|
|
697
|
-
return node;
|
|
698
|
-
}
|
|
699
|
-
const childVNode = type(props);
|
|
700
|
-
return mount(childVNode, env);
|
|
701
|
-
}
|
|
702
|
-
if (isLeaf(vnode)) {
|
|
703
|
-
return document.createTextNode(`${vnode}`);
|
|
704
|
-
}
|
|
705
|
-
if (isElement(vnode)) {
|
|
706
|
-
let node;
|
|
707
|
-
const {
|
|
708
|
-
type,
|
|
709
|
-
props
|
|
710
|
-
} = vnode;
|
|
711
|
-
if (!env.isSvg && type === 'svg') {
|
|
712
|
-
env = Object.assign({}, env, {
|
|
713
|
-
isSvg: true
|
|
714
|
-
});
|
|
715
|
-
}
|
|
716
|
-
if (!env.isSvg) {
|
|
717
|
-
node = document.createElement(type);
|
|
718
|
-
} else {
|
|
719
|
-
node = document.createElementNS(SVG_NS, type);
|
|
720
|
-
}
|
|
721
|
-
mountAttributes(node, props, env);
|
|
722
|
-
if (props.children) {
|
|
723
|
-
let childEnv = env;
|
|
724
|
-
if (env.isSvg && type === 'foreignObject') {
|
|
725
|
-
childEnv = Object.assign({}, childEnv, {
|
|
726
|
-
isSvg: false
|
|
727
|
-
});
|
|
728
|
-
}
|
|
729
|
-
const children = mountChildren(props.children, childEnv);
|
|
730
|
-
if (children != null) insertDom(node, children);
|
|
731
|
-
}
|
|
732
|
-
const {
|
|
733
|
-
ref
|
|
734
|
-
} = props;
|
|
735
|
-
if (typeof ref === 'function') ref(node);
|
|
736
|
-
return node;
|
|
737
|
-
}
|
|
738
|
-
throw new Error('mount: Invalid Vnode!');
|
|
739
|
-
}
|
|
740
|
-
|
|
741
|
-
/**
|
|
742
|
-
* Mount vdom as real DOM nodes.
|
|
743
|
-
*/
|
|
744
|
-
function mountDom(vnode) {
|
|
745
|
-
return mount(vnode);
|
|
746
|
-
}
|
|
747
|
-
|
|
748
|
-
var css_248z$1 = ".markmap{font:300 16px/20px sans-serif}.markmap-link{fill:none}.markmap-node>circle{cursor:pointer}.markmap-foreign{display:inline-block}.markmap-foreign a{color:#0097e6}.markmap-foreign a:hover{color:#00a8ff}.markmap-foreign code{background-color:#f0f0f0;border-radius:2px;color:#555;font-size:calc(1em - 2px)}.markmap-foreign :not(pre)>code{padding:.2em .4em}.markmap-foreign del{text-decoration:line-through}.markmap-foreign em{font-style:italic}.markmap-foreign strong{font-weight:700}.markmap-foreign mark{background:#ffeaa7}.markmap-foreign pre,.markmap-foreign pre[class*=language-]{margin:0;padding:.2em .4em}";
|
|
749
|
-
|
|
750
|
-
var css_248z = ".markmap-container{height:0;left:-100px;overflow:hidden;position:absolute;top:-100px;width:0}.markmap-container>.markmap-foreign{display:inline-block}.markmap-container>.markmap-foreign>div:last-child{white-space:nowrap}";
|
|
751
|
-
|
|
752
|
-
const globalCSS = css_248z$1;
|
|
753
|
-
function linkWidth(nodeData) {
|
|
754
|
-
const data = nodeData.data;
|
|
755
|
-
return Math.max(4 - 2 * data.depth, 1.5);
|
|
756
|
-
}
|
|
757
|
-
function minBy(numbers, by) {
|
|
758
|
-
const index = d3.minIndex(numbers, by);
|
|
759
|
-
return numbers[index];
|
|
760
|
-
}
|
|
761
|
-
function stopPropagation(e) {
|
|
762
|
-
e.stopPropagation();
|
|
763
|
-
}
|
|
764
|
-
function createViewHooks() {
|
|
765
|
-
return {
|
|
766
|
-
transformHtml: new Hook()
|
|
767
|
-
};
|
|
768
|
-
}
|
|
769
|
-
|
|
770
|
-
/**
|
|
771
|
-
* A global hook to refresh all markmaps when called.
|
|
772
|
-
*/
|
|
773
|
-
const refreshHook = new Hook();
|
|
774
|
-
const defaultColorFn = d3.scaleOrdinal(d3.schemeCategory10);
|
|
775
|
-
const isMacintosh = typeof navigator !== 'undefined' && navigator.userAgent.includes('Macintosh');
|
|
776
|
-
class Markmap {
|
|
777
|
-
constructor(svg, opts) {
|
|
778
|
-
this.options = Markmap.defaultOptions;
|
|
779
|
-
this.revokers = [];
|
|
780
|
-
this.handleZoom = e => {
|
|
781
|
-
const {
|
|
782
|
-
transform
|
|
783
|
-
} = e;
|
|
784
|
-
this.g.attr('transform', transform);
|
|
785
|
-
};
|
|
786
|
-
this.handlePan = e => {
|
|
787
|
-
e.preventDefault();
|
|
788
|
-
const transform = d3.zoomTransform(this.svg.node());
|
|
789
|
-
const newTransform = transform.translate(-e.deltaX / transform.k, -e.deltaY / transform.k);
|
|
790
|
-
this.svg.call(this.zoom.transform, newTransform);
|
|
791
|
-
};
|
|
792
|
-
this.handleClick = (e, d) => {
|
|
793
|
-
let recursive = this.options.toggleRecursively;
|
|
794
|
-
if (e.ctrlKey) recursive = !recursive;
|
|
795
|
-
this.toggleNode(d.data, recursive);
|
|
796
|
-
};
|
|
797
|
-
this.viewHooks = createViewHooks();
|
|
798
|
-
this.svg = svg.datum ? svg : d3.select(svg);
|
|
799
|
-
this.styleNode = this.svg.append('style');
|
|
800
|
-
this.zoom = d3.zoom().filter(event => {
|
|
801
|
-
if (this.options.scrollForPan) {
|
|
802
|
-
// Pan with wheels, zoom with ctrl+wheels
|
|
803
|
-
if (event.type === 'wheel') return event.ctrlKey && !event.button;
|
|
804
|
-
}
|
|
805
|
-
return (!event.ctrlKey || event.type === 'wheel') && !event.button;
|
|
806
|
-
}).on('zoom', this.handleZoom);
|
|
807
|
-
this.setOptions(opts);
|
|
808
|
-
this.state = {
|
|
809
|
-
id: this.options.id || this.svg.attr('id') || getId(),
|
|
810
|
-
minX: 0,
|
|
811
|
-
maxX: 0,
|
|
812
|
-
minY: 0,
|
|
813
|
-
maxY: 0
|
|
814
|
-
};
|
|
815
|
-
this.g = this.svg.append('g');
|
|
816
|
-
this.revokers.push(refreshHook.tap(() => {
|
|
817
|
-
this.setData();
|
|
818
|
-
}));
|
|
819
|
-
}
|
|
820
|
-
getStyleContent() {
|
|
821
|
-
const {
|
|
822
|
-
style
|
|
823
|
-
} = this.options;
|
|
824
|
-
const {
|
|
825
|
-
id
|
|
826
|
-
} = this.state;
|
|
827
|
-
const styleText = typeof style === 'function' ? style(id) : '';
|
|
828
|
-
return [this.options.embedGlobalCSS && css_248z$1, styleText].filter(Boolean).join('\n');
|
|
829
|
-
}
|
|
830
|
-
updateStyle() {
|
|
831
|
-
this.svg.attr('class', addClass(this.svg.attr('class'), 'markmap', this.state.id));
|
|
832
|
-
const style = this.getStyleContent();
|
|
833
|
-
this.styleNode.text(style);
|
|
834
|
-
}
|
|
835
|
-
toggleNode(data, recursive = false) {
|
|
836
|
-
var _data$payload;
|
|
837
|
-
const fold = (_data$payload = data.payload) != null && _data$payload.fold ? 0 : 1;
|
|
838
|
-
if (recursive) {
|
|
839
|
-
// recursively
|
|
840
|
-
walkTree(data, (item, next) => {
|
|
841
|
-
item.payload = _extends({}, item.payload, {
|
|
842
|
-
fold
|
|
843
|
-
});
|
|
844
|
-
next();
|
|
845
|
-
});
|
|
846
|
-
} else {
|
|
847
|
-
var _data$payload2;
|
|
848
|
-
data.payload = _extends({}, data.payload, {
|
|
849
|
-
fold: (_data$payload2 = data.payload) != null && _data$payload2.fold ? 0 : 1
|
|
850
|
-
});
|
|
851
|
-
}
|
|
852
|
-
this.renderData(data);
|
|
853
|
-
}
|
|
854
|
-
initializeData(node) {
|
|
855
|
-
let nodeId = 0;
|
|
856
|
-
const {
|
|
857
|
-
color,
|
|
858
|
-
nodeMinHeight,
|
|
859
|
-
maxWidth,
|
|
860
|
-
initialExpandLevel
|
|
861
|
-
} = this.options;
|
|
862
|
-
const {
|
|
863
|
-
id
|
|
864
|
-
} = this.state;
|
|
865
|
-
const container = mountDom(jsx("div", {
|
|
866
|
-
className: `markmap-container markmap ${id}-g`
|
|
867
|
-
}));
|
|
868
|
-
const style = mountDom(jsx("style", {
|
|
869
|
-
children: [this.getStyleContent(), css_248z].join('\n')
|
|
870
|
-
}));
|
|
871
|
-
document.body.append(container, style);
|
|
872
|
-
const groupStyle = maxWidth ? `max-width: ${maxWidth}px` : '';
|
|
873
|
-
let foldRecursively = 0;
|
|
874
|
-
walkTree(node, (item, next, parent) => {
|
|
875
|
-
var _item$children, _parent$state, _item$payload;
|
|
876
|
-
item.children = (_item$children = item.children) == null ? void 0 : _item$children.map(child => _extends({}, child));
|
|
877
|
-
nodeId += 1;
|
|
878
|
-
const group = mountDom(jsx("div", {
|
|
879
|
-
className: "markmap-foreign",
|
|
880
|
-
style: groupStyle,
|
|
881
|
-
children: jsx("div", {
|
|
882
|
-
dangerouslySetInnerHTML: {
|
|
883
|
-
__html: item.content
|
|
884
|
-
}
|
|
885
|
-
})
|
|
886
|
-
}));
|
|
887
|
-
container.append(group);
|
|
888
|
-
item.state = _extends({}, item.state, {
|
|
889
|
-
id: nodeId,
|
|
890
|
-
el: group.firstChild
|
|
891
|
-
});
|
|
892
|
-
item.state.path = [parent == null || (_parent$state = parent.state) == null ? void 0 : _parent$state.path, item.state.id].filter(Boolean).join('.');
|
|
893
|
-
color(item); // preload colors
|
|
894
|
-
|
|
895
|
-
const isFoldRecursively = ((_item$payload = item.payload) == null ? void 0 : _item$payload.fold) === 2;
|
|
896
|
-
if (isFoldRecursively) {
|
|
897
|
-
foldRecursively += 1;
|
|
898
|
-
} else if (foldRecursively || initialExpandLevel >= 0 && item.depth >= initialExpandLevel) {
|
|
899
|
-
item.payload = _extends({}, item.payload, {
|
|
900
|
-
fold: 1
|
|
901
|
-
});
|
|
902
|
-
}
|
|
903
|
-
next();
|
|
904
|
-
if (isFoldRecursively) foldRecursively -= 1;
|
|
905
|
-
});
|
|
906
|
-
const nodes = Array.from(container.childNodes).map(group => group.firstChild);
|
|
907
|
-
this.viewHooks.transformHtml.call(this, nodes);
|
|
908
|
-
// Clone the rendered HTML and set `white-space: nowrap` to it to detect its max-width.
|
|
909
|
-
// The parent node will have a width of the max-width and the original content without
|
|
910
|
-
// `white-space: nowrap` gets re-layouted, then we will get the expected layout, with
|
|
911
|
-
// content in one line as much as possible, and subjecting to the given max-width.
|
|
912
|
-
nodes.forEach(node => {
|
|
913
|
-
var _node$parentNode;
|
|
914
|
-
(_node$parentNode = node.parentNode) == null ? void 0 : _node$parentNode.append(node.cloneNode(true));
|
|
915
|
-
});
|
|
916
|
-
walkTree(node, (item, next, parent) => {
|
|
917
|
-
var _parent$state2;
|
|
918
|
-
const state = item.state;
|
|
919
|
-
const rect = state.el.getBoundingClientRect();
|
|
920
|
-
item.content = state.el.innerHTML;
|
|
921
|
-
state.size = [Math.ceil(rect.width) + 1, Math.max(Math.ceil(rect.height), nodeMinHeight)];
|
|
922
|
-
state.key = [parent == null || (_parent$state2 = parent.state) == null ? void 0 : _parent$state2.id, state.id].filter(Boolean).join('.') +
|
|
923
|
-
// FIXME: find a way to check content hash
|
|
924
|
-
item.content;
|
|
925
|
-
next();
|
|
926
|
-
});
|
|
927
|
-
container.remove();
|
|
928
|
-
style.remove();
|
|
929
|
-
}
|
|
930
|
-
setOptions(opts) {
|
|
931
|
-
this.options = _extends({}, this.options, opts);
|
|
932
|
-
if (this.options.zoom) {
|
|
933
|
-
this.svg.call(this.zoom);
|
|
934
|
-
} else {
|
|
935
|
-
this.svg.on('.zoom', null);
|
|
936
|
-
}
|
|
937
|
-
if (this.options.pan) {
|
|
938
|
-
this.svg.on('wheel', this.handlePan);
|
|
939
|
-
} else {
|
|
940
|
-
this.svg.on('wheel', null);
|
|
941
|
-
}
|
|
942
|
-
}
|
|
943
|
-
setData(data, opts) {
|
|
944
|
-
if (opts) this.setOptions(opts);
|
|
945
|
-
if (data) this.state.data = data;
|
|
946
|
-
if (!this.state.data) return;
|
|
947
|
-
this.initializeData(this.state.data);
|
|
948
|
-
this.updateStyle();
|
|
949
|
-
this.renderData();
|
|
950
|
-
}
|
|
951
|
-
renderData(originData) {
|
|
952
|
-
var _origin$data$state$x, _origin$data$state$y;
|
|
953
|
-
if (!this.state.data) return;
|
|
954
|
-
const {
|
|
955
|
-
spacingHorizontal,
|
|
956
|
-
paddingX,
|
|
957
|
-
spacingVertical,
|
|
958
|
-
autoFit,
|
|
959
|
-
color
|
|
960
|
-
} = this.options;
|
|
961
|
-
const layout = flextree({}).children(d => {
|
|
962
|
-
var _d$payload;
|
|
963
|
-
if (!((_d$payload = d.payload) != null && _d$payload.fold)) return d.children;
|
|
964
|
-
}).nodeSize(node => {
|
|
965
|
-
const [width, height] = node.data.state.size;
|
|
966
|
-
return [height, width + (width ? paddingX * 2 : 0) + spacingHorizontal];
|
|
967
|
-
}).spacing((a, b) => {
|
|
968
|
-
return a.parent === b.parent ? spacingVertical : spacingVertical * 2;
|
|
969
|
-
});
|
|
970
|
-
const tree = layout.hierarchy(this.state.data);
|
|
971
|
-
layout(tree);
|
|
972
|
-
const descendants = tree.descendants().reverse();
|
|
973
|
-
const links = tree.links();
|
|
974
|
-
const linkShape = d3.linkHorizontal();
|
|
975
|
-
const minX = d3.min(descendants, d => d.x - d.xSize / 2);
|
|
976
|
-
const maxX = d3.max(descendants, d => d.x + d.xSize / 2);
|
|
977
|
-
const minY = d3.min(descendants, d => d.y);
|
|
978
|
-
const maxY = d3.max(descendants, d => d.y + d.ySize - spacingHorizontal);
|
|
979
|
-
Object.assign(this.state, {
|
|
980
|
-
minX,
|
|
981
|
-
maxX,
|
|
982
|
-
minY,
|
|
983
|
-
maxY
|
|
984
|
-
});
|
|
985
|
-
if (autoFit) this.fit();
|
|
986
|
-
const origin = originData && descendants.find(item => item.data === originData) || tree;
|
|
987
|
-
const x0 = (_origin$data$state$x = origin.data.state.x0) != null ? _origin$data$state$x : origin.x;
|
|
988
|
-
const y0 = (_origin$data$state$y = origin.data.state.y0) != null ? _origin$data$state$y : origin.y;
|
|
989
|
-
|
|
990
|
-
// Update the nodes
|
|
991
|
-
const node = this.g.selectAll(childSelector('g')).data(descendants, d => d.data.state.key);
|
|
992
|
-
const nodeEnter = node.enter().append('g').attr('data-depth', d => d.data.depth).attr('data-path', d => d.data.state.path).attr('transform', d => `translate(${y0 + origin.ySize - d.ySize},${x0 + origin.xSize / 2 - d.xSize})`);
|
|
993
|
-
const nodeExit = this.transition(node.exit());
|
|
994
|
-
nodeExit.select('line').attr('x1', d => d.ySize - spacingHorizontal).attr('x2', d => d.ySize - spacingHorizontal);
|
|
995
|
-
nodeExit.select('foreignObject').style('opacity', 0);
|
|
996
|
-
nodeExit.attr('transform', d => `translate(${origin.y + origin.ySize - d.ySize},${origin.x + origin.xSize / 2 - d.xSize})`).remove();
|
|
997
|
-
const nodeMerge = node.merge(nodeEnter).attr('class', d => {
|
|
998
|
-
var _d$data$payload;
|
|
999
|
-
return ['markmap-node', ((_d$data$payload = d.data.payload) == null ? void 0 : _d$data$payload.fold) && 'markmap-fold'].filter(Boolean).join(' ');
|
|
1000
|
-
});
|
|
1001
|
-
this.transition(nodeMerge).attr('transform', d => `translate(${d.y},${d.x - d.xSize / 2})`);
|
|
1002
|
-
|
|
1003
|
-
// Update lines under the content
|
|
1004
|
-
const line = nodeMerge.selectAll(childSelector('line')).data(d => [d], d => d.data.state.key).join(enter => {
|
|
1005
|
-
return enter.append('line').attr('x1', d => d.ySize - spacingHorizontal).attr('x2', d => d.ySize - spacingHorizontal);
|
|
1006
|
-
}, update => update, exit => exit.remove());
|
|
1007
|
-
this.transition(line).attr('x1', -1).attr('x2', d => d.ySize - spacingHorizontal + 2).attr('y1', d => d.xSize).attr('y2', d => d.xSize).attr('stroke', d => color(d.data)).attr('stroke-width', linkWidth);
|
|
1008
|
-
|
|
1009
|
-
// Circle to link to children of the node
|
|
1010
|
-
const circle = nodeMerge.selectAll(childSelector('circle')).data(d => {
|
|
1011
|
-
var _d$data$children;
|
|
1012
|
-
return (_d$data$children = d.data.children) != null && _d$data$children.length ? [d] : [];
|
|
1013
|
-
}, d => d.data.state.key).join(enter => {
|
|
1014
|
-
return enter.append('circle').attr('stroke-width', '1.5').attr('cx', d => d.ySize - spacingHorizontal).attr('cy', d => d.xSize).attr('r', 0).on('click', (e, d) => this.handleClick(e, d)).on('mousedown', stopPropagation);
|
|
1015
|
-
}, update => update, exit => exit.remove());
|
|
1016
|
-
this.transition(circle).attr('r', 6).attr('cx', d => d.ySize - spacingHorizontal).attr('cy', d => d.xSize).attr('stroke', d => color(d.data)).attr('fill', d => {
|
|
1017
|
-
var _d$data$payload2;
|
|
1018
|
-
return (_d$data$payload2 = d.data.payload) != null && _d$data$payload2.fold && d.data.children ? color(d.data) : '#fff';
|
|
1019
|
-
});
|
|
1020
|
-
const foreignObject = nodeMerge.selectAll(childSelector('foreignObject')).data(d => [d], d => d.data.state.key).join(enter => {
|
|
1021
|
-
const fo = enter.append('foreignObject').attr('class', 'markmap-foreign').attr('x', paddingX).attr('y', 0).style('opacity', 0).on('mousedown', stopPropagation).on('dblclick', stopPropagation);
|
|
1022
|
-
fo.append('xhtml:div').select(function select(d) {
|
|
1023
|
-
const clone = d.data.state.el.cloneNode(true);
|
|
1024
|
-
this.replaceWith(clone);
|
|
1025
|
-
return clone;
|
|
1026
|
-
}).attr('xmlns', 'http://www.w3.org/1999/xhtml');
|
|
1027
|
-
return fo;
|
|
1028
|
-
}, update => update, exit => exit.remove()).attr('width', d => Math.max(0, d.ySize - spacingHorizontal - paddingX * 2)).attr('height', d => d.xSize);
|
|
1029
|
-
this.transition(foreignObject).style('opacity', 1);
|
|
1030
|
-
|
|
1031
|
-
// Update the links
|
|
1032
|
-
const path = this.g.selectAll(childSelector('path')).data(links, d => d.target.data.state.key).join(enter => {
|
|
1033
|
-
const source = [y0 + origin.ySize - spacingHorizontal, x0 + origin.xSize / 2];
|
|
1034
|
-
return enter.insert('path', 'g').attr('class', 'markmap-link').attr('data-depth', d => d.target.data.depth).attr('data-path', d => d.target.data.state.path).attr('d', linkShape({
|
|
1035
|
-
source,
|
|
1036
|
-
target: source
|
|
1037
|
-
}));
|
|
1038
|
-
}, update => update, exit => {
|
|
1039
|
-
const source = [origin.y + origin.ySize - spacingHorizontal, origin.x + origin.xSize / 2];
|
|
1040
|
-
return this.transition(exit).attr('d', linkShape({
|
|
1041
|
-
source,
|
|
1042
|
-
target: source
|
|
1043
|
-
})).remove();
|
|
1044
|
-
});
|
|
1045
|
-
this.transition(path).attr('stroke', d => color(d.target.data)).attr('stroke-width', d => linkWidth(d.target)).attr('d', d => {
|
|
1046
|
-
const origSource = d.source;
|
|
1047
|
-
const origTarget = d.target;
|
|
1048
|
-
const source = [origSource.y + origSource.ySize - spacingHorizontal, origSource.x + origSource.xSize / 2];
|
|
1049
|
-
const target = [origTarget.y, origTarget.x + origTarget.xSize / 2];
|
|
1050
|
-
return linkShape({
|
|
1051
|
-
source,
|
|
1052
|
-
target
|
|
1053
|
-
});
|
|
1054
|
-
});
|
|
1055
|
-
descendants.forEach(d => {
|
|
1056
|
-
d.data.state.x0 = d.x;
|
|
1057
|
-
d.data.state.y0 = d.y;
|
|
1058
|
-
});
|
|
1059
|
-
}
|
|
1060
|
-
transition(sel) {
|
|
1061
|
-
const {
|
|
1062
|
-
duration
|
|
1063
|
-
} = this.options;
|
|
1064
|
-
return sel.transition().duration(duration);
|
|
1065
|
-
}
|
|
1066
|
-
|
|
1067
|
-
/**
|
|
1068
|
-
* Fit the content to the viewport.
|
|
1069
|
-
*/
|
|
1070
|
-
async fit() {
|
|
1071
|
-
const svgNode = this.svg.node();
|
|
1072
|
-
const {
|
|
1073
|
-
width: offsetWidth,
|
|
1074
|
-
height: offsetHeight
|
|
1075
|
-
} = svgNode.getBoundingClientRect();
|
|
1076
|
-
const {
|
|
1077
|
-
fitRatio
|
|
1078
|
-
} = this.options;
|
|
1079
|
-
const {
|
|
1080
|
-
minX,
|
|
1081
|
-
maxX,
|
|
1082
|
-
minY,
|
|
1083
|
-
maxY
|
|
1084
|
-
} = this.state;
|
|
1085
|
-
const naturalWidth = maxY - minY;
|
|
1086
|
-
const naturalHeight = maxX - minX;
|
|
1087
|
-
const scale = Math.min(offsetWidth / naturalWidth * fitRatio, offsetHeight / naturalHeight * fitRatio, 2);
|
|
1088
|
-
const initialZoom = d3.zoomIdentity.translate((offsetWidth - naturalWidth * scale) / 2 - minY * scale, (offsetHeight - naturalHeight * scale) / 2 - minX * scale).scale(scale);
|
|
1089
|
-
return this.transition(this.svg).call(this.zoom.transform, initialZoom).end().catch(noop);
|
|
1090
|
-
}
|
|
1091
|
-
|
|
1092
|
-
/**
|
|
1093
|
-
* Pan the content to make the provided node visible in the viewport.
|
|
1094
|
-
*/
|
|
1095
|
-
async ensureView(node, padding) {
|
|
1096
|
-
let itemData;
|
|
1097
|
-
this.g.selectAll(childSelector('g')).each(function walk(d) {
|
|
1098
|
-
if (d.data === node) {
|
|
1099
|
-
itemData = d;
|
|
1100
|
-
}
|
|
1101
|
-
});
|
|
1102
|
-
if (!itemData) return;
|
|
1103
|
-
const svgNode = this.svg.node();
|
|
1104
|
-
const {
|
|
1105
|
-
spacingHorizontal
|
|
1106
|
-
} = this.options;
|
|
1107
|
-
const relRect = svgNode.getBoundingClientRect();
|
|
1108
|
-
const transform = d3.zoomTransform(svgNode);
|
|
1109
|
-
const [left, right] = [itemData.y, itemData.y + itemData.ySize - spacingHorizontal + 2].map(x => x * transform.k + transform.x);
|
|
1110
|
-
const [top, bottom] = [itemData.x - itemData.xSize / 2, itemData.x + itemData.xSize / 2].map(y => y * transform.k + transform.y);
|
|
1111
|
-
// Skip if the node includes or is included in the container.
|
|
1112
|
-
const pd = _extends({
|
|
1113
|
-
left: 0,
|
|
1114
|
-
right: 0,
|
|
1115
|
-
top: 0,
|
|
1116
|
-
bottom: 0
|
|
1117
|
-
}, padding);
|
|
1118
|
-
const dxs = [pd.left - left, relRect.width - pd.right - right];
|
|
1119
|
-
const dys = [pd.top - top, relRect.height - pd.bottom - bottom];
|
|
1120
|
-
const dx = dxs[0] * dxs[1] > 0 ? minBy(dxs, Math.abs) / transform.k : 0;
|
|
1121
|
-
const dy = dys[0] * dys[1] > 0 ? minBy(dys, Math.abs) / transform.k : 0;
|
|
1122
|
-
if (dx || dy) {
|
|
1123
|
-
const newTransform = transform.translate(dx, dy);
|
|
1124
|
-
return this.transition(this.svg).call(this.zoom.transform, newTransform).end().catch(noop);
|
|
1125
|
-
}
|
|
1126
|
-
}
|
|
1127
|
-
|
|
1128
|
-
/**
|
|
1129
|
-
* Scale content with it pinned at the center of the viewport.
|
|
1130
|
-
*/
|
|
1131
|
-
async rescale(scale) {
|
|
1132
|
-
const svgNode = this.svg.node();
|
|
1133
|
-
const {
|
|
1134
|
-
width: offsetWidth,
|
|
1135
|
-
height: offsetHeight
|
|
1136
|
-
} = svgNode.getBoundingClientRect();
|
|
1137
|
-
const halfWidth = offsetWidth / 2;
|
|
1138
|
-
const halfHeight = offsetHeight / 2;
|
|
1139
|
-
const transform = d3.zoomTransform(svgNode);
|
|
1140
|
-
const newTransform = transform.translate((halfWidth - transform.x) * (1 - scale) / transform.k, (halfHeight - transform.y) * (1 - scale) / transform.k).scale(scale);
|
|
1141
|
-
return this.transition(this.svg).call(this.zoom.transform, newTransform).end().catch(noop);
|
|
1142
|
-
}
|
|
1143
|
-
destroy() {
|
|
1144
|
-
this.svg.on('.zoom', null);
|
|
1145
|
-
this.svg.html(null);
|
|
1146
|
-
this.revokers.forEach(fn => {
|
|
1147
|
-
fn();
|
|
1148
|
-
});
|
|
1149
|
-
}
|
|
1150
|
-
static create(svg, opts, data = null) {
|
|
1151
|
-
const mm = new Markmap(svg, opts);
|
|
1152
|
-
if (data) {
|
|
1153
|
-
mm.setData(data);
|
|
1154
|
-
mm.fit(); // always fit for the first render
|
|
1155
|
-
}
|
|
1156
|
-
|
|
1157
|
-
return mm;
|
|
1158
|
-
}
|
|
1159
|
-
}
|
|
1160
|
-
Markmap.defaultOptions = {
|
|
1161
|
-
autoFit: false,
|
|
1162
|
-
color: node => {
|
|
1163
|
-
var _node$state;
|
|
1164
|
-
return defaultColorFn(`${((_node$state = node.state) == null ? void 0 : _node$state.path) || ''}`);
|
|
1165
|
-
},
|
|
1166
|
-
duration: 500,
|
|
1167
|
-
embedGlobalCSS: true,
|
|
1168
|
-
fitRatio: 0.95,
|
|
1169
|
-
maxWidth: 0,
|
|
1170
|
-
nodeMinHeight: 16,
|
|
1171
|
-
paddingX: 8,
|
|
1172
|
-
scrollForPan: isMacintosh,
|
|
1173
|
-
spacingHorizontal: 80,
|
|
1174
|
-
spacingVertical: 5,
|
|
1175
|
-
initialExpandLevel: -1,
|
|
1176
|
-
zoom: true,
|
|
1177
|
-
pan: true,
|
|
1178
|
-
toggleRecursively: false
|
|
1179
|
-
};
|
|
1180
|
-
function deriveOptions(jsonOptions) {
|
|
1181
|
-
const derivedOptions = {};
|
|
1182
|
-
const options = _extends({}, jsonOptions);
|
|
1183
|
-
const {
|
|
1184
|
-
color,
|
|
1185
|
-
colorFreezeLevel
|
|
1186
|
-
} = options;
|
|
1187
|
-
if ((color == null ? void 0 : color.length) === 1) {
|
|
1188
|
-
const solidColor = color[0];
|
|
1189
|
-
derivedOptions.color = () => solidColor;
|
|
1190
|
-
} else if (color != null && color.length) {
|
|
1191
|
-
const colorFn = d3.scaleOrdinal(color);
|
|
1192
|
-
derivedOptions.color = node => colorFn(`${node.state.path}`);
|
|
1193
|
-
}
|
|
1194
|
-
if (colorFreezeLevel) {
|
|
1195
|
-
const color = derivedOptions.color || Markmap.defaultOptions.color;
|
|
1196
|
-
derivedOptions.color = node => {
|
|
1197
|
-
node = _extends({}, node, {
|
|
1198
|
-
state: _extends({}, node.state, {
|
|
1199
|
-
path: node.state.path.split('.').slice(0, colorFreezeLevel).join('.')
|
|
1200
|
-
})
|
|
1201
|
-
});
|
|
1202
|
-
return color(node);
|
|
1203
|
-
};
|
|
1204
|
-
}
|
|
1205
|
-
const numberKeys = ['duration', 'maxWidth', 'initialExpandLevel'];
|
|
1206
|
-
numberKeys.forEach(key => {
|
|
1207
|
-
const value = options[key];
|
|
1208
|
-
if (typeof value === 'number') derivedOptions[key] = value;
|
|
1209
|
-
});
|
|
1210
|
-
const booleanKeys = ['zoom', 'pan'];
|
|
1211
|
-
booleanKeys.forEach(key => {
|
|
1212
|
-
const value = options[key];
|
|
1213
|
-
if (value != null) derivedOptions[key] = !!value;
|
|
1214
|
-
});
|
|
1215
|
-
return derivedOptions;
|
|
1216
|
-
}
|
|
1217
|
-
|
|
1218
|
-
export { Markmap, defaultColorFn, deriveOptions, globalCSS, refreshHook };
|