Skip to content

Commit ea21887

Browse files
authored
fix(code-viewer): support SSR (#192)
1 parent 379f748 commit ea21887

5 files changed

Lines changed: 7 additions & 50 deletions

File tree

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
"refractor": "^3.0.0",
5959
"tslib": "^1.12.0",
6060
"use-debounce": "^3.4.3",
61-
"use-resize-observer": "^6.1.0",
6261
"yup": "^0.28.3"
6362
},
6463
"devDependencies": {

src/CodeViewer/__tests__/Viewer.spec.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ jest.mock('../components/BlockCodeViewer/ObservableSet', () => ({
1919
},
2020
}));
2121

22-
jest.mock('use-resize-observer');
23-
2422
describe('Code Viewer component', () => {
2523
afterEach(() => {
2624
(parseCode as jest.Mock).mockReset();

src/CodeViewer/components/BlockCodeViewer/BlockCodeViewer.tsx

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import cn from 'classnames';
2-
import { debounce } from 'lodash';
32
import * as React from 'react';
4-
import useResizeObserver from 'use-resize-observer';
53

64
import { Classes } from '../../../classes';
7-
import { SINGLE_LINE_SIZE } from './consts';
85
import { useSlicedBlocks } from './hooks/useSlicedBlocks';
96
import { ObservableSet } from './ObservableSet';
107
import { SingleCodeBlock } from './SingleCodeBlock';
@@ -15,29 +12,17 @@ export interface IBlockCodeViewerProps extends React.HTMLAttributes<HTMLPreEleme
1512
showLineNumbers: boolean;
1613
}
1714

18-
function calculateMaxLines(height: number) {
19-
return Math.floor(Math.min(window.innerHeight, height) / SINGLE_LINE_SIZE) + 1;
20-
}
21-
2215
const BlockCodeViewer: React.FC<IBlockCodeViewerProps> = ({ className, language, value, showLineNumbers, ...rest }) => {
2316
const nodeRef = React.useRef<HTMLPreElement | null>(null);
24-
const [maxLines, setMaxLines] = React.useState<number | null>(null);
17+
const maxLines = 100;
2518
const [observer, setObserver] = React.useState<IntersectionObserver>();
2619
const viewportSet = React.useRef(new ObservableSet());
27-
const slicedBlocks = useSlicedBlocks(value, maxLines === null ? null : Math.max(0, maxLines - 1));
28-
const lineNumberCharacterCount = String(
29-
slicedBlocks !== null && maxLines !== null ? slicedBlocks.length * maxLines : 0,
30-
).length;
31-
32-
React.useLayoutEffect(() => {
33-
if (nodeRef.current !== null) {
34-
setMaxLines(calculateMaxLines(window.innerHeight)); // we have to use window here, as element may not ave any height at this time
35-
}
36-
}, [nodeRef]);
20+
const slicedBlocks = useSlicedBlocks(value ?? '', maxLines - 1);
21+
const lineNumberCharacterCount = String(slicedBlocks.length * maxLines).length;
3722

3823
React.useEffect(() => {
3924
const { current: viewport } = viewportSet;
40-
if (nodeRef.current === null || maxLines === null) {
25+
if (nodeRef.current === null) {
4126
return;
4227
}
4328

@@ -71,21 +56,7 @@ const BlockCodeViewer: React.FC<IBlockCodeViewerProps> = ({ className, language,
7156
setObserver(void 0);
7257
observer.disconnect();
7358
};
74-
}, [nodeRef, maxLines]);
75-
76-
useResizeObserver({
77-
onResize: debounce(
78-
({ height }) => {
79-
const newMaxLines = calculateMaxLines(height);
80-
if (newMaxLines !== maxLines) {
81-
setMaxLines(newMaxLines);
82-
}
83-
},
84-
250,
85-
{ leading: true },
86-
),
87-
ref: nodeRef,
88-
});
59+
}, [nodeRef]);
8960

9061
return (
9162
<pre

src/CodeViewer/components/BlockCodeViewer/hooks/useSlicedBlocks.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@ function createSlicedBlock(): SlicedBlock {
1414
};
1515
}
1616

17-
export const useSlicedBlocks = (value: string, maxLines: number | null) => {
18-
return React.useMemo<SlicedBlock[] | null>(() => {
19-
if (maxLines === null) {
20-
return null;
21-
}
22-
17+
export const useSlicedBlocks = (value: string, maxLines: number) => {
18+
return React.useMemo<SlicedBlock[]>(() => {
2319
const blocks: SlicedBlock[] = [createSlicedBlock()];
2420

2521
for (let i = 0, n = 0; i < value.length; i++) {

yarn.lock

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15863,13 +15863,6 @@ use-debounce@^3.4.3:
1586315863
resolved "https://registry.yarnpkg.com/use-debounce/-/use-debounce-3.4.3.tgz#5df9322322b3f1b1c263d46413f9facf6d8b56ab"
1586415864
integrity sha512-nxy+opOxDccWfhMl36J5BSCTpvcj89iaQk2OZWLAtBJQj7ISCtx1gh+rFbdjGfMl6vtCZf6gke/kYvrkVfHMoA==
1586515865

15866-
use-resize-observer@^6.1.0:
15867-
version "6.1.0"
15868-
resolved "https://registry.yarnpkg.com/use-resize-observer/-/use-resize-observer-6.1.0.tgz#d4d267a940dbf9c326da6042f8a4bb8c89d29729"
15869-
integrity sha512-SiPcWHiIQ1CnHmb6PxbYtygqiZXR0U9dNkkbpX9VYnlstUwF8+QqpUTrzh13pjPwcjMVGR+QIC+nvF5ujfFNng==
15870-
dependencies:
15871-
resize-observer-polyfill "^1.5.1"
15872-
1587315866
use-sidecar@^1.0.1:
1587415867
version "1.0.2"
1587515868
resolved "https://registry.yarnpkg.com/use-sidecar/-/use-sidecar-1.0.2.tgz#e72f582a75842f7de4ef8becd6235a4720ad8af6"

0 commit comments

Comments
 (0)