diff --git a/core/modules/parsers/parseutils.js b/core/modules/parsers/parseutils.js index 97c994050..0d74355f7 100644 --- a/core/modules/parsers/parseutils.js +++ b/core/modules/parsers/parseutils.js @@ -218,6 +218,7 @@ exports.parseAttribute = function(source,pos) { // Define our regexps var reAttributeName = /([^\/\s>"'=]+)/g, reUnquotedAttribute = /([^\/\s<>"'=]+)/g, + reFilteredValue = /\{\{\{(.+?)\}\}\}/g, reIndirectValue = /\{\{([^\}]+)\}\}/g; // Skip whitespace pos = $tw.utils.skipWhiteSpace(source,pos); @@ -243,29 +244,37 @@ exports.parseAttribute = function(source,pos) { node.type = "string"; node.value = stringLiteral.value; } else { - // Look for an indirect value - var indirectValue = $tw.utils.parseTokenRegExp(source,pos,reIndirectValue); - if(indirectValue) { - pos = indirectValue.end; - node.type = "indirect"; - node.textReference = indirectValue.match[1]; + // Look for a filtered value + var filteredValue = $tw.utils.parseTokenRegExp(source,pos,reFilteredValue); + if(filteredValue) { + pos = filteredValue.end; + node.type = "filtered"; + node.filter = filteredValue.match[1]; } else { - // Look for a unquoted value - var unquotedValue = $tw.utils.parseTokenRegExp(source,pos,reUnquotedAttribute); - if(unquotedValue) { - pos = unquotedValue.end; - node.type = "string"; - node.value = unquotedValue.match[1]; + // Look for an indirect value + var indirectValue = $tw.utils.parseTokenRegExp(source,pos,reIndirectValue); + if(indirectValue) { + pos = indirectValue.end; + node.type = "indirect"; + node.textReference = indirectValue.match[1]; } else { - // Look for a macro invocation value - var macroInvocation = $tw.utils.parseMacroInvocation(source,pos); - if(macroInvocation) { - pos = macroInvocation.end; - node.type = "macro"; - node.value = macroInvocation; - } else { + // Look for a unquoted value + var unquotedValue = $tw.utils.parseTokenRegExp(source,pos,reUnquotedAttribute); + if(unquotedValue) { + pos = unquotedValue.end; node.type = "string"; - node.value = "true"; + node.value = unquotedValue.match[1]; + } else { + // Look for a macro invocation value + var macroInvocation = $tw.utils.parseMacroInvocation(source,pos); + if(macroInvocation) { + pos = macroInvocation.end; + node.type = "macro"; + node.value = macroInvocation; + } else { + node.type = "string"; + node.value = "true"; + } } } } diff --git a/core/modules/widgets/action-createtiddler.js b/core/modules/widgets/action-createtiddler.js new file mode 100644 index 000000000..01010b940 --- /dev/null +++ b/core/modules/widgets/action-createtiddler.js @@ -0,0 +1,81 @@ +/*\ +title: $:/core/modules/widgets/action-createtiddler.js +type: application/javascript +module-type: widget + +Action widget to create a new tiddler with a unique name and specified fields. + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +var Widget = require("$:/core/modules/widgets/widget.js").widget; + +var CreateTiddlerWidget = function(parseTreeNode,options) { + this.initialise(parseTreeNode,options); +}; + +/* +Inherit from the base widget class +*/ +CreateTiddlerWidget.prototype = new Widget(); + +/* +Render this widget into the DOM +*/ +CreateTiddlerWidget.prototype.render = function(parent,nextSibling) { + this.computeAttributes(); + this.execute(); +}; + +/* +Compute the internal state of the widget +*/ +CreateTiddlerWidget.prototype.execute = function() { + this.actionBaseTitle = this.getAttribute("$basetitle"); + this.actionSaveTitle = this.getAttribute("$savetitle"); + this.actionTimestamp = this.getAttribute("$timestamp","yes") === "yes"; +}; + +/* +Refresh the widget by ensuring our attributes are up to date +*/ +CreateTiddlerWidget.prototype.refresh = function(changedTiddlers) { + var changedAttributes = this.computeAttributes(); + if($tw.utils.count(changedAttributes) > 0) { + this.refreshSelf(); + return true; + } + return this.refreshChildren(changedTiddlers); +}; + +/* +Invoke the action associated with this widget +*/ +CreateTiddlerWidget.prototype.invokeAction = function(triggeringWidget,event) { + var title = this.wiki.generateNewTitle(this.actionBaseTitle), + fields = {}, + creationFields, + modificationFields; + $tw.utils.each(this.attributes,function(attribute,name) { + if(name.charAt(0) !== "$") { + fields[name] = attribute; + } + }); + if(this.actionTimestamp) { + creationFields = this.wiki.getCreationFields(); + modificationFields = this.wiki.getModificationFields(); + } + var tiddler = this.wiki.addTiddler(new $tw.Tiddler(creationFields,fields,modificationFields,{title: title})); + if(this.actionSaveTitle) { + this.wiki.setTextReference(this.actionSaveTitle,title,this.getVariable("currentTiddler")); + } + return true; // Action was invoked +}; + +exports["action-createtiddler"] = CreateTiddlerWidget; + +})(); diff --git a/core/modules/widgets/widget.js b/core/modules/widgets/widget.js index 5d8b167d1..f4905d433 100755 --- a/core/modules/widgets/widget.js +++ b/core/modules/widgets/widget.js @@ -222,7 +222,9 @@ Widget.prototype.computeAttributes = function() { self = this, value; $tw.utils.each(this.parseTreeNode.attributes,function(attribute,name) { - if(attribute.type === "indirect") { + if(attribute.type === "filtered") { + value = self.wiki.filterTiddlers(attribute.filter,self)[0] || ""; + } else if(attribute.type === "indirect") { value = self.wiki.getTextReference(attribute.textReference,"",self.getVariable("currentTiddler")); } else if(attribute.type === "macro") { value = self.getVariable(attribute.value.name,{params: attribute.value.params}); diff --git a/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget.tid b/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget.tid new file mode 100644 index 000000000..a7d223e4e --- /dev/null +++ b/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget.tid @@ -0,0 +1,30 @@ +caption: action-createtiddler +created: 20161020152745942 +modified: 20161020155119177 +tags: Widgets ActionWidgets +title: ActionCreateTiddlerWidget +type: text/vnd.tiddlywiki + +! Introduction + +The ''action-createtiddler'' widget is an [[action widget|ActionWidgets]] that creates new tiddlers. ActionWidgets are used within triggering widgets such as the ButtonWidget. + +There are several differences from the [[tm-new-tiddler message|WidgetMessage: tm-new-tiddler]]: + +* The new tiddler is not automatically displayed in the [[story river|StoryRiver]] +* The title of the new tiddler is made available for subsequent operations + +! Content and Attributes + +The ''action-createtiddler'' widget is invisible. Any content within it is ignored. + +|!Attribute |!Description | +|$basetitle |The initial title that will be attempted. If a tiddler with that title already exists, then a numerical counter is added to the title and incremented until it is unique| +|$savetitle |A text reference identifying a field or index into which the title of the newly created tiddler will be stored after it is created | +|$timestamp |Specifies whether the timestamp(s) of the target tiddler will be updated (''modified'' and ''modifier'', plus ''created'' and ''creator'' for newly created tiddlers). Can be "yes" (the default) or "no" | +|//{any attributes not starting with $}// |Each attribute name specifies a field to be created in the new tiddler | + +! Examples + +<$macrocall $name='wikitext-example-without-html' +src={{ActionCreateTiddlerWidget Example}}/> diff --git a/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget_Example.tid b/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget_Example.tid new file mode 100644 index 000000000..2be233566 --- /dev/null +++ b/editions/tw5.com/tiddlers/widgets/ActionCreateTiddlerWidget_Example.tid @@ -0,0 +1,12 @@ +created: 20161020153426686 +modified: 20161020155142990 +tags: ActionCreateTiddlerWidget +title: ActionCreateTiddlerWidget Example +type: text/vnd.tiddlywiki + +New button caption: <$edit-text tiddler="$:/state/new-button-caption" tag="input" default=""/> + +<$button> +<$action-createtiddler $basetitle="Homemade Button" tags="$:/tags/PageControls" text={{$:/state/new-button-caption}}/> +Create non-functional page control button +$button> \ No newline at end of file diff --git a/editions/tw5.com/tiddlers/wikitext/HTML in WikiText.tid b/editions/tw5.com/tiddlers/wikitext/HTML in WikiText.tid index f8575f002..f18aad0d1 100644 --- a/editions/tw5.com/tiddlers/wikitext/HTML in WikiText.tid +++ b/editions/tw5.com/tiddlers/wikitext/HTML in WikiText.tid @@ -1,6 +1,6 @@ caption: HTML created: 20131205160816081 -modified: 20160622112259272 +modified: 20161021102422842 tags: WikiText title: HTML in WikiText type: text/vnd.tiddlywiki @@ -14,25 +14,29 @@ This is my nice and simple block of text. HelloThere ``` +[[Widgets share the same syntax as HTML tags|Widgets in WikiText]], and so the following information applies to them, too. + ! Content Parsing -The content of an HTML element will be parsed in inline mode unless the opening tag is followed by two linebreaks, in which case it is parsed in block mode. (Inline mode means that block mode formatting such as tables, lists and headings is not recognised). +The content of an HTML element will be parsed in inline mode unless the opening tag is followed by two linebreaks, in which case it will be parsed in block mode. (Inline mode means that block mode formatting such as tables, lists and headings is not recognised). ! Attributes -Attributes in HTML tags can be specified as a literal, a transclusion or a macro invocation. For example, here the value of the `href` attribute will be set to the value of the tiddler MyLinkDestination: +In an extension of conventional HTML syntax, attributes of elements/widgets can be specified in several different ways: -``` -link -``` +* a literal string +* a transclusion of a TextReference +* a transclusion of a [[macro/variable|Macros in WikiText]] +* as the result of a [[Filter Expression]] -Note that the link should have the `rel` attribute set to `noopener noreferrer` to maintain privacy of the URLs of private TiddlyWiki's (eg on Dropbox). See https://mathiasbynens.github.io/rel-noopener/ for more information. +!! Literal Attribute Values -Here an attribute is specified as a macro invocation: +Literal attribute values can use several different styles of quoting: -``` -> rel="noopener noreferrer">link -``` +* Single quotes (eg `attr='value'`) +* Double quotes (eg `attr="value"`) +* Tripe double quotes (eg `attr="""value"""`) +* No quoting is necessary for values that do not contain spaces (eg `attr=value`) Literal attribute values can include line breaks. For example: @@ -43,7 +47,7 @@ Rodentville, Ratland."/> ``` -By using triple-double quotes you can specify attribute values that include single double quotes. For example: +By using triple-double quotes you can specify attribute values that contain single double quotes. For example: ```
``` + +!! Transcluded Attribute Values + +Transcluded attribute values are indicated with double curly braces around a TextReference. For example: + +``` +attr={{tiddler}} +attr={{!!field}} +attr={{tiddler!!field}} +``` + +!! Variable Attribute Values + +Variable attribute values are indicated with double angle brackets around a [[macro invocation|Macro Calls in WikiText]]. For example: + +``` +