Skip to content

Commit cb62a2d

Browse files
authored
Merge branch 'master' into issue/rename-inklecate.js
2 parents 101af14 + 6a590f2 commit cb62a2d

36 files changed

+1671
-1437
lines changed

README.md

+22-17
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ If you frequently need to update your story, pasting the content into `story.js`
4242
Once the server is running, use the [other boilerplate](https://github.com/y-lohse/inkjs/blob/master/templates/browser_with_server) and place your story content inside `story.json`. Behind the scenes, the only difference is that we load the JSON file via ajax before creating the story:
4343

4444
```javascript
45-
fetch("story.json")
46-
.then(function (response) {
47-
return response.text();
48-
})
49-
.then(function (storyContent) {
50-
story = new inkjs.Story(storyContent);
51-
continueStory();
52-
});
45+
fetch('story.json')
46+
.then(function (response) {
47+
return response.text();
48+
})
49+
.then(function (storyContent) {
50+
story = new inkjs.Story(storyContent);
51+
continueStory();
52+
});
5353
```
5454

5555
## Using node.js
@@ -65,14 +65,14 @@ Require the module: `var Story = require('inkjs').Story;`.
6565
You can load the json file using a simple call to `require`:
6666

6767
```javascript
68-
var json = require("./ink_file.json");
68+
var json = require('./ink_file.json');
6969
```
7070

7171
You can also load it using `fs`. In that case, please note that inklecate outputs a json file encoded **with** BOM, and node isn't very good at handling that.
7272

7373
```javascript
74-
var fs = require("fs");
75-
var json = fs.readFileSync("./ink_file.json", "UTF-8").replace(/^\uFEFF/, ""); //strips the BOM
74+
var fs = require('fs');
75+
var json = fs.readFileSync('./ink_file.json', 'UTF-8').replace(/^\uFEFF/, ''); //strips the BOM
7676
```
7777

7878
### Starting a story
@@ -92,15 +92,15 @@ From there on, you can follow [the official documentation](https://github.com/in
9292

9393
There are a few very minor API differences between ink C# and inkjs:
9494

95-
### [Getting and setting ink variables](https://github.com/inkle/ink/blob/master/Documentation/RunningYourInk.md#settinggetting-ink-variables).
95+
### [Getting and setting ink variables](https://github.com/inkle/ink/blob/master/Documentation/RunningYourInk.md#settinggetting-ink-variables)
9696

9797
On platforms that do not support [ES2015 Proxies](https://kangax.github.io/compat-table/es6/) (basically node.js v5, IE 11, Safari 9 and everything below), you can't directly read and write variables to the story state. Instead you will have to use the `$` function:
9898

9999
```javascript
100-
_inkStory.variablesState.$("player_health", 100);
100+
_inkStory.variablesState.$('player_health', 100);
101101
//instead of _inkStory.variablesState["player_health"] = 100;
102102

103-
var health = _inkStory.variablesState.$("player_health");
103+
var health = _inkStory.variablesState.$('player_health');
104104
//instead of var health = _inkStory.variablesState["player_health"];
105105
```
106106

@@ -109,14 +109,14 @@ var health = _inkStory.variablesState.$("player_health");
109109
`EvaluateFunction()` lets you evaluate an ink function from within your javascript. The "normal" call is the same than in C#:
110110

111111
```javascript
112-
var result = EvaluateFunction("my_ink_function", ["arg1", "arg2"]);
112+
var result = EvaluateFunction('my_ink_function', ['arg1', 'arg2']);
113113
//result is the return value of my_ink_function("arg1", "arg2")
114114
```
115115

116116
However, if you also wish to retrieve the text that `my_ink_function` output, you need to call it like this:
117117

118118
```javascript
119-
var result = EvaluateFunction("my_ink_function", ["arg1", "arg2"], true);
119+
var result = EvaluateFunction('my_ink_function', ['arg1', 'arg2'], true);
120120
//now result is an object with two properties:
121121
// result.returned is the return value of my_ink_function("arg1", "arg2")
122122
// result.output is the text that was written to the output while the function was evaluated
@@ -125,6 +125,7 @@ var result = EvaluateFunction("my_ink_function", ["arg1", "arg2"], true);
125125
## Compiler
126126

127127
### inkjs-compiler.js
128+
128129
```shell
129130
$ node inkjs-compiler.js -h
130131

@@ -137,15 +138,19 @@ Usage: inkjs-compiler <options> <ink file>
137138
```
138139

139140
### online compiler
141+
140142
```javascript
141-
const story = (new inkjs.Compiler(`Hello World`)).Compile()
143+
const story = new inkjs.Compiler(`Hello World`).Compile();
142144
// story is an inkjs.Story that can be played right away
143145

144146
const jsonBytecode = story.ToJson();
145147
// the generated json can be further re-used
146148
```
147149

150+
You can use this in combination with [Webpack and TypeScript](docs/working-with-typescript-and-webpack.md).
151+
148152
### Differences with the C# Compiler
153+
149154
See [Differences with the C# Compiler](docs/compiler-differences.md).
150155

151156
## Compatibility table

docs/compiler-differences.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
The C# compiler is intented to always be used on a file system and thus the question of how files are included follow a classic pattern.
66
Nevertheless, when using the compiler inside a browser, the concept of "file" is a blurry one.
77
Inkjs provides 2 file handlers :
8-
* A POSIX file handler : similar to the one used in the C# compiler that will look for files in folders
9-
* A JSON file handler : expects a JSON object of the form
8+
* A JSON file handler : it is included by default : it expects a JSON object representing all the files of the project of the form :
109
```
1110
{
1211
"filename1.ink": "INCLUDE filename2.ink",
1312
"filename2.ink": "This content is included",
1413
}
1514
```
1615

16+
* A POSIX file handler : delivered as a separate `inkjs-full-posixhandler.js` file that must be included/required : similar to the one used in the C# compiler that will look for files in folders.
17+
1718
## Float and ints
1819

1920
As the JSON format and javascript in general do not differentiate between float and integers, the inkjs runtime is known to behave differently from the C# runtime when dealing with floating point operations.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Working with TypeScript and Webpack
2+
3+
Version 2.1.0 introduces support for handling ink files directly without having to convert them to JSON first.
4+
5+
This guide explains how to adjust projects using TypeScript and Webpack (common when working with React) to make use of this.
6+
7+
## Webpack
8+
9+
In Webpack 5, static resources are loaded with [Asset Modules](https://webpack.js.org/guides/asset-modules/).
10+
11+
Here we load `.ink` files and inline their content into the script. This is most useful for programs which unconditionally use a single Ink file.
12+
13+
```javascript
14+
rules: [
15+
{
16+
test: /\.ink$/i,
17+
type: 'asset/source',
18+
},
19+
// ... and then the rest of your rules
20+
];
21+
```
22+
23+
We can then `import` (or `require`) the ink file and compile it.
24+
25+
```typescript
26+
import * as Inkjs from 'inkjs';
27+
import data from '../assets/myStory.ink';
28+
29+
const inkStory = new Inkjs.Compiler(data).Compile();
30+
```
31+
32+
If you are working in JavaScript, then that is all you need.
33+
34+
### TypeScript
35+
36+
If you are working in TypeScript then you will probably see an error message like:
37+
38+
> Cannot find module '../assets/myStory.ink' or its corresponding type declarations.
39+
40+
You need to declare your types for the `.ink` file. This is done using a [Declaration File](https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html).
41+
42+
Create one, such as `global.d.ts` in the top level of your source code, which reads like this:
43+
44+
```typescript
45+
declare module '*.ink' {
46+
const value: string;
47+
export default value;
48+
}
49+
```
50+
51+
This says that when you import an `.ink` file, you will get a string.
52+
53+
This should work, but you might need to adjust your `tsconfig.json` file to tell it where to find `global.d.ts`:
54+
55+
```json
56+
{
57+
compiler: {
58+
...
59+
},
60+
include: ["path/to/global.d.ts"]
61+
}
62+
```
63+
64+
Credit to [felixmosh's Stackoverflow answer](https://stackoverflow.com/a/66176397/19068) for providing my reference for this TypeScript.

ink.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { Story, InkList } from './engine/Story'
22
import { Compiler } from './compiler/Compiler'
33
import { CompilerOptions } from './compiler/CompilerOptions'
4+
import { PosixFileHandler } from './compiler/FileHandler/PosixFileHandler'
5+
import { JsonFileHandler } from './compiler/FileHandler/JsonFileHandler'
46

57
declare interface Inkjs {
68
Story: typeof Story
79
InkList: typeof InkList
810
Compiler: typeof Compiler
911
CompilerOptions: typeof CompilerOptions
12+
PosixFileHandler: typeof PosixFileHandler
13+
JsonFileHandler: typeof JsonFileHandler
1014
}
1115

1216
declare let inkjs: Inkjs

0 commit comments

Comments
 (0)