1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-08-08 23:03:50 +00:00

Support for saving result lists in a variable

Extend let filter run prefix to store list of results, and add varlist operator for accessing variables as a list.

We already had partial support for variables returning a list of values in order for functions to work, now we extend it so that any variable can be used to store a list

We should extend the set widget so that it returns a result list that can be accessed with the varlist operator
This commit is contained in:
Jeremy Ruston 2025-03-09 16:50:48 +00:00
parent e7b713c277
commit 7397f4fa3a
4 changed files with 69 additions and 8 deletions

View File

@ -25,7 +25,7 @@ exports.let = function(operationSubFunction,options) {
var inputSource = widget.wiki.makeTiddlerIterator(results.toArray()); var inputSource = widget.wiki.makeTiddlerIterator(results.toArray());
// Assign the result of the subfunction to the variable // Assign the result of the subfunction to the variable
var variables = {}; var variables = {};
variables[name] = operationSubFunction(inputSource,widget)[0] || ""; variables[name] = operationSubFunction(inputSource,widget);
// Clear the results // Clear the results
results.clear(); results.clear();
// Return the variables // Return the variables

View File

@ -0,0 +1,30 @@
/*\
title: $:/core/modules/filters/varlist.js
type: application/javascript
module-type: filteroperator
Retrieve the value of a variable as a list
[all[shadows+tiddlers]]
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Export our filter function
*/
exports.varlist = function(source,operator,options) {
// Check for common optimisations
var variableInfo = options.widget.getVariableInfo(operator.operand);
if(variableInfo) {
return options.wiki.makeTiddlerIterator(variableInfo.resultList);
} else {
return [];
}
};
})();

View File

@ -83,7 +83,7 @@ Widget.prototype.execute = function() {
/* /*
Set the value of a context variable Set the value of a context variable
name: name of the variable name: name of the variable
value: value of the variable value: value of the variable, can be a string or an array
params: array of {name:, default:} for each parameter params: array of {name:, default:} for each parameter
isMacroDefinition: true if the variable is set via a \define macro pragma (and hence should have variable substitution performed) isMacroDefinition: true if the variable is set via a \define macro pragma (and hence should have variable substitution performed)
options includes: options includes:
@ -93,8 +93,10 @@ options includes:
*/ */
Widget.prototype.setVariable = function(name,value,params,isMacroDefinition,options) { Widget.prototype.setVariable = function(name,value,params,isMacroDefinition,options) {
options = options || {}; options = options || {};
var valueIsArray = $tw.utils.isArray(value);
this.variables[name] = { this.variables[name] = {
value: value, value: valueIsArray ? (value[0] || "") : value,
resultList: valueIsArray ? value : [value],
params: params, params: params,
isMacroDefinition: !!isMacroDefinition, isMacroDefinition: !!isMacroDefinition,
isFunctionDefinition: !!options.isFunctionDefinition, isFunctionDefinition: !!options.isFunctionDefinition,
@ -164,6 +166,9 @@ Widget.prototype.getVariableInfo = function(name,options) {
resultList = this.wiki.filterTiddlers(value,this.makeFakeWidgetWithVariables(variables),options.source); resultList = this.wiki.filterTiddlers(value,this.makeFakeWidgetWithVariables(variables),options.source);
value = resultList[0] || ""; value = resultList[0] || "";
} else { } else {
if(variable.resultList) {
resultList = variable.resultList;
}
params = variable.params; params = variable.params;
} }
return { return {
@ -313,7 +318,7 @@ Widget.prototype.getStateQualifier = function(name) {
}; };
/* /*
Make a fake widget with specified variables, suitable for variable lookup in filters 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) { Widget.prototype.makeFakeWidgetWithVariables = function(variables) {
var self = this, var self = this,
@ -321,7 +326,12 @@ Widget.prototype.makeFakeWidgetWithVariables = function(variables) {
return { return {
getVariable: function(name,opts) { getVariable: function(name,opts) {
if($tw.utils.hop(variables,name)) { if($tw.utils.hop(variables,name)) {
return variables[name]; var value = variables[name];
if($tw.utils.isArray(value)) {
return value[0];
} else {
return value;
}
} else { } else {
opts = opts || {}; opts = opts || {};
opts.variables = variables; opts.variables = variables;
@ -330,9 +340,18 @@ Widget.prototype.makeFakeWidgetWithVariables = function(variables) {
}, },
getVariableInfo: function(name,opts) { getVariableInfo: function(name,opts) {
if($tw.utils.hop(variables,name)) { if($tw.utils.hop(variables,name)) {
var value = variables[name];
if($tw.utils.isArray(value)) {
return { return {
text: variables[name] text: value[0],
resultList: value
}; };
} else {
return {
text: value,
resultList: [value]
};
}
} else { } else {
opts = opts || {}; opts = opts || {};
opts.variables = $tw.utils.extend({},variables,opts.variables); opts.variables = $tw.utils.extend({},variables,opts.variables);

View File

@ -0,0 +1,12 @@
title: LetFilterRunPrefix/ResultList
description: Using the "let" filter run prefix to store result lists, not just single values
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
<$text text={{{ :let:varname[all[tiddlers]] [varlist[varname]sort[]join[,]] }}}/>
+
title: ExpectedResult
<p>$:/core,ExpectedResult,Output</p>