Skip to content
This repository was archived by the owner on Feb 22, 2022. It is now read-only.

Commit 826b0ab

Browse files
committed
added callback hooks for story start and finish
1 parent f8876c7 commit 826b0ab

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ Your `playerOptions` should be an object with the following properties:
4040
- `html` (optional): Set this to true if your ficdown file contains raw html that you want rendered. By default html will not be rendered.
4141
- `startText` (optional): Set this to override the link text that begins the game.
4242
- `endMarkdown` (optional): Set this to override the "game over" content that is displayed when the player reaches a scene with no more links to follow. Include an anchor with the href set to `restart()` if you want a link that will start the story over from the beginning.
43+
- `start` (optional): Javascript callback function to execute when the user clicks the first link to begin the story.
44+
- `finish` (optional): Javascript callback function to execute when the story has finished.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ficdown.js",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"description": "A parser and player for Interactive Fiction written in Ficdown",
55
"scripts": {
66
"build": "rm -rf ./build && tsc",

src/Model/PlayerOptions.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ export type PlayerOptions = {
55
html?: boolean, // allow html in story source
66
startText?: string, // custom link text to start game
77
endMarkdown?: string, // custom markdown when game ends
8+
start?: () => void, // callback when the game starts
9+
finish?: () => void, // callback when the game finishes
810
}

src/Player.ts

+14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export class Player {
2121
private startText: string;
2222
private endMarkdown: string;
2323

24+
private startCallback?: () => void;
25+
private finishCallback?: () => void;
26+
private started: boolean = false;
27+
2428
constructor(private options: PlayerOptions) {
2529
this.converter = new Markdown({
2630
html: options.html,
@@ -38,6 +42,9 @@ export class Player {
3842
}
3943
this.container = $(`#${ options.id }`);
4044
this.container.addClass('ficdown').data('player', this);
45+
46+
this.startCallback = options.start;
47+
this.finishCallback = options.finish;
4148
}
4249

4350
public play(): void {
@@ -47,6 +54,10 @@ export class Player {
4754
}
4855

4956
public handleHref(href: string): false {
57+
if (this.startCallback && !this.started) {
58+
this.started = true;
59+
this.startCallback();
60+
}
5061
const match = Util.matchHref(href);
5162
let matchedScene: Scene | undefined = undefined;
5263
const actions: Action[] = [];
@@ -182,6 +193,9 @@ export class Player {
182193
player.play();
183194
return false;
184195
});
196+
if (this.finishCallback) {
197+
this.finishCallback();
198+
}
185199
}
186200
}
187201
}

0 commit comments

Comments
 (0)