This commit is contained in:
Simon Huber 2024-04-18 16:58:17 +02:00 committed by GitHub
commit 53f74991aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 342 additions and 155 deletions

View File

@ -21,6 +21,7 @@ exports.synchronous = true;
// Default story and history lists
var PAGE_TITLE_TITLE = "$:/core/wiki/title";
var PAGE_STYLESHEET_TITLE = "$:/core/ui/PageStylesheet";
var ROOT_STYLESHEET_TITLE = "$:/core/ui/RootStylesheet";
var PAGE_TEMPLATE_TITLE = "$:/core/ui/RootTemplate";
// Time (in ms) that we defer refreshing changes to draft tiddlers
@ -38,22 +39,13 @@ exports.startup = function() {
document.title = $tw.titleContainer.textContent;
}
});
// Set up the styles
$tw.styleWidgetNode = $tw.wiki.makeTranscludeWidget(PAGE_STYLESHEET_TITLE,{document: $tw.fakeDocument});
$tw.styleContainer = $tw.fakeDocument.createElement("style");
$tw.styleWidgetNode.render($tw.styleContainer,null);
$tw.styleWidgetNode.assignedStyles = $tw.styleContainer.textContent;
$tw.styleElement = document.createElement("style");
$tw.styleElement.innerHTML = $tw.styleWidgetNode.assignedStyles;
document.head.insertBefore($tw.styleElement,document.head.firstChild);
var styleParser = $tw.wiki.parseTiddler(ROOT_STYLESHEET_TITLE,{parseAsInline: true}),
styleWidgetNode = $tw.wiki.makeWidget(styleParser,{document: document});
styleWidgetNode.render(document.head,null);
$tw.wiki.addEventListener("change",$tw.perf.report("styleRefresh",function(changes) {
if($tw.styleWidgetNode.refresh(changes,$tw.styleContainer,null)) {
var newStyles = $tw.styleContainer.textContent;
if(newStyles !== $tw.styleWidgetNode.assignedStyles) {
$tw.styleWidgetNode.assignedStyles = newStyles;
$tw.styleElement.innerHTML = $tw.styleWidgetNode.assignedStyles;
}
}
styleWidgetNode.refresh(changes,document.head,null);
}));
// Display the $:/core/ui/PageTemplate tiddler to kick off the display
$tw.perf.report("mainRender",function() {
@ -62,7 +54,7 @@ exports.startup = function() {
$tw.utils.addClass($tw.pageContainer,"tc-page-container-wrapper");
document.body.insertBefore($tw.pageContainer,document.body.firstChild);
$tw.pageWidgetNode.render($tw.pageContainer,null);
$tw.hooks.invokeHook("th-page-refreshed");
$tw.hooks.invokeHook("th-page-refreshed");
})();
// Remove any splash screen elements
var removeList = document.querySelectorAll(".tc-remove-when-wiki-loaded");

View File

@ -64,24 +64,16 @@ exports.startup = function() {
$tw.wiki.removeEventListener("change",refreshHandler);
},false);
// Set up the styles
var styleWidgetNode = $tw.wiki.makeTranscludeWidget("$:/core/ui/PageStylesheet",{
document: $tw.fakeDocument,
variables: variables,
importPageMacros: true}),
styleContainer = $tw.fakeDocument.createElement("style");
styleWidgetNode.render(styleContainer,null);
var styleElement = srcDocument.createElement("style");
styleElement.innerHTML = styleContainer.textContent;
srcDocument.head.insertBefore(styleElement,srcDocument.head.firstChild);
var styleParser = $tw.wiki.parseTiddler("$:/core/ui/RootStylesheet",{parseAsInline: true}),
styleWidgetNode = $tw.wiki.makeWidget(styleParser,{document: srcDocument});
styleWidgetNode.render(srcDocument.head,null);
// Render the text of the tiddler
var parser = $tw.wiki.parseTiddler(template),
widgetNode = $tw.wiki.makeWidget(parser,{document: srcDocument, parentWidget: $tw.rootWidget, variables: variables});
widgetNode.render(srcDocument.body,srcDocument.body.firstChild);
// Function to handle refreshes
refreshHandler = function(changes) {
if(styleWidgetNode.refresh(changes,styleContainer,null)) {
styleElement.innerHTML = styleContainer.textContent;
}
styleWidgetNode.refresh(changes);
widgetNode.refresh(changes);
};
$tw.wiki.addEventListener("change",refreshHandler);

