Skip to content

Commit e15fcaf

Browse files
authored
Merge pull request #1042 from rust-lang/no-react-prism
Remove react-prism
2 parents 71eb2bf + c604322 commit e15fcaf

10 files changed

+74
-49
lines changed

ui/frontend/.eslintrc.js

+6
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,13 @@ module.exports = {
7676
'BuildMenu.tsx',
7777
'ButtonSet.tsx',
7878
'Header.tsx',
79+
'HelpExample.tsx',
7980
'Notifications.tsx',
81+
'Output/OutputPrism.tsx',
82+
'Output/PaneWithMir.tsx',
83+
'Output/SimplePane.tsx',
8084
'PopButton.tsx',
85+
'Prism.tsx',
8186
'Stdin.tsx',
8287
'actions.ts',
8388
'api.ts',
@@ -87,6 +92,7 @@ module.exports = {
8792
'editor/SimpleEditor.tsx',
8893
'hooks.ts',
8994
'observer.ts',
95+
'prism-shim.ts',
9096
'reducers/**/*.ts',
9197
'reducers/websocket.ts',
9298
'selectors/index.spec.ts',

ui/frontend/.prettierignore

+6
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ node_modules
1616
!BuildMenu.tsx
1717
!ButtonSet.tsx
1818
!Header.tsx
19+
!HelpExample.tsx
1920
!Notifications.tsx
21+
!Output/OutputPrism.tsx
22+
!Output/PaneWithMir.tsx
23+
!Output/SimplePane.tsx
2024
!PopButton.tsx
25+
!Prism.tsx
2126
!Stdin.tsx
2227
!actions.ts
2328
!api.ts
@@ -27,6 +32,7 @@ node_modules
2732
!editor/SimpleEditor.tsx
2833
!hooks.ts
2934
!observer.ts
35+
!prism-shim.ts
3036
!reducers/**/*.ts
3137
!selectors/index.spec.ts
3238
!types.ts

ui/frontend/HelpExample.tsx

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import React, { useCallback } from 'react';
22
import root from 'react-shadow';
33

4-
import 'prismjs/components/prism-rust.min';
5-
import { PrismCode } from 'react-prism';
6-
4+
import Prism from './Prism';
75
import * as actions from './actions';
86
import { useAppDispatch } from './hooks';
97

@@ -16,10 +14,7 @@ export interface HelpExampleProps {
1614

1715
const HelpExample: React.FC<HelpExampleProps> = ({ code }) => {
1816
const dispatch = useAppDispatch();
19-
const showExample = useCallback(
20-
() => dispatch(actions.showExample(code)),
21-
[dispatch, code]
22-
);
17+
const showExample = useCallback(() => dispatch(actions.showExample(code)), [dispatch, code]);
2318

2419
return (
2520
<div className={styles.container}>
@@ -29,11 +24,7 @@ const HelpExample: React.FC<HelpExampleProps> = ({ code }) => {
2924
<root.div>
3025
<link href={prismTheme} rel="stylesheet" />
3126

32-
<pre>
33-
<PrismCode className="language-rust">
34-
{code}
35-
</PrismCode>
36-
</pre>
27+
<Prism language="rust">{code}</Prism>
3728
</root.div>
3829
</div>
3930
);

ui/frontend/Output/OutputPrism.tsx

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import React from 'react';
2-
import { PrismCode } from 'react-prism';
2+
3+
import Prism from '../Prism';
34

45
import styles from './OutputPrism.module.css';
56

67
interface OutputPrismProps {
7-
children: React.ReactNode;
8-
languageCode: 'language-rust_mir' | 'language-rust_errors';
8+
children?: string;
9+
language: 'rust_mir' | 'rust_errors';
910
}
1011

11-
const OutputPrism: React.FC<OutputPrismProps> = ({ languageCode, children }) => (
12-
<pre>
13-
<PrismCode className={`${styles.container} ${languageCode}`}>
14-
{children}
15-
</PrismCode>
16-
</pre>
12+
const OutputPrism: React.FC<OutputPrismProps> = ({ language, children }) => (
13+
<Prism className={styles.container} language={language}>
14+
{children}
15+
</Prism>
1716
);
1817

1918
export default OutputPrism;

ui/frontend/Output/PaneWithMir.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from 'react';
22

33
import Header from './Header';
4-
import SimplePane, { SimplePaneProps } from './SimplePane';
54
import OutputPrism from './OutputPrism';
5+
import SimplePane, { SimplePaneProps } from './SimplePane';
66

77
interface PaneWithMirProps extends SimplePaneProps {
88
code?: string;
@@ -12,9 +12,7 @@ const PaneWithMir: React.FC<PaneWithMirProps> = ({ code, ...rest }) => (
1212
<SimplePane {...rest}>
1313
<div data-test-id="output-result">
1414
<Header label="Result" />
15-
<OutputPrism languageCode="language-rust_mir">
16-
{code}
17-
</OutputPrism>
15+
<OutputPrism language="rust_mir">{code}</OutputPrism>
1816
</div>
1917
</SimplePane>
2018
);

ui/frontend/Output/SimplePane.tsx

+12-10
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import React from 'react';
22

33
import Header from './Header';
44
import Loader from './Loader';
5-
import Section from './Section';
65
import OutputPrism from './OutputPrism';
6+
import Section from './Section';
77

88
interface HighlightErrorsProps {
9-
children: React.ReactNode;
9+
children?: string;
1010
label: string;
1111
}
1212

13-
const onOutputPrismCopy: React.ClipboardEventHandler = event => {
13+
const onOutputPrismCopy: React.ClipboardEventHandler = (event) => {
1414
// Blank out HTML copy data.
1515
// Though linkified output is handy in the Playground, it does more harm
1616
// than good when copied elsewhere, and terminal output is usable on its own.
@@ -24,9 +24,7 @@ const onOutputPrismCopy: React.ClipboardEventHandler = event => {
2424
const HighlightErrors: React.FC<HighlightErrorsProps> = ({ label, children }) => (
2525
<div data-test-id="output-stderr" onCopy={onOutputPrismCopy}>
2626
<Header label={label} />
27-
<OutputPrism languageCode="language-rust_errors">
28-
{children}
29-
</OutputPrism>
27+
<OutputPrism language="rust_errors">{children}</OutputPrism>
3028
</div>
3129
);
3230

@@ -42,12 +40,16 @@ export interface ReallySimplePaneProps {
4240
error?: string;
4341
}
4442

45-
const SimplePane: React.FC<SimplePaneProps> = props => (
43+
const SimplePane: React.FC<SimplePaneProps> = (props) => (
4644
<div data-test-id={`output-${props.kind}`}>
47-
{(props.requestsInProgress > 0) && <Loader />}
48-
<Section kind="error" label="Errors">{props.error}</Section>
45+
{props.requestsInProgress > 0 && <Loader />}
46+
<Section kind="error" label="Errors">
47+
{props.error}
48+
</Section>
4949
<HighlightErrors label="Standard Error">{props.stderr}</HighlightErrors>
50-
<Section kind="stdout" label="Standard Output">{props.stdout}</Section>
50+
<Section kind="stdout" label="Standard Output">
51+
{props.stdout}
52+
</Section>
5153
{props.children}
5254
</div>
5355
);

ui/frontend/Prism.tsx

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React, { useEffect, useRef } from 'react';
2+
3+
import RealPrism from './prism-shim';
4+
5+
interface PrismProps {
6+
children?: string;
7+
language: 'rust' | 'rust_mir' | 'rust_errors';
8+
className?: string;
9+
}
10+
11+
const Prism: React.FC<PrismProps> = ({ children, language, className = '' }) => {
12+
const element = useRef(null);
13+
14+
useEffect(() => {
15+
if (!element.current) {
16+
return;
17+
}
18+
19+
RealPrism.highlightElement(element.current);
20+
}, [element, children, language]);
21+
22+
return (
23+
<pre>
24+
<code ref={element} className={`${className} language-${language}`}>
25+
{children}
26+
</code>
27+
</pre>
28+
);
29+
};
30+
31+
export default Prism;

ui/frontend/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"react-dom": "^18.2.0",
2121
"react-monaco-editor": "^0.55.0",
2222
"react-portal": "^4.1.4",
23-
"react-prism": "^4.0.0",
2423
"react-redux": "^9.0.2",
2524
"react-shadow": "^20.0.0",
2625
"redux-thunk": "^3.1.0",

ui/frontend/pnpm-lock.yaml

-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui/frontend/prism-shim.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Prism from 'prismjs';
2+
import 'prismjs/components/prism-rust';
3+
4+
Prism.manual = true;
5+
6+
export default Prism;

0 commit comments

Comments
 (0)