> On Wednesday, April 18, 2018 at 8:10:01 AM UTC-7, Jeremy Ruston wrote:
>> IF the auto update of "list and tag fields" that happens when changing 
>> tiddler titles could be made to include other designated fields, (e.g. 
>> matching link-*) then the fields would be updated automatically whenever a 
>> corresponding title was changed.  
>>
>
Exactly that, it would be a very modest extension of the existing relinking 
> code.
>
>
> https://github.com/Jermolene/TiddlyWiki5/blob/master/core/modules/wiki-bulkops.js
>

Here's my version of bulkops. It borrows the option settings from "tags" 
for now.

-- Mark

/*\
title: $:/core/modules/wiki-bulkops.js
type: application/javascript
module-type: wikimethod

MAS notes: Attempt to implement auto-rename of link fields (e.g. link1-#). 
Changes marked as MAS


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.
*/
function renameTiddler(fromTitle,toTitle,options) {
    fromTitle = (fromTitle || "").trim();
    toTitle = (toTitle || "").trim();
    options = options || {};
    if(fromTitle && toTitle && fromTitle !== toTitle) {
        // Rename the tiddler itself
        var oldTiddler = this.getTiddler(fromTitle),
            newTiddler = new $tw.Tiddler(oldTiddler,{title: toTitle},this.
getModificationFields());
        newTiddler = $tw.hooks.invokeHook("th-renaming-tiddler",newTiddler,
oldTiddler);
        this.addTiddler(newTiddler);
        this.deleteTiddler(fromTitle);
        // Rename any tags or lists that reference it
        this.relinkTiddler(fromTitle,toTitle,options)
    }
}

/*
Relink any tags or lists that reference a given tiddler
*/
function relinkTiddler(fromTitle,toTitle,options) {
    var self = this;
    fromTitle = (fromTitle || "").trim();
    toTitle = (toTitle || "").trim();
    options = options || {};
    options.dontRenameLinkFields = options.dontRenameInTags ; // MAS -- 
until actual option is set up somewhere
    if(fromTitle && toTitle && fromTitle !== toTitle) {
        this.each(function(tiddler,title) {
            var type = tiddler.fields.type || "";
            // Don't touch plugins or JavaScript modules
            // MAS adding 'fieldsWithLinks' for capturing fields that match 
pattern link-#
            // MAS adding 'changedFieldLinks' which will be filled new 
link-# contents if reference link changes
            if(!tiddler.fields["plugin-type"] && type !== 
"application/javascript") {
                var tags = (tiddler.fields.tags || []).slice(0),
                    list = (tiddler.fields.list || []).slice(0),
                    fieldsWithLinks = findlinks(tiddler.fields,/link-/),
                    changedFieldLinks = {} ,
                    isModified = false;
                if(!options.dontRenameInTags) {
                    // Rename tags
                    $tw.utils.each(tags,function (title,index) {
                        if(title === fromTitle) {
console.log("Renaming tag '" + tags[index] + "' to '" + toTitle + "' of 
tiddler '" + tiddler.fields.title + "'");
                            tags[index] = toTitle;
                            isModified = true;
                        }
                    });
                }
                 if(!options.dontRenameInLists) {
                    // Rename lists
                    $tw.utils.each(list,function (title,index) {
                        if(title === fromTitle) {
console.log("Renaming list item '" + list[index] + "' to '" + toTitle + "' 
of tiddler '" + tiddler.fields.title + "'");
                            list[index] = toTitle;
                            isModified = true;
                        }
                    });
                }


                if(!options.dontRenameLinkFields) { // MAS
                    // Rename link fields
                    var linkTitle ;
                    $tw.utils.each(fieldsWithLinks,function (linkset,index) 
{
                        linkTitle = (linkset.title || "").trim() ; 
                        if(linkTitle == fromTitle) {
                            console.log("DEBUG Renaming link item of field " 
+ linkset.field + " from '" + linkset.title +"' to '" + toTitle + "' of 
tiddler '" + tiddler.fields.title + "'");
                            changedFieldLinks[linkset.field] = toTitle;
                            isModified = true;
                        }
                    });
                }

                if(isModified) {
                    var newTiddler = new $tw.Tiddler(tiddler,
changedFieldLinks,{tags:tags, list: list},self.getModificationFields())
                    newTiddler = $tw.hooks.invokeHook("th-relinking-tiddler"
,newTiddler,tiddler);
                    self.addTiddler(newTiddler);
                }
            }
        });
    }
};


var findlinks = function(obj, filter) {
  var key, keys = [];
    console.log("Object type " + typeof obj) ;
  for (key in obj) {
    if ( filter.test(key)) {
        keys.push({"field" :key, "title": obj[key]});
    }
  }
  return keys;
}

exports.renameTiddler = renameTiddler;
exports.relinkTiddler = relinkTiddler;

})();




-- 
You received this message because you are subscribed to the Google Groups 
"TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywiki/f418f16d-8c8e-41d6-af8d-cc2274dfe23e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to