You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/modules/1-getting-started/5-faq.md
+30-15Lines changed: 30 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,54 +7,65 @@ title: FAQ
7
7
8
8
## Infrastructure
9
9
10
-
### Could you explain more on the infrastructure of the modules system? Especially regarding how the bundle and tabs communicate.
11
-
> Could you perhaps explain more on the program structure? Especially on how they communicate to achieve what I need.
12
-
> And from what I currently understand,
13
-
> 1.`bundle/**/*.ts` is where we store all logical functions
10
+
### Could you explain more on the infrastructure of the modules system? Especially regarding how the bundle and tabs communicate
11
+
12
+
> Could you perhaps explain more on the program structure? Especially on how they communicate to achieve what I need.
13
+
> And from what I currently understand,
14
+
>
15
+
> 1.`bundle/**/*.ts` is where we store all logical functions
14
16
> 2.`tabs/**/*.tsx` is where we use all the react and jsx which will be shown in the frontend
15
17
16
-
A brief overview of the current infrastructure is explained [here](../4-advanced/flow/flow). However, I would like to explain in more detail how the `bundle` and `tab` interacts with `js-slang` and `cadet-frontend` respectively.
18
+
A brief overview of the current infrastructure is explained [here](../4-advanced/flow/flow). However, I would like to explain in more detail how the `bundle` and `tab` interacts with `js-slang` and `cadet-frontend` respectively.
19
+
20
+
Firstly, a `bundle` is defined as the suite of functions that are provided by the module. More specifically, what we mean by this is that the bundle will provide all the functions and constants that are intended to be available for use by the cadet when programming in the Source language.
17
21
18
-
Firstly, a `bundle` is defined as the suite of functions that are provided by the module. More specifically, what we mean by this is that the bundle will provide all the functions and constants that are intended to be available for use by the cadet when programming in the Source language.
An example of the code that makes use of a module written in the Source language is given above. The main objective of the `bundle` is to provide the behind the scenes implementation of the `install_filter`, `video_height`, `video_width` and `init` functions so that the cadet programming in Source language can use it as a black box. In the above example, you can find the implementations of the said functions [here](https://github.com/source-academy/modules/blob/master/src/bundles/pix_n_flix/index.ts). The `bundle` however, does not store all logical functions that is required by your module like those needed only by the tab that are not provided to the cadets.
26
30
27
-
A `tab` on the other hand is more formally defined as an _optional_ user interface used by the module. A module can exist without a tab. The tab exists for developers of modules to make use of Source Academy's side content tabs to display user interfaces that are used with the module's bundle. The tab can optionally choose to make use of the result returned from the evaluation of the Source code. The tab can also even be entirely not dependent on the result from the evaluation of the Source code. So how then does the `tab` make use of information from the `bundle`? The tab does this through the use of an object from `cadet-frontend` called the `debuggerContext`. When constructing the side content tabs, we would occasionally want to make use of front-end data like the code that was evaluated, the result of the code evaluated and Source version used. On `cadet-frontend`, all these information is stored within the `debuggerContext` object which is defined [here](https://github.com/source-academy/cadet-frontend/blob/master/src/commons/workspace/WorkspaceTypes.ts). Hence, all tabs will receieve the object `debuggerContext` as a component prop.
31
+
A `tab` on the other hand is more formally defined as an _optional_ user interface used by the module. A module can exist without a tab. The tab exists for developers of modules to make use of Source Academy's side content tabs to display user interfaces that are used with the module's bundle. The tab can optionally choose to make use of the result returned from the evaluation of the Source code. The tab can also even be entirely not dependent on the result from the evaluation of the Source code. So how then does the `tab` make use of information from the `bundle`? The tab does this through the use of an object from `cadet-frontend` called the `debuggerContext`. When constructing the side content tabs, we would occasionally want to make use of front-end data like the code that was evaluated, the result of the code evaluated and Source version used. On `cadet-frontend`, all these information is stored within the `debuggerContext` object which is defined [here](https://github.com/source-academy/cadet-frontend/blob/master/src/commons/workspace/WorkspaceTypes.ts). Hence, all tabs will receieve the object `debuggerContext` as a component prop.
32
+
33
+
An example of an implementation of this is from the `pix_n_flix` module. The implementation can be found [here](https://github.com/source-academy/modules/blob/master/src/bundles/pix_n_flix/index.ts). In the module, a function `init()` is provided to the Source programmer. The specifications of the `pix_n_flix` module requires this `init()` function to be applied as the last statement of the Source program. As a result, the `js-slang` evaluator will return the return value of the `init()` function which is a JavaScript object with the type signature shown below.
28
34
29
-
An example of an implementation of this is from the `pix_n_flix` module. The implementation can be found [here](https://github.com/source-academy/modules/blob/master/src/bundles/pix_n_flix/index.ts). In the module, a function `init()` is provided to the Source programmer. The specifications of the `pix_n_flix` module requires this `init()` function to be applied as the last statement of the Source program. As a result, the `js-slang` evaluator will return the return value of the `init()` function which is a JavaScript object with the type signature shown below.
As described in the paragraphs above, this return value of the `init()` function will be stored within the `debuggerContext` in `debuggerContext.result.value`. The `tab` associated with the rendering of the video and canvas element will then render the HTMLVideoElement and HTMLCanvasElement, before creating references to the respective elements and applying the `init()` function in `debuggerContext.result.value` in the component's `componentDidMount()` method.
41
+
42
+
As described in the paragraphs above, this return value of the `init()` function will be stored within the `debuggerContext` in `debuggerContext.result.value`. The `tab` associated with the rendering of the video and canvas element will then render the HTMLVideoElement and HTMLCanvasElement, before creating references to the respective elements and applying the `init()` function in `debuggerContext.result.value` in the component's `componentDidMount()` method.
37
43
38
44
## Set Up and Configuration
39
45
40
46
### How do we use our own local version of the js-slang interpreter with the modules?
41
-
> I have made some code changes to js-slang library and I want to test them out with my own local modules.
42
47
43
-
To use your local `js-slang` interpreter with your local modules, you will need to follow the steps below.
44
-
1. Serve your modules on a local server, done by transpiling your modules into JavaScript (`yarn build`) and then serving them as static assets (`yarn serve`). The default url for the local server is `http://localhost:8022`. Note that `yarn serve` serves the contents of the `build` folder.
48
+
> I have made some code changes to js-slang library and I want to test them out with my own local modules.
49
+
50
+
To use your local `js-slang` interpreter with your local modules, you will need to follow the steps below.
51
+
52
+
1. Serve your modules on a local server, done by transpiling your modules into JavaScript (`yarn build`) and then serving them as static assets (`yarn serve`). The default url for the local server is `http://localhost:8022`. Note that `yarn serve` serves the contents of the `build` folder.
45
53
2. Ensure that your local version of `js-slang` is linked to your local `cadet-frontend`. This is achieved by `yarn link` at the local `js-slang` root folder and `yarn link js-slang` at the local `cadet-frontend` root folder.
46
54
3. Ensure that your `cadet-frontend` environment variable `REACT_APP_MODULE_BACKEND_URL` is set to the address of your locally served modules server (from step 1). Again, the default url for the local server is `http://localhost:8022`.
47
55
4. Start your `cadet-frontend` web server locally to test your module.
48
56
49
57
### Is it possible to be using modules served from more than one location simultaneously?
58
+
50
59
> I want to use my own modules served from `http://localhost:8022` and the official modules from `https://source-academy.github.io/modules` at the same time. Is it going to be possible?
51
60
52
61
It is not possible to be retrieving the modules' JavaScript files from more than one place simultaneously. The endpoint to retrieve the modules' JavaScript files from is the one in `cadet-frontend` evironment variable `REACT_APP_MODULE_BACKEND_URL` in the `.env` file. If you are using the `js-slang` library without `cadet-frontend`, the default source will be `https://source-academy.github.io/modules`.
53
62
54
63
## Language Specifications
55
64
56
65
### Can a user on Source Academy import more than one module at once?
66
+
57
67
> Am I able to run the following code in Source?
68
+
58
69
```ts
59
70
import { function_a } from'module_a';
60
71
import { function_b } from'module_b';
@@ -68,25 +79,29 @@ Yes this is possible.
68
79
## Tabs
69
80
70
81
### Why is my Tab not spawning on Source Academy?
82
+
71
83
> Why is my tab not spawning on Source Academy? It is importing the functions but not spawning the tab.
72
84
73
85
* Check that the `toSpawn()` function in the Tab's `index.ts` returns `true` in your context.
74
86
* Check that the environment variable `REACT_APP_MODULE_BACKEND_URL` of the running `cadet-frontend` web server is set to the correct url.
75
87
* Check that `modules.json` in the root folder of the modules repository contains your module and your tab and that it is spelled correctly (eg. `{ module: { tabs: [ "Tab" ] } }`).
76
-
* Refresh your browser with empty cache and hard reload.
88
+
* Refresh your browser with empty cache and hard reload.
77
89
* Build and restart your modules static server again with `yarn build` and `yarn serve`.
78
90
79
91
### How does the modules system handles css for the tabs?
92
+
80
93
> How to include css styles in the module tabs?
81
94
82
-
Currently, two options are supported.
95
+
Currently, two options are supported.
96
+
83
97
*[Inline styles](https://www.w3schools.com/react/react_css.asp), where styles are added as an object to the react component's `style` prop
### How can we switch to the Source interpreter rather than the Source transpiler?
89
-
> I am modifying `js-slang`'s Source interpreter and need to change the mode of evaluation from the Source transpiler (default) to the Source interpreter.
103
+
104
+
> I am modifying `js-slang`'s Source interpreter and need to change the mode of evaluation from the Source transpiler (default) to the Source interpreter.
90
105
91
106
Include `"enable verbose";` as the first line of your program. Other functions of this setting can be found [here](https://github.com/source-academy/js-slang/blob/master/README.md#error-messages).
Copy file name to clipboardExpand all lines: docs/src/modules/2-bundle/2-creating/2-creating.md
+10-1Lines changed: 10 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,24 @@
1
1
# Creating a Bundle
2
+
2
3
This page contains instructions for creating a new bundle from scratch. If you are looking to edit an existing bundle refer to [these](../3-editing) instructions instead.
4
+
3
5
## Running the Template Command
6
+
4
7
> [!TIP]
5
-
> If necessary, before running the `template` command, you can run
8
+
> If necessary, before running the `template` command, you can run
9
+
>
6
10
> ```sh
7
11
> yarn workspaces focus @sourceacademy/modules
8
12
>```
13
+
>
9
14
> to install **only** the dependencies required for creating your bundle.
10
15
11
16
To create a new bundle, use the `template` command.
12
17
13
18
```sh
14
19
yarn template
15
20
```
21
+
16
22
This will start an interactive prompt that will help you through the process of creating a bundle. Enter `module` to create a new bundle.
17
23
18
24
```txt
@@ -21,6 +27,7 @@ module
21
27
```
22
28
23
29
Then enter the name of your new bundle. It must be in `snake_case`.
30
+
24
31
```txt
25
32
What would you like to create? (module/tab)
26
33
module
@@ -38,7 +45,9 @@ The command should have creates a new folder `src/bundles/new_bundle` with all t
38
45
From there you can edit your bundle as necessary
39
46
40
47
## Naming your Bundle
48
+
41
49
The name of your bundle is what Source users will import from to actually use your bundle:
Copy file name to clipboardExpand all lines: docs/src/modules/3-tabs/2-creating.md
+8-1Lines changed: 8 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,19 @@
1
1
# Creating a Tab
2
+
2
3
This page contains instructions for creating a new bundle from scratch. If you are looking to edit an existing bundle refer to [these](./3-editing) instructions instead.
3
4
4
5
All tabs require at least one parent bundle. Before you create your tab, make sure you identify which bundle(s) are
5
6
supposed to be the parent of your tab.
6
7
7
8
## Running the Template Command
9
+
8
10
> [!TIP]
9
-
> If necessary, before running the `template` command, you can run
11
+
> If necessary, before running the `template` command, you can run
12
+
>
10
13
> ```sh
11
14
> yarn workspaces focus @sourceacademy/modules
12
15
>```
16
+
>
13
17
> to install **only** the dependencies required for creating your tab.
14
18
15
19
To create a new tab, use the `template` command:
@@ -24,7 +28,9 @@ This will start an interactive prompt that will help you through the process of
24
28
What would you like to create? (module/tab)
25
29
tab
26
30
```
31
+
27
32
Then enter the name of the parent bundle. It must be a bundle that already exists.
33
+
28
34
```txt
29
35
What would you like to create? (module/tab)
30
36
tab
@@ -34,6 +40,7 @@ new_module
34
40
```
35
41
36
42
Then enter the name of your new tab. It must be in `PascalCase`.
Copy file name to clipboardExpand all lines: docs/src/modules/3-tabs/3-editing.md
+18-1Lines changed: 18 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,23 +1,29 @@
1
1
# Editing an Existing Tab
2
+
2
3
This page contains instructions for modifying an existing tab. If you are creating a new tab from scratch, refer to [these](./2-creating) instructions instead.
3
4
4
5
## Installing Dependencies
6
+
5
7
To install **only** the dependencies required by the tab you are modifying, use the command below:
Your tab may need other Javascript packages. To add a dependency to your tab, run the command below:
16
+
13
17
```sh
14
18
yarn add [dependency]
15
19
```
16
20
17
21
If the dependency does not need to be present during runtime, then use:
22
+
18
23
```sh
19
24
yarn add --dev [dependency]
20
25
```
26
+
21
27
This adds the dependency to `devDependencies` instead.
22
28
23
29
> [!WARNING]
@@ -27,26 +33,30 @@ This adds the dependency to `devDependencies` instead.
27
33
> [!NOTE]
28
34
> There are certain dependencies that are common to all tab (like `react`). When adding such a dependency, remember to add the exact version
29
35
> specified in the root repository `package.json`:
36
+
>
30
37
> ```sh
31
38
> yarn add react@^18.3.1
32
39
>```
40
+
>
33
41
> You can also use the command`yarn constraints` to check if you have incorrectly specified the version of a dependency.
34
42
35
43
> [!NOTE]
36
44
> When adding dependencies that originate from the repository (e.g `@sourceacademy/modules-lib`), use `workspace:^` as the given version:
45
+
>
37
46
>```sh
38
47
> yarn add @sourceacademy/modules-lib@workspace:^
39
48
>```
40
49
41
50
## React/UI Components
51
+
42
52
Tabs are written using the React (JSX) syntax. In React, each UI element is referred to as a "component". Documentation on how to create UIs and use React can be found [here](https://react.dev). Where possible,
43
53
you should aim to write components using the funcional syntax.
44
54
45
55
::: details Functional vs Class Components
46
56
Traditionally, React's functional components could not be "stateful". If a component needed to store state, it had to be written as a [class component](https://react.dev/reference/react/Component).
47
57
48
58
With the introduction of [hooks](https://react.dev/reference/react/hooks) in React 16, functional components could now be stateful and became the go-to method for writing React components. Officially, React now considers class components a legacy API and
49
-
all new code should be written using the functional style.
59
+
all new code should be written using the functional style.
50
60
51
61
Within this repository, many of the tabs were written before the introduction and widespread use of functional components. Regardless, you should be designing your components using the functional component syntax where possible.
52
62
:::
@@ -67,11 +77,13 @@ in the root `tsconfig.json` used by tabs. `esbuild` reads from `tsconfig.json` a
67
77
:::
68
78
69
79
### Components from the Common Library
80
+
70
81
There are also several React components defined under `@sourceacademy/modules-lib/tabs` that perform functions common across tabs.
71
82
72
83
You can see the documentation for these components [here](/lib/modules-lib/)
73
84
74
85
## Export Interface
86
+
75
87
As mentioned in the [overview](./1-overview), all tabs should export a single default export from their entry point using the `defineTab` helper from `@sourceacademy/modules-lib/tabs`:
76
88
77
89
```tsx
@@ -96,6 +108,7 @@ if you do not use the `defineTab` helper or if your tab is missing the default e
96
108
> [!TIP]
97
109
> For the Devserver to be able to accurately detect the changes you make to your tab whilein hot-reload mode, you should define your component
98
110
> outside of the `body` property as in the example **above** and **not** within the property as in the example below:
111
+
>
99
112
>```tsx
100
113
> // incorrect example
101
114
>export default defineTab({
@@ -110,6 +123,7 @@ if you do not use the `defineTab` helper or if your tab is missing the default e
110
123
111
124
Only the default export is used by the Frontend for displaying your tab. It is not necessary to also export your components, but it can be donefor testing:
112
125
::: code-group
126
+
113
127
```tsx [index.tsx]
114
128
import { defineTab } from '@sourceacademy/modules-lib/tabs';
As mentioned, the frontend will go through the list of tabs for the bundles currently loaded and determine if it should spawn those tabs. This is done
142
158
by calling the tab's `toSpawn` function (if it exists). If the function is missing, then the tab is always spawned.
143
159
@@ -148,6 +164,7 @@ However, there are some cases where it makes sense to have the tab always spawn.
148
164
So long as the `pix_n_flix` bundle is imported, the tab will be spawned.
149
165
150
166
### Can I update other UI elements in the frontend?
167
+
151
168
Right now, the UI is only updated once per evaluation (a limitation on the side of the frontend). This means that if code from your tab calls functions
152
169
like `display`, the output of those functions won't be displayed in the main REPL. The only elements that can be changed are elements within the tab.
0 commit comments