@codecademy/codebytes 0.1.1-alpha.19caf8.0 → 0.1.1-alpha.675361.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,7 +3,7 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
- ### [0.1.1-alpha.19caf8.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.1.0...@codecademy/codebytes@0.1.1-alpha.19caf8.0) (2021-12-21)
6
+ ### [0.1.1-alpha.675361.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.1.0...@codecademy/codebytes@0.1.1-alpha.675361.0) (2022-01-03)
7
7
 
8
8
  **Note:** Version bump only for package @codecademy/codebytes
9
9
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@codecademy/codebytes",
3
3
  "description": "Codebytes Code Editor",
4
- "version": "0.1.1-alpha.19caf8.0",
4
+ "version": "0.1.1-alpha.675361.0",
5
5
  "author": "Codecademy Engineering <dev@codecademy.com>",
6
6
  "sideEffects": [
7
7
  "**/*.css",
@@ -23,6 +23,7 @@
23
23
  "@codecademy/gamut": "*",
24
24
  "@codecademy/gamut-icons": "*",
25
25
  "@codecademy/gamut-styles": "*",
26
+ "@codecademy/variance": "*",
26
27
  "@emotion/react": "^11.4.0",
27
28
  "@emotion/styled": "^11.3.0"
28
29
  },
@@ -43,5 +44,5 @@
43
44
  "publishConfig": {
44
45
  "access": "public"
45
46
  },
46
- "gitHead": "5df74cc4516c8894a448ede333784ca4deb371e7"
47
+ "gitHead": "d9c4df33e1b24cd2f123e939a0e4c2279096ba36"
47
48
  }
package/src/api.ts ADDED
@@ -0,0 +1,28 @@
1
+ import type { languageOption } from './consts';
2
+
3
+ interface Response {
4
+ stderr: string;
5
+ stdout: string;
6
+ exit_code: number;
7
+ }
8
+
9
+ interface PostSnippetData {
10
+ language: languageOption;
11
+ code: string;
12
+ }
13
+
14
+ export const postSnippet = async (
15
+ data: PostSnippetData,
16
+ snippetsBaseUrl?: string
17
+ ): Promise<Response> => {
18
+ const snippetsEndpoint = `https://${snippetsBaseUrl}/snippets`;
19
+
20
+ const response = await fetch(snippetsEndpoint, {
21
+ method: 'POST',
22
+ body: JSON.stringify(data),
23
+ headers: {
24
+ 'x-codecademy-user-id': 'codebytes-anon-user',
25
+ },
26
+ });
27
+ return response.json();
28
+ };
package/src/drawers.tsx CHANGED
@@ -19,7 +19,7 @@ const Drawer = styled(FlexBox)<{ open?: boolean; hideOnClose?: boolean }>`
19
19
  position: relative;
20
20
  ${({ open, hideOnClose }) => `
21
21
  flex-basis: ${open ? '100%' : '0%'};
22
- ${!open && hideOnClose ? 'visibility: hidden;' : ''}
22
+ visibility: ${!open && hideOnClose ? 'hidden' : 'visible'};
23
23
  transition: flex-basis 0.2s ${
24
24
  open ? 'ease-out' : 'ease-in, visibility 0s 0.2s'
25
25
  };
@@ -36,48 +36,52 @@ export type DrawersProps = {
36
36
  };
37
37
 
