@codecademy/codebytes 0.2.1-alpha.f45f38.0 → 0.3.1-alpha.16a37d.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 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.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)
6
+ ### [0.3.1-alpha.16a37d.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.3.0...@codecademy/codebytes@0.3.1-alpha.16a37d.0) (2022-01-21)
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/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.f45f38.0",
4
+ "version": "0.3.1-alpha.16a37d.0",
5
5
  "author": "Codecademy Engineering <dev@codecademy.com>",
6
6
  "sideEffects": [
7
7
  "**/*.css",
@@ -23,11 +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
- "@loadable/component": "^5.13.1",
30
30
  "@monaco-editor/react": "4.3.1",
31
+ "js-base64": "^3.6.0",
32
+ "jsuri": "^1.3.1",
31
33
  "monaco-editor": ">= 0.25.0 < 1",
32
34
  "react-resize-observer": "1.1.1"
33
35
  },
@@ -44,11 +46,11 @@
44
46
  "@emotion/jest": "^11.3.0",
45
47
  "@testing-library/dom": "^7.31.2",
46
48
  "@testing-library/react": "^11.0.4",
47
- "@types/loadable__component": "^5.13.2",
48
- "monaco-editor-webpack-plugin": "1.9.1"
49
+ "@types/jsuri": "^1.3.30",
50
+ "@types/loadable__component": "^5.13.2"
49
51
  },
50
52
  "publishConfig": {
51
53
  "access": "public"
52
54
  },
53
- "gitHead": "783fed789d9754d46535c05f38b2a8a8185aa298"
55
+ "gitHead": "f6569c17f562407f3f6873b2e3f8949794028656"
54
56
  }
