Skip to content

Commit 87e69d0

Browse files
committed
docs: rest of tutorialkit api docs
1 parent 91197ed commit 87e69d0

File tree

4 files changed

+80
-60
lines changed

4 files changed

+80
-60
lines changed

docs/tutorialkit.dev/astro.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ export default defineConfig({
6868
link: '/guides/overriding-components/',
6969
},
7070
{
71-
label: 'How to',
72-
link: '/guides/how-to/',
71+
label: 'How to use TutorialKit API',
72+
link: '/guides/how-to-use-tutorialkit-api/',
7373
},
7474
],
7575
},

docs/tutorialkit.dev/src/content/docs/guides/how-to.mdx renamed to docs/tutorialkit.dev/src/content/docs/guides/how-to-use-tutorialkit-api.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: How to
2+
title: How to use TutorialKit API
33
description: "Examples showing how to utilize TutorialKit APIs"
44
---
55

docs/tutorialkit.dev/src/content/docs/reference/tutorialkit-api.mdx

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ description: Use TutorialKit's lower level APIs
44
---
55

66
TutorialKit exposes low level APIs that authors can utilize to provide highly custom experience in their tutorials.
7+
These APIs allow authors to control internal parts of TutorialKit. See [How to use TutorialKit API](/guides/how-to-use-tutorialkit-api/) guide for examples.
78

89
## Tutorial Store
910

@@ -19,11 +20,14 @@ This module may introduce breaking changes in patch and minor version updates. P
1920

