diff --git a/core/modules/macros/uniquetitle.js b/core/modules/macros/unusedtitle.js similarity index 59% rename from core/modules/macros/uniquetitle.js rename to core/modules/macros/unusedtitle.js index 8531d1ce7..952bd0264 100644 --- a/core/modules/macros/uniquetitle.js +++ b/core/modules/macros/unusedtitle.js @@ -2,6 +2,7 @@ title: $:/core/modules/macros/unusedtitle.js type: application/javascript module-type: macro + Macro to return a new title that is unused in the wiki. It can be given a name as a base. \*/ (function(){ @@ -10,25 +11,25 @@ Macro to return a new title that is unused in the wiki. It can be given a name a /*global $tw: false */ "use strict"; -/* -Information about this macro -*/ - exports.name = "unusedtitle"; exports.params = [ {name: "baseName"}, - {name: "options"} + {name: "separator"}, + {name: "template"} ]; /* Run the macro */ -exports.run = function(baseName, options) { +exports.run = function(baseName,separator,template) { + separator = separator || " "; if(!baseName) { baseName = $tw.language.getString("DefaultNewTiddlerTitle"); } - return this.wiki.generateNewTitle(baseName, options); + // $tw.wiki.generateNewTitle = function(baseTitle,options) + // options.prefix must be a string! + return this.wiki.generateNewTitle(baseName, {"prefix": separator, "template": template}); }; })(); diff --git a/core/modules/utils/utils.js b/core/modules/utils/utils.js index 8e028e47c..b1d82a427 100644 --- a/core/modules/utils/utils.js +++ b/core/modules/utils/utils.js @@ -294,6 +294,47 @@ exports.slowInSlowOut = function(t) { return (1 - ((Math.cos(t * Math.PI) + 1) / 2)); }; +exports.formatTitleString = function(template,options) { + var base = options.base || "", + separator = options.separator || "", + counter = options.counter || ""; + var result = "", + t = template, + matches = [ + [/^\$basename\$/i, function() { + return base; + }], + [/^\$count:(\d+)\$/i, function(match) { + return $tw.utils.pad(counter,match[1]); + }], + [/^\$separator\$/i, function() { + return separator; + }], + [/^\$count\$/i, function() { + return counter + ""; + }] + ]; + while(t.length){ + var matchString = ""; + $tw.utils.each(matches, function(m) { + var match = m[0].exec(t); + if(match) { + matchString = m[1].call(null,match); + t = t.substr(match[0].length); + return false; + } + }); + if(matchString) { + result += matchString; + } else { + result += t.charAt(0); + t = t.substr(1); + } + } + result = result.replace(/\\(.)/g,"$1"); + return result; +}; + exports.formatDateString = function(date,template) { var result = "", t = template, diff --git a/core/modules/wiki.js b/core/modules/wiki.js index ccda3ee95..54c3f0810 100755 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -190,15 +190,25 @@ exports.getChangeCount = function(title) { /* Generate an unused title from the specified base +options.prefix must be a string */ exports.generateNewTitle = function(baseTitle,options) { options = options || {}; var c = 0, - title = baseTitle; - while(this.tiddlerExists(title) || this.isShadowTiddler(title) || this.findDraft(title)) { - title = baseTitle + - (options.prefix || " ") + - (++c); + title = baseTitle, + template = options.template, + prefix = (typeof(options.prefix) === "string") ? options.prefix : " "; + if (template) { + // "count" is important to avoid an endless loop in while(...)!! + template = (/\$count:?(\d+)?\$/i.test(template)) ? template : template + "$count$"; + title = $tw.utils.formatTitleString(template,{"base":baseTitle,"separator":prefix,"counter":c}); + while(this.tiddlerExists(title) || this.isShadowTiddler(title) || this.findDraft(title)) { + title = $tw.utils.formatTitleString(template,{"base":baseTitle,"separator":prefix,"counter":(++c)}); + } + } else { + while(this.tiddlerExists(title) || this.isShadowTiddler(title) || this.findDraft(title)) { + title = baseTitle + prefix + (++c); + } } return title; }; diff --git a/editions/tw5.com/tiddlers/macros/UnusedTitleMacro.tid b/editions/tw5.com/tiddlers/macros/UnusedTitleMacro.tid index 39fdacf93..67c6df23d 100644 --- a/editions/tw5.com/tiddlers/macros/UnusedTitleMacro.tid +++ b/editions/tw5.com/tiddlers/macros/UnusedTitleMacro.tid @@ -1,5 +1,6 @@ - caption: unusedtitle +created: 20210104143546885 +modified: 20210427184035684 tags: Macros [[Core Macros]] title: unusedtitle Macro type: text/vnd.tiddlywiki @@ -10,7 +11,30 @@ It uses the same method as the create new tiddler button, a number is appended t !! Parameters -;baseName -: A string specifying the desired base name, defaulting to `New Tiddler` +; baseName +: A string specifying the desired base name, defaulting to `New Tiddler`.
The default setting can be adjusted in the $:/ControlPanel '': Info : Basics - tab.'' -<<.macro-examples "unusedtitle">> +; separator +: <<.from-version "5.1.24">> An ''optional'' string specifying the separator between baseName and the unique number. eg: `separator:"-"`. Defaults to a space: `" "`. If you need an empty separator use the ''template''! + +; template +: <<.from-version "5.1.24">> A ''optional'' template string can be used to allow you maximum flexibility. If the template string is used, there will always be a counter value. + +!! Template String + +; `$basename$` +: This variable will be replaced by the content of the ''baseName'' parameter + +; `$separator$` +: This variable will be replaced by the ''separator'' parameter + +;`$count$` +: This variable will be createad automatically and is a counter starting with 0 + +;`$count:4$` +: This variable will be createad automatically and starts at 0000 +: `:4` represents the number of digits + +!! Examples + +<> diff --git a/editions/tw5.com/tiddlers/macros/examples/unusedtitle Macro (Examples 1).tid b/editions/tw5.com/tiddlers/macros/examples/unusedtitle Macro (Examples 1).tid new file mode 100644 index 000000000..5c4bc3125 --- /dev/null +++ b/editions/tw5.com/tiddlers/macros/examples/unusedtitle Macro (Examples 1).tid @@ -0,0 +1,51 @@ +created: 20210227212730299 +modified: 20210427184057456 +tags: +title: unusedtitle Macro (Examples 1) +type: text/vnd.tiddlywiki + +\define testCreate() +<$action-createtiddler $basetitle=<>/> +\end + +\define testCreate1() +<$action-createtiddler $basetitle=<>/> +\end + +\define testNew() +<$action-sendmessage $message="tm-new-tiddler" title=<> /> +\end + +``` +<> +``` + +<$button actions=<> > +<$action-setfield $tiddler="$:/state/tab/sidebar--595412856" text="$:/core/ui/SideBar/Recent"/> +Create Tiddler + + +``` +<> +``` + +<$button actions=<>> +<$action-setfield $tiddler="$:/state/tab/sidebar--595412856" text="$:/core/ui/SideBar/Recent"/> +New Tiddler + + +``` +<> +``` + +<$button actions=<>> +<$action-setfield $tiddler="$:/state/tab/sidebar--595412856" text="$:/core/ui/SideBar/Recent"/> +Create Tiddler + + +--- + +
+ Show the code +
<$view>

+
diff --git a/editions/tw5.com/tiddlers/macros/examples/unusedtitle.tid b/editions/tw5.com/tiddlers/macros/examples/unusedtitle.tid index 0389d2d67..a86f0d16e 100644 --- a/editions/tw5.com/tiddlers/macros/examples/unusedtitle.tid +++ b/editions/tw5.com/tiddlers/macros/examples/unusedtitle.tid @@ -1,7 +1,16 @@ +created: 20210104143940715 +modified: 20210228141241657 tags: [[unusedtitle Macro]] [[Macro Examples]] title: unusedtitle Macro (Examples) type: text/vnd.tiddlywiki <$macrocall $name=".example" n="1" eg="""<>"""/> -<$macrocall $name=".example" n="2" eg="""<>"""/> -<$macrocall $name=".example" n="3" eg="""<>"""/> +''The following example works best if there is an open tiddler in draft mode, or there is a tiddler named "New Tiddler".'' So you can see the automatic numbering. +<$macrocall $name=".example" n="2" eg="""<>"""/> +<$macrocall $name=".example" n="3" eg="""<>"""/> +<$macrocall $name=".example" n="4" eg="""<>"""/> +<$macrocall $name=".example" n="5" eg="""<>"""/> + +--- + +Working buttons can be found at: [[unusedtitle Macro (Examples 1)]]. You'll have to examine the code to see, what's going on. \ No newline at end of file