Skip to content

Commit 4e7df17

Browse files
authored
Merge pull request #12 from ts-graphviz/auto-fit-height-on-editor
Auto resize the editor
2 parents e22a1a6 + a76f8ad commit 4e7df17

File tree

3 files changed

+39
-8
lines changed
  • docs/ts-graphviz/02-core-features
  • i18n/ja/docusaurus-plugin-content-docs/current/ts-graphviz/02-core-features
  • src/components/TSGraphvizLiveEditor

3 files changed

+39
-8
lines changed

docs/ts-graphviz/02-core-features/index.mdx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ import TSGraphvizLiveEditor from '@site/src/components/TSGraphvizLiveEditor';
1313
**Playground:**
1414

1515
<TSGraphvizLiveEditor
16-
script={`
17-
import { digraph, attribute as _ } from 'ts-graphviz';
16+
script={`import { digraph, attribute as _ } from 'ts-graphviz';
1817
1918
const G = digraph('G', (g) => {
2019
g.node('A', { [_.color]: 'red' });
2120
g.edge(['A', 'B'], { [_.label]: 'A to B' });
2221
});`}
23-
height={150}
2422
readOnly
2523
/>
2624

i18n/ja/docusaurus-plugin-content-docs/current/ts-graphviz/02-core-features/index.mdx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ import TSGraphvizLiveEditor from '@site/src/components/TSGraphvizLiveEditor';
1212
**プレイグラウンド:**
1313

1414
<TSGraphvizLiveEditor
15-
script={`
16-
import { digraph, attribute as _ } from 'ts-graphviz';
15+
script={`import { digraph, attribute as _ } from 'ts-graphviz';
1716
1817
const G = digraph('G', (g) => {
1918
g.node('A', { [_.color]: 'red' });
2019
g.edge(['A', 'B'], { [_.label]: 'A to B' });
2120
});`}
22-
height={150}
2321
readOnly
2422
/>
2523

src/components/TSGraphvizLiveEditor/index.tsx

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import useBaseUrl from '@docusaurus/useBaseUrl';
33
import { Editor as Monaco, useMonaco } from '@monaco-editor/react';
44
import type monaco_editor from 'monaco-editor';
55
import type { editor } from 'monaco-editor';
6-
import { memo, useEffect, useMemo } from 'react';
6+
import { memo, useCallback, useEffect, useMemo } from 'react';
77

88
interface Props {
99
script: string;
@@ -21,6 +21,7 @@ const MONACO_THEMES: Record<ColorMode, string> = {
2121

2222
function TSGraphvizLiveEditor({
2323
script,
24+
// Auto resize the editor to fit the content height
2425
onMount,
2526
readOnly,
2627
}: Props): JSX.Element {
@@ -31,6 +32,40 @@ function TSGraphvizLiveEditor({
3132
() => (colorMode === 'dark' ? MONACO_THEMES.dark : MONACO_THEMES.light),
3233
[colorMode],
3334
);
35+
const onMountCallback = useCallback(
36+
onMount ??
37+
((editor: editor.IStandaloneCodeEditor) => {
38+
const lineHeight = editor.getOption(
39+
monaco.editor.EditorOption.lineHeight,
40+
);
41+
const currentLayout = editor.getLayoutInfo();
42+
const MIN_HEIGHT = 100;
43+
const MAX_HEIGHT = 500;
44+
const PADDING = 10;
45+
resize();
46+
47+
// Auto-resize on content changes
48+
if (!readOnly) {
49+
editor.getModel().onDidChangeContent(() => {
50+
resize();
51+
});
52+
}
53+
function resize() {
54+
const newHeight = Math.min(
55+
Math.max(
56+
editor.getModel().getLineCount() * lineHeight + PADDING,
57+
MIN_HEIGHT,
58+
),
59+
MAX_HEIGHT,
60+
);
61+
editor.layout({
62+
height: newHeight,
63+
width: currentLayout.contentWidth,
64+
});
65+
}
66+
}),
67+
[monaco, readOnly],
68+
);
3469
const dtsUrl = useBaseUrl('/dts.json');
3570
useEffect(() => {
3671
if (monaco) {
@@ -70,7 +105,7 @@ function TSGraphvizLiveEditor({
70105
lineDecorationsWidth: 0,
71106
readOnly,
72107
}}
73-
onMount={onMount}
108+
onMount={onMountCallback}
74109
/>
75110
) : null}
76111
</>

0 commit comments

Comments
 (0)