Skip to content

Commit c0d70f8

Browse files
committed
fix: move active node into viewport
fix #56
1 parent dac97ce commit c0d70f8

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

assets/app.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ let root;
44
let style;
55
const handlers = {
66
setData(data) {
7-
mm.setData(root = data.root, markmap.deriveOptions(data.jsonOptions) || {});
7+
mm.setData(
8+
(root = data.root),
9+
markmap.deriveOptions(data.jsonOptions) || {},
10+
);
811
if (firstTime) {
912
mm.fit();
1013
firstTime = false;
@@ -29,12 +32,12 @@ const handlers = {
2932
document.documentElement.classList[dark ? 'add' : 'remove']('markmap-dark');
3033
},
3134
};
32-
window.addEventListener('message', e => {
35+
window.addEventListener('message', (e) => {
3336
const { type, data } = e.data;
3437
const handler = handlers[type];
3538
handler?.(data);
3639
});
37-
document.addEventListener('click', e => {
40+
document.addEventListener('click', (e) => {
3841
const el = e.target?.closest('a');
3942
if (el) {
4043
const href = el.getAttribute('href');
@@ -61,7 +64,14 @@ toolbar.register({
6164
content: createButton('Export'),
6265
onClick: clickHandler('exportAsHtml'),
6366
});
64-
toolbar.setItems(['zoomIn', 'zoomOut', 'fit', 'recurse', 'editAsText', 'exportAsHtml']);
67+
toolbar.setItems([
68+
'zoomIn',
69+
'zoomOut',
70+
'fit',
71+
'recurse',
72+
'editAsText',
73+
'exportAsHtml',
74+
]);
6575
setTimeout(() => {
6676
toolbar.attach(mm);
6777
document.body.append(toolbar.el);
@@ -82,11 +92,11 @@ function clickHandler(type) {
8292

8393
function findActiveNode(line) {
8494
function dfs(node) {
85-
const lines = node.p?.lines;
86-
if (lines && lines[0] <= line && line < lines[1]) {
95+
const [start, end] = node.payload?.lines?.split(',').map((s) => +s) || [];
96+
if (start >= 0 && start <= line && line < end) {
8797
best = node;
8898
}
89-
node.c?.forEach(dfs);
99+
node.children?.forEach(dfs);
90100
}
91101
let best;
92102
dfs(root);

src/extension.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const renderToolbar = () => {
3535
};
3636

3737
class MarkmapEditor implements CustomTextEditorProvider {
38-
constructor(private context: ExtensionContext) {}
38+
constructor(private context: ExtensionContext) { }
3939

4040
private resolveAssetPath(relPath: string) {
4141
return Utils.joinPath(this.context.extensionUri, relPath);
@@ -153,6 +153,7 @@ class MarkmapEditor implements CustomTextEditorProvider {
153153
const debouncedUpdateCursor = debounce(updateCursor, 300);
154154
const debouncedUpdate = debounce(update, 300);
155155

156+
const logger = vscodeWindow.createOutputChannel('Markmap');
156157
const messageHandlers: { [key: string]: (data?: any) => void } = {
157158
refresh: () => {
158159
update();
@@ -186,11 +187,11 @@ class MarkmapEditor implements CustomTextEditorProvider {
186187
styles: [
187188
...(customCSS
188189
? [
189-
{
190-
type: 'style',
191-
data: customCSS,
192-
} as CSSItem,
193-
]
190+
{
191+
type: 'style',
192+
data: customCSS,
193+
} as CSSItem,
194+
]
194195
: []),
195196
],
196197
scripts: [
@@ -256,21 +257,20 @@ class MarkmapEditor implements CustomTextEditorProvider {
256257
const filePath = Utils.joinPath(Utils.dirname(document.uri), relPath);
257258
commands.executeCommand('vscode.open', filePath);
258259
},
259-
};
260-
const logger = vscodeWindow.createOutputChannel('Markmap');
261-
messageHandlers.log = (data: string) => {
262-
logger.appendLine(data);
260+
log(data: string) {
261+
logger.appendLine(data);
262+
},
263263
};
264264
webviewPanel.webview.onDidReceiveMessage((e) => {
265265
const handler = messageHandlers[e.type];
266266
handler?.(e.data);
267267
});
268268
workspace.onDidChangeTextDocument((e) => {
269-
if (e.document === document) {
270-
debouncedUpdate();
271-
}
269+
if (e.document !== document) return;
270+
debouncedUpdate();
272271
});
273-
vscodeWindow.onDidChangeTextEditorSelection(() => {
272+
vscodeWindow.onDidChangeTextEditorSelection((e) => {
273+
if (e.textEditor.document !== document) return;
274274
debouncedUpdateCursor();
275275
});
276276
vscodeWindow.onDidChangeActiveColorTheme(updateTheme);

0 commit comments

Comments
 (0)