From 424b2fea3279c14daac0b0ca2baabc7c8ad7d41e Mon Sep 17 00:00:00 2001 From: Jermolene Date: Tue, 8 Sep 2015 16:16:50 +0100 Subject: [PATCH] Add th-saving-tiddler hook for autotagging etc. @danielo515 this enhancement is intended to make it easier to implement things like the auto tag plugin. Does it meet your needs? --- core/modules/startup/story.js | 2 +- core/modules/widgets/navigator.js | 8 ++++--- ...d => th-opening-default-tiddlers-list.tid} | 2 +- .../dev/tiddlers/new/th-saving-tiddler.tid | 22 +++++++++++++++++++ 4 files changed, 29 insertions(+), 5 deletions(-) rename editions/dev/tiddlers/new/{tc-opening-default-tiddlers-list.tid => th-opening-default-tiddlers-list.tid} (92%) create mode 100644 editions/dev/tiddlers/new/th-saving-tiddler.tid diff --git a/core/modules/startup/story.js b/core/modules/startup/story.js index ec0202c1f..987bee216 100644 --- a/core/modules/startup/story.js +++ b/core/modules/startup/story.js @@ -118,7 +118,7 @@ function openStartupTiddlers(options) { } // Process the story filter to get the story list var storyList = $tw.wiki.filterTiddlers(storyFilter); - //invoke any hooks that might change the default story list + // Invoke any hooks that want to change the default story list storyList = $tw.hooks.invokeHook("th-opening-default-tiddlers-list",storyList); // If the target tiddler isn't included then splice it in at the top if(target && storyList.indexOf(target) === -1) { diff --git a/core/modules/widgets/navigator.js b/core/modules/widgets/navigator.js index 082500a45..c6aab7aee 100755 --- a/core/modules/widgets/navigator.js +++ b/core/modules/widgets/navigator.js @@ -341,12 +341,14 @@ NavigatorWidget.prototype.handleSaveTiddlerEvent = function(event) { )); } if(isConfirmed) { - // Save the draft tiddler as the real tiddler - this.wiki.addTiddler(new $tw.Tiddler(this.wiki.getCreationFields(),tiddler,{ + // Create the new tiddler and pass it through the th-saving-tiddler hook + var newTiddler = new $tw.Tiddler(this.wiki.getCreationFields(),tiddler,{ title: draftTitle, "draft.title": undefined, "draft.of": undefined - },this.wiki.getModificationFields())); + },this.wiki.getModificationFields()); + newTiddler = $tw.hooks.invokeHook("th-saving-tiddler",newTiddler); + this.wiki.addTiddler(newTiddler); // Remove the draft tiddler this.wiki.deleteTiddler(title); // Remove the original tiddler if we're renaming it diff --git a/editions/dev/tiddlers/new/tc-opening-default-tiddlers-list.tid b/editions/dev/tiddlers/new/th-opening-default-tiddlers-list.tid similarity index 92% rename from editions/dev/tiddlers/new/tc-opening-default-tiddlers-list.tid rename to editions/dev/tiddlers/new/th-opening-default-tiddlers-list.tid index b44c0ff90..7c848aeca 100644 --- a/editions/dev/tiddlers/new/tc-opening-default-tiddlers-list.tid +++ b/editions/dev/tiddlers/new/th-opening-default-tiddlers-list.tid @@ -1,6 +1,6 @@ created: 20141122200310516 modified: 20141122200310516 -title: Hook: tc-opening-default-tiddlers-list +title: Hook: th-opening-default-tiddlers-list type: text/vnd.tiddlywiki This hook allows plugins to add to or remove from the list of tiddlers that are opened when the wiki is first loaded or the home button is clicked. diff --git a/editions/dev/tiddlers/new/th-saving-tiddler.tid b/editions/dev/tiddlers/new/th-saving-tiddler.tid new file mode 100644 index 000000000..82e37756e --- /dev/null +++ b/editions/dev/tiddlers/new/th-saving-tiddler.tid @@ -0,0 +1,22 @@ +created: 20150908150314994 +modified: 20150908150314994 +title: Hook: th-saving-tiddler +type: text/vnd.tiddlywiki + +This hook allows plugins to modify tiddlers before they are saved via the ''confirm'' toolbar button; the hook is not invoked for tiddlers that are saved through other means, such as state tiddlers created by the ActionSetFieldWidget. + +Hook function parameters: + +* ''tiddler'': tiddler object about to be saved + +Return value: + +* tiddler object to be saved + +The original tiddler object can be returned unmodified by the hook. If the hook needs to modify the tiddler then it should return a new tiddler object, for example: + +``` + return new $tw.Tiddler(tiddler,{"my-field": value}); +``` + +Hooks must not change the ''title'' field but can freely modify any other field of the tiddler.