markmap-view-plus 0.0.1
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/README.md +20 -0
- package/dist/action.d.ts +110 -0
- package/dist/browser/index.js +2159 -0
- package/dist/constants.d.ts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +1506 -0
- package/dist/to-markdown.d.ts +34 -0
- package/dist/types.d.ts +73 -0
- package/dist/util.d.ts +7 -0
- package/dist/view.d.ts +94 -0
- package/package.json +54 -0
|
@@ -0,0 +1,2159 @@
|
|
|
1
|
+
(function(exports, d32) {
|
|
2
|
+
"use strict";
|
|
3
|
+
class Hook {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.listeners = [];
|
|
6
|
+
}
|
|
7
|
+
tap(fn) {
|
|
8
|
+
this.listeners.push(fn);
|
|
9
|
+
return () => this.revoke(fn);
|
|
10
|
+
}
|
|
11
|
+
revoke(fn) {
|
|
12
|
+
const i = this.listeners.indexOf(fn);
|
|
13
|
+
if (i >= 0) this.listeners.splice(i, 1);
|
|
14
|
+
}
|
|
15
|
+
revokeAll() {
|
|
16
|
+
this.listeners.splice(0);
|
|
17
|
+
}
|
|
18
|
+
call(...args) {
|
|
19
|
+
for (const fn of this.listeners) {
|
|
20
|
+
fn(...args);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const uniqId = Math.random().toString(36).slice(2, 8);
|
|
25
|
+
let globalIndex = 0;
|
|
26
|
+
function getId() {
|
|
27
|
+
globalIndex += 1;
|
|
28
|
+
return `mm-${uniqId}-${globalIndex}`;
|
|
29
|
+
}
|
|
30
|
+
function noop() {
|
|
31
|
+
}
|
|
32
|
+
function walkTree(tree, callback) {
|
|
33
|
+
const walk = (item, parent) => callback(
|
|
34
|
+
item,
|
|
35
|
+
() => {
|
|
36
|
+
var _a;
|
|
37
|
+
return (_a = item.children) == null ? void 0 : _a.map((child) => walk(child, item));
|
|
38
|
+
},
|
|
39
|
+
parent
|
|
40
|
+
);
|
|
41
|
+
return walk(tree);
|
|
42
|
+
}
|
|
43
|
+
function addClass(className, ...rest) {
|
|
44
|
+
const classList = "".split(" ").filter(Boolean);
|
|
45
|
+
rest.forEach((item) => {
|
|
46
|
+
if (item && classList.indexOf(item) < 0) classList.push(item);
|
|
47
|
+
});
|
|
48
|
+
return classList.join(" ");
|
|
49
|
+
}
|
|
50
|
+
function defer() {
|
|
51
|
+
const obj = {};
|
|
52
|
+
obj.promise = new Promise((resolve, reject) => {
|
|
53
|
+
obj.resolve = resolve;
|
|
54
|
+
obj.reject = reject;
|
|
55
|
+
});
|
|
56
|
+
return obj;
|
|
57
|
+
}
|
|
58
|
+
function memoize(fn) {
|
|
59
|
+
const cache = {};
|
|
60
|
+
return function memoized(...args) {
|
|
61
|
+
const key = `${args[0]}`;
|
|
62
|
+
let data = cache[key];
|
|
63
|
+
if (!data) {
|
|
64
|
+
data = {
|
|
65
|
+
value: fn(...args)
|
|
66
|
+
};
|
|
67
|
+
cache[key] = data;
|
|
68
|
+
}
|
|
69
|
+
return data.value;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function debounce(fn, time) {
|
|
73
|
+
const state = {
|
|
74
|
+
timer: 0
|
|
75
|
+
};
|
|
76
|
+
function reset() {
|
|
77
|
+
if (state.timer) {
|
|
78
|
+
window.clearTimeout(state.timer);
|
|
79
|
+
state.timer = 0;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function run() {
|
|
83
|
+
reset();
|
|
84
|
+
if (state.args) state.result = fn(...state.args);
|
|
85
|
+
}
|
|
86
|
+
return function debounced(...args) {
|
|
87
|
+
reset();
|
|
88
|
+
state.args = args;
|
|
89
|
+
state.timer = window.setTimeout(run, time);
|
|
90
|
+
return state.result;
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/*! @gera2ld/jsx-dom v2.2.2 | ISC License */
|
|
94
|
+
const VTYPE_ELEMENT = 1;
|
|
95
|
+
const VTYPE_FUNCTION = 2;
|
|
96
|
+
const SVG_NS = "http://www.w3.org/2000/svg";
|
|
97
|
+
const XLINK_NS = "http://www.w3.org/1999/xlink";
|
|
98
|
+
const NS_ATTRS = {
|
|
99
|
+
show: XLINK_NS,
|
|
100
|
+
actuate: XLINK_NS,
|
|
101
|
+
href: XLINK_NS
|
|
102
|
+
};
|
|
103
|
+
const isLeaf = (c) => typeof c === "string" || typeof c === "number";
|
|
104
|
+
const isElement = (c) => (c == null ? void 0 : c.vtype) === VTYPE_ELEMENT;
|
|
105
|
+
const isRenderFunction = (c) => (c == null ? void 0 : c.vtype) === VTYPE_FUNCTION;
|
|
106
|
+
function h(type, props, ...children) {
|
|
107
|
+
props = Object.assign({}, props, {
|
|
108
|
+
children: children.length === 1 ? children[0] : children
|
|
109
|
+
});
|
|
110
|
+
return jsx(type, props);
|
|
111
|
+
}
|
|
112
|
+
function jsx(type, props) {
|
|
113
|
+
let vtype;
|
|
114
|
+
if (typeof type === "string") vtype = VTYPE_ELEMENT;
|
|
115
|
+
else if (typeof type === "function") vtype = VTYPE_FUNCTION;
|
|
116
|
+
else throw new Error("Invalid VNode type");
|
|
117
|
+
return {
|
|
118
|
+
vtype,
|
|
119
|
+
type,
|
|
120
|
+
props
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function Fragment(props) {
|
|
124
|
+
return props.children;
|
|
125
|
+
}
|
|
126
|
+
const DEFAULT_ENV = {
|
|
127
|
+
isSvg: false
|
|
128
|
+
};
|
|
129
|
+
function insertDom(parent, nodes) {
|
|
130
|
+
if (!Array.isArray(nodes)) nodes = [nodes];
|
|
131
|
+
nodes = nodes.filter(Boolean);
|
|
132
|
+
if (nodes.length) parent.append(...nodes);
|
|
133
|
+
}
|
|
134
|
+
function mountAttributes(domElement, props, env) {
|
|
135
|
+
for (const key in props) {
|
|
136
|
+
if (key === "key" || key === "children" || key === "ref") continue;
|
|
137
|
+
if (key === "dangerouslySetInnerHTML") {
|
|
138
|
+
domElement.innerHTML = props[key].__html;
|
|
139
|
+
} else if (key === "innerHTML" || key === "textContent" || key === "innerText" || key === "value" && ["textarea", "select"].includes(domElement.tagName)) {
|
|
140
|
+
const value = props[key];
|
|
141
|
+
if (value != null) domElement[key] = value;
|
|
142
|
+
} else if (key.startsWith("on")) {
|
|
143
|
+
domElement[key.toLowerCase()] = props[key];
|
|
144
|
+
} else {
|
|
145
|
+
setDOMAttribute(domElement, key, props[key], env.isSvg);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const attrMap = {
|
|
150
|
+
className: "class",
|
|
151
|
+
labelFor: "for"
|
|
152
|
+
};
|
|
153
|
+
function setDOMAttribute(el, attr, value, isSVG) {
|
|
154
|
+
attr = attrMap[attr] || attr;
|
|
155
|
+
if (value === true) {
|
|
156
|
+
el.setAttribute(attr, "");
|
|
157
|
+
} else if (value === false) {
|
|
158
|
+
el.removeAttribute(attr);
|
|
159
|
+
} else {
|
|
160
|
+
const namespace = isSVG ? NS_ATTRS[attr] : void 0;
|
|
161
|
+
if (namespace !== void 0) {
|
|
162
|
+
el.setAttributeNS(namespace, attr, value);
|
|
163
|
+
} else {
|
|
164
|
+
el.setAttribute(attr, value);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function flatten(arr) {
|
|
169
|
+
return arr.reduce((prev, item) => prev.concat(item), []);
|
|
170
|
+
}
|
|
171
|
+
function mountChildren(children, env) {
|
|
172
|
+
return Array.isArray(children) ? flatten(children.map((child) => mountChildren(child, env))) : mount(children, env);
|
|
173
|
+
}
|
|
174
|
+
function mount(vnode, env = DEFAULT_ENV) {
|
|
175
|
+
if (vnode == null || typeof vnode === "boolean") {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
if (vnode instanceof Node) {
|
|
179
|
+
return vnode;
|
|
180
|
+
}
|
|
181
|
+
if (isRenderFunction(vnode)) {
|
|
182
|
+
const {
|
|
183
|
+
type,
|
|
184
|
+
props
|
|
185
|
+
} = vnode;
|
|
186
|
+
if (type === Fragment) {
|
|
187
|
+
const node = document.createDocumentFragment();
|
|
188
|
+
if (props.children) {
|
|
189
|
+
const children = mountChildren(props.children, env);
|
|
190
|
+
insertDom(node, children);
|
|
191
|
+
}
|
|
192
|
+
return node;
|
|
193
|
+
}
|
|
194
|
+
const childVNode = type(props);
|
|
195
|
+
return mount(childVNode, env);
|
|
196
|
+
}
|
|
197
|
+
if (isLeaf(vnode)) {
|
|
198
|
+
return document.createTextNode(`${vnode}`);
|
|
199
|
+
}
|
|
200
|
+
if (isElement(vnode)) {
|
|
201
|
+
let node;
|
|
202
|
+
const {
|
|
203
|
+
type,
|
|
204
|
+
props
|
|
205
|
+
} = vnode;
|
|
206
|
+
if (!env.isSvg && type === "svg") {
|
|
207
|
+
env = Object.assign({}, env, {
|
|
208
|
+
isSvg: true
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
if (!env.isSvg) {
|
|
212
|
+
node = document.createElement(type);
|
|
213
|
+
} else {
|
|
214
|
+
node = document.createElementNS(SVG_NS, type);
|
|
215
|
+
}
|
|
216
|
+
mountAttributes(node, props, env);
|
|
217
|
+
if (props.children) {
|
|
218
|
+
let childEnv = env;
|
|
219
|
+
if (env.isSvg && type === "foreignObject") {
|
|
220
|
+
childEnv = Object.assign({}, childEnv, {
|
|
221
|
+
isSvg: false
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
const children = mountChildren(props.children, childEnv);
|
|
225
|
+
if (children != null) insertDom(node, children);
|
|
226
|
+
}
|
|
227
|
+
const {
|
|
228
|
+
ref
|
|
229
|
+
} = props;
|
|
230
|
+
if (typeof ref === "function") ref(node);
|
|
231
|
+
return node;
|
|
232
|
+
}
|
|
233
|
+
throw new Error("mount: Invalid Vnode!");
|
|
234
|
+
}
|
|
235
|
+
function mountDom(vnode) {
|
|
236
|
+
return mount(vnode);
|
|
237
|
+
}
|
|
238
|
+
function hm(...args) {
|
|
239
|
+
return mountDom(h(...args));
|
|
240
|
+
}
|
|
241
|
+
const memoizedPreloadJS = memoize((url) => {
|
|
242
|
+
document.head.append(
|
|
243
|
+
hm("link", {
|
|
244
|
+
rel: "preload",
|
|
245
|
+
as: "script",
|
|
246
|
+
href: url
|
|
247
|
+
})
|
|
248
|
+
);
|
|
249
|
+
});
|
|
250
|
+
const jsCache = {};
|
|
251
|
+
const cssCache = {};
|
|
252
|
+
async function loadJSItem(item, context) {
|
|
253
|
+
var _a;
|
|
254
|
+
const src = item.type === "script" && ((_a = item.data) == null ? void 0 : _a.src) || "";
|
|
255
|
+
item.loaded || (item.loaded = jsCache[src]);
|
|
256
|
+
if (!item.loaded) {
|
|
257
|
+
const deferred = defer();
|
|
258
|
+
item.loaded = deferred.promise;
|
|
259
|
+
if (item.type === "script") {
|
|
260
|
+
document.head.append(
|
|
261
|
+
hm("script", {
|
|
262
|
+
...item.data,
|
|
263
|
+
onLoad: () => deferred.resolve(),
|
|
264
|
+
onError: deferred.reject
|
|
265
|
+
})
|
|
266
|
+
);
|
|
267
|
+
if (!src) {
|
|
268
|
+
deferred.resolve();
|
|
269
|
+
} else {
|
|
270
|
+
jsCache[src] = item.loaded;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (item.type === "iife") {
|
|
274
|
+
const { fn, getParams } = item.data;
|
|
275
|
+
fn(...(getParams == null ? void 0 : getParams(context)) || []);
|
|
276
|
+
deferred.resolve();
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
await item.loaded;
|
|
280
|
+
}
|
|
281
|
+
async function loadCSSItem(item) {
|
|
282
|
+
const url = item.type === "stylesheet" && item.data.href || "";
|
|
283
|
+
item.loaded || (item.loaded = cssCache[url]);
|
|
284
|
+
if (!item.loaded) {
|
|
285
|
+
const deferred = defer();
|
|
286
|
+
item.loaded = deferred.promise;
|
|
287
|
+
if (url) cssCache[url] = item.loaded;
|
|
288
|
+
if (item.type === "style") {
|
|
289
|
+
document.head.append(
|
|
290
|
+
hm("style", {
|
|
291
|
+
textContent: item.data
|
|
292
|
+
})
|
|
293
|
+
);
|
|
294
|
+
deferred.resolve();
|
|
295
|
+
} else if (url) {
|
|
296
|
+
document.head.append(
|
|
297
|
+
hm("link", {
|
|
298
|
+
rel: "stylesheet",
|
|
299
|
+
...item.data
|
|
300
|
+
})
|
|
301
|
+
);
|
|
302
|
+
fetch(url).then((res) => {
|
|
303
|
+
if (res.ok) return res.text();
|
|
304
|
+
throw res;
|
|
305
|
+
}).then(() => deferred.resolve(), deferred.reject);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
await item.loaded;
|
|
309
|
+
}
|
|
310
|
+
async function loadJS(items, context) {
|
|
311
|
+
items.forEach((item) => {
|
|
312
|
+
var _a;
|
|
313
|
+
if (item.type === "script" && ((_a = item.data) == null ? void 0 : _a.src)) {
|
|
314
|
+
memoizedPreloadJS(item.data.src);
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
context = {
|
|
318
|
+
getMarkmap: () => window.markmap,
|
|
319
|
+
...context
|
|
320
|
+
};
|
|
321
|
+
for (const item of items) {
|
|
322
|
+
await loadJSItem(item, context);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
async function loadCSS(items) {
|
|
326
|
+
await Promise.all(items.map((item) => loadCSSItem(item)));
|
|
327
|
+
}
|
|
328
|
+
const isMacintosh = typeof navigator !== "undefined" && navigator.userAgent.includes("Macintosh");
|
|
329
|
+
const defaultColorFn = d32.scaleOrdinal(d32.schemeCategory10);
|
|
330
|
+
const lineWidthFactory = (baseWidth = 1, deltaWidth = 3, k = 2) => (node) => baseWidth + deltaWidth / k ** node.state.depth;
|
|
331
|
+
const defaultOptions = {
|
|
332
|
+
autoFit: false,
|
|
333
|
+
duration: 500,
|
|
334
|
+
embedGlobalCSS: true,
|
|
335
|
+
fitRatio: 0.95,
|
|
336
|
+
maxInitialScale: 2,
|
|
337
|
+
scrollForPan: isMacintosh,
|
|
338
|
+
initialExpandLevel: -1,
|
|
339
|
+
zoom: true,
|
|
340
|
+
pan: true,
|
|
341
|
+
toggleRecursively: false,
|
|
342
|
+
editable: true,
|
|
343
|
+
addable: true,
|
|
344
|
+
deletable: true,
|
|
345
|
+
collapseOnHover: true,
|
|
346
|
+
hoverBorder: true,
|
|
347
|
+
clickBorder: true,
|
|
348
|
+
onNodeEdit: void 0,
|
|
349
|
+
onNodeAdd: void 0,
|
|
350
|
+
inputPlaceholder: "Enter text",
|
|
351
|
+
color: (node) => {
|
|
352
|
+
var _a;
|
|
353
|
+
return defaultColorFn(`${((_a = node.state) == null ? void 0 : _a.path) || ""}`);
|
|
354
|
+
},
|
|
355
|
+
lineWidth: lineWidthFactory(),
|
|
356
|
+
maxWidth: 0,
|
|
357
|
+
nodeMinHeight: 16,
|
|
358
|
+
paddingX: 8,
|
|
359
|
+
spacingHorizontal: 80,
|
|
360
|
+
spacingVertical: 5
|
|
361
|
+
};
|
|
362
|
+
function deriveOptions(jsonOptions) {
|
|
363
|
+
const derivedOptions = {};
|
|
364
|
+
const options = { ...jsonOptions };
|
|
365
|
+
const { color, colorFreezeLevel, lineWidth } = options;
|
|
366
|
+
if ((color == null ? void 0 : color.length) === 1) {
|
|
367
|
+
const solidColor = color[0];
|
|
368
|
+
derivedOptions.color = () => solidColor;
|
|
369
|
+
} else if (color == null ? void 0 : color.length) {
|
|
370
|
+
const colorFn = d32.scaleOrdinal(color);
|
|
371
|
+
derivedOptions.color = (node) => colorFn(`${node.state.path}`);
|
|
372
|
+
}
|
|
373
|
+
if (colorFreezeLevel) {
|
|
374
|
+
const color2 = derivedOptions.color || defaultOptions.color;
|
|
375
|
+
derivedOptions.color = (node) => {
|
|
376
|
+
node = {
|
|
377
|
+
...node,
|
|
378
|
+
state: {
|
|
379
|
+
...node.state,
|
|
380
|
+
path: node.state.path.split(".").slice(0, colorFreezeLevel).join(".")
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
return color2(node);
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
if (lineWidth) {
|
|
387
|
+
const args = Array.isArray(lineWidth) ? lineWidth : [lineWidth, 0, 1];
|
|
388
|
+
derivedOptions.lineWidth = lineWidthFactory(
|
|
389
|
+
...args
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
const numberKeys = [
|
|
393
|
+
"duration",
|
|
394
|
+
"fitRatio",
|
|
395
|
+
"initialExpandLevel",
|
|
396
|
+
"maxInitialScale",
|
|
397
|
+
"maxWidth",
|
|
398
|
+
"nodeMinHeight",
|
|
399
|
+
"paddingX",
|
|
400
|
+
"spacingHorizontal",
|
|
401
|
+
"spacingVertical"
|
|
402
|
+
];
|
|
403
|
+
numberKeys.forEach((key) => {
|
|
404
|
+
const value = options[key];
|
|
405
|
+
if (typeof value === "number") derivedOptions[key] = value;
|
|
406
|
+
});
|
|
407
|
+
const booleanKeys = ["zoom", "pan"];
|
|
408
|
+
booleanKeys.forEach((key) => {
|
|
409
|
+
const value = options[key];
|
|
410
|
+
if (value != null) derivedOptions[key] = !!value;
|
|
411
|
+
});
|
|
412
|
+
return derivedOptions;
|
|
413
|
+
}
|
|
414
|
+
function simpleHash(str) {
|
|
415
|
+
let hash = 0;
|
|
416
|
+
for (let i = 0; i < str.length; i++) {
|
|
417
|
+
hash = (hash << 5) - hash + str.charCodeAt(i) | 0;
|
|
418
|
+
}
|
|
419
|
+
return (hash >>> 0).toString(36);
|
|
420
|
+
}
|
|
421
|
+
function childSelector(filter) {
|
|
422
|
+
if (typeof filter === "string") {
|
|
423
|
+
const selector = filter;
|
|
424
|
+
filter = (el) => el.matches(selector);
|
|
425
|
+
}
|
|
426
|
+
const filterFn = filter;
|
|
427
|
+
return function selector() {
|
|
428
|
+
let nodes = Array.from(this.childNodes);
|
|
429
|
+
if (filterFn) nodes = nodes.filter((node) => filterFn(node));
|
|
430
|
+
return nodes;
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
function count(node) {
|
|
434
|
+
var sum = 0, children = node.children, i = children && children.length;
|
|
435
|
+
if (!i) sum = 1;
|
|
436
|
+
else while (--i >= 0) sum += children[i].value;
|
|
437
|
+
node.value = sum;
|
|
438
|
+
}
|
|
439
|
+
function node_count() {
|
|
440
|
+
return this.eachAfter(count);
|
|
441
|
+
}
|
|
442
|
+
function node_each(callback) {
|
|
443
|
+
var node = this, current, next = [node], children, i, n;
|
|
444
|
+
do {
|
|
445
|
+
current = next.reverse(), next = [];
|
|
446
|
+
while (node = current.pop()) {
|
|
447
|
+
callback(node), children = node.children;
|
|
448
|
+
if (children) for (i = 0, n = children.length; i < n; ++i) {
|
|
449
|
+
next.push(children[i]);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
} while (next.length);
|
|
453
|
+
return this;
|
|
454
|
+
}
|
|
455
|
+
function node_eachBefore(callback) {
|
|
456
|
+
var node = this, nodes = [node], children, i;
|
|
457
|
+
while (node = nodes.pop()) {
|
|
458
|
+
callback(node), children = node.children;
|
|
459
|
+
if (children) for (i = children.length - 1; i >= 0; --i) {
|
|
460
|
+
nodes.push(children[i]);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return this;
|
|
464
|
+
}
|
|
465
|
+
function node_eachAfter(callback) {
|
|
466
|
+
var node = this, nodes = [node], next = [], children, i, n;
|
|
467
|
+
while (node = nodes.pop()) {
|
|
468
|
+
next.push(node), children = node.children;
|
|
469
|
+
if (children) for (i = 0, n = children.length; i < n; ++i) {
|
|
470
|
+
nodes.push(children[i]);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
while (node = next.pop()) {
|
|
474
|
+
callback(node);
|
|
475
|
+
}
|
|
476
|
+
return this;
|
|
477
|
+
}
|
|
478
|
+
function node_sum(value) {
|
|
479
|
+
return this.eachAfter(function(node) {
|
|
480
|
+
var sum = +value(node.data) || 0, children = node.children, i = children && children.length;
|
|
481
|
+
while (--i >= 0) sum += children[i].value;
|
|
482
|
+
node.value = sum;
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
function node_sort(compare) {
|
|
486
|
+
return this.eachBefore(function(node) {
|
|
487
|
+
if (node.children) {
|
|
488
|
+
node.children.sort(compare);
|
|
489
|
+
}
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
function node_path(end) {
|
|
493
|
+
var start = this, ancestor = leastCommonAncestor(start, end), nodes = [start];
|
|
494
|
+
while (start !== ancestor) {
|
|
495
|
+
start = start.parent;
|
|
496
|
+
nodes.push(start);
|
|
497
|
+
}
|
|
498
|
+
var k = nodes.length;
|
|
499
|
+
while (end !== ancestor) {
|
|
500
|
+
nodes.splice(k, 0, end);
|
|
501
|
+
end = end.parent;
|
|
502
|
+
}
|
|
503
|
+
return nodes;
|
|
504
|
+
}
|
|
505
|
+
function leastCommonAncestor(a, b) {
|
|
506
|
+
if (a === b) return a;
|
|
507
|
+
var aNodes = a.ancestors(), bNodes = b.ancestors(), c = null;
|
|
508
|
+
a = aNodes.pop();
|
|
509
|
+
b = bNodes.pop();
|
|
510
|
+
while (a === b) {
|
|
511
|
+
c = a;
|
|
512
|
+
a = aNodes.pop();
|
|
513
|
+
b = bNodes.pop();
|
|
514
|
+
}
|
|
515
|
+
return c;
|
|
516
|
+
}
|
|
517
|
+
function node_ancestors() {
|
|
518
|
+
var node = this, nodes = [node];
|
|
519
|
+
while (node = node.parent) {
|
|
520
|
+
nodes.push(node);
|
|
521
|
+
}
|
|
522
|
+
return nodes;
|
|
523
|
+
}
|
|
524
|
+
function node_descendants() {
|
|
525
|
+
var nodes = [];
|
|
526
|
+
this.each(function(node) {
|
|
527
|
+
nodes.push(node);
|
|
528
|
+
});
|
|
529
|
+
return nodes;
|
|
530
|
+
}
|
|
531
|
+
function node_leaves() {
|
|
532
|
+
var leaves = [];
|
|
533
|
+
this.eachBefore(function(node) {
|
|
534
|
+
if (!node.children) {
|
|
535
|
+
leaves.push(node);
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
return leaves;
|
|
539
|
+
}
|
|
540
|
+
function node_links() {
|
|
541
|
+
var root = this, links = [];
|
|
542
|
+
root.each(function(node) {
|
|
543
|
+
if (node !== root) {
|
|
544
|
+
links.push({ source: node.parent, target: node });
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
return links;
|
|
548
|
+
}
|
|
549
|
+
function hierarchy(data, children) {
|
|
550
|
+
var root = new Node$1(data), valued = +data.value && (root.value = data.value), node, nodes = [root], child, childs, i, n;
|
|
551
|
+
if (children == null) children = defaultChildren;
|
|
552
|
+
while (node = nodes.pop()) {
|
|
553
|
+
if (valued) node.value = +node.data.value;
|
|
554
|
+
if ((childs = children(node.data)) && (n = childs.length)) {
|
|
555
|
+
node.children = new Array(n);
|
|
556
|
+
for (i = n - 1; i >= 0; --i) {
|
|
557
|
+
nodes.push(child = node.children[i] = new Node$1(childs[i]));
|
|
558
|
+
child.parent = node;
|
|
559
|
+
child.depth = node.depth + 1;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return root.eachBefore(computeHeight);
|
|
564
|
+
}
|
|
565
|
+
function node_copy() {
|
|
566
|
+
return hierarchy(this).eachBefore(copyData);
|
|
567
|
+
}
|
|
568
|
+
function defaultChildren(d) {
|
|
569
|
+
return d.children;
|
|
570
|
+
}
|
|
571
|
+
function copyData(node) {
|
|
572
|
+
node.data = node.data.data;
|
|
573
|
+
}
|
|
574
|
+
function computeHeight(node) {
|
|
575
|
+
var height = 0;
|
|
576
|
+
do
|
|
577
|
+
node.height = height;
|
|
578
|
+
while ((node = node.parent) && node.height < ++height);
|
|
579
|
+
}
|
|
580
|
+
function Node$1(data) {
|
|
581
|
+
this.data = data;
|
|
582
|
+
this.depth = this.height = 0;
|
|
583
|
+
this.parent = null;
|
|
584
|
+
}
|
|
585
|
+
Node$1.prototype = hierarchy.prototype = {
|
|
586
|
+
constructor: Node$1,
|
|
587
|
+
count: node_count,
|
|
588
|
+
each: node_each,
|
|
589
|
+
eachAfter: node_eachAfter,
|
|
590
|
+
eachBefore: node_eachBefore,
|
|
591
|
+
sum: node_sum,
|
|
592
|
+
sort: node_sort,
|
|
593
|
+
path: node_path,
|
|
594
|
+
ancestors: node_ancestors,
|
|
595
|
+
descendants: node_descendants,
|
|
596
|
+
leaves: node_leaves,
|
|
597
|
+
links: node_links,
|
|
598
|
+
copy: node_copy
|
|
599
|
+
};
|
|
600
|
+
const version$1 = "2.1.2";
|
|
601
|
+
const packageInfo = {
|
|
602
|
+
version: version$1
|
|
603
|
+
};
|
|
604
|
+
const { version } = packageInfo;
|
|
605
|
+
const defaults = Object.freeze({
|
|
606
|
+
children: (data) => data.children,
|
|
607
|
+
nodeSize: (node) => node.data.size,
|
|
608
|
+
spacing: 0
|
|
609
|
+
});
|
|
610
|
+
function flextree(options) {
|
|
611
|
+
const opts = Object.assign({}, defaults, options);
|
|
612
|
+
function accessor(name) {
|
|
613
|
+
const opt = opts[name];
|
|
614
|
+
return typeof opt === "function" ? opt : () => opt;
|
|
615
|
+
}
|
|
616
|
+
function layout(tree) {
|
|
617
|
+
const wtree = wrap(getWrapper(), tree, (node) => node.children);
|
|
618
|
+
wtree.update();
|
|
619
|
+
return wtree.data;
|
|
620
|
+
}
|
|
621
|
+
function getFlexNode() {
|
|
622
|
+
const nodeSize = accessor("nodeSize");
|
|
623
|
+
const spacing = accessor("spacing");
|
|
624
|
+
return class FlexNode extends hierarchy.prototype.constructor {
|
|
625
|
+
constructor(data) {
|
|
626
|
+
super(data);
|
|
627
|
+
}
|
|
628
|
+
copy() {
|
|
629
|
+
const c = wrap(this.constructor, this, (node) => node.children);
|
|
630
|
+
c.each((node) => node.data = node.data.data);
|
|
631
|
+
return c;
|
|
632
|
+
}
|
|
633
|
+
get size() {
|
|
634
|
+
return nodeSize(this);
|
|
635
|
+
}
|
|
636
|
+
spacing(oNode) {
|
|
637
|
+
return spacing(this, oNode);
|
|
638
|
+
}
|
|
639
|
+
get nodes() {
|
|
640
|
+
return this.descendants();
|
|
641
|
+
}
|
|
642
|
+
get xSize() {
|
|
643
|
+
return this.size[0];
|
|
644
|
+
}
|
|
645
|
+
get ySize() {
|
|
646
|
+
return this.size[1];
|
|
647
|
+
}
|
|
648
|
+
get top() {
|
|
649
|
+
return this.y;
|
|
650
|
+
}
|
|
651
|
+
get bottom() {
|
|
652
|
+
return this.y + this.ySize;
|
|
653
|
+
}
|
|
654
|
+
get left() {
|
|
655
|
+
return this.x - this.xSize / 2;
|
|
656
|
+
}
|
|
657
|
+
get right() {
|
|
658
|
+
return this.x + this.xSize / 2;
|
|
659
|
+
}
|
|
660
|
+
get root() {
|
|
661
|
+
const ancs = this.ancestors();
|
|
662
|
+
return ancs[ancs.length - 1];
|
|
663
|
+
}
|
|
664
|
+
get numChildren() {
|
|
665
|
+
return this.hasChildren ? this.children.length : 0;
|
|
666
|
+
}
|
|
667
|
+
get hasChildren() {
|
|
668
|
+
return !this.noChildren;
|
|
669
|
+
}
|
|
670
|
+
get noChildren() {
|
|
671
|
+
return this.children === null;
|
|
672
|
+
}
|
|
673
|
+
get firstChild() {
|
|
674
|
+
return this.hasChildren ? this.children[0] : null;
|
|
675
|
+
}
|
|
676
|
+
get lastChild() {
|
|
677
|
+
return this.hasChildren ? this.children[this.numChildren - 1] : null;
|
|
678
|
+
}
|
|
679
|
+
get extents() {
|
|
680
|
+
return (this.children || []).reduce(
|
|
681
|
+
(acc, kid) => FlexNode.maxExtents(acc, kid.extents),
|
|
682
|
+
this.nodeExtents
|
|
683
|
+
);
|
|
684
|
+
}
|
|
685
|
+
get nodeExtents() {
|
|
686
|
+
return {
|
|
687
|
+
top: this.top,
|
|
688
|
+
bottom: this.bottom,
|
|
689
|
+
left: this.left,
|
|
690
|
+
right: this.right
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
static maxExtents(e0, e1) {
|
|
694
|
+
return {
|
|
695
|
+
top: Math.min(e0.top, e1.top),
|
|
696
|
+
bottom: Math.max(e0.bottom, e1.bottom),
|
|
697
|
+
left: Math.min(e0.left, e1.left),
|
|
698
|
+
right: Math.max(e0.right, e1.right)
|
|
699
|
+
};
|
|
700
|
+
}
|
|
701
|
+
};
|
|
702
|
+
}
|
|
703
|
+
function getWrapper() {
|
|
704
|
+
const FlexNode = getFlexNode();
|
|
705
|
+
const nodeSize = accessor("nodeSize");
|
|
706
|
+
const spacing = accessor("spacing");
|
|
707
|
+
return class extends FlexNode {
|
|
708
|
+
constructor(data) {
|
|
709
|
+
super(data);
|
|
710
|
+
Object.assign(this, {
|
|
711
|
+
x: 0,
|
|
712
|
+
y: 0,
|
|
713
|
+
relX: 0,
|
|
714
|
+
prelim: 0,
|
|
715
|
+
shift: 0,
|
|
716
|
+
change: 0,
|
|
717
|
+
lExt: this,
|
|
718
|
+
lExtRelX: 0,
|
|
719
|
+
lThr: null,
|
|
720
|
+
rExt: this,
|
|
721
|
+
rExtRelX: 0,
|
|
722
|
+
rThr: null
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
get size() {
|
|
726
|
+
return nodeSize(this.data);
|
|
727
|
+
}
|
|
728
|
+
spacing(oNode) {
|
|
729
|
+
return spacing(this.data, oNode.data);
|
|
730
|
+
}
|
|
731
|
+
get x() {
|
|
732
|
+
return this.data.x;
|
|
733
|
+
}
|
|
734
|
+
set x(v) {
|
|
735
|
+
this.data.x = v;
|
|
736
|
+
}
|
|
737
|
+
get y() {
|
|
738
|
+
return this.data.y;
|
|
739
|
+
}
|
|
740
|
+
set y(v) {
|
|
741
|
+
this.data.y = v;
|
|
742
|
+
}
|
|
743
|
+
update() {
|
|
744
|
+
layoutChildren(this);
|
|
745
|
+
resolveX(this);
|
|
746
|
+
return this;
|
|
747
|
+
}
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
function wrap(FlexClass, treeData, children) {
|
|
751
|
+
const _wrap = (data, parent) => {
|
|
752
|
+
const node = new FlexClass(data);
|
|
753
|
+
Object.assign(node, {
|
|
754
|
+
parent,
|
|
755
|
+
depth: parent === null ? 0 : parent.depth + 1,
|
|
756
|
+
height: 0,
|
|
757
|
+
length: 1
|
|
758
|
+
});
|
|
759
|
+
const kidsData = children(data) || [];
|
|
760
|
+
node.children = kidsData.length === 0 ? null : kidsData.map((kd) => _wrap(kd, node));
|
|
761
|
+
if (node.children) {
|
|
762
|
+
Object.assign(node, node.children.reduce(
|
|
763
|
+
(hl, kid) => ({
|
|
764
|
+
height: Math.max(hl.height, kid.height + 1),
|
|
765
|
+
length: hl.length + kid.length
|
|
766
|
+
}),
|
|
767
|
+
node
|
|
768
|
+
));
|
|
769
|
+
}
|
|
770
|
+
return node;
|
|
771
|
+
};
|
|
772
|
+
return _wrap(treeData, null);
|
|
773
|
+
}
|
|
774
|
+
Object.assign(layout, {
|
|
775
|
+
nodeSize(arg) {
|
|
776
|
+
return arguments.length ? (opts.nodeSize = arg, layout) : opts.nodeSize;
|
|
777
|
+
},
|
|
778
|
+
spacing(arg) {
|
|
779
|
+
return arguments.length ? (opts.spacing = arg, layout) : opts.spacing;
|
|
780
|
+
},
|
|
781
|
+
children(arg) {
|
|
782
|
+
return arguments.length ? (opts.children = arg, layout) : opts.children;
|
|
783
|
+
},
|
|
784
|
+
hierarchy(treeData, children) {
|
|
785
|
+
const kids = typeof children === "undefined" ? opts.children : children;
|
|
786
|
+
return wrap(getFlexNode(), treeData, kids);
|
|
787
|
+
},
|
|
788
|
+
dump(tree) {
|
|
789
|
+
const nodeSize = accessor("nodeSize");
|
|
790
|
+
const _dump = (i0) => (node) => {
|
|
791
|
+
const i1 = i0 + " ";
|
|
792
|
+
const i2 = i0 + " ";
|
|
793
|
+
const { x, y } = node;
|
|
794
|
+
const size = nodeSize(node);
|
|
795
|
+
const kids = node.children || [];
|
|
796
|
+
const kdumps = kids.length === 0 ? " " : `,${i1}children: [${i2}${kids.map(_dump(i2)).join(i2)}${i1}],${i0}`;
|
|
797
|
+
return `{ size: [${size.join(", ")}],${i1}x: ${x}, y: ${y}${kdumps}},`;
|
|
798
|
+
};
|
|
799
|
+
return _dump("\n")(tree);
|
|
800
|
+
}
|
|
801
|
+
});
|
|
802
|
+
return layout;
|
|
803
|
+
}
|
|
804
|
+
flextree.version = version;
|
|
805
|
+
const layoutChildren = (w, y = 0) => {
|
|
806
|
+
w.y = y;
|
|
807
|
+
(w.children || []).reduce((acc, kid) => {
|
|
808
|
+
const [i, lastLows] = acc;
|
|
809
|
+
layoutChildren(kid, w.y + w.ySize);
|
|
810
|
+
const lowY = (i === 0 ? kid.lExt : kid.rExt).bottom;
|
|
811
|
+
if (i !== 0) separate(w, i, lastLows);
|
|
812
|
+
const lows = updateLows(lowY, i, lastLows);
|
|
813
|
+
return [i + 1, lows];
|
|
814
|
+
}, [0, null]);
|
|
815
|
+
shiftChange(w);
|
|
816
|
+
positionRoot(w);
|
|
817
|
+
return w;
|
|
818
|
+
};
|
|
819
|
+
const resolveX = (w, prevSum, parentX) => {
|
|
820
|
+
if (typeof prevSum === "undefined") {
|
|
821
|
+
prevSum = -w.relX - w.prelim;
|
|
822
|
+
parentX = 0;
|
|
823
|
+
}
|
|
824
|
+
const sum = prevSum + w.relX;
|
|
825
|
+
w.relX = sum + w.prelim - parentX;
|
|
826
|
+
w.prelim = 0;
|
|
827
|
+
w.x = parentX + w.relX;
|
|
828
|
+
(w.children || []).forEach((k) => resolveX(k, sum, w.x));
|
|
829
|
+
return w;
|
|
830
|
+
};
|
|
831
|
+
const shiftChange = (w) => {
|
|
832
|
+
(w.children || []).reduce((acc, child) => {
|
|
833
|
+
const [lastShiftSum, lastChangeSum] = acc;
|
|
834
|
+
const shiftSum = lastShiftSum + child.shift;
|
|
835
|
+
const changeSum = lastChangeSum + shiftSum + child.change;
|
|
836
|
+
child.relX += changeSum;
|
|
837
|
+
return [shiftSum, changeSum];
|
|
838
|
+
}, [0, 0]);
|
|
839
|
+
};
|
|
840
|
+
const separate = (w, i, lows) => {
|
|
841
|
+
const lSib = w.children[i - 1];
|
|
842
|
+
const curSubtree = w.children[i];
|
|
843
|
+
let rContour = lSib;
|
|
844
|
+
let rSumMods = lSib.relX;
|
|
845
|
+
let lContour = curSubtree;
|
|
846
|
+
let lSumMods = curSubtree.relX;
|
|
847
|
+
let isFirst = true;
|
|
848
|
+
while (rContour && lContour) {
|
|
849
|
+
if (rContour.bottom > lows.lowY) lows = lows.next;
|
|
850
|
+
const dist = rSumMods + rContour.prelim - (lSumMods + lContour.prelim) + rContour.xSize / 2 + lContour.xSize / 2 + rContour.spacing(lContour);
|
|
851
|
+
if (dist > 0 || dist < 0 && isFirst) {
|
|
852
|
+
lSumMods += dist;
|
|
853
|
+
moveSubtree(curSubtree, dist);
|
|
854
|
+
distributeExtra(w, i, lows.index, dist);
|
|
855
|
+
}
|
|
856
|
+
isFirst = false;
|
|
857
|
+
const rightBottom = rContour.bottom;
|
|
858
|
+
const leftBottom = lContour.bottom;
|
|
859
|
+
if (rightBottom <= leftBottom) {
|
|
860
|
+
rContour = nextRContour(rContour);
|
|
861
|
+
if (rContour) rSumMods += rContour.relX;
|
|
862
|
+
}
|
|
863
|
+
if (rightBottom >= leftBottom) {
|
|
864
|
+
lContour = nextLContour(lContour);
|
|
865
|
+
if (lContour) lSumMods += lContour.relX;
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
if (!rContour && lContour) setLThr(w, i, lContour, lSumMods);
|
|
869
|
+
else if (rContour && !lContour) setRThr(w, i, rContour, rSumMods);
|
|
870
|
+
};
|
|
871
|
+
const moveSubtree = (subtree, distance) => {
|
|
872
|
+
subtree.relX += distance;
|
|
873
|
+
subtree.lExtRelX += distance;
|
|
874
|
+
subtree.rExtRelX += distance;
|
|
875
|
+
};
|
|
876
|
+
const distributeExtra = (w, curSubtreeI, leftSibI, dist) => {
|
|
877
|
+
const curSubtree = w.children[curSubtreeI];
|
|
878
|
+
const n = curSubtreeI - leftSibI;
|
|
879
|
+
if (n > 1) {
|
|
880
|
+
const delta = dist / n;
|
|
881
|
+
w.children[leftSibI + 1].shift += delta;
|
|
882
|
+
curSubtree.shift -= delta;
|
|
883
|
+
curSubtree.change -= dist - delta;
|
|
884
|
+
}
|
|
885
|
+
};
|
|
886
|
+
const nextLContour = (w) => {
|
|
887
|
+
return w.hasChildren ? w.firstChild : w.lThr;
|
|
888
|
+
};
|
|
889
|
+
const nextRContour = (w) => {
|
|
890
|
+
return w.hasChildren ? w.lastChild : w.rThr;
|
|
891
|
+
};
|
|
892
|
+
const setLThr = (w, i, lContour, lSumMods) => {
|
|
893
|
+
const firstChild = w.firstChild;
|
|
894
|
+
const lExt = firstChild.lExt;
|
|
895
|
+
const curSubtree = w.children[i];
|
|
896
|
+
lExt.lThr = lContour;
|
|
897
|
+
const diff = lSumMods - lContour.relX - firstChild.lExtRelX;
|
|
898
|
+
lExt.relX += diff;
|
|
899
|
+
lExt.prelim -= diff;
|
|
900
|
+
firstChild.lExt = curSubtree.lExt;
|
|
901
|
+
firstChild.lExtRelX = curSubtree.lExtRelX;
|
|
902
|
+
};
|
|
903
|
+
const setRThr = (w, i, rContour, rSumMods) => {
|
|
904
|
+
const curSubtree = w.children[i];
|
|
905
|
+
const rExt = curSubtree.rExt;
|
|
906
|
+
const lSib = w.children[i - 1];
|
|
907
|
+
rExt.rThr = rContour;
|
|
908
|
+
const diff = rSumMods - rContour.relX - curSubtree.rExtRelX;
|
|
909
|
+
rExt.relX += diff;
|
|
910
|
+
rExt.prelim -= diff;
|
|
911
|
+
curSubtree.rExt = lSib.rExt;
|
|
912
|
+
curSubtree.rExtRelX = lSib.rExtRelX;
|
|
913
|
+
};
|
|
914
|
+
const positionRoot = (w) => {
|
|
915
|
+
if (w.hasChildren) {
|
|
916
|
+
const k0 = w.firstChild;
|
|
917
|
+
const kf = w.lastChild;
|
|
918
|
+
const prelim = (k0.prelim + k0.relX - k0.xSize / 2 + kf.relX + kf.prelim + kf.xSize / 2) / 2;
|
|
919
|
+
Object.assign(w, {
|
|
920
|
+
prelim,
|
|
921
|
+
lExt: k0.lExt,
|
|
922
|
+
lExtRelX: k0.lExtRelX,
|
|
923
|
+
rExt: kf.rExt,
|
|
924
|
+
rExtRelX: kf.rExtRelX
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
};
|
|
928
|
+
const updateLows = (lowY, index, lastLows) => {
|
|
929
|
+
while (lastLows !== null && lowY >= lastLows.lowY)
|
|
930
|
+
lastLows = lastLows.next;
|
|
931
|
+
return {
|
|
932
|
+
lowY,
|
|
933
|
+
index,
|
|
934
|
+
next: lastLows
|
|
935
|
+
};
|
|
936
|
+
};
|
|
937
|
+
const css = ".markmap {\r\n --markmap-max-width: 9999px;\r\n --markmap-a-color: #0097e6;\r\n --markmap-a-hover-color: #00a8ff;\r\n --markmap-code-bg: #f0f0f0;\r\n --markmap-code-color: #555;\r\n --markmap-highlight-bg: #ffeaa7;\r\n --markmap-table-border: 1px solid currentColor;\r\n --markmap-font: 300 16px/20px sans-serif;\r\n --markmap-circle-open-bg: #fff;\r\n --markmap-text-color: #333;\r\n --markmap-highlight-node-bg: #ff02;\r\n\r\n font: var(--markmap-font);\r\n color: var(--markmap-text-color);\r\n}\r\n\r\n .markmap-link {\r\n fill: none;\r\n }\r\n\r\n .markmap-node > circle {\r\n cursor: pointer;\r\n }\r\n\r\n .markmap-node-hovered > foreignObject > div > div {\r\n padding: 2px 6px;\r\n border-radius: 14px;\r\n border: 2px solid #d9d9d9;\r\n }\r\n\r\n .markmap-selected > foreignObject > div > div,\r\n .markmap-selected.markmap-node-hovered > foreignObject > div > div {\r\n padding: 2px 6px;\r\n border-radius: 14px;\r\n border: 2px solid #b4b4b4;\r\n }\r\n\r\n .markmap-selected > circle {\r\n opacity: 0 !important;\r\n pointer-events: none;\r\n }\r\n\r\n .markmap-collapse-on-hover .markmap-node > circle {\r\n opacity: 0;\r\n transition: opacity 0.2s;\r\n }\r\n\r\n .markmap-collapse-on-hover .markmap-node:hover > circle {\r\n opacity: 1;\r\n }\r\n\r\n .markmap-foreign {\r\n display: inline-block;\r\n }\r\n\r\n .markmap-foreign > div > div {\r\n padding: 4px 6px;\r\n }\r\n\r\n .markmap-foreign .markmap-edit-wrapper {\r\n position: relative;\r\n width: 100%;\r\n padding: 4px;\r\n background: white;\r\n border-radius: 4px;\r\n box-shadow: 0 2px 8px rgba(0,0,0,0.15);\r\n animation: fadeIn 0.2s ease-in;\r\n }\r\n\r\n .markmap-foreign .markmap-edit-input {\r\n width: 100%;\r\n padding: 8px 12px;\r\n border: 2px solid #4A90E2;\r\n border-radius: 4px;\r\n font-size: 14px;\r\n font-family: inherit;\r\n outline: none;\r\n background: #f8f9fa;\r\n transition: all 0.2s;\r\n }\r\n\r\n .markmap-foreign .markmap-edit-input:focus {\r\n border-color: #2D7DD2;\r\n background: white;\r\n box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.1);\r\n }\r\n\r\n .markmap-foreign p {\r\n margin: 0;\r\n }\r\n\r\n .markmap-foreign a {\r\n color: var(--markmap-a-color);\r\n }\r\n\r\n .markmap-foreign a:hover {\r\n color: var(--markmap-a-hover-color);\r\n }\r\n\r\n .markmap-foreign code {\r\n padding: 0.25em;\r\n font-size: calc(1em - 2px);\r\n color: var(--markmap-code-color);\r\n background-color: var(--markmap-code-bg);\r\n border-radius: 2px;\r\n }\r\n\r\n .markmap-foreign pre {\r\n margin: 0;\r\n }\r\n\r\n .markmap-foreign pre > code {\r\n display: block;\r\n }\r\n\r\n .markmap-foreign del {\r\n text-decoration: line-through;\r\n }\r\n\r\n .markmap-foreign em {\r\n font-style: italic;\r\n }\r\n\r\n .markmap-foreign strong {\r\n font-weight: bold;\r\n }\r\n\r\n .markmap-foreign mark {\r\n background: var(--markmap-highlight-bg);\r\n }\r\n\r\n .markmap-foreign table,\r\n .markmap-foreign th,\r\n .markmap-foreign td {\r\n border-collapse: collapse;\r\n border: var(--markmap-table-border);\r\n }\r\n\r\n .markmap-foreign img {\r\n display: inline-block;\r\n }\r\n\r\n .markmap-foreign svg {\r\n fill: currentColor;\r\n }\r\n\r\n .markmap-foreign > div {\r\n width: var(--markmap-max-width);\r\n text-align: left;\r\n }\r\n\r\n .markmap-foreign > div > div {\r\n display: inline-block;\r\n max-width: var(--markmap-wrap-width, 30em);\r\n overflow-wrap: break-word;\r\n word-break: break-word;\r\n white-space: normal;\r\n }\r\n\r\n .markmap-highlight rect {\r\n fill: var(--markmap-highlight-node-bg);\r\n }\r\n\r\n.markmap-dark .markmap {\r\n --markmap-code-bg: #1a1b26;\r\n --markmap-code-color: #ddd;\r\n --markmap-circle-open-bg: #444;\r\n --markmap-text-color: #eee;\r\n}\r\n\r\n@keyframes fadeIn {\r\n from {\r\n opacity: 0;\r\n transform: scale(0.95);\r\n }\r\n to {\r\n opacity: 1;\r\n transform: scale(1);\r\n }\r\n}\r\n";
|
|
938
|
+
const SELECTOR_NODE$1 = "g.markmap-node";
|
|
939
|
+
class ActionManager {
|
|
940
|
+
constructor(ctx) {
|
|
941
|
+
this.ctx = ctx;
|
|
942
|
+
this.editingNode = null;
|
|
943
|
+
this.selectedNode = null;
|
|
944
|
+
}
|
|
945
|
+
// ── Private helpers ──────────────────────────────────────────────────────
|
|
946
|
+
_getNodeContentEl(node) {
|
|
947
|
+
const el = this.ctx.findElement(node);
|
|
948
|
+
if (!el) return null;
|
|
949
|
+
const fo = d32.select(el.g).select("foreignObject");
|
|
950
|
+
const contentDiv = fo.select("div").select("div");
|
|
951
|
+
return contentDiv.node() || null;
|
|
952
|
+
}
|
|
953
|
+
_getOverlayRoot() {
|
|
954
|
+
const svgNode = this.ctx.svg.node();
|
|
955
|
+
return (svgNode == null ? void 0 : svgNode.parentElement) || document.body;
|
|
956
|
+
}
|
|
957
|
+
_positionOverlayToEl(wrap, targetEl, opts) {
|
|
958
|
+
const overlayRoot = this._getOverlayRoot();
|
|
959
|
+
const rootStyle = window.getComputedStyle(overlayRoot);
|
|
960
|
+
if (rootStyle.position === "static") overlayRoot.style.position = "relative";
|
|
961
|
+
const targetRect = targetEl.getBoundingClientRect();
|
|
962
|
+
const rootRect = overlayRoot.getBoundingClientRect();
|
|
963
|
+
const dx = (opts == null ? void 0 : opts.dx) ?? 0;
|
|
964
|
+
const dy = (opts == null ? void 0 : opts.dy) ?? 0;
|
|
965
|
+
const minW = (opts == null ? void 0 : opts.minW) ?? 0;
|
|
966
|
+
const minH = (opts == null ? void 0 : opts.minH) ?? 0;
|
|
967
|
+
let left = targetRect.left - rootRect.left + dx;
|
|
968
|
+
let top = targetRect.top - rootRect.top + dy;
|
|
969
|
+
if ((opts == null ? void 0 : opts.anchor) === "br") {
|
|
970
|
+
left = targetRect.right - rootRect.left + dx;
|
|
971
|
+
top = targetRect.bottom - rootRect.top + dy;
|
|
972
|
+
} else if ((opts == null ? void 0 : opts.anchor) === "r") {
|
|
973
|
+
left = targetRect.right - rootRect.left + dx;
|
|
974
|
+
top = targetRect.top - rootRect.top + dy;
|
|
975
|
+
}
|
|
976
|
+
wrap.style.left = `${left}px`;
|
|
977
|
+
wrap.style.top = `${top}px`;
|
|
978
|
+
if (minW) wrap.style.width = `${Math.max(minW, targetRect.width)}px`;
|
|
979
|
+
if (minH) wrap.style.height = `${Math.max(minH, targetRect.height)}px`;
|
|
980
|
+
}
|
|
981
|
+
_safeRemoveEl(el) {
|
|
982
|
+
try {
|
|
983
|
+
if (el.parentNode) el.remove();
|
|
984
|
+
} catch {
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
_clearSelectionCss() {
|
|
988
|
+
if (!this.ctx.options.clickBorder) return;
|
|
989
|
+
this.ctx.g.selectAll(childSelector(SELECTOR_NODE$1)).classed("markmap-selected", false);
|
|
990
|
+
}
|
|
991
|
+
_findParent(target) {
|
|
992
|
+
if (!this.ctx.state.data) return null;
|
|
993
|
+
let result = null;
|
|
994
|
+
walkTree(this.ctx.state.data, (item, next) => {
|
|
995
|
+
if (result) return;
|
|
996
|
+
const children = item.children || [];
|
|
997
|
+
for (let i = 0; i < children.length; i++) {
|
|
998
|
+
if (children[i] === target) {
|
|
999
|
+
result = { parent: item, index: i };
|
|
1000
|
+
return;
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
next();
|
|
1004
|
+
});
|
|
1005
|
+
return result;
|
|
1006
|
+
}
|
|
1007
|
+
/** Create a blank child INode and append it to parent.children. */
|
|
1008
|
+
_insertNewChildNode(parent) {
|
|
1009
|
+
var _a, _b, _c;
|
|
1010
|
+
let maxId = 0;
|
|
1011
|
+
walkTree(this.ctx.state.data, (item, next) => {
|
|
1012
|
+
var _a2;
|
|
1013
|
+
if (((_a2 = item.state) == null ? void 0 : _a2.id) > maxId) maxId = item.state.id;
|
|
1014
|
+
next();
|
|
1015
|
+
});
|
|
1016
|
+
const newId = maxId + 1;
|
|
1017
|
+
const depth = (((_a = parent.state) == null ? void 0 : _a.depth) ?? 0) + 1;
|
|
1018
|
+
const placeholder = " ";
|
|
1019
|
+
const node = {
|
|
1020
|
+
content: placeholder,
|
|
1021
|
+
children: [],
|
|
1022
|
+
payload: {},
|
|
1023
|
+
state: {
|
|
1024
|
+
id: newId,
|
|
1025
|
+
depth,
|
|
1026
|
+
key: `${(_b = parent.state) == null ? void 0 : _b.id}.${newId}` + simpleHash(placeholder),
|
|
1027
|
+
path: [(_c = parent.state) == null ? void 0 : _c.path, newId].filter(Boolean).join("."),
|
|
1028
|
+
rect: { x: 0, y: 0, width: 0, height: 0 },
|
|
1029
|
+
size: [0, 0]
|
|
1030
|
+
}
|
|
1031
|
+
};
|
|
1032
|
+
parent.children = [...parent.children || [], node];
|
|
1033
|
+
this.ctx.options.color(node);
|
|
1034
|
+
return node;
|
|
1035
|
+
}
|
|
1036
|
+
/**
|
|
1037
|
+
* Core overlay builder shared by _editNewNode and handleEdit.
|
|
1038
|
+
*
|
|
1039
|
+
* @param node - The INode being edited (content will be mutated on save).
|
|
1040
|
+
* @param contentNode - The inner <div> whose position / style drives the overlay.
|
|
1041
|
+
* @param opts.initialValue - Starting text in the input ('': new node, textContent: existing).
|
|
1042
|
+
* @param opts.minWidth - Minimum overlay width in px.
|
|
1043
|
+
* @param opts.placeholder - Input placeholder (only shown when initialValue is empty).
|
|
1044
|
+
* @param opts.selectAll - Whether to select all text on focus (true for existing nodes).
|
|
1045
|
+
* @param opts.onSave - Called after the overlay is torn down on a successful save.
|
|
1046
|
+
* @param opts.onCancel - Called after the overlay is torn down on cancel / empty input.
|
|
1047
|
+
*/
|
|
1048
|
+
_openEditOverlay(node, contentNode, opts) {
|
|
1049
|
+
const overlayRoot = this._getOverlayRoot();
|
|
1050
|
+
const rootStyle = window.getComputedStyle(overlayRoot);
|
|
1051
|
+
if (rootStyle.position === "static") overlayRoot.style.position = "relative";
|
|
1052
|
+
const targetRect = contentNode.getBoundingClientRect();
|
|
1053
|
+
const rootRect = overlayRoot.getBoundingClientRect();
|
|
1054
|
+
const wrap = document.createElement("div");
|
|
1055
|
+
wrap.className = "markmap-node-edit-overlay";
|
|
1056
|
+
wrap.style.cssText = `
|
|
1057
|
+
position: absolute;
|
|
1058
|
+
left: ${targetRect.left - rootRect.left}px;
|
|
1059
|
+
top: ${targetRect.top - rootRect.top}px;
|
|
1060
|
+
width: ${Math.max(opts.minWidth, targetRect.width)}px;
|
|
1061
|
+
height: ${Math.max(20, targetRect.height)}px;
|
|
1062
|
+
z-index: 9999;
|
|
1063
|
+
pointer-events: auto;
|
|
1064
|
+
`;
|
|
1065
|
+
const computedStyle = window.getComputedStyle(contentNode);
|
|
1066
|
+
const input = document.createElement("input");
|
|
1067
|
+
input.type = "text";
|
|
1068
|
+
input.value = opts.initialValue;
|
|
1069
|
+
if (opts.placeholder) input.placeholder = opts.placeholder;
|
|
1070
|
+
input.autocomplete = "off";
|
|
1071
|
+
input.spellcheck = false;
|
|
1072
|
+
input.className = "markmap-node-edit-overlay-input";
|
|
1073
|
+
input.style.cssText = `
|
|
1074
|
+
width: 100%;
|
|
1075
|
+
height: 100%;
|
|
1076
|
+
margin: 0;
|
|
1077
|
+
padding: 2px 6px;
|
|
1078
|
+
border: 2px solid #52c41a;
|
|
1079
|
+
border-radius: 14px;
|
|
1080
|
+
background: #fff;
|
|
1081
|
+
box-sizing: border-box;
|
|
1082
|
+
outline: none;
|
|
1083
|
+
font-size: ${computedStyle.fontSize};
|
|
1084
|
+
font-family: ${computedStyle.fontFamily};
|
|
1085
|
+
line-height: ${computedStyle.lineHeight};
|
|
1086
|
+
font-weight: ${computedStyle.fontWeight};
|
|
1087
|
+
letter-spacing: ${computedStyle.letterSpacing};
|
|
1088
|
+
color: ${computedStyle.color};
|
|
1089
|
+
`;
|
|
1090
|
+
const prevVisibility = contentNode.style.visibility;
|
|
1091
|
+
contentNode.style.visibility = "hidden";
|
|
1092
|
+
wrap.appendChild(input);
|
|
1093
|
+
overlayRoot.appendChild(wrap);
|
|
1094
|
+
this.editOverlay = { wrap, input, contentEl: contentNode, prevVisibility };
|
|
1095
|
+
setTimeout(() => {
|
|
1096
|
+
input.focus();
|
|
1097
|
+
if (opts.selectAll) input.select();
|
|
1098
|
+
}, 0);
|
|
1099
|
+
const escapeHtml = (text) => text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
1100
|
+
let cleanedUp = false;
|
|
1101
|
+
const cleanup = (cancel) => {
|
|
1102
|
+
if (cleanedUp) return;
|
|
1103
|
+
cleanedUp = true;
|
|
1104
|
+
input.removeEventListener("keydown", handleKeydown);
|
|
1105
|
+
input.removeEventListener("blur", handleBlur);
|
|
1106
|
+
if (this.editOverlay) {
|
|
1107
|
+
this.editOverlay.contentEl.style.visibility = this.editOverlay.prevVisibility;
|
|
1108
|
+
this.editOverlay.wrap.remove();
|
|
1109
|
+
this.editOverlay = void 0;
|
|
1110
|
+
} else {
|
|
1111
|
+
contentNode.style.visibility = prevVisibility;
|
|
1112
|
+
wrap.remove();
|
|
1113
|
+
}
|
|
1114
|
+
this.editingNode = null;
|
|
1115
|
+
if (cancel) {
|
|
1116
|
+
opts.onCancel();
|
|
1117
|
+
} else {
|
|
1118
|
+
opts.onSave(escapeHtml(input.value.trim()));
|
|
1119
|
+
}
|
|
1120
|
+
};
|
|
1121
|
+
const handleKeydown = (e) => {
|
|
1122
|
+
if (e.key === "Enter") {
|
|
1123
|
+
e.preventDefault();
|
|
1124
|
+
e.stopPropagation();
|
|
1125
|
+
const text = input.value.trim();
|
|
1126
|
+
if (text) {
|
|
1127
|
+
node.content = escapeHtml(text);
|
|
1128
|
+
cleanup(false);
|
|
1129
|
+
} else {
|
|
1130
|
+
cleanup(true);
|
|
1131
|
+
}
|
|
1132
|
+
} else if (e.key === "Escape") {
|
|
1133
|
+
e.preventDefault();
|
|
1134
|
+
e.stopPropagation();
|
|
1135
|
+
cleanup(true);
|
|
1136
|
+
}
|
|
1137
|
+
};
|
|
1138
|
+
const handleBlur = () => {
|
|
1139
|
+
setTimeout(() => {
|
|
1140
|
+
if (document.activeElement !== input) {
|
|
1141
|
+
const text = input.value.trim();
|
|
1142
|
+
if (text) {
|
|
1143
|
+
node.content = escapeHtml(text);
|
|
1144
|
+
cleanup(false);
|
|
1145
|
+
} else {
|
|
1146
|
+
cleanup(true);
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
}, 100);
|
|
1150
|
+
};
|
|
1151
|
+
input.addEventListener("keydown", handleKeydown);
|
|
1152
|
+
input.addEventListener("blur", handleBlur);
|
|
1153
|
+
}
|
|
1154
|
+
/**
|
|
1155
|
+
* Open an edit overlay for a freshly-inserted node.
|
|
1156
|
+
* Empty input → confirm saves; blank input → cancels and removes the node from the tree.
|
|
1157
|
+
*/
|
|
1158
|
+
_editNewNode(node, parent) {
|
|
1159
|
+
const element = this.ctx.findElement(node);
|
|
1160
|
+
if (!element) return;
|
|
1161
|
+
this.editingNode = node;
|
|
1162
|
+
const contentNode = d32.select(element.g).select("foreignObject").select("div").select("div").node();
|
|
1163
|
+
if (!contentNode) return;
|
|
1164
|
+
this._openEditOverlay(node, contentNode, {
|
|
1165
|
+
initialValue: "",
|
|
1166
|
+
minWidth: 120,
|
|
1167
|
+
placeholder: this.ctx.options.inputPlaceholder,
|
|
1168
|
+
selectAll: false,
|
|
1169
|
+
onCancel: () => {
|
|
1170
|
+
parent.children = (parent.children || []).filter((c) => c !== node);
|
|
1171
|
+
this.hideAddUI();
|
|
1172
|
+
this.selectedNode = null;
|
|
1173
|
+
this._clearSelectionCss();
|
|
1174
|
+
void this.ctx.renderData();
|
|
1175
|
+
},
|
|
1176
|
+
onSave: () => {
|
|
1177
|
+
var _a, _b;
|
|
1178
|
+
(_b = (_a = this.ctx.options).onNodeAdd) == null ? void 0 : _b.call(_a, parent, node);
|
|
1179
|
+
this.hideAddUI();
|
|
1180
|
+
void this.ctx.renderData().then(() => {
|
|
1181
|
+
const showPlus = () => {
|
|
1182
|
+
this.selectedNode = node;
|
|
1183
|
+
if (this.ctx.options.clickBorder) {
|
|
1184
|
+
this.ctx.g.selectAll(childSelector(SELECTOR_NODE$1)).classed("markmap-selected", (n) => n === node);
|
|
1185
|
+
}
|
|
1186
|
+
this.showAddUI(node);
|
|
1187
|
+
};
|
|
1188
|
+
if (this.ctx.options.duration > 0) {
|
|
1189
|
+
setTimeout(showPlus, this.ctx.options.duration);
|
|
1190
|
+
} else {
|
|
1191
|
+
showPlus();
|
|
1192
|
+
}
|
|
1193
|
+
});
|
|
1194
|
+
}
|
|
1195
|
+
});
|
|
1196
|
+
}
|
|
1197
|
+
// ── Public API ───────────────────────────────────────────────────────────
|
|
1198
|
+
/** Reposition all active overlays after zoom / pan. */
|
|
1199
|
+
repositionOverlays() {
|
|
1200
|
+
if (this.addUI) {
|
|
1201
|
+
const contentEl = this._getNodeContentEl(this.addUI.node);
|
|
1202
|
+
if (contentEl) {
|
|
1203
|
+
const svgEl = this.ctx.svg.node();
|
|
1204
|
+
const scale = svgEl ? d32.zoomTransform(svgEl).k : 1;
|
|
1205
|
+
const btnSize = Math.round(16 * scale);
|
|
1206
|
+
const fontSize = Math.round(14 * scale);
|
|
1207
|
+
this.addUI.btn.style.width = `${btnSize}px`;
|
|
1208
|
+
this.addUI.btn.style.height = `${btnSize}px`;
|
|
1209
|
+
this.addUI.btn.style.fontSize = `${fontSize}px`;
|
|
1210
|
+
this._positionOverlayToEl(this.addUI.wrap, contentEl, { anchor: "br", dx: Math.round(14 * scale), dy: 0 });
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
if (this.addInputUI) {
|
|
1214
|
+
const contentEl = this._getNodeContentEl(this.addInputUI.node);
|
|
1215
|
+
if (contentEl) {
|
|
1216
|
+
this._positionOverlayToEl(this.addInputUI.wrap, contentEl, { anchor: "r", dx: 16, dy: 0 });
|
|
1217
|
+
const inputH = 30;
|
|
1218
|
+
const nodeH = contentEl.getBoundingClientRect().height;
|
|
1219
|
+
const currentTop = parseFloat(this.addInputUI.wrap.style.top) || 0;
|
|
1220
|
+
this.addInputUI.wrap.style.top = `${currentTop + nodeH / 2 - inputH / 2}px`;
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
if (this.editOverlay) {
|
|
1224
|
+
const overlayRoot = this._getOverlayRoot();
|
|
1225
|
+
const targetRect = this.editOverlay.contentEl.getBoundingClientRect();
|
|
1226
|
+
const rootRect = overlayRoot.getBoundingClientRect();
|
|
1227
|
+
this.editOverlay.wrap.style.left = `${targetRect.left - rootRect.left}px`;
|
|
1228
|
+
this.editOverlay.wrap.style.top = `${targetRect.top - rootRect.top}px`;
|
|
1229
|
+
this.editOverlay.wrap.style.width = `${Math.max(40, targetRect.width)}px`;
|
|
1230
|
+
this.editOverlay.wrap.style.height = `${Math.max(20, targetRect.height)}px`;
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
/** Hide and destroy the + button and any associated input overlay. */
|
|
1234
|
+
hideAddUI() {
|
|
1235
|
+
if (this.addInputUI) {
|
|
1236
|
+
this._safeRemoveEl(this.addInputUI.wrap);
|
|
1237
|
+
this.addInputUI = void 0;
|
|
1238
|
+
}
|
|
1239
|
+
if (this.addUI) {
|
|
1240
|
+
this.addUI.cleanupDoc();
|
|
1241
|
+
this._safeRemoveEl(this.addUI.wrap);
|
|
1242
|
+
this.addUI = void 0;
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
/** Show the + button anchored to the bottom-right corner of `node`. */
|
|
1246
|
+
showAddUI(node) {
|
|
1247
|
+
var _a;
|
|
1248
|
+
if (!this.ctx.options.addable) return;
|
|
1249
|
+
if (this.editingNode) return;
|
|
1250
|
+
const contentEl = this._getNodeContentEl(node);
|
|
1251
|
+
if (!contentEl) return;
|
|
1252
|
+
if (((_a = this.addUI) == null ? void 0 : _a.node) === node) {
|
|
1253
|
+
this._positionOverlayToEl(this.addUI.wrap, contentEl, { anchor: "br", dx: 14, dy: 0 });
|
|
1254
|
+
return;
|
|
1255
|
+
}
|
|
1256
|
+
this.hideAddUI();
|
|
1257
|
+
const overlayRoot = this._getOverlayRoot();
|
|
1258
|
+
const wrap = document.createElement("div");
|
|
1259
|
+
wrap.style.cssText = `
|
|
1260
|
+
position: absolute;
|
|
1261
|
+
z-index: 9999;
|
|
1262
|
+
pointer-events: auto;
|
|
1263
|
+
`;
|
|
1264
|
+
const svgEl = this.ctx.svg.node();
|
|
1265
|
+
const scale = svgEl ? d32.zoomTransform(svgEl).k : 1;
|
|
1266
|
+
const btnSize = Math.round(16 * scale);
|
|
1267
|
+
const fontSize = Math.round(14 * scale);
|
|
1268
|
+
const btn = document.createElement("button");
|
|
1269
|
+
btn.type = "button";
|
|
1270
|
+
btn.className = "markmap-add-btn";
|
|
1271
|
+
btn.textContent = "+";
|
|
1272
|
+
btn.style.cssText = `
|
|
1273
|
+
width: ${btnSize}px;
|
|
1274
|
+
height: ${btnSize}px;
|
|
1275
|
+
border-radius: 50%;
|
|
1276
|
+
background: #b4b4b4;
|
|
1277
|
+
color: #fff;
|
|
1278
|
+
font-weight: 600;
|
|
1279
|
+
font-size: ${fontSize}px;
|
|
1280
|
+
line-height: 1;
|
|
1281
|
+
padding: 0;
|
|
1282
|
+
cursor: pointer;
|
|
1283
|
+
box-shadow: none;
|
|
1284
|
+
outline: none;
|
|
1285
|
+
border: none;
|
|
1286
|
+
user-select: none;
|
|
1287
|
+
display: flex;
|
|
1288
|
+
align-items: center;
|
|
1289
|
+
justify-content: center;
|
|
1290
|
+
`;
|
|
1291
|
+
btn.addEventListener("click", (e) => {
|
|
1292
|
+
e.preventDefault();
|
|
1293
|
+
e.stopPropagation();
|
|
1294
|
+
this.showAddInput(node);
|
|
1295
|
+
});
|
|
1296
|
+
wrap.style.transform = "translate(-50%, -50%)";
|
|
1297
|
+
wrap.appendChild(btn);
|
|
1298
|
+
overlayRoot.appendChild(wrap);
|
|
1299
|
+
this._positionOverlayToEl(wrap, contentEl, { anchor: "br", dx: Math.round(14 * scale), dy: 0 });
|
|
1300
|
+
const onDocDown = (ev) => {
|
|
1301
|
+
var _a2;
|
|
1302
|
+
const t = ev.target;
|
|
1303
|
+
if (!t) return;
|
|
1304
|
+
if (wrap.contains(t)) return;
|
|
1305
|
+
if ((_a2 = this.addInputUI) == null ? void 0 : _a2.wrap.contains(t)) return;
|
|
1306
|
+
const nodeEl = this._getNodeContentEl(node);
|
|
1307
|
+
if (nodeEl && nodeEl.contains(t)) return;
|
|
1308
|
+
this.hideAddUI();
|
|
1309
|
+
};
|
|
1310
|
+
document.addEventListener("click", onDocDown, true);
|
|
1311
|
+
const cleanupDoc = () => document.removeEventListener("click", onDocDown, true);
|
|
1312
|
+
this.addUI = { node, btn, wrap, cleanupDoc };
|
|
1313
|
+
}
|
|
1314
|
+
/** Delete `node` from the tree and re-render. */
|
|
1315
|
+
deleteNode(node) {
|
|
1316
|
+
if (!this.ctx.options.deletable) return;
|
|
1317
|
+
if (!this.ctx.state.data) return;
|
|
1318
|
+
const found = this._findParent(node);
|
|
1319
|
+
if (!found) return;
|
|
1320
|
+
const { parent, index } = found;
|
|
1321
|
+
const children = [...parent.children || []];
|
|
1322
|
+
children.splice(index, 1);
|
|
1323
|
+
parent.children = children;
|
|
1324
|
+
this.selectedNode = null;
|
|
1325
|
+
this.hideAddUI();
|
|
1326
|
+
this._clearSelectionCss();
|
|
1327
|
+
void this.ctx.renderData();
|
|
1328
|
+
}
|
|
1329
|
+
/**
|
|
1330
|
+
* Insert a new sibling node after `sibling`, render it, then open an edit overlay.
|
|
1331
|
+
* Called when the user presses Enter on a selected node.
|
|
1332
|
+
*/
|
|
1333
|
+
async showAddSiblingInput(sibling) {
|
|
1334
|
+
var _a, _b, _c;
|
|
1335
|
+
if (!this.ctx.state.data) return;
|
|
1336
|
+
const found = this._findParent(sibling);
|
|
1337
|
+
if (!found) return;
|
|
1338
|
+
const { parent, index } = found;
|
|
1339
|
+
this.hideAddUI();
|
|
1340
|
+
let maxId = 0;
|
|
1341
|
+
walkTree(this.ctx.state.data, (item, next) => {
|
|
1342
|
+
var _a2;
|
|
1343
|
+
if (((_a2 = item.state) == null ? void 0 : _a2.id) > maxId) maxId = item.state.id;
|
|
1344
|
+
next();
|
|
1345
|
+
});
|
|
1346
|
+
const newId = maxId + 1;
|
|
1347
|
+
const depth = ((_a = sibling.state) == null ? void 0 : _a.depth) ?? 1;
|
|
1348
|
+
const placeholder = " ";
|
|
1349
|
+
const newNode = {
|
|
1350
|
+
content: placeholder,
|
|
1351
|
+
children: [],
|
|
1352
|
+
payload: {},
|
|
1353
|
+
state: {
|
|
1354
|
+
id: newId,
|
|
1355
|
+
depth,
|
|
1356
|
+
key: `${(_b = parent.state) == null ? void 0 : _b.id}.${newId}` + simpleHash(placeholder),
|
|
1357
|
+
path: [(_c = parent.state) == null ? void 0 : _c.path, newId].filter(Boolean).join("."),
|
|
1358
|
+
rect: { x: 0, y: 0, width: 0, height: 0 },
|
|
1359
|
+
size: [0, 0]
|
|
1360
|
+
}
|
|
1361
|
+
};
|
|
1362
|
+
const children = [...parent.children || []];
|
|
1363
|
+
children.splice(index + 1, 0, newNode);
|
|
1364
|
+
parent.children = children;
|
|
1365
|
+
this.ctx.options.color(newNode);
|
|
1366
|
+
await this.ctx.renderData();
|
|
1367
|
+
if (this.ctx.options.duration > 0) {
|
|
1368
|
+
await new Promise((resolve) => setTimeout(resolve, this.ctx.options.duration));
|
|
1369
|
+
}
|
|
1370
|
+
this._editNewNode(newNode, parent);
|
|
1371
|
+
}
|
|
1372
|
+
/**
|
|
1373
|
+
* Insert a new child node under `node`, render it, then open an edit overlay.
|
|
1374
|
+
* Called when the user clicks the + button or presses Tab.
|
|
1375
|
+
*/
|
|
1376
|
+
async showAddInput(node) {
|
|
1377
|
+
if (!this.ctx.state.data) return;
|
|
1378
|
+
this.hideAddUI();
|
|
1379
|
+
const child = this._insertNewChildNode(node);
|
|
1380
|
+
await this.ctx.renderData();
|
|
1381
|
+
if (this.ctx.options.duration > 0) {
|
|
1382
|
+
await new Promise((resolve) => setTimeout(resolve, this.ctx.options.duration));
|
|
1383
|
+
}
|
|
1384
|
+
this._editNewNode(child, node);
|
|
1385
|
+
}
|
|
1386
|
+
/**
|
|
1387
|
+
* Open an edit overlay for an *existing* node.
|
|
1388
|
+
* Called on double-click. On confirm the node content is updated in-place.
|
|
1389
|
+
*/
|
|
1390
|
+
handleEdit(e, d) {
|
|
1391
|
+
if (!this.ctx.options.editable) return;
|
|
1392
|
+
e.preventDefault();
|
|
1393
|
+
e.stopPropagation();
|
|
1394
|
+
e.stopImmediatePropagation();
|
|
1395
|
+
if (this.editingNode) {
|
|
1396
|
+
this.saveEdit();
|
|
1397
|
+
}
|
|
1398
|
+
const element = this.ctx.findElement(d);
|
|
1399
|
+
if (!element) return;
|
|
1400
|
+
const contentNode = d32.select(element.g).select("foreignObject").select("div").select("div").node();
|
|
1401
|
+
if (!contentNode) return;
|
|
1402
|
+
if (this.editOverlay) {
|
|
1403
|
+
try {
|
|
1404
|
+
this.editOverlay.contentEl.style.visibility = this.editOverlay.prevVisibility;
|
|
1405
|
+
this.editOverlay.wrap.remove();
|
|
1406
|
+
} catch {
|
|
1407
|
+
}
|
|
1408
|
+
this.editOverlay = void 0;
|
|
1409
|
+
}
|
|
1410
|
+
const originalHtml = d.content;
|
|
1411
|
+
this.editingNode = d;
|
|
1412
|
+
this._openEditOverlay(d, contentNode, {
|
|
1413
|
+
initialValue: contentNode.textContent || "",
|
|
1414
|
+
minWidth: 40,
|
|
1415
|
+
selectAll: true,
|
|
1416
|
+
onCancel: () => {
|
|
1417
|
+
d.content = originalHtml;
|
|
1418
|
+
void this.ctx.renderData();
|
|
1419
|
+
},
|
|
1420
|
+
onSave: (newHtml) => {
|
|
1421
|
+
var _a, _b;
|
|
1422
|
+
(_b = (_a = this.ctx.options).onNodeEdit) == null ? void 0 : _b.call(_a, d, newHtml);
|
|
1423
|
+
this.hideAddUI();
|
|
1424
|
+
void this.ctx.renderData().then(() => {
|
|
1425
|
+
if (!this.ctx.options.addable) return;
|
|
1426
|
+
const showPlus = () => {
|
|
1427
|
+
this.selectedNode = d;
|
|
1428
|
+
if (this.ctx.options.clickBorder) {
|
|
1429
|
+
this.ctx.g.selectAll(childSelector(SELECTOR_NODE$1)).classed("markmap-selected", (n) => n === d);
|
|
1430
|
+
}
|
|
1431
|
+
this.showAddUI(d);
|
|
1432
|
+
};
|
|
1433
|
+
if (this.ctx.options.duration > 0) {
|
|
1434
|
+
setTimeout(showPlus, this.ctx.options.duration);
|
|
1435
|
+
} else {
|
|
1436
|
+
showPlus();
|
|
1437
|
+
}
|
|
1438
|
+
});
|
|
1439
|
+
}
|
|
1440
|
+
});
|
|
1441
|
+
}
|
|
1442
|
+
/** Immediately discard any in-progress edit overlay without saving. */
|
|
1443
|
+
saveEdit() {
|
|
1444
|
+
if (this.editOverlay) {
|
|
1445
|
+
const { input, contentEl, prevVisibility, wrap } = this.editOverlay;
|
|
1446
|
+
input.remove();
|
|
1447
|
+
contentEl.style.visibility = prevVisibility;
|
|
1448
|
+
wrap.remove();
|
|
1449
|
+
this.editOverlay = void 0;
|
|
1450
|
+
}
|
|
1451
|
+
if (this.editingNode) this.editingNode = null;
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
const globalCSS = css;
|
|
1455
|
+
const SELECTOR_NODE = "g.markmap-node";
|
|
1456
|
+
const SELECTOR_LINK = "path.markmap-link";
|
|
1457
|
+
const SELECTOR_HIGHLIGHT = "g.markmap-highlight";
|
|
1458
|
+
const linkShape = d32.linkHorizontal();
|
|
1459
|
+
function minBy(numbers, by) {
|
|
1460
|
+
const index = d32.minIndex(numbers, by);
|
|
1461
|
+
return numbers[index];
|
|
1462
|
+
}
|
|
1463
|
+
function stopPropagation(e) {
|
|
1464
|
+
e.stopPropagation();
|
|
1465
|
+
}
|
|
1466
|
+
const refreshHook = new Hook();
|
|
1467
|
+
class Markmap {
|
|
1468
|
+
constructor(svg, opts) {
|
|
1469
|
+
this.options = { ...defaultOptions };
|
|
1470
|
+
this._disposeList = [];
|
|
1471
|
+
this.handleZoom = (e) => {
|
|
1472
|
+
const { transform } = e;
|
|
1473
|
+
this.g.attr("transform", transform);
|
|
1474
|
+
this._actions.repositionOverlays();
|
|
1475
|
+
};
|
|
1476
|
+
this.handlePan = (e) => {
|
|
1477
|
+
e.preventDefault();
|
|
1478
|
+
const transform = d32.zoomTransform(this.svg.node());
|
|
1479
|
+
const newTransform = transform.translate(
|
|
1480
|
+
-e.deltaX / transform.k,
|
|
1481
|
+
-e.deltaY / transform.k
|
|
1482
|
+
);
|
|
1483
|
+
this.svg.call(this.zoom.transform, newTransform);
|
|
1484
|
+
};
|
|
1485
|
+
this.handleClick = (e, d) => {
|
|
1486
|
+
let recursive = this.options.toggleRecursively;
|
|
1487
|
+
if (isMacintosh ? e.metaKey : e.ctrlKey) recursive = !recursive;
|
|
1488
|
+
this.toggleNode(d, recursive);
|
|
1489
|
+
};
|
|
1490
|
+
this.ensureView = this.ensureVisible;
|
|
1491
|
+
this.svg = svg.datum ? svg : d32.select(svg);
|
|
1492
|
+
this.styleNode = this.svg.append("style");
|
|
1493
|
+
this.zoom = d32.zoom().filter((event) => {
|
|
1494
|
+
if (this.options.scrollForPan) {
|
|
1495
|
+
if (event.type === "wheel") return event.ctrlKey && !event.button;
|
|
1496
|
+
}
|
|
1497
|
+
return (!event.ctrlKey || event.type === "wheel") && !event.button;
|
|
1498
|
+
}).on("zoom", this.handleZoom);
|
|
1499
|
+
this.setOptions(opts);
|
|
1500
|
+
this.state = {
|
|
1501
|
+
id: this.options.id || this.svg.attr("id") || getId(),
|
|
1502
|
+
rect: { x1: 0, y1: 0, x2: 0, y2: 0 }
|
|
1503
|
+
};
|
|
1504
|
+
this.g = this.svg.append("g");
|
|
1505
|
+
this.g.append("g").attr("class", "markmap-highlight");
|
|
1506
|
+
this._actions = new ActionManager(this);
|
|
1507
|
+
this.svg.on("click", () => {
|
|
1508
|
+
if (this._actions.selectedNode) {
|
|
1509
|
+
this._actions.selectedNode = null;
|
|
1510
|
+
if (this.options.clickBorder) {
|
|
1511
|
+
this.g.selectAll(childSelector(SELECTOR_NODE)).classed("markmap-selected", false);
|
|
1512
|
+
}
|
|
1513
|
+
this._actions.hideAddUI();
|
|
1514
|
+
}
|
|
1515
|
+
});
|
|
1516
|
+
const handleGlobalKeydown = (e) => {
|
|
1517
|
+
var _a;
|
|
1518
|
+
if (!this._actions.selectedNode) return;
|
|
1519
|
+
if (this._actions.editingNode || this._actions.editOverlay || this._actions.addInputUI) return;
|
|
1520
|
+
const tag = (_a = document.activeElement) == null ? void 0 : _a.tagName;
|
|
1521
|
+
if (tag === "INPUT" || tag === "TEXTAREA") return;
|
|
1522
|
+
if (e.key === "Tab" && this.options.addable) {
|
|
1523
|
+
e.preventDefault();
|
|
1524
|
+
e.stopPropagation();
|
|
1525
|
+
this._actions.showAddInput(this._actions.selectedNode);
|
|
1526
|
+
} else if (e.key === "Enter" && this.options.addable) {
|
|
1527
|
+
e.preventDefault();
|
|
1528
|
+
e.stopPropagation();
|
|
1529
|
+
this._actions.showAddSiblingInput(this._actions.selectedNode);
|
|
1530
|
+
} else if ((e.key === "Delete" || e.key === "Backspace") && this.options.deletable) {
|
|
1531
|
+
e.preventDefault();
|
|
1532
|
+
e.stopPropagation();
|
|
1533
|
+
this._actions.deleteNode(this._actions.selectedNode);
|
|
1534
|
+
}
|
|
1535
|
+
};
|
|
1536
|
+
document.addEventListener("keydown", handleGlobalKeydown);
|
|
1537
|
+
this._disposeList.push(() => document.removeEventListener("keydown", handleGlobalKeydown));
|
|
1538
|
+
this._observer = new ResizeObserver(
|
|
1539
|
+
debounce(() => {
|
|
1540
|
+
if (this._actions.editingNode) return;
|
|
1541
|
+
this.renderData();
|
|
1542
|
+
}, 100)
|
|
1543
|
+
);
|
|
1544
|
+
this._disposeList.push(
|
|
1545
|
+
refreshHook.tap(() => {
|
|
1546
|
+
this.setData();
|
|
1547
|
+
}),
|
|
1548
|
+
() => this._actions.hideAddUI(),
|
|
1549
|
+
() => this._observer.disconnect()
|
|
1550
|
+
);
|
|
1551
|
+
}
|
|
1552
|
+
getStyleContent() {
|
|
1553
|
+
const { style } = this.options;
|
|
1554
|
+
const { id } = this.state;
|
|
1555
|
+
const styleText = typeof style === "function" ? style(id) : "";
|
|
1556
|
+
return [this.options.embedGlobalCSS && css, styleText].filter(Boolean).join("\n");
|
|
1557
|
+
}
|
|
1558
|
+
updateStyle() {
|
|
1559
|
+
const baseClass = addClass("", "markmap", this.state.id);
|
|
1560
|
+
const collapseClass = this.options.collapseOnHover ? "markmap-collapse-on-hover" : "";
|
|
1561
|
+
this.svg.attr("class", [baseClass, collapseClass].filter(Boolean).join(" "));
|
|
1562
|
+
const style = this.getStyleContent();
|
|
1563
|
+
this.styleNode.text(style);
|
|
1564
|
+
}
|
|
1565
|
+
async toggleNode(data, recursive = false) {
|
|
1566
|
+
var _a, _b;
|
|
1567
|
+
const fold = ((_a = data.payload) == null ? void 0 : _a.fold) ? 0 : 1;
|
|
1568
|
+
if (recursive) {
|
|
1569
|
+
walkTree(data, (item, next) => {
|
|
1570
|
+
item.payload = {
|
|
1571
|
+
...item.payload,
|
|
1572
|
+
fold
|
|
1573
|
+
};
|
|
1574
|
+
next();
|
|
1575
|
+
});
|
|
1576
|
+
} else {
|
|
1577
|
+
data.payload = {
|
|
1578
|
+
...data.payload,
|
|
1579
|
+
fold: ((_b = data.payload) == null ? void 0 : _b.fold) ? 0 : 1
|
|
1580
|
+
};
|
|
1581
|
+
}
|
|
1582
|
+
await this.renderData(data);
|
|
1583
|
+
}
|
|
1584
|
+
_initializeData(node) {
|
|
1585
|
+
let nodeId = 0;
|
|
1586
|
+
const { color, initialExpandLevel } = this.options;
|
|
1587
|
+
let foldRecursively = 0;
|
|
1588
|
+
let depth = 0;
|
|
1589
|
+
walkTree(node, (item, next, parent) => {
|
|
1590
|
+
var _a, _b, _c, _d;
|
|
1591
|
+
depth += 1;
|
|
1592
|
+
item.children = (_a = item.children) == null ? void 0 : _a.map((child) => ({ ...child }));
|
|
1593
|
+
nodeId += 1;
|
|
1594
|
+
item.state = {
|
|
1595
|
+
...item.state,
|
|
1596
|
+
depth,
|
|
1597
|
+
id: nodeId,
|
|
1598
|
+
rect: {
|
|
1599
|
+
x: 0,
|
|
1600
|
+
y: 0,
|
|
1601
|
+
width: 0,
|
|
1602
|
+
height: 0
|
|
1603
|
+
},
|
|
1604
|
+
size: [0, 0]
|
|
1605
|
+
};
|
|
1606
|
+
item.state.key = [(_b = parent == null ? void 0 : parent.state) == null ? void 0 : _b.id, item.state.id].filter(Boolean).join(".") + simpleHash(item.content);
|
|
1607
|
+
item.state.path = [(_c = parent == null ? void 0 : parent.state) == null ? void 0 : _c.path, item.state.id].filter(Boolean).join(".");
|
|
1608
|
+
color(item);
|
|
1609
|
+
const isFoldRecursively = ((_d = item.payload) == null ? void 0 : _d.fold) === 2;
|
|
1610
|
+
if (isFoldRecursively) {
|
|
1611
|
+
foldRecursively += 1;
|
|
1612
|
+
} else if (foldRecursively || initialExpandLevel >= 0 && item.state.depth >= initialExpandLevel) {
|
|
1613
|
+
item.payload = { ...item.payload, fold: 1 };
|
|
1614
|
+
}
|
|
1615
|
+
next();
|
|
1616
|
+
if (isFoldRecursively) foldRecursively -= 1;
|
|
1617
|
+
depth -= 1;
|
|
1618
|
+
});
|
|
1619
|
+
return node;
|
|
1620
|
+
}
|
|
1621
|
+
_relayout() {
|
|
1622
|
+
if (!this.state.data) return;
|
|
1623
|
+
this.g.selectAll(childSelector(SELECTOR_NODE)).selectAll(
|
|
1624
|
+
childSelector("foreignObject")
|
|
1625
|
+
).each(function(d) {
|
|
1626
|
+
var _a;
|
|
1627
|
+
const el = (_a = this.firstChild) == null ? void 0 : _a.firstChild;
|
|
1628
|
+
const newSize = [el.scrollWidth, el.scrollHeight];
|
|
1629
|
+
d.state.size = newSize;
|
|
1630
|
+
});
|
|
1631
|
+
const { lineWidth, paddingX, spacingHorizontal, spacingVertical } = this.options;
|
|
1632
|
+
const getEffectiveSH = (node) => {
|
|
1633
|
+
var _a;
|
|
1634
|
+
if ((_a = node.payload) == null ? void 0 : _a.fold) return spacingHorizontal;
|
|
1635
|
+
const childCount = (node.children || []).length;
|
|
1636
|
+
return childCount === 1 ? Math.round(spacingHorizontal / 2) : spacingHorizontal;
|
|
1637
|
+
};
|
|
1638
|
+
const layout = flextree({}).children((d) => {
|
|
1639
|
+
var _a;
|
|
1640
|
+
if (!((_a = d.payload) == null ? void 0 : _a.fold)) return d.children;
|
|
1641
|
+
}).nodeSize((node) => {
|
|
1642
|
+
const [width, height] = node.data.state.size;
|
|
1643
|
+
const sh = getEffectiveSH(node.data);
|
|
1644
|
+
return [height, width + (width ? paddingX * 2 : 0) + sh];
|
|
1645
|
+
}).spacing((a, b) => {
|
|
1646
|
+
return (a.parent === b.parent ? spacingVertical : spacingVertical * 2) + lineWidth(a.data);
|
|
1647
|
+
});
|
|
1648
|
+
const tree = layout.hierarchy(this.state.data);
|
|
1649
|
+
layout(tree);
|
|
1650
|
+
const fnodes = tree.descendants();
|
|
1651
|
+
fnodes.forEach((fnode) => {
|
|
1652
|
+
const node = fnode.data;
|
|
1653
|
+
const sh = getEffectiveSH(node);
|
|
1654
|
+
node.state.rect = {
|
|
1655
|
+
x: fnode.y,
|
|
1656
|
+
y: fnode.x - fnode.xSize / 2,
|
|
1657
|
+
width: fnode.ySize - sh,
|
|
1658
|
+
height: fnode.xSize
|
|
1659
|
+
};
|
|
1660
|
+
});
|
|
1661
|
+
this.state.rect = {
|
|
1662
|
+
x1: d32.min(fnodes, (fnode) => fnode.data.state.rect.x) || 0,
|
|
1663
|
+
y1: d32.min(fnodes, (fnode) => fnode.data.state.rect.y) || 0,
|
|
1664
|
+
x2: d32.max(
|
|
1665
|
+
fnodes,
|
|
1666
|
+
(fnode) => fnode.data.state.rect.x + fnode.data.state.rect.width
|
|
1667
|
+
) || 0,
|
|
1668
|
+
y2: d32.max(
|
|
1669
|
+
fnodes,
|
|
1670
|
+
(fnode) => fnode.data.state.rect.y + fnode.data.state.rect.height
|
|
1671
|
+
) || 0
|
|
1672
|
+
};
|
|
1673
|
+
}
|
|
1674
|
+
setOptions(opts) {
|
|
1675
|
+
const modePreset = (opts == null ? void 0 : opts.mode) === "display" ? { editable: false, addable: false, deletable: false, collapseOnHover: false, hoverBorder: false, clickBorder: false } : (opts == null ? void 0 : opts.mode) === "editable" ? { editable: true, addable: true, deletable: true, collapseOnHover: true, hoverBorder: true, clickBorder: true } : {};
|
|
1676
|
+
this.options = {
|
|
1677
|
+
...this.options,
|
|
1678
|
+
...modePreset,
|
|
1679
|
+
...opts
|
|
1680
|
+
};
|
|
1681
|
+
if (this.options.zoom) {
|
|
1682
|
+
this.svg.call(this.zoom);
|
|
1683
|
+
} else {
|
|
1684
|
+
this.svg.on(".zoom", null);
|
|
1685
|
+
}
|
|
1686
|
+
if (this.options.pan) {
|
|
1687
|
+
this.svg.on("wheel", this.handlePan);
|
|
1688
|
+
} else {
|
|
1689
|
+
this.svg.on("wheel", null);
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
async setData(data, opts) {
|
|
1693
|
+
if (opts) this.setOptions(opts);
|
|
1694
|
+
if (data) this.state.data = this._initializeData(data);
|
|
1695
|
+
if (!this.state.data) return;
|
|
1696
|
+
this.updateStyle();
|
|
1697
|
+
await this.renderData();
|
|
1698
|
+
}
|
|
1699
|
+
getData(pure) {
|
|
1700
|
+
const data = this.state.data;
|
|
1701
|
+
if (!data) return;
|
|
1702
|
+
if (!pure) return data;
|
|
1703
|
+
const toPure = (node) => ({
|
|
1704
|
+
content: node.content,
|
|
1705
|
+
payload: node.payload,
|
|
1706
|
+
children: (node.children || []).map(toPure)
|
|
1707
|
+
});
|
|
1708
|
+
return toPure(data);
|
|
1709
|
+
}
|
|
1710
|
+
async setHighlight(node) {
|
|
1711
|
+
this.state.highlight = node || void 0;
|
|
1712
|
+
await this.renderData();
|
|
1713
|
+
}
|
|
1714
|
+
_getHighlightRect(highlight) {
|
|
1715
|
+
const svgNode = this.svg.node();
|
|
1716
|
+
const transform = d32.zoomTransform(svgNode);
|
|
1717
|
+
const padding = 4 / transform.k;
|
|
1718
|
+
const rect = {
|
|
1719
|
+
...highlight.state.rect
|
|
1720
|
+
};
|
|
1721
|
+
rect.x -= padding;
|
|
1722
|
+
rect.y -= padding;
|
|
1723
|
+
rect.width += 2 * padding;
|
|
1724
|
+
rect.height += 2 * padding;
|
|
1725
|
+
return rect;
|
|
1726
|
+
}
|
|
1727
|
+
async renderData(originData) {
|
|
1728
|
+
const { paddingX, autoFit, color, maxWidth, lineWidth } = this.options;
|
|
1729
|
+
const rootNode = this.state.data;
|
|
1730
|
+
if (!rootNode) return;
|
|
1731
|
+
const nodeMap = {};
|
|
1732
|
+
const parentMap = {};
|
|
1733
|
+
const nodes = [];
|
|
1734
|
+
walkTree(rootNode, (item, next, parent) => {
|
|
1735
|
+
var _a;
|
|
1736
|
+
if (!((_a = item.payload) == null ? void 0 : _a.fold)) next();
|
|
1737
|
+
nodeMap[item.state.id] = item;
|
|
1738
|
+
if (parent) parentMap[item.state.id] = parent.state.id;
|
|
1739
|
+
nodes.push(item);
|
|
1740
|
+
});
|
|
1741
|
+
const originMap = {};
|
|
1742
|
+
const sourceRectMap = {};
|
|
1743
|
+
const setOriginNode = (originNode) => {
|
|
1744
|
+
if (!originNode || originMap[originNode.state.id]) return;
|
|
1745
|
+
walkTree(originNode, (item, next) => {
|
|
1746
|
+
originMap[item.state.id] = originNode.state.id;
|
|
1747
|
+
next();
|
|
1748
|
+
});
|
|
1749
|
+
};
|
|
1750
|
+
const getOriginSourceRect = (node) => {
|
|
1751
|
+
const rect = sourceRectMap[originMap[node.state.id]];
|
|
1752
|
+
return rect || rootNode.state.rect;
|
|
1753
|
+
};
|
|
1754
|
+
const getOriginTargetRect = (node) => (nodeMap[originMap[node.state.id]] || rootNode).state.rect;
|
|
1755
|
+
sourceRectMap[rootNode.state.id] = rootNode.state.rect;
|
|
1756
|
+
if (originData) setOriginNode(originData);
|
|
1757
|
+
let { highlight } = this.state;
|
|
1758
|
+
if (highlight && !nodeMap[highlight.state.id]) highlight = void 0;
|
|
1759
|
+
let highlightNodes = this.g.selectAll(childSelector(SELECTOR_HIGHLIGHT)).selectAll(childSelector("rect")).data(highlight ? [this._getHighlightRect(highlight)] : []).join("rect").attr("x", (d) => d.x).attr("y", (d) => d.y).attr("width", (d) => d.width).attr("height", (d) => d.height);
|
|
1760
|
+
const mmG = this.g.selectAll(childSelector(SELECTOR_NODE)).each((d) => {
|
|
1761
|
+
sourceRectMap[d.state.id] = d.state.rect;
|
|
1762
|
+
}).data(nodes, (d) => d.state.key);
|
|
1763
|
+
const mmGEnter = mmG.enter().append("g").attr("data-depth", (d) => d.state.depth).attr("data-path", (d) => d.state.path).each((d) => {
|
|
1764
|
+
setOriginNode(nodeMap[parentMap[d.state.id]]);
|
|
1765
|
+
});
|
|
1766
|
+
const mmGExit = mmG.exit().each((d) => {
|
|
1767
|
+
setOriginNode(nodeMap[parentMap[d.state.id]]);
|
|
1768
|
+
});
|
|
1769
|
+
const mmGMerge = mmG.merge(mmGEnter).attr(
|
|
1770
|
+
"class",
|
|
1771
|
+
(d) => {
|
|
1772
|
+
var _a;
|
|
1773
|
+
return ["markmap-node", ((_a = d.payload) == null ? void 0 : _a.fold) && "markmap-fold"].filter(Boolean).join(" ");
|
|
1774
|
+
}
|
|
1775
|
+
);
|
|
1776
|
+
const mmLine = mmGMerge.selectAll(childSelector("line")).data(
|
|
1777
|
+
(d) => [d],
|
|
1778
|
+
(d) => d.state.key
|
|
1779
|
+
);
|
|
1780
|
+
const mmLineEnter = mmLine.enter().append("line").attr("stroke", (d) => color(d)).attr("stroke-width", 0);
|
|
1781
|
+
const mmLineMerge = mmLine.merge(mmLineEnter);
|
|
1782
|
+
const mmCircle = mmGMerge.selectAll(childSelector("circle")).data(
|
|
1783
|
+
(d) => {
|
|
1784
|
+
var _a;
|
|
1785
|
+
return ((_a = d.children) == null ? void 0 : _a.length) ? [d] : [];
|
|
1786
|
+
},
|
|
1787
|
+
(d) => d.state.key
|
|
1788
|
+
);
|
|
1789
|
+
this.transition(mmCircle.exit()).attr("r", 0).attr("stroke-width", 0).remove();
|
|
1790
|
+
const mmCircleEnter = mmCircle.enter().append("circle").attr("stroke-width", 0).attr("r", 0).on("click", (e, d) => this.handleClick(e, d)).on("mousedown", stopPropagation);
|
|
1791
|
+
const mmCircleMerge = mmCircleEnter.merge(mmCircle).attr("stroke", (d) => color(d)).attr(
|
|
1792
|
+
"fill",
|
|
1793
|
+
(d) => {
|
|
1794
|
+
var _a;
|
|
1795
|
+
return ((_a = d.payload) == null ? void 0 : _a.fold) && d.children ? color(d) : "var(--markmap-circle-open-bg)";
|
|
1796
|
+
}
|
|
1797
|
+
);
|
|
1798
|
+
const observer = this._observer;
|
|
1799
|
+
const mmFo = mmGMerge.selectAll(childSelector("foreignObject")).data(
|
|
1800
|
+
(d) => [d],
|
|
1801
|
+
(d) => d.state.key
|
|
1802
|
+
);
|
|
1803
|
+
const mmFoEnter = mmFo.enter().append("foreignObject").attr("class", "markmap-foreign").attr("x", paddingX).attr("y", 0).style("opacity", 0).on("mousedown", stopPropagation).on("dblclick", (e, d) => {
|
|
1804
|
+
e.preventDefault();
|
|
1805
|
+
e.stopPropagation();
|
|
1806
|
+
e.stopImmediatePropagation();
|
|
1807
|
+
this._actions.handleEdit(e, d);
|
|
1808
|
+
});
|
|
1809
|
+
mmFoEnter.append("xhtml:div").append("xhtml:div").html((d) => d.content).attr("xmlns", "http://www.w3.org/1999/xhtml");
|
|
1810
|
+
mmFoEnter.each(function() {
|
|
1811
|
+
var _a;
|
|
1812
|
+
const el = (_a = this.firstChild) == null ? void 0 : _a.firstChild;
|
|
1813
|
+
observer.observe(el);
|
|
1814
|
+
});
|
|
1815
|
+
const mmFoExit = mmGExit.selectAll(
|
|
1816
|
+
childSelector("foreignObject")
|
|
1817
|
+
);
|
|
1818
|
+
mmFoExit.each(function() {
|
|
1819
|
+
var _a;
|
|
1820
|
+
const el = (_a = this.firstChild) == null ? void 0 : _a.firstChild;
|
|
1821
|
+
observer.unobserve(el);
|
|
1822
|
+
});
|
|
1823
|
+
const mmFoMerge = mmFoEnter.merge(mmFo);
|
|
1824
|
+
mmFoMerge.on("dblclick", (e, d) => {
|
|
1825
|
+
e.preventDefault();
|
|
1826
|
+
e.stopPropagation();
|
|
1827
|
+
e.stopImmediatePropagation();
|
|
1828
|
+
this._actions.handleEdit(e, d);
|
|
1829
|
+
});
|
|
1830
|
+
mmFoMerge.on("mouseenter", (e, d) => {
|
|
1831
|
+
var _a, _b;
|
|
1832
|
+
if (!this.options.hoverBorder) return;
|
|
1833
|
+
const el = this.findElement(d);
|
|
1834
|
+
if (!el) return;
|
|
1835
|
+
const contentDiv = (_b = (_a = d32.select(el.g).select("foreignObject").node()) == null ? void 0 : _a.firstChild) == null ? void 0 : _b.firstChild;
|
|
1836
|
+
if (!contentDiv) return;
|
|
1837
|
+
const rect = contentDiv.getBoundingClientRect();
|
|
1838
|
+
if (e.clientX >= rect.left && e.clientX <= rect.right && e.clientY >= rect.top && e.clientY <= rect.bottom) {
|
|
1839
|
+
d32.select(el.g).classed("markmap-node-hovered", true);
|
|
1840
|
+
}
|
|
1841
|
+
});
|
|
1842
|
+
mmFoMerge.on("mousemove", (e, d) => {
|
|
1843
|
+
var _a, _b;
|
|
1844
|
+
if (!this.options.hoverBorder) return;
|
|
1845
|
+
const el = this.findElement(d);
|
|
1846
|
+
if (!el) return;
|
|
1847
|
+
const contentDiv = (_b = (_a = d32.select(el.g).select("foreignObject").node()) == null ? void 0 : _a.firstChild) == null ? void 0 : _b.firstChild;
|
|
1848
|
+
if (!contentDiv) return;
|
|
1849
|
+
const rect = contentDiv.getBoundingClientRect();
|
|
1850
|
+
const inside = e.clientX >= rect.left && e.clientX <= rect.right && e.clientY >= rect.top && e.clientY <= rect.bottom;
|
|
1851
|
+
d32.select(el.g).classed("markmap-node-hovered", inside);
|
|
1852
|
+
});
|
|
1853
|
+
mmFoMerge.on("mouseleave", (_e, d) => {
|
|
1854
|
+
if (!this.options.hoverBorder) return;
|
|
1855
|
+
const el = this.findElement(d);
|
|
1856
|
+
if (el) d32.select(el.g).classed("markmap-node-hovered", false);
|
|
1857
|
+
});
|
|
1858
|
+
mmFoMerge.on("click", (e, d) => {
|
|
1859
|
+
var _a, _b;
|
|
1860
|
+
if (this._actions.editingNode || this._actions.editOverlay) return;
|
|
1861
|
+
e.stopPropagation();
|
|
1862
|
+
const el = this.findElement(d);
|
|
1863
|
+
const contentDiv = el ? (_b = (_a = d32.select(el.g).select("foreignObject").node()) == null ? void 0 : _a.firstChild) == null ? void 0 : _b.firstChild : null;
|
|
1864
|
+
const clickedText = (() => {
|
|
1865
|
+
if (!contentDiv) return false;
|
|
1866
|
+
const rect = contentDiv.getBoundingClientRect();
|
|
1867
|
+
return e.clientX >= rect.left && e.clientX <= rect.right && e.clientY >= rect.top && e.clientY <= rect.bottom;
|
|
1868
|
+
})();
|
|
1869
|
+
if (!clickedText) return;
|
|
1870
|
+
this._actions.selectedNode = d;
|
|
1871
|
+
if (this.options.clickBorder) {
|
|
1872
|
+
this.g.selectAll(childSelector(SELECTOR_NODE)).classed("markmap-selected", (n) => n === d);
|
|
1873
|
+
}
|
|
1874
|
+
if (this.options.addable) {
|
|
1875
|
+
this._actions.showAddUI(d);
|
|
1876
|
+
}
|
|
1877
|
+
});
|
|
1878
|
+
mmFoMerge.select("div").select("div").html((d) => d.content);
|
|
1879
|
+
const links = nodes.flatMap(
|
|
1880
|
+
(node) => {
|
|
1881
|
+
var _a;
|
|
1882
|
+
return ((_a = node.payload) == null ? void 0 : _a.fold) ? [] : node.children.map((child) => ({ source: node, target: child }));
|
|
1883
|
+
}
|
|
1884
|
+
);
|
|
1885
|
+
const mmPath = this.g.selectAll(childSelector(SELECTOR_LINK)).data(links, (d) => d.target.state.key);
|
|
1886
|
+
const mmPathExit = mmPath.exit();
|
|
1887
|
+
const mmPathEnter = mmPath.enter().insert("path", "g").attr("class", "markmap-link").attr("data-depth", (d) => d.target.state.depth).attr("data-path", (d) => d.target.state.path).attr("d", (d) => {
|
|
1888
|
+
const originRect = getOriginSourceRect(d.target);
|
|
1889
|
+
const pathOrigin = [
|
|
1890
|
+
originRect.x + originRect.width,
|
|
1891
|
+
originRect.y + originRect.height
|
|
1892
|
+
];
|
|
1893
|
+
return linkShape({ source: pathOrigin, target: pathOrigin });
|
|
1894
|
+
}).attr("stroke-width", 0);
|
|
1895
|
+
const mmPathMerge = mmPathEnter.merge(mmPath);
|
|
1896
|
+
this.svg.style(
|
|
1897
|
+
"--markmap-max-width",
|
|
1898
|
+
maxWidth ? `${maxWidth}px` : null
|
|
1899
|
+
);
|
|
1900
|
+
await new Promise(requestAnimationFrame);
|
|
1901
|
+
this._relayout();
|
|
1902
|
+
highlightNodes = highlightNodes.data(highlight ? [this._getHighlightRect(highlight)] : []).join("rect");
|
|
1903
|
+
this.transition(highlightNodes).attr("x", (d) => d.x).attr("y", (d) => d.y).attr("width", (d) => d.width).attr("height", (d) => d.height);
|
|
1904
|
+
mmGEnter.attr("transform", (d) => {
|
|
1905
|
+
const originRect = getOriginSourceRect(d);
|
|
1906
|
+
return `translate(${originRect.x + originRect.width - d.state.rect.width},${originRect.y + originRect.height - d.state.rect.height})`;
|
|
1907
|
+
});
|
|
1908
|
+
this.transition(mmGExit).attr("transform", (d) => {
|
|
1909
|
+
const targetRect = getOriginTargetRect(d);
|
|
1910
|
+
const targetX = targetRect.x + targetRect.width - d.state.rect.width;
|
|
1911
|
+
const targetY = targetRect.y + targetRect.height - d.state.rect.height;
|
|
1912
|
+
return `translate(${targetX},${targetY})`;
|
|
1913
|
+
}).remove();
|
|
1914
|
+
this.transition(mmGMerge).attr(
|
|
1915
|
+
"transform",
|
|
1916
|
+
(d) => `translate(${d.state.rect.x},${d.state.rect.y})`
|
|
1917
|
+
);
|
|
1918
|
+
const mmLineExit = mmGExit.selectAll(
|
|
1919
|
+
childSelector("line")
|
|
1920
|
+
);
|
|
1921
|
+
this.transition(mmLineExit).attr("x1", (d) => d.state.rect.width).attr("stroke-width", 0);
|
|
1922
|
+
mmLineEnter.attr("x1", (d) => d.state.rect.width).attr("x2", (d) => d.state.rect.width);
|
|
1923
|
+
mmLineMerge.attr("y1", (d) => d.state.rect.height + lineWidth(d) / 2).attr("y2", (d) => d.state.rect.height + lineWidth(d) / 2);
|
|
1924
|
+
this.transition(mmLineMerge).attr("x1", -1).attr("x2", (d) => d.state.rect.width + 2).attr("stroke", (d) => color(d)).attr("stroke-width", lineWidth);
|
|
1925
|
+
const mmCircleExit = mmGExit.selectAll(
|
|
1926
|
+
childSelector("circle")
|
|
1927
|
+
);
|
|
1928
|
+
this.transition(mmCircleExit).attr("r", 0).attr("stroke-width", 0);
|
|
1929
|
+
mmCircleMerge.attr("cx", (d) => d.state.rect.width + 4).attr("cy", (d) => d.state.rect.height + lineWidth(d) / 2);
|
|
1930
|
+
this.transition(mmCircleMerge).attr("r", 6).attr("stroke-width", 1.5);
|
|
1931
|
+
this.transition(mmFoExit).style("opacity", 0);
|
|
1932
|
+
mmFoMerge.attr("width", (d) => Math.max(0, d.state.rect.width - paddingX * 2 + 16)).attr("height", (d) => d.state.rect.height);
|
|
1933
|
+
this.transition(mmFoMerge).style("opacity", 1);
|
|
1934
|
+
this.transition(mmPathExit).attr("d", (d) => {
|
|
1935
|
+
const targetRect = getOriginTargetRect(d.target);
|
|
1936
|
+
const pathTarget = [
|
|
1937
|
+
targetRect.x + targetRect.width,
|
|
1938
|
+
targetRect.y + targetRect.height + lineWidth(d.target) / 2
|
|
1939
|
+
];
|
|
1940
|
+
return linkShape({ source: pathTarget, target: pathTarget });
|
|
1941
|
+
}).attr("stroke-width", 0).remove();
|
|
1942
|
+
this.transition(mmPathMerge).attr("stroke", (d) => color(d.target)).attr("stroke-width", (d) => lineWidth(d.target)).attr("d", (d) => {
|
|
1943
|
+
const origSource = d.source;
|
|
1944
|
+
const origTarget = d.target;
|
|
1945
|
+
const source = [
|
|
1946
|
+
origSource.state.rect.x + origSource.state.rect.width,
|
|
1947
|
+
origSource.state.rect.y + origSource.state.rect.height + lineWidth(origSource) / 2
|
|
1948
|
+
];
|
|
1949
|
+
const target = [
|
|
1950
|
+
origTarget.state.rect.x,
|
|
1951
|
+
origTarget.state.rect.y + origTarget.state.rect.height + lineWidth(origTarget) / 2
|
|
1952
|
+
];
|
|
1953
|
+
return linkShape({ source, target });
|
|
1954
|
+
});
|
|
1955
|
+
if (autoFit) this.fit();
|
|
1956
|
+
}
|
|
1957
|
+
transition(sel) {
|
|
1958
|
+
const { duration } = this.options;
|
|
1959
|
+
return sel.transition().duration(duration);
|
|
1960
|
+
}
|
|
1961
|
+
/**
|
|
1962
|
+
* Fit the content to the viewport.
|
|
1963
|
+
*/
|
|
1964
|
+
async fit(maxScale = this.options.maxInitialScale) {
|
|
1965
|
+
const svgNode = this.svg.node();
|
|
1966
|
+
const { width: offsetWidth, height: offsetHeight } = svgNode.getBoundingClientRect();
|
|
1967
|
+
const { fitRatio } = this.options;
|
|
1968
|
+
const { x1, y1, x2, y2 } = this.state.rect;
|
|
1969
|
+
const naturalWidth = x2 - x1;
|
|
1970
|
+
const naturalHeight = y2 - y1;
|
|
1971
|
+
const scale = Math.min(
|
|
1972
|
+
offsetWidth / naturalWidth * fitRatio,
|
|
1973
|
+
offsetHeight / naturalHeight * fitRatio,
|
|
1974
|
+
maxScale
|
|
1975
|
+
);
|
|
1976
|
+
const initialZoom = d32.zoomIdentity.translate(
|
|
1977
|
+
(offsetWidth - naturalWidth * scale) / 2 - x1 * scale,
|
|
1978
|
+
(offsetHeight - naturalHeight * scale) / 2 - y1 * scale
|
|
1979
|
+
).scale(scale);
|
|
1980
|
+
return this.transition(this.svg).call(this.zoom.transform, initialZoom).end().catch(noop);
|
|
1981
|
+
}
|
|
1982
|
+
findElement(node) {
|
|
1983
|
+
let result;
|
|
1984
|
+
this.g.selectAll(childSelector(SELECTOR_NODE)).each(function walk(d) {
|
|
1985
|
+
if (d === node) {
|
|
1986
|
+
result = {
|
|
1987
|
+
data: d,
|
|
1988
|
+
g: this
|
|
1989
|
+
};
|
|
1990
|
+
}
|
|
1991
|
+
});
|
|
1992
|
+
return result;
|
|
1993
|
+
}
|
|
1994
|
+
/**
|
|
1995
|
+
* Pan the content to make the provided node visible in the viewport.
|
|
1996
|
+
*/
|
|
1997
|
+
async ensureVisible(node, padding) {
|
|
1998
|
+
var _a;
|
|
1999
|
+
const itemData = (_a = this.findElement(node)) == null ? void 0 : _a.data;
|
|
2000
|
+
if (!itemData) return;
|
|
2001
|
+
const svgNode = this.svg.node();
|
|
2002
|
+
const relRect = svgNode.getBoundingClientRect();
|
|
2003
|
+
const transform = d32.zoomTransform(svgNode);
|
|
2004
|
+
const [left, right] = [
|
|
2005
|
+
itemData.state.rect.x,
|
|
2006
|
+
itemData.state.rect.x + itemData.state.rect.width + 2
|
|
2007
|
+
].map((x) => x * transform.k + transform.x);
|
|
2008
|
+
const [top, bottom] = [
|
|
2009
|
+
itemData.state.rect.y,
|
|
2010
|
+
itemData.state.rect.y + itemData.state.rect.height
|
|
2011
|
+
].map((y) => y * transform.k + transform.y);
|
|
2012
|
+
const pd = {
|
|
2013
|
+
left: 0,
|
|
2014
|
+
right: 0,
|
|
2015
|
+
top: 0,
|
|
2016
|
+
bottom: 0,
|
|
2017
|
+
...padding
|
|
2018
|
+
};
|
|
2019
|
+
const dxs = [pd.left - left, relRect.width - pd.right - right];
|
|
2020
|
+
const dys = [pd.top - top, relRect.height - pd.bottom - bottom];
|
|
2021
|
+
const dx = dxs[0] * dxs[1] > 0 ? minBy(dxs, Math.abs) / transform.k : 0;
|
|
2022
|
+
const dy = dys[0] * dys[1] > 0 ? minBy(dys, Math.abs) / transform.k : 0;
|
|
2023
|
+
if (dx || dy) {
|
|
2024
|
+
const newTransform = transform.translate(dx, dy);
|
|
2025
|
+
return this.transition(this.svg).call(this.zoom.transform, newTransform).end().catch(noop);
|
|
2026
|
+
}
|
|
2027
|
+
}
|
|
2028
|
+
async centerNode(node, padding) {
|
|
2029
|
+
var _a;
|
|
2030
|
+
const itemData = (_a = this.findElement(node)) == null ? void 0 : _a.data;
|
|
2031
|
+
if (!itemData) return;
|
|
2032
|
+
const svgNode = this.svg.node();
|
|
2033
|
+
const relRect = svgNode.getBoundingClientRect();
|
|
2034
|
+
const transform = d32.zoomTransform(svgNode);
|
|
2035
|
+
const x = (itemData.state.rect.x + itemData.state.rect.width / 2) * transform.k + transform.x;
|
|
2036
|
+
const y = (itemData.state.rect.y + itemData.state.rect.height / 2) * transform.k + transform.y;
|
|
2037
|
+
const pd = {
|
|
2038
|
+
left: 0,
|
|
2039
|
+
right: 0,
|
|
2040
|
+
top: 0,
|
|
2041
|
+
bottom: 0,
|
|
2042
|
+
...padding
|
|
2043
|
+
};
|
|
2044
|
+
const cx = (pd.left + relRect.width - pd.right) / 2;
|
|
2045
|
+
const cy = (pd.top + relRect.height - pd.bottom) / 2;
|
|
2046
|
+
const dx = (cx - x) / transform.k;
|
|
2047
|
+
const dy = (cy - y) / transform.k;
|
|
2048
|
+
if (dx || dy) {
|
|
2049
|
+
const newTransform = transform.translate(dx, dy);
|
|
2050
|
+
return this.transition(this.svg).call(this.zoom.transform, newTransform).end().catch(noop);
|
|
2051
|
+
}
|
|
2052
|
+
}
|
|
2053
|
+
/**
|
|
2054
|
+
* Scale content with it pinned at the center of the viewport.
|
|
2055
|
+
*/
|
|
2056
|
+
async rescale(scale) {
|
|
2057
|
+
const svgNode = this.svg.node();
|
|
2058
|
+
const { width: offsetWidth, height: offsetHeight } = svgNode.getBoundingClientRect();
|
|
2059
|
+
const halfWidth = offsetWidth / 2;
|
|
2060
|
+
const halfHeight = offsetHeight / 2;
|
|
2061
|
+
const transform = d32.zoomTransform(svgNode);
|
|
2062
|
+
const newTransform = transform.translate(
|
|
2063
|
+
(halfWidth - transform.x) * (1 - scale) / transform.k,
|
|
2064
|
+
(halfHeight - transform.y) * (1 - scale) / transform.k
|
|
2065
|
+
).scale(scale);
|
|
2066
|
+
return this.transition(this.svg).call(this.zoom.transform, newTransform).end().catch(noop);
|
|
2067
|
+
}
|
|
2068
|
+
destroy() {
|
|
2069
|
+
this.svg.on(".zoom", null);
|
|
2070
|
+
this.svg.html(null);
|
|
2071
|
+
this._disposeList.forEach((fn) => {
|
|
2072
|
+
fn();
|
|
2073
|
+
});
|
|
2074
|
+
}
|
|
2075
|
+
static create(svg, opts, data = null) {
|
|
2076
|
+
const mm = new Markmap(svg, opts);
|
|
2077
|
+
if (data) {
|
|
2078
|
+
mm.setData(data).then(() => {
|
|
2079
|
+
mm.fit();
|
|
2080
|
+
});
|
|
2081
|
+
}
|
|
2082
|
+
return mm;
|
|
2083
|
+
}
|
|
2084
|
+
}
|
|
2085
|
+
function htmlInlineToMarkdown(html) {
|
|
2086
|
+
if (!html) return "";
|
|
2087
|
+
let md = html;
|
|
2088
|
+
md = md.replace(
|
|
2089
|
+
/<img(?=[^>]*?\bsrc="([^"]*)")(?=[^>]*?\balt="([^"]*)")(?=[^>]*?\btitle="([^"]*)")?[^>]*?\/?>/gi,
|
|
2090
|
+
(_, src, alt, title) => title ? `` : ``
|
|
2091
|
+
);
|
|
2092
|
+
md = md.replace(
|
|
2093
|
+
/<img\s[^>]*?\/?>/gi,
|
|
2094
|
+
(match) => {
|
|
2095
|
+
const src = (match.match(/\bsrc="([^"]*)"/) || [])[1] ?? "";
|
|
2096
|
+
const alt = (match.match(/\balt="([^"]*)"/) || [])[1] ?? "";
|
|
2097
|
+
const title = (match.match(/\btitle="([^"]*)"/) || [])[1];
|
|
2098
|
+
return title ? `` : ``;
|
|
2099
|
+
}
|
|
2100
|
+
);
|
|
2101
|
+
md = md.replace(
|
|
2102
|
+
/<a\s[^>]*?href="([^"]*)"[^>]*?>([\s\S]*?)<\/a>/gi,
|
|
2103
|
+
(_, href, inner) => {
|
|
2104
|
+
const title = (_.match(/\btitle="([^"]*)"/) || [])[1];
|
|
2105
|
+
const innerMd = htmlInlineToMarkdown(inner);
|
|
2106
|
+
return title ? `[${innerMd}](${href} "${title}")` : `[${innerMd}](${href})`;
|
|
2107
|
+
}
|
|
2108
|
+
);
|
|
2109
|
+
md = md.replace(/<strong>([\s\S]*?)<\/strong>/gi, (_, t) => `**${htmlInlineToMarkdown(t)}**`);
|
|
2110
|
+
md = md.replace(/<b>([\s\S]*?)<\/b>/gi, (_, t) => `**${htmlInlineToMarkdown(t)}**`);
|
|
2111
|
+
md = md.replace(/<em>([\s\S]*?)<\/em>/gi, (_, t) => `*${htmlInlineToMarkdown(t)}*`);
|
|
2112
|
+
md = md.replace(/<i>([\s\S]*?)<\/i>/gi, (_, t) => `*${htmlInlineToMarkdown(t)}*`);
|
|
2113
|
+
md = md.replace(/<(?:del|s)>([\s\S]*?)<\/(?:del|s)>/gi, (_, t) => `~~${htmlInlineToMarkdown(t)}~~`);
|
|
2114
|
+
md = md.replace(/<code>([\s\S]*?)<\/code>/gi, (_, t) => {
|
|
2115
|
+
const decoded = t.replace(/&#x([0-9a-f]+);/gi, (_2, hex) => String.fromCodePoint(parseInt(hex, 16))).replace(/&#([0-9]+);/g, (_2, dec) => String.fromCodePoint(parseInt(dec, 10))).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'");
|
|
2116
|
+
return "`" + decoded + "`";
|
|
2117
|
+
});
|
|
2118
|
+
md = md.replace(/<br\s*\/?>/gi, "\n");
|
|
2119
|
+
md = md.replace(/<[^>]+>/g, "");
|
|
2120
|
+
md = md.replace(/&#x([0-9a-f]+);/gi, (_, hex) => String.fromCodePoint(parseInt(hex, 16))).replace(/&#([0-9]+);/g, (_, dec) => String.fromCodePoint(parseInt(dec, 10))).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'").replace(/ /g, " ");
|
|
2121
|
+
return md;
|
|
2122
|
+
}
|
|
2123
|
+
function toMarkdown(root) {
|
|
2124
|
+
const lines = [];
|
|
2125
|
+
function walk(node, depth) {
|
|
2126
|
+
const text = htmlInlineToMarkdown(node.content).trim();
|
|
2127
|
+
if (text) {
|
|
2128
|
+
if (depth <= 2) {
|
|
2129
|
+
const hashes = "#".repeat(depth + 1);
|
|
2130
|
+
if (lines.length > 0) lines.push("");
|
|
2131
|
+
lines.push(`${hashes} ${text}`);
|
|
2132
|
+
} else {
|
|
2133
|
+
const indent = " ".repeat(depth - 3);
|
|
2134
|
+
lines.push(`${indent}- ${text}`);
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
for (const child of node.children) {
|
|
2138
|
+
walk(child, depth + 1);
|
|
2139
|
+
}
|
|
2140
|
+
}
|
|
2141
|
+
walk(root, 0);
|
|
2142
|
+
return lines.join("\n").trimStart() + "\n";
|
|
2143
|
+
}
|
|
2144
|
+
exports.Markmap = Markmap;
|
|
2145
|
+
exports.childSelector = childSelector;
|
|
2146
|
+
exports.defaultColorFn = defaultColorFn;
|
|
2147
|
+
exports.defaultOptions = defaultOptions;
|
|
2148
|
+
exports.deriveOptions = deriveOptions;
|
|
2149
|
+
exports.globalCSS = globalCSS;
|
|
2150
|
+
exports.htmlInlineToMarkdown = htmlInlineToMarkdown;
|
|
2151
|
+
exports.isMacintosh = isMacintosh;
|
|
2152
|
+
exports.lineWidthFactory = lineWidthFactory;
|
|
2153
|
+
exports.loadCSS = loadCSS;
|
|
2154
|
+
exports.loadJS = loadJS;
|
|
2155
|
+
exports.refreshHook = refreshHook;
|
|
2156
|
+
exports.simpleHash = simpleHash;
|
|
2157
|
+
exports.toMarkdown = toMarkdown;
|
|
2158
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2159
|
+
})(this.markmap = this.markmap || {}, d3);
|