@@ -3,16 +3,14 @@
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 loadable from '@loadable/component';
7
- import { OnMount } from '@monaco-editor/react';
6
+ import ReactMonacoEditor, { OnMount } from '@monaco-editor/react';
7
+ import { editor } from 'monaco-editor/esm/vs/editor/editor.api';
8
8
  import React, { useCallback, useRef } from 'react';
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('@monaco-editor/react'));
15
-
16
14
  export type SimpleMonacoEditorProps = {
17
15
  value: string;
18
16
  language: string;
@@ -26,7 +24,7 @@ export const SimpleMonacoEditor: React.FC<SimpleMonacoEditorProps> = ({
26
24
  language,
27
25
  onChange,
28
26
  }) => {
29
- const editorRef = useRef<any>(null);
27
+ const editorRef = useRef<editor.IStandaloneCodeEditor | null>(null);
30
28
  const editorWillMount = useCallback(
31
29
  (editor: ThemedEditor, monaco: Monaco) => {
32
30
  editorRef.current = editor;
@@ -39,7 +37,7 @@ export const SimpleMonacoEditor: React.FC<SimpleMonacoEditorProps> = ({
39
37
  <>
40
38
  <ResizeObserver
41
39
  onResize={({ height, width }) => {
42
- editorRef.current?.editor?.layout({
40
+ editorRef.current?.layout({
43
41
  height,
44
42
  width,
45
43
  });
@@ -49,7 +47,7 @@ export const SimpleMonacoEditor: React.FC<SimpleMonacoEditorProps> = ({
49
47
  onMount={editorWillMount}
50
48
  onChange={onChange}
51
49
  options={{ minimap: { enabled: false } }}
52
- theme="dark"
50
+ theme="vs-dark"
53
51
  value={value}
54
52
  language={language}
55
53
  />
package/src/api.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { languageOption } from './consts';
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: languageOption;
10
+ language: LanguageOption;
11
11
  code: string;
12
12
  }
13
13
 
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
- export const languageOptions = {
4
- '': 'Select your language',
3
+ export const LanguageOptions = {
4
+ '': 'Select a language',
5
5
  cpp: 'C++',
6
6
  csharp: 'C#',
7
7
  golang: 'Go',
@@ -12,4 +12,53 @@ export const languageOptions = {
12
12
  scheme: 'Scheme',
13
13
  };
14
14
 
15
- export type languageOption = keyof typeof languageOptions;
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 = {
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,13 +6,13 @@ 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
 
13
12
  import { postSnippet } from './api';
14
- import type { languageOption } from './consts';
13
+ import type { LanguageOption } from './consts';
15
14
  import { Drawers } from './drawers';
15
+ import { trackClick } from './helpers';
16
16
  import { SimpleMonacoEditor } from './MonacoEditor';
17
17
 
18
18
  const Output = styled.pre<{ hasError: boolean }>`
@@ -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['gray-900']};
28
+ background-color: ${theme.colors['navy-900']};
29
29
  `}
30
30
  `;
31
31
 
@@ -37,10 +37,9 @@ const DOCKER_SIGTERM = 143;
37
37
 
38
38
  type EditorProps = {
39
39
  hideCopyButton: boolean;
40
- language: languageOption;
40
+ language: LanguageOption;
41
41
  text: string;
42
42
  onChange: (text: string) => void;
43
- onCopy?: (text: string, language: string) => void;
44
43
  snippetsBaseUrl?: string;
45
44
  };
46
45
 
@@ -48,7 +47,6 @@ export const Editor: React.FC<EditorProps> = ({
48
47
  language,
49
48
  text,
50
49
  hideCopyButton,
51
- onCopy,
52
50
  onChange,
53
51
  snippetsBaseUrl,
54
52
  }) => {
@@ -61,8 +59,8 @@ export const Editor: React.FC<EditorProps> = ({
61
59
  .writeText(text)
62
60
  // eslint-disable-next-line no-console
63
61
  .catch(() => console.error('Failed to copy'));
64
- onCopy?.(text, language);
65
62
  setIsCodeByteCopied(true);
63
+ trackClick('copy');
66
64
  }
67
65
  };
68
66
 
@@ -81,6 +79,7 @@ export const Editor: React.FC<EditorProps> = ({
81
79
  };
82
80
  setStatus('waiting');
83
81
  setOutput('');
82
+ trackClick('run');
84
83
 
85
84
  try {
86
85
  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,20 +3,14 @@ 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
- export interface CodeByteEditorProps {
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 { getOptions, trackClick } from './helpers';
11
+ import { LanguageSelection } from './languageSelection';
12
+ import { trackUserImpression } from './libs/eventTracking';
13
+ import { CodeByteEditorProps } from './types';
20
14
 
21
15
  const editorStates = states({
22
16
  isIFrame: { height: '100vh' },
@@ -42,14 +36,28 @@ const EditorContainer = styled(Background)<EditorStyleProps>(
42
36
 
43
37
  export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
44
38
  text: initialText,
45
- language,
39
+ language: initialLanguage,
46
40
  hideCopyButton,
47
- onCopy,
48
41
  isIFrame = false,
49
- snippetsBaseUrl = '',
50
- onTextChange,
42
+ snippetsBaseUrl = process.env.CONTAINER_API_BASE,
51
43
  }) => {
52
44
  const [text, setText] = useState<string>(initialText);
45
+ const [language, setLanguage] = useState<LanguageOption>(initialLanguage);
46
+ const [hasBeenEdited, setHasBeenEdited] = useState(false);
47
+
48
+ useEffect(() => {
49
+ const options = getOptions();
50
+ const page_name = options.renderMode
51
+ ? `${options.clientName}_${options.renderMode}`
52
+ : options.clientName;
53
+
54
+ trackUserImpression({
55
+ page_name,
56
+ context: options.parentPage,
57
+ target: 'codebyte',
58
+ });
59
+ }, []);
60
+
53
61
  return (
54
62
  <EditorContainer bg="black" as="main" isIFrame={isIFrame}>
55
63
  <Box borderBottom={1} borderColor="gray-900" py={4} pl={8}>
@@ -60,19 +68,36 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
60
68
  target="_blank"
61
69
  rel="noreferrer"
62
70
  aria-label="visit codecademy.com"
71
+ onClick={() => trackClick('logo')}
63
72
  />
64
73
  </Box>
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
- />
74
+ {language ? (
75
+ <Editor
76
+ language={language}
77
+ text={text}
78
+ hideCopyButton={hideCopyButton}
79
+ onChange={(newText: string) => {
80
+ setText(newText);
81
+
82
+ const { renderMode } = getOptions();
83
+ if (!renderMode && hasBeenEdited === false) {
84
+ setHasBeenEdited(true);
85
+ trackClick('edit');
86
+ }
87
+ }}
88
+ snippetsBaseUrl={snippetsBaseUrl}
89
+ />
90
+ ) : (
91
+ <LanguageSelection
92
+ onChange={(newLanguage) => {
93
+ const newText: string =
94
+ text || (newLanguage ? helloWorld[newLanguage] : '');
95
+ setLanguage(newLanguage);
96
+ setText(newText);
97
+ trackClick('lang_select');
98
+ }}
99
+ />
100
+ )}
76
101
  </EditorContainer>
77
102
  );
78
103
  };
@@ -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
+ };
@@ -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 ADDED
@@ -0,0 +1,13 @@
1
+ import { LanguageOption } from './consts';
2
+
3
+ type CodebytesChangeHandler = (text: string, language: LanguageOption) => void;
4
+ export interface CodeByteEditorProps {
5
+ text: string;
6
+ language: LanguageOption;
7
+ hideCopyButton: boolean;
8
+ onCopy?: CodebytesChangeHandler;
9
+ isIFrame?: boolean;
10
+ snippetsBaseUrl?: string;
11
+ onEdit?: CodebytesChangeHandler;
12
+ onLanguageChange?: CodebytesChangeHandler;
13
+ }