CHAPTER 1 OF 3
One button can call the document directly.
A small editor only appends text. Its button calls the document. This is a clear solution for one simple action.
Appearance
COMMAND / THE WALKTHROUGH
CHAPTER 1 OF 3
A small editor only appends text. Its button calls the document. This is a clear solution for one simple action.
Hello
Start with one direct edit.
Lesson control: Create a fresh TextDocument starting at Hello. This restarts the example; it does not undo an edit.
Append reads the current text, adds “, world!” and passes the result to write(). Restart example creates a new TextDocument, whose default text is “Hello”. There is no undo history in this chapter.
Equivalent TypeScript button handlers; UI rendering is omitted.
let document = new TextDocument(); // starts with "Hello"
function onAppendClick() {
document.write(document.text + ", world!");
}
// Lesson control: discard this example and create a fresh document.
function onRestartExampleClick() {
document = new TextDocument();
}export class TextDocument {
private value: string;
constructor(initial = "Hello") {
this.value = initial;
}
get text() {
return this.value;
}
write(text: string) {
this.value = text;
}
}Changing chapter starts a fresh example. Language and theme preserve progress.