Skip to content

Commit 247153f

Browse files
committed
fix: always rebind events on render
close #65
1 parent 1b91c89 commit 247153f

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

src/app.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IDeferred, defer, type INode } from 'markmap-common';
1+
import { IDeferred, defer, wrapFunction, type INode } from 'markmap-common';
22
import { Toolbar } from 'markmap-toolbar';
33
import {
44
defaultOptions,
@@ -38,21 +38,6 @@ const handlers = {
3838
...defaultOptions,
3939
...deriveOptions(data.jsonOptions),
4040
});
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-
);
5641
activeNodeOptions.placement = data.jsonOptions?.activeNode?.placement;
5742
if (firstTime) {
5843
await mm.fit();
@@ -132,10 +117,32 @@ toolbar.setItems([
132117
checkTheme();
133118

134119
setTimeout(() => {
120+
initialize(mm);
135121
toolbar.attach(mm);
136122
document.body.append(toolbar.el);
137123
});
138124

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+
139146
function checkTheme() {
140147
// https://code.visualstudio.com/api/extension-guides/webview#theming-webview-content
141148
const isDark = ['vscode-dark', 'vscode-high-contrast'].some((cls) =>

0 commit comments

Comments
 (0)