markmap-autoloader 0.14.5-alpha.13 → 0.14.5-alpha.16

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.
Files changed (3) hide show
  1. package/README.md +33 -28
  2. package/dist/index.js +238 -37
  3. package/package.json +6 -5
package/README.md CHANGED
@@ -12,18 +12,23 @@ HTML:
12
12
 
13
13
  ```html
14
14
  <style>
15
- .markmap > svg {
16
- width: 100%;
17
- height: 300px;
18
- }
15
+ .markmap {
16
+ position: relative;
17
+ }
18
+ .markmap > svg {
19
+ width: 100%;
20
+ height: 300px;
21
+ }
19
22
  </style>
20
23
 
21
- <div class="markmap"><script type="text/template">
22
- - markmap
23
- - autoloader
24
- - transformer
25
- - view
26
- </script></div>
24
+ <div class="markmap">
25
+ <script type="text/template">
26
+ - markmap
27
+ - autoloader
28
+ - transformer
29
+ - view
30
+ </script>
31
+ </div>
27
32
  ```
28
33
 
29
34
  Note that `<script type="text/template">` is optional, for the content inside to be invisible before the markmap is rendered.
@@ -44,16 +49,16 @@ Load manually:
44
49
 
45
50
  ```html
46
51
  <script>
47
- window.markmap = {
48
- autoLoader: { manual: true },
49
- };
52
+ window.markmap = {
53
+ autoLoader: { manual: true },
54
+ };
50
55
  </script>
51
56
  <script src="https://cdn.jsdelivr.net/npm/markmap-autoloader"></script>
52
57
  <script>
53
- // Render in 5s
54
- setTimeout(() => {
55
- markmap.autoLoader.renderAll();
56
- }, 5000);
58
+ // Render in 5s
59
+ setTimeout(() => {
60
+ markmap.autoLoader.renderAll();
61
+ }, 5000);
57
62
  </script>
58
63
  ```
59
64
 
@@ -61,11 +66,11 @@ Disable built-in plugins:
61
66
 
62
67
  ```html
63
68
  <script>
64
- window.markmap = {
65
- autoLoader: {
66
- transformPlugins: [],
67
- },
68
- };
69
+ window.markmap = {
70
+ autoLoader: {
71
+ transformPlugins: [],
72
+ },
73
+ };
69
74
  </script>
70
75
  <script src="https://cdn.jsdelivr.net/npm/markmap-autoloader"></script>
71
76
  ```
