@@ -188,15 +188,48 @@ These attributes are actions that will be executed when a ``pause`` or ``step``
188
188
Custom scripts
189
189
--------------
190
190
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.
192
192
193
193
.. code-block :: markdown
194
194
195
- {exec-at-unpause}
195
+ {exec-at-unpause pause }
196
196
```slip-script
197
197
let elem = document.querySelector("#id")
198
198
let old_value = elem.style.opacity;
199
199
elem.style.opacity = "1";
200
200
return {undo : () => { elem.style.opacity = old_value }}
201
201
```
202
202
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
+ ```
0 commit comments