adev/src/app/editor/README.md
The page responsible for the embedded editor lazy loads the EmbeddedEditor component and the NodeRuntimeSandbox, then triggers the initialization of all components and services. The embedded editor is available in the following pages:
The project assets are fetched by the EmbeddedTutorialManager. Meanwhile:
The tutorial source code is mounted in the WebContainer's filesystem by the NodeRuntimeSandbox
The tutorial project dependencies are installed by the NodeRuntimeSandbox.
The development server is started by the NodeRuntimeSandbox and the types are loaded by the TypingsLoader service.
The preview is loaded with the URL provided by the WebContainer API after the development server is started.
The project is ready.
NodeRuntimeSandbox.NodeRuntimeSandbox, then the user can see the changes in the preview.The user clicks on the new file button.
The new file tab is opened.
The user types the new file name.
If the file name is valid, the file is created in the WebContainer filesystem by the NodeRuntimeSandbox.
.. is disallowed in the file name to prevent users to create files outside the src directory.The file is added to the TypeScript virtual file system, allowing the TypeScript web worker to provide diagnostics, autocomplete and type features for the new file. Also, exports from the new file are available in other files.
The new file is added as the last tab in the code editor and the new file can be edited.
NOTE: If the new file name matches a file that already exists but is hidden in the code editor, the content for that file will show up in the created file. An example for a file that always exists is index.html.
NodeRuntimeSandbox.NOTE: Some files can't be deleted to prevent users to break the app, being src/main.tsand src/index.html
The embedded editor considers a project change when the embedded editor was already initialized and the user changes the page in the following scenarios:
When a project change is detected, the EmbeddedTutorialManager emits the tutorialChanged observable, which is listened in multiple sub-components and services, then each component/service performs the necessary operations to switch the project.
The following steps are executed on project change:
EmbeddedTutorialManager.npm install is triggered, hiding the preview and going to the install loading step.EmbeddedEditorThe embedded editor is the parent component that holds all the components and services that compose the embedded editor.
CodeEditorThe component that holds the code editor view and the code editor state.
CodeMirrorEditorCodeMirror is the library used to handle the code editor.
The CodeMirrorEditor service manages the CodeMirror instance and all the interactions with the library used to handle the code editor.
The TypeScript features are provided by the TypeScript web worker, that is initialized by the CodeMirrorEditor service.
The TypeScript web worker uses @typescript/vfs and the TypeScript language service to provide diagnostics, autocomplete and type features.
PreviewThe preview component manages the iframe responsible for displaying the tutorial project preview, with the URL provided by the WebContainer API after the development server is started.
While the project is being initialized, the preview displays the loading state.
TerminalXterm.js is the library used to handle the terminals.
The terminal component handles the Xterm.js instance for the console and for the interactive terminal.
InteractiveTerminalThe interactive terminal is the terminal where the user can interact with the terminal and run commands, supporting only commands for the Angular CLI.
The console displays the output for npm install and ng serve.
NodeRuntimeSandboxResponsible for managing the WebContainer instance and all communication with its API. This service handles:
ng CLI.NodeRuntimeStateManages the NodeRuntimeSandbox loading and error state.
EmbeddedTutorialManagerManages the tutorial assets, being responsible for fetching the tutorial source code and metadata.
The source code is mounted in the WebContainer filesystem by the NodeRuntimeSandbox.
The metadata is used to manage the project, handle the project changes and the user interactivity with the app.
This service also handles the reveal answer and reset reveal answer feature.
EditorUiStateManages the editor UI state, being responsible for handling the user interactions with the editor tabs, switching between the preview, the terminal and the console.
DownloadManagerResponsible for handling the download button in the embedded editor, fetching the tutorial project files and generating a zip file with the project content.
AlertManagerManage the alerts displayed in the embedded editor, being the out of memory alert when multiple tabs are opened, and unsupported environments alerts.
TypingsLoaderManages the types definitions for the code editor.