Skip to content

Commit bbcc309

Browse files
committed
feat: focus text editor on double click
close #59
1 parent bbceaa5 commit bbcc309

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

src/app.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ 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+
);
4156
activeNodeOptions.placement = data.jsonOptions?.activeNode?.placement;
4257
if (firstTime) {
4358
await mm.fit();

src/extension.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import {
66
ColorThemeKind,
77
CustomTextEditorProvider,
88
ExtensionContext,
9+
Position,
10+
Selection,
11+
TabInputText,
912
TextDocument,
1013
Uri,
1114
ViewColumn,
@@ -123,6 +126,7 @@ class MarkmapEditor implements CustomTextEditorProvider {
123126
};
124127
let globalOptions: IMarkmapJSONOptions & {
125128
autoExpand?: boolean;
129+
htmlParser?: unknown;
126130
};
127131
let customCSS: string;
128132
const updateOptions = () => {
@@ -288,6 +292,22 @@ class MarkmapEditor implements CustomTextEditorProvider {
288292
const filePath = Utils.joinPath(Utils.dirname(document.uri), relPath);
289293
commands.executeCommand('vscode.open', filePath);
290294
},
295+
async setFocus(line: number) {
296+
const viewColumn = vscodeWindow.tabGroups.all
297+
.flatMap((group) => group.tabs)
298+
.find(
299+
(tab) =>
300+
tab.input instanceof TabInputText &&
301+
tab.group &&
302+
tab.input.uri.toString() === document.uri.toString(),
303+
)?.group.viewColumn;
304+
const editor = await vscodeWindow.showTextDocument(document, {
305+
viewColumn,
306+
});
307+
const pos = new Position(line, 0);
308+
editor.selection = new Selection(pos, pos);
309+
editor.revealRange(editor.selection);
310+
},
291311
log(data: string) {
292312
logger.appendLine(data);
293313
},

src/postbuild.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ async function fetchAssets() {
1919
transformer.urlBuilder.provider = provider;
2020
const paths = [
2121
...(assets.scripts?.map(
22-
(item) => (item.type === 'script' && item.data.src) || ''
22+
(item) => (item.type === 'script' && item.data.src) || '',
2323
) || []),
2424
...(assets.styles?.map(
25-
(item) => (item.type === 'stylesheet' && item.data.href) || ''
25+
(item) => (item.type === 'stylesheet' && item.data.href) || '',
2626
) || []),
2727
]
2828
.filter((url) => url.startsWith(ASSETS_PREFIX))
@@ -32,9 +32,9 @@ async function fetchAssets() {
3232
paths.map((path) =>
3333
downloadAsset(
3434
resolve(ASSETS_PREFIX, path),
35-
transformer.urlBuilder.getFullUrl(path, fastest)
36-
)
37-
)
35+
transformer.urlBuilder.getFullUrl(path, fastest),
36+
),
37+
),
3838
);
3939
}
4040

@@ -52,8 +52,8 @@ async function downloadAsset(fullPath: string, url: string) {
5252
await mkdir(dirname(fullPath), { recursive: true });
5353
await finished(
5454
Readable.fromWeb(res.body as ReadableStream).pipe(
55-
createWriteStream(fullPath)
56-
)
55+
createWriteStream(fullPath),
56+
),
5757
);
5858
}
5959

src/util.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { buildCSSItem, buildJSItem } from 'markmap-common';
2-
import { builtInPlugins, IAssets, ITransformPlugin, Transformer } from 'markmap-lib';
2+
import {
3+
builtInPlugins,
4+
IAssets,
5+
ITransformPlugin,
6+
Transformer,
7+
} from 'markmap-lib';
38
import { baseJsPaths } from 'markmap-render';
49

510
const TOOLBAR_VERSION = process.env.TOOLBAR_VERSION;

0 commit comments

Comments
 (0)