markmap-toolbar 0.15.0-alpha.0 → 0.15.0

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 CHANGED
@@ -15,7 +15,7 @@ $ npm i markmap-toolbar
15
15
  ```js
16
16
  import { Toolbar } from 'markmap-toolbar';
17
17
 
18
- const el = Toolbar.create(mm);
18
+ const { el } = Toolbar.create(mm);
19
19
  el.style.position = 'absolute';
20
20
  el.style.bottom = '0.5rem';
21
21
  el.style.right = '0.5rem';
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! markmap-toolbar v0.15.0-alpha.0 | MIT License */
1
+ /*! markmap-toolbar v0.15.0 | MIT License */
2
2
  (function (global, factory) {
3
3
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
4
4
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
@@ -20,61 +20,48 @@ function _extends() {
20
20
  return _extends.apply(this, arguments);
21
21
  }
22
22
 
23
- /*! @gera2ld/jsx-dom v2.1.1 | ISC License */
24
- var VTYPE_ELEMENT = 1;
25
- var VTYPE_FUNCTION = 2;
26
- var MOUNT_SINGLE = 1;
27
- var MOUNT_ARRAY = 4;
28
- var SVG_NS = 'http://www.w3.org/2000/svg';
29
- var XLINK_NS = 'http://www.w3.org/1999/xlink';
30
- var NS_ATTRS = {
23
+ /*! @gera2ld/jsx-dom v2.2.2 | ISC License */
24
+ const VTYPE_ELEMENT = 1;
25
+ const VTYPE_FUNCTION = 2;
26
+ const SVG_NS = 'http://www.w3.org/2000/svg';
27
+ const XLINK_NS = 'http://www.w3.org/1999/xlink';
28
+ const NS_ATTRS = {
31
29
  show: XLINK_NS,
32
30
  actuate: XLINK_NS,
33
31
  href: XLINK_NS
34
32
  };
35
- var isLeaf = function isLeaf(c) {
36
- return typeof c === 'string' || typeof c === 'number';
37
- };
38
- var isElement = function isElement(c) {
39
- return (c == null ? void 0 : c.vtype) === VTYPE_ELEMENT;
40
- };
41
- var isRenderFunction = function isRenderFunction(c) {
42
- return (c == null ? void 0 : c.vtype) === VTYPE_FUNCTION;
43
- };
33
+ const isLeaf = c => typeof c === 'string' || typeof c === 'number';
34
+ const isElement = c => (c == null ? void 0 : c.vtype) === VTYPE_ELEMENT;
35
+ const isRenderFunction = c => (c == null ? void 0 : c.vtype) === VTYPE_FUNCTION;
44
36
  function jsx(type, props) {
45
- var vtype;
37
+ let vtype;
46
38
  if (typeof type === 'string') vtype = VTYPE_ELEMENT;else if (typeof type === 'function') vtype = VTYPE_FUNCTION;else throw new Error('Invalid VNode type');
47
39
  return {
48
- vtype: vtype,
49
- type: type,
50
- props: props
40
+ vtype,
41
+ type,
42
+ props
51
43
  };
52
44
  }
53
- var jsxs = jsx;
45
+ const jsxs = jsx;
54
46
  function Fragment(props) {
55
47
  return props.children;
56
48
  }
57
- var DEFAULT_ENV = {
49
+ const DEFAULT_ENV = {
58
50
  isSvg: false
59
51
  };
60
- function insertDom(parent, ref) {
61
- if (ref.type === MOUNT_SINGLE) {
62
- if (ref.node != null) parent.append(ref.node);
63
- } else if (ref.type === MOUNT_ARRAY) {
64
- ref.children.forEach(function (ch) {
65
- insertDom(parent, ch);
66
- });
67
- } else {
68
- throw new Error("Unkown ref type " + JSON.stringify(ref));
69
- }
52
+ function insertDom(parent, nodes) {
53
+ if (!Array.isArray(nodes)) nodes = [nodes];
54
+ nodes = nodes.filter(Boolean);
55
+ if (nodes.length) parent.append(...nodes);
70
56
  }
71
57
  function mountAttributes(domElement, props, env) {
72
- for (var key in props) {
58
+ for (const key in props) {
73
59
  if (key === 'key' || key === 'children' || key === 'ref') continue;
74
60
  if (key === 'dangerouslySetInnerHTML') {
75
61
  domElement.innerHTML = props[key].__html;
76
- } else if (key === 'innerHTML' || key === 'textContent' || key === 'innerText') {
77
- domElement[key] = props[key];
62
+ } else if (key === 'innerHTML' || key === 'textContent' || key === 'innerText' || key === 'value' && ['textarea', 'select'].includes(domElement.tagName)) {
63
+ const value = props[key];
64
+ if (value != null) domElement[key] = value;
78
65
  } else if (key.startsWith('on')) {
79
66
  domElement[key.toLowerCase()] = props[key];
80
67
  } else {
@@ -82,7 +69,7 @@ function mountAttributes(domElement, props, env) {
82
69
  }
83
70
  }
84
71
  }
85
- var attrMap = {
72
+ const attrMap = {
86
73
  className: 'class',
87
74
  labelFor: 'for'
88
75
  };
@@ -93,7 +80,7 @@ function setDOMAttribute(el, attr, value, isSVG) {
93
80
  } else if (value === false) {
94
81
  el.removeAttribute(attr);
95
82
  } else {
96
- var namespace = isSVG ? NS_ATTRS[attr] : undefined;
83
+ const namespace = isSVG ? NS_ATTRS[attr] : undefined;
97
84
  if (namespace !== undefined) {
98
85
  el.setAttributeNS(namespace, attr, value);
99
86
  } else {
@@ -101,116 +88,83 @@ function setDOMAttribute(el, attr, value, isSVG) {
101
88
  }
102
89
  }
103
90
  }
104
- function mount(vnode, env) {
105
- if (env === void 0) {
106
- env = DEFAULT_ENV;
107
- }
91
+ function flatten(arr) {
92
+ return arr.reduce((prev, item) => prev.concat(item), []);
93
+ }
94
+ function mountChildren(children, env) {
95
+ return Array.isArray(children) ? flatten(children.map(child => mountChildren(child, env))) : mount(children, env);
96
+ }
97
+ function mount(vnode, env = DEFAULT_ENV) {
108
98
  if (vnode == null || typeof vnode === 'boolean') {
109
- return {
110
- type: MOUNT_SINGLE,
111
- node: null
112
- };
99
+ return null;
113
100
  }
114
101
  if (vnode instanceof Node) {
115
- return {
116
- type: MOUNT_SINGLE,
117
- node: vnode
118
- };
102
+ return vnode;
119
103
  }
120
104
  if (isRenderFunction(vnode)) {
121
- var _ref = vnode,
122
- type = _ref.type,
123
- props = _ref.props;
105
+ const {
106
+ type,
107
+ props
108
+ } = vnode;
124
109
  if (type === Fragment) {
125
- var node = document.createDocumentFragment();
110
+ const node = document.createDocumentFragment();
126
111
  if (props.children) {
127
- var childrenRef = mount(props.children, env);
128
- insertDom(node, childrenRef);
112
+ const children = mountChildren(props.children, env);
113
+ insertDom(node, children);
129
114
  }
130
- return {
131
- type: MOUNT_SINGLE,
132
- node: node
133
- };
115
+ return node;
134
116
  }
135
- var childVNode = type(props);
117
+ const childVNode = type(props);
136
118
  return mount(childVNode, env);
137
119
  }
138
120
  if (isLeaf(vnode)) {
139
- return {
140
- type: MOUNT_SINGLE,
141
- node: document.createTextNode("" + vnode)
142
- };
121
+ return document.createTextNode(`${vnode}`);
143
122
  }
144
123
  if (isElement(vnode)) {
145
- var _node;
146
- var _ref2 = vnode,
147
- _type = _ref2.type,
148
- _props = _ref2.props;
149
- if (!env.isSvg && _type === 'svg') {
124
+ let node;
125
+ const {
126
+ type,
127
+ props
128
+ } = vnode;
129
+ if (!env.isSvg && type === 'svg') {
150
130
  env = Object.assign({}, env, {
151
131
  isSvg: true
152
132
  });
153
133
  }
154
134
  if (!env.isSvg) {
155
- _node = document.createElement(_type);
135
+ node = document.createElement(type);
156
136
  } else {
157
- _node = document.createElementNS(SVG_NS, _type);
137
+ node = document.createElementNS(SVG_NS, type);
158
138
  }
159
- mountAttributes(_node, _props, env);
160
- var _childrenRef;
161
- if (_props.children) {
162
- var childEnv = env;
163
- if (env.isSvg && _type === 'foreignObject') {
139
+ mountAttributes(node, props, env);
140
+ if (props.children) {
141
+ let childEnv = env;
142
+ if (env.isSvg && type === 'foreignObject') {
164
143
  childEnv = Object.assign({}, childEnv, {
165
144
  isSvg: false
166
145
  });
167
146
  }
168
- _childrenRef = mount(_props.children, childEnv);
147
+ const children = mountChildren(props.children, childEnv);
148
+ if (children != null) insertDom(node, children);
169
149
  }
170
- if (_childrenRef != null) insertDom(_node, _childrenRef);
171
- var ref = _props.ref;
172
- if (typeof ref === 'function') ref(_node);
173
- return {
174
- type: MOUNT_SINGLE,
175
- node: _node
176
- };
177
- }
178
- if (Array.isArray(vnode)) {
179
- return {
180
- type: MOUNT_ARRAY,
181
- children: vnode.map(function (child) {
182
- return mount(child, env);
183
- })
184
- };
150
+ const {
151
+ ref
152
+ } = props;
153
+ if (typeof ref === 'function') ref(node);
154
+ return node;
185
155
  }
186
156
  throw new Error('mount: Invalid Vnode!');
187
157
  }
188
- function flattenWithoutNull(array) {
189
- var result = [];
190
- for (var i = 0; i < array.length; i += 1) {
191
- var item = array[i];
192
- if (Array.isArray(item)) result = result.concat(flattenWithoutNull(item));else if (item != null) result.push(item);
193
- }
194
- return result;
195
- }
196
- function asDom(result) {
197
- if (result.type === MOUNT_SINGLE) {
198
- return result.node;
199
- }
200
- return result.children.map(asDom);
201
- }
158
+
202
159
  /**
203
160
  * Mount vdom as real DOM nodes.
204
161
  */
205
-
206
162
  function mountDom(vnode) {
207
- if (Array.isArray(vnode)) {
208
- return flattenWithoutNull(vnode.map(mountDom));
209
- }
210
- return asDom(mount(vnode));
163
+ return mount(vnode);
211
164
  }
212
165
 
213
166
  const clsToolbarItem = 'mm-toolbar-item';
167
+ const clsActive = 'active';
214
168
  function renderBrand() {
215
169
  return jsxs("a", {
216
170
  className: "mm-toolbar-brand",
@@ -243,7 +197,7 @@ function safeCaller(fn) {
243
197
  try {
244
198
  await promise;
245
199
  } finally {
246
- promise = null;
200
+ promise = undefined;
247
201
  }
248
202
  };
249
203
  }
@@ -251,7 +205,7 @@ class Toolbar {
251
205
  static create(mm) {
252
206
  const toolbar = new Toolbar();
253
207
  toolbar.attach(mm);
254
- return toolbar.render();
208
+ return toolbar;
255
209
  }
256
210
  static icon(path, attrs = {}) {
257
211
  attrs = _extends({
@@ -271,7 +225,10 @@ class Toolbar {
271
225
  constructor() {
272
226
  this.showBrand = true;
273
227
  this.registry = {};
274
- this.markmap = null;
228
+ this.el = mountDom(jsx("div", {
229
+ className: "mm-toolbar"
230
+ }));
231
+ this.items = [...Toolbar.defaultItems];
275
232
  this.register({
276
233
  id: 'zoomIn',
277
234
  title: 'Zoom in',
@@ -290,10 +247,24 @@ class Toolbar {
290
247
  content: Toolbar.icon('M4 7h2v-2h2v4h-4zM4 13h2v2h2v-4h-4zM16 7h-2v-2h-2v4h4zM16 13h-2v2h-2v-4h4z'),
291
248
  onClick: this.getHandler(mm => mm.fit())
292
249
  });
293
- this.setItems(Toolbar.defaultItems);
250
+ this.register({
251
+ id: 'recurse',
252
+ title: 'Toggle recursively',
253
+ content: Toolbar.icon('M16 4h-12v12h12v-8h-8v4h2v-2h4v4h-8v-8h10z'),
254
+ onClick: e => {
255
+ var _this$markmap;
256
+ const button = e.target.closest(`.${clsToolbarItem}`);
257
+ const active = button == null ? void 0 : button.classList.toggle(clsActive);
258
+ (_this$markmap = this.markmap) == null ? void 0 : _this$markmap.setOptions({
259
+ toggleRecursively: active
260
+ });
261
+ }
262
+ });
263
+ this.render();
294
264
  }
295
265
  setBrand(show) {
296
266
  this.showBrand = show;
267
+ return this.render();
297
268
  }
298
269
  register(data) {
299
270
  this.registry[data.id] = data;
@@ -306,6 +277,7 @@ class Toolbar {
306
277
  }
307
278
  setItems(items) {
308
279
  this.items = [...items];
280
+ return this.render();
309
281
  }
310
282
  attach(mm) {
311
283
  this.markmap = mm;
@@ -319,10 +291,13 @@ class Toolbar {
319
291
  }
320
292
  return item;
321
293
  }).filter(Boolean);
322
- return mountDom(jsxs("div", {
323
- className: "mm-toolbar",
294
+ while (this.el.firstChild) {
295
+ this.el.firstChild.remove();
296
+ }
297
+ this.el.append(mountDom(jsxs(Fragment, {
324
298
  children: [this.showBrand && renderBrand(), items.map(renderItem)]
325
- }));
299
+ })));
300
+ return this.el;
326
301
  }
327
302
  }
328
303
  Toolbar.defaultItems = ['zoomIn', 'zoomOut', 'fit', 'recurse'];
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
- /*! markmap-toolbar v0.15.0-alpha.0 | MIT License */
1
+ /*! markmap-toolbar v0.15.0 | MIT License */
2
2
  import { mountDom } from '@gera2ld/jsx-dom';
3
- import { jsx, jsxs } from '@gera2ld/jsx-dom/jsx-runtime';
3
+ import { jsx, jsxs, Fragment } from '@gera2ld/jsx-dom/jsx-runtime';
4
4
 
5
5
  function _extends() {
6
6
  _extends = Object.assign ? Object.assign.bind() : function (target) {
@@ -18,6 +18,7 @@ function _extends() {
18
18
  }
19
19
 
20
20
  const clsToolbarItem = 'mm-toolbar-item';
21
+ const clsActive = 'active';
21
22
  function renderBrand() {
22
23
  return jsxs("a", {
23
24
  className: "mm-toolbar-brand",
@@ -50,7 +51,7 @@ function safeCaller(fn) {
50
51
  try {
51
52
  await promise;
52
53
  } finally {
53
- promise = null;
54
+ promise = undefined;
54
55
  }
55
56
  };
56
57
  }
@@ -58,7 +59,7 @@ class Toolbar {
58
59
  static create(mm) {
59
60
  const toolbar = new Toolbar();
60
61
  toolbar.attach(mm);
61
- return toolbar.render();
62
+ return toolbar;
62
63
  }
63
64
  static icon(path, attrs = {}) {
64
65
  attrs = _extends({
@@ -78,7 +79,10 @@ class Toolbar {
78
79
  constructor() {
79
80
  this.showBrand = true;
80
81
  this.registry = {};
81
- this.markmap = null;
82
+ this.el = mountDom(jsx("div", {
83
+ className: "mm-toolbar"
84
+ }));
85
+ this.items = [...Toolbar.defaultItems];
82
86
  this.register({
83
87
  id: 'zoomIn',
84
88
  title: 'Zoom in',
@@ -97,10 +101,24 @@ class Toolbar {
97
101
  content: Toolbar.icon('M4 7h2v-2h2v4h-4zM4 13h2v2h2v-4h-4zM16 7h-2v-2h-2v4h4zM16 13h-2v2h-2v-4h4z'),
98
102
  onClick: this.getHandler(mm => mm.fit())
99
103
  });
100
- this.setItems(Toolbar.defaultItems);
104
+ this.register({
105
+ id: 'recurse',
106
+ title: 'Toggle recursively',
107
+ content: Toolbar.icon('M16 4h-12v12h12v-8h-8v4h2v-2h4v4h-8v-8h10z'),
108
+ onClick: e => {
109
+ var _this$markmap;
110
+ const button = e.target.closest(`.${clsToolbarItem}`);
111
+ const active = button == null ? void 0 : button.classList.toggle(clsActive);
112
+ (_this$markmap = this.markmap) == null ? void 0 : _this$markmap.setOptions({
113
+ toggleRecursively: active
114
+ });
115
+ }
116
+ });
117
+ this.render();
101
118
  }
102
119
  setBrand(show) {
103
120
  this.showBrand = show;
121
+ return this.render();
104
122
  }
105
123
  register(data) {
106
124
  this.registry[data.id] = data;
@@ -113,6 +131,7 @@ class Toolbar {
113
131
  }
114
132
  setItems(items) {
115
133
  this.items = [...items];
134
+ return this.render();
116
135
  }
117
136
  attach(mm) {
118
137
  this.markmap = mm;
@@ -126,10 +145,13 @@ class Toolbar {
126
145
  }
127
146
  return item;
128
147
  }).filter(Boolean);
129
- return mountDom(jsxs("div", {
130
- className: "mm-toolbar",
148
+ while (this.el.firstChild) {
149
+ this.el.firstChild.remove();
150
+ }
151
+ this.el.append(mountDom(jsxs(Fragment, {
131
152
  children: [this.showBrand && renderBrand(), items.map(renderItem)]
132
- }));
153
+ })));
154
+ return this.el;
133
155
  }
134
156
  }
135
157
  Toolbar.defaultItems = ['zoomIn', 'zoomOut', 'fit', 'recurse'];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markmap-toolbar",
3
- "version": "0.15.0-alpha.0",
3
+ "version": "0.15.0",
4
4
  "description": "Extensible toolbar for markmap",
5
5
  "author": "Gerald <gera2ld@live.com>",
6
6
  "license": "MIT",
@@ -35,12 +35,12 @@
35
35
  "markmap-common": "*"
36
36
  },
37
37
  "devDependencies": {
38
- "markmap-common": "0.15.0-alpha.0",
39
- "markmap-view": "0.15.0-alpha.0"
38
+ "markmap-common": "0.15.0",
39
+ "markmap-view": "0.15.0"
40
40
  },
41
41
  "dependencies": {
42
- "@babel/runtime": "^7.16.3",
43
- "@gera2ld/jsx-dom": "^2.1.1"
42
+ "@babel/runtime": "^7.22.6",
43
+ "@gera2ld/jsx-dom": "^2.2.2"
44
44
  },
45
- "gitHead": "cea14d4791b8483481a99baa5d256017f52f267c"
45
+ "gitHead": "9c13bae5e28346ec613362046a023839ef64538b"
46
46
  }
@@ -1,7 +1,7 @@
1
1
  import { VChildren } from '@gera2ld/jsx-dom';
2
2
  import type { Markmap } from 'markmap-view';
3
3
  import './style.css';
4
- interface IToolbarItem {
4
+ export interface IToolbarItem {
5
5
  id?: string;
6
6
  title?: string;
7
7
  content: VChildren;
@@ -12,17 +12,17 @@ export declare class Toolbar {
12
12
  private registry;
13
13
  private markmap;
14
14
  static defaultItems: (string | IToolbarItem)[];
15
+ el: HTMLDivElement;
15
16
  items: (string | IToolbarItem)[];
16
- static create(mm: Markmap): Node;
17
+ static create(mm: Markmap): Toolbar;
17
18
  static icon(path: string, attrs?: {}): import("@gera2ld/jsx-dom").VNode;
18
19
  constructor();
19
- setBrand(show: boolean): void;
20
+ setBrand(show: boolean): HTMLDivElement;
20
21
  register(data: IToolbarItem & {
21
22
  id: string;
22
23
  }): void;
23
24
  getHandler(handle: (mm: Markmap) => Promise<void>): () => void;
24
- setItems(items: (string | IToolbarItem)[]): void;
25
+ setItems(items: (string | IToolbarItem)[]): HTMLDivElement;
25
26
  attach(mm: Markmap): void;
26
- render(): Node;
27
+ render(): HTMLDivElement;
27
28
  }
28
- export {};