1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-11 18:00:26 +00:00

Merge pull request #586 from sukima/feature/issue-570-cancle-unchanged-drafts

Fix issue #570 Make it harder to accidentally modify a shadow tiddler
This commit is contained in:
Jeremy Ruston 2014-04-28 13:22:44 +01:00
commit a90339d1e5
6 changed files with 73 additions and 8 deletions

View File

@ -85,6 +85,18 @@ $tw.utils.each = function(object,callback) {
} }
}; };
/*
Check if an array is equal by value and by reference.
*/
$tw.utils.isArrayEqual = function(array1,array2) {
if(array1 === array2) { return true; }
array1 = array1 || []; array2 = array2 || [];
if(array1.length !== array2.length) { return false; }
return array1.every(function(value,index) {
return value === array2[index];
});
};
/* /*
Helper for making DOM elements Helper for making DOM elements
tag: tag name tag: tag name
@ -779,6 +791,10 @@ $tw.Tiddler.prototype.hasField = function(field) {
return $tw.utils.hop(this.fields,field); return $tw.utils.hop(this.fields,field);
}; };
$tw.Tiddler.prototype.isDraft = function() {
return this.hasField("draft.of");
};
/* /*
Register and install the built in tiddler field modules Register and install the built in tiddler field modules
*/ */
@ -1108,6 +1124,22 @@ $tw.Wiki.prototype.deserializeTiddlers = function(type,text,srcFields) {
} }
}; };
$tw.Wiki.prototype.isModifiedTiddler = function(title) {
var tiddler = this.getTiddler(title);
if(!tiddler.isDraft()) {
return false;
}
var ignoredFields = ["created", "modified", "title", "draft.title", "draft.of", "tags"],
origTiddler = this.getTiddler(tiddler.fields["draft.of"]);
if(!$tw.utils.isArrayEqual(tiddler.fields.tags,origTiddler.fields.tags)) {
return true;
}
return !Object.keys(tiddler.fields).every(function(field) {
if(ignoredFields.indexOf(field) >= 0) { return true; }
return tiddler.fields[field] === origTiddler.fields[field];
});
};
/* /*
Register the built in tiddler deserializer modules Register the built in tiddler deserializer modules
*/ */

View File

