|
1 | | -import { IDeferred, defer, type INode } from 'markmap-common'; |
| 1 | +import { IDeferred, defer, wrapFunction, type INode } from 'markmap-common'; |
2 | 2 | import { Toolbar } from 'markmap-toolbar'; |
3 | 3 | import { |
4 | 4 | defaultOptions, |
@@ -38,21 +38,6 @@ const handlers = { |
38 | 38 | ...defaultOptions, |
39 | 39 | ...deriveOptions(data.jsonOptions), |
40 | 40 | }); |
41 | | - mm.g |
42 | | - .selectAll<SVGGElement, INode>(function () { |
43 | | - const nodes = Array.from(this.childNodes) as Element[]; |
44 | | - return nodes.filter((el) => el.tagName === 'g') as SVGGElement[]; |
45 | | - }) |
46 | | - .on( |
47 | | - 'dblclick.focus', |
48 | | - (e, d) => { |
49 | | - const lines = d.payload?.lines as string | undefined; |
50 | | - const line = +lines?.split(',')[0]; |
51 | | - if (!isNaN(line)) |
52 | | - vscode.postMessage({ type: 'setFocus', data: line }); |
53 | | - }, |
54 | | - true, |
55 | | - ); |
56 | 41 | activeNodeOptions.placement = data.jsonOptions?.activeNode?.placement; |
57 | 42 | if (firstTime) { |
58 | 43 | await mm.fit(); |
@@ -132,10 +117,32 @@ toolbar.setItems([ |
132 | 117 | checkTheme(); |
133 | 118 |
|
134 | 119 | setTimeout(() => { |
| 120 | + initialize(mm); |
135 | 121 | toolbar.attach(mm); |
136 | 122 | document.body.append(toolbar.el); |
137 | 123 | }); |
138 | 124 |
|
| 125 | +function initialize(mm: Markmap) { |
| 126 | + mm.renderData = wrapFunction(mm.renderData, async (fn, ...args) => { |
| 127 | + await fn.call(mm, ...args); |
| 128 | + mm.g |
| 129 | + .selectAll<SVGGElement, INode>(function () { |
| 130 | + const nodes = Array.from(this.childNodes) as Element[]; |
| 131 | + return nodes.filter((el) => el.tagName === 'g') as SVGGElement[]; |
| 132 | + }) |
| 133 | + .on( |
| 134 | + 'dblclick.focus', |
| 135 | + (e, d) => { |
| 136 | + const lines = d.payload?.lines as string | undefined; |
| 137 | + const line = +lines?.split(',')[0]; |
| 138 | + if (!isNaN(line)) |
| 139 | + vscode.postMessage({ type: 'setFocus', data: line }); |
| 140 | + }, |
| 141 | + true, |
| 142 | + ); |
| 143 | + }); |
| 144 | +} |
| 145 | + |
139 | 146 | function checkTheme() { |
140 | 147 | // https://code.visualstudio.com/api/extension-guides/webview#theming-webview-content |
141 | 148 | const isDark = ['vscode-dark', 'vscode-high-contrast'].some((cls) => |
|
0 commit comments