Skip to content
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

Fix refreshing of transcluded functions #7698

Merged
merged 5 commits into from
Oct 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 19 additions & 6 deletions core/modules/widgets/transclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ TranscludeWidget.prototype.collectAttributes = function() {
this.recursionMarker = this.getAttribute("recursionMarker","yes");
} else {
this.transcludeVariable = this.getAttribute("$variable");
this.transcludeVariableIsFunction = false;
this.transcludeType = this.getAttribute("$type");
this.transcludeOutput = this.getAttribute("$output","text/html");
this.transcludeTitle = this.getAttribute("$tiddler",this.getVariable("currentTiddler"));
Expand Down Expand Up @@ -184,7 +185,9 @@ TranscludeWidget.prototype.getTransclusionTarget = function() {
if(this.transcludeVariable) {
// Transcluding a variable
var variableInfo = this.getVariableInfo(this.transcludeVariable,{params: this.getOrderedTransclusionParameters()});
this.transcludeVariableIsFunction = variableInfo.srcVariable && variableInfo.srcVariable.isFunctionDefinition;
text = variableInfo.text;
this.transcludeFunctionResult = text;
return {
text: variableInfo.text,
type: this.transcludeType
Expand Down Expand Up @@ -219,29 +222,32 @@ TranscludeWidget.prototype.parseTransclusionTarget = function(parseAsInline) {
// Transcluding a variable
var variableInfo = this.getVariableInfo(this.transcludeVariable,{params: this.getOrderedTransclusionParameters()}),
srcVariable = variableInfo && variableInfo.srcVariable;
if(srcVariable && srcVariable.isFunctionDefinition) {
this.transcludeVariableIsFunction = true;
this.transcludeFunctionResult = (variableInfo.resultList ? variableInfo.resultList[0] : variableInfo.text) || "";
}
if(variableInfo.text) {
if(srcVariable && srcVariable.isFunctionDefinition) {
var result = (variableInfo.resultList ? variableInfo.resultList[0] : variableInfo.text) || "";
parser = {
tree: [{
type: "text",
text: result
text: this.transcludeFunctionResult
}],
source: result,
source: this.transcludeFunctionResult,
type: "text/vnd.tiddlywiki"
};
if(parseAsInline) {
parser.tree[0] = {
type: "text",
text: result
text: this.transcludeFunctionResult
};
} else {
parser.tree[0] = {
type: "element",
tag: "p",
children: [{
type: "text",
text: result
text: this.transcludeFunctionResult
}]
}
}
Expand Down Expand Up @@ -430,12 +436,19 @@ TranscludeWidget.prototype.parserNeedsRefresh = function() {
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
*/
TranscludeWidget.prototype.refresh = function(changedTiddlers) {
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();
return true;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
15 changes: 15 additions & 0 deletions editions/test/tiddlers/tests/data/transclude/Variable-Static.tid
Original file line number Diff line number Diff line change
@@ -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>