@ -4,6 +4,7 @@ Body/Hint: Use [[wiki text|http://tiddlywiki.com/static/WikiText.html]] to add f
Body/Placeholder: Type the text for this tiddler Body/Placeholder: Type the text for this tiddler
Body/Preview/Button/Hide: hide preview Body/Preview/Button/Hide: hide preview
Body/Preview/Button/Show: show preview Body/Preview/Button/Show: show preview
Body/ShadowWarning: You are editing a shadow tiddler. Any changes will override the default version.
Fields/Add/Button: add Fields/Add/Button: add
Fields/Add/Name/Placeholder: field name Fields/Add/Name/Placeholder: field name
Fields/Add/Prompt: Add a new field: Fields/Add/Prompt: Add a new field:

View File

@ -6,6 +6,7 @@ CloseAll/Button: close all
ConfirmCancelTiddler: Do you wish to discard changes to the tiddler "<$text text=<<title>>/>"? ConfirmCancelTiddler: Do you wish to discard changes to the tiddler "<$text text=<<title>>/>"?
ConfirmDeleteTiddler: Do you wish to delete the tiddler "<$text text=<<title>>/>"? ConfirmDeleteTiddler: Do you wish to delete the tiddler "<$text text=<<title>>/>"?
ConfirmOverwriteTiddler: Do you wish to overwrite the tiddler "<$text text=<<title>>/>"? ConfirmOverwriteTiddler: Do you wish to overwrite the tiddler "<$text text=<<title>>/>"?
ConfirmEditShadowTiddler: Your about to edit a ShaddowTiddler. This will override the default system making upgrading non-trivial. Are you sure you want to edit "<$text text=<<title>>/>"?
InvalidFieldName: Illegal characters in field name "<$text text=<<fieldName>>/>". Fields can only contain lowercase letters and the characters underscore (`_`), hyphen (`-`) and period (`.`) InvalidFieldName: Illegal characters in field name "<$text text=<<fieldName>>/>". Fields can only contain lowercase letters and the characters underscore (`_`), hyphen (`-`) and period (`.`)
MissingTiddler/Hint: Missing tiddler "<$text text=<<currentTiddler>>/>" - click {{$:/core/images/edit-button}} to create MissingTiddler/Hint: Missing tiddler "<$text text=<<currentTiddler>>/>" - click {{$:/core/images/edit-button}} to create
RecentChanges/DateFormat: DDth MMM YYYY RecentChanges/DateFormat: DDth MMM YYYY

View File

@ -181,9 +181,28 @@ NavigatorWidget.prototype.handleCloseOtherTiddlersEvent = function(event) {
// Place a tiddler in edit mode // Place a tiddler in edit mode
NavigatorWidget.prototype.handleEditTiddlerEvent = function(event) { NavigatorWidget.prototype.handleEditTiddlerEvent = function(event) {
function isUnmodifiedShadow(title) {
// jshint eqnull:true
var tiddler = $tw.wiki.getTiddler(title);
return (
$tw.wiki.isShadowTiddler(title) &&
tiddler.fields.modified == null
);
}
function confirmEditShadow(title) {
return confirm($tw.language.getString(
"ConfirmEditShadowTiddler",
{variables:
{title: title}
}
));
}
var title = event.param || event.tiddlerTitle;
if(isUnmodifiedShadow(title) && !confirmEditShadow(title)) {
return false;
}
// Replace the specified tiddler with a draft in edit mode // Replace the specified tiddler with a draft in edit mode
var title = event.param || event.tiddlerTitle, var draftTiddler = this.makeDraftTiddler(title),
draftTiddler = this.makeDraftTiddler(title),
draftTitle = draftTiddler.fields.title, draftTitle = draftTiddler.fields.title,
storyList = this.getStoryList(); storyList = this.getStoryList();
this.removeTitleFromStory(storyList,draftTitle); this.removeTitleFromStory(storyList,draftTitle);
@ -281,8 +300,7 @@ NavigatorWidget.prototype.generateDraftTitle = function(title) {
NavigatorWidget.prototype.handleSaveTiddlerEvent = function(event) { NavigatorWidget.prototype.handleSaveTiddlerEvent = function(event) {
var title = event.param || event.tiddlerTitle, var title = event.param || event.tiddlerTitle,
tiddler = this.wiki.getTiddler(title), tiddler = this.wiki.getTiddler(title),
storyList = this.getStoryList(), storyList = this.getStoryList();
storyTiddlerModified = false; // We have to special case saving the story tiddler itself
// Replace the original tiddler with the draft // Replace the original tiddler with the draft
if(tiddler) { if(tiddler) {
var draftTitle = (tiddler.fields["draft.title"] || "").trim(), var draftTitle = (tiddler.fields["draft.title"] || "").trim(),
@ -297,8 +315,10 @@ NavigatorWidget.prototype.handleSaveTiddlerEvent = function(event) {
{title: draftTitle} {title: draftTitle}
} }
)); ));
} } else if(!this.wiki.isModifiedTiddler(title)) {
if(isConfirmed) { event.type = "tw-cancel-tiddler";
this.dispatchEvent(event);
} else if(isConfirmed) {
// Save the draft tiddler as the real tiddler // Save the draft tiddler as the real tiddler
this.wiki.addTiddler(new $tw.Tiddler(this.wiki.getCreationFields(),tiddler,{ this.wiki.addTiddler(new $tw.Tiddler(this.wiki.getCreationFields(),tiddler,{
title: draftTitle, title: draftTitle,

View File

@ -0,0 +1,11 @@
title: $:/core/ui/EditTemplate/shadowWarning
tags: $:/tags/EditTemplate
\define lingo-base() $:/language/EditTemplate/
<$list filter="[all[current]get[draft.of]is[shadow]]">
<div class="tw-message-box">
<<lingo Body/ShadowWarning>>
</div>
</$list>

View File

@ -1,2 +1,2 @@
title: $:/tags/EditTemplate title: $:/tags/EditTemplate
list: [[$:/core/ui/EditTemplate/controls]] [[$:/core/ui/EditTemplate/title]] [[$:/core/ui/EditTemplate/tags]] [[$:/core/ui/ViewTemplate/classic]] [[$:/core/ui/EditTemplate/body]] [[$:/core/ui/EditTemplate/type]] [[$:/core/ui/EditTemplate/fields]] list: [[$:/core/ui/EditTemplate/controls]] [[$:/core/ui/EditTemplate/title]] [[$:/core/ui/EditTemplate/tags]] [[$:/core/ui/EditTemplate/shadowWarning]] [[$:/core/ui/ViewTemplate/classic]] [[$:/core/ui/EditTemplate/body]] [[$:/core/ui/EditTemplate/type]] [[$:/core/ui/EditTemplate/fields]]