View File

@ -18,6 +18,89 @@ var ViewWidget = function(parseTreeNode,options) {
this.initialise(parseTreeNode,options);
};
var ViewHandler = function(widget) {
this.wiki = widget.wiki;
this.widget = widget;
this.document = widget.document;
};
/*
Base ViewHandler render method
*/
ViewHandler.prototype.render = function(parent,nextSibling) {
this.text = this.getValue();
this.createTextNode(parent,nextSibling);
};
/*
Base ViewHandler render method for wikified views
*/
ViewHandler.prototype.renderWikified = function(parent,nextSibling) {
this.createFakeWidget();
this.text = this.getValue();
this.createWikifiedTextNode(parent,nextSibling);
};
/*
ViewHandler method to create a simple text node
*/
ViewHandler.prototype.createTextNode = function(parent,nextSibling) {
if(this.text) {
var textNode = this.document.createTextNode(this.text);
parent.insertBefore(textNode,nextSibling);
this.widget.domNodes.push(textNode);
} else {
this.widget.makeChildWidgets();
this.widget.renderChildren(parent,nextSibling);
}
};
/*
ViewHandler method to always create a text node, even if there's no text
*/
ViewHandler.prototype.createWikifiedTextNode = function(parent,nextSibling) {
var textNode = this.document.createTextNode(this.text || "");
parent.insertBefore(textNode,nextSibling);
this.widget.domNodes.push(textNode);
};
/*
ViewHandler method to create a fake widget used by wikified views
*/
ViewHandler.prototype.createFakeWidget = function() {
this.fakeWidget = this.wiki.makeTranscludeWidget(this.widget.viewTitle,{
document: $tw.fakeDocument,
field: this.widget.viewField,
index: this.widget.viewIndex,
parseAsInline: this.widget.viewMode !== "block",
mode: this.widget.viewMode === "block" ? "block" : "inline",
parentWidget: this.widget,
subTiddler: this.widget.viewSubTiddler
});
this.fakeNode = $tw.fakeDocument.createElement("div");
this.fakeWidget.makeChildWidgets();
this.fakeWidget.render(this.fakeNode,null);
};
ViewHandler.prototype.refreshWikified = function(changedTiddlers) {
var refreshed = this.fakeWidget.refresh(changedTiddlers);
if(refreshed) {
var newText = this.getValue();
if(newText !== this.text) {
this.widget.domNodes[0].textContent = newText;
this.text = newText;
}
}
return refreshed;
};
/*
Base ViewHandler refresh method
*/
ViewHandler.prototype.refresh = function(changedTiddlers) {
return false;
};
/*
Inherit from the base widget class
*/
@ -30,14 +113,8 @@ ViewWidget.prototype.render = function(parent,nextSibling) {
this.parentDomNode = parent;
this.computeAttributes();
this.execute();
if(this.text) {
var textNode = this.document.createTextNode(this.text);
parent.insertBefore(textNode,nextSibling);
this.domNodes.push(textNode);
} else {
this.makeChildWidgets();
this.renderChildren(parent,nextSibling);
}
this.view = this.getView(this.viewFormat);
this.view.render(parent,nextSibling);
};
/*
@ -52,49 +129,238 @@ ViewWidget.prototype.execute = function() {
this.viewFormat = this.getAttribute("format","text");
this.viewTemplate = this.getAttribute("template","");
this.viewMode = this.getAttribute("mode","block");
switch(this.viewFormat) {
case "htmlwikified":
this.text = this.getValueAsHtmlWikified(this.viewMode);
break;
case "plainwikified":
this.text = this.getValueAsPlainWikified(this.viewMode);
break;
case "htmlencodedplainwikified":
this.text = this.getValueAsHtmlEncodedPlainWikified(this.viewMode);
break;
case "htmlencoded":
this.text = this.getValueAsHtmlEncoded();
break;
case "htmltextencoded":
this.text = this.getValueAsHtmlTextEncoded();
break;
case "urlencoded":
this.text = this.getValueAsUrlEncoded();
break;
case "doubleurlencoded":
this.text = this.getValueAsDoubleUrlEncoded();
break;
case "date":
this.text = this.getValueAsDate(this.viewTemplate);
break;
case "relativedate":
this.text = this.getValueAsRelativeDate();
break;
case "stripcomments":
this.text = this.getValueAsStrippedComments();
break;
case "jsencoded":
this.text = this.getValueAsJsEncoded();
break;
default: // "text"
this.text = this.getValueAsText();
break;
}
};
/*
The various formatter functions are baked into this widget for the moment. Eventually they will be replaced by macro functions
Initialise the view subclasses
*/
ViewWidget.prototype.getView = function(format) {
var View = this.initialiseView();
View.prototype = Object.create(ViewHandler.prototype);
switch(format) {
case "htmlwikified":
View = this.initialiseHTMLWikifiedView(View);
break;
case "plainwikified":
View = this.initialisePlainWikifiedView(View);
break;
case "htmlencodedplainwikified":
View = this.initialiseHTMLEncodedPlainWikifiedView(View);
break;
case "htmlencoded":
View = this.initialiseHTMLEncodedView(View);
break;
case "htmltextencoded":
View = this.initialiseHTMLTextEncodedView(View);
break;
case "urlencoded":
View = this.initialiseURLEncodedView(View);
break;
case "doubleurlencoded":
View = this.initialiseDoubleURLEncodedView(View);
break;
case "date":
View = this.initialiseDateView(View);
break;
case "relativedate":
View = this.initialiseRelativeDateView(View);
break;
case "stripcomments":
View = this.initialiseStripCommentsView(View);
break;
case "jsencoded":
View = this.initialiseJSEncodedView(View);
break;
default: // "text"
View = this.initialiseTextView(View);
break;
};
return new View(this);
};
/*
Return the function to intitialise the view subclass
*/
ViewWidget.prototype.initialiseView = function() {
return function(widget) {
ViewHandler.call(this,widget);
};
};
/*
Initialise HTML wikified view methods
*/
ViewWidget.prototype.initialiseHTMLWikifiedView = function(View) {
View.prototype.render = function(parent,nextSibling) {
this.renderWikified(parent,nextSibling);
};
View.prototype.getValue = function() {
return this.fakeNode.innerHTML;
};
View.prototype.refresh = function(changedTiddlers) {
return this.refreshWikified(changedTiddlers);
};
return View;
};
/*
Initialise plain wikified view methods
*/
ViewWidget.prototype.initialisePlainWikifiedView = function(View) {
View.prototype.render = function(parent,nextSibling) {
this.renderWikified(parent,nextSibling);
};
View.prototype.getValue = function() {
return this.fakeNode.textContent;
};
View.prototype.refresh = function(changedTiddlers) {
return this.refreshWikified(changedTiddlers);
};
return View;
};
/*
Initialise HTML encoded plain wikified methods
*/
ViewWidget.prototype.initialiseHTMLEncodedPlainWikifiedView = function(View) {
View.prototype.render = function(parent,nextSibling) {
this.renderWikified(parent,nextSibling);
};
View.prototype.getValue = function() {
return $tw.utils.htmlEncode(this.fakeNode.textContent);
};
View.prototype.refresh = function(changedTiddlers) {
return this.refreshWikified(changedTiddlers);
};
return View;
};
/*
Initialise HTML encoded mehods
*/
ViewWidget.prototype.initialiseHTMLEncodedView = function(View) {
var self = this;
View.prototype.getValue = function() {
return $tw.utils.htmlEncode(self.getValueAsText());
};
return View;
};
/*
Initialise HTML text encoded mehods
*/
ViewWidget.prototype.initialiseHTMLTextEncodedView = function(View) {
var self = this;
View.prototype.getValue = function() {
return $tw.utils.htmlTextEncode(self.getValueAsText());
};
return View;
};
/*
Initialise URL encoded mehods
*/
ViewWidget.prototype.initialiseURLEncodedView = function(View) {
var self = this;
View.prototype.getValue = function() {
return $tw.utils.encodeURIComponentExtended(self.getValueAsText());
};
return View;
};
/*
Initialise double URL encoded mehods
*/
ViewWidget.prototype.initialiseDoubleURLEncodedView = function(View) {
var self = this;
View.prototype.getValue = function() {
return $tw.utils.encodeURIComponentExtended($tw.utils.encodeURIComponentExtended(self.getValueAsText()));
};
return View;
};
/*
Initialise date mehods
*/
ViewWidget.prototype.initialiseDateView = function(View) {
var self = this;
View.prototype.getValue = function(format) {
format = format || "YYYY MM DD 0hh:0mm";
var value = $tw.utils.parseDate(self.getValue());
if(value && $tw.utils.isDate(value) && value.toString() !== "Invalid Date") {
return $tw.utils.formatDateString(value,format);
} else {
return "";
}
};
return View;
};
/*
Initialise relative date mehods
*/
ViewWidget.prototype.initialiseRelativeDateView = function(View) {
var self = this;
View.prototype.getValue = function(format) {
var value = $tw.utils.parseDate(self.getValue());
if(value && $tw.utils.isDate(value) && value.toString() !== "Invalid Date") {
return $tw.utils.getRelativeDate((new Date()) - (new Date(value))).description;
} else {
return "";
}
};
return View;
};
/*
Initialise stripcomments mehods
*/
ViewWidget.prototype.initialiseStripCommentsView = function(View) {
var self = this;
View.prototype.getValue = function() {
var lines = self.getValueAsText().split("\n"),
out = [];
for(var line=0; line<lines.length; line++) {
var text = lines[line];
if(!/^\s*\/\/#/.test(text)) {
out.push(text);
}
}
return out.join("\n");
};
return View;
};
/*
Initialise JS encoded mehods
*/
ViewWidget.prototype.initialiseJSEncodedView = function(View) {
var self = this;
View.prototype.getValue = function() {
return $tw.utils.stringify(self.getValueAsText());
};
return View;
};
/*
Initialise text mehods
*/
ViewWidget.prototype.initialiseTextView = function(View) {
var self = this;
View.prototype.getValue = function() {
return self.getValueAsText();
};
return View;
};
/*
Retrieve the value of the widget. Options are:
@ -138,78 +404,6 @@ ViewWidget.prototype.getValueAsText = function() {
return this.getValue({asString: true});
};
ViewWidget.prototype.getValueAsHtmlWikified = function(mode) {
return this.wiki.renderText("text/html","text/vnd.tiddlywiki",this.getValueAsText(),{
parseAsInline: mode !== "block",
parentWidget: this
});
};
ViewWidget.prototype.getValueAsPlainWikified = function(mode) {
return this.wiki.renderText("text/plain","text/vnd.tiddlywiki",this.getValueAsText(),{
parseAsInline: mode !== "block",
parentWidget: this
});
};
ViewWidget.prototype.getValueAsHtmlEncodedPlainWikified = function(mode) {
return $tw.utils.htmlEncode(this.wiki.renderText("text/plain","text/vnd.tiddlywiki",this.getValueAsText(),{
parseAsInline: mode !== "block",
parentWidget: this
}));
};
ViewWidget.prototype.getValueAsHtmlEncoded = function() {
return $tw.utils.htmlEncode(this.getValueAsText());
};
ViewWidget.prototype.getValueAsHtmlTextEncoded = function() {
return $tw.utils.htmlTextEncode(this.getValueAsText());
};
ViewWidget.prototype.getValueAsUrlEncoded = function() {
return $tw.utils.encodeURIComponentExtended(this.getValueAsText());
};
ViewWidget.prototype.getValueAsDoubleUrlEncoded = function() {
return $tw.utils.encodeURIComponentExtended($tw.utils.encodeURIComponentExtended(this.getValueAsText()));
};
ViewWidget.prototype.getValueAsDate = function(format) {
format = format || "YYYY MM DD 0hh:0mm";
var value = $tw.utils.parseDate(this.getValue());
if(value && $tw.utils.isDate(value) && value.toString() !== "Invalid Date") {
return $tw.utils.formatDateString(value,format);
} else {
return "";
}
};
ViewWidget.prototype.getValueAsRelativeDate = function(format) {
var value = $tw.utils.parseDate(this.getValue());
if(value && $tw.utils.isDate(value) && value.toString() !== "Invalid Date") {
return $tw.utils.getRelativeDate((new Date()) - (new Date(value))).description;
} else {
return "";
}
};
ViewWidget.prototype.getValueAsStrippedComments = function() {
var lines = this.getValueAsText().split("\n"),
out = [];
for(var line=0; line<lines.length; line++) {
var text = lines[line];
if(!/^\s*\/\/#/.test(text)) {
out.push(text);
}
}
return out.join("\n");
};
ViewWidget.prototype.getValueAsJsEncoded = function() {
return $tw.utils.stringify(this.getValueAsText());
};
/*
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
*/
@ -219,7 +413,7 @@ ViewWidget.prototype.refresh = function(changedTiddlers) {
this.refreshSelf();
return true;
} else {
return false;
return this.view.refresh(changedTiddlers);
}
};

