@codecademy/codebytes 0.1.1-alpha.ddee0b.0 → 0.2.1-alpha.20333e.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.ddee0b.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.1.0...@codecademy/codebytes@0.1.1-alpha.ddee0b.0) (2022-01-03)
6
+ ### [0.2.1-alpha.20333e.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.2.0...@codecademy/codebytes@0.2.1-alpha.20333e.0) (2022-01-11)
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.ddee0b.0",
4
+ "version": "0.2.1-alpha.20333e.0",
5
5
  "author": "Codecademy Engineering <dev@codecademy.com>",
6
6
  "sideEffects": [
7
7
  "**/*.css",
@@ -25,7 +25,10 @@
25
25
  "@codecademy/gamut-styles": "*",
26
26
  "@codecademy/variance": "*",
27
27
  "@emotion/react": "^11.4.0",
28
- "@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"
29
32
  },
30
33
  "scripts": {
31
34
  "verify": "tsc --noEmit",
@@ -39,10 +42,11 @@
39
42
  "devDependencies": {
40
43
  "@emotion/jest": "^11.3.0",
41
44
  "@testing-library/dom": "^7.31.2",
42
- "@testing-library/react": "^11.0.4"
45
+ "@testing-library/react": "^11.0.4",
46
+ "@types/loadable__component": "^5.13.2"
43
47
  },
44
48
  "publishConfig": {
45
49
  "access": "public"
46
50
  },
47
- "gitHead": "a32b92d7fe89f0553729f4d82d9965e1d56340ec"
51
+ "gitHead": "6e9e6706acf142e4f1cb09d1455b72beca3168fb"
48
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,56 @@
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 editorWillMount = useCallback(
29
+ (editor: ThemedEditor, monaco: Monaco) => {
30
+ editorRef.current = editor;
31
+ monaco.editor.defineTheme('dark', dark);
32
+ monaco.editor.setTheme('dark');
33
+ },
34
+ []
35
+ );
36
+ return (
37
+ <>
38
+ <ResizeObserver
39
+ onResize={({ height, width }) => {
40
+ editorRef.current?.layout({
41
+ height,
42
+ width,
43
+ });
44
+ }}
45
+ />
46
+ <ReactMonacoEditor
47
+ onMount={editorWillMount}
48
+ onChange={onChange}
49
+ options={{ minimap: { enabled: false } }}
50
+ theme="vs-dark"
51
+ value={value}
52
+ language={language}
53
+ />
54
+ </>
55
+ );
56
+ };
@@ -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 CHANGED
@@ -1,25 +1,28 @@
1
1
  import type { languageOption } from './consts';
2
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
- }
3
+ interface Response {
4
+ stderr: string;
5
+ stdout: string;
6
+ exit_code: number;
7
+ }
13
8
 
14
- export const postSnippet = async (data: PostSnippetData, snippetsBaseUrl?: string): Promise<Response> => {
15
- const snippetsEndpoint = `https://${snippetsBaseUrl}/snippets`;
9
+ interface PostSnippetData {
10
+ language: languageOption;
11
+ code: string;
12
+ }
16
13
 
17
- const response = await fetch(snippetsEndpoint, {
18
- method: 'POST',
19
- body: JSON.stringify(data),
20
- headers: {
21
- 'x-codecademy-user-id': 'codebytes-anon-user',
22
- },
23
- });
24
- return response.json();
25
- };
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
@@ -13,3 +13,52 @@ export const languageOptions = {
13
13
  };
14
14
 
15
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/editor.tsx CHANGED
@@ -6,13 +6,13 @@ 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
 
13
12
  import { postSnippet } from './api';
14
13
  import type { languageOption } from './consts';
15
14
  import { Drawers } from './drawers';
15
+ import { SimpleMonacoEditor } from './MonacoEditor';
16
16
 
17
17
  const Output = styled.pre<{ hasError: boolean }>`
18
18
  width: 100%;
@@ -22,9 +22,9 @@ const Output = styled.pre<{ hasError: boolean }>`
22
22
  font-family: Monaco;
23
23
  font-size: 0.875rem;
24
24
  overflow: auto;
