@codecademy/codebytes 0.2.1-alpha.cfe771.0 → 0.3.1-alpha.b79ad6.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 +10 -1
- package/package.json +5 -7
- package/src/MonacoEditor/index.tsx +16 -13
- package/src/consts.ts +50 -1
- package/src/editor.tsx +2 -6
- package/src/index.tsx +34 -19
- package/src/languageSelection.tsx +45 -0
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.
|
|
6
|
+
### [0.3.1-alpha.b79ad6.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.3.0...@codecademy/codebytes@0.3.1-alpha.b79ad6.0) (2022-01-13)
|
|
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.3.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.2.0...@codecademy/codebytes@0.3.0) (2022-01-12)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Features
|
|
18
|
+
|
|
19
|
+
* **Codebytes:** add simple monaco editor disc 353 ([#16](https://github.com/Codecademy/client-modules/issues/16)) ([eec98ba](https://github.com/Codecademy/client-modules/commit/eec98ba9aad45f07fb5f3417e3da1e1935985deb))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
14
23
|
## [0.2.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.1.0...@codecademy/codebytes@0.2.0) (2022-01-04)
|
|
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.
|
|
4
|
+
"version": "0.3.1-alpha.b79ad6.0",
|
|
5
5
|
"author": "Codecademy Engineering <dev@codecademy.com>",
|
|
6
6
|
"sideEffects": [
|
|
7
7
|
"**/*.css",
|
|
@@ -26,9 +26,8 @@
|
|
|
26
26
|
"@codecademy/variance": "*",
|
|
27
27
|
"@emotion/react": "^11.4.0",
|
|
28
28
|
"@emotion/styled": "^11.3.0",
|
|
29
|
-
"@
|
|
30
|
-
"monaco-editor": "0.
|
|
31
|
-
"react-monaco-editor": "0.34.0",
|
|
29
|
+
"@monaco-editor/react": "4.3.1",
|
|
30
|
+
"monaco-editor": ">= 0.25.0 < 1",
|
|
32
31
|
"react-resize-observer": "1.1.1"
|
|
33
32
|
},
|
|
34
33
|
"scripts": {
|
|
@@ -44,11 +43,10 @@
|
|
|
44
43
|
"@emotion/jest": "^11.3.0",
|
|
45
44
|
"@testing-library/dom": "^7.31.2",
|
|
46
45
|
"@testing-library/react": "^11.0.4",
|
|
47
|
-
"@types/loadable__component": "^5.13.2"
|
|
48
|
-
"monaco-editor-webpack-plugin": "1.9.1"
|
|
46
|
+
"@types/loadable__component": "^5.13.2"
|
|
49
47
|
},
|
|
50
48
|
"publishConfig": {
|
|
51
49
|
"access": "public"
|
|
52
50
|
},
|
|
53
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "11c60da316a1e033c4b9bc45914733c0b93a0ade"
|
|
54
52
|
}
|
|
@@ -3,48 +3,51 @@
|
|
|
3
3
|
// We are working on a monaco package in client-modules that has more configuration around themes and languages
|
|
4
4
|
// Monaco as a shared package RFC https://www.notion.so/codecademy/Monaco-editor-as-a-shared-package-1f4484db165b4abc8394c3556451ef6a
|
|
5
5
|
|
|
6
|
-
import
|
|
6
|
+
import ReactMonacoEditor, { OnMount } from '@monaco-editor/react';
|
|
7
|
+
import { editor } from 'monaco-editor/esm/vs/editor/editor.api';
|
|
7
8
|
import React, { useCallback, useRef } from 'react';
|
|
8
|
-
import MonacoEditor from 'react-monaco-editor';
|
|
9
9
|
import ResizeObserver from 'react-resize-observer';
|
|
10
10
|
|
|
11
11
|
import { dark } from './theme';
|
|
12
12
|
import { Monaco } from './types';
|
|
13
13
|
|
|
14
|
-
const ReactMonacoEditor = loadable(() => import('react-monaco-editor'));
|
|
15
|
-
|
|
16
14
|
export type SimpleMonacoEditorProps = {
|
|
17
15
|
value: string;
|
|
18
16
|
language: string;
|
|
19
17
|
onChange?: (value: string) => void;
|
|
20
18
|
};
|
|
21
19
|
|
|
20
|
+
type ThemedEditor = Parameters<OnMount>[0];
|
|
21
|
+
|
|
22
22
|
export const SimpleMonacoEditor: React.FC<SimpleMonacoEditorProps> = ({
|
|
23
23
|
value,
|
|
24
24
|
language,
|
|
25
25
|
onChange,
|
|
26
26
|
}) => {
|
|
27
|
-
const editorRef = useRef<
|
|
28
|
-
const editorWillMount = useCallback(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
+
);
|
|
32
36
|
return (
|
|
33
37
|
<>
|
|
34
38
|
<ResizeObserver
|
|
35
39
|
onResize={({ height, width }) => {
|
|
36
|
-
editorRef.current?.
|
|
40
|
+
editorRef.current?.layout({
|
|
37
41
|
height,
|
|
38
42
|
width,
|
|
39
43
|
});
|
|
40
44
|
}}
|
|
41
45
|
/>
|
|
42
46
|
<ReactMonacoEditor
|
|
43
|
-
|
|
44
|
-
editorWillMount={editorWillMount}
|
|
47
|
+
onMount={editorWillMount}
|
|
45
48
|
onChange={onChange}
|
|
46
49
|
options={{ minimap: { enabled: false } }}
|
|
47
|
-
theme="dark"
|
|
50
|
+
theme="vs-dark"
|
|
48
51
|
value={value}
|
|
49
52
|
language={language}
|
|
50
53
|
/>
|
package/src/consts.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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
|
|
4
|
+
'': 'Select a language',
|
|
5
5
|
cpp: 'C++',
|
|
6
6
|
csharp: 'C#',
|
|
7
7
|
golang: 'Go',
|
|
@@ -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,7 +6,6 @@ import {
|
|
|
6
6
|
ToolTip,
|
|
7
7
|
} from '@codecademy/gamut';
|
|
8
8
|
import { CopyIcon } from '@codecademy/gamut-icons';
|
|
9
|
-
import { colors } from '@codecademy/gamut-styles';
|
|
10
9
|
import styled from '@emotion/styled';
|
|
11
10
|
import React, { useState } from 'react';
|
|
12
11
|
|
|
@@ -25,7 +24,7 @@ const Output = styled.pre<{ hasError: boolean }>`
|
|
|
25
24
|
overflow: auto;
|
|
26
25
|
${({ hasError, theme }) => `
|
|
27
26
|
color: ${hasError ? theme.colors.orange : theme.colors.white};
|
|
28
|
-
background-color: ${colors['
|
|
27
|
+
background-color: ${theme.colors['navy-900']};
|
|
29
28
|
`}
|
|
30
29
|
`;
|
|
31
30
|
|
|
@@ -39,10 +38,7 @@ type EditorProps = {
|
|
|
39
38
|
hideCopyButton: boolean;
|
|
40
39
|
language: languageOption;
|
|
41
40
|
text: string;
|
|
42
|
-
|
|
43
|
-
onChange: (
|
|
44
|
-
text: string
|
|
45
|
-
) => void /* TODO: Add onChange behavior in DISC-353 */;
|
|
41
|
+
onChange: (text: string) => void;
|
|
46
42
|
onCopy?: (text: string, language: string) => void;
|
|
47
43
|
snippetsBaseUrl?: string;
|
|
48
44
|
};
|
package/src/index.tsx
CHANGED
|
@@ -5,17 +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 CodebytesChangeHandler = (text: string, language: languageOption) => void;
|
|
10
13
|
|
|
11
14
|
export interface CodeByteEditorProps {
|
|
12
15
|
text: string;
|
|
13
16
|
language: languageOption;
|
|
14
17
|
hideCopyButton: boolean;
|
|
15
|
-
onCopy?:
|
|
18
|
+
onCopy?: CodebytesChangeHandler;
|
|
16
19
|
isIFrame?: boolean;
|
|
17
|
-
snippetsBaseUrl?: string
|
|
18
|
-
|
|
20
|
+
snippetsBaseUrl?: string;
|
|
21
|
+
onEdit?: CodebytesChangeHandler;
|
|
22
|
+
onLanguageChange?: CodebytesChangeHandler;
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
const editorStates = states({
|
|
@@ -42,14 +46,15 @@ const EditorContainer = styled(Background)<EditorStyleProps>(
|
|
|
42
46
|
|
|
43
47
|
export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
44
48
|
text: initialText,
|
|
45
|
-
language,
|
|
49
|
+
language: initialLanguage,
|
|
46
50
|
hideCopyButton,
|
|
47
|
-
onCopy,
|
|
48
51
|
isIFrame = false,
|
|
49
|
-
snippetsBaseUrl =
|
|
50
|
-
|
|
52
|
+
snippetsBaseUrl = process.env.CONTAINER_API_BASE,
|
|
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,27 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
62
67
|
aria-label="visit codecademy.com"
|
|
63
68
|
/>
|
|
64
69
|
</Box>
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
70
|
+
{language ? (
|
|
71
|
+
<Editor
|
|
72
|
+
language={language}
|
|
73
|
+
text={text}
|
|
74
|
+
hideCopyButton={hideCopyButton}
|
|
75
|
+
onChange={(newText: string) => {
|
|
76
|
+
setText(newText);
|
|
77
|
+
onEdit?.(newText, language);
|
|
78
|
+
}}
|
|
79
|
+
snippetsBaseUrl={snippetsBaseUrl}
|
|
80
|
+
/>
|
|
81
|
+
) : (
|
|
82
|
+
<LanguageSelection
|
|
83
|
+
onChange={(newLanguage) => {
|
|
84
|
+
const newText: string = text || helloWorld[newLanguage] || '';
|
|
85
|
+
setLanguage(newLanguage);
|
|
86
|
+
setText(newText);
|
|
87
|
+
onLanguageChange?.(newText, newLanguage);
|
|
88
|
+
}}
|
|
89
|
+
/>
|
|
90
|
+
)}
|
|
76
91
|
</EditorContainer>
|
|
77
92
|
);
|
|
78
93
|
};
|
|
@@ -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 a language"
|
|
40
|
+
options={languageOptions}
|
|
41
|
+
onChange={(e) => onChange(e.target.value as languageOption)}
|
|
42
|
+
/>
|
|
43
|
+
</Background>
|
|
44
|
+
);
|
|
45
|
+
};
|