@codecademy/codebytes 0.2.1-alpha.cfe771.0 → 0.2.1-alpha.ea0d6d.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/jest.config.js +1 -0
- package/package.json +7 -6
- package/src/MonacoEditor/index.tsx +16 -13
- package/src/__tests__/codebyte-test.tsx +71 -0
- package/src/__tests__/editor-test.tsx +59 -0
- package/src/__tests__/language-selection-test.tsx +14 -0
- package/src/consts.ts +50 -1
- package/src/editor.tsx +16 -15
- package/src/index.tsx +43 -29
- package/src/languageSelection.tsx +26 -0
- package/src/types.ts +20 -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.2.1-alpha.
|
|
6
|
+
### [0.2.1-alpha.ea0d6d.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.3.0...@codecademy/codebytes@0.2.1-alpha.ea0d6d.0) (2022-01-14)
|
|
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/jest.config.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../../jest.config.base')('codebytes');
|
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.ea0d6d.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": {
|
|
@@ -41,14 +40,16 @@
|
|
|
41
40
|
},
|
|
42
41
|
"license": "MIT",
|
|
43
42
|
"devDependencies": {
|
|
43
|
+
"@codecademy/gamut-tests": "*",
|
|
44
44
|
"@emotion/jest": "^11.3.0",
|
|
45
45
|
"@testing-library/dom": "^7.31.2",
|
|
46
46
|
"@testing-library/react": "^11.0.4",
|
|
47
|
-
"@
|
|
47
|
+
"@testing-library/react-hooks": "3.2.1",
|
|
48
|
+
"@testing-library/user-event": "13.1.1",
|
|
48
49
|
"monaco-editor-webpack-plugin": "1.9.1"
|
|
49
50
|
},
|
|
50
51
|
"publishConfig": {
|
|
51
52
|
"access": "public"
|
|
52
53
|
},
|
|
53
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "0c38f7a667d0ab4f0e11a9a802d03734a932c0ae"
|
|
54
55
|
}
|
|
@@ -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
|
/>
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { setupRtl } from '@codecademy/gamut-tests';
|
|
2
|
+
import userEvent from '@testing-library/user-event';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
|
|
5
|
+
import { CodeByteEditor } from '..';
|
|
6
|
+
import { helloWorld, validLanguages } from '../consts';
|
|
7
|
+
|
|
8
|
+
const mockEditorTestId = 'mock-editor-test-id';
|
|
9
|
+
|
|
10
|
+
jest.mock('react-resize-observer');
|
|
11
|
+
// This is a super simplified mock capable of render value and trigger onChange.
|
|
12
|
+
jest.mock('../MonacoEditor', () => ({
|
|
13
|
+
SimpleMonacoEditor: ({
|
|
14
|
+
value,
|
|
15
|
+
onChange,
|
|
16
|
+
}: {
|
|
17
|
+
value: string;
|
|
18
|
+
onChange?: (value: string) => void;
|
|
19
|
+
}) => (
|
|
20
|
+
<>
|
|
21
|
+
{value}
|
|
22
|
+
<input
|
|
23
|
+
data-testid={mockEditorTestId}
|
|
24
|
+
type="text"
|
|
25
|
+
onChange={(e) => {
|
|
26
|
+
onChange?.(e.target.value);
|
|
27
|
+
}}
|
|
28
|
+
value={value}
|
|
29
|
+
/>
|
|
30
|
+
</>
|
|
31
|
+
),
|
|
32
|
+
}));
|
|
33
|
+
|
|
34
|
+
const renderWrapper = setupRtl(CodeByteEditor, {});
|
|
35
|
+
|
|
36
|
+
describe('CodeBytes', () => {
|
|
37
|
+
it('has a language-specific "hello world" program defined for each language', () => {
|
|
38
|
+
validLanguages.forEach((language) => {
|
|
39
|
+
expect(helloWorld[language]).toBeDefined();
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('initializes with a language-specific "hello world" program when there is no language prop', () => {
|
|
44
|
+
const { view } = renderWrapper();
|
|
45
|
+
const selectedLanguage = view.getByRole('combobox') as Element;
|
|
46
|
+
userEvent.selectOptions(selectedLanguage, ['javascript']);
|
|
47
|
+
view.getByText(helloWorld.javascript);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('initializes with a language-specific "hello world" program when there is a language prop but no text prop', () => {
|
|
51
|
+
const { view } = renderWrapper({ language: 'javascript' });
|
|
52
|
+
view.getByText(helloWorld.javascript);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('initializes with deserialized text when there is a text prop but no language prop', () => {
|
|
56
|
+
const testString = 'yes hello';
|
|
57
|
+
const { view } = renderWrapper({ text: testString });
|
|
58
|
+
const selectedLanguage = view.getByRole('combobox') as Element;
|
|
59
|
+
userEvent.selectOptions(selectedLanguage, ['javascript']);
|
|
60
|
+
view.getByText(testString);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('initializes with deserialized text when there is both a language and text prop', () => {
|
|
64
|
+
const testString = 'yes hello';
|
|
65
|
+
const { view } = renderWrapper({
|
|
66
|
+
text: testString,
|
|
67
|
+
language: 'javascript',
|
|
68
|
+
});
|
|
69
|
+
view.getByText(testString);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { setupRtl } from '@codecademy/gamut-tests';
|
|
2
|
+
import userEvent from '@testing-library/user-event';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
|
|
5
|
+
import { Editor } from '../editor';
|
|
6
|
+
|
|
7
|
+
jest.mock('../MonacoEditor', () => ({
|
|
8
|
+
SimpleMonacoEditor: ({ value }: { value: string }) => <>{value}</>,
|
|
9
|
+
}));
|
|
10
|
+
|
|
11
|
+
jest.mock('react-resize-observer');
|
|
12
|
+
|
|
13
|
+
const renderWrapper = setupRtl(Editor, {
|
|
14
|
+
hideCopyButton: false,
|
|
15
|
+
language: 'javascript',
|
|
16
|
+
text: 'hello world',
|
|
17
|
+
onChange: jest.fn(),
|
|
18
|
+
snippetsBaseUrl: '',
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
Object.defineProperty(navigator, 'clipboard', {
|
|
22
|
+
value: {
|
|
23
|
+
writeText: jest.fn(),
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('Editor', () => {
|
|
28
|
+
(global as any).fetch = jest.fn();
|
|
29
|
+
afterEach(() => {
|
|
30
|
+
(global as any).fetch.mockClear();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('shows a prompt tooltip when the CodeByte has __not__ been copied via the button', () => {
|
|
34
|
+
const { view } = renderWrapper();
|
|
35
|
+
expect(view.queryByTestId('copy-confirmation-tooltip')).toBeFalsy();
|
|
36
|
+
view.getByTestId('copy-prompt-tooltip');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('shows a confirmation tooltip when the CodeByte has been copied via the button', () => {
|
|
40
|
+
const { view } = renderWrapper();
|
|
41
|
+
const copyBtn = view.getByTestId('copy-codebyte-btn');
|
|
42
|
+
userEvent.click(copyBtn as HTMLButtonElement);
|
|
43
|
+
expect(view.queryByTestId('copy-prompt-tooltip')).toBeFalsy();
|
|
44
|
+
view.getByTestId('copy-confirmation-tooltip');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('hides the copy codebyte button if hideCopyButton prop is true"', () => {
|
|
48
|
+
const { view } = renderWrapper({
|
|
49
|
+
hideCopyButton: true,
|
|
50
|
+
});
|
|
51
|
+
expect(view.queryByTestId('copy-codebyte-btn')).toBeNull();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('shows the copy codebyte button if hideCopyButton prop is not set', () => {
|
|
55
|
+
const { view } = renderWrapper();
|
|
56
|
+
|
|
57
|
+
view.getByTestId('copy-codebyte-btn');
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { setupRtl } from '@codecademy/gamut-tests';
|
|
2
|
+
|
|
3
|
+
import { LanguageSelection } from '../languageSelection';
|
|
4
|
+
|
|
5
|
+
const renderWrapper = setupRtl(LanguageSelection, {
|
|
6
|
+
onChange: () => null,
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
describe('LanguageSelection', () => {
|
|
10
|
+
it('has placeholder text', () => {
|
|
11
|
+
const { view } = renderWrapper();
|
|
12
|
+
view.getByText('Select your language');
|
|
13
|
+
});
|
|
14
|
+
});
|
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 Exclude<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 = {
|
|
56
|
+
cpp,
|
|
57
|
+
csharp,
|
|
58
|
+
golang,
|
|
59
|
+
javascript,
|
|
60
|
+
php,
|
|
61
|
+
python,
|
|
62
|
+
ruby,
|
|
63
|
+
scheme,
|
|
64
|
+
} as const;
|
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
|
|
|
@@ -14,6 +13,7 @@ import { postSnippet } from './api';
|
|
|
14
13
|
import type { languageOption } from './consts';
|
|
15
14
|
import { Drawers } from './drawers';
|
|
16
15
|
import { SimpleMonacoEditor } from './MonacoEditor';
|
|
16
|
+
import { CodebytesChangeHandlerMap } from './types';
|
|
17
17
|
|
|
18
18
|
const Output = styled.pre<{ hasError: boolean }>`
|
|
19
19
|
width: 100%;
|
|
@@ -25,7 +25,7 @@ const Output = styled.pre<{ hasError: boolean }>`
|
|
|
25
25
|
overflow: auto;
|
|
26
26
|
${({ hasError, theme }) => `
|
|
27
27
|
color: ${hasError ? theme.colors.orange : theme.colors.white};
|
|
28
|
-
background-color: ${colors['
|
|
28
|
+
background-color: ${theme.colors['navy-900']};
|
|
29
29
|
`}
|
|
30
30
|
`;
|
|
31
31
|
|
|
@@ -39,11 +39,8 @@ type EditorProps = {
|
|
|
39
39
|
hideCopyButton: boolean;
|
|
40
40
|
language: languageOption;
|
|
41
41
|
text: string;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
text: string
|
|
45
|
-
) => void /* TODO: Add onChange behavior in DISC-353 */;
|
|
46
|
-
onCopy?: (text: string, language: string) => void;
|
|
42
|
+
onChange: (text: string) => void;
|
|
43
|
+
on?: CodebytesChangeHandlerMap;
|
|
47
44
|
snippetsBaseUrl?: string;
|
|
48
45
|
};
|
|
49
46
|
|
|
@@ -51,7 +48,7 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
51
48
|
language,
|
|
52
49
|
text,
|
|
53
50
|
hideCopyButton,
|
|
54
|
-
|
|
51
|
+
on = {},
|
|
55
52
|
onChange,
|
|
56
53
|
snippetsBaseUrl,
|
|
57
54
|
}) => {
|
|
@@ -60,11 +57,9 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
60
57
|
const [isCodeByteCopied, setIsCodeByteCopied] = useState(false);
|
|
61
58
|
const onCopyClick = () => {
|
|
62
59
|
if (!isCodeByteCopied) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
.catch(() => console.error('Failed to copy'));
|
|
67
|
-
onCopy?.(text, language);
|
|
60
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
61
|
+
navigator.clipboard.writeText(text);
|
|
62
|
+
on.copy?.(text, language);
|
|
68
63
|
setIsCodeByteCopied(true);
|
|
69
64
|
}
|
|
70
65
|
};
|
|
@@ -84,6 +79,7 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
84
79
|
};
|
|
85
80
|
setStatus('waiting');
|
|
86
81
|
setOutput('');
|
|
82
|
+
on.run?.();
|
|
87
83
|
|
|
88
84
|
try {
|
|
89
85
|
const response = await postSnippet(data, snippetsBaseUrl);
|
|
@@ -134,15 +130,20 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
134
130
|
variant="secondary"
|
|
135
131
|
onClick={onCopyClick}
|
|
136
132
|
onBlur={() => setIsCodeByteCopied(false)}
|
|
133
|
+
data-testid="copy-codebyte-btn"
|
|
137
134
|
>
|
|
138
135
|
<CopyIconStyled aria-hidden="true" /> Copy Codebyte
|
|
139
136
|
</TextButton>
|
|
140
137
|
}
|
|
141
138
|
>
|
|
142
139
|
{isCodeByteCopied ? (
|
|
143
|
-
<span role="alert">
|
|
140
|
+
<span data-testid="copy-confirmation-tooltip" role="alert">
|
|
141
|
+
Copied!
|
|
142
|
+
</span>
|
|
144
143
|
) : (
|
|
145
|
-
<span
|
|
144
|
+
<span data-testid="copy-prompt-tooltip">
|
|
145
|
+
Copy to your clipboard
|
|
146
|
+
</span>
|
|
146
147
|
)}
|
|
147
148
|
</ToolTip>
|
|
148
149
|
) : null}
|
package/src/index.tsx
CHANGED
|
@@ -3,20 +3,12 @@ import { FaviconIcon } from '@codecademy/gamut-icons';
|
|
|
3
3
|
import { Background, states, system } from '@codecademy/gamut-styles';
|
|
4
4
|
import { StyleProps } from '@codecademy/variance';
|
|
5
5
|
import styled from '@emotion/styled';
|
|
6
|
-
import React, { useState } from 'react';
|
|
6
|
+
import React, { useEffect, useState } from 'react';
|
|
7
7
|
|
|
8
|
-
import { languageOption } from './consts';
|
|
8
|
+
import { helloWorld, languageOption } from './consts';
|
|
9
9
|
import { Editor } from './editor';
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
text: string;
|
|
13
|
-
language: languageOption;
|
|
14
|
-
hideCopyButton: boolean;
|
|
15
|
-
onCopy?: (text: string, language: string) => void;
|
|
16
|
-
isIFrame?: boolean;
|
|
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. */;
|
|
18
|
-
onTextChange?: (text: string) => void;
|
|
19
|
-
}
|
|
10
|
+
import { LanguageSelection } from './languageSelection';
|
|
11
|
+
import { CodeByteEditorProps } from './types';
|
|
20
12
|
|
|
21
13
|
const editorStates = states({
|
|
22
14
|
isIFrame: { height: '100vh' },
|
|
@@ -42,14 +34,23 @@ const EditorContainer = styled(Background)<EditorStyleProps>(
|
|
|
42
34
|
|
|
43
35
|
export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
44
36
|
text: initialText,
|
|
45
|
-
language,
|
|
46
|
-
hideCopyButton,
|
|
47
|
-
onCopy,
|
|
37
|
+
language: initialLanguage,
|
|
38
|
+
hideCopyButton = false,
|
|
48
39
|
isIFrame = false,
|
|
49
|
-
|
|
50
|
-
|
|
40
|
+
on = {},
|
|
41
|
+
snippetsBaseUrl = process.env.CONTAINER_API_BASE,
|
|
51
42
|
}) => {
|
|
52
|
-
const [text, setText] = useState<string>(initialText);
|
|
43
|
+
const [text, setText] = useState<string>(initialText ?? '');
|
|
44
|
+
const [language, setLanguage] = useState<languageOption>(
|
|
45
|
+
initialLanguage ?? ''
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (language) {
|
|
50
|
+
setText(initialText ?? (helloWorld[language] || ''));
|
|
51
|
+
}
|
|
52
|
+
}, [language, initialText]);
|
|
53
|
+
|
|
53
54
|
return (
|
|
54
55
|
<EditorContainer bg="black" as="main" isIFrame={isIFrame}>
|
|
55
56
|
<Box borderBottom={1} borderColor="gray-900" py={4} pl={8}>
|
|
@@ -60,19 +61,32 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
60
61
|
target="_blank"
|
|
61
62
|
rel="noreferrer"
|
|
62
63
|
aria-label="visit codecademy.com"
|
|
64
|
+
onClick={() => on.logoClick?.()}
|
|
63
65
|
/>
|
|
64
66
|
</Box>
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
67
|
+
{language ? (
|
|
68
|
+
<Editor
|
|
69
|
+
language={language}
|
|
70
|
+
text={text}
|
|
71
|
+
hideCopyButton={hideCopyButton}
|
|
72
|
+
onChange={(newText: string) => {
|
|
73
|
+
setText(newText);
|
|
74
|
+
on.edit?.(newText, language);
|
|
75
|
+
}}
|
|
76
|
+
on={on}
|
|
77
|
+
snippetsBaseUrl={snippetsBaseUrl}
|
|
78
|
+
/>
|
|
79
|
+
) : (
|
|
80
|
+
<LanguageSelection
|
|
81
|
+
onChange={(newLanguage) => {
|
|
82
|
+
const newText: string =
|
|
83
|
+
text || (newLanguage ? helloWorld[newLanguage] : '');
|
|
84
|
+
setLanguage(newLanguage);
|
|
85
|
+
setText(newText);
|
|
86
|
+
on.languageChange?.(newText, newLanguage);
|
|
87
|
+
}}
|
|
88
|
+
/>
|
|
89
|
+
)}
|
|
76
90
|
</EditorContainer>
|
|
77
91
|
);
|
|
78
92
|
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Select, Text } from '@codecademy/gamut';
|
|
2
|
+
import { ColorMode } from '@codecademy/gamut-styles';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
|
|
5
|
+
import type { languageOption } from './consts';
|
|
6
|
+
import { languageOptions } from './consts';
|
|
7
|
+
|
|
8
|
+
export type LanguageSelectionProps = {
|
|
9
|
+
onChange: (newLanguage: languageOption) => void;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const LanguageSelection: React.FC<LanguageSelectionProps> = ({
|
|
13
|
+
onChange,
|
|
14
|
+
}) => {
|
|
15
|
+
return (
|
|
16
|
+
<ColorMode mode="dark" flex={1} px={16} pt={48}>
|
|
17
|
+
<Text mb={16}>Which language do you want to code in?</Text>
|
|
18
|
+
<Select
|
|
19
|
+
id="language-select"
|
|
20
|
+
aria-label="Select a language"
|
|
21
|
+
options={languageOptions}
|
|
22
|
+
onChange={(e) => onChange(e.target.value as languageOption)}
|
|
23
|
+
/>
|
|
24
|
+
</ColorMode>
|
|
25
|
+
);
|
|
26
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { languageOption } from './consts';
|
|
2
|
+
|
|
3
|
+
type CodebytesChangeHandler = (text: string, language: languageOption) => void;
|
|
4
|
+
|
|
5
|
+
export type CodebytesChangeHandlerMap = {
|
|
6
|
+
logoClick?: () => void;
|
|
7
|
+
edit?: CodebytesChangeHandler;
|
|
8
|
+
languageChange?: CodebytesChangeHandler;
|
|
9
|
+
copy?: CodebytesChangeHandler;
|
|
10
|
+
run?: () => void;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export interface CodeByteEditorProps {
|
|
14
|
+
text?: string;
|
|
15
|
+
language?: languageOption;
|
|
16
|
+
hideCopyButton?: boolean;
|
|
17
|
+
isIFrame?: boolean;
|
|
18
|
+
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. */;
|
|
19
|
+
on?: CodebytesChangeHandlerMap;
|
|
20
|
+
}
|