@codecademy/codebytes 0.2.1-alpha.9c7a19.0 → 0.2.1-alpha.f45f38.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 +1 -1
- package/package.json +4 -4
- package/src/MonacoEditor/index.tsx +14 -9
- package/src/consts.ts +0 -49
- package/src/editor.tsx +4 -7
- package/src/index.tsx +15 -37
- package/src/languageSelection.tsx +0 -46
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.2.1-alpha.
|
|
6
|
+
### [0.2.1-alpha.f45f38.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.2.0...@codecademy/codebytes@0.2.1-alpha.f45f38.0) (2022-01-11)
|
|
7
7
|
|
|
8
8
|
**Note:** Version bump only for package @codecademy/codebytes
|
|
9
9
|
|
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.1-alpha.
|
|
4
|
+
"version": "0.2.1-alpha.f45f38.0",
|
|
5
5
|
"author": "Codecademy Engineering <dev@codecademy.com>",
|
|
6
6
|
"sideEffects": [
|
|
7
7
|
"**/*.css",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"@emotion/react": "^11.4.0",
|
|
28
28
|
"@emotion/styled": "^11.3.0",
|
|
29
29
|
"@loadable/component": "^5.13.1",
|
|
30
|
-
"monaco-editor": "
|
|
31
|
-
"
|
|
30
|
+
"@monaco-editor/react": "4.3.1",
|
|
31
|
+
"monaco-editor": ">= 0.25.0 < 1",
|
|
32
32
|
"react-resize-observer": "1.1.1"
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
|
@@ -50,5 +50,5 @@
|
|
|
50
50
|
"publishConfig": {
|
|
51
51
|
"access": "public"
|
|
52
52
|
},
|
|
53
|
-
"gitHead": "
|
|
53
|
+
"gitHead": "783fed789d9754d46535c05f38b2a8a8185aa298"
|
|
54
54
|
}
|
|
@@ -4,14 +4,14 @@
|
|
|
4
4
|
// Monaco as a shared package RFC https://www.notion.so/codecademy/Monaco-editor-as-a-shared-package-1f4484db165b4abc8394c3556451ef6a
|
|
5
5
|
|
|
6
6
|
import loadable from '@loadable/component';
|
|
7
|
+
import { OnMount } from '@monaco-editor/react';
|
|
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('
|
|
14
|
+
const ReactMonacoEditor = loadable(() => import('@monaco-editor/react'));
|
|
15
15
|
|
|
16
16
|
export type SimpleMonacoEditorProps = {
|
|
17
17
|
value: string;
|
|
@@ -19,16 +19,22 @@ export type SimpleMonacoEditorProps = {
|
|
|
19
19
|
onChange?: (value: string) => void;
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
+
type ThemedEditor = Parameters<OnMount>[0];
|
|
23
|
+
|
|
22
24
|
export const SimpleMonacoEditor: React.FC<SimpleMonacoEditorProps> = ({
|
|
23
25
|
value,
|
|
24
26
|
language,
|
|
25
27
|
onChange,
|
|
26
28
|
}) => {
|
|
27
|
-
const editorRef = useRef<
|
|
28
|
-
const editorWillMount = useCallback(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
const editorRef = useRef<any>(null);
|
|
30
|
+
const editorWillMount = useCallback(
|
|
31
|
+
(editor: ThemedEditor, monaco: Monaco) => {
|
|
32
|
+
editorRef.current = editor;
|
|
33
|
+
monaco.editor.defineTheme('dark', dark);
|
|
34
|
+
monaco.editor.setTheme('dark');
|
|
35
|
+
},
|
|
36
|
+
[]
|
|
37
|
+
);
|
|
32
38
|
return (
|
|
33
39
|
<>
|
|
34
40
|
<ResizeObserver
|
|
@@ -40,8 +46,7 @@ export const SimpleMonacoEditor: React.FC<SimpleMonacoEditorProps> = ({
|
|
|
40
46
|
}}
|
|
41
47
|
/>
|
|
42
48
|
<ReactMonacoEditor
|
|
43
|
-
|
|
44
|
-
editorWillMount={editorWillMount}
|
|
49
|
+
onMount={editorWillMount}
|
|
45
50
|
onChange={onChange}
|
|
46
51
|
options={{ minimap: { enabled: false } }}
|
|
47
52
|
theme="dark"
|
package/src/consts.ts
CHANGED
|
@@ -13,52 +13,3 @@ 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,7 @@ import {
|
|
|
6
6
|
ToolTip,
|
|
7
7
|
} from '@codecademy/gamut';
|
|
8
8
|
import { CopyIcon } from '@codecademy/gamut-icons';
|
|
9
|
-
import {
|
|
9
|
+
import { colors } from '@codecademy/gamut-styles';
|
|
10
10
|
import styled from '@emotion/styled';
|
|
11
11
|
import React, { useState } from 'react';
|
|
12
12
|
|
|
@@ -23,9 +23,9 @@ const Output = styled.pre<{ hasError: boolean }>`
|
|
|
23
23
|
font-family: Monaco;
|
|
24
24
|
font-size: 0.875rem;
|
|
25
25
|
overflow: auto;
|
|
26
|
-
${({ hasError }) => `
|
|
26
|
+
${({ hasError, theme }) => `
|
|
27
27
|
color: ${hasError ? theme.colors.orange : theme.colors.white};
|
|
28
|
-
background-color: ${
|
|
28
|
+
background-color: ${colors['gray-900']};
|
|
29
29
|
`}
|
|
30
30
|
`;
|
|
31
31
|
|
|
@@ -39,10 +39,7 @@ type EditorProps = {
|
|
|
39
39
|
hideCopyButton: boolean;
|
|
40
40
|
language: languageOption;
|
|
41
41
|
text: string;
|
|
42
|
-
|
|
43
|
-
onChange: (
|
|
44
|
-
text: string
|
|
45
|
-
) => void /* TODO: Add onChange behavior in DISC-353 */;
|
|
42
|
+
onChange: (text: string) => void;
|
|
46
43
|
onCopy?: (text: string, language: string) => void;
|
|
47
44
|
snippetsBaseUrl?: string;
|
|
48
45
|
};
|
package/src/index.tsx
CHANGED
|
@@ -5,11 +5,9 @@ import { StyleProps } from '@codecademy/variance';
|
|
|
5
5
|
import styled from '@emotion/styled';
|
|
6
6
|
import React, { useState } from 'react';
|
|
7
7
|
|
|
8
|
-
import {
|
|
8
|
+
import { languageOption } from './consts';
|
|
9
9
|
import { Editor } from './editor';
|
|
10
|
-
import { LanguageSelection } from './languageSelection';
|
|
11
10
|
|
|
12
|
-
type ChangeHandler = (text: string, language: languageOption) => void;
|
|
13
11
|
export interface CodeByteEditorProps {
|
|
14
12
|
text: string;
|
|
15
13
|
language: languageOption;
|
|
@@ -17,8 +15,7 @@ export interface CodeByteEditorProps {
|
|
|
17
15
|
onCopy?: (text: string, language: string) => void;
|
|
18
16
|
isIFrame?: boolean;
|
|
19
17
|
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
|
-
|
|
21
|
-
onLanguageChange?: ChangeHandler;
|
|
18
|
+
onTextChange?: (text: string) => void;
|
|
22
19
|
}
|
|
23
20
|
|
|
24
21
|
const editorStates = states({
|
|
@@ -45,16 +42,14 @@ const EditorContainer = styled(Background)<EditorStyleProps>(
|
|
|
45
42
|
|
|
46
43
|
export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
47
44
|
text: initialText,
|
|
48
|
-
language
|
|
45
|
+
language,
|
|
49
46
|
hideCopyButton,
|
|
50
47
|
onCopy,
|
|
51
48
|
isIFrame = false,
|
|
52
49
|
snippetsBaseUrl = '',
|
|
53
|
-
|
|
54
|
-
onLanguageChange,
|
|
50
|
+
onTextChange,
|
|
55
51
|
}) => {
|
|
56
52
|
const [text, setText] = useState<string>(initialText);
|
|
57
|
-
const [language, setLanguage] = useState<languageOption>(initialLanguage);
|
|
58
53
|
return (
|
|
59
54
|
<EditorContainer bg="black" as="main" isIFrame={isIFrame}>
|
|
60
55
|
<Box borderBottom={1} borderColor="gray-900" py={4} pl={8}>
|
|
@@ -67,34 +62,17 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
67
62
|
aria-label="visit codecademy.com"
|
|
68
63
|
/>
|
|
69
64
|
</Box>
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
-
)}
|
|
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
|
+
/>
|
|
98
76
|
</EditorContainer>
|
|
99
77
|
);
|
|
100
78
|
};
|
|
@@ -1,46 +0,0 @@
|
|
|
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
|
-
};
|