25
- ${({ hasError }) => `
25
+ ${({ hasError, theme }) => `
26
26
  color: ${hasError ? theme.colors.orange : theme.colors.white};
27
- background-color: ${theme.colors['gray-900']};
27
+ background-color: ${theme.colors['navy-900']};
28
28
  `}
29
29
  `;
30
30
 
@@ -38,10 +38,7 @@ type EditorProps = {
38
38
  hideCopyButton: boolean;
39
39
  language: languageOption;
40
40
  text: string;
41
- // eslint-disable-next-line react/no-unused-prop-types
42
- onChange: (
43
- text: string
44
- ) => void /* TODO: Add onChange behavior in DISC-353 */;
41
+ onChange: (text: string) => void;
45
42
  onCopy?: (text: string, language: string) => void;
46
43
  snippetsBaseUrl?: string;
47
44
  };
@@ -51,6 +48,7 @@ export const Editor: React.FC<EditorProps> = ({
51
48
  text,
52
49
  hideCopyButton,
53
50
  onCopy,
51
+ onChange,
54
52
  snippetsBaseUrl,
55
53
  }) => {
56
54
  const [output, setOutput] = useState('');
@@ -62,10 +60,7 @@ export const Editor: React.FC<EditorProps> = ({
62
60
  .writeText(text)
63
61
  // eslint-disable-next-line no-console
64
62
  .catch(() => console.error('Failed to copy'));
65
- onCopy?.(
66
- text,
67
- language
68
- ); /* TODO: pass in onCopyBBCodeblock behavior from the future version we migrate to Next.js */
63
+ onCopy?.(text, language);
69
64
  setIsCodeByteCopied(true);
70
65
  }
71
66
  };
@@ -108,7 +103,13 @@ export const Editor: React.FC<EditorProps> = ({
108
103
  return (
109
104
  <>
110
105
  <Drawers
111
- leftChild={<div>{text}</div>}
106
+ leftChild={
107
+ <SimpleMonacoEditor
108
+ value={text}
109
+ language={language}
110
+ onChange={onChange}
111
+ />
112
+ }
112
113
  rightChild={
113
114
  <Output hasError={status === 'error'} aria-live="polite">
114
115
  {output}
package/src/index.tsx CHANGED
@@ -5,8 +5,21 @@ import { StyleProps } from '@codecademy/variance';
5
5
  import styled from '@emotion/styled';
6
6
  import React, { useState } from 'react';
7
7
 
8
- import { languageOption } from './consts';
8
+ import { helloWorld, languageOption } from './consts';
9
9
  import { Editor } from './editor';
10
+ import { LanguageSelection } from './languageSelection';
11
+
12
+ type ChangeHandler = (text: string, language: languageOption) => void;
13
+ export interface CodeByteEditorProps {
14
+ text: string;
15
+ language: languageOption;
16
+ hideCopyButton: boolean;
17
+ onCopy?: (text: string, language: string) => void;
18
+ isIFrame?: boolean;
19
+ 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. */;
20
+ onEdit?: ChangeHandler;
21
+ onLanguageChange?: ChangeHandler;
22
+ }
10
23
 
11
24
  const editorStates = states({
12
25
  isIFrame: { height: '100vh' },
@@ -30,26 +43,17 @@ const EditorContainer = styled(Background)<EditorStyleProps>(
30
43
  editorStates
31
44
  );
32
45
 
33
- export interface CodeByteEditorProps {
34
- text: string;
35
- language: languageOption;
36
- hideCopyButton: boolean;
37
- onCopy?: (text: string, language: string) => void;
38
- isIFrame?: boolean;
39
- snippetsBaseUrl?: string;
40
- onTextChange?: (text: string) => void;
41
- }
42
-
43
46
  export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
44
47
  text: initialText,
45
- language,
48
+ language: initialLanguage,
46
49
  hideCopyButton,
47
50
  onCopy,
48
51
  isIFrame = false,
49
52
  snippetsBaseUrl = process.env.CONTAINER_API_BASE,
50
- onTextChange,
53
+ onEdit,
51
54
  }) => {
52
55
  const [text, setText] = useState<string>(initialText);
56
+ const [language, setLanguage] = useState<languageOption>(initialLanguage);
53
57
  return (
54
58
  <EditorContainer bg="black" as="main" isIFrame={isIFrame}>
55
59
  <Box borderBottom={1} borderColor="gray-900" py={4} pl={8}>
@@ -62,17 +66,34 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
62
66
  aria-label="visit codecademy.com"
63
67
  />
64
68
  </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
- />
69
+ {language ? (
70
+ <Editor
71
+ language={language}
72
+ text={text}
73
+ hideCopyButton={hideCopyButton}
74
+ onChange={(newText: string) => {
75
+ setText(newText);
76
+ onEdit?.(
77
+ newText,
78
+ language
79
+ ); /* TODO: in DISC-355 update component to use an object that contains all change handlers */
80
+ }}
81
+ onCopy={onCopy}
82
+ snippetsBaseUrl={snippetsBaseUrl}
83
+ />
84
+ ) : (
85
+ <LanguageSelection
86
+ onChange={(newLanguage) => {
87
+ const newText: string = text || helloWorld[newLanguage] || '';
88
+ setLanguage(newLanguage);
89
+ setText(newText);
90
+ onLanguageChange?.(
91
+ newText,
92
+ newLanguage
93
+ ); /* TODO: in DISC-355 update component to use an object that contains all change handlers */
94
+ }}
95
+ />
96
+ )}
76
97
  </EditorContainer>
77
98
  );
78
99
  };
@@ -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 CHANGED
@@ -1,5 +1,4 @@
1
1
  import { CoreTheme } from '@codecademy/gamut-styles';
2
-
3
2
  declare module '@emotion/react' {
4
3
  export interface Theme extends CoreTheme {}
5
- }
4
+ }