2021
You can help us stabilize the API by providing feedback at [Stabilizing `tutorialkit:store` API | Github Discussions](https://github.com/stackblitz/tutorialkit/discussions).
2122
Please let us know how you are using this API.
23+
24+
TODO UPDATE THIS LINK
2225
:::
2326

2427
### Common types
2528

2629
- `ReadableAtom` from [`nanostores`](https://www.npmjs.com/package/nanostores)
30+
- `WebContainerProcess` from [`@webcontainer/api`](https://www.npmjs.com/package/@webcontainer/api)
2731

2832
### Properties
2933

@@ -159,8 +163,22 @@ Status of the webcontainer's booting.
159163
Type: `ReadableAtom<EditorDocuments>`
160164

161165
```ts
162-
// See type of `EditorDocument` above
166+
import type { FileDescriptor } from '@tutorialkit/types';
167+
163168
type EditorDocuments = Record<string, EditorDocument | undefined>
169+
170+
interface EditorDocument {
171+
value: string | Uint8Array;
172+
loading: boolean;
173+
filePath: string;
174+
type: FileDescriptor['type'];
175+
scroll?: ScrollPosition;
176+
}
177+
178+
interface ScrollPosition {
179+
top: number;
180+
left: number;
181+
}
164182
```
165183

166184
Files that are available in the editor.
@@ -178,16 +196,6 @@ type FileDescriptor = {
178196
179197
Paths of the files that are available in the lesson.
180198
181-
#### `template`
182-
183-
Type: `Files | undefined`
184-
185-
```ts
186-
type Files = Record<string, string | Uint8Array>
187-
```
188-
189-
Files of the template.
190-
191199
#### `selectedFile`
192200
193201
Type: `ReadableAtom<string | undefined>`
@@ -199,34 +207,7 @@ File that's currently selected in the file tree.
199207
Type: `Readonly<Lesson> | undefined`
200208
201209
```ts
202-
interface Lesson<T = unknown> {
203-
id: string;
204-
order: number;
205-
data: LessonSchema;
206-
part: {
207-
id: string;
208-
title: string;
209-
};
210-
chapter: {
211-
id: string;
212-
title: string;
213-
};
214-
slug: string;
215-
filepath: string;
216-
editPageLink?: string;
217-
files: FilesRefList;
218-
solution: FilesRefList;
219-
next?: LessonLink;
220-
prev?: LessonLink;
221-
Markdown: T;
222-
}
223-
224-
interface LessonLink {
225-
href: string;
226-
title: string;
227-
}
228-
229-
type FilesRefList = [ref: string, files: string[]]
210+
import type { Lesson } from '@tutorialkit/types';
230211
```
231212

232213
Currently active lesson.
@@ -273,54 +254,94 @@ Unlock webcontainer's boot process if it was in `'blocked'` state.
273254

274255
Type: `() => void`
275256

257+
Reset changed files back to lesson's initial state.
258+
276259
#### `solve`
277260

278261
Type: `() => void`
279262

263+
Apply lesson solution into the lesson files.
264+
280265
#### `setSelectedFile`
281266

282267
Type: `(filePath: string | undefined) => void`
283268

269+
Set file from file tree as selected.
270+
284271
#### `addFile`
285272

286273
Type: `(filePath: string) => Promise<void>`
287274

275+
Add new file to file tree.
276+
Throws error with message `FILE_EXISTS` if file exists already on file system.
277+
288278
#### `addFolder`
289279

290280
Type: `(folderPath: string) => Promise<void>`
291281

282+
Add new file to file tree.
283+
Throws error with message `FOLDER_EXISTS` if folder exists already on file system.
284+
292285
#### `updateFile`
293286

294287
Type: `(filePath: string, content: string) => void`
295288

296-
#### `updateFiles`
297-
298-
Type: `(files: Files) => void`
289+
Update contents of file.
299290

300291
#### `setCurrentDocumentContent`
301292

302293
Type: `(newContent: string) => void`
303294

295+
Update content of the active file.
296+
304297
#### `setCurrentDocumentScrollPosition`
305298

306299
Type: `(position: ScrollPosition) => void`
307300

301+
```ts
302+
interface ScrollPosition {
303+
top: number;
304+
left: number;
305+
}
306+
```
307+
308+
Update scroll position of the file in editor.
309+
308310
#### `onTerminalResize`
309311

310312
Type: `(cols: number, rows: number) => void`
311313

314+
Callback that should be called when terminal resizes.
315+
312316
#### `onDocumentChanged`
313317

314318
Type: `(filePath: string, callback: (document: Readonly<EditorDocument>) => void) => () => void`
315319

316-
#### `refreshStyles`
320+
```ts
321+
import type { FileDescriptor } from '@tutorialkit/types';
317322

318-
Type: `() => void`
323+
interface EditorDocument {
324+
value: string | Uint8Array;
325+
loading: boolean;
326+
filePath: string;
327+
type: FileDescriptor['type'];
328+
scroll?: ScrollPosition;
329+
}
330+
331+
interface ScrollPosition {
332+
top: number;
333+
left: number;
334+
}
335+
```
336+
337+
Listen for file changes made in the editor.
319338

320339
#### `takeSnapshot`
321340

322341
Type: `() => { files: Record<string, string> }`
323342

343+
Take snapshot of the current state of the lesson.
344+
324345
## Tutorial Core
325346

326347
You can access Tutorial Core by importing the `tutorialkit:core` entrypoint.

packages/runtime/src/store/index.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,6 @@ export class TutorialStore {
227227
return this._editorStore.files;
228228
}
229229

230-
/** Files of the template */
231-
get template(): Files | undefined {
232-
return this._lessonTemplate;
233-
}
234-
235230
/** File that's currently selected in the file tree */
236231
get selectedFile(): ReadableAtom<string | undefined> {
237232
return this._editorStore.selectedFile;
@@ -300,10 +295,12 @@ export class TutorialStore {
300295
return !!this._lesson && Object.keys(this._lesson.solution[1]).length >= 1;
301296
}
302297

298+
/** Unlock webcontainer's boot process if it was in `'blocked'` state */
303299
unblockBoot() {
304300
unblockBoot();
305301
}
306302

303+
/** Reset changed files back to lesson's initial state */
307304
reset() {
308305
const isReady = this.lessonFullyLoaded.value;
309306

@@ -315,6 +312,7 @@ export class TutorialStore {
315312
this._runner.updateFiles(this._lessonFiles);
316313
}
317314

315+
/** Apply lesson solution into the lesson files */
318316
solve() {
319317
const isReady = this.lessonFullyLoaded.value;
320318

@@ -328,10 +326,12 @@ export class TutorialStore {
328326
this._runner.updateFiles(files);
329327
}
330328

329+
/** Set file from file tree as selected */
331330
setSelectedFile(filePath: string | undefined) {
332331
this._editorStore.setSelectedFile(filePath);
333332
}
334333

334+
/** Add new file to file tree */
335335
async addFile(filePath: string): Promise<void> {
336336
// always select the existing or newly created file
337337
this.setSelectedFile(filePath);
@@ -349,6 +349,7 @@ export class TutorialStore {
349349
this._runner.updateFile(filePath, '');
350350
}
351351

352+
/** Add new folder to file tree */
352353
async addFolder(folderPath: string) {
353354
// prevent creating duplicates
354355
if (this._editorStore.files.get().some((file) => file.path.startsWith(folderPath))) {
@@ -363,6 +364,7 @@ export class TutorialStore {
363364
this._runner.createFolder(folderPath);
364365
}
365366

367+
/** Update contents of file */
366368
updateFile(filePath: string, content: string) {
367369
const hasChanged = this._editorStore.updateFile(filePath, content);
368370

@@ -371,10 +373,7 @@ export class TutorialStore {
371373
}
372374
}
373375

374-
updateFiles(files: Files) {
375-
this._runner.updateFiles(files);
376-
}
377-
376+
/** Update content of the active file */
378377
setCurrentDocumentContent(newContent: string) {
379378
const filePath = this.currentDocument.get()?.filePath;
380379

@@ -385,6 +384,7 @@ export class TutorialStore {
385384
this.updateFile(filePath, newContent);
386385
}
387386

387+
/** Update scroll position of the file in editor */
388388
setCurrentDocumentScrollPosition(position: ScrollPosition) {
389389
const editorDocument = this.currentDocument.get();
390390

@@ -402,21 +402,20 @@ export class TutorialStore {
402402
this._terminalStore.attachTerminal(id, terminal);
403403
}
404404

405+
/** Callback that should be called when terminal resizes */
405406
onTerminalResize(cols: number, rows: number) {
406407
if (cols && rows) {
407408
this._terminalStore.onTerminalResize(cols, rows);
408409
this._runner.onTerminalResize(cols, rows);
409410
}
410411
}
411412

413+
/** Listen for file changes made in the editor */
412414
onDocumentChanged(filePath: string, callback: (document: Readonly<EditorDocument>) => void) {
413415
return this._editorStore.onDocumentChanged(filePath, callback);
414416
}
415417

416-
refreshStyles() {
417-
this._themeRef.set(this._themeRef.get() + 1);
418-
}
419-
418+
/** Take snapshot of the current state of the lesson */
420419
takeSnapshot() {
421420
return this._runner.takeSnapshot();
422421
}

0 commit comments

Comments
 (0)