Skip to content

Commit 24be44c

Browse files
committed
Implement 'chain'
1 parent 1c4a8c5 commit 24be44c

File tree

6 files changed

+265
-72
lines changed

6 files changed

+265
-72
lines changed

dist/plugins/iwsy.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,17 @@ const EasyCoder_IWSY = {
5050
});
5151
return true;
5252
}
53-
return false;
53+
return false;
54+
case `path`:
55+
const path = compiler.getNextValue();
56+
compiler.addCommand({
57+
domain: `iwsy`,
58+
keyword: `iwsy`,
59+
lino,
60+
action,
61+
path
62+
});
63+
return true;
5464
case `script`:
5565
const script = compiler.getNextValue();
5666
compiler.addCommand({
@@ -141,7 +151,6 @@ const EasyCoder_IWSY = {
141151
run: (program) => {
142152
const command = program[program.pc];
143153
const action = command.action;
144-
let scriptRecord;
145154
let script;
146155
switch (action) {
147156
case `init`:
@@ -167,6 +176,11 @@ const EasyCoder_IWSY = {
167176
}
168177
EasyCoder.iwsyFunctions = IWSY(player, script);
169178
break;
179+
case `path`:
180+
if (EasyCoder.iwsyFunctions) {
181+
EasyCoder.iwsyFunctions.setPath(program.getValue(command.path));
182+
}
183+
break;
170184
case `script`:
171185
script = program.getValue(command.script);
172186
try {
@@ -242,19 +256,33 @@ const EasyCoder_IWSY = {
242256

243257
compile: (compiler) => {
244258
if (compiler.tokenIs(`the`)) {
245-
if (compiler.nextTokenIs(`step`)) {
246-
compiler.next();
247-
return {
248-
domain: `iwsy`,
249-
type: `step`
250-
};
259+
if (compiler.nextTokenIs(`iwsy`)) {
260+
const type = compiler.nextToken();
261+
if ([`script`, `step`].includes(type)) {
262+
compiler.next();
263+
return {
264+
domain: `iwsy`,
265+
type
266+
};
267+
}
251268
}
252269
}
253270
return null;
254271
},
255272

256273
get: (program, value) => {
257274
switch (value.type) {
275+
case `script`:
276+
let script = null;
277+
if (EasyCoder.iwsyFunctions) {
278+
script = EasyCoder.iwsyFunctions.getScript();
279+
return {
280+
type: `constant`,
281+
numeric: false,
282+
content: JSON.stringify(script)
283+
}
284+
}
285+
break;
258286
case `step`:
259287
return {
260288
type: `constant`,

iwsy/iwsy.js

Lines changed: 86 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// IWSY
22

3-
const IWSY = (playerElement, text) => {
3+
const IWSY = (playerElement, scriptObject) => {
44

55
let player = playerElement;
6-
let script = text;
6+
let script = scriptObject;
7+
let homeScript;
8+
let afterRun;
9+
let path;
710

811
// Set up all the blocks
912
const setupBlocks = () => {
@@ -281,7 +284,7 @@ const IWSY = (playerElement, text) => {
281284
step.next();
282285
} else {
283286
const element = document.createElement(`div`);
284-
block.element.parentElement.appendChild(element);
287+
player.appendChild(element);
285288
element.style.position = `absolute`;
286289
element.style.opacity = `0.0`;
287290
element.style.left = block.element.style.left;
@@ -480,7 +483,12 @@ const IWSY = (playerElement, text) => {
480483
const interval = setInterval(() => {
481484
if (animStep < animSteps) {
482485
const ratio = 0.5 - Math.cos(Math.PI * animStep / animSteps) / 2;
483-
doTransitionStep(block, target, ratio);
486+
try {
487+
doTransitionStep(block, target, ratio);
488+
} catch (err) {
489+
clearInterval(interval);
490+
throw Error(err);
491+
}
484492
animStep++;
485493
} else {
486494
clearInterval(interval);
@@ -502,10 +510,12 @@ const IWSY = (playerElement, text) => {
502510

503511
// Remove all the blocks from the player
504512
const removeBlocks = () => {
505-
for (const block of script.blocks) {
506-
if (block.element) {
507-
removeElement(block.element);
508-
delete(block.element);
513+
if (Array.isArray(script.blocks)) {
514+
for (const block of script.blocks) {
515+
if (block.element) {
516+
removeElement(block.element);
517+
delete(block.element);
518+
}
509519
}
510520
}
511521
};
@@ -515,8 +525,6 @@ const IWSY = (playerElement, text) => {
515525
const parent = element.parentElement;
516526
if (parent) {
517527
parent.removeChild(element);
518-
} else {
519-
throw Error(`element has no parent`);
520528
}
521529
element.remove();
522530
};
@@ -545,7 +553,6 @@ const IWSY = (playerElement, text) => {
545553
document.title = step.title;
546554
}
547555
if (step.css) {
548-
console.log(`Set styles at ${step.index}`);
549556
setHeadStyle(step.css.split(`%0a`).join(`\n`));
550557
}
551558
const aspect = step[`aspect ratio`];
@@ -589,22 +596,17 @@ const IWSY = (playerElement, text) => {
589596
}
590597
};
591598

592-
// Chain to another presentation
593-
const chain = step => {
594-
step.next();
595-
};
596-
597-
// Embed another presentation
599+
// Embed another script
598600
const embed = step => {
599601
step.next();
600602
};
601603

602604
// Restore the cursor
603605
const restoreCursor = () => {
604606
player.style.cursor = `pointer`;
605-
if (script.then) {
606-
script.then();
607-
script.then = null;
607+
script = homeScript;
608+
if (afterRun) {
609+
afterRun();
608610
}
609611
};
610612

@@ -821,7 +823,6 @@ const IWSY = (playerElement, text) => {
821823
transition,
822824
goto,
823825
load,
824-
chain,
825826
embed
826827
};
827828

@@ -843,27 +844,68 @@ const IWSY = (playerElement, text) => {
843844
}
844845
}
845846
}
847+
848+
const onStepCB = script.onStepCB;
849+
if (step.action === `chain`) {
850+
const runMode = script.runMode;
851+
fetch(`${path}${step.script}`)
852+
.then(response => {
853+
if (response.status >= 400) {
854+
throw Error(`Unable to load ${step.script}: ${response.status}`)
855+
}
856+
response.json().then(data => {
857+
script = data;
858+
if (onStepCB) {
859+
onStepCB(-1);
860+
}
861+
initScript();
862+
script.runMode = runMode;
863+
doStep(script.steps[1]);
864+
});
865+
})
866+
.catch(err => {
867+
throw Error(`Fetch Error :${err}`);
868+
});
869+
return;
870+
}
871+
846872
const actionName = step.action.split(` `).join(``);
847873
let handler = actions[actionName];
848-
if (typeof handler === `undefined`) {
849-
handler = IWSY.plugins[actionName];
874+
if (script.runMode === `auto`) {
850875
if (typeof handler === `undefined`) {
851-
throw Error(`Unknown action: '${step.action}'`);
876+
handler = IWSY.plugins[actionName];
877+
if (typeof handler === `undefined`) {
878+
throw Error(`Unknown action: '${step.action}'`);
879+
}
880+
}
881+
if (onStepCB) {
882+
onStepCB(step.index);
883+
}
884+
try {
885+
handler(step);
886+
} catch (err) {
887+
console.log(`Step ${step.index} (${step.action}): ${err}`);
888+
alert(`Step ${step.index} (${step.action}): ${err}`);
889+
}
890+
} else {
891+
try {
892+
handler(step);
893+
} catch (err) {
894+
console.log(JSON.stringify(step,0,2) + `\n` + JSON.stringify(handler,0,2));
895+
console.log(`Step ${step.index} (${step.action}): ${err}`);
896+
alert(`Step ${step.index} (${step.action}): ${err}`);
852897
}
853898
}
854-
if (script.onStepCB && script.runMode === `auto`) {
855-
script.onStepCB(step.index);
856-
}
857-
try {
858-
handler(step);
859-
} catch (err) {
860-
console.log(`Step ${step.index} (${step.action}): ${err}`);
861-
alert(`Step ${step.index} (${step.action}): ${err}`);
862-
}
899+
863900
};
864901

865902
///////////////////////////////////////////////////////////////////////////////
866903
// These are all the exported functions
904+
905+
// Get the script
906+
const getScript = () => {
907+
return script;
908+
};
867909

868910
// Set the script
869911
const setScript = newScript => {
@@ -872,12 +914,17 @@ const IWSY = (playerElement, text) => {
872914
initScript();
873915
};
874916

917+
// Set the path
918+
const setPath = p => {
919+
path = p;
920+
};
921+
875922
// Go to a specified step number
876-
const gotoStep = (target) => {
923+
const gotoStep = target => {
877924
script.scanTarget = target;
878925
script.runMode = `manual`;
879926
scan();
880-
};
927+
};
881928

882929
// Show a block
883930
const block = blockIndex => {
@@ -936,7 +983,8 @@ const IWSY = (playerElement, text) => {
936983

937984
// Run the presentation
938985
const run = (mode, startMode, then) => {
939-
script.then = then;
986+
homeScript = JSON.parse(JSON.stringify(script));
987+
afterRun = then;
940988
initScript();
941989
if (mode === `fullscreen`) {
942990
if (document.fullscreenElement) {
@@ -996,7 +1044,9 @@ const IWSY = (playerElement, text) => {
9961044
setupShowdown();
9971045
initScript();
9981046
return {
1047+
getScript,
9991048
setScript,
1049+
setPath,
10001050
gotoStep,
10011051
block,
10021052
run,

iwsy/resources/ecs/content.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,8 @@ Restart:
197197
on click DeleteItem
198198
begin
199199
put the index of DeleteItem into N
200-
put element N of ItemNames into ItemName
201200
put property `content` of Presentation into Items
202-
json delete property ItemName of Items
201+
json delete element N of Items
203202
set property `content` of Presentation to Items
204203
put -1 into SelectedItem
205204
go to Restart

0 commit comments

Comments
 (0)