@codecademy/codebytes 0.2.1-alpha.547196.0 → 0.2.1-alpha.62069b.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/jest.config.js +1 -1
- package/package.json +4 -6
- package/src/__tests__/codebyte-test.tsx +2 -2
- package/src/__tests__/editor-test.tsx +59 -0
- package/src/__tests__/language-selection-test.tsx +14 -0
- package/src/consts.ts +4 -4
- package/src/editor.tsx +14 -12
- package/src/index.tsx +7 -24
- 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.62069b.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.3.0...@codecademy/codebytes@0.2.1-alpha.62069b.0) (2022-01-14)
|
|
7
7
|
|
|
8
8
|
**Note:** Version bump only for package @codecademy/codebytes
|
|
9
9
|
|
package/jest.config.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
module.exports = require('../../jest.config.base')('codebytes');
|
|
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.62069b.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": {
|
|
@@ -47,11 +46,10 @@
|
|
|
47
46
|
"@testing-library/react": "^11.0.4",
|
|
48
47
|
"@testing-library/react-hooks": "3.2.1",
|
|
49
48
|
"@testing-library/user-event": "13.1.1",
|
|
50
|
-
"@types/loadable__component": "^5.13.2",
|
|
51
49
|
"monaco-editor-webpack-plugin": "1.9.1"
|
|
52
50
|
},
|
|
53
51
|
"publishConfig": {
|
|
54
52
|
"access": "public"
|
|
55
53
|
},
|
|
56
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "644608025776c10ef5db464c2cbc8e2c00da48f2"
|
|
57
55
|
}
|
|
@@ -44,12 +44,12 @@ describe('CodeBytes', () => {
|
|
|
44
44
|
const { view } = renderWrapper();
|
|
45
45
|
const selectedLanguage = view.getByRole('combobox') as Element;
|
|
46
46
|
userEvent.selectOptions(selectedLanguage, ['javascript']);
|
|
47
|
-
view.getByText(helloWorld.javascript
|
|
47
|
+
view.getByText(helloWorld.javascript);
|
|
48
48
|
});
|
|
49
49
|
|
|
50
50
|
it('initializes with a language-specific "hello world" program when there is a language prop but no text prop', () => {
|
|
51
51
|
const { view } = renderWrapper({ language: 'javascript' });
|
|
52
|
-
view.getByText(helloWorld.javascript
|
|
52
|
+
view.getByText(helloWorld.javascript);
|
|
53
53
|
});
|
|
54
54
|
|
|
55
55
|
it('initializes with deserialized text when there is a text prop but no language prop', () => {
|
|
@@ -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',
|
|
@@ -16,7 +16,7 @@ export type languageOption = keyof typeof languageOptions;
|
|
|
16
16
|
|
|
17
17
|
export const validLanguages = Object.keys(languageOptions).filter(
|
|
18
18
|
(option) => !!option
|
|
19
|
-
) as languageOption[];
|
|
19
|
+
) as Exclude<languageOption, ''>[];
|
|
20
20
|
|
|
21
21
|
const cpp = `#include <iostream>
|
|
22
22
|
int main() {
|
|
@@ -52,7 +52,7 @@ const scheme = `(begin
|
|
|
52
52
|
(display "Hello world!")
|
|
53
53
|
(newline))`;
|
|
54
54
|
|
|
55
|
-
export const helloWorld
|
|
55
|
+
export const helloWorld = {
|
|
56
56
|
cpp,
|
|
57
57
|
csharp,
|
|
58
58
|
golang,
|
|
@@ -61,4 +61,4 @@ export const helloWorld: { [key in languageOption]?: string } = {
|
|
|
61
61
|
python,
|
|
62
62
|
ruby,
|
|
63
63
|
scheme,
|
|
64
|
-
};
|
|
64
|
+
} as const;
|
package/src/editor.tsx
CHANGED
|
@@ -9,11 +9,11 @@ import { CopyIcon } from '@codecademy/gamut-icons';
|
|
|
9
9
|
import styled from '@emotion/styled';
|
|
10
10
|
import React, { useState } from 'react';
|
|
11
11
|
|
|
12
|
-
import { CodebytesChangeHandlerMap } from '.';
|
|
13
12
|
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%;
|
|
@@ -39,9 +39,8 @@ type EditorProps = {
|
|
|
39
39
|
hideCopyButton: boolean;
|
|
40
40
|
language: languageOption;
|
|
41
41
|
text: string;
|
|
42
|
-
|
|
43
42
|
onChange: (text: string) => void;
|
|
44
|
-
on?:
|
|
43
|
+
on?: CodebytesChangeHandlerMap;
|
|
45
44
|
snippetsBaseUrl?: string;
|
|
46
45
|
};
|
|
47
46
|
|
|
@@ -49,7 +48,7 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
49
48
|
language,
|
|
50
49
|
text,
|
|
51
50
|
hideCopyButton,
|
|
52
|
-
on,
|
|
51
|
+
on = {},
|
|
53
52
|
onChange,
|
|
54
53
|
snippetsBaseUrl,
|
|
55
54
|
}) => {
|
|
@@ -58,11 +57,9 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
58
57
|
const [isCodeByteCopied, setIsCodeByteCopied] = useState(false);
|
|
59
58
|
const onCopyClick = () => {
|
|
60
59
|
if (!isCodeByteCopied) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
.catch(() => console.error('Failed to copy'));
|
|
65
|
-
on?.copy?.(text, language);
|
|
60
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
61
|
+
navigator.clipboard.writeText(text);
|
|
62
|
+
on.copy?.(text, language);
|
|
66
63
|
setIsCodeByteCopied(true);
|
|
67
64
|
}
|
|
68
65
|
};
|
|
@@ -82,7 +79,7 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
82
79
|
};
|
|
83
80
|
setStatus('waiting');
|
|
84
81
|
setOutput('');
|
|
85
|
-
on
|
|
82
|
+
on.run?.();
|
|
86
83
|
|
|
87
84
|
try {
|
|
88
85
|
const response = await postSnippet(data, snippetsBaseUrl);
|
|
@@ -133,15 +130,20 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
133
130
|
variant="secondary"
|
|
134
131
|
onClick={onCopyClick}
|
|
135
132
|
onBlur={() => setIsCodeByteCopied(false)}
|
|
133
|
+
data-testid="copy-codebyte-btn"
|
|
136
134
|
>
|
|
137
135
|
<CopyIconStyled aria-hidden="true" /> Copy Codebyte
|
|
138
136
|
</TextButton>
|
|
139
137
|
}
|
|
140
138
|
>
|
|
141
139
|
{isCodeByteCopied ? (
|
|
142
|
-
<span role="alert">
|
|
140
|
+
<span data-testid="copy-confirmation-tooltip" role="alert">
|
|
141
|
+
Copied!
|
|
142
|
+
</span>
|
|
143
143
|
) : (
|
|
144
|
-
<span
|
|
144
|
+
<span data-testid="copy-prompt-tooltip">
|
|
145
|
+
Copy to your clipboard
|
|
146
|
+
</span>
|
|
145
147
|
)}
|
|
146
148
|
</ToolTip>
|
|
147
149
|
) : null}
|
package/src/index.tsx
CHANGED
|
@@ -8,24 +8,7 @@ import React, { useEffect, useState } from 'react';
|
|
|
8
8
|
import { helloWorld, languageOption } from './consts';
|
|
9
9
|
import { Editor } from './editor';
|
|
10
10
|
import { LanguageSelection } from './languageSelection';
|
|
11
|
-
|
|
12
|
-
type CodebytesChangeHandler = (text: string, language: languageOption) => void;
|
|
13
|
-
export type CodebytesChangeHandlerMap = {
|
|
14
|
-
logoClick?: () => void;
|
|
15
|
-
edit?: CodebytesChangeHandler;
|
|
16
|
-
languageChange?: CodebytesChangeHandler;
|
|
17
|
-
copy?: CodebytesChangeHandler;
|
|
18
|
-
run?: () => void;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export interface CodeByteEditorProps {
|
|
22
|
-
text?: string;
|
|
23
|
-
language?: languageOption;
|
|
24
|
-
hideCopyButton?: boolean;
|
|
25
|
-
isIFrame?: boolean;
|
|
26
|
-
snippetsBaseUrl?: string;
|
|
27
|
-
on?: CodebytesChangeHandlerMap;
|
|
28
|
-
}
|
|
11
|
+
import { CodeByteEditorProps } from './types';
|
|
29
12
|
|
|
30
13
|
const editorStates = states({
|
|
31
14
|
isIFrame: { height: '100vh' },
|
|
@@ -54,7 +37,7 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
54
37
|
language: initialLanguage,
|
|
55
38
|
hideCopyButton = false,
|
|
56
39
|
isIFrame = false,
|
|
57
|
-
on,
|
|
40
|
+
on = {},
|
|
58
41
|
snippetsBaseUrl = process.env.CONTAINER_API_BASE,
|
|
59
42
|
}) => {
|
|
60
43
|
const [text, setText] = useState<string>(initialText ?? '');
|
|
@@ -62,7 +45,6 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
62
45
|
initialLanguage ?? ''
|
|
63
46
|
);
|
|
64
47
|
|
|
65
|
-
/* Resetting the state when the props change. If the language changes and the text is not provided output helloworld */
|
|
66
48
|
useEffect(() => {
|
|
67
49
|
if (language) {
|
|
68
50
|
setText(initialText ?? (helloWorld[language] || ''));
|
|
@@ -79,7 +61,7 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
79
61
|
target="_blank"
|
|
80
62
|
rel="noreferrer"
|
|
81
63
|
aria-label="visit codecademy.com"
|
|
82
|
-
onClick={() => on
|
|
64
|
+
onClick={() => on.logoClick?.()}
|
|
83
65
|
/>
|
|
84
66
|
</Box>
|
|
85
67
|
{language ? (
|
|
@@ -89,7 +71,7 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
89
71
|
hideCopyButton={hideCopyButton}
|
|
90
72
|
onChange={(newText: string) => {
|
|
91
73
|
setText(newText);
|
|
92
|
-
on
|
|
74
|
+
on.edit?.(newText, language);
|
|
93
75
|
}}
|
|
94
76
|
on={on}
|
|
95
77
|
snippetsBaseUrl={snippetsBaseUrl}
|
|
@@ -97,10 +79,11 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
97
79
|
) : (
|
|
98
80
|
<LanguageSelection
|
|
99
81
|
onChange={(newLanguage) => {
|
|
100
|
-
const newText: string =
|
|
82
|
+
const newText: string =
|
|
83
|
+
text || (newLanguage ? helloWorld[newLanguage] : '');
|
|
101
84
|
setLanguage(newLanguage);
|
|
102
85
|
setText(newText);
|
|
103
|
-
on
|
|
86
|
+
on.languageChange?.(newText, newLanguage);
|
|
104
87
|
}}
|
|
105
88
|
/>
|
|
106
89
|
)}
|
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
|
+
}
|