@codecademy/codebytes 0.0.2-alpha.d83a50.0 → 0.1.1-alpha.9d702d.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,6 +3,17 @@
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.0.2-alpha.d83a50.0 (2021-12-17)
6
+ ### [0.1.1-alpha.9d702d.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.1.0...@codecademy/codebytes@0.1.1-alpha.9d702d.0) (2021-12-21)
7
7
 
8
8
  **Note:** Version bump only for package @codecademy/codebytes
9
+
10
+
11
+
12
+
13
+
14
+ ## 0.1.0 (2021-12-17)
15
+
16
+
17
+ ### Features
18
+
19
+ * **Codebytes:** move codebytes parent disc 351 ([#11](https://github.com/Codecademy/client-modules/issues/11)) ([30edd2b](https://github.com/Codecademy/client-modules/commit/30edd2b7a0e50c27d3adcf231b56441b8e8f6b81))
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@codecademy/codebytes",
3
3
  "description": "Codebytes Code Editor",
4
- "version": "0.0.2-alpha.d83a50.0",
4
+ "version": "0.1.1-alpha.9d702d.0",
5
5
  "author": "Codecademy Engineering <dev@codecademy.com>",
6
6
  "sideEffects": [
7
7
  "**/*.css",
@@ -24,16 +24,7 @@
24
24
  "@codecademy/gamut-icons": "*",
25
25
  "@codecademy/gamut-styles": "*",
26
26
  "@emotion/react": "^11.4.0",
27
- "@emotion/styled": "^11.3.0",
28
- "@storybook/addon-a11y": "6.3.4",
29
- "@storybook/addon-actions": "6.3.4",
30
- "@storybook/addon-controls": "6.3.4",
31
- "@storybook/addon-docs": "6.3.4",
32
- "@storybook/addon-essentials": "6.3.4",
33
- "@storybook/addon-links": "6.3.4",
34
- "@storybook/addons": "6.3.4",
35
- "@storybook/react": "6.3.4",
36
- "@storybook/theming": "6.3.4"
27
+ "@emotion/styled": "^11.3.0"
37
28
  },
38
29
  "scripts": {
39
30
  "verify": "tsc --noEmit",
@@ -41,28 +32,16 @@
41
32
  "build:clean": "rm -rf dist",
42
33
  "build:types": "tsc --emitDeclarationOnly",
43
34
  "build": "yarn build:clean && yarn build:compile && yarn build:types",
44
- "build:watch": "yarn build && onchange ./src -- yarn build:compile && yarn build:types",
45
- "lernaBuildTask": "yarn build",
46
- "storybook": "start-storybook -p 6006",
47
- "build-storybook": "build-storybook"
35
+ "build:watch": "yarn build && onchange ./src -- yarn build:compile && yarn build:types"
48
36
  },
49
37
  "license": "MIT",
50
38
  "devDependencies": {
51
39
  "@emotion/jest": "^11.3.0",
52
- "@storybook/addon-a11y": "^6.4.4",
53
- "@storybook/addon-actions": "^6.4.4",
54
- "@storybook/addon-controls": "^6.4.4",
55
- "@storybook/addon-docs": "^6.4.4",
56
- "@storybook/addon-essentials": "^6.4.4",
57
- "@storybook/addon-links": "^6.4.4",
58
- "@storybook/addons": "^6.4.4",
59
- "@storybook/react": "^6.4.4",
60
- "@storybook/theming": "^6.4.4",
61
40
  "@testing-library/dom": "^7.31.2",
62
41
  "@testing-library/react": "^11.0.4"
63
42
  },
64
43
  "publishConfig": {
65
44
  "access": "public"
66
45
  },
67
- "gitHead": "3e1e0013158d73c6f50cd60cd7cc71bbfa2008a5"
46
+ "gitHead": "61905c84b7898bbfe7dbd2702afed7911d5eaf6b"
68
47
  }
package/src/consts.ts ADDED
@@ -0,0 +1,15 @@
1
+ // key = language param to send to snippets service
2
+ // val = label in language selection drop down
3
+ export const languageOptions = {
4
+ '': 'Select your language',
5
+ cpp: 'C++',
6
+ csharp: 'C#',
7
+ golang: 'Go',
8
+ javascript: 'JavaScript',
9
+ php: 'PHP',
10
+ python: 'Python 3',
11
+ ruby: 'Ruby',
12
+ scheme: 'Scheme',
13
+ };
14
+
15
+ export type languageOption = keyof typeof languageOptions;
@@ -0,0 +1,128 @@
1
+ import { FlexBox, IconButton } from '@codecademy/gamut';
2
+ import {
3
+ ArrowChevronLeftIcon,
4
+ ArrowChevronRightIcon,
5
+ } from '@codecademy/gamut-icons';
6
+ import styled from '@emotion/styled';
7
+ import React, { useState } from 'react';
8
+
9
+ const DrawerLabel = styled.span`
10
+ padding: 0.875rem 0.5rem;
11
+ `;
12
+
13
+ const LeftDrawerIcon = styled(ArrowChevronLeftIcon)<{ open?: boolean }>`
14
+ transition: transform 0.2s ease-in-out;
15
+ `;
16
+ const RightDrawerIcon = LeftDrawerIcon.withComponent(ArrowChevronRightIcon);
17
+
18
+ const Drawer = styled(FlexBox)<{ open?: boolean; hideOnClose?: boolean }>`
19
+ position: relative;
20
+ ${({ open, hideOnClose }) => `
21
+ flex-basis: ${open ? '100%' : '0%'};
22
+ ${!open && hideOnClose ? 'visibility: hidden;' : ''}
23
+ transition: flex-basis 0.2s ${
24
+ open ? 'ease-out' : 'ease-in, visibility 0s 0.2s'
25
+ };
26
+
27
+ ${LeftDrawerIcon}, ${RightDrawerIcon} {
28
+ transform: rotateZ(${open ? '0' : '180'}deg)};
29
+ }
30
+ `}
31
+ `;
32
+
33
+ export type DrawersProps = {
34
+ leftChild: React.ReactNode;
35
+ rightChild: React.ReactNode;
36
+ };
37
+
38
+ export const Drawers: React.FC<DrawersProps> = ({ leftChild, rightChild }) => {
39
+ const [show, setShow] = useState<'left' | 'right' | 'both'>('both');
40
+
41
+ let buttonLabelL = 'hide code';
42
+ let buttonLabelR = 'hide output';
43
+
44
+ if (show === 'left') {
45
+ buttonLabelL = buttonLabelR = 'show output';
46
+ } else if (show === 'right') {
47
+ buttonLabelL = buttonLabelR = 'show code';
48
+ }
49
+
50
+ return (
51
+ <>
52
+ <FlexBox>
53
+ <Drawer
54
+ open={show !== 'right'}
55
+ alignItems="center"
56
+ flexWrap="nowrap"
57
+ textAlign="left"
58
+ borderRight="1"
59
+ borderColor="gray-900"
60
+ px="8"
61
+ >
62
+ <IconButton
63
+ icon={LeftDrawerIcon}
64
+ variant="secondary"
65
+ size="small"
66
+ onClick={() =>
67
+ setShow((state) => (state === 'both' ? 'right' : 'both'))
68
+ }
69
+ aria-label={buttonLabelL}
70
+ aria-controls="code-drawer"
71
+ aria-expanded={show !== 'right'}
72
+ />
73
+ <DrawerLabel id="code-drawer-label">Code</DrawerLabel>
74
+ </Drawer>
75
+ <Drawer
76
+ open={show !== 'left'}
77
+ alignItems="center"
78
+ flexWrap="nowrap"
79
+ justifyContent="flex-end"
80
+ px="8"
81
+ >
82
+ <DrawerLabel id="output-drawer-label">Output</DrawerLabel>
83
+ <IconButton
84
+ icon={RightDrawerIcon}
85
+ variant="secondary"
86
+ size="small"
87
+ onClick={() =>
88
+ setShow((state) => (state === 'both' ? 'left' : 'both'))
89
+ }
90
+ aria-label={buttonLabelR}
91
+ aria-controls="output-drawer"
92
+ aria-expanded={show !== 'left'}
93
+ />
94
+ </Drawer>
95
+ </FlexBox>
96
+ <FlexBox
97
+ flexGrow={1}
98
+ borderY="1"
99
+ borderColor="gray-900"
100
+ alignItems="stretch"
101
+ overflow="hidden"
102
+ >
103
+ <Drawer
104
+ hideOnClose
105
+ id="code-drawer"
106
+ aria-labelledby="code-drawer-label"
107
+ open={show !== 'right'}
108
+ flexGrow={0}
109
+ overflow="hidden"
110
+ borderColor="gray-900"
111
+ borderStyleRight="solid"
112
+ borderWidthRight="thin"
113
+ >
114
+ {leftChild}
115
+ </Drawer>
116
+ <Drawer
117
+ hideOnClose
118
+ id="output-drawer"
119
+ aria-labelledby="output-drawer-label"
120
+ open={show !== 'left'}
121
+ overflow="hidden"
122
+ >
123
+ {rightChild}
124
+ </Drawer>
125
+ </FlexBox>
126
+ </>
127
+ );
128
+ };
package/src/editor.tsx CHANGED
@@ -1,10 +1,164 @@
1
- import React from 'react';
1
+ import {
2
+ FillButton,
3
+ FlexBox,
4
+ Spinner,
5
+ TextButton,
6
+ ToolTip,
7
+ } from '@codecademy/gamut';
8
+ import { CopyIcon } from '@codecademy/gamut-icons';
9
+ import { theme } from '@codecademy/gamut-styles';
10
+ import styled from '@emotion/styled';
11
+ import React, { useState } from 'react';
12
+
13
+ import type { languageOption } from './consts';
14
+ import { Drawers } from './drawers';
15
+
16
+ const Output = styled.pre<{ hasError: boolean }>`
17
+ width: 100%;
18
+ height: 20rem;
19
+ margin: 0;
20
+ padding: 0 1rem;
21
+ font-family: Monaco;
22
+ font-size: 0.875rem;
23
+ overflow: auto;
24
+ ${({ hasError }) => `
25
+ color: ${hasError ? theme.colors.orange : theme.colors.white};
26
+ background-color: ${theme.colors['gray-900']};
27
+ `}
28
+ `;
29
+
30
+ const CopyIconStyled = styled(CopyIcon)`
31
+ margin-right: 0.5rem;
32
+ `;
33
+
34
+ const DOCKER_SIGTERM = 143;
2
35
 
