Skip to content

Commit ba9627b

Browse files
committed
Update doc and tests with scripts state and onUndo
1 parent e8351e7 commit ba9627b

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

docs/syntax.rst

+35-2
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,48 @@ These attributes are actions that will be executed when a ``pause`` or ``step``
188188
Custom scripts
189189
--------------
190190

191-
Use a slipscript code block to add a script, and ``exec-at-unpause`` to execute it. The script should return the function to undo the change.
191+
Use a slipscript code block to add a script, and ``exec-at-unpause`` to execute it. The script can return the function to undo the change.
192192

193193
.. code-block:: markdown
194194
195-
{exec-at-unpause}
195+
{exec-at-unpause pause}
196196
```slip-script
197197
let elem = document.querySelector("#id")
198198
let old_value = elem.style.opacity;
199199
elem.style.opacity = "1";
200200
return {undo : () => { elem.style.opacity = old_value }}
201201
```
202202
203+
Alternatively, the script can use ``slip.onUndo`` to register a callback to run
204+
during undo. Multiple callback can be registered and will be called in the right
205+
order (that is, the reverse order in which they were registered).
206+
207+
.. code-block:: markdown
208+
209+
{exec-at-unpause pause}
210+
```slip-script
211+
let set_class = (elem, name, value) => {
212+
let old_value = elem.style[name];
213+
elem.style[name] = value;
214+
slip.onUndo(() => {elem.style[name] = old_value})
215+
}
216+
let elem = document.querySelector("#id");
217+
set_class(elem, "backgroundColor", "yellow");
218+
set_class(elem, "color", "red");
219+
```
220+
221+
222+
Scripts have access to the ``slip.state`` object, which will be persisted from
223+
scripts to scripts.
224+
225+
.. code-block:: markdown
226+
227+
{exec-at-unpause pause}
228+
```slip-script
229+
slip.state.x = 1;
230+
```
231+
232+
{exec-at-unpause pause}
233+
```slip-script
234+
console.log(slip.state.x); // 1
235+
```

test/engine/basic.t/new_engine.md

+26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
Salut !
22

3+
{#id}
4+
Hello
5+
6+
{exec-at-unpause pause}
7+
```slip-script
8+
let set_class = (elem, name, value) => {
9+
let old_value = elem.style[name];
10+
elem.style[name] = value;
11+
slip.onUndo(() => {elem.style[name] = old_value})
12+
}
13+
let elem = document.querySelector("#id");
14+
set_class(elem, "backgroundColor", "yellow");
15+
set_class(elem, "color", "red");
16+
```
17+
18+
{exec-at-unpause pause}
19+
```slip-script
20+
console.log("setting state to 1");
21+
slip.state.one = 1
22+
```
23+
24+
{exec-at-unpause pause}
25+
```slip-script
26+
console.log("state is", slip.state.one);
27+
```
28+
329
{#a .unstatic}
430
a
531

0 commit comments

Comments
 (0)