update the view widget

This commit is contained in:
BurningTreeC 2024-03-31 06:06:27 +02:00
parent eb5efcfbac
commit 3e99663c66
2 changed files with 52 additions and 5 deletions

View File

@ -30,8 +30,23 @@ ViewWidget.prototype.render = function(parent,nextSibling) {
this.parentDomNode = parent;
this.computeAttributes();
this.execute();
if(this.text) {
var textNode = this.document.createTextNode(this.text);
var textNode;
if(this.viewUpdate && this.viewWikified) {
this.fakeWidget = this.wiki.makeTranscludeWidget(this.viewTitle,{
document: $tw.fakeDocument,
field: this.viewField,
parseAsInline: this.viewMode !== "block",
parentWidget: this,
subTiddler: this.viewSubtiddler
});
this.fakeNode = $tw.fakeDocument.createElement("div");
this.fakeWidget.makeChildWidgets();
this.fakeWidget.renderChildren(this.fakeNode,null);
textNode = this.document.createTextNode(this.text || "");
parent.insertBefore(textNode,nextSibling);
this.domNodes.push(textNode);
} else if(this.text) {
textNode = this.document.createTextNode(this.text);
parent.insertBefore(textNode,nextSibling);
this.domNodes.push(textNode);
} else {
@ -52,15 +67,21 @@ ViewWidget.prototype.execute = function() {
this.viewFormat = this.getAttribute("format","text");
this.viewTemplate = this.getAttribute("template","");
this.viewMode = this.getAttribute("mode","block");
this.viewStripComments = this.getAttribute("stripcomments","no") === "yes";
this.viewUpdate = this.getAttribute("update","no") === "yes";
this.viewWikified = false;
switch(this.viewFormat) {
case "htmlwikified":
this.text = this.getValueAsHtmlWikified(this.viewMode);
this.viewWikified = true;
break;
case "plainwikified":
this.text = this.getValueAsPlainWikified(this.viewMode);
this.viewWikified = true;
break;
case "htmlencodedplainwikified":
this.text = this.getValueAsHtmlEncodedPlainWikified(this.viewMode);
this.viewWikified = true;
break;
case "htmlencoded":
this.text = this.getValueAsHtmlEncoded();
@ -135,7 +156,9 @@ ViewWidget.prototype.getValue = function(options) {
};
ViewWidget.prototype.getValueAsText = function() {
return this.getValue({asString: true});
var value = this.getValue({asString: true}),
textValue = this.viewStripComments ? this.stripComments(value) : value;
return textValue;
};
ViewWidget.prototype.getValueAsHtmlWikified = function(mode) {
@ -194,7 +217,7 @@ ViewWidget.prototype.getValueAsRelativeDate = function(format) {
}
};
ViewWidget.prototype.getValueAsStrippedComments = function(srcText) {
ViewWidget.prototype.getValueAsStrippedComments = function() {
var lines = this.getValueAsText().split("\n"),
out = [];
for(var line=0; line<lines.length; line++) {
@ -211,6 +234,7 @@ ViewWidget.prototype.getValueAsJsEncoded = function() {
};
ViewWidget.prototype.stripComments = function(text) {
// https://stackoverflow.com/questions/37051797/remove-comments-from-string-with-javascript-using-javascript
return text.replace(/\/\*[\s\S]*?\*\/|(?<=[^:])\/\/.*|^\/\/.*/g,'').trim();
};
@ -219,9 +243,30 @@ Selectively refreshes the widget if needed. Returns true if the widget or any of
*/
ViewWidget.prototype.refresh = function(changedTiddlers) {
var changedAttributes = this.computeAttributes();
if(changedAttributes.tiddler || changedAttributes.field || changedAttributes.index || changedAttributes.template || changedAttributes.format || changedTiddlers[this.viewTitle]) {
if(changedAttributes.tiddler || changedAttributes.field || changedAttributes.index || changedAttributes.template || changedAttributes.format || changedAttributes.update || changedTiddlers[this.viewTitle]) {
this.refreshSelf();
return true;
} else if(this.viewUpdate && this.viewWikified) {
var refreshed = this.fakeWidget.refresh(changedTiddlers);
if(refreshed) {
var newText;
switch(this.viewFormat) {
case "htmlwikified":
newText = this.fakeNode.innerHTML;
break;
case "plainwikified":
newText = this.fakeNode.textContent;
break;
case "htmlencodedplainwikified":
newText = $tw.utils.htmlEncode(this.fakeNode.textContent);
break;
}
if(newText !== this.text) {
this.domNodes[0].textContent = newText;
this.text = newText;
}
}
return refreshed;
} else {
return false;
}

View File

@ -21,6 +21,8 @@ The content of the `<$view>` widget is displayed if the field or property is mis
|template |Optional template string used when the `format` attribute is set to "date" |
|subtiddler |Optional SubTiddler title when the target tiddler is a [[plugin|Plugins]] (see below) |
|mode |<<.from-version "5.1.15">> Optional transclusion parsing mode for wikified formats. May be "inline" or "block" (the default) |
|update |<<.from-version "5.3.4">> Optional. If set to "yes", the ''wikified'' text updates if its value changes |
|stripcomments |<<.from-version "5.3.4">> Optional. If set to "yes", comments like `// ...`, `/* ... */` are stripped |
!! Formats