1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-05-05 17:04:10 +00:00

Improve new tiddler behaviour with existing tiddlers

Now repeatedly clicking “new journal” will reuse the existing journal
if one exists
This commit is contained in:
Jermolene 2014-10-09 17:30:53 +01:00
parent 612e05a247
commit 3827f98a43
3 changed files with 73 additions and 39 deletions

View File

@ -177,12 +177,7 @@ NavigatorWidget.prototype.handleCloseOtherTiddlersEvent = function(event) {
NavigatorWidget.prototype.handleEditTiddlerEvent = function(event) { NavigatorWidget.prototype.handleEditTiddlerEvent = function(event) {
var self = this; var self = this;
function isUnmodifiedShadow(title) { function isUnmodifiedShadow(title) {
// jshint eqnull:true return self.wiki.isShadowTiddler(title) && !self.wiki.tiddlerExists(title);
var tiddler = self.wiki.getTiddler(title);
return (
self.wiki.isShadowTiddler(title) &&
tiddler.fields.modified == null
);
} }
function confirmEditShadow(title) { function confirmEditShadow(title) {
return confirm($tw.language.getString( return confirm($tw.language.getString(
@ -253,19 +248,14 @@ Create/reuse the draft tiddler for a given title
*/ */
NavigatorWidget.prototype.makeDraftTiddler = function(targetTitle) { NavigatorWidget.prototype.makeDraftTiddler = function(targetTitle) {
// See if there is already a draft tiddler for this tiddler // See if there is already a draft tiddler for this tiddler
var drafts = []; var draftTitle = this.wiki.findDraft(targetTitle);
this.wiki.forEachTiddler({includeSystem: true},function(title,tiddler) { if(draftTitle) {
if(tiddler.fields["draft.title"] && tiddler.fields["draft.of"] === targetTitle) { return draftTitle;
drafts.push(tiddler);
}
});
if(drafts.length > 0) {
return drafts[0];
} }
// Get the current value of the tiddler we're editing // Get the current value of the tiddler we're editing
var tiddler = this.wiki.getTiddler(targetTitle), var tiddler = this.wiki.getTiddler(targetTitle);
draftTitle = this.generateDraftTitle(targetTitle);
// Save the initial value of the draft tiddler // Save the initial value of the draft tiddler
draftTitle = this.generateDraftTitle(targetTitle);
var draftTiddler = new $tw.Tiddler( var draftTiddler = new $tw.Tiddler(
tiddler, tiddler,
{ {
@ -372,26 +362,46 @@ NavigatorWidget.prototype.handleCancelTiddlerEvent = function(event) {
}; };
// Create a new draft tiddler // Create a new draft tiddler
// event.param can either be the title of a template tiddler, or a hashmap of fields.
//
// The title of the newly created tiddler follows these rules:
// * If a hashmap was used and a title field was specified, use that title
// * If a hashmap was used without a title field, use a default title, if necessary making it unique with a numeric suffix
// * If a template tiddler was used, use the title of the template, if necessary making it unique with a numeric suffix
//
// If a draft of the target tiddler already exists then it is reused
NavigatorWidget.prototype.handleNewTiddlerEvent = function(event) { NavigatorWidget.prototype.handleNewTiddlerEvent = function(event) {
// Get the story details // Get the story details
var storyList = this.getStoryList(), var storyList = this.getStoryList(),
templateTiddler,originalTitle; existingTiddler, templateTiddler,
title;
// Get the template // Get the template
if(typeof event.param === "object") { if(typeof event.param === "object") {
// If we got a hashmap use it as the template
templateTiddler = event.param; templateTiddler = event.param;
originalTitle = templateTiddler.title; if(templateTiddler.title) {
} else { // Pull in any existing tiddler
templateTiddler = this.wiki.getTiddler(event.param); existingTiddler = this.wiki.getTiddler(templateTiddler.title);
originalTitle = templateTiddler && templateTiddler.fields.title; if(existingTiddler && existingTiddler.fields.tags && templateTiddler.tags) {
// Merge tags
templateTiddler.tags = $tw.utils.pushTop($tw.utils.parseStringArray(templateTiddler.tags),existingTiddler.fields.tags);
} }
originalTitle = originalTitle || $tw.language.getString("DefaultNewTiddlerTitle"); // Use the provided title
// Title the new tiddler title = templateTiddler.title
var title = this.wiki.generateNewTitle(originalTitle); }
// Create the draft tiddler } else {
var draftTitle = this.generateDraftTitle(title), existingTiddler = this.wiki.getTiddler(event.param);
draftTiddler = new $tw.Tiddler({ title = this.wiki.generateNewTitle(event.param);
}
title = title || this.wiki.generateNewTitle($tw.language.getString("DefaultNewTiddlerTitle"));
// Try to reuse any existing draft of the tiddler
var draftTitle = this.wiki.findDraft(title);
if(!draftTitle) {
// Otherwise, create a new draft of the tiddler
draftTitle = this.generateDraftTitle(title);
var draftTiddler = new $tw.Tiddler({
text: "" text: ""
},templateTiddler, },existingTiddler,templateTiddler,
this.wiki.getCreationFields(), this.wiki.getCreationFields(),
{ {
title: draftTitle, title: draftTitle,
@ -399,10 +409,15 @@ NavigatorWidget.prototype.handleNewTiddlerEvent = function(event) {
"draft.of": title "draft.of": title
},this.wiki.getModificationFields()); },this.wiki.getModificationFields());
this.wiki.addTiddler(draftTiddler); this.wiki.addTiddler(draftTiddler);
// Update the story to insert the new draft at the top }
// Update the story to insert the new draft at the top and remove any existing tiddler
if(storyList.indexOf(draftTitle) === -1) {
var slot = storyList.indexOf(event.navigateFromTitle); var slot = storyList.indexOf(event.navigateFromTitle);
storyList.splice(slot + 1,0,draftTitle); storyList.splice(slot + 1,0,draftTitle);
// Save the updated story }
if(storyList.indexOf(title) !== -1) {
storyList.splice(storyList.indexOf(title),1);
}
this.saveStoryList(storyList); this.saveStoryList(storyList);
// Add a new record to the top of the history stack // Add a new record to the top of the history stack
this.addToHistory(draftTitle); this.addToHistory(draftTitle);

View File

@ -1132,6 +1132,19 @@ exports.readFile = function(file,callback) {
} }
}; };
/*
Find any existing draft of a specified tiddler
*/
exports.findDraft = function(targetTitle) {
var draftTitle = undefined;
this.forEachTiddler({includeSystem: true},function(title,tiddler) {
if(tiddler.fields["draft.title"] && tiddler.fields["draft.of"] === targetTitle) {
draftTitle = title;
}
});
return draftTitle;
}
/* /*
Check whether the specified draft tiddler has been modified Check whether the specified draft tiddler has been modified
*/ */

View File

@ -11,6 +11,12 @@ The new tiddler message creates a new draft tiddler and adds it to the current s
|param |Either the title of a tiddler to use as a template for the new tiddler or a hashmap of tiddler fields | |param |Either the title of a tiddler to use as a template for the new tiddler or a hashmap of tiddler fields |
|navigateFromTitle |Title of the tiddler from which the navigation to the new tiddler was initiated | |navigateFromTitle |Title of the tiddler from which the navigation to the new tiddler was initiated |
The title for the draft tiddler is chosen according to these rules:
* If a hashmap was used and a title field was specified, use that title
* If a hashmap was used without a title field, use a default title, if necessary making it unique with a numeric suffix
* If a template tiddler was used, use the title of the template tiddler, if necessary making it unique with a numeric suffix
The new tiddler message is usually generated with the LinkWidget, ButtonWidget or ActionSendMessageWidget and is handled by the NavigatorWidget. The new tiddler message is usually generated with the LinkWidget, ButtonWidget or ActionSendMessageWidget and is handled by the NavigatorWidget.
! Example ! Example