@@ -76,17 +81,17 @@ window.markmap = {
76
81
 
77
82
  If `window.markmap.autoLoader` is defined before this package is loaded, it will be regarded as autoLoader options.
78
83
 
79
- - `autoLoader.manual` *boolean* default as `false`
84
+ - `autoLoader.manual` _boolean_ default as `false`
80
85
 
81
- Whether to render markmaps manually. If false, all elements matching `.markmap` will be rendered once this package loads or DOMContentLoaded is emitted, whichever later.
86
+ Whether to render markmaps manually. If false, all elements matching `.markmap` will be rendered once this package loads or DOMContentLoaded is emitted, whichever later.
82
87
 
83
- - `autoLoader.transformPlugins` *ITransformPlugin[]*
88
+ - `autoLoader.transformPlugins` _ITransformPlugin[]_
84
89
 
85
- Override built-in plugins if provided. Set to `[]` to disable all built-in plugins for auto-loader.
90
+ Override built-in plugins if provided. Set to `[]` to disable all built-in plugins for auto-loader.
86
91
 
87
- - `autoLoader.onReady` *function*
92
+ - `autoLoader.onReady` _function_
88
93
 
89
- Callback when markmap-lib/markmap-view and their dependencies are loaded. We can tweak global options in this callback.
94
+ Callback when markmap-lib/markmap-view and their dependencies are loaded. We can tweak global options in this callback.
90
95
 
91
96
  ### markmap.autoLoader.renderAll()
92
97
 
package/dist/index.js CHANGED
@@ -1,9 +1,24 @@
1
- /*! markmap-autoloader v0.14.5-alpha.13+50ca560 | MIT License */
1
+ /*! markmap-autoloader v0.14.5-alpha.16+33e8294 | MIT License */
2
2
  this.markmap = this.markmap || {};
3
3
  (function (exports) {
4
4
  'use strict';
5
5
 
6
- /*! markmap-common v0.14.5-alpha.13+50ca560 | MIT License */
6
+ function _extends$1() {
7
+ _extends$1 = Object.assign ? Object.assign.bind() : function (target) {
8
+ for (var i = 1; i < arguments.length; i++) {
9
+ var source = arguments[i];
10
+ for (var key in source) {
11
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
12
+ target[key] = source[key];
13
+ }
14
+ }
15
+ }
16
+ return target;
17
+ };
18
+ return _extends$1.apply(this, arguments);
19
+ }
20
+
21
+ /*! markmap-common v0.14.5-alpha.16+33e8294 | MIT License */
7
22
  function _extends() {
8
23
  _extends = Object.assign ? Object.assign.bind() : function (target) {
9
24
  for (var i = 1; i < arguments.length; i++) {
@@ -33,6 +48,161 @@ function memoize(fn) {
33
48
  return data.value;
34
49
  };
35
50
  }
51
+
52
+ /*! @gera2ld/jsx-dom v2.2.2 | ISC License */
53
+ const VTYPE_ELEMENT = 1;
54
+ const VTYPE_FUNCTION = 2;
55
+ const SVG_NS = 'http://www.w3.org/2000/svg';
56
+ const XLINK_NS = 'http://www.w3.org/1999/xlink';
57
+ const NS_ATTRS = {
58
+ show: XLINK_NS,
59
+ actuate: XLINK_NS,
60
+ href: XLINK_NS
61
+ };
62
+ const isLeaf = c => typeof c === 'string' || typeof c === 'number';
63
+ const isElement = c => (c == null ? void 0 : c.vtype) === VTYPE_ELEMENT;
64
+ const isRenderFunction = c => (c == null ? void 0 : c.vtype) === VTYPE_FUNCTION;
65
+ function h(type, props, ...children) {
66
+ props = Object.assign({}, props, {
67
+ children: children.length === 1 ? children[0] : children
68
+ });
69
+ return jsx(type, props);
70
+ }
71
+ function jsx(type, props) {
72
+ let vtype;
73
+ if (typeof type === 'string') vtype = VTYPE_ELEMENT;else if (typeof type === 'function') vtype = VTYPE_FUNCTION;else throw new Error('Invalid VNode type');
74
+ return {
75
+ vtype,
76
+ type,
77
+ props
78
+ };
79
+ }
80
+ function Fragment(props) {
81
+ return props.children;
82
+ }
83
+ const DEFAULT_ENV = {
84
+ isSvg: false
85
+ };
86
+ function insertDom(parent, nodes) {
87
+ if (!Array.isArray(nodes)) nodes = [nodes];
88
+ nodes = nodes.filter(Boolean);
89
+ if (nodes.length) parent.append(...nodes);
90
+ }
91
+ function mountAttributes(domElement, props, env) {
92
+ for (const key in props) {
93
+ if (key === 'key' || key === 'children' || key === 'ref') continue;
94
+ if (key === 'dangerouslySetInnerHTML') {
95
+ domElement.innerHTML = props[key].__html;
96
+ } else if (key === 'innerHTML' || key === 'textContent' || key === 'innerText' || key === 'value' && ['textarea', 'select'].includes(domElement.tagName)) {
97
+ const value = props[key];
98
+ if (value != null) domElement[key] = value;
99
+ } else if (key.startsWith('on')) {
100
+ domElement[key.toLowerCase()] = props[key];
101
+ } else {
102
+ setDOMAttribute(domElement, key, props[key], env.isSvg);
103
+ }
104
+ }
105
+ }
106
+ const attrMap = {
107
+ className: 'class',
108
+ labelFor: 'for'
109
+ };
110
+ function setDOMAttribute(el, attr, value, isSVG) {
111
+ attr = attrMap[attr] || attr;
112
+ if (value === true) {
113
+ el.setAttribute(attr, '');
114
+ } else if (value === false) {
115
+ el.removeAttribute(attr);
116
+ } else {
117
+ const namespace = isSVG ? NS_ATTRS[attr] : undefined;
118
+ if (namespace !== undefined) {
119
+ el.setAttributeNS(namespace, attr, value);
120
+ } else {
121
+ el.setAttribute(attr, value);
122
+ }
123
+ }
124
+ }
125
+ function flatten(arr) {
126
+ return arr.reduce((prev, item) => prev.concat(item), []);
127
+ }
128
+ function mountChildren(children, env) {
129
+ return Array.isArray(children) ? flatten(children.map(child => mountChildren(child, env))) : mount(children, env);
130
+ }
131
+ function mount(vnode, env = DEFAULT_ENV) {
132
+ if (vnode == null || typeof vnode === 'boolean') {
133
+ return null;
134
+ }
135
+ if (vnode instanceof Node) {
136
+ return vnode;
137
+ }
138
+ if (isRenderFunction(vnode)) {
139
+ const {
140
+ type,
141
+ props
142
+ } = vnode;
143
+ if (type === Fragment) {
144
+ const node = document.createDocumentFragment();
145
+ if (props.children) {
146
+ const children = mountChildren(props.children, env);
147
+ insertDom(node, children);
148
+ }
149
+ return node;
150
+ }
151
+ const childVNode = type(props);
152
+ return mount(childVNode, env);
153
+ }
154
+ if (isLeaf(vnode)) {
155
+ return document.createTextNode(`${vnode}`);
156
+ }
157
+ if (isElement(vnode)) {
158
+ let node;
159
+ const {
160
+ type,
161
+ props
162
+ } = vnode;
163
+ if (!env.isSvg && type === 'svg') {
164
+ env = Object.assign({}, env, {
165
+ isSvg: true
166
+ });
167
+ }
168
+ if (!env.isSvg) {
169
+ node = document.createElement(type);
170
+ } else {
171
+ node = document.createElementNS(SVG_NS, type);
172
+ }
173
+ mountAttributes(node, props, env);
174
+ if (props.children) {
175
+ let childEnv = env;
176
+ if (env.isSvg && type === 'foreignObject') {
177
+ childEnv = Object.assign({}, childEnv, {
178
+ isSvg: false
179
+ });
180
+ }
181
+ const children = mountChildren(props.children, childEnv);
182
+ if (children != null) insertDom(node, children);
183
+ }
184
+ const {
185
+ ref
186
+ } = props;
187
+ if (typeof ref === 'function') ref(node);
188
+ return node;
189
+ }
190
+ throw new Error('mount: Invalid Vnode!');
191
+ }
192
+
193
+ /**
194
+ * Mount vdom as real DOM nodes.
195
+ */
196
+ function mountDom(vnode) {
197
+ return mount(vnode);
198
+ }
199
+
200
+ /**
201
+ * Render and mount without returning VirtualDOM, useful when you don't need SVG support.
202
+ */
203
+ function hm(...args) {
204
+ return mountDom(h(...args));
205
+ }
36
206
  const providers = {
37
207
  jsdelivr: path => `https://cdn.jsdelivr.net/npm/${path}`,
38
208
  unpkg: path => `https://unpkg.com/${path}`
@@ -44,22 +214,8 @@ function cdnUrl(provider, path) {
44
214
  }
45
215
  return factory(path);
46
216
  }
47
- function createElement(tagName, props, attrs) {
48
- const el = document.createElement(tagName);
49
- if (props) {
50
- Object.entries(props).forEach(([key, value]) => {
51
- el[key] = value;
52
- });
53
- }
54
- if (attrs) {
55
- Object.entries(attrs).forEach(([key, value]) => {
56
- el.setAttribute(key, value);
57
- });
58
- }
59
- return el;
60
- }
61
217
  const memoizedPreloadJS = memoize(url => {
62
- document.head.append(createElement('link', {
218
+ document.head.append(hm('link', {
63
219
  rel: 'preload',
64
220
  as: 'script',
65
221
  href: url
@@ -70,9 +226,9 @@ async function loadJSItem(item, context) {
70
226
  if (item.type === 'script') {
71
227
  item.loaded = new Promise((resolve, reject) => {
72
228
  var _item$data;
73
- document.head.append(createElement('script', _extends({}, item.data, {
74
- onload: resolve,
75
- onerror: reject
229
+ document.head.append(hm('script', _extends({}, item.data, {
230
+ onLoad: resolve,
231
+ onError: reject
76
232
  })));
77
233
  // Run inline script synchronously
78
234
  if (!((_item$data = item.data) != null && _item$data.src)) resolve(undefined);
@@ -91,12 +247,26 @@ async function loadJSItem(item, context) {
91
247
  }
92
248
  await item.loaded;
93
249
  }
250
+ function loadCSSItem(item) {
251
+ if (item.loaded) return;
252
+ item.loaded = true;
253
+ if (item.type === 'style') {
254
+ document.head.append(hm('style', {
255
+ textContent: item.data
256
+ }));
257
+ } else if (item.type === 'stylesheet') {
258
+ document.head.append(hm('link', _extends({
259
+ rel: 'stylesheet'
260
+ }, item.data)));
261
+ }
262
+ }
94
263
  async function loadJS(items, context) {
95
- const needPreload = items.filter(item => {
264
+ items.forEach(item => {
96
265
  var _item$data2;
97
- return item.type === 'script' && ((_item$data2 = item.data) == null ? void 0 : _item$data2.src);
266
+ if (item.type === 'script' && (_item$data2 = item.data) != null && _item$data2.src) {
267
+ memoizedPreloadJS(item.data.src);
268
+ }
98
269
  });
99
- if (needPreload.length > 1) needPreload.forEach(item => memoizedPreloadJS(item.data.src));
100
270
  context = _extends({
101
271
  getMarkmap: () => window.markmap
102
272
  }, context);
@@ -104,24 +274,42 @@ async function loadJS(items, context) {
104
274
  await loadJSItem(item, context);
105
275
  }
106
276
  }
277
+ function loadCSS(items) {
278
+ for (const item of items) {
279
+ loadCSSItem(item);
280
+ }
281
+ }
107
282
  let provider = 'jsdelivr';
108
- function getFullUrl(path) {
283
+ function getFullUrl(path, overrideProvider = provider) {
109
284
  if (path.includes('://')) return path;
110
- return cdnUrl(provider, path);
285
+ return cdnUrl(overrideProvider, path);
111
286
  }
112
- function buildJSItem(path) {
287
+ function buildJSItem(path, overrideProvider) {
113
288
  return {
114
289
  type: 'script',
115
290
  data: {
116
- src: getFullUrl(path)
291
+ src: getFullUrl(path, overrideProvider)
292
+ }
293
+ };
294
+ }
295
+ function buildCSSItem(path, overrideProvider) {
296
+ return {
297
+ type: 'stylesheet',
298
+ data: {
299
+ href: getFullUrl(path, overrideProvider)
117
300
  }
118
301
  };
119
302
  }
120
303
 
121
- var _window$markmap, _window$markmap2;
304
+ var _window$markmap;
122
305
  const enabled = {};
123
- const ready = loadJS(((_window$markmap = window.markmap) == null || (_window$markmap = _window$markmap.autoLoader) == null ? void 0 : _window$markmap.baseJs) || [`d3@${"6.2.0"}`, `markmap-lib@${"0.14.5-alpha.13+50ca560"}`, `markmap-view@${"0.14.5-alpha.13+50ca560"}`].map(buildJSItem)).then(() => {
124
- var _markmap$autoLoader;
306
+ const autoLoaderOptions = _extends$1({
307
+ baseJs: [`d3@${"6.2.0"}`, `markmap-lib@${"0.14.5-alpha.16+33e8294"}`, `markmap-view@${"0.14.5-alpha.16+33e8294"}`, `markmap-toolbar@${process.env.TOOLBAR_VERSION}`],
308
+ baseCss: [`markmap-toolbar@${process.env.TOOLBAR_VERSION}/dist/style.css`],
309
+ manual: false,
310
+ toolbar: false
311
+ }, (_window$markmap = window.markmap) == null ? void 0 : _window$markmap.autoLoader);
312
+ const ready = Promise.all([loadJS(autoLoaderOptions.baseJs.map(item => typeof item === 'string' ? buildJSItem(item) : item)), loadCSS(autoLoaderOptions.baseCss.map(item => typeof item === 'string' ? buildCSSItem(item) : item))]).then(() => {
125
313
  const {
126
314
  markmap
127
315
  } = window;
@@ -129,7 +317,7 @@ const ready = loadJS(((_window$markmap = window.markmap) == null || (_window$mar
129
317
  style.textContent = markmap.globalCSS;
130
318
  // Insert global CSS to body so it has higher priority than prism.css, etc.
131
319
  document.body.prepend(style);
132
- (_markmap$autoLoader = markmap.autoLoader) == null || _markmap$autoLoader.onReady == null ? void 0 : _markmap$autoLoader.onReady();
320
+ autoLoaderOptions.onReady == null ? void 0 : autoLoaderOptions.onReady();
133
321
  });
134
322
  function transform(transformer, content) {
135
323
  const result = transformer.transform(content);
@@ -149,25 +337,38 @@ function transform(transformer, content) {
149
337
  return result;
150
338
  }
151
339
  function render(el) {
340
+ var _el$textContent;
152
341
  const {
153
342
  Transformer,
154
343
  Markmap,
155
- autoLoader,
156
- deriveOptions
344
+ deriveOptions,
345
+ Toolbar
157
346
  } = window.markmap;
158
- const lines = el.textContent.split('\n');
347
+ const lines = ((_el$textContent = el.textContent) == null ? void 0 : _el$textContent.split('\n')) || [];
159
348
  let indent = Infinity;
160
349
  lines.forEach(line => {
161
- const spaces = line.match(/^\s*/)[0].length;
350
+ var _line$match;
351
+ const spaces = ((_line$match = line.match(/^\s*/)) == null ? void 0 : _line$match[0].length) || 0;
162
352
  if (spaces < line.length) indent = Math.min(indent, spaces);
163
353
  });
164
354
  const content = lines.map(line => line.slice(indent)).join('\n').trim();
165
- const transformer = new Transformer(autoLoader == null ? void 0 : autoLoader.transformPlugins);
355
+ const transformer = new Transformer(autoLoaderOptions.transformPlugins);
166
356
  el.innerHTML = '<svg></svg>';
167
357
  const svg = el.firstChild;
168
358
  const mm = Markmap.create(svg, {
169
359
  embedGlobalCSS: false
170
360
  });
361
+ if (autoLoaderOptions.toolbar) {
362
+ const {
363
+ el: toolbar
364
+ } = Toolbar.create(mm);
365
+ Object.assign(toolbar.style, {
366
+ position: 'absolute',
367
+ right: '20px',
368
+ bottom: '20px'
369
+ });
370
+ el.append(toolbar);
371
+ }
171
372
  const doRender = () => {
172
373
  const {
173
374
  root,
@@ -188,7 +389,7 @@ async function renderAllUnder(container) {
188
389
  function renderAll() {
189
390
  return renderAllUnder(document);
190
391
  }
191
- if (!((_window$markmap2 = window.markmap) != null && (_window$markmap2 = _window$markmap2.autoLoader) != null && _window$markmap2.manual)) {
392
+ if (!autoLoaderOptions.manual) {
192
393
  if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', () => {
193
394
  renderAll();
194
395
  });else renderAll();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markmap-autoloader",
3
- "version": "0.14.5-alpha.13+50ca560",
3
+ "version": "0.14.5-alpha.16+33e8294",
4
4
  "description": "Load markmaps automatically in HTML",
5
5
  "author": "Gerald <gera2ld@live.com>",
6
6
  "license": "MIT",
@@ -38,9 +38,10 @@
38
38
  "markmap-common": "*"
39
39
  },
40
40
  "devDependencies": {
41
- "markmap-common": "0.14.5-alpha.13+50ca560",
42
- "markmap-lib": "0.14.5-alpha.13+50ca560",
43
- "markmap-view": "0.14.5-alpha.13+50ca560"
41
+ "markmap-common": "0.14.5-alpha.16+33e8294",
42
+ "markmap-lib": "0.14.5-alpha.16+33e8294",
43
+ "markmap-toolbar": "0.14.5-alpha.16+33e8294",
44
+ "markmap-view": "0.14.5-alpha.16+33e8294"
44
45
  },
45
- "gitHead": "50ca5603019498f93dba6485c96d0a5630b221e5"
46
+ "gitHead": "33e8294050d7438f6cdadd5bf06a665560585195"
46
47
  }