Skip to content
Snippets Groups Projects
Commit 1be86a19 authored by Jimmy's avatar Jimmy
Browse files

Style clean-up for dolwidget

parent a7c81698
No related branches found
No related tags found
No related merge requests found
/* eslint-disable jsdoc/require-description-complete-sentence */
/**
* Provides a magic variable `$_` that creates a custom scope for the current
* widget invocation
......@@ -12,9 +13,9 @@ const vContext = [];
const d = JSON.stringify.bind(JSON);
const devOptions = {
trace: false,
invocationId: false
invocationId: false,
};
$(document).one(':storyready', function() {
$(document).one(":storyready", function () {
State.variables.devOptions = devOptions;
});
// We declare some global debug utils that other code is free to use
......@@ -22,32 +23,35 @@ $(document).one(':storyready', function() {
// Note that clog is currently non-configured and will always be invoked
// TODO: add more granular debug log levels if needed
function clog() {
console.log(`${State.passage}:${d(vContext)}`, ...arguments)
console.log(`${State.passage}:${d(vContext)}`, ...arguments);
}
function trace() {
if (devOptions.trace) {
clog(...arguments)
clog(...arguments);
}
}
function allMagical() {
return Object.keys(State.variables).filter(key => key.startsWith(VIRTUAL_CURRENT) && key != VIRTUAL_CURRENT)
return Object.keys(State.variables).filter(
key => key.startsWith(VIRTUAL_CURRENT) && key !== VIRTUAL_CURRENT
);
}
let uniqueInvocation = 0;
// eslint-disable-next-line no-unused-vars
const uniqueInvocation = 0;
Macro.delete('widget');
Macro.add('widget', {
tags : null,
Macro.delete("widget");
Macro.add("widget", {
tags: null,
handler() {
if (this.args.length === 0) {
return this.error('no widget name specified');
return this.error("no widget name specified");
}
const widgetName = this.args[0];
const isNonVoid = this.args.length > 1 && this.args[1] === 'container';
const isNonVoid = this.args.length > 1 && this.args[1] === "container";
if (Macro.has(widgetName)) {
if (!Macro.get(widgetName).isWidget) {
......@@ -63,8 +67,8 @@ Macro.add('widget', {
try {
const widgetDef = {
isWidget : true,
handler : (function (widgetCode) {
isWidget: true,
handler: (function (widgetCode) {
return function () {
// Custom code
DOL.Stack.push(widgetName);
......@@ -78,25 +82,29 @@ Macro.add('widget', {
* note: that would mean the $_var was defined in the body,
* as we clean our local magic variables
*/
const priorFrame = vStack[vStack.length - 2]
const priorFrame = vStack[vStack.length - 2];
const magicals = allMagical();
if (magicals.length > 0) {
trace(`saving ${d(magicals)} to ${d(priorFrame)}`)
trace(`saving ${d(magicals)} to ${d(priorFrame)}`);
}
if (priorFrame !== undefined) {
magicals.forEach(key => {
priorFrame[key] = State.variables[key]
delete State.variables[key]
})
priorFrame[key] = State.variables[key];
delete State.variables[key];
});
} else if (magicals.length > 0) {
console.warn(`Found variables: ${JSON.stringify(magicals)} declared in :: ${State.passage}`)
console.warn(
`Found variables: ${JSON.stringify(magicals)} declared in :: ${
State.passage
}`
);
}
// End custom code
const shadowStore = {};
// Cache the existing value of the `_args` variable, if necessary.
if (State.temporary.hasOwnProperty('args')) {
if (Object.hasOwn(State.temporary, "args")) {
shadowStore._args = State.temporary.args;
}
......@@ -104,106 +112,105 @@ Macro.add('widget', {
State.temporary.args = [...this.args];
State.temporary.args.raw = this.args.raw;
State.temporary.args.full = this.args.full;
this.addShadow('_args');
this.addShadow("_args");
if (isNonVoid) {
// Cache the existing value of the `_contents` variable, if necessary.
if (State.temporary.hasOwnProperty('contents')) {
if (Object.hasOwn(State.temporary, "contents")) {
shadowStore._contents = State.temporary.contents;
}
// Set up the widget `_contents` variable and add a shadow.
State.temporary.contents = this.payload[0].contents;
this.addShadow('_contents');
this.addShadow("_contents");
}
/* legacy */
// Cache the existing value of the `$args` variable, if necessary.
if (State.variables.hasOwnProperty('args')) {
if (Object.hasOwn(State.variables, "args")) {
shadowStore.$args = State.variables.args;
}
// Set up the widget `$args` variable and add a shadow.
State.variables.args = State.temporary.args;
this.addShadow('$args');
this.addShadow("$args");
/* /legacy */
try {
// Set up the error trapping variables.
const resFrag = document.createDocumentFragment();
const errList = [];
// Wikify the widget's code.
new Wikifier(resFrag, widgetCode.replace(/^\n+|\n+$/g, '').replace(/\n+/g, ' '));
const resFrag = Wikifier.wikifyEval(
widgetCode.replace(/^\n+|\n+$/g, "").replace(/\n+/g, " ")
);
// Carry over the output, unless there were errors.
Array.from(resFrag.querySelectorAll('.error')).forEach(errEl => {
Array.from(resFrag.querySelectorAll(".error")).forEach(errEl => {
errList.push(errEl.textContent);
});
if (errList.length === 0) {
this.output.appendChild(resFrag);
}
else {
} else {
console.error(`Error rendering widget ${widgetName}`, errList);
return this.error(`error${errList.length > 1 ? 's' : ''} within widget code (${errList.join('; ')})`);
return this.error(
`error${
errList.length > 1 ? "s" : ""
} within widget code (${errList.join("; ")})`
);
}
}
catch (ex) {
} catch (ex) {
return this.error(`cannot execute widget: ${ex.message}`);
}
finally {
} finally {
// Custom code
DOL.Stack.pop();
vStack.pop();
vContext.pop();
State.variables[VIRTUAL_CURRENT] = priorFrame
State.variables[VIRTUAL_CURRENT] = priorFrame;
const magicals = allMagical();
if (magicals.length > 0) {
trace(`cleaning up ${d(magicals)}`)
trace(`cleaning up ${d(magicals)}`);
magicals.forEach(key => {
// don't pollute the global namespace
delete State.variables[key]
})
delete State.variables[key];
});
}
if (priorFrame !== undefined && Object.keys(priorFrame).length > 0) {
trace(`restoring ${d(priorFrame)}`)
trace(`restoring ${d(priorFrame)}`);
// restore prior frame
Object.assign(State.variables, priorFrame)
Object.assign(State.variables, priorFrame);
}
DOL.Perflog.logWidgetEnd(widgetName);
// End custom code
// Revert the `_args` variable shadowing.
if (shadowStore.hasOwnProperty('_args')) {
if (Object.hasOwn(shadowStore, "_args")) {
State.temporary.args = shadowStore._args;
}
else {
} else {
delete State.temporary.args;
}
if (isNonVoid) {
// Revert the `_contents` variable shadowing.
if (shadowStore.hasOwnProperty('_contents')) {
if (Object.hasOwn(shadowStore, "_contents")) {
State.temporary.contents = shadowStore._contents;
}
else {
} else {
delete State.temporary.contents;
}
}
/* legacy */
// Revert the `$args` variable shadowing.
if (shadowStore.hasOwnProperty('$args')) {
if (Object.hasOwn(shadowStore, "$args")) {
State.variables.args = shadowStore.$args;
}
else {
} else {
delete State.variables.args;
}
/* /legacy */
}
};
})(this.payload[0].contents)
})(this.payload[0].contents),
};
if (isNonVoid) {
......@@ -214,11 +221,10 @@ Macro.add('widget', {
// Custom debug view setup.
if (Config.debug) {
this.debugView.modes({ hidden : true });
this.debugView.modes({ hidden: true });
}
}
catch (ex) {
} catch (ex) {
return this.error(`cannot create widget macro "${widgetName}": ${ex.message}`);
}
}
},
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment