@codecademy/codebytes 0.2.1-alpha.524b82.0 → 0.2.1-alpha.547196.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.524b82.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.2.0...@codecademy/codebytes@0.2.1-alpha.524b82.0) (2022-01-11)
6
+ ### [0.2.1-alpha.547196.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.3.0...@codecademy/codebytes@0.2.1-alpha.547196.0) (2022-01-13)
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.524b82.0",
4
+ "version": "0.2.1-alpha.547196.0",
5
5
  "author": "Codecademy Engineering <dev@codecademy.com>",
6
6
  "sideEffects": [
7
7
  "**/*.css",
@@ -27,8 +27,8 @@
27
27
  "@emotion/react": "^11.4.0",
28
28
  "@emotion/styled": "^11.3.0",
29
29
  "@loadable/component": "^5.13.1",
30
- "@monaco-editor/react": "4.3.1",
31
- "monaco-editor": ">= 0.25.0 < 1",
30
+ "monaco-editor": "0.20.0",
31
+ "react-monaco-editor": "0.34.0",
32
32
  "react-resize-observer": "1.1.1"
33
33
  },
34
34
  "scripts": {
@@ -41,14 +41,17 @@
41
41
  },
42
42
  "license": "MIT",
43
43
  "devDependencies": {
44
+ "@codecademy/gamut-tests": "*",
44
45
  "@emotion/jest": "^11.3.0",
45
46
  "@testing-library/dom": "^7.31.2",
46
47
  "@testing-library/react": "^11.0.4",
48
+ "@testing-library/react-hooks": "3.2.1",
49
+ "@testing-library/user-event": "13.1.1",
47
50
  "@types/loadable__component": "^5.13.2",
48
51
  "monaco-editor-webpack-plugin": "1.9.1"
49
52
  },
50
53
  "publishConfig": {
51
54
  "access": "public"
52
55
  },
53
- "gitHead": "d70500edcc3765e46127f9b32bb89e2626c55ba3"
56
+ "gitHead": "bb68468f760bc1b81d2f0c179a2974196993cf39"
54
57
  }
@@ -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
  />
@@ -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
+ });
package/src/consts.ts CHANGED
@@ -61,4 +61,4 @@ export const helloWorld: { [key in languageOption]?: string } = {
61
61
  python,
62
62
  ruby,
63
63
  scheme,
64
- };
64
+ };
package/src/editor.tsx CHANGED
@@ -6,10 +6,10 @@ 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
 
12
+ import { CodebytesChangeHandlerMap } from '.';
13
13
  import { postSnippet } from './api';
14
14
  import type { languageOption } from './consts';
15
15
  import { Drawers } from './drawers';
@@ -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
 
@@ -39,8 +39,9 @@ type EditorProps = {
39
39
  hideCopyButton: boolean;
40
40
  language: languageOption;
41
41
  text: string;
42
+
42
43
  onChange: (text: string) => void;
43
- onCopy?: (text: string, language: string) => void;
44
+ on?: Pick<CodebytesChangeHandlerMap, 'copy' | 'run'>;
44
45
  snippetsBaseUrl?: string;
45
46
  };
46
47
 