3
36
  type EditorProps = {
37
+ hideCopyButton: boolean;
38
+ language: languageOption;
4
39
  text: string;
5
- onChange: (text: string) => void;
40
+ // eslint-disable-next-line react/no-unused-prop-types
41
+ onChange: (
42
+ text: string
43
+ ) => void /* TODO: Add onChange behavior in DISC-353 */;
44
+ onCopy?: (text: string, language: string) => void;
6
45
  };
7
46
 
8
- export const Editor: React.FC<EditorProps> = ({ text, onChange }) => {
9
- return <textarea value={text} onChange={(e) => onChange(e.target.value)} />;
47
+ interface Response {
48
+ stderr: string;
49
+ stdout: string;
50
+ exit_code: number;
51
+ }
52
+
53
+ export const Editor: React.FC<EditorProps> = ({
54
+ language,
55
+ text,
56
+ hideCopyButton,
57
+ onCopy,
58
+ }) => {
59
+ const [output, setOutput] = useState('');
60
+ const [status, setStatus] = useState<'ready' | 'waiting' | 'error'>('ready');
61
+ const [isCodeByteCopied, setIsCodeByteCopied] = useState(false);
62
+ const onCopyClick = () => {
63
+ if (!isCodeByteCopied) {
64
+ navigator.clipboard
65
+ .writeText(text)
66
+ // eslint-disable-next-line no-console
67
+ .catch(() => console.error('Failed to copy'));
68
+ onCopy?.(
69
+ text,
70
+ language
71
+ ); /* TODO: pass in onCopyBBCodeblock behavior from the future version we migrate to Next.js */
72
+ setIsCodeByteCopied(true);
73
+ }
74
+ };
75
+
76
+ const setErrorStatusAndOutput = (message: string) => {
77
+ setOutput(message);
78
+ setStatus('error');
79
+ };
80
+
81
+ const handleSubmit = () => {
82
+ if (text.trim().length === 0) {
83
+ return;
84
+ }
85
+ const data = {
86
+ language,
87
+ code: text,
88
+ };
89
+ setStatus('waiting');
90
+ setOutput('');
91
+
92
+ const snippetsEndpoint =
93
+ 'https://' + process.env.GATSBY_CONTAINER_API_BASE + '/snippets';
94
+
95
+ fetch(snippetsEndpoint, {
96
+ method: 'POST',
97
+ body: JSON.stringify(data),
98
+ headers: {
99
+ 'x-codecademy-user-id': 'codebytes-anon-user',
100
+ },
101
+ })
102
+ .then((res) => res.json())
103
+ .then((response: Response) => {
104
+ if (response.stderr.length > 0) {
105
+ setErrorStatusAndOutput(response.stderr);
106
+ } else if (response.exit_code === DOCKER_SIGTERM) {
107
+ setErrorStatusAndOutput(
108
+ 'Your code took too long to return a result. Double check your code for any issues and try again!'
109
+ );
110
+ } else if (response.exit_code !== 0) {
111
+ setErrorStatusAndOutput('An unknown error occured.');
112
+ } else {
113
+ setOutput(response.stdout);
114
+ setStatus('ready');
115
+ }
116
+ })
117
+ .catch((error) => {
118
+ setErrorStatusAndOutput('Error: ' + error);
119
+ });
120
+ };
121
+
122
+ return (
123
+ <>
124
+ <Drawers
125
+ leftChild={<div>{text}</div>}
126
+ rightChild={
127
+ <Output hasError={status === 'error'} aria-live="polite">
128
+ {output}
129
+ </Output>
130
+ }
131
+ />
132
+ <FlexBox
133
+ justifyContent={hideCopyButton ? 'flex-end' : 'space-between'}
134
+ pl="8"
135
+ >
136
+ {!hideCopyButton ? (
137
+ <ToolTip
138
+ id="codebyte-copied"
139
+ alignment="top-right"
140
+ mode="dark"
141
+ target={
142
+ <TextButton
143
+ variant="secondary"
144
+ onClick={onCopyClick}
145
+ onBlur={() => setIsCodeByteCopied(false)}
146
+ >
147
+ <CopyIconStyled aria-hidden="true" /> Copy Codebyte
148
+ </TextButton>
149
+ }
150
+ >
151
+ {isCodeByteCopied ? (
152
+ <span role="alert">Copied!</span>
153
+ ) : (
154
+ <span>Copy to your clipboard</span>
155
+ )}
156
+ </ToolTip>
157
+ ) : null}
158
+ <FillButton onClick={handleSubmit}>
159
+ {status === 'waiting' ? <Spinner /> : 'Run'}
160
+ </FillButton>
161
+ </FlexBox>
162
+ </>
163
+ );
10
164
  };
package/src/index.tsx CHANGED
@@ -1,23 +1,61 @@
1
+ import { Box, IconButton } from '@codecademy/gamut';
2
+ import { FaviconIcon } from '@codecademy/gamut-icons';
3
+ import { Background, system } from '@codecademy/gamut-styles';
4
+ import styled from '@emotion/styled';
1
5
  import React, { useState } from 'react';
2
6
 
7
+ import { languageOption } from './consts';
3
8
  import { Editor } from './editor';
4
9
 
5
10
  export interface CodeByteEditorProps {
6
11
  text: string;
12
+ language: languageOption;
13
+ hideCopyButton: boolean;
14
+ onCopy?: (text: string, language: string) => void;
7
15
  }
8
16
 
17
+ const EditorContainer = styled(Background)(
18
+ system.css({
19
+ border: '1',
20
+ borderColor: 'gray-900',
21
+ display: 'flex',
22
+ flexDirection: 'column',
23
+ height: '400px',
24
+ width: '688px',
25
+ overflow: 'hidden',
26
+ })
27
+ );
28
+
9
29
  export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
10
30
  text: initialText,
31
+ language,
32
+ hideCopyButton,
33
+ onCopy,
11
34
  }) => {
12
35
  const [text, setText] = useState<string>(initialText);
13
36
  return (
14
37
  <>
15
- <Editor
16
- text={text}
17
- onChange={(newText: string) => {
18
- setText(newText);
19
- }}
20
- />
38
+ <EditorContainer bg="black" as="main">
39
+ <Box borderBottom="1" borderColor="gray-900" py="4" pl="8">
40
+ <IconButton
41
+ icon={FaviconIcon}
42
+ variant="secondary"
43
+ href="https://www.codecademy.com/"
44
+ target="_blank"
45
+ rel="noreferrer"
46
+ aria-label="visit codecademy.com"
47
+ />
48
+ </Box>
49
+ <Editor
50
+ language={language}
51
+ text={text}
52
+ hideCopyButton={hideCopyButton}
53
+ onChange={(newText: string) => {
54
+ setText(newText);
55
+ }}
56
+ onCopy={onCopy}
57
+ />
58
+ </EditorContainer>
21
59
  </>
22
60
  );
23
61
  };
package/src/editor.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import React from 'react';
2
- declare type EditorProps = {
3
- text: string;
4
- onChange: (text: string) => void;
5
- };
6
- export declare const Editor: React.FC<EditorProps>;
7
- export {};
package/src/index.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import React from 'react';
2
- export interface CodeByteEditorProps {
3
- text: string;
4
- }
5
- export declare const CodeByteEditor: React.FC<CodeByteEditorProps>;
6
- export default CodeByteEditor;