@codecademy/codebytes 0.3.0 → 0.3.1-alpha.9e2320.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,6 +3,14 @@
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.3.1-alpha.9e2320.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.3.0...@codecademy/codebytes@0.3.1-alpha.9e2320.0) (2022-01-14)
7
+
8
+ **Note:** Version bump only for package @codecademy/codebytes
9
+
10
+
11
+
12
+
13
+
6
14
  ## [0.3.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.2.0...@codecademy/codebytes@0.3.0) (2022-01-12)
7
15
 
8
16
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@codecademy/codebytes",
3
3
  "description": "Codebytes Code Editor",
4
- "version": "0.3.0",
4
+ "version": "0.3.1-alpha.9e2320.0",
5
5
  "author": "Codecademy Engineering <dev@codecademy.com>",
6
6
  "sideEffects": [
7
7
  "**/*.css",
@@ -48,5 +48,5 @@
48
48
  "publishConfig": {
49
49
  "access": "public"
50
50
  },
51
- "gitHead": "7845d303c2191ed8d56b06edbd6aae9741d68aeb"
51
+ "gitHead": "d87dd11aab216ef63dfb2be65087dac8f1c69ee3"
52
52
  }
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 your language',
4
+ '': 'Select a language',
5
5
  cpp: 'C++',
6
6
  csharp: 'C#',
7
7
  golang: 'Go',
@@ -13,3 +13,52 @@ export const languageOptions = {
13
13
  };
14
14
 
15
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
@@ -13,6 +13,7 @@ import { postSnippet } from './api';
13
13
  import type { languageOption } from './consts';
14
14
  import { Drawers } from './drawers';
15
15
  import { SimpleMonacoEditor } from './MonacoEditor';
16
+ import { CodebytesChangeHandlerMap } from './types';
16
17
 
17
18
  const Output = styled.pre<{ hasError: boolean }>`
18
19
  width: 100%;
@@ -39,7 +40,7 @@ type EditorProps = {
39
40
  language: languageOption;
40
41
  text: string;
41
42
  onChange: (text: string) => void;
42
- onCopy?: (text: string, language: string) => void;
43
+ on?: CodebytesChangeHandlerMap;
43
44
  snippetsBaseUrl?: string;
44
45
  };
45
46
 
@@ -47,7 +48,7 @@ export const Editor: React.FC<EditorProps> = ({
47
48
  language,
48
49
  text,
49
50
  hideCopyButton,
50
- onCopy,
51
+ on = {},
51
52
  onChange,
52
53
  snippetsBaseUrl,
53
54
  }) => {
@@ -60,7 +61,7 @@ export const Editor: React.FC<EditorProps> = ({
60
61
  .writeText(text)
61
62
  // eslint-disable-next-line no-console
62
63
  .catch(() => console.error('Failed to copy'));
63
- onCopy?.(text, language);
64
+ on.copy?.(text, language);
64
65
  setIsCodeByteCopied(true);
65
66
  }
66
67
  };
@@ -80,6 +81,7 @@ export const Editor: React.FC<EditorProps> = ({
80
81
  };
81
82
  setStatus('waiting');
82
83
  setOutput('');
84
+ on.run?.();
83
85
 
84
86
  try {
85
87
  const response = await postSnippet(data, snippetsBaseUrl);
package/src/index.tsx CHANGED
@@ -5,18 +5,10 @@ import { StyleProps } from '@codecademy/variance';
5
5
  import styled from '@emotion/styled';
6
6
  import React, { 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 { LanguageSelection } from './languageSelection';
11
+ import { CodeByteEditorProps } from './types';
20
12
 
21
13
  const editorStates = states({
22
14
  isIFrame: { height: '100vh' },
@@ -42,14 +34,14 @@ const EditorContainer = styled(Background)<EditorStyleProps>(
42
34
 
43
35
  export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
44
36
  text: initialText,
45
- language,
37
+ language: initialLanguage,
46
38
  hideCopyButton,
47
- onCopy,
48
39
  isIFrame = false,
40
+ on = {},
49
41
  snippetsBaseUrl = process.env.CONTAINER_API_BASE,
50
- onTextChange,
51
42
  }) => {
52
43
  const [text, setText] = useState<string>(initialText);
44
+ const [language, setLanguage] = useState<languageOption>(initialLanguage);
53
45
  return (
54
46
  <EditorContainer bg="black" as="main" isIFrame={isIFrame}>
55
47
  <Box borderBottom={1} borderColor="gray-900" py={4} pl={8}>
@@ -60,19 +52,32 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
60
52
  target="_blank"
61
53
  rel="noreferrer"
62
54
  aria-label="visit codecademy.com"
55
+ onClick={() => on.logoClick?.()}
63
56
  />
64
57
  </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
- />
58
+ {language ? (
59
+ <Editor
60
+ language={language}
61
+ text={text}
62
+ hideCopyButton={hideCopyButton}
63
+ onChange={(newText: string) => {
64
+ setText(newText);
65
+ on.edit?.(newText, language);
66
+ }}
67
+ on={on}
68
+ snippetsBaseUrl={snippetsBaseUrl}
69
+ />
70
+ ) : (
71
+ <LanguageSelection
72
+ onChange={(newLanguage) => {
73
+ const newText: string =
74
+ text || (newLanguage ? helloWorld[newLanguage] : '');
75
+ setLanguage(newLanguage);
76
+ setText(newText);
77
+ on.languageChange?.(newText, newLanguage);
78
+ }}
79
+ />
80
+ )}
76
81
  </EditorContainer>
77
82
  );
78
83
  };
@@ -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
+ };
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
+ }