mirror of
				https://github.com/Jermolene/TiddlyWiki5
				synced 2025-11-04 01:23:01 +00:00 
			
		
		
		
	allow unusedtitle macro to use the prefix parameter and fix wiki.generateNewTitle() (#5361)
This commit is contained in:
		@@ -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});
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
})();
 | 
			
		||||
@@ -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,
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
		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 = baseTitle + 
 | 
			
		||||
			(options.prefix || " ") + 
 | 
			
		||||
			(++c);
 | 
			
		||||
			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;
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
 | 
			
		||||
caption: unusedtitle
 | 
			
		||||
created: 20210104143546885
 | 
			
		||||
modified: 20210427184035684
 | 
			
		||||
tags: Macros [[Core Macros]]
 | 
			
		||||
title: unusedtitle Macro
 | 
			
		||||
type: text/vnd.tiddlywiki
 | 
			
		||||
@@ -11,6 +12,29 @@ 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`
 | 
			
		||||
: A string specifying the desired base name, defaulting to `New Tiddler`. <br>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
 | 
			
		||||
 | 
			
		||||
<<list-links "[prefix[unusedtitle Macro (E]!sort[]]">>
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,51 @@
 | 
			
		||||
created: 20210227212730299
 | 
			
		||||
modified: 20210427184057456
 | 
			
		||||
tags: 
 | 
			
		||||
title: unusedtitle Macro (Examples 1)
 | 
			
		||||
type: text/vnd.tiddlywiki
 | 
			
		||||
 | 
			
		||||
\define testCreate()
 | 
			
		||||
<$action-createtiddler $basetitle=<<unusedtitle template:"$count:2$-new">>/>
 | 
			
		||||
\end
 | 
			
		||||
 | 
			
		||||
\define testCreate1()
 | 
			
		||||
<$action-createtiddler $basetitle=<<unusedtitle baseName:"new" separator:"-" template:"$count:2$$separator$$basename$">>/>
 | 
			
		||||
\end
 | 
			
		||||
 | 
			
		||||
\define testNew()
 | 
			
		||||
<$action-sendmessage $message="tm-new-tiddler" title=<<unusedtitle baseName:"new" template:"$count:2$-$basename$">> />
 | 
			
		||||
\end
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
<<unusedtitle template:"$count:2$-new">>
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
<$button actions=<<testCreate>> >
 | 
			
		||||
<$action-setfield $tiddler="$:/state/tab/sidebar--595412856" text="$:/core/ui/SideBar/Recent"/>
 | 
			
		||||
Create Tiddler
 | 
			
		||||
</$button>
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
<<unusedtitle baseName:"new" template:"$count:2$-$basename$">>
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
<$button actions=<<testNew>>>
 | 
			
		||||
<$action-setfield $tiddler="$:/state/tab/sidebar--595412856" text="$:/core/ui/SideBar/Recent"/>
 | 
			
		||||
New Tiddler
 | 
			
		||||
</$button>
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
<<unusedtitle baseName:"new" separator:"-" template:"$count:2$$separator$$basename$">>
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
<$button actions=<<testCreate1>>>
 | 
			
		||||
<$action-setfield $tiddler="$:/state/tab/sidebar--595412856" text="$:/core/ui/SideBar/Recent"/>
 | 
			
		||||
Create Tiddler
 | 
			
		||||
</$button>
 | 
			
		||||
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
<details>
 | 
			
		||||
    <summary>Show the code</summary>
 | 
			
		||||
    <pre><code><$view><pre><code>
 | 
			
		||||
</details>
 | 
			
		||||
@@ -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="""<<unusedtitle>>"""/>
 | 
			
		||||
<$macrocall $name=".example" n="2" eg="""<<unusedtitle AnotherBase>>"""/>
 | 
			
		||||
<$macrocall $name=".example" n="3" eg="""<<unusedtitle TiddlyWiki>>"""/>
 | 
			
		||||
''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="""<<unusedtitle separator:"-">>"""/>
 | 
			
		||||
<$macrocall $name=".example" n="3" eg="""<<unusedtitle baseName:"anotherBase">>"""/>
 | 
			
		||||
<$macrocall $name=".example" n="4" eg="""<<unusedtitle baseName:"About" separator:"-">>"""/>
 | 
			
		||||
<$macrocall $name=".example" n="5" eg="""<<unusedtitle template:"$count:2$-test">>"""/>
 | 
			
		||||
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
Working buttons can be found at: [[unusedtitle Macro (Examples 1)]]. You'll have to examine the code to see, what's going on.
 | 
			
		||||
		Reference in New Issue
	
	Block a user