Skip to content

Implement 'chain' #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions dist/plugins/iwsy.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,17 @@ const EasyCoder_IWSY = {
});
return true;
}
return false;
return false;
case `path`:
const path = compiler.getNextValue();
compiler.addCommand({
domain: `iwsy`,
keyword: `iwsy`,
lino,
action,
path
});
return true;
case `script`:
const script = compiler.getNextValue();
compiler.addCommand({
Expand Down Expand Up @@ -141,7 +151,6 @@ const EasyCoder_IWSY = {
run: (program) => {
const command = program[program.pc];
const action = command.action;
let scriptRecord;
let script;
switch (action) {
case `init`:
Expand All @@ -167,6 +176,11 @@ const EasyCoder_IWSY = {
}
EasyCoder.iwsyFunctions = IWSY(player, script);
break;
case `path`:
if (EasyCoder.iwsyFunctions) {
EasyCoder.iwsyFunctions.setPath(program.getValue(command.path));
}
break;
case `script`:
script = program.getValue(command.script);
try {
Expand Down Expand Up @@ -242,19 +256,33 @@ const EasyCoder_IWSY = {

compile: (compiler) => {
if (compiler.tokenIs(`the`)) {
if (compiler.nextTokenIs(`step`)) {
compiler.next();
return {
domain: `iwsy`,
type: `step`
};
if (compiler.nextTokenIs(`iwsy`)) {
const type = compiler.nextToken();
if ([`script`, `step`].includes(type)) {
compiler.next();
return {
domain: `iwsy`,
type
};
}
}
}
return null;
},

get: (program, value) => {
switch (value.type) {
case `script`:
let script = null;
if (EasyCoder.iwsyFunctions) {
script = EasyCoder.iwsyFunctions.getScript();
return {
type: `constant`,
numeric: false,
content: JSON.stringify(script)
}
}
break;
case `step`:
return {
type: `constant`,
Expand Down
122 changes: 86 additions & 36 deletions iwsy/iwsy.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// IWSY

const IWSY = (playerElement, text) => {
const IWSY = (playerElement, scriptObject) => {

let player = playerElement;
let script = text;
let script = scriptObject;
let homeScript;
let afterRun;
let path;

// Set up all the blocks
const setupBlocks = () => {
Expand Down Expand Up @@ -281,7 +284,7 @@ const IWSY = (playerElement, text) => {
step.next();
} else {
const element = document.createElement(`div`);
block.element.parentElement.appendChild(element);
player.appendChild(element);
element.style.position = `absolute`;
element.style.opacity = `0.0`;
element.style.left = block.element.style.left;
Expand Down Expand Up @@ -480,7 +483,12 @@ const IWSY = (playerElement, text) => {
const interval = setInterval(() => {
if (animStep < animSteps) {
const ratio = 0.5 - Math.cos(Math.PI * animStep / animSteps) / 2;
doTransitionStep(block, target, ratio);
try {
doTransitionStep(block, target, ratio);
} catch (err) {
clearInterval(interval);
throw Error(err);
}
animStep++;
} else {
clearInterval(interval);
Expand All @@ -502,10 +510,12 @@ const IWSY = (playerElement, text) => {

// Remove all the blocks from the player
const removeBlocks = () => {
for (const block of script.blocks) {
if (block.element) {
removeElement(block.element);
delete(block.element);
if (Array.isArray(script.blocks)) {
for (const block of script.blocks) {
if (block.element) {
removeElement(block.element);
delete(block.element);
}
}
}
};
Expand All @@ -515,8 +525,6 @@ const IWSY = (playerElement, text) => {
const parent = element.parentElement;
if (parent) {
parent.removeChild(element);
} else {
throw Error(`element has no parent`);
}
element.remove();
};
Expand Down Expand Up @@ -545,7 +553,6 @@ const IWSY = (playerElement, text) => {
document.title = step.title;
}
if (step.css) {
console.log(`Set styles at ${step.index}`);
setHeadStyle(step.css.split(`%0a`).join(`\n`));
}
const aspect = step[`aspect ratio`];
Expand Down Expand Up @@ -589,22 +596,17 @@ const IWSY = (playerElement, text) => {
}
};

// Chain to another presentation
const chain = step => {
step.next();
};

// Embed another presentation
// Embed another script
const embed = step => {
step.next();
};

// Restore the cursor
const restoreCursor = () => {
player.style.cursor = `pointer`;
if (script.then) {
script.then();
script.then = null;
script = homeScript;
if (afterRun) {
afterRun();
}
};

Expand Down Expand Up @@ -821,7 +823,6 @@ const IWSY = (playerElement, text) => {
transition,
goto,
load,
chain,
embed
};

Expand All @@ -843,27 +844,68 @@ const IWSY = (playerElement, text) => {
}
}
}

const onStepCB = script.onStepCB;
if (step.action === `chain`) {
const runMode = script.runMode;
fetch(`${path}${step.script}`)
.then(response => {
if (response.status >= 400) {
throw Error(`Unable to load ${step.script}: ${response.status}`)
}
response.json().then(data => {
script = data;
if (onStepCB) {
onStepCB(-1);
}
initScript();
script.runMode = runMode;
doStep(script.steps[1]);
});
})
.catch(err => {
throw Error(`Fetch Error :${err}`);
});
return;
}

const actionName = step.action.split(` `).join(``);
let handler = actions[actionName];
if (typeof handler === `undefined`) {
handler = IWSY.plugins[actionName];
if (script.runMode === `auto`) {
if (typeof handler === `undefined`) {
throw Error(`Unknown action: '${step.action}'`);
handler = IWSY.plugins[actionName];
if (typeof handler === `undefined`) {
throw Error(`Unknown action: '${step.action}'`);
}
}
if (onStepCB) {
onStepCB(step.index);
}
try {
handler(step);
} catch (err) {
console.log(`Step ${step.index} (${step.action}): ${err}`);
alert(`Step ${step.index} (${step.action}): ${err}`);
}
} else {
try {
handler(step);
} catch (err) {
console.log(JSON.stringify(step,0,2) + `\n` + JSON.stringify(handler,0,2));
console.log(`Step ${step.index} (${step.action}): ${err}`);
alert(`Step ${step.index} (${step.action}): ${err}`);
}
}
if (script.onStepCB && script.runMode === `auto`) {
script.onStepCB(step.index);
}
try {
handler(step);
} catch (err) {
console.log(`Step ${step.index} (${step.action}): ${err}`);
alert(`Step ${step.index} (${step.action}): ${err}`);
}

};

///////////////////////////////////////////////////////////////////////////////
// These are all the exported functions

// Get the script
const getScript = () => {
return script;
};

// Set the script
const setScript = newScript => {
Expand All @@ -872,12 +914,17 @@ const IWSY = (playerElement, text) => {
initScript();
};

// Set the path
const setPath = p => {
path = p;
};

// Go to a specified step number
const gotoStep = (target) => {
const gotoStep = target => {
script.scanTarget = target;
script.runMode = `manual`;
scan();
};
};

// Show a block
const block = blockIndex => {
Expand Down Expand Up @@ -936,7 +983,8 @@ const IWSY = (playerElement, text) => {

// Run the presentation
const run = (mode, startMode, then) => {
script.then = then;
homeScript = JSON.parse(JSON.stringify(script));
afterRun = then;
initScript();
if (mode === `fullscreen`) {
if (document.fullscreenElement) {
Expand Down Expand Up @@ -996,7 +1044,9 @@ const IWSY = (playerElement, text) => {
setupShowdown();
initScript();
return {
getScript,
setScript,
setPath,
gotoStep,
block,
run,
Expand Down
3 changes: 1 addition & 2 deletions iwsy/resources/ecs/content.txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,8 @@ Restart:
on click DeleteItem
begin
put the index of DeleteItem into N
put element N of ItemNames into ItemName
put property `content` of Presentation into Items
json delete property ItemName of Items
json delete element N of Items
set property `content` of Presentation to Items
put -1 into SelectedItem
go to Restart
Expand Down
Loading