1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-25 17:40:29 +00:00
TiddlyWiki5/core/modules/wiki-bulkops.js
Jermolene 72ed4b2673 Add renaming to text-slicer tiddler toolbar
The plan is to expose the functionality in other places, too - for
example, the edit template and the tag manager
2015-08-04 17:44:07 +01:00

54 lines
1.3 KiB
JavaScript

/*\
title: $:/core/modules/wiki-bulkops.js
type: application/javascript
module-type: wikimethod
Bulk tiddler operations such as rename.
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Rename a tiddler, and relink any tags or lists that reference it.
*/
exports.renameTiddler = function(fromTitle,toTitle) {
var self = this;
fromTitle = (fromTitle || "").trim();
toTitle = (toTitle || "").trim();
if(fromTitle && toTitle && fromTitle !== toTitle) {
// Rename the tiddler itself
var tiddler = this.getTiddler(fromTitle);
this.addTiddler(new $tw.Tiddler(tiddler,{title: toTitle},this.getModificationFields()));
this.deleteTiddler(fromTitle);
// Rename any tags or lists that reference it
this.each(function(tiddler,title) {
var tags = (tiddler.fields.tags || []).slice(0),
list = (tiddler.fields.list || []).slice(0),
isModified = false;
// Rename tags
$tw.utils.each(tags,function (title,index) {
if(title === fromTitle) {
tags[index] = toTitle;
isModified = true;
}
});
// Rename lists
$tw.utils.each(list,function (title,index) {
if(title === fromTitle) {
list[index] = toTitle;
isModified = true;
}
});
if(isModified) {
self.addTiddler(new $tw.Tiddler(tiddler,{tags: tags, list: list},self.getModificationFields()));
}
});
}
}
})();