markmap-view 0.14.0 → 0.14.2-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -1,209 +1,8 @@
1
- /*! markmap-view v0.14.0 | MIT License */
1
+ /*! markmap-view v0.14.2-alpha.4+569dd11 | MIT License */
2
+ import { Hook, getId, addClass, walkTree, arrayFrom, childSelector, noop } from 'markmap-common';
3
+ export { loadCSS, loadJS } from 'markmap-common';
2
4
  import * as d3 from 'd3';
3
5
 
4
- /*! markmap-common v0.14.0 | MIT License */
5
- class Hook {
6
- constructor() {
7
- this.listeners = [];
8
- }
9
-
10
- tap(fn) {
11
- this.listeners.push(fn);
12
- return () => this.revoke(fn);
13
- }
14
-
15
- revoke(fn) {
16
- const i = this.listeners.indexOf(fn);
17
- if (i >= 0) this.listeners.splice(i, 1);
18
- }
19
-
20
- revokeAll() {
21
- this.listeners.splice(0);
22
- }
23
-
24
- call(...args) {
25
- for (const fn of this.listeners) {
26
- fn(...args);
27
- }
28
- }
29
-
30
- }
31
-
32
- function _extends$1() {
33
- _extends$1 = Object.assign || function (target) {
34
- for (var i = 1; i < arguments.length; i++) {
35
- var source = arguments[i];
36
-
37
- for (var key in source) {
38
- if (Object.prototype.hasOwnProperty.call(source, key)) {
39
- target[key] = source[key];
40
- }
41
- }
42
- }
43
-
44
- return target;
45
- };
46
-
47
- return _extends$1.apply(this, arguments);
48
- }
49
-
50
- const uniqId = Math.random().toString(36).slice(2, 8);
51
- let globalIndex = 0;
52
-
53
- function getId() {
54
- globalIndex += 1;
55
- return `mm-${uniqId}-${globalIndex}`;
56
- }
57
-
58
- function noop() {// noop
59
- }
60
-
61
- function walkTree(tree, callback, key = 'children') {
62
- const walk = (item, parent) => callback(item, () => {
63
- var _item$key;
64
-
65
- (_item$key = item[key]) == null ? void 0 : _item$key.forEach(child => {
66
- walk(child, item);
67
- });
68
- }, parent);
69
-
70
- walk(tree);
71
- }
72
-
73
- function arrayFrom(arrayLike) {
74
- if (Array.from) return Array.from(arrayLike);
75
- const array = [];
76
-
77
- for (let i = 0; i < arrayLike.length; i += 1) {
78
- array.push(arrayLike[i]);
79
- }
80
-
81
- return array;
82
- }
83
-
84
- function addClass(className, ...rest) {
85
- const classList = (className || '').split(' ').filter(Boolean);
86
- rest.forEach(item => {
87
- if (item && classList.indexOf(item) < 0) classList.push(item);
88
- });
89
- return classList.join(' ');
90
- }
91
-
92
- function childSelector(filter) {
93
- if (typeof filter === 'string') {
94
- const tagName = filter;
95
-
96
- filter = el => el.tagName === tagName;
97
- }
98
-
99
- const filterFn = filter;
100
- return function selector() {
101
- let nodes = arrayFrom(this.childNodes);
102
- if (filterFn) nodes = nodes.filter(node => filterFn(node));
103
- return nodes;
104
- };
105
- }
106
-
107
- function memoize(fn) {
108
- const cache = {};
109
- return function memoized(...args) {
110
- const key = `${args[0]}`;
111
- let data = cache[key];
112
-
113
- if (!data) {
114
- data = {
115
- value: fn(...args)
116
- };
117
- cache[key] = data;
118
- }
119
-
120
- return data.value;
121
- };
122
- }
123
-
124
- function createElement(tagName, props, attrs) {
125
- const el = document.createElement(tagName);
126
-
127
- if (props) {
128
- Object.entries(props).forEach(([key, value]) => {
129
- el[key] = value;
130
- });
131
- }
132
-
133
- if (attrs) {
134
- Object.entries(attrs).forEach(([key, value]) => {
135
- el.setAttribute(key, value);
136
- });
137
- }
138
-
139
- return el;
140
- }
141
-
142
- const memoizedPreloadJS = memoize(url => {
143
- document.head.append(createElement('link', {
144
- rel: 'preload',
145
- as: 'script',
146
- href: url
147
- }));
148
- });
149
-
150
- function loadJSItem(item, context) {
151
- if (item.type === 'script') {
152
- return new Promise((resolve, reject) => {
153
- var _item$data;
154
-
155
- document.head.append(createElement('script', _extends$1({}, item.data, {
156
- onload: resolve,
157
- onerror: reject
158
- }))); // Run inline script synchronously
159
-
160
- if (!((_item$data = item.data) != null && _item$data.src)) resolve();
161
- });
162
- }
163
-
164
- if (item.type === 'iife') {
165
- const {
166
- fn,
167
- getParams
168
- } = item.data;
169
- fn(...((getParams == null ? void 0 : getParams(context)) || []));
170
- }
171
- }
172
-
173
- function loadCSSItem(item) {
174
- if (item.type === 'style') {
175
- document.head.append(createElement('style', {
176
- textContent: item.data
177
- }));
178
- } else if (item.type === 'stylesheet') {
179
- document.head.append(createElement('link', _extends$1({
180
- rel: 'stylesheet'
181
- }, item.data)));
182
- }
183
- }
184
-
185
- async function loadJS(items, context) {
186
- const needPreload = items.filter(item => {
187
- var _item$data2;
188
-
189
- return item.type === 'script' && ((_item$data2 = item.data) == null ? void 0 : _item$data2.src);
190
- });
191
- if (needPreload.length > 1) needPreload.forEach(item => memoizedPreloadJS(item.data.src));
192
- context = _extends$1({
193
- getMarkmap: () => window.markmap
194
- }, context);
195
-
196
- for (const item of items) {
197
- await loadJSItem(item, context);
198
- }
199
- }
200
-
201
- function loadCSS(items) {
202
- for (const item of items) {
203
- loadCSSItem(item);
204
- }
205
- }
206
-
207
6
  function _extends() {
208
7
  _extends = Object.assign || function (target) {
209
8
  for (var i = 1; i < arguments.length; i++) {
@@ -1266,7 +1065,6 @@ class Markmap {
1266
1065
  };
1267
1066
  this.g = this.svg.append('g');
1268
1067
  this.updateStyle();
1269
- this.svg.call(this.zoom).on('wheel', this.handlePan);
1270
1068
  this.revokers.push(refreshHook.tap(() => {
1271
1069
  this.setData();
1272
1070
  }));
@@ -1335,8 +1133,8 @@ class Markmap {
1335
1133
  document.body.append(container, style);
1336
1134
  const groupStyle = maxWidth ? `max-width: ${maxWidth}px` : '';
1337
1135
  let foldRecursively = 0;
1338
- walkTree(node, (item, next) => {
1339
- var _item$children, _item$payload;
1136
+ walkTree(node, (item, next, parent) => {
1137
+ var _item$children, _parent$state, _item$payload;
1340
1138
 
1341
1139
  item.children = (_item$children = item.children) == null ? void 0 : _item$children.map(child => _extends({}, child));
1342
1140
  nodeId += 1;
@@ -1352,7 +1150,8 @@ class Markmap {
1352
1150
  container.append(group);
1353
1151
  item.state = _extends({}, item.state, {
1354
1152
  id: nodeId,
1355
- el: group.firstChild
1153
+ el: group.firstChild,
1154
+ path: [parent == null ? void 0 : (_parent$state = parent.state) == null ? void 0 : _parent$state.path, item.state.id].filter(Boolean).join('.')
1356
1155
  });
1357
1156
  color(item); // preload colors
1358
1157
 
@@ -1379,12 +1178,11 @@ class Markmap {
1379
1178
  node.parentNode.append(node.cloneNode(true));
1380
1179
  });
1381
1180
  walkTree(node, (item, next, parent) => {
1382
- var _parent$state, _parent$state2;
1181
+ var _parent$state2;
1383
1182
 
1384
1183
  const rect = item.state.el.getBoundingClientRect();
1385
1184
  item.content = item.state.el.innerHTML;
1386
1185
  item.state.size = [Math.ceil(rect.width) + 1, Math.max(Math.ceil(rect.height), nodeMinHeight)];
1387
- item.state.path = [parent == null ? void 0 : (_parent$state = parent.state) == null ? void 0 : _parent$state.path, item.state.id].filter(Boolean).join('.');
1388
1186
  item.state.key = [parent == null ? void 0 : (_parent$state2 = parent.state) == null ? void 0 : _parent$state2.id, item.state.id].filter(Boolean).join('.') + // FIXME: find a way to check content hash
1389
1187
  item.content;
1390
1188
  next();
@@ -1395,6 +1193,14 @@ class Markmap {
1395
1193
 
1396
1194
  setOptions(opts) {
1397
1195
  this.options = _extends({}, Markmap.defaultOptions, opts);
1196
+
1197
+ if (this.options.zoom) {
1198
+ this.svg.call(this.zoom);
1199
+ } else {
1200
+ this.svg.on('.zoom', null);
1201
+ }
1202
+
1203
+ this.svg.on('wheel', this.options.pan ? this.handlePan : null);
1398
1204
  }
1399
1205
 
1400
1206
  setData(data, opts) {
@@ -1620,7 +1426,7 @@ class Markmap {
1620
1426
  }
1621
1427
  Markmap.defaultOptions = {
1622
1428
  autoFit: false,
1623
- color: node => defaultColorFn(`${node.state.id}`),
1429
+ color: node => defaultColorFn(`${node.state.path}`),
1624
1430
  duration: 500,
1625
1431
  embedGlobalCSS: true,
1626
1432
  fitRatio: 0.95,
@@ -1630,13 +1436,16 @@ Markmap.defaultOptions = {
1630
1436
  scrollForPan: isMacintosh,
1631
1437
  spacingHorizontal: 80,
1632
1438
  spacingVertical: 5,
1633
- initialExpandLevel: -1
1439
+ initialExpandLevel: -1,
1440
+ zoom: true,
1441
+ pan: true
1634
1442
  };
1635
1443
  function deriveOptions(jsonOptions) {
1636
1444
  const opts = {};
1637
1445
  jsonOptions || (jsonOptions = {});
1638
1446
  const {
1639
- color
1447
+ color,
1448
+ colorFreezeLevel
1640
1449
  } = jsonOptions;
1641
1450
 
1642
1451
  if ((color == null ? void 0 : color.length) === 1) {
@@ -1646,7 +1455,20 @@ function deriveOptions(jsonOptions) {
1646
1455
  } else if (color != null && color.length) {
1647
1456
  const colorFn = d3.scaleOrdinal(color);
1648
1457
 
1649
- opts.color = node => colorFn(`${node.state.id}`);
1458
+ opts.color = node => colorFn(`${node.state.path}`);
1459
+ }
1460
+
1461
+ if (colorFreezeLevel) {
1462
+ const color = opts.color || Markmap.defaultOptions.color;
1463
+
1464
+ opts.color = node => {
1465
+ node = _extends({}, node, {
1466
+ state: _extends({}, node.state, {
1467
+ path: node.state.path.split('.').slice(0, colorFreezeLevel + 1).join('.')
1468
+ })
1469
+ });
1470
+ return color(node);
1471
+ };
1650
1472
  }
1651
1473
 
1652
1474
  const numberKeys = ['duration', 'maxWidth', 'initialExpandLevel'];
@@ -1654,7 +1476,12 @@ function deriveOptions(jsonOptions) {
1654
1476
  const value = jsonOptions[key];
1655
1477
  if (typeof value === 'number') opts[key] = value;
1656
1478
  });
1479
+ const booleanKeys = ['zoom', 'pan'];
1480
+ booleanKeys.forEach(key => {
1481
+ const value = jsonOptions[key];
1482
+ if (value != null) opts[key] = !!value;
1483
+ });
1657
1484
  return opts;
1658
1485
  }
1659
1486
 
1660
- export { Markmap, defaultColorFn, deriveOptions, globalCSS, loadCSS, loadJS, refreshHook };
1487
+ export { Markmap, defaultColorFn, deriveOptions, globalCSS, refreshHook };