Skip to content

Commit 1c2a4b9

Browse files
authored
docs: Don't SSR hidden editor tabs (#3136)
1 parent d8d7a54 commit 1c2a4b9

File tree

5 files changed

+102
-96
lines changed

5 files changed

+102
-96
lines changed

website/docusaurus.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,14 @@ const config: Config = {
318318
to: '/docs/getting-started/resource',
319319
from: ['/docs/getting-started/endpoint'],
320320
},
321+
{
322+
to: '/docs/api/useQuery',
323+
from: ['/rest/api/IndexEndpoint'],
324+
},
325+
{
326+
to: '/rest/api/schema',
327+
from: ['/rest/api/Schema'],
328+
},
321329
{
322330
to: '/blog/2023/07/04/v0.2-release-announcement',
323331
from: ['/blog/2023/07/04/v8-release-announcement'],

website/src/components/DiffEditorMonaco.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import clsx from 'clsx';
44
import { useMemo } from 'react';
55

66
import { extensionToMonacoLanguage } from './Playground/extensionToMonacoLanguage';
7+
import { options } from './Playground/monacoOptions';
78
import MonacoPreloads from './Playground/MonacoPreloads';
8-
import { options } from './Playground/PlaygroundMonacoEditor';
99
import styles from './Playground/styles.module.css';
1010
import useAutoHeight from './Playground/useAutoHeight';
1111

website/src/components/Playground/PlaygroundMonacoEditor.tsx

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { LiveEditor } from 'react-live';
77
import './monaco-init';
88

99
import { extensionToMonacoLanguage } from './extensionToMonacoLanguage';
10+
import { largeOptions, options } from './monacoOptions';
1011
import useAutoHeight from './useAutoHeight';
1112

1213
export default function PlaygroundMonacoEditor({
@@ -15,13 +16,12 @@ export default function PlaygroundMonacoEditor({
1516
path = '',
1617
onFocus,
1718
tabIndex,
18-
highlights,
19+
highlights = undefined,
1920
autoFocus = false,
2021
large = false,
2122
isFocused = false,
2223
language = 'tsx',
23-
original,
24-
readOnly,
24+
readOnly = false,
2525
...rest
2626
}) {
2727
const editorOptions = useMemo(
@@ -103,59 +103,3 @@ export default function PlaygroundMonacoEditor({
103103
/>
104104
);
105105
}
106-
107-
export const options = {
108-
scrollbar: { alwaysConsumeMouseWheel: false },
109-
minimap: { enabled: false },
110-
wordWrap: 'on',
111-
scrollBeyondLastLine: false,
112-
wrappingIndent: 'indent',
113-
lineNumbers: 'off',
114-
//glyphMargin: false,
115-
folding: false,
116-
// Undocumented see https://github.com/Microsoft/vscode/issues/30795#issuecomment-410998882
117-
//lineDecorationsWidth: 0,
118-
//lineNumbersMinChars: 0,
119-
fontLigatures: true,
120-
fontFamily:
121-
'"Roboto Mono",SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace',
122-
fontSize: 13,
123-
lineHeight: 19,
124-
} as const;
125-
126-
const largeOptions = {
127-
...options,
128-
fontSize: 14,
129-
lineHeight: 20,
130-
};
131-
132-
/*function useMonacoWorker(
133-
callback: (worker: languages.typescript.TypeScriptWorker) => void,
134-
): MutableRefObject<languages.typescript.TypeScriptWorker | undefined> {
135-
const monaco = useMonaco();
136-
const workerRef = useRef<languages.typescript.TypeScriptWorker | undefined>();
137-
useEffect(() => {
138-
if (!monaco) return;
139-
monaco.languages.typescript
140-
.getTypeScriptWorker()
141-
.then(a => a())
142-
.then(worker => {
143-
workerRef.current = worker;
144-
//callback(worker);
145-
});
146-
}, [monaco]);
147-
return workerRef;
148-
}
149-
150-
function useWorkerCB(
151-
callback: (worker: languages.typescript.TypeScriptWorker) => void,
152-
deps: any[],
153-
) {
154-
const tsWorkerRef = useMonacoWorker(callback);
155-
156-
return useCallback((...args) => {
157-
if (!tsWorkerRef.current) return;
158-
callback(tsWorkerRef.current);
159-
}, deps);
160-
}
161-
*/

website/src/components/Playground/PlaygroundTextEdit.tsx

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@ import type { Fixture, Interceptor } from '@data-client/test';
22
import BrowserOnly from '@docusaurus/BrowserOnly';
33
import Translate from '@docusaurus/Translate';
44
import clsx from 'clsx';
5-
import { useCallback, useContext, useMemo, useState } from 'react';
5+
import {
6+
ComponentProps,
7+
useCallback,
8+
useContext,
9+
useMemo,
10+
useState,
11+
} from 'react';
612
import React from 'react';
713
import { LiveEditor } from 'react-live';
814

915
import FixturePreview from './FixturePreview';
1016
import Header from './Header';
17+
import { isGoogleBot } from './isGoogleBot';
1118
import PlaygroundEditor from './PlaygroundEditor';
1219
import styles from './styles.module.css';
1320
import CodeTabContext from '../Demo/CodeTabContext';
@@ -77,42 +84,23 @@ export function PlaygroundTextEdit({
7784
lastChild={i === codeTabs.length - 1 && large}
7885
/>
7986
: null}
80-
<div
87+
<TextEditTab
88+
hidden={closedList[i]}
8189
key={i}
82-
className={clsx(styles.playgroundEditor, {
83-
[styles.hidden]: closedList[i],
84-
})}
85-
>
86-
<BrowserOnly
87-
fallback={
88-
<LiveEditor
89-
key={i}
90-
language={language}
91-
code={codes[i]}
92-
disabled
93-
/>
94-
}
95-
>
96-
{() => (
97-
/*closedList[i] ? null : */ <PlaygroundEditor
98-
key={i}
99-
tabIndex={i}
100-
onFocus={
101-
row && !col && codeTabs.length > 1 ?
102-
handleTabSwitch
103-
: handleTabOpen
104-
}
105-
onChange={handleCodeChange[i]}
106-
code={codes[i]}
107-
path={'/' + id + '/' + path}
108-
isFocused={!closedList[i]}
109-
language={language}
110-
{...rest}
111-
large={large}
112-
/>
113-
)}
114-
</BrowserOnly>
115-
</div>
90+
tabIndex={i}
91+
onFocus={
92+
row && !col && codeTabs.length > 1 ?
93+
handleTabSwitch
94+
: handleTabOpen
95+
}
96+
onChange={handleCodeChange[i]}
97+
code={codes[i]}
98+
path={'/' + id + '/' + path}
99+
isFocused={!closedList[i]}
100+
language={language}
101+
{...rest}
102+
large={large}
103+
/>
116104
</React.Fragment>
117105
),
118106
)}
@@ -129,6 +117,48 @@ interface PlaygroundProps {
129117
isPlayground?: boolean;
130118
}
131119

120+
function TextEditTab({
121+
hidden,
122+
code,
123+
language,
124+
tabIndex,
125+
...rest
126+
}: ComponentProps<typeof PlaygroundEditor> & { hidden: boolean }) {
127+
const fallback =
128+
// to reduce HTML sent, in SSR don't render hidden tabs
129+
hidden ? <></>
130+
// google doesn't benefit from syntax highlighting, so just rendering code will greatly reduce html
131+
// TODO: make this actually work in SSR for google - currently does nothing since isGoogleBot is only true client-side
132+
: isGoogleBot ?
133+
<pre className="prism-code language-tsx" spellCheck="false">
134+
{code}
135+
</pre>
136+
// monaco editor doesn't work with SSR - so use LiveEditor which still shows readable code while the js loads
137+
: <LiveEditor key={tabIndex} language={language} code={code} disabled />;
138+
return (
139+
<>
140+
<div
141+
key={tabIndex}
142+
className={clsx(styles.playgroundEditor, {
143+
[styles.hidden]: hidden,
144+
})}
145+
>
146+
<BrowserOnly fallback={fallback}>
147+
{() => (
148+
<PlaygroundEditor
149+
key={tabIndex}
150+
tabIndex={tabIndex}
151+
code={code}
152+
language={language}
153+
{...rest}
154+
/>
155+
)}
156+
</BrowserOnly>
157+
</div>
158+
</>
159+
);
160+
}
161+
132162
function CodeTabHeader({
133163
onClick,
134164
closed,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const options = {
2+
scrollbar: { alwaysConsumeMouseWheel: false },
3+
minimap: { enabled: false },
4+
wordWrap: 'on',
5+
scrollBeyondLastLine: false,
6+
wrappingIndent: 'indent',
7+
lineNumbers: 'off',
8+
//glyphMargin: false,
9+
folding: false,
10+
// Undocumented see https://github.com/Microsoft/vscode/issues/30795#issuecomment-410998882
11+
//lineDecorationsWidth: 0,
12+
//lineNumbersMinChars: 0,
13+
fontLigatures: true,
14+
fontFamily:
15+
'"Roboto Mono",SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace',
16+
fontSize: 13,
17+
lineHeight: 19,
18+
} as const;
19+
20+
export const largeOptions = {
21+
...options,
22+
fontSize: 14,
23+
lineHeight: 20,
24+
};

0 commit comments

Comments
 (0)