1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-29 12:59:56 +00:00

Fix refreshing of transcluded functions (#7698)

* Passing test

* Failing test

* Fix test

It still fails, but now fails correctly

* Fix refreshing transcluded functions (#7755)

We store the previous result of the filter function and recalculate it
when the transclude widget needs to be refreshed, refreshing the widget
if the result is different.

---------

Co-authored-by: Jeremy Ruston <174761+Jermolene@users.noreply.github.com>
Co-authored-by: Robin Munn <rmunn@pobox.com>
This commit is contained in:
Jeremy Ruston 2023-10-14 09:44:18 +01:00 committed by GitHub
parent 32966c9e91
commit ff1437e439
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 6 deletions

View File

@ -109,6 +109,7 @@ TranscludeWidget.prototype.collectAttributes = function() {
this.recursionMarker = this.getAttribute("recursionMarker","yes"); this.recursionMarker = this.getAttribute("recursionMarker","yes");
} else { } else {
this.transcludeVariable = this.getAttribute("$variable"); this.transcludeVariable = this.getAttribute("$variable");
this.transcludeVariableIsFunction = false;
this.transcludeType = this.getAttribute("$type"); this.transcludeType = this.getAttribute("$type");
this.transcludeOutput = this.getAttribute("$output","text/html"); this.transcludeOutput = this.getAttribute("$output","text/html");
this.transcludeTitle = this.getAttribute("$tiddler",this.getVariable("currentTiddler")); this.transcludeTitle = this.getAttribute("$tiddler",this.getVariable("currentTiddler"));
@ -184,7 +185,9 @@ TranscludeWidget.prototype.getTransclusionTarget = function() {
if(this.transcludeVariable) { if(this.transcludeVariable) {
// Transcluding a variable // Transcluding a variable
var variableInfo = this.getVariableInfo(this.transcludeVariable,{params: this.getOrderedTransclusionParameters()}); var variableInfo = this.getVariableInfo(this.transcludeVariable,{params: this.getOrderedTransclusionParameters()});
this.transcludeVariableIsFunction = variableInfo.srcVariable && variableInfo.srcVariable.isFunctionDefinition;
text = variableInfo.text; text = variableInfo.text;
this.transcludeFunctionResult = text;
return { return {
text: variableInfo.text, text: variableInfo.text,
type: this.transcludeType type: this.transcludeType
@ -219,21 +222,24 @@ TranscludeWidget.prototype.parseTransclusionTarget = function(parseAsInline) {
// Transcluding a variable // Transcluding a variable
var variableInfo = this.getVariableInfo(this.transcludeVariable,{params: this.getOrderedTransclusionParameters()}), var variableInfo = this.getVariableInfo(this.transcludeVariable,{params: this.getOrderedTransclusionParameters()}),
srcVariable = variableInfo && variableInfo.srcVariable; srcVariable = variableInfo && variableInfo.srcVariable;
if(srcVariable && srcVariable.isFunctionDefinition) {
this.transcludeVariableIsFunction = true;
this.transcludeFunctionResult = (variableInfo.resultList ? variableInfo.resultList[0] : variableInfo.text) || "";
}
if(variableInfo.text) { if(variableInfo.text) {
if(srcVariable && srcVariable.isFunctionDefinition) { if(srcVariable && srcVariable.isFunctionDefinition) {
var result = (variableInfo.resultList ? variableInfo.resultList[0] : variableInfo.text) || "";
parser = { parser = {
tree: [{ tree: [{
type: "text", type: "text",
text: result text: this.transcludeFunctionResult
}], }],
source: result, source: this.transcludeFunctionResult,
type: "text/vnd.tiddlywiki" type: "text/vnd.tiddlywiki"
}; };
if(parseAsInline) { if(parseAsInline) {
parser.tree[0] = { parser.tree[0] = {
type: "text", type: "text",
text: result text: this.transcludeFunctionResult
}; };
} else { } else {
parser.tree[0] = { parser.tree[0] = {
@ -241,7 +247,7 @@ TranscludeWidget.prototype.parseTransclusionTarget = function(parseAsInline) {
tag: "p", tag: "p",
children: [{ children: [{
type: "text", type: "text",
text: result text: this.transcludeFunctionResult
}] }]
} }
} }
@ -430,12 +436,19 @@ TranscludeWidget.prototype.parserNeedsRefresh = function() {
return (this.sourceText === undefined || parserInfo.sourceText !== this.sourceText || parserInfo.parserType !== this.parserType) return (this.sourceText === undefined || parserInfo.sourceText !== this.sourceText || parserInfo.parserType !== this.parserType)
}; };
TranscludeWidget.prototype.functionNeedsRefresh = function() {
var oldResult = this.transcludeFunctionResult;
var variableInfo = this.getVariableInfo(this.transcludeVariable,{params: this.getOrderedTransclusionParameters()});
var newResult = (variableInfo.resultList ? variableInfo.resultList[0] : variableInfo.text) || "";
return oldResult !== newResult;
}
/* /*
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
*/ */
TranscludeWidget.prototype.refresh = function(changedTiddlers) { TranscludeWidget.prototype.refresh = function(changedTiddlers) {
var changedAttributes = this.computeAttributes(); var changedAttributes = this.computeAttributes();
if(($tw.utils.count(changedAttributes) > 0) || (!this.transcludeVariable && changedTiddlers[this.transcludeTitle] && this.parserNeedsRefresh())) { if(($tw.utils.count(changedAttributes) > 0) || (this.transcludeVariableIsFunction && this.functionNeedsRefresh()) || (!this.transcludeVariable && changedTiddlers[this.transcludeTitle] && this.parserNeedsRefresh())) {
this.refreshSelf(); this.refreshSelf();
return true; return true;
} else { } else {

View File

@ -0,0 +1,27 @@
title: Transclude/Variable/Refreshing
description: Transcluding and refreshing a function
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
\function list-join(filter, sep:", ") [subfilter<filter>join<sep>]
<$tiddler tiddler="TestData">
<<list-join "[enlist{!!items}]">>
</$tiddler>
+
title: TestData
+
title: Actions
<$action-setfield $tiddler="TestData" items={{{ [range[10]join[ ]] }}}/>
+
title: ExpectedResult
<p>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</p>

View File

@ -0,0 +1,15 @@
title: Transclude/Variable/Static
description: Transcluding a function
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
items: 1 2 3 4 5 6 7 8 9 10
\function list-join(filter, sep:", ") [subfilter<filter>join<sep>]
<<list-join "[enlist{!!items}]">>
+
title: ExpectedResult
<p>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</p>