markmap-view 0.14.2-alpha.0 → 0.14.2

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,219 +1,8 @@
1
- /*! markmap-view v0.14.2-alpha.0+518591b | MIT License */
1
+ /*! markmap-view v0.14.2 | 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.2-alpha.0+518591b | 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
- async function loadJSItem(item, context) {
151
- if (!item.loaded) {
152
- if (item.type === 'script') {
153
- item.loaded = new Promise((resolve, reject) => {
154
- var _item$data;
155
-
156
- document.head.append(createElement('script', _extends$1({}, item.data, {
157
- onload: resolve,
158
- onerror: reject
159
- }))); // Run inline script synchronously
160
-
161
- if (!((_item$data = item.data) != null && _item$data.src)) resolve(undefined);
162
- }).then(() => {
163
- item.loaded = true;
164
- });
165
- }
166
-
167
- if (item.type === 'iife') {
168
- const {
169
- fn,
170
- getParams
171
- } = item.data;
172
- fn(...((getParams == null ? void 0 : getParams(context)) || []));
173
- item.loaded = true;
174
- }
175
- }
176
-
177
- await item.loaded;
178
- }
179
-
180
- function loadCSSItem(item) {
181
- if (item.loaded) return;
182
- item.loaded = true;
183
-
184
- if (item.type === 'style') {
185
- document.head.append(createElement('style', {
186
- textContent: item.data
187
- }));
188
- } else if (item.type === 'stylesheet') {
189
- document.head.append(createElement('link', _extends$1({
190
- rel: 'stylesheet'
191
- }, item.data)));
192
- }
193
- }
194
-
195
- async function loadJS(items, context) {
196
- const needPreload = items.filter(item => {
197
- var _item$data2;
198
-
199
- return item.type === 'script' && ((_item$data2 = item.data) == null ? void 0 : _item$data2.src);
200
- });
201
- if (needPreload.length > 1) needPreload.forEach(item => memoizedPreloadJS(item.data.src));
202
- context = _extends$1({
203
- getMarkmap: () => window.markmap
204
- }, context);
205
-
206
- for (const item of items) {
207
- await loadJSItem(item, context);
208
- }
209
- }
210
-
211
- function loadCSS(items) {
212
- for (const item of items) {
213
- loadCSSItem(item);
214
- }
215
- }
216
-
217
6
  function _extends() {
218
7
  _extends = Object.assign || function (target) {
219
8
  for (var i = 1; i < arguments.length; i++) {
@@ -1276,7 +1065,6 @@ class Markmap {
1276
1065
  };
1277
1066
  this.g = this.svg.append('g');
1278
1067
  this.updateStyle();
1279
- this.svg.call(this.zoom).on('wheel', this.handlePan);
1280
1068
  this.revokers.push(refreshHook.tap(() => {
1281
1069
  this.setData();
1282
1070
  }));
@@ -1345,8 +1133,8 @@ class Markmap {
1345
1133
  document.body.append(container, style);
1346
1134
  const groupStyle = maxWidth ? `max-width: ${maxWidth}px` : '';
1347
1135
  let foldRecursively = 0;
1348
- walkTree(node, (item, next) => {
1349
- var _item$children, _item$payload;
1136
+ walkTree(node, (item, next, parent) => {
1137
+ var _item$children, _parent$state, _item$payload;
1350
1138
 
1351
1139
  item.children = (_item$children = item.children) == null ? void 0 : _item$children.map(child => _extends({}, child));
1352
1140
  nodeId += 1;
@@ -1364,6 +1152,7 @@ class Markmap {
1364
1152
  id: nodeId,
1365
1153
  el: group.firstChild
1366
1154
  });
1155
+ item.state.path = [parent == null ? void 0 : (_parent$state = parent.state) == null ? void 0 : _parent$state.path, item.state.id].filter(Boolean).join('.');
1367
1156
  color(item); // preload colors
1368
1157
 
1369
1158
  const isFoldRecursively = ((_item$payload = item.payload) == null ? void 0 : _item$payload.fold) === 2;
@@ -1389,12 +1178,11 @@ class Markmap {
1389
1178
  node.parentNode.append(node.cloneNode(true));
1390
1179
  });
1391
1180
  walkTree(node, (item, next, parent) => {
1392
- var _parent$state, _parent$state2;
1181
+ var _parent$state2;
1393
1182
 
1394
1183
  const rect = item.state.el.getBoundingClientRect();
1395
1184
  item.content = item.state.el.innerHTML;
1396
1185
  item.state.size = [Math.ceil(rect.width) + 1, Math.max(Math.ceil(rect.height), nodeMinHeight)];
1397
- item.state.path = [parent == null ? void 0 : (_parent$state = parent.state) == null ? void 0 : _parent$state.path, item.state.id].filter(Boolean).join('.');
1398
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
1399
1187
  item.content;
1400
1188
  next();
@@ -1405,6 +1193,14 @@ class Markmap {
1405
1193
 
1406
1194
  setOptions(opts) {
1407
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);
1408
1204
  }
1409
1205
 
1410
1206
  setData(data, opts) {
@@ -1630,7 +1426,7 @@ class Markmap {
1630
1426
  }
1631
1427
  Markmap.defaultOptions = {
1632
1428
  autoFit: false,
1633
- color: node => defaultColorFn(`${node.state.id}`),
1429
+ color: node => defaultColorFn(`${node.state.path}`),
1634
1430
  duration: 500,
1635
1431
  embedGlobalCSS: true,
1636
1432
  fitRatio: 0.95,
@@ -1640,13 +1436,16 @@ Markmap.defaultOptions = {
1640
1436
  scrollForPan: isMacintosh,
1641
1437
  spacingHorizontal: 80,
1642
1438
  spacingVertical: 5,
1643
- initialExpandLevel: -1
1439
+ initialExpandLevel: -1,
1440
+ zoom: true,
1441
+ pan: true
1644
1442
  };
1645
1443
  function deriveOptions(jsonOptions) {
1646
1444
  const opts = {};
1647
1445
  jsonOptions || (jsonOptions = {});
1648
1446
  const {
1649
- color
1447
+ color,
1448
+ colorFreezeLevel
1650
1449
  } = jsonOptions;
1651
1450
 
1652
1451
  if ((color == null ? void 0 : color.length) === 1) {
@@ -1656,7 +1455,20 @@ function deriveOptions(jsonOptions) {
1656
1455
  } else if (color != null && color.length) {
1657
1456
  const colorFn = d3.scaleOrdinal(color);
1658
1457
 
1659
- 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).join('.')
1468
+ })
1469
+ });
1470
+ return color(node);
1471
+ };
1660
1472
  }
1661
1473
 
1662
1474
  const numberKeys = ['duration', 'maxWidth', 'initialExpandLevel'];
@@ -1664,7 +1476,12 @@ function deriveOptions(jsonOptions) {
1664
1476
  const value = jsonOptions[key];
1665
1477
  if (typeof value === 'number') opts[key] = value;
1666
1478
  });
1479
+ const booleanKeys = ['zoom', 'pan'];
1480
+ booleanKeys.forEach(key => {
1481
+ const value = jsonOptions[key];
1482
+ if (value != null) opts[key] = !!value;
1483
+ });
1667
1484
  return opts;
1668
1485
  }
1669
1486
 
1670
- export { Markmap, defaultColorFn, deriveOptions, globalCSS, loadCSS, loadJS, refreshHook };
1487
+ export { Markmap, defaultColorFn, deriveOptions, globalCSS, refreshHook };