Skip to content

Commit fa16af3

Browse files
Better markdown replies (#3613)
* replay changes * fix ts * add better markdown suppoprt * fix currentCommentId * dont show hr * not allow images in public sandboxes Co-authored-by: Siddharth Kshetrapal <[email protected]> Co-authored-by: Christian Alfoni <[email protected]>
1 parent 8ab6646 commit fa16af3

File tree

4 files changed

+129
-84
lines changed

4 files changed

+129
-84
lines changed

packages/app/src/app/overmind/namespaces/editor/state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ type State = {
8888

8989
export const state: State = {
9090
comments: {},
91-
currentCommentId: null,
91+
currentCommentId: null, // '5e5961e0c277a40fef1e391b',
9292
currentComment: ({ comments, currentSandbox, currentCommentId }) => {
9393
if (!currentSandbox || !comments[currentSandbox.id] || !currentCommentId) {
9494
return null;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from 'react';
2+
import Highlight, { defaultProps } from 'prism-react-renderer';
3+
import { Element } from '@codesandbox/components';
4+
import css from '@styled-system/css';
5+
import nightOwlLight from 'prism-react-renderer/themes/nightOwlLight';
6+
7+
import nightOwl from 'prism-react-renderer/themes/nightOwl';
8+
import { withTheme } from 'styled-components';
9+
10+
export const Code = withTheme(({ value, language, theme }) => (
11+
<>
12+
<Highlight
13+
{...defaultProps}
14+
code={value}
15+
language={language || 'js'}
16+
theme={theme.vscodeTheme.type === 'dark' ? nightOwlLight : nightOwl}
17+
>
18+
{({ className, style, tokens, getLineProps, getTokenProps }) => (
19+
<Element
20+
as="pre"
21+
paddingX={4}
22+
paddingY={2}
23+
marginY={2}
24+
className={className}
25+
style={style}
26+
css={css({
27+
whiteSpace: 'pre-wrap',
28+
})}
29+
>
30+
{tokens.map((line, i) => (
31+
<Element {...getLineProps({ line, key: i })}>
32+
{line.map((token, key) => (
33+
<Element as="span" {...getTokenProps({ token, key })} />
34+
))}
35+
</Element>
36+
))}
37+
</Element>
38+
)}
39+
</Highlight>
40+
</>
41+
));
Lines changed: 84 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,88 @@
11
import React from 'react';
22
import ReactMarkdown from 'react-markdown';
3-
import { Text, Element } from '@codesandbox/components';
3+
import { useOvermind } from 'app/overmind';
4+
import { Text, Element, Link } from '@codesandbox/components';
5+
import css from '@styled-system/css';
46
import { Code } from './Code';
57

6-
export const Comment = ({ source }) => (
7-
<Element paddingX={4} paddingBottom={6}>
8-
<ReactMarkdown
9-
source={source}
10-
renderers={{
11-
text: ({ children }) => (
12-
<Text variant="muted" size={13}>
13-
{children}
14-
</Text>
15-
),
16-
heading: ({ children }) => (
17-
<Text variant="muted" size={13}>
18-
{children}
19-
</Text>
20-
),
21-
code: props => <Code {...props} />,
22-
}}
23-
/>
24-
</Element>
25-
);
8+
export const Comment = ({ source }) => {
9+
const { state } = useOvermind();
10+
const privateSandbox =
11+
state.editor.currentSandbox.privacy === 1 ||
12+
state.editor.currentSandbox.privacy === 2;
13+
14+
const image = props =>
15+
privateSandbox ? (
16+
<img
17+
{...props}
18+
alt={props.alt}
19+
css={css({
20+
maxWidth: '100%',
21+
})}
22+
/>
23+
) : (
24+
<Text marginY={4} variant="muted" block align="center">
25+
You cannot use images in comments on public sandboxes.
26+
</Text>
27+
);
28+
29+
return (
30+
<Element
31+
paddingX={4}
32+
paddingBottom={6}
33+
css={css({
34+
'ul, ol': {
35+
paddingLeft: 0,
36+
fontSize: 13,
37+
},
38+
'ol li': {
39+
counterIncrement: 'counter',
40+
},
41+
'ol li::before': {
42+
color: 'mutedForeground',
43+
content: "counter(counter) '. '",
44+
},
45+
li: {
46+
listStyle: 'none',
47+
},
48+
'li:before': {
49+
content: "'•'",
50+
color: 'mutedForeground',
51+
paddingRight: '0.5em',
52+
},
53+
})}
54+
>
55+
<ReactMarkdown
56+
source={source}
57+
renderers={{
58+
text: ({ children }) => (
59+
<Text variant="muted" size={13}>
60+
{children}
61+
</Text>
62+
),
63+
heading: ({ children }) => (
64+
<Text block variant="muted" size={13}>
65+
{children}
66+
</Text>
67+
),
68+
code: props => <Code {...props} />,
69+
link: props => <Link {...props}>{props.children}</Link>,
70+
linkReference: props => <Link {...props}>{props.children}</Link>,
71+
image: props => image(props),
72+
imageReference: props => image(props),
73+
thematicBreak: () => null,
74+
inlineCode: props => (
75+
<Element
76+
as="span"
77+
css={css({ backgroundColor: 'mutedForeground' })}
78+
>
79+
<Text variant="danger" as="code">
80+
{props.children}
81+
</Text>
82+
</Element>
83+
),
84+
}}
85+
/>
86+
</Element>
87+
);
88+
};

packages/app/src/app/pages/Sandbox/Editor/Workspace/screens/Comments/Dialog/index.tsx

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,16 @@ import {
1515
import { useOvermind } from 'app/overmind';
1616
import { Comment } from './Comment';
1717

18-
// // eslint-disable-next-line no-var
19-
// var markdown = `
20-
// I am a comment
21-
22-
// Here's that code block:
23-
24-
// \`\`\`js
25-
// const total = [1, 2, 3, 4, 5]
26-
// .map(num => num * 3)
27-
// .filter(num => num < 9)
28-
// .reduce((sum, num) => sum += num, 0)
29-
30-
// \`\`\`
31-
32-
// Neat, eh?`;
33-
34-
// const defaultComment = {
35-
// id: '5e59204cc277a40fef1e3916',
36-
// isResolved: false,
37-
// originalMessage: {
38-
// id: 'gier2bk769d03r',
39-
// content: markdown,
40-
// author: {
41-
// id: '740dca51-993d-4b0e-b8cb-0e46acada86b',
42-
// avatarUrl: 'https://avatars0.githubusercontent.com/u/1863771?v=4',
43-
// username: 'siddharthkp',
44-
// },
45-
// },
46-
// replies: [
47-
// {
48-
// id: 'a\n-SaraVieira',
49-
// content: 'A reply',
50-
// author: {
51-
// avatarUrl: 'https://avatars0.githubusercontent.com/u/1051509?v=4',
52-
// badges: [{ id: 'patron_2', name: 'Patron II', visible: true }],
53-
// curatorAt: '2018-11-25T18:51:34Z',
54-
// email: '[email protected]',
55-
// experiments: { containerLsp: null },
56-
// id: '8d35d7c1-eecb-4aad-87b0-c22d30d12081',
57-
// integrations: {
58-
// github: null,
59-
// zeit: { token: 'pqshnGwa4t3y7RxMzfHnoSW1' },
60-
// },
61-
// name: 'Sara Vieira',
62-
// sendSurvey: false,
63-
// subscription: {
64-
// amount: 10,
65-
// cancelAtPeriodEnd: false,
66-
// duration: 'monthly',
67-
// plan: 'patron',
68-
// since: '2018-03-01T16:00:18Z',
69-
// },
70-
// username: 'SaraVieira',
71-
// },
72-
// },
73-
// ],
74-
// insertedAt: '2020-02-28T14:14:36.859Z',
75-
// updatedAt: '2020-02-28T14:14:36.859Z',
76-
// };
7718
export const CommentDialog = props =>
7819
ReactDOM.createPortal(<Dialog {...props} />, document.body);
7920

8021
export const Dialog = props => {
8122
const { state, actions } = useOvermind();
8223
const [value, setValue] = useState('');
8324
const comment = state.editor.currentComment;
84-
const [position, setPosition] = React.useState({
85-
x: 200,
86-
y: 100,
25+
const [position, setPosition] = useState({
26+
x: props.x || 200,
27+
y: props.y || 100,
8728
});
8829

8930
const closeDialog = () => actions.editor.selectComment(null);

0 commit comments

Comments
 (0)