@codecademy/codebytes 0.3.1-alpha.cef935.0 → 0.4.1-alpha.0fdd4b.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 +6 -2
- package/src/api.ts +2 -2
- package/src/consts.ts +6 -6
- package/src/editor.tsx +8 -7
- package/src/helpers/index.ts +43 -0
- package/src/index.tsx +39 -12
- package/src/languageSelection.tsx +11 -30
- package/src/libs/eventTracking.ts +16 -0
- package/src/types.ts +10 -15
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.4.1-alpha.0fdd4b.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.4.0...@codecademy/codebytes@0.4.1-alpha.0fdd4b.0) (2022-01-26)
|
|
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.4.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.3.0...@codecademy/codebytes@0.4.0) (2022-01-21)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Features
|
|
18
|
+
|
|
19
|
+
* **Codebytes:** move language selection component disc 354 ([#17](https://github.com/Codecademy/client-modules/issues/17)) ([040553d](https://github.com/Codecademy/client-modules/commit/040553dcc7867b6e331712365bcc19ea2df306d5))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
14
23
|
## [0.3.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.2.0...@codecademy/codebytes@0.3.0) (2022-01-12)
|
|
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.4.1-alpha.0fdd4b.0",
|
|
5
5
|
"author": "Codecademy Engineering <dev@codecademy.com>",
|
|
6
6
|
"sideEffects": [
|
|
7
7
|
"**/*.css",
|
|
@@ -23,10 +23,13 @@
|
|
|
23
23
|
"@codecademy/gamut": "*",
|
|
24
24
|
"@codecademy/gamut-icons": "*",
|
|
25
25
|
"@codecademy/gamut-styles": "*",
|
|
26
|
+
"@codecademy/tracking": "0.18.0",
|
|
26
27
|
"@codecademy/variance": "*",
|
|
27
28
|
"@emotion/react": "^11.4.0",
|
|
28
29
|
"@emotion/styled": "^11.3.0",
|
|
29
30
|
"@monaco-editor/react": "4.3.1",
|
|
31
|
+
"js-base64": "^3.6.0",
|
|
32
|
+
"jsuri": "^1.3.1",
|
|
30
33
|
"monaco-editor": ">= 0.25.0 < 1",
|
|
31
34
|
"react-resize-observer": "1.1.1"
|
|
32
35
|
},
|
|
@@ -43,10 +46,11 @@
|
|
|
43
46
|
"@emotion/jest": "^11.3.0",
|
|
44
47
|
"@testing-library/dom": "^7.31.2",
|
|
45
48
|
"@testing-library/react": "^11.0.4",
|
|
49
|
+
"@types/jsuri": "^1.3.30",
|
|
46
50
|
"@types/loadable__component": "^5.13.2"
|
|
47
51
|
},
|
|
48
52
|
"publishConfig": {
|
|
49
53
|
"access": "public"
|
|
50
54
|
},
|
|
51
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "1090a927c92ab7920a1e065fe3adc6a22c44a99d"
|
|
52
56
|
}
|
package/src/api.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { LanguageOption } from './consts';
|
|
2
2
|
|
|
3
3
|
interface Response {
|
|
4
4
|
stderr: string;
|
|
@@ -7,7 +7,7 @@ interface Response {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
interface PostSnippetData {
|
|
10
|
-
language:
|
|
10
|
+
language: LanguageOption;
|
|
11
11
|
code: string;
|
|
12
12
|
}
|
|
13
13
|
|
package/src/consts.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// key = language param to send to snippets service
|
|
2
2
|
// val = label in language selection drop down
|
|
3
|
-
export const
|
|
3
|
+
export const LanguageOptions = {
|
|
4
4
|
'': 'Select a language',
|
|
5
5
|
cpp: 'C++',
|
|
6
6
|
csharp: 'C#',
|
|
@@ -12,11 +12,11 @@ export const languageOptions = {
|
|
|
12
12
|
scheme: 'Scheme',
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
-
export type
|
|
15
|
+
export type LanguageOption = keyof typeof LanguageOptions;
|
|
16
16
|
|
|
17
|
-
export const validLanguages = Object.keys(
|
|
17
|
+
export const validLanguages = Object.keys(LanguageOptions).filter(
|
|
18
18
|
(option) => !!option
|
|
19
|
-
) as
|
|
19
|
+
) as 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
|
@@ -10,10 +10,10 @@ import styled from '@emotion/styled';
|
|
|
10
10
|
import React, { useState } from 'react';
|
|
11
11
|
|
|
12
12
|
import { postSnippet } from './api';
|
|
13
|
-
import type {
|
|
13
|
+
import type { LanguageOption } from './consts';
|
|
14
14
|
import { Drawers } from './drawers';
|
|
15
|
+
import { trackClick } from './helpers';
|
|
15
16
|
import { SimpleMonacoEditor } from './MonacoEditor';
|
|
16
|
-
import { CodebytesChangeHandlerMap } from './types';
|
|
17
17
|
|
|
18
18
|
const Output = styled.pre<{ hasError: boolean }>`
|
|
19
19
|
width: 100%;
|
|
@@ -37,10 +37,10 @@ const DOCKER_SIGTERM = 143;
|
|
|
37
37
|
|
|
38
38
|
type EditorProps = {
|
|
39
39
|
hideCopyButton: boolean;
|
|
40
|
-
language:
|
|
40
|
+
language: LanguageOption;
|
|
41
41
|
text: string;
|
|
42
42
|
onChange: (text: string) => void;
|
|
43
|
-
|
|
43
|
+
onCopy?: (text: string, language: string) => void;
|
|
44
44
|
snippetsBaseUrl?: string;
|
|
45
45
|
};
|
|
46
46
|
|
|
@@ -48,8 +48,8 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
48
48
|
language,
|
|
49
49
|
text,
|
|
50
50
|
hideCopyButton,
|
|
51
|
-
on = {},
|
|
52
51
|
onChange,
|
|
52
|
+
onCopy,
|
|
53
53
|
snippetsBaseUrl,
|
|
54
54
|
}) => {
|
|
55
55
|
const [output, setOutput] = useState('');
|
|
@@ -61,8 +61,9 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
61
61
|
.writeText(text)
|
|
62
62
|
// eslint-disable-next-line no-console
|
|
63
63
|
.catch(() => console.error('Failed to copy'));
|
|
64
|
-
|
|
64
|
+
onCopy?.(text, language);
|
|
65
65
|
setIsCodeByteCopied(true);
|
|
66
|
+
trackClick('copy');
|
|
66
67
|
}
|
|
67
68
|
};
|
|
68
69
|
|
|
@@ -81,7 +82,7 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
81
82
|
};
|
|
82
83
|
setStatus('waiting');
|
|
83
84
|
setOutput('');
|
|
84
|
-
|
|
85
|
+
trackClick('run');
|
|
85
86
|
|
|
86
87
|
try {
|
|
87
88
|
const response = await postSnippet(data, snippetsBaseUrl);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import Uri from 'jsuri';
|
|
2
|
+
|
|
3
|
+
import { trackUserClick } from '../libs/eventTracking';
|
|
4
|
+
|
|
5
|
+
export type CodebyteOptions = {
|
|
6
|
+
clientName: string;
|
|
7
|
+
parentPage: string;
|
|
8
|
+
renderMode: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export enum CodebytesParams {
|
|
12
|
+
Language = 'lang',
|
|
13
|
+
Text = 'text',
|
|
14
|
+
CopyMode = 'copy-mode',
|
|
15
|
+
ClientName = 'client-name',
|
|
16
|
+
Page = 'page',
|
|
17
|
+
Mode = 'mode',
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const getOptions = (): CodebyteOptions => {
|
|
21
|
+
const currentUri = new Uri(window.location.href);
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
clientName:
|
|
25
|
+
currentUri.getQueryParamValue(CodebytesParams.ClientName) || 'Unknown',
|
|
26
|
+
parentPage:
|
|
27
|
+
currentUri.getQueryParamValue(CodebytesParams.Page) || document.referrer,
|
|
28
|
+
renderMode: currentUri.getQueryParamValue(CodebytesParams.Mode) || '',
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const trackClick = (target: string) => {
|
|
33
|
+
const options = getOptions();
|
|
34
|
+
const page_name = options.renderMode
|
|
35
|
+
? `${options.clientName}_${options.renderMode}`
|
|
36
|
+
: options.clientName;
|
|
37
|
+
|
|
38
|
+
trackUserClick({
|
|
39
|
+
page_name,
|
|
40
|
+
context: options.parentPage,
|
|
41
|
+
target,
|
|
42
|
+
});
|
|
43
|
+
};
|
package/src/index.tsx
CHANGED
|
@@ -3,15 +3,17 @@ 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 { helloWorld,
|
|
8
|
+
import { helloWorld, LanguageOption } from './consts';
|
|
9
9
|
import { Editor } from './editor';
|
|
10
|
+
import { getOptions, trackClick } from './helpers';
|
|
10
11
|
import { LanguageSelection } from './languageSelection';
|
|
12
|
+
import { trackUserImpression } from './libs/eventTracking';
|
|
11
13
|
import { CodeByteEditorProps } from './types';
|
|
12
14
|
|
|
13
15
|
const editorStates = states({
|
|
14
|
-
|
|
16
|
+
isForums: { height: '100vh' },
|
|
15
17
|
});
|
|
16
18
|
|
|
17
19
|
const editorBaseStyles = system.css({
|
|
@@ -36,14 +38,32 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
36
38
|
text: initialText,
|
|
37
39
|
language: initialLanguage,
|
|
38
40
|
hideCopyButton,
|
|
39
|
-
|
|
40
|
-
on = {},
|
|
41
|
+
isForums = false,
|
|
41
42
|
snippetsBaseUrl = process.env.CONTAINER_API_BASE,
|
|
43
|
+
onEdit,
|
|
44
|
+
onLanguageChange,
|
|
42
45
|
}) => {
|
|
43
46
|
const [text, setText] = useState<string>(initialText);
|
|
44
|
-
const [language, setLanguage] = useState<
|
|
47
|
+
const [language, setLanguage] = useState<LanguageOption>(initialLanguage);
|
|
48
|
+
const [hasBeenEdited, setHasBeenEdited] = useState(false);
|
|
49
|
+
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
if (isForums) {
|
|
52
|
+
const options = getOptions();
|
|
53
|
+
const page_name = options.renderMode
|
|
54
|
+
? `${options.clientName}_${options.renderMode}`
|
|
55
|
+
: options.clientName;
|
|
56
|
+
|
|
57
|
+
trackUserImpression({
|
|
58
|
+
page_name,
|
|
59
|
+
context: options.parentPage,
|
|
60
|
+
target: 'codebyte',
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}, []);
|
|
64
|
+
|
|
45
65
|
return (
|
|
46
|
-
<EditorContainer bg="black" as="main"
|
|
66
|
+
<EditorContainer bg="black" as="main" isForums={isForums}>
|
|
47
67
|
<Box borderBottom={1} borderColor="gray-900" py={4} pl={8}>
|
|
48
68
|
<IconButton
|
|
49
69
|
icon={FaviconIcon}
|
|
@@ -52,7 +72,7 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
52
72
|
target="_blank"
|
|
53
73
|
rel="noreferrer"
|
|
54
74
|
aria-label="visit codecademy.com"
|
|
55
|
-
onClick={() =>
|
|
75
|
+
onClick={() => trackClick('logo')}
|
|
56
76
|
/>
|
|
57
77
|
</Box>
|
|
58
78
|
{language ? (
|
|
@@ -62,18 +82,25 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
62
82
|
hideCopyButton={hideCopyButton}
|
|
63
83
|
onChange={(newText: string) => {
|
|
64
84
|
setText(newText);
|
|
65
|
-
|
|
85
|
+
onEdit?.(newText, language);
|
|
86
|
+
|
|
87
|
+
const { renderMode } = getOptions();
|
|
88
|
+
if (!renderMode && hasBeenEdited === false) {
|
|
89
|
+
setHasBeenEdited(true);
|
|
90
|
+
trackClick('edit');
|
|
91
|
+
}
|
|
66
92
|
}}
|
|
67
|
-
on={on}
|
|
68
93
|
snippetsBaseUrl={snippetsBaseUrl}
|
|
69
94
|
/>
|
|
70
95
|
) : (
|
|
71
96
|
<LanguageSelection
|
|
72
97
|
onChange={(newLanguage) => {
|
|
73
|
-
const newText: string =
|
|
98
|
+
const newText: string =
|
|
99
|
+
text || (newLanguage ? helloWorld[newLanguage] : '');
|
|
74
100
|
setLanguage(newLanguage);
|
|
75
101
|
setText(newText);
|
|
76
|
-
|
|
102
|
+
trackClick('lang_select');
|
|
103
|
+
onLanguageChange?.(newText, newLanguage);
|
|
77
104
|
}}
|
|
78
105
|
/>
|
|
79
106
|
)}
|
|
@@ -1,45 +1,26 @@
|
|
|
1
|
-
import { Select } from '@codecademy/gamut';
|
|
2
|
-
import {
|
|
3
|
-
import styled from '@emotion/styled';
|
|
1
|
+
import { Select, Text } from '@codecademy/gamut';
|
|
2
|
+
import { ColorMode } from '@codecademy/gamut-styles';
|
|
4
3
|
import React from 'react';
|
|
5
4
|
|
|
6
|
-
import type {
|
|
7
|
-
import {
|
|
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
|
-
`;
|
|
5
|
+
import type { LanguageOption } from './consts';
|
|
6
|
+
import { LanguageOptions } from './consts';
|
|
26
7
|
|
|
27
8
|
export type LanguageSelectionProps = {
|
|
28
|
-
onChange: (newLanguage:
|
|
9
|
+
onChange: (newLanguage: LanguageOption) => void;
|
|
29
10
|
};
|
|
30
11
|
|
|
31
12
|
export const LanguageSelection: React.FC<LanguageSelectionProps> = ({
|
|
32
13
|
onChange,
|
|
33
14
|
}) => {
|
|
34
15
|
return (
|
|
35
|
-
<
|
|
36
|
-
Which language do you want to code in
|
|
37
|
-
<
|
|
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
|
|
38
19
|
id="language-select"
|
|
39
20
|
aria-label="Select a language"
|
|
40
|
-
options={
|
|
41
|
-
onChange={(e) => onChange(e.target.value as
|
|
21
|
+
options={LanguageOptions}
|
|
22
|
+
onChange={(e) => onChange(e.target.value as LanguageOption)}
|
|
42
23
|
/>
|
|
43
|
-
</
|
|
24
|
+
</ColorMode>
|
|
44
25
|
);
|
|
45
26
|
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createTracker } from '@codecademy/tracking';
|
|
2
|
+
|
|
3
|
+
const IS_DEV = process.env.NODE_ENV === 'development';
|
|
4
|
+
const API_BASE_URL =
|
|
5
|
+
process.env.GATSBY_MONOLITH_API_BASE || 'https://www.codecademy.com';
|
|
6
|
+
|
|
7
|
+
const tracker = createTracker({
|
|
8
|
+
apiBaseUrl: API_BASE_URL,
|
|
9
|
+
verbose: IS_DEV,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export const {
|
|
13
|
+
click: trackUserClick,
|
|
14
|
+
visit: trackUserVisit,
|
|
15
|
+
impression: trackUserImpression,
|
|
16
|
+
} = tracker;
|
package/src/types.ts
CHANGED
|
@@ -1,20 +1,15 @@
|
|
|
1
|
-
import {
|
|
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
|
-
};
|
|
1
|
+
import { LanguageOption } from './consts';
|
|
12
2
|
|
|
3
|
+
interface CodebytesChangeHandler {
|
|
4
|
+
(text: string, language: LanguageOption): void;
|
|
5
|
+
}
|
|
13
6
|
export interface CodeByteEditorProps {
|
|
14
7
|
text: string;
|
|
15
|
-
language:
|
|
8
|
+
language: LanguageOption;
|
|
16
9
|
hideCopyButton: boolean;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
10
|
+
onCopy?: CodebytesChangeHandler;
|
|
11
|
+
isForums?: boolean;
|
|
12
|
+
snippetsBaseUrl?: string;
|
|
13
|
+
onEdit?: CodebytesChangeHandler;
|
|
14
|
+
onLanguageChange?: CodebytesChangeHandler;
|
|
20
15
|
}
|