@codecademy/codebytes 1.0.4-alpha.41a141e2a.0 → 1.0.4-alpha.4641cabd2.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/dist/MonacoEditor/colorsDark.d.ts +32 -0
- package/{src/MonacoEditor/colorsDark.ts → dist/MonacoEditor/colorsDark.js} +8 -16
- package/dist/MonacoEditor/index.d.ts +8 -0
- package/dist/MonacoEditor/index.js +42 -0
- package/dist/MonacoEditor/theme.d.ts +2 -0
- package/dist/MonacoEditor/theme.js +123 -0
- package/dist/MonacoEditor/types.d.ts +1 -0
- package/dist/MonacoEditor/types.js +1 -0
- package/dist/api.d.ts +12 -0
- package/dist/api.js +41 -0
- package/dist/codeByteEditor.d.ts +4 -0
- package/dist/codeByteEditor.js +141 -0
- package/dist/consts.d.ts +23 -0
- package/dist/consts.js +34 -0
- package/dist/drawers.d.ts +6 -0
- package/dist/drawers.js +149 -0
- package/dist/editor.d.ts +15 -0
- package/dist/editor.js +194 -0
- package/dist/helpers/index.d.ts +2 -0
- package/dist/helpers/index.js +12 -0
- package/dist/helpers/useEverInView.d.ts +5 -0
- package/dist/helpers/useEverInView.js +45 -0
- package/dist/helpers/useEverInView.test.js +63 -0
- package/dist/helpers/useIntersection.d.ts +2 -0
- package/dist/helpers/useIntersection.js +42 -0
- package/dist/helpers/useIntersection.test.js +127 -0
- package/{src/index.ts → dist/index.d.ts} +0 -0
- package/dist/index.js +3 -0
- package/dist/languageSelection.d.ts +6 -0
- package/dist/languageSelection.js +22 -0
- package/dist/libs/eventTracking.d.ts +1 -0
- package/dist/libs/eventTracking.js +11 -0
- package/{src → dist}/theme.d.ts +0 -0
- package/dist/types.d.ts +22 -0
- package/dist/types.js +1 -0
- package/package.json +8 -3
- package/.eslintrc.json +0 -35
- package/CHANGELOG.md +0 -14
- package/babel.config.js +0 -22
- package/jest.config.ts +0 -21
- package/project.json +0 -52
- package/src/MonacoEditor/index.tsx +0 -56
- package/src/MonacoEditor/theme.ts +0 -65
- package/src/MonacoEditor/types.ts +0 -1
- package/src/__tests__/codebyte.test.tsx +0 -186
- package/src/__tests__/editor.test.tsx +0 -108
- package/src/__tests__/helpers.test.tsx +0 -39
- package/src/__tests__/language-selection.test.tsx +0 -14
- package/src/api.ts +0 -28
- package/src/codeByteEditor.tsx +0 -115
- package/src/consts.ts +0 -64
- package/src/drawers.tsx +0 -133
- package/src/editor.tsx +0 -162
- package/src/helpers/index.ts +0 -8
- package/src/helpers/useEverInView.test.ts +0 -29
- package/src/helpers/useEverInView.ts +0 -28
- package/src/helpers/useIntersection.test.ts +0 -87
- package/src/helpers/useIntersection.ts +0 -35
- package/src/languageSelection.tsx +0 -26
- package/src/libs/eventTracking.ts +0 -18
- package/src/types.ts +0 -30
- package/tsconfig.json +0 -27
- package/tsconfig.spec.json +0 -21
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
import { setupRtl } from '@codecademy/gamut-tests';
|
|
2
|
-
import { act } from '@testing-library/react';
|
|
3
|
-
import userEvent from '@testing-library/user-event';
|
|
4
|
-
import React from 'react';
|
|
5
|
-
|
|
6
|
-
import { CodeByteEditor } from '..';
|
|
7
|
-
import { helloWorld, validLanguages } from '../consts';
|
|
8
|
-
import { trackClick } from '../helpers';
|
|
9
|
-
import { trackUserImpression } from '../libs/eventTracking';
|
|
10
|
-
|
|
11
|
-
const mockEditorTestId = 'mock-editor-test-id';
|
|
12
|
-
|
|
13
|
-
jest.mock('react-resize-observer');
|
|
14
|
-
jest.mock('../libs/eventTracking');
|
|
15
|
-
jest.mock('../helpers', () => ({
|
|
16
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
17
|
-
// @ts-ignore
|
|
18
|
-
...jest.requireActual('../helpers'),
|
|
19
|
-
trackClick: jest.fn(),
|
|
20
|
-
}));
|
|
21
|
-
jest.mock('../helpers/useEverInView', () => ({
|
|
22
|
-
useEverInView: () => ({
|
|
23
|
-
everInView: true,
|
|
24
|
-
ref: () => null,
|
|
25
|
-
}),
|
|
26
|
-
}));
|
|
27
|
-
|
|
28
|
-
// This is a super simplified mock capable of render value and trigger onChange.
|
|
29
|
-
jest.mock('../MonacoEditor', () => ({
|
|
30
|
-
SimpleMonacoEditor: ({
|
|
31
|
-
value,
|
|
32
|
-
onChange,
|
|
33
|
-
}: {
|
|
34
|
-
value: string;
|
|
35
|
-
onChange?: (value: string) => void;
|
|
36
|
-
}) => (
|
|
37
|
-
<>
|
|
38
|
-
{value}
|
|
39
|
-
<input
|
|
40
|
-
data-testid={mockEditorTestId}
|
|
41
|
-
type="text"
|
|
42
|
-
onChange={(e) => {
|
|
43
|
-
onChange?.(e.target.value);
|
|
44
|
-
}}
|
|
45
|
-
value={value}
|
|
46
|
-
/>
|
|
47
|
-
</>
|
|
48
|
-
),
|
|
49
|
-
}));
|
|
50
|
-
|
|
51
|
-
const renderWrapper = setupRtl(CodeByteEditor, {});
|
|
52
|
-
|
|
53
|
-
describe('CodeBytes', () => {
|
|
54
|
-
const initialUrl = window.location.href;
|
|
55
|
-
|
|
56
|
-
afterEach(() => {
|
|
57
|
-
window.history.replaceState(null, '', initialUrl);
|
|
58
|
-
(trackClick as any).mockReset();
|
|
59
|
-
(trackUserImpression as any).mockReset();
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('has a language-specific "hello world" program defined for each language', () => {
|
|
63
|
-
validLanguages.forEach((language) => {
|
|
64
|
-
expect(helloWorld[language]).toBeDefined();
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('initializes with a language-specific "hello world" program when there is no language prop', async () => {
|
|
69
|
-
const { view } = renderWrapper();
|
|
70
|
-
const selectedLanguage = view.getByRole('combobox') as Element;
|
|
71
|
-
await act(async () => {
|
|
72
|
-
userEvent.selectOptions(selectedLanguage, ['javascript']);
|
|
73
|
-
});
|
|
74
|
-
view.getByText(helloWorld.javascript);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('initializes with a language-specific "hello world" program when there is a language prop but no text prop', () => {
|
|
78
|
-
const { view } = renderWrapper({ language: 'javascript' });
|
|
79
|
-
view.getByText(helloWorld.javascript);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('initializes with deserialized text when there is a text prop but no language prop', async () => {
|
|
83
|
-
const testString = 'yes hello';
|
|
84
|
-
const { view } = renderWrapper({ text: testString });
|
|
85
|
-
const selectedLanguage = view.getByRole('combobox') as Element;
|
|
86
|
-
await act(async () => {
|
|
87
|
-
userEvent.selectOptions(selectedLanguage, ['javascript']);
|
|
88
|
-
});
|
|
89
|
-
view.getByText(testString);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('initializes with deserialized text when there is both a language and text prop', () => {
|
|
93
|
-
const testString = 'yes hello';
|
|
94
|
-
const { view } = renderWrapper({
|
|
95
|
-
text: testString,
|
|
96
|
-
language: 'javascript',
|
|
97
|
-
});
|
|
98
|
-
view.getByText(testString);
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
describe('Change Handlers', () => {
|
|
102
|
-
it('triggers onEdit on text edit', async () => {
|
|
103
|
-
const onEdit = jest.fn();
|
|
104
|
-
const { view } = renderWrapper({
|
|
105
|
-
text: '',
|
|
106
|
-
language: 'javascript',
|
|
107
|
-
onEdit,
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
const editor = view.getByTestId(mockEditorTestId);
|
|
111
|
-
await act(async () => {
|
|
112
|
-
userEvent.type(editor, 'dog');
|
|
113
|
-
});
|
|
114
|
-
expect(onEdit).toHaveBeenCalledTimes(3);
|
|
115
|
-
expect(onEdit).toHaveBeenLastCalledWith('g', 'javascript');
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it('triggers onLanguageChange on language selection', async () => {
|
|
119
|
-
const onLanguageChange = jest.fn();
|
|
120
|
-
const { view } = renderWrapper({
|
|
121
|
-
onLanguageChange,
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
const selectedLanguage = view.getByRole('combobox') as Element;
|
|
125
|
-
await act(async () => {
|
|
126
|
-
userEvent.selectOptions(selectedLanguage, ['javascript']);
|
|
127
|
-
});
|
|
128
|
-
expect(onLanguageChange).toHaveBeenCalledWith(
|
|
129
|
-
"console.log('Hello world!');",
|
|
130
|
-
'javascript'
|
|
131
|
-
);
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
describe('Tracking', () => {
|
|
136
|
-
it('triggers trackClick on clicking the logo', async () => {
|
|
137
|
-
const { view } = renderWrapper({});
|
|
138
|
-
const logo = view.getByLabelText('visit codecademy.com');
|
|
139
|
-
await act(async () => {
|
|
140
|
-
userEvent.click(logo);
|
|
141
|
-
});
|
|
142
|
-
expect(trackClick).toHaveBeenCalledWith('logo', undefined);
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
it('triggers trackClick on language selection', async () => {
|
|
146
|
-
const { view } = renderWrapper();
|
|
147
|
-
const selectedLanguage = view.getByRole('combobox') as Element;
|
|
148
|
-
await act(async () => {
|
|
149
|
-
userEvent.selectOptions(selectedLanguage, ['javascript']);
|
|
150
|
-
});
|
|
151
|
-
expect(trackClick).toHaveBeenCalledWith('lang_select', undefined);
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
it('triggers trackClick for the first edit', async () => {
|
|
155
|
-
const testString = 'original-value';
|
|
156
|
-
const { view } = renderWrapper({
|
|
157
|
-
text: testString,
|
|
158
|
-
language: 'javascript',
|
|
159
|
-
trackFirstEdit: true,
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
const editor = view.getByTestId(mockEditorTestId);
|
|
163
|
-
await act(async () => {
|
|
164
|
-
userEvent.type(editor, 'd');
|
|
165
|
-
});
|
|
166
|
-
expect(trackClick).toHaveBeenCalledWith('edit', undefined);
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
it('triggers trackUserImpression', () => {
|
|
170
|
-
renderWrapper({
|
|
171
|
-
text: 'some-value',
|
|
172
|
-
language: 'javascript',
|
|
173
|
-
trackingData: {
|
|
174
|
-
page_name: 'forum_compose',
|
|
175
|
-
context: 'https://discuss.codecademy.com/some-interesting/post',
|
|
176
|
-
},
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
expect(trackUserImpression).toHaveBeenCalledWith({
|
|
180
|
-
page_name: 'forum_compose',
|
|
181
|
-
context: 'https://discuss.codecademy.com/some-interesting/post',
|
|
182
|
-
target: 'codebyte',
|
|
183
|
-
});
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
});
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { setupRtl } from '@codecademy/gamut-tests';
|
|
2
|
-
import { act } from '@testing-library/react';
|
|
3
|
-
import userEvent from '@testing-library/user-event';
|
|
4
|
-
|
|
5
|
-
import { Editor } from '../editor';
|
|
6
|
-
import { trackClick } from '../helpers';
|
|
7
|
-
|
|
8
|
-
jest.mock('react-resize-observer');
|
|
9
|
-
jest.mock('../libs/eventTracking');
|
|
10
|
-
jest.mock('../helpers', () => ({
|
|
11
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
12
|
-
// @ts-ignore
|
|
13
|
-
...jest.requireActual('../helpers'),
|
|
14
|
-
trackClick: jest.fn(),
|
|
15
|
-
}));
|
|
16
|
-
|
|
17
|
-
jest.mock('../MonacoEditor', () => ({
|
|
18
|
-
SimpleMonacoEditor: ({ value }: { value: string }) => value,
|
|
19
|
-
}));
|
|
20
|
-
|
|
21
|
-
const renderWrapper = setupRtl(Editor, {
|
|
22
|
-
hideCopyButton: false,
|
|
23
|
-
language: 'javascript',
|
|
24
|
-
text: 'hello world',
|
|
25
|
-
onChange: jest.fn(),
|
|
26
|
-
snippetsBaseUrl: '',
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
Object.defineProperty(navigator, 'clipboard', {
|
|
30
|
-
value: {
|
|
31
|
-
writeText: jest.fn().mockImplementation(() => Promise.resolve()),
|
|
32
|
-
},
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
describe('Editor', () => {
|
|
36
|
-
(global as any).fetch = jest.fn();
|
|
37
|
-
afterEach(() => {
|
|
38
|
-
(global as any).fetch.mockClear();
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('shows a prompt tooltip when the CodeByte has __not__ been copied via the button', () => {
|
|
42
|
-
const { view } = renderWrapper();
|
|
43
|
-
expect(view.queryByTestId('copy-confirmation-tooltip')).toBeFalsy();
|
|
44
|
-
view.getByTestId('copy-prompt-tooltip');
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('shows a confirmation tooltip when the CodeByte has been copied via the button', async () => {
|
|
48
|
-
const { view } = renderWrapper();
|
|
49
|
-
const copyBtn = view.getByTestId('copy-codebyte-btn');
|
|
50
|
-
await act(async () => {
|
|
51
|
-
userEvent.click(copyBtn as HTMLButtonElement);
|
|
52
|
-
});
|
|
53
|
-
expect(view.queryByTestId('copy-prompt-tooltip')).toBeFalsy();
|
|
54
|
-
view.getByTestId('copy-confirmation-tooltip');
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('hides the copy codebyte button if hideCopyButton prop is true"', () => {
|
|
58
|
-
const { view } = renderWrapper({
|
|
59
|
-
hideCopyButton: true,
|
|
60
|
-
});
|
|
61
|
-
expect(view.queryByTestId('copy-codebyte-btn')).toBeNull();
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('shows the copy codebyte button if hideCopyButton prop is not set', () => {
|
|
65
|
-
const { view } = renderWrapper();
|
|
66
|
-
|
|
67
|
-
view.getByTestId('copy-codebyte-btn');
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
describe('Tracking', () => {
|
|
71
|
-
it('tracks clicks on the run button', async () => {
|
|
72
|
-
(global as any).fetch.mockResolvedValue({
|
|
73
|
-
json: () =>
|
|
74
|
-
Promise.resolve({
|
|
75
|
-
stderr: [],
|
|
76
|
-
exit_code: 0,
|
|
77
|
-
stdout: '',
|
|
78
|
-
}),
|
|
79
|
-
});
|
|
80
|
-
const { view } = renderWrapper({
|
|
81
|
-
onChange: jest.fn(),
|
|
82
|
-
text: 'test',
|
|
83
|
-
language: 'javascript',
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
const runButton = view.getByText('Run');
|
|
87
|
-
await act(async () => {
|
|
88
|
-
userEvent.click(runButton);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
expect(trackClick).toHaveBeenCalledWith('run', undefined);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('tracks clicks on the copy codebyte button', async () => {
|
|
95
|
-
const { view } = renderWrapper({
|
|
96
|
-
onChange: jest.fn(),
|
|
97
|
-
text: 'test',
|
|
98
|
-
language: 'javascript',
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
const copyButton = view.getByTestId('copy-codebyte-btn');
|
|
102
|
-
await act(async () => {
|
|
103
|
-
userEvent.click(copyButton);
|
|
104
|
-
});
|
|
105
|
-
expect(trackClick).toHaveBeenCalledWith('copy', undefined);
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
});
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { trackClick } from '../helpers';
|
|
2
|
-
import { trackUserClick } from '../libs/eventTracking';
|
|
3
|
-
|
|
4
|
-
jest.mock('../libs/eventTracking');
|
|
5
|
-
|
|
6
|
-
const initialUrl = window.location.href;
|
|
7
|
-
const resetCodebytesParams = () =>
|
|
8
|
-
window.history.replaceState(null, '', initialUrl);
|
|
9
|
-
|
|
10
|
-
describe('trackClick', () => {
|
|
11
|
-
afterEach(() => {
|
|
12
|
-
resetCodebytesParams();
|
|
13
|
-
(trackUserClick as any).mockReset();
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it('tracks user click when tracking data is provided', () => {
|
|
17
|
-
const target = 'foobar';
|
|
18
|
-
const trackingData = {
|
|
19
|
-
page_name: 'forum',
|
|
20
|
-
context: 'https://discuss.codecademy.com/some-interesting/post',
|
|
21
|
-
target,
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
trackClick(target, trackingData);
|
|
25
|
-
expect(trackUserClick).toHaveBeenCalledWith({
|
|
26
|
-
page_name: 'forum',
|
|
27
|
-
context: 'https://discuss.codecademy.com/some-interesting/post',
|
|
28
|
-
target,
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('tracks user click when tracking data is not provided', () => {
|
|
33
|
-
const target = 'foobar';
|
|
34
|
-
trackClick(target);
|
|
35
|
-
expect(trackUserClick).toHaveBeenCalledWith({
|
|
36
|
-
target,
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
});
|
|
@@ -1,14 +0,0 @@
|
|
|
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('Which language do you want to code in?');
|
|
13
|
-
});
|
|
14
|
-
});
|
package/src/api.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { LanguageOption } from './consts';
|
|
2
|
-
|
|
3
|
-
interface Response {
|
|
4
|
-
stderr: string;
|
|
5
|
-
stdout: string;
|
|
6
|
-
exit_code: number;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
interface PostSnippetData {
|
|
10
|
-
language: LanguageOption;
|
|
11
|
-
code: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export const postSnippet = async (
|
|
15
|
-
data: PostSnippetData,
|
|
16
|
-
snippetsBaseUrl?: string
|
|
17
|
-
): Promise<Response> => {
|
|
18
|
-
const snippetsEndpoint = `https://${snippetsBaseUrl}/snippets`;
|
|
19
|
-
|
|
20
|
-
const response = await fetch(snippetsEndpoint, {
|
|
21
|
-
method: 'POST',
|
|
22
|
-
body: JSON.stringify(data),
|
|
23
|
-
headers: {
|
|
24
|
-
'x-codecademy-user-id': 'codebytes-anon-user',
|
|
25
|
-
},
|
|
26
|
-
});
|
|
27
|
-
return response.json();
|
|
28
|
-
};
|
package/src/codeByteEditor.tsx
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { Box, IconButton } from '@codecademy/gamut';
|
|
2
|
-
import { FaviconIcon } from '@codecademy/gamut-icons';
|
|
3
|
-
import { Background, system } from '@codecademy/gamut-styles';
|
|
4
|
-
import { StyleProps } from '@codecademy/variance';
|
|
5
|
-
import styled from '@emotion/styled';
|
|
6
|
-
import React, { useEffect, useState } from 'react';
|
|
7
|
-
|
|
8
|
-
import { helloWorld, LanguageOption } from './consts';
|
|
9
|
-
import { Editor } from './editor';
|
|
10
|
-
import { trackClick } from './helpers';
|
|
11
|
-
import { useEverInView } from './helpers/useEverInView';
|
|
12
|
-
import { LanguageSelection } from './languageSelection';
|
|
13
|
-
import { trackUserImpression } from './libs/eventTracking';
|
|
14
|
-
import { CodeByteEditorProps } from './types';
|
|
15
|
-
|
|
16
|
-
const editorBaseStyles = system.css({
|
|
17
|
-
border: 1,
|
|
18
|
-
borderColor: 'gray-900',
|
|
19
|
-
display: 'flex',
|
|
20
|
-
flexDirection: 'column',
|
|
21
|
-
minHeight: '25rem',
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
const EditorContainer = styled(Background)<StyleProps<typeof editorBaseStyles>>(
|
|
25
|
-
editorBaseStyles
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
29
|
-
text: initialText,
|
|
30
|
-
language: initialLanguage,
|
|
31
|
-
hideCopyButton = false,
|
|
32
|
-
snippetsBaseUrl,
|
|
33
|
-
onEdit,
|
|
34
|
-
onLanguageChange,
|
|
35
|
-
copyFormatter,
|
|
36
|
-
trackingData,
|
|
37
|
-
trackFirstEdit = false,
|
|
38
|
-
...rest
|
|
39
|
-
}) => {
|
|
40
|
-
const { everInView, ref } = useEverInView();
|
|
41
|
-
|
|
42
|
-
const getInitialText = () => {
|
|
43
|
-
if (initialText !== undefined) return initialText;
|
|
44
|
-
return initialLanguage ? helloWorld[initialLanguage] : '';
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const [text, setText] = useState<string>(getInitialText());
|
|
48
|
-
const [language, setLanguage] = useState<LanguageOption>(
|
|
49
|
-
initialLanguage ?? ''
|
|
50
|
-
);
|
|
51
|
-
const [hasBeenEdited, setHasBeenEdited] = useState(false);
|
|
52
|
-
|
|
53
|
-
useEffect(() => {
|
|
54
|
-
if (everInView) {
|
|
55
|
-
trackUserImpression({
|
|
56
|
-
page_name: trackingData?.page_name ?? 'Unknown',
|
|
57
|
-
context: trackingData?.context ?? document.referrer,
|
|
58
|
-
target: 'codebyte',
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
}, [everInView, trackingData]);
|
|
62
|
-
|
|
63
|
-
return (
|
|
64
|
-
<EditorContainer
|
|
65
|
-
bg="black"
|
|
66
|
-
maxWidth="43rem"
|
|
67
|
-
{...rest}
|
|
68
|
-
overflow="hidden"
|
|
69
|
-
ref={ref}
|
|
70
|
-
>
|
|
71
|
-
<Box borderBottom={1} borderColor="gray-900" py={4} pl={8}>
|
|
72
|
-
<IconButton
|
|
73
|
-
icon={FaviconIcon}
|
|
74
|
-
variant="secondary"
|
|
75
|
-
href="https://www.codecademy.com/"
|
|
76
|
-
target="_blank"
|
|
77
|
-
rel="noreferrer"
|
|
78
|
-
aria-label="visit codecademy.com"
|
|
79
|
-
onClick={() => trackClick('logo', trackingData)}
|
|
80
|
-
/>
|
|
81
|
-
</Box>
|
|
82
|
-
{language ? (
|
|
83
|
-
<Editor
|
|
84
|
-
language={language}
|
|
85
|
-
text={text}
|
|
86
|
-
hideCopyButton={hideCopyButton}
|
|
87
|
-
onChange={(newText: string) => {
|
|
88
|
-
setText(newText);
|
|
89
|
-
onEdit?.(newText, language);
|
|
90
|
-
if (trackFirstEdit && hasBeenEdited === false) {
|
|
91
|
-
setHasBeenEdited(true);
|
|
92
|
-
trackClick('edit', trackingData);
|
|
93
|
-
}
|
|
94
|
-
}}
|
|
95
|
-
snippetsBaseUrl={snippetsBaseUrl}
|
|
96
|
-
copyFormatter={copyFormatter}
|
|
97
|
-
trackingData={trackingData}
|
|
98
|
-
/>
|
|
99
|
-
) : (
|
|
100
|
-
<LanguageSelection
|
|
101
|
-
onChange={(newLanguage) => {
|
|
102
|
-
const newText: string =
|
|
103
|
-
text || (newLanguage ? helloWorld[newLanguage] : '');
|
|
104
|
-
setLanguage(newLanguage);
|
|
105
|
-
setText(newText);
|
|
106
|
-
trackClick('lang_select', trackingData);
|
|
107
|
-
onLanguageChange?.(newText, newLanguage);
|
|
108
|
-
}}
|
|
109
|
-
/>
|
|
110
|
-
)}
|
|
111
|
-
</EditorContainer>
|
|
112
|
-
);
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
export default CodeByteEditor;
|
package/src/consts.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
// key = language param to send to snippets service
|
|
2
|
-
// val = label in language selection drop down
|
|
3
|
-
export const LanguageOptions = {
|
|
4
|
-
'': 'Select a language',
|
|
5
|
-
cpp: 'C++',
|
|
6
|
-
csharp: 'C#',
|
|
7
|
-
golang: 'Go',
|
|
8
|
-
javascript: 'JavaScript',
|
|
9
|
-
php: 'PHP',
|
|
10
|
-
python: 'Python 3',
|
|
11
|
-
ruby: 'Ruby',
|
|
12
|
-
scheme: 'Scheme',
|
|
13
|
-
};
|
|
14
|
-
|
|
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/drawers.tsx
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import { FlexBox, IconButton } from '@codecademy/gamut';
|
|
2
|
-
import {
|
|
3
|
-
ArrowChevronLeftIcon,
|
|
4
|
-
ArrowChevronRightIcon,
|
|
5
|
-
} from '@codecademy/gamut-icons';
|
|
6
|
-
import styled from '@emotion/styled';
|
|
7
|
-
import React, { useState } from 'react';
|
|
8
|
-
|
|
9
|
-
const DrawerLabel = styled.span`
|
|
10
|
-
padding: 0.875rem 0.5rem;
|
|
11
|
-
`;
|
|
12
|
-
|
|
13
|
-
const LeftDrawerIcon = styled(ArrowChevronLeftIcon)<{ open?: boolean }>`
|
|
14
|
-
transition: transform 0.2s ease-in-out;
|
|
15
|
-
`;
|
|
16
|
-
const RightDrawerIcon = LeftDrawerIcon.withComponent(ArrowChevronRightIcon);
|
|
17
|
-
|
|
18
|
-
const Drawer = styled(FlexBox)<{ open?: boolean; hideOnClose?: boolean }>`
|
|
19
|
-
position: relative;
|
|
20
|
-
${({ open, hideOnClose }) => `
|
|
21
|
-
flex-basis: ${open ? '100%' : '0%'};
|
|
22
|
-
visibility: ${!open && hideOnClose ? 'hidden' : 'visible'};
|
|
23
|
-
transition: flex-basis 0.2s ${
|
|
24
|
-
open ? 'ease-out' : 'ease-in, visibility 0s 0.2s'
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
${LeftDrawerIcon}, ${RightDrawerIcon} {
|
|
28
|
-
transform: rotateZ(${open ? '0' : '180'}deg)};
|
|
29
|
-
}
|
|
30
|
-
`}
|
|
31
|
-
`;
|
|
32
|
-
|
|
33
|
-
export type DrawersProps = {
|
|
34
|
-
leftChild: React.ReactNode;
|
|
35
|
-
rightChild: React.ReactNode;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export const Drawers: React.FC<DrawersProps> = ({ leftChild, rightChild }) => {
|
|
39
|
-
const [open, setOpen] = useState<'left' | 'right' | 'both'>('both');
|
|
40
|
-
|
|
41
|
-
let ariaLabelCodeButton = 'hide code';
|
|
42
|
-
let ariaLabelOutputButton = 'hide output';
|
|
43
|
-
let isLeftOpen = false;
|
|
44
|
-
let isRightOpen = false;
|
|
45
|
-
|
|
46
|
-
if (open === 'left') {
|
|
47
|
-
ariaLabelCodeButton = ariaLabelOutputButton = 'show output';
|
|
48
|
-
isLeftOpen = true;
|
|
49
|
-
} else if (open === 'right') {
|
|
50
|
-
ariaLabelCodeButton = ariaLabelOutputButton = 'show code';
|
|
51
|
-
isRightOpen = true;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return (
|
|
55
|
-
<>
|
|
56
|
-
<FlexBox>
|
|
57
|
-
<Drawer
|
|
58
|
-
open={!isRightOpen}
|
|
59
|
-
alignItems="center"
|
|
60
|
-
flexWrap="nowrap"
|
|
61
|
-
textAlign="left"
|
|
62
|
-
borderRight={1}
|
|
63
|
-
borderColor="gray-900"
|
|
64
|
-
px={8}
|
|
65
|
-
>
|
|
66
|
-
<IconButton
|
|
67
|
-
icon={LeftDrawerIcon}
|
|
68
|
-
variant="secondary"
|
|
69
|
-
size="small"
|
|
70
|
-
onClick={() =>
|
|
71
|
-
setOpen((state) => (state === 'both' ? 'right' : 'both'))
|
|
72
|
-
}
|
|
73
|
-
aria-label={ariaLabelCodeButton}
|
|
74
|
-
aria-controls="code-drawer"
|
|
75
|
-
aria-expanded={!isRightOpen}
|
|
76
|
-
/>
|
|
77
|
-
<DrawerLabel id="code-drawer-label">Code</DrawerLabel>
|
|
78
|
-
</Drawer>
|
|
79
|
-
<Drawer
|
|
80
|
-
open={!isLeftOpen}
|
|
81
|
-
alignItems="center"
|
|
82
|
-
flexWrap="nowrap"
|
|
83
|
-
justifyContent="flex-end"
|
|
84
|
-
px={8}
|
|
85
|
-
>
|
|
86
|
-
<DrawerLabel id="output-drawer-label">Output</DrawerLabel>
|
|
87
|
-
<IconButton
|
|
88
|
-
icon={RightDrawerIcon}
|
|
89
|
-
variant="secondary"
|
|
90
|
-
size="small"
|
|
91
|
-
onClick={() =>
|
|
92
|
-
setOpen((state) => (state === 'both' ? 'left' : 'both'))
|
|
93
|
-
}
|
|
94
|
-
aria-label={ariaLabelOutputButton}
|
|
95
|
-
aria-controls="output-drawer"
|
|
96
|
-
aria-expanded={!isLeftOpen}
|
|
97
|
-
/>
|
|
98
|
-
</Drawer>
|
|
99
|
-
</FlexBox>
|
|
100
|
-
<FlexBox
|
|
101
|
-
flexGrow={1}
|
|
102
|
-
borderY={1}
|
|
103
|
-
borderColor="gray-900"
|
|
104
|
-
alignItems="stretch"
|
|
105
|
-
overflow="hidden"
|
|
106
|
-
>
|
|
107
|
-
<Drawer
|
|
108
|
-
hideOnClose
|
|
109
|
-
id="code-drawer"
|
|
110
|
-
aria-labelledby="code-drawer-label"
|
|
111
|
-
open={!isRightOpen}
|
|
112
|
-
flexGrow={0}
|
|
113
|
-
overflow="hidden"
|
|
114
|
-
borderColor="gray-900"
|
|
115
|
-
borderStyleRight="solid"
|
|
116
|
-
borderWidthRight="thin"
|
|
117
|
-
>
|
|
118
|
-
{leftChild}
|
|
119
|
-
</Drawer>
|
|
120
|
-
<Drawer
|
|
121
|
-
hideOnClose
|
|
122
|
-
id="output-drawer"
|
|
123
|
-
aria-labelledby="output-drawer-label"
|
|
124
|
-
role="region"
|
|
125
|
-
open={!isLeftOpen}
|
|
126
|
-
overflow="hidden"
|
|
127
|
-
>
|
|
128
|
-
{rightChild}
|
|
129
|
-
</Drawer>
|
|
130
|
-
</FlexBox>
|
|
131
|
-
</>
|
|
132
|
-
);
|
|
133
|
-
};
|