@@ -48,7 +49,7 @@ export const Editor: React.FC<EditorProps> = ({
48
49
  language,
49
50
  text,
50
51
  hideCopyButton,
51
- onCopy,
52
+ on,
52
53
  onChange,
53
54
  snippetsBaseUrl,
54
55
  }) => {
@@ -61,7 +62,7 @@ export const Editor: React.FC<EditorProps> = ({
61
62
  .writeText(text)
62
63
  // eslint-disable-next-line no-console
63
64
  .catch(() => console.error('Failed to copy'));
64
- onCopy?.(text, language);
65
+ on?.copy?.(text, language);
65
66
  setIsCodeByteCopied(true);
66
67
  }
67
68
  };
@@ -81,6 +82,7 @@ export const Editor: React.FC<EditorProps> = ({
81
82
  };
82
83
  setStatus('waiting');
83
84
  setOutput('');
85
+ on?.run?.();
84
86
 
85
87
  try {
86
88
  const response = await postSnippet(data, snippetsBaseUrl);
package/src/index.tsx CHANGED
@@ -3,22 +3,28 @@ 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
8
  import { helloWorld, languageOption } from './consts';
9
9
  import { Editor } from './editor';
10
10
  import { LanguageSelection } from './languageSelection';
11
11
 
12
- type ChangeHandler = (text: string, language: languageOption) => void;
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
+
13
21
  export interface CodeByteEditorProps {
14
- text: string;
15
- language: languageOption;
16
- hideCopyButton: boolean;
17
- onCopy?: (text: string, language: string) => void;
22
+ text?: string;
23
+ language?: languageOption;
24
+ hideCopyButton?: boolean;
18
25
  isIFrame?: boolean;
19
- 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. */;
20
- onEdit?: ChangeHandler;
21
- onLanguageChange?: ChangeHandler;
26
+ snippetsBaseUrl?: string;
27
+ on?: CodebytesChangeHandlerMap;
22
28
  }
23
29
 
24
30
  const editorStates = states({
@@ -46,15 +52,23 @@ const EditorContainer = styled(Background)<EditorStyleProps>(
46
52
  export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
47
53
  text: initialText,
48
54
  language: initialLanguage,
49
- hideCopyButton,
50
- onCopy,
55
+ hideCopyButton = false,
51
56
  isIFrame = false,
52
- snippetsBaseUrl = '',
53
- onEdit,
54
- onLanguageChange,
57
+ on,
58
+ snippetsBaseUrl = process.env.CONTAINER_API_BASE,
55
59
  }) => {
56
- const [text, setText] = useState<string>(initialText);
57
- const [language, setLanguage] = useState<languageOption>(initialLanguage);
60
+ const [text, setText] = useState<string>(initialText ?? '');
61
+ const [language, setLanguage] = useState<languageOption>(
62
+ initialLanguage ?? ''
63
+ );
64
+
65
+ /* Resetting the state when the props change. If the language changes and the text is not provided output helloworld */
66
+ useEffect(() => {
67
+ if (language) {
68
+ setText(initialText ?? (helloWorld[language] || ''));
69
+ }
70
+ }, [language, initialText]);
71
+
58
72
  return (
59
73
  <EditorContainer bg="black" as="main" isIFrame={isIFrame}>
60
74
  <Box borderBottom={1} borderColor="gray-900" py={4} pl={8}>
@@ -65,6 +79,7 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
65
79
  target="_blank"
66
80
  rel="noreferrer"
67
81
  aria-label="visit codecademy.com"
82
+ onClick={() => on?.logoClick?.()}
68
83
  />
69
84
  </Box>
70
85
  {language ? (
@@ -74,12 +89,9 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
74
89
  hideCopyButton={hideCopyButton}
75
90
  onChange={(newText: string) => {
76
91
  setText(newText);
77
- onEdit?.(
78
- newText,
79
- language
80
- ); /* TODO: in DISC-355 update component to use an object that contains all change handlers */
92
+ on?.edit?.(newText, language);
81
93
  }}
82
- onCopy={onCopy}
94
+ on={on}
83
95
  snippetsBaseUrl={snippetsBaseUrl}
84
96
  />
85
97
  ) : (
@@ -88,10 +100,7 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
88
100
  const newText: string = text || helloWorld[newLanguage] || '';
89
101
  setLanguage(newLanguage);
90
102
  setText(newText);
91
- onLanguageChange?.(
92
- newText,
93
- newLanguage
94
- ); /* TODO: in DISC-355 update component to use an object that contains all change handlers */
103
+ on?.languageChange?.(newText, newLanguage);
95
104
  }}
96
105
  />
97
106
  )}
@@ -1,5 +1,5 @@
1
1
  import { Select } from '@codecademy/gamut';
2
- import { Background, themed } from '@codecademy/gamut-styles';
2
+ import { Background } from '@codecademy/gamut-styles';
3
3
  import styled from '@emotion/styled';
4
4
  import React from 'react';
5
5
 
@@ -8,19 +8,18 @@ import { languageOptions } from './consts';
8
8
 
9
9
  const StyledSelect = styled(Select)`
10
10
  margin-top: 1rem;
11
- color: ${themed('colors.text')};
12
-
11
+ color: ${({ theme }) => theme.colors.text};
13
12
  select {
14
- color: ${themed('colors.text')};
15
- background-color: ${themed('colors.black')};
13
+ color: ${({ theme }) => theme.colors.text};
14
+ background-color: ${({ theme }) => theme.colors.black};
16
15
 
17
16
  &:hover,
18
17
  &:active,
19
18
  &:focus {
20
- border-color: ${themed('colors.primary')};
19
+ border-color: ${({ theme }) => theme.colors.primary};
21
20
  }
22
21
  &:focus {
23
- box-shadow: inset 0 0 0 1px ${themed('colors.primary')};
22
+ box-shadow: inset 0 0 0 1px ${({ theme }) => theme.colors.primary};
24
23
  }
25
24
  }
26
25
  `;
@@ -34,10 +33,10 @@ export const LanguageSelection: React.FC<LanguageSelectionProps> = ({
34
33
  }) => {
35
34
  return (
36
35
  <Background bg="black" flex={1} px={16} pt={48}>
37
- What language do you want to write?
36
+ Which language do you want to code in?
38
37
  <StyledSelect
39
38
  id="language-select"
40
- aria-label="Select your language"
39
+ aria-label="Select a language"
41
40
  options={languageOptions}
42
41
  onChange={(e) => onChange(e.target.value as languageOption)}
43
42
  />