mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2026-09-30 13:11:42 +00:00
Fixes variable enumeration in widgets (#9648)
* fix: variable enumeration in widgets * fix: fakeWidget should have a variables property * fix: don't use spread properties * fix: resolve more embarassing bugs * chore: tests and whitespace * fix: simplify opts check
This commit is contained in:
@@ -329,48 +329,43 @@ Widget.prototype.getStateQualifier = function(name) {
|
||||
/*
|
||||
Make a fake widget with specified variables, suitable for variable lookup in filters. Each variable can be a string or an array of strings
|
||||
*/
|
||||
Widget.prototype.makeFakeWidgetWithVariables = function(variables) {
|
||||
var self = this,
|
||||
variables = variables || {};
|
||||
return {
|
||||
getVariable: function(name,opts) {
|
||||
if($tw.utils.hop(variables,name)) {
|
||||
var value = variables[name];
|
||||
if($tw.utils.isArray(value)) {
|
||||
return value[0];
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
} else {
|
||||
opts = opts || {};
|
||||
opts.variables = $tw.utils.extend({},variables,opts.variables);
|
||||
return self.getVariable(name,opts);
|
||||
};
|
||||
Widget.prototype.makeFakeWidgetWithVariables = function(vars = {}) {
|
||||
const self = this;
|
||||
|
||||
const fakeWidget = {
|
||||
getVariableInfo(name,opts = {}) {
|
||||
if(name in vars) {
|
||||
const value = vars[name];
|
||||
return Array.isArray(value)
|
||||
? { text: value[0], resultList: value }
|
||||
: { text: value, resultList: [value] };
|
||||
}
|
||||
opts = opts || {};
|
||||
opts.variables = Object.assign({}, vars, opts.variables || {});
|
||||
return self.getVariableInfo(name, opts);
|
||||
},
|
||||
getVariableInfo: function(name,opts) {
|
||||
if($tw.utils.hop(variables,name)) {
|
||||
var value = variables[name];
|
||||
if($tw.utils.isArray(value)) {
|
||||
return {
|
||||
text: value[0],
|
||||
resultList: value
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
text: value,
|
||||
resultList: [value]
|
||||
};
|
||||
}
|
||||
} else {
|
||||
opts = opts || {};
|
||||
opts.variables = $tw.utils.extend({},variables,opts.variables);
|
||||
return self.getVariableInfo(name,opts);
|
||||
};
|
||||
|
||||
|
||||
getVariable(name,opts) {
|
||||
return this.getVariableInfo(name, opts).text;
|
||||
},
|
||||
makeFakeWidgetWithVariables: self.makeFakeWidgetWithVariables,
|
||||
|
||||
resolveVariableParameters: self.resolveVariableParameters,
|
||||
wiki: self.wiki
|
||||
wiki: self.wiki,
|
||||
makeFakeWidgetWithVariables: self.makeFakeWidgetWithVariables,
|
||||
|
||||
get variables() {
|
||||
// Merge parent vars via prototype-like delegation
|
||||
return Object.create(self.variables || {},
|
||||
Object.keys(vars).reduce((acc, key) => {
|
||||
acc[key] = { value: vars[key], enumerable: true, configurable: true };
|
||||
return acc;
|
||||
}, {})
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
return fakeWidget;
|
||||
};
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user