View File

@ -25,6 +25,7 @@ title: $:/core/templates/tiddlywiki5.html
`{{{ [enlist<saveTiddlerAndShadowsFilter>tag[$:/core/wiki/rawmarkup]] ||$:/core/templates/plain-text-tiddler}}}
{{{ [enlist<saveTiddlerAndShadowsFilter>tag[$:/tags/RawMarkup]] ||$:/core/templates/plain-text-tiddler}}}
{{{ [enlist<saveTiddlerAndShadowsFilter>tag[$:/tags/RawMarkupWikified]] ||$:/core/templates/raw-static-tiddler}}}`
<!--~~ Style section start ~~-->
</head>
<body class="tc-body">
<!--~~ Raw markup for the top of the body section ~~-->

View File

@ -0,0 +1,12 @@
title: $:/core/ui/RootStylesheet
code-body: yes
\import [subfilter{$:/core/config/GlobalImportFilter}]
\whitespace trim
<$let currentTiddler={{$:/language}} languageTitle={{!!name}}>
<$list filter="[all[shadows+tiddlers]tag[$:/tags/Stylesheet]!is[draft]]">
<style type="text/css">
<$view format={{{ [<currentTiddler>tag[$:/tags/Stylesheet/Static]then[text]else[plainwikified]] }}} mode="block"/>
</style>
</$list>
</$let>

View File

@ -1,2 +1,2 @@
title: $:/config/Performance/Instrumentation
text: no
text: yes

View File

@ -65,12 +65,6 @@ $else$
\rules only filteredtranscludeinline transcludeinline macrodef macrocallinline macrocallblock
/*
** Start with the normalize CSS reset, and then belay some of its effects
*/
{{$:/themes/tiddlywiki/vanilla/reset}}
*, input[type="search"] {
box-sizing: border-box;
-moz-box-sizing: border-box;

View File

@ -1,5 +1,7 @@
title: $:/themes/tiddlywiki/vanilla/reset
type: text/css
tags: $:/tags/Stylesheet $:/tags/Stylesheet/Static
list-before: $:/themes/tiddlywiki/vanilla/base
/*! modern-normalize v2.0.0 | MIT License | https://github.com/sindresorhus/modern-normalize */