@codecademy/codebytes 0.1.1-alpha.db68a2.0 → 0.2.1-alpha.1bbcee.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.db68a2.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.1.0...@codecademy/codebytes@0.1.1-alpha.db68a2.0) (2021-12-21)
6
+ ### [0.2.1-alpha.1bbcee.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.2.0...@codecademy/codebytes@0.2.1-alpha.1bbcee.0) (2022-01-12)
7
7
 
8
8
  **Note:** Version bump only for package @codecademy/codebytes
9
9
 
@@ -11,6 +11,15 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
11
11
 
12
12
 
13
13
 
14
+ ## [0.2.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.1.0...@codecademy/codebytes@0.2.0) (2022-01-04)
15
+
16
+
17
+ ### Features
18
+
19
+ * **Codebytes:** add editor and drawers disc 351 ([#14](https://github.com/Codecademy/client-modules/issues/14)) ([e84e265](https://github.com/Codecademy/client-modules/commit/e84e265e4cf4bf8360830ebf2dbea930ab503c9c))
20
+
21
+
22
+
14
23
  ## 0.1.0 (2021-12-17)
15
24
 
16
25
 
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.db68a2.0",
4
+ "version": "0.2.1-alpha.1bbcee.0",
5
5
  "author": "Codecademy Engineering <dev@codecademy.com>",
6
6
  "sideEffects": [
7
7
  "**/*.css",
@@ -23,8 +23,12 @@
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
- "@emotion/styled": "^11.3.0"
28
+ "@emotion/styled": "^11.3.0",
29
+ "@monaco-editor/react": "4.3.1",
30
+ "monaco-editor": ">= 0.25.0 < 1",
31
+ "react-resize-observer": "1.1.1"
28
32
  },
29
33
  "scripts": {
30
34
  "verify": "tsc --noEmit",
@@ -38,10 +42,11 @@
38
42
  "devDependencies": {
39
43
  "@emotion/jest": "^11.3.0",
40
44
  "@testing-library/dom": "^7.31.2",
41
- "@testing-library/react": "^11.0.4"
45
+ "@testing-library/react": "^11.0.4",
46
+ "@types/loadable__component": "^5.13.2"
42
47
  },
43
48
  "publishConfig": {
44
49
  "access": "public"
45
50
  },
46
- "gitHead": "0418b8a6fda0efc860f1b0f454957fca092213ea"
51
+ "gitHead": "5684e9349dc3938435670eea193968b6fce784eb"
47
52
  }
@@ -0,0 +1,54 @@
1
+ // DO NOT CHANGE ANYTHING HERE
2
+ // This file is part of the Codebytes MVP and only includes basic configuration around theming for the SimpleMonacoEditor component
3
+ // We are working on a monaco package in client-modules that has more configuration around themes and languages
4
+ // Monaco as a shared package RFC https://www.notion.so/codecademy/Monaco-editor-as-a-shared-package-1f4484db165b4abc8394c3556451ef6a
5
+
6
+ import { colors, editorColors } from '@codecademy/gamut-styles';
7
+
8
+ const darkTheme = {
9
+ blue: editorColors.blue,
10
+ deepPurple: editorColors.deepPurple,
11
+ gray: editorColors.gray,
12
+ green: editorColors.green,
13
+ orange: editorColors.orange,
14
+ purple: editorColors.purple,
15
+ red: editorColors.red,
16
+ white: colors.white,
17
+ yellow: editorColors.yellow,
18
+ };
19
+
20
+ export const syntax = {
21
+ attribute: darkTheme.green,
22
+ annotation: darkTheme.red,
23
+ atom: darkTheme.deepPurple,
24
+ basic: darkTheme.white,
25
+ comment: darkTheme.gray,
26
+ constant: darkTheme.orange,
27
+ decoration: darkTheme.red,
28
+ invalid: darkTheme.red,
29
+ key: darkTheme.blue,
30
+ keyword: darkTheme.purple,
31
+ number: darkTheme.red,
32
+ operator: darkTheme.red,
33
+ predefined: darkTheme.white,
34
+ property: darkTheme.red,
35
+ regexp: darkTheme.green,
36
+ string: darkTheme.yellow,
37
+ tag: darkTheme.red,
38
+ text: darkTheme.orange,
39
+ value: darkTheme.yellow,
40
+ variable: darkTheme.green,
41
+ };
42
+
43
+ export const ui = {
44
+ background: '#211E2F',
45
+ text: darkTheme.white,
46
+ indent: {
47
+ active: '#393b41',
48
+ inactive: '#494b51',
49
+ },
50
+ };
51
+
52
+ export type SyntaxColors = typeof syntax;
53
+
54
+ export type UIColors = typeof ui;
@@ -0,0 +1,53 @@
1
+ // DO NOT CHANGE ANYTHING HERE
2
+ // This component is part of the Codebytes MVP and only includes basic configuration around theming
3
+ // We are working on a monaco package in client-modules that has more configuration around themes and languages
4
+ // Monaco as a shared package RFC https://www.notion.so/codecademy/Monaco-editor-as-a-shared-package-1f4484db165b4abc8394c3556451ef6a
5
+
6
+ import ReactMonacoEditor, { OnMount } from '@monaco-editor/react';
7
+ import { editor } from 'monaco-editor/esm/vs/editor/editor.api';
8
+ import React, { useCallback, useRef } from 'react';
9
+ import ResizeObserver from 'react-resize-observer';
10
+
11
+ import { dark } from './theme';
12
+ import { Monaco } from './types';
13
+
14
+ export type SimpleMonacoEditorProps = {
15
+ value: string;
16
+ language: string;
17
+ onChange?: (value: string) => void;
18
+ };
19
+
20
+ type ThemedEditor = Parameters<OnMount>[0];
21
+
22
+ export const SimpleMonacoEditor: React.FC<SimpleMonacoEditorProps> = ({
23
+ value,
24
+ language,
25
+ onChange,
26
+ }) => {
27
+ const editorRef = useRef<editor.IStandaloneCodeEditor | null>(null);
28
+ const editorOnMount = useCallback((editor: ThemedEditor, monaco: Monaco) => {
29
+ editorRef.current = editor;
30
+ monaco.editor.defineTheme('dark', dark);
31
+ monaco.editor.setTheme('dark');
32
+ }, []);
33
+ return (
34
+ <>
35
+ <ResizeObserver
36
+ onResize={({ height, width }) => {
37
+ editorRef.current?.layout({
38
+ height,
39
+ width,
40
+ });
41
+ }}
42
+ />
43
+ <ReactMonacoEditor
44
+ onMount={editorOnMount}
45
+ onChange={onChange}
46
+ options={{ minimap: { enabled: false } }}
47
+ theme="vs-dark"
48
+ value={value}
49
+ language={language}
50
+ />
51
+ </>
52
+ );
53
+ };
@@ -0,0 +1,64 @@
1
+ // DO NOT CHANGE ANYTHING HERE
2
+ // This file is part of the Codebytes MVP and only includes basic configuration around theming for the SimpleMonacoEditor component
3
+ // We are working on a monaco package in client-modules that has more configuration around themes and languages
4
+ // Monaco as a shared package RFC https://www.notion.so/codecademy/Monaco-editor-as-a-shared-package-1f4484db165b4abc8394c3556451ef6a
5
+
6
+ import type * as monaco from 'monaco-editor';
7
+
8
+ import * as darkColors from './colorsDark';
9
+
10
+ const c = (color: string) => color.substr(1);
11
+
12
+ const theme = ({
13
+ ui,
14
+ syntax,
15
+ }: {
16
+ ui: darkColors.UIColors;
17
+ syntax: darkColors.SyntaxColors;
18
+ }): monaco.editor.IStandaloneThemeData => ({
19
+ base: 'vs-dark',
20
+ inherit: true,
21
+ rules: [
22
+ // Base
23
+ { token: '', foreground: c(syntax.basic) },
24
+ { token: 'regexp', foreground: c(syntax.regexp) },
25
+ { token: 'annotation', foreground: c(syntax.annotation) },
26
+ { token: 'type', foreground: c(syntax.annotation) },
27
+ { token: 'doctype', foreground: c(syntax.comment) },
28
+ { token: 'delimiter', foreground: c(syntax.decoration) },
29
+ { token: 'invalid', foreground: c(syntax.invalid) },
30
+ { token: 'emphasis', fontStyle: 'italic' },
31
+ { token: 'strong', fontStyle: 'bold' },
32
+ { token: 'variable', foreground: c(syntax.variable) },
33
+ { token: 'variable.predefined', foreground: c(syntax.variable) },
34
+ { token: 'constant', foreground: c(syntax.constant) },
35
+ { token: 'comment', foreground: c(syntax.comment) },
36
+ { token: 'number', foreground: c(syntax.number) },
37
+ { token: 'number.hex', foreground: c(syntax.number) },
38
+ { token: 'keyword.directive', foreground: c(syntax.comment) },
39
+ { token: 'include', foreground: c(syntax.comment) },
40
+ { token: 'key', foreground: c(syntax.property) },
41
+ { token: 'attribute.name', foreground: c(syntax.attribute) },
42
+ { token: 'attribute.name-numeric', foreground: c(syntax.string) },
43
+ { token: 'attribute.value', foreground: c(syntax.property) },
44
+ { token: 'attribute.value.number', foreground: c(syntax.number) },
45
+ { token: 'string', foreground: c(syntax.string) },
46
+ { token: 'string.yaml', foreground: c(syntax.string) },
47
+ { token: 'tag', foreground: c(syntax.tag) },
48
+ { token: 'tag.id.jade', foreground: c(syntax.tag) },
49
+ { token: 'tag.class.jade', foreground: c(syntax.tag) },
50
+ { token: 'metatag', foreground: c(syntax.comment) },
51
+ { token: 'attribute.value.unit', foreground: c(syntax.string) },
52
+ { token: 'keyword', foreground: c(syntax.keyword) },
53
+ { token: 'keyword.flow', foreground: c(syntax.keyword) },
54
+ ],
55
+ colors: {
56
+ 'editor.background': ui.background,
57
+ 'editor.foreground': ui.text,
58
+ 'editorIndentGuide.background': ui.indent.inactive,
59
+ 'editorIndentGuide.activeBackground': ui.indent.active,
60
+ 'editorWhitespace.foreground': syntax.comment,
61
+ },
62
+ });
63
+
64
+ export const dark = theme(darkColors);
@@ -0,0 +1 @@
1
+ export type Monaco = typeof import('monaco-editor');
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/consts.ts CHANGED
@@ -1,15 +1,64 @@
1
1
  // key = language param to send to snippets service
2
2
  // val = label in language selection drop down
3
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;
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;
16
+
17
+ export const validLanguages = Object.keys(languageOptions).filter(
18
+ (option) => !!option
19
+ ) as languageOption[];
20
+
21
+ const cpp = `#include <iostream>
22
+ int main() {
23
+ std::cout << "Hello world!";
24
+ return 0;
25
+ }`;
26
+
27
+ const csharp = `namespace HelloWorld {
28
+ class Hello {
29
+ static void Main(string[] args) {
30
+ System.Console.WriteLine("Hello world!");
31
+ }
32
+ }
33
+ }`;
34
+
35
+ const golang = `package main
36
+ import "fmt"
37
+ func main() {
38
+ fmt.Println("Hello world!")
39
+ }`;
40
+
41
+ const javascript = "console.log('Hello world!');";
42
+
43
+ const php = `<?php
44
+ echo "Hello world!";
45
+ ?>`;
46
+
47
+ const python = "print('Hello world!')";
48
+
49
+ const ruby = 'puts "Hello world!"';
50
+
51
+ const scheme = `(begin
52
+ (display "Hello world!")
53
+ (newline))`;
54
+
55
+ export const helloWorld: { [key in languageOption]?: string } = {
56
+ cpp,
57
+ csharp,
58
+ golang,
59
+ javascript,
60
+ php,
61
+ python,
62
+ ruby,
63
+ scheme,
64
+ };
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
@@ -6,24 +6,26 @@ import {
6
6
  ToolTip,
7
7
  } from '@codecademy/gamut';
8
8
  import { CopyIcon } from '@codecademy/gamut-icons';
9
- import { theme } from '@codecademy/gamut-styles';
10
9
  import styled from '@emotion/styled';
11
10
  import React, { useState } from 'react';
12
11
 
12
+ import { CodebytesChangeHandlerMap } from '.';
13
+ import { postSnippet } from './api';
13
14
  import type { languageOption } from './consts';
14
15
  import { Drawers } from './drawers';
16
+ import { SimpleMonacoEditor } from './MonacoEditor';
15
17
 
16
18
  const Output = styled.pre<{ hasError: boolean }>`
17
19
  width: 100%;
18
- height: 20rem;
20
+ height: 100%;
19
21
  margin: 0;
20
22
  padding: 0 1rem;
21
23
  font-family: Monaco;
22
24
  font-size: 0.875rem;
23
25
  overflow: auto;
24
- ${({ hasError }) => `
26
+ ${({ hasError, theme }) => `
25
27
  color: ${hasError ? theme.colors.orange : theme.colors.white};
26
- background-color: ${theme.colors['gray-900']};
28
+ background-color: ${theme.colors['navy-900']};
27
29
  `}
28
30
  `;
29
31
 
@@ -37,22 +39,19 @@ type EditorProps = {
37
39
  hideCopyButton: boolean;
38
40
  language: languageOption;
39
41
  text: string;
40
- // eslint-disable-next-line react/no-unused-prop-types
41
- onChange: (text: string) => void /* Will use in upcoming pr */;
42
- onCopy?: (text: string, language: string) => void;
43
- };
44
42
 
45
- interface Response {
46
- stderr: string;
47
- stdout: string;
48
- exit_code: number;
49
- }
43
+ onChange: (text: string) => void;
44
+ on?: Pick<CodebytesChangeHandlerMap, 'copy' | 'run'>;
45
+ snippetsBaseUrl?: string;
46
+ };
50
47
 
51
48
  export const Editor: React.FC<EditorProps> = ({
52
49
  language,
53
50
  text,
54
51
  hideCopyButton,
55
- onCopy,
52
+ on,
53
+ onChange,
54
+ snippetsBaseUrl,
56
55
  }) => {
57
56
  const [output, setOutput] = useState('');
58
57
  const [status, setStatus] = useState<'ready' | 'waiting' | 'error'>('ready');
@@ -63,10 +62,7 @@ export const Editor: React.FC<EditorProps> = ({
63
62
  .writeText(text)
64
63
  // eslint-disable-next-line no-console
65
64
  .catch(() => console.error('Failed to copy'));
66
- onCopy?.(
67
- text,
68
- language
69
- ); /* TODO: pass in onCopyBBCodeblock behavior from static sites */
65
+ on?.copy?.(text, language);
70
66
  setIsCodeByteCopied(true);
71
67
  }
72
68
  };
@@ -76,7 +72,7 @@ export const Editor: React.FC<EditorProps> = ({
76
72
  setStatus('error');
77
73
  };
78
74
 
79
- const handleSubmit = () => {
75
+ const handleSubmit = async () => {
80
76
  if (text.trim().length === 0) {
81
77
  return;
82
78
  }
@@ -86,41 +82,37 @@ export const Editor: React.FC<EditorProps> = ({
86
82
  };
87
83
  setStatus('waiting');
88
84
  setOutput('');
85
+ on?.run?.();
89
86
 
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
- });
87
+ try {
88
+ const response = await postSnippet(data, snippetsBaseUrl);
89
+ if (response.stderr.length > 0) {
90
+ setErrorStatusAndOutput(response.stderr);
91
+ } else if (response.exit_code === DOCKER_SIGTERM) {
92
+ setErrorStatusAndOutput(
93
+ 'Your code took too long to return a result. Double check your code for any issues and try again!'
94
+ );
95
+ } else if (response.exit_code !== 0) {
96
+ setErrorStatusAndOutput('An unknown error occured.');
97
+ } else {
98
+ setOutput(response.stdout);
99
+ setStatus('ready');
100
+ }
101
+ } catch (error) {
102
+ setErrorStatusAndOutput('Error: ' + error);
103
+ }
118
104
  };
119
105
 
120
106
  return (
121
107
  <>
122
108
  <Drawers
123
- leftChild={<div>{text}</div>}
109
+ leftChild={
110
+ <SimpleMonacoEditor
111
+ value={text}
112
+ language={language}
113
+ onChange={onChange}
114
+ />
115
+ }
124
116
  rightChild={
125
117
  <Output hasError={status === 'error'} aria-live="polite">
126
118
  {output}
@@ -129,7 +121,7 @@ export const Editor: React.FC<EditorProps> = ({
129
121
  />
130
122
  <FlexBox
131
123
  justifyContent={hideCopyButton ? 'flex-end' : 'space-between'}
132
- pl="8"
124
+ pl={8}
133
125
  >
134
126
  {!hideCopyButton ? (
135
127
  <ToolTip
package/src/index.tsx CHANGED
@@ -1,59 +1,100 @@
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
- import { languageOption } from './consts';
8
+ import { helloWorld, languageOption } from './consts';
8
9
  import { Editor } from './editor';
10
+ import { LanguageSelection } from './languageSelection';
11
+
12
+ type CodebytesChangeHandler = (text: string, language: languageOption) => void;
13
+ export type CodebytesChangeHandlerMap = {
14
+ logoClick?: () => void;
15
+ edit?: CodebytesChangeHandler;
16
+ languageChange?: CodebytesChangeHandler;
17
+ copy?: CodebytesChangeHandler;
18
+ run?: () => void;
19
+ };
9
20
 
10
21
  export interface CodeByteEditorProps {
11
22
  text: string;
12
23
  language: languageOption;
13
24
  hideCopyButton: boolean;
25
+ isIFrame?: boolean;
26
+ snippetsBaseUrl?: string /* TODO in DISC-353: Determine best way to host and include snippets endpoint for both staging and production in both the monolith and next.js repo. */;
27
+ on?: CodebytesChangeHandlerMap;
14
28
  }
15
29
 
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
- })
30
+ const editorStates = states({
31
+ isIFrame: { height: '100vh' },
32
+ });
33
+
34
+ const editorBaseStyles = system.css({
35
+ border: 1,
36
+ borderColor: 'gray-900',
37
+ display: 'flex',
38
+ flexDirection: 'column',
39
+ height: '25rem',
40
+ width: '43rem',
41
+ overflow: 'hidden',
42
+ });
43
+ export interface EditorStyleProps
44
+ extends StyleProps<typeof editorBaseStyles>,
45
+ StyleProps<typeof editorStates> {}
46
+
47
+ const EditorContainer = styled(Background)<EditorStyleProps>(
48
+ editorBaseStyles,
49
+ editorStates
26
50
  );
27
51
 
28
52
  export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
29
53
  text: initialText,
30
- language,
54
+ language: initialLanguage,
31
55
  hideCopyButton,
56
+ isIFrame = false,
57
+ on,
58
+ snippetsBaseUrl = process.env.CONTAINER_API_BASE,
32
59
  }) => {
33
60
  const [text, setText] = useState<string>(initialText);
61
+ const [language, setLanguage] = useState<languageOption>(initialLanguage);
34
62
  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>
63
+ <EditorContainer bg="black" as="main" isIFrame={isIFrame}>
64
+ <Box borderBottom={1} borderColor="gray-900" py={4} pl={8}>
65
+ <IconButton
66
+ icon={FaviconIcon}
67
+ variant="secondary"
68
+ href="https://www.codecademy.com/"
69
+ target="_blank"
70
+ rel="noreferrer"
71
+ aria-label="visit codecademy.com"
72
+ onClick={() => on?.logoClick?.()}
73
+ />
74
+ </Box>
75
+ {language ? (
47
76
  <Editor
77
+ language={language}
48
78
  text={text}
79
+ hideCopyButton={hideCopyButton}
49
80
  onChange={(newText: string) => {
50
81
  setText(newText);
82
+ on?.edit?.(newText, language);
83
+ }}
84
+ on={on}
85
+ snippetsBaseUrl={snippetsBaseUrl}
86
+ />
87
+ ) : (
88
+ <LanguageSelection
89
+ onChange={(newLanguage) => {
90
+ const newText: string = text || helloWorld[newLanguage] || '';
91
+ setLanguage(newLanguage);
92
+ setText(newText);
93
+ on?.languageChange?.(newText, newLanguage);
51
94
  }}
52
- hideCopyButton={hideCopyButton}
53
- language={language}
54
95
  />
55
- </EditorContainer>
56
- </>
96
+ )}
97
+ </EditorContainer>
57
98
  );
58
99
  };
59
100
 
@@ -0,0 +1,45 @@
1
+ import { Select } from '@codecademy/gamut';
2
+ import { Background } from '@codecademy/gamut-styles';
3
+ import styled from '@emotion/styled';
4
+ import React from 'react';
5
+
6
+ import type { languageOption } from './consts';
7
+ import { languageOptions } from './consts';
8
+
9
+ const StyledSelect = styled(Select)`
10
+ margin-top: 1rem;
11
+ color: ${({ theme }) => theme.colors.text};
12
+ select {
13
+ color: ${({ theme }) => theme.colors.text};
14
+ background-color: ${({ theme }) => theme.colors.black};
15
+
16
+ &:hover,
17
+ &:active,
18
+ &:focus {
19
+ border-color: ${({ theme }) => theme.colors.primary};
20
+ }
21
+ &:focus {
22
+ box-shadow: inset 0 0 0 1px ${({ theme }) => theme.colors.primary};
23
+ }
24
+ }
25
+ `;
26
+
27
+ export type LanguageSelectionProps = {
28
+ onChange: (newLanguage: languageOption) => void;
29
+ };
30
+
31
+ export const LanguageSelection: React.FC<LanguageSelectionProps> = ({
32
+ onChange,
33
+ }) => {
34
+ return (
35
+ <Background bg="black" flex={1} px={16} pt={48}>
36
+ Which language do you want to code in?
37
+ <StyledSelect
38
+ id="language-select"
39
+ aria-label="Select your language"
40
+ options={languageOptions}
41
+ onChange={(e) => onChange(e.target.value as languageOption)}
42
+ />
43
+ </Background>
44
+ );
45
+ };
package/src/theme.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { CoreTheme } from '@codecademy/gamut-styles';
2
+ declare module '@emotion/react' {
3
+ export interface Theme extends CoreTheme {}
4
+ }
package/tsconfig.json CHANGED
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "extends": "../../tsconfig.json",
3
3
  "include": ["../../typings/*.d.ts", "./src/**/*.ts", "./src/**/*.tsx"]
4
- }
4
+ }