@codecademy/codebytes 0.2.0 → 0.2.1-alpha.3da9ab.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,14 @@
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.2.1-alpha.3da9ab.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.2.0...@codecademy/codebytes@0.2.1-alpha.3da9ab.0) (2022-01-07)
7
+
8
+ **Note:** Version bump only for package @codecademy/codebytes
9
+
10
+
11
+
12
+
13
+
6
14
  ## [0.2.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.1.0...@codecademy/codebytes@0.2.0) (2022-01-04)
7
15
 
8
16
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@codecademy/codebytes",
3
3
  "description": "Codebytes Code Editor",
4
- "version": "0.2.0",
4
+ "version": "0.2.1-alpha.3da9ab.0",
5
5
  "author": "Codecademy Engineering <dev@codecademy.com>",
6
6
  "sideEffects": [
7
7
  "**/*.css",
@@ -25,7 +25,11 @@
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
+ "@loadable/component": "^5.13.1",
30
+ "monaco-editor": "0.20.0",
31
+ "react-monaco-editor": "0.34.0",
32
+ "react-resize-observer": "1.1.1"
29
33
  },
30
34
  "scripts": {
31
35
  "verify": "tsc --noEmit",
@@ -39,10 +43,12 @@
39
43
  "devDependencies": {
40
44
  "@emotion/jest": "^11.3.0",
41
45
  "@testing-library/dom": "^7.31.2",
42
- "@testing-library/react": "^11.0.4"
46
+ "@testing-library/react": "^11.0.4",
47
+ "@types/loadable__component": "^5.13.2",
48
+ "monaco-editor-webpack-plugin": "1.9.1"
43
49
  },
44
50
  "publishConfig": {
45
51
  "access": "public"
46
52
  },
47
- "gitHead": "db6be3e2ec6108c6eddb25c04097ed959c67db76"
53
+ "gitHead": "55fa9a0e726d555db29ddc01771ff7429e77549c"
48
54
  }
@@ -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 loadable from '@loadable/component';
7
+ import React, { useCallback, useRef } from 'react';
8
+ import MonacoEditor from 'react-monaco-editor';
9
+ import ResizeObserver from 'react-resize-observer';
10
+
11
+ import { dark } from './theme';
12
+ import { Monaco } from './types';
13
+
14
+ const ReactMonacoEditor = loadable(() => import('react-monaco-editor'));
15
+
16
+ export type SimpleMonacoEditorProps = {
17
+ value: string;
18
+ language: string;
19
+ onChange?: (value: string) => void;
20
+ };
21
+
22
+ export const SimpleMonacoEditor: React.FC<SimpleMonacoEditorProps> = ({
23
+ value,
24
+ language,
25
+ onChange,
26
+ }) => {
27
+ const editorRef = useRef<MonacoEditor>(null);
28
+ const editorWillMount = useCallback((monaco: Monaco) => {
29
+ monaco.editor.defineTheme('dark', dark);
30
+ monaco.editor.setTheme('dark');
31
+ }, []);
32
+ return (
33
+ <>
34
+ <ResizeObserver
35
+ onResize={({ height, width }) => {
36
+ editorRef.current?.editor?.layout({
37
+ height,
38
+ width,
39
+ });
40
+ }}
41
+ />
42
+ <ReactMonacoEditor
43
+ ref={editorRef}
44
+ editorWillMount={editorWillMount}
45
+ onChange={onChange}
46
+ options={{ minimap: { enabled: false } }}
47
+ theme="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/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
@@ -13,6 +13,7 @@ import React, { useState } from 'react';
13
13
  import { postSnippet } from './api';
14
14
  import type { languageOption } from './consts';
15
15
  import { Drawers } from './drawers';
16
+ import { SimpleMonacoEditor } from './MonacoEditor';
16
17
 
17
18
  const Output = styled.pre<{ hasError: boolean }>`
18
19
  width: 100%;
@@ -51,6 +52,7 @@ export const Editor: React.FC<EditorProps> = ({
51
52
  text,
52
53
  hideCopyButton,
53
54
  onCopy,
55
+ onChange,
54
56
  snippetsBaseUrl,
55
57
  }) => {
56
58
  const [output, setOutput] = useState('');
@@ -105,7 +107,13 @@ export const Editor: React.FC<EditorProps> = ({
105
107
  return (
106
108
  <>
107
109
  <Drawers
108
- leftChild={<div>{text}</div>}
110
+ leftChild={
111
+ <SimpleMonacoEditor
112
+ value={text}
113
+ language={language}
114
+ onChange={onChange}
115
+ />
116
+ }
109
117
  rightChild={
110
118
  <Output hasError={status === 'error'} aria-live="polite">
111
119
  {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,18 @@ 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 /* 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. */;
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 = '',
50
- onTextChange,
53
+ onEdit,
54
+ onLanguageChange,
51
55
  }) => {
52
56
  const [text, setText] = useState<string>(initialText);
57
+ const [language, setLanguage] = useState<languageOption>(initialLanguage);
53
58
  return (
54
59
  <EditorContainer bg="black" as="main" isIFrame={isIFrame}>
55
60
  <Box borderBottom={1} borderColor="gray-900" py={4} pl={8}>
@@ -62,17 +67,34 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
62
67
  aria-label="visit codecademy.com"
63
68
  />
64
69
  </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
- />
70
+ {language ? (
71
+ <Editor
72
+ language={language}
73
+ text={text}
74
+ hideCopyButton={hideCopyButton}
75
+ onChange={(newText: string) => {
76
+ setText(newText);
77
+ onEdit?.(
78
+ newText,
79
+ language
80
+ ); /* TODO: in DISC-355 update component to use an object that contains all change handlers */
81
+ }}
82
+ onCopy={onCopy}
83
+ snippetsBaseUrl={snippetsBaseUrl}
84
+ />
85
+ ) : (
86
+ <LanguageSelection
87
+ onChange={(newLanguage) => {
88
+ const newText: string = text || helloWorld[newLanguage] || '';
89
+ setLanguage(newLanguage);
90
+ setText(newText);
91
+ onLanguageChange?.(
92
+ newText,
93
+ newLanguage
94
+ ); /* TODO: in DISC-355 update component to use an object that contains all change handlers */
95
+ }}
96
+ />
97
+ )}
76
98
  </EditorContainer>
77
99
  );
78
100
  };
@@ -0,0 +1,46 @@
1
+ import { Select } from '@codecademy/gamut';
2
+ import { Background, themed } 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: ${themed('colors.text')};
12
+
13
+ select {
14
+ color: ${themed('colors.text')};
15
+ background-color: ${themed('colors.black')};
16
+
17
+ &:hover,
18
+ &:active,
19
+ &:focus {
20
+ border-color: ${themed('colors.primary')};
21
+ }
22
+ &:focus {
23
+ box-shadow: inset 0 0 0 1px ${themed('colors.primary')};
24
+ }
25
+ }
26
+ `;
27
+
28
+ export type LanguageSelectionProps = {
29
+ onChange: (newLanguage: languageOption) => void;
30
+ };
31
+
32
+ export const LanguageSelection: React.FC<LanguageSelectionProps> = ({
33
+ onChange,
34
+ }) => {
35
+ return (
36
+ <Background bg="black" flex={1} px={16} pt={48}>
37
+ What language do you want to write?
38
+ <StyledSelect
39
+ id="language-select"
40
+ aria-label="Select your language"
41
+ options={languageOptions}
42
+ onChange={(e) => onChange(e.target.value as languageOption)}
43
+ />
44
+ </Background>
45
+ );
46
+ };
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
  }