virtual-code-block
is a TypeScript class that simulates a code editor environment with features like cursor navigation, text insertion, and line manipulation. It provides a flexible interface for applying various code editing actions such as typing, moving the cursor, and executing commands. This lightweight and versatile library is ideal for building educational tools, code playgrounds, and interactive coding environments within web applications.
This library heavily relys on the types from codevideo-types
import { VirtualCodeBlock } from '@fullstackcraftllc/virtual-code-block';
// Initialize a VirtualCodeBlock instance with initial code lines
const initialCodeLines = [
'function greet(name) {',
' return "Hello, " + name + "!";',
'}'
];
const virtualCodeBlock = new VirtualCodeBlock(initialCodeLines);
// Apply code editing actions
virtualCodeBlock.applyActions([
{ name: 'arrow-down', value: '1' }, // Move cursor down one time
{ name: 'arrow-right', value: '13' }, // Move cursor right 13 times
{ name: 'type-editor', value: 'world' }, // Type 'world'
{ name: 'space', value: '1' }, // Insert space
{ name: 'type-text', value: '😊' } // Type emoji
]);
// Get the final code and actions applied
const finalCode = virtualCodeBlock.getCode();
const actionsApplied = virtualCodeBlock.getActionsApplied();
// Log the final code and actions applied
console.log('Final code:');
console.log(finalCode);
console.log('Actions applied:');
console.log(actionsApplied);
Apply a single action to the code.
Apply a series of actions to the code. Returns the final code as a string.
Get the current code lines.
Get the current caret position.
Get the actions that were applied to the code.
Get the code as a single string.
Get the history of code lines.
Get the code after each step.
Get the editor state after each step.
getDataForAnnotatedFrames(): Array<{ actionApplied: IAction; code: string; caretPosition: { row: number; col: number }; speechCaptions: Array<ISpeechCaption>; }>
Get data for annotated frames.
Why do we need a seemingly useless class? This library, along with syntax-spy
, are used to validate steps across the CodeVideo ecosystem. This is a small part of a larger project to create a declarative way to build step by step educational software courses.
See more at codevideo.io