38
38
  export const Drawers: React.FC<DrawersProps> = ({ leftChild, rightChild }) => {
39
- const [show, setShow] = useState<'left' | 'right' | 'both'>('both');
39
+ const [open, setOpen] = useState<'left' | 'right' | 'both'>('both');
40
40
 
41
- let buttonLabelL = 'hide code';
42
- let buttonLabelR = 'hide output';
41
+ let ariaLabelCodeButton = 'hide code';
42
+ let ariaLabelOutputButton = 'hide output';
43
+ let isLeftOpen = false;
44
+ let isRightOpen = false;
43
45
 
44
- if (show === 'left') {
45
- buttonLabelL = buttonLabelR = 'show output';
46
- } else if (show === 'right') {
47
- buttonLabelL = buttonLabelR = 'show code';
46
+ if (open === 'left') {
47
+ ariaLabelCodeButton = ariaLabelOutputButton = 'show output';
48
+ isLeftOpen = true;
49
+ } else if (open === 'right') {
50
+ ariaLabelCodeButton = ariaLabelOutputButton = 'show code';
51
+ isRightOpen = true;
48
52
  }
49
53
 
50
54
  return (
51
55
  <>
52
56
  <FlexBox>
53
57
  <Drawer
54
- open={show !== 'right'}
58
+ open={!isRightOpen}
55
59
  alignItems="center"
56
60
  flexWrap="nowrap"
57
61
  textAlign="left"
58
- borderRight="1"
62
+ borderRight={1}
59
63
  borderColor="gray-900"
60
- px="8"
64
+ px={8}
61
65
  >
62
66
  <IconButton
63
67
  icon={LeftDrawerIcon}
64
68
  variant="secondary"
65
69
  size="small"
66
70
  onClick={() =>
67
- setShow((state) => (state === 'both' ? 'right' : 'both'))
71
+ setOpen((state) => (state === 'both' ? 'right' : 'both'))
68
72
  }
69
- aria-label={buttonLabelL}
73
+ aria-label={ariaLabelCodeButton}
70
74
  aria-controls="code-drawer"
71
- aria-expanded={show !== 'right'}
75
+ aria-expanded={!isRightOpen}
72
76
  />
73
77
  <DrawerLabel id="code-drawer-label">Code</DrawerLabel>
74
78
  </Drawer>
75
79
  <Drawer
76
- open={show !== 'left'}
80
+ open={!isLeftOpen}
77
81
  alignItems="center"
78
82
  flexWrap="nowrap"
79
83
  justifyContent="flex-end"
80
- px="8"
84
+ px={8}
81
85
  >
82
86
  <DrawerLabel id="output-drawer-label">Output</DrawerLabel>
83
87
  <IconButton
@@ -85,17 +89,17 @@ export const Drawers: React.FC<DrawersProps> = ({ leftChild, rightChild }) => {
85
89
  variant="secondary"
86
90
  size="small"
87
91
  onClick={() =>
88
- setShow((state) => (state === 'both' ? 'left' : 'both'))
92
+ setOpen((state) => (state === 'both' ? 'left' : 'both'))
89
93
  }
90
- aria-label={buttonLabelR}
94
+ aria-label={ariaLabelOutputButton}
91
95
  aria-controls="output-drawer"
92
- aria-expanded={show !== 'left'}
96
+ aria-expanded={!isLeftOpen}
93
97
  />
94
98
  </Drawer>
95
99
  </FlexBox>
96
100
  <FlexBox
97
101
  flexGrow={1}
98
- borderY="1"
102
+ borderY={1}
99
103
  borderColor="gray-900"
100
104
  alignItems="stretch"
101
105
  overflow="hidden"
@@ -104,7 +108,7 @@ export const Drawers: React.FC<DrawersProps> = ({ leftChild, rightChild }) => {
104
108
  hideOnClose
105
109
  id="code-drawer"
106
110
  aria-labelledby="code-drawer-label"
107
- open={show !== 'right'}
111
+ open={!isRightOpen}
108
112
  flexGrow={0}
109
113
  overflow="hidden"
110
114
  borderColor="gray-900"
@@ -117,7 +121,7 @@ export const Drawers: React.FC<DrawersProps> = ({ leftChild, rightChild }) => {
117
121
  hideOnClose
118
122
  id="output-drawer"
119
123
  aria-labelledby="output-drawer-label"
120
- open={show !== 'left'}
124
+ open={!isLeftOpen}
121
125
  overflow="hidden"
122
126
  >
123
127
  {rightChild}
package/src/editor.tsx CHANGED
@@ -10,12 +10,13 @@ import { theme } from '@codecademy/gamut-styles';
10
10
  import styled from '@emotion/styled';
11
11
  import React, { useState } from 'react';
12
12
 
13
+ import { postSnippet } from './api';
13
14
  import type { languageOption } from './consts';
14
15
  import { Drawers } from './drawers';
15
16
 
16
17
  const Output = styled.pre<{ hasError: boolean }>`
17
18
  width: 100%;
18
- height: 20rem;
19
+ height: 100%;
19
20
  margin: 0;
20
21
  padding: 0 1rem;
21
22
  font-family: Monaco;
@@ -38,21 +39,19 @@ type EditorProps = {
38
39
  language: languageOption;
39
40
  text: string;
40
41
  // eslint-disable-next-line react/no-unused-prop-types
41
- onChange: (text: string) => void /* Will use in upcoming pr */;
42
+ onChange: (
43
+ text: string
44
+ ) => void /* TODO: Add onChange behavior in DISC-353 */;
42
45
  onCopy?: (text: string, language: string) => void;
46
+ snippetsBaseUrl?: string;
43
47
  };
44
48
 
45
- interface Response {
46
- stderr: string;
47
- stdout: string;
48
- exit_code: number;
49
- }
50
-
51
49
  export const Editor: React.FC<EditorProps> = ({
52
50
  language,
53
51
  text,
54
52
  hideCopyButton,
55
53
  onCopy,
54
+ snippetsBaseUrl,
56
55
  }) => {
57
56
  const [output, setOutput] = useState('');
58
57
  const [status, setStatus] = useState<'ready' | 'waiting' | 'error'>('ready');
@@ -66,7 +65,7 @@ export const Editor: React.FC<EditorProps> = ({
66
65
  onCopy?.(
67
66
  text,
68
67
  language
69
- ); /* TODO: pass in onCopyBBCodeblock behavior from static sites */
68
+ ); /* TODO: pass in onCopyBBCodeblock behavior from the future version we migrate to Next.js */
70
69
  setIsCodeByteCopied(true);
71
70
  }
72
71
  };
@@ -76,7 +75,7 @@ export const Editor: React.FC<EditorProps> = ({
76
75
  setStatus('error');
77
76
  };
78
77
 
79
- const handleSubmit = () => {
78
+ const handleSubmit = async () => {
80
79
  if (text.trim().length === 0) {
81
80
  return;
82
81
  }
@@ -87,34 +86,23 @@ export const Editor: React.FC<EditorProps> = ({
87
86
  setStatus('waiting');
88
87
  setOutput('');
89
88
 
90
- const snippetsEndpoint =
91
- 'https://' + process.env.GATSBY_CONTAINER_API_BASE + '/snippets';
92
-
93
- fetch(snippetsEndpoint, {
94
- method: 'POST',
95
- body: JSON.stringify(data),
96
- headers: {
97
- 'x-codecademy-user-id': 'codebytes-anon-user',
98
- },
99
- })
100
- .then((res) => res.json())
101
- .then((response: Response) => {
102
- if (response.stderr.length > 0) {
103
- setErrorStatusAndOutput(response.stderr);
104
- } else if (response.exit_code === DOCKER_SIGTERM) {
105
- setErrorStatusAndOutput(
106
- 'Your code took too long to return a result. Double check your code for any issues and try again!'
107
- );
108
- } else if (response.exit_code !== 0) {
109
- setErrorStatusAndOutput('An unknown error occured.');
110
- } else {
111
- setOutput(response.stdout);
112
- setStatus('ready');
113
- }
114
- })
115
- .catch((error) => {
116
- setErrorStatusAndOutput('Error: ' + error);
117
- });
89
+ try {
90
+ const response = await postSnippet(data, snippetsBaseUrl);
91
+ if (response.stderr.length > 0) {
92
+ setErrorStatusAndOutput(response.stderr);
93
+ } else if (response.exit_code === DOCKER_SIGTERM) {
94
+ setErrorStatusAndOutput(
95
+ 'Your code took too long to return a result. Double check your code for any issues and try again!'
96
+ );
97
+ } else if (response.exit_code !== 0) {
98
+ setErrorStatusAndOutput('An unknown error occured.');
99
+ } else {
100
+ setOutput(response.stdout);
101
+ setStatus('ready');
102
+ }
103
+ } catch (error) {
104
+ setErrorStatusAndOutput('Error: ' + error);
105
+ }
118
106
  };
119
107
 
120
108
  return (
@@ -129,7 +117,7 @@ export const Editor: React.FC<EditorProps> = ({
129
117
  />
130
118
  <FlexBox
131
119
  justifyContent={hideCopyButton ? 'flex-end' : 'space-between'}
132
- pl="8"
120
+ pl={8}
133
121
  >
134
122
  {!hideCopyButton ? (
135
123
  <ToolTip
package/src/index.tsx CHANGED
@@ -1,59 +1,79 @@
1
1
  import { Box, IconButton } from '@codecademy/gamut';
2
2
  import { FaviconIcon } from '@codecademy/gamut-icons';
3
- import { Background, system } from '@codecademy/gamut-styles';
3
+ import { Background, states, system } from '@codecademy/gamut-styles';
4
+ import { StyleProps } from '@codecademy/variance';
4
5
  import styled from '@emotion/styled';
5
6
  import React, { useState } from 'react';
6
7
 
7
8
  import { languageOption } from './consts';
8
9
  import { Editor } from './editor';
9
10
 
11
+ const editorStates = states({
12
+ isIFrame: { height: '100vh' },
13
+ });
14
+
15
+ const editorBaseStyles = system.css({
16
+ border: 1,
17
+ borderColor: 'gray-900',
18
+ display: 'flex',
19
+ flexDirection: 'column',
20
+ height: '25rem',
21
+ width: '43rem',
22
+ overflow: 'hidden',
23
+ });
24
+ export interface EditorStyleProps
25
+ extends StyleProps<typeof editorBaseStyles>,
26
+ StyleProps<typeof editorStates> {}
27
+
28
+ const EditorContainer = styled(Background)<EditorStyleProps>(
29
+ editorBaseStyles,
30
+ editorStates
31
+ );
32
+
10
33
  export interface CodeByteEditorProps {
11
34
  text: string;
12
35
  language: languageOption;
13
36
  hideCopyButton: boolean;
37
+ onCopy?: (text: string, language: string) => void;
38
+ isIFrame?: boolean;
39
+ snippetsBaseUrl?: string;
40
+ onTextChange?: (text: string) => void;
14
41
  }
15
42
 
16
- const EditorContainer = styled(Background)(
17
- system.css({
18
- border: '1',
19
- borderColor: 'gray-900',
20
- display: 'flex',
21
- flexDirection: 'column',
22
- height: '400px',
23
- width: '688px',
24
- overflow: 'hidden',
25
- })
26
- );
27
-
28
43
  export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
29
44
  text: initialText,
30
45
  language,
31
46
  hideCopyButton,
47
+ onCopy,
48
+ isIFrame = false,
49
+ snippetsBaseUrl = process.env.CONTAINER_API_BASE,
50
+ onTextChange,
32
51
  }) => {
33
52
  const [text, setText] = useState<string>(initialText);
34
53
  return (
35
- <>
36
- <EditorContainer bg="black" as="main">
37
- <Box borderBottom="1" borderColor="gray-900" py="4" pl="8">
38
- <IconButton
39
- icon={FaviconIcon}
40
- variant="secondary"
41
- href="https://www.codecademy.com/"
42
- target="_blank"
43
- rel="noreferrer"
44
- aria-label="visit codecademy.com"
45
- />
46
- </Box>
47
- <Editor
48
- text={text}
49
- onChange={(newText: string) => {
50
- setText(newText);
51
- }}
52
- hideCopyButton={hideCopyButton}
53
- language={language}
54
+ <EditorContainer bg="black" as="main" isIFrame={isIFrame}>
55
+ <Box borderBottom={1} borderColor="gray-900" py={4} pl={8}>
56
+ <IconButton
57
+ icon={FaviconIcon}
58
+ variant="secondary"
59
+ href="https://www.codecademy.com/"
60
+ target="_blank"
61
+ rel="noreferrer"
62
+ aria-label="visit codecademy.com"
54
63
  />
55
- </EditorContainer>
56
- </>
64
+ </Box>
65
+ <Editor
66
+ language={language}
67
+ text={text}
68
+ hideCopyButton={hideCopyButton}
69
+ onChange={(newText: string) => {
70
+ setText(newText);
71
+ onTextChange?.(newText);
72
+ }}
73
+ onCopy={onCopy}
74
+ snippetsBaseUrl={snippetsBaseUrl}
75
+ />
76
+ </EditorContainer>
57
77
  );
58
78
  };
59
79
 
package/src/theme.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { CoreTheme } from '@codecademy/gamut-styles';
2
+
3
+ declare module '@emotion/react' {
4
+ export interface Theme extends CoreTheme {}
5
+ }