From 2397f0aa6f7af305807b61ccb1d435c2e6a789ab Mon Sep 17 00:00:00 2001 From: Jermolene Date: Thu, 9 Feb 2017 15:42:55 +0000 Subject: [PATCH] Add several new hooks for UI actions --- core/modules/widgets/navigator.js | 11 +++++++-- core/modules/wiki-bulkops.js | 10 +++++--- editions/dev/tiddlers/new/HookMechanism.tid | 2 +- .../new/Hook__th-deleting-tiddler.tid | 17 +++++++++++++ .../new/Hook__th-importing-tiddler.tid | 23 ++++++++++++++++++ .../new/Hook__th-relinking-tiddler.tid | 24 +++++++++++++++++++ .../new/Hook__th-renaming-tiddler.tid | 24 +++++++++++++++++++ .../new/th-opening-default-tiddlers-list.tid | 11 +++++++-- .../dev/tiddlers/new/th-saving-tiddler.tid | 5 ++-- 9 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 editions/dev/tiddlers/new/Hook__th-deleting-tiddler.tid create mode 100644 editions/dev/tiddlers/new/Hook__th-importing-tiddler.tid create mode 100644 editions/dev/tiddlers/new/Hook__th-relinking-tiddler.tid create mode 100644 editions/dev/tiddlers/new/Hook__th-renaming-tiddler.tid diff --git a/core/modules/widgets/navigator.js b/core/modules/widgets/navigator.js index 669b22dad..f27da6f79 100755 --- a/core/modules/widgets/navigator.js +++ b/core/modules/widgets/navigator.js @@ -245,6 +245,7 @@ NavigatorWidget.prototype.handleDeleteTiddlerEvent = function(event) { tiddler = this.wiki.getTiddler(title), storyList = this.getStoryList(), originalTitle = tiddler ? tiddler.fields["draft.of"] : "", + originalTiddler = originalTitle ? this.wiki.getTiddler(originalTitle) : undefined, confirmationTitle; if(!tiddler) { return false; @@ -268,10 +269,14 @@ NavigatorWidget.prototype.handleDeleteTiddlerEvent = function(event) { } // Delete the original tiddler if(originalTitle) { + if(originalTiddler) { + $tw.hooks.invokeHook("th-deleting-tiddler",originalTiddler); + } this.wiki.deleteTiddler(originalTitle); this.removeTitleFromStory(storyList,originalTitle); } - // Delete this tiddler + // Invoke the hook function and delete this tiddler + $tw.hooks.invokeHook("th-deleting-tiddler",tiddler); this.wiki.deleteTiddler(title); // Remove the closed tiddler from the story this.removeTitleFromStory(storyList,title); @@ -568,7 +573,9 @@ NavigatorWidget.prototype.handlePerformImportEvent = function(event) { $tw.utils.each(importData.tiddlers,function(tiddlerFields) { var title = tiddlerFields.title; if(title && importTiddler && importTiddler.fields["selection-" + title] !== "unchecked") { - self.wiki.addTiddler(new $tw.Tiddler(tiddlerFields)); + var tiddler = new $tw.Tiddler(tiddlerFields); + tiddler = $tw.hooks.invokeHook("th-importing-tiddler",tiddler); + self.wiki.addTiddler(tiddler); importReport.push("# [[" + tiddlerFields.title + "]]"); } }); diff --git a/core/modules/wiki-bulkops.js b/core/modules/wiki-bulkops.js index ce660048f..fa2acbe19 100644 --- a/core/modules/wiki-bulkops.js +++ b/core/modules/wiki-bulkops.js @@ -21,8 +21,10 @@ function renameTiddler(fromTitle,toTitle,options) { options = options || {}; if(fromTitle && toTitle && fromTitle !== toTitle) { // Rename the tiddler itself - var tiddler = this.getTiddler(fromTitle); - this.addTiddler(new $tw.Tiddler(tiddler,{title: toTitle},this.getModificationFields())); + 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) @@ -66,7 +68,9 @@ console.log("Renaming list item '" + list[index] + "' to '" + toTitle + "' of ti }); } if(isModified) { - self.addTiddler(new $tw.Tiddler(tiddler,{tags: tags, list: list},self.getModificationFields())); + var newTiddler = new $tw.Tiddler(tiddler,{tags: tags, list: list},self.getModificationFields()) + newTiddler = $tw.hooks.invokeHook("th-relinking-tiddler",newTiddler,tiddler); + self.addTiddler(newTiddler); } } }); diff --git a/editions/dev/tiddlers/new/HookMechanism.tid b/editions/dev/tiddlers/new/HookMechanism.tid index 93b7a1cb4..82883d16d 100644 --- a/editions/dev/tiddlers/new/HookMechanism.tid +++ b/editions/dev/tiddlers/new/HookMechanism.tid @@ -1,5 +1,5 @@ created: 20141122200310516 -modified: 20141122200310516 +modified: 20170209130807520 title: HookMechanism type: text/vnd.tiddlywiki diff --git a/editions/dev/tiddlers/new/Hook__th-deleting-tiddler.tid b/editions/dev/tiddlers/new/Hook__th-deleting-tiddler.tid new file mode 100644 index 000000000..dc482c1b8 --- /dev/null +++ b/editions/dev/tiddlers/new/Hook__th-deleting-tiddler.tid @@ -0,0 +1,17 @@ +created: 20170209115611070 +modified: 20170209145906743 +tags: HookMechanism +title: Hook: th-deleting-tiddler +type: text/vnd.tiddlywiki + +This hook allows plugins to inspect tiddlers before they are deleted via the ''delete'' toolbar button. When the delete button is used from the edit toolbar there are actually two invocations of the `th-deleting-tiddler` hook function: one for the original tiddler and one for the draft. + +Note that this hook is not invoked for tiddlers deleted by other means such as the ActionDeleteTiddlerWidget. + +Hook function parameters: + +* ''tiddler'': tiddler object about to be deleted + +Return value: + +* unmodified tiddler to be deleted diff --git a/editions/dev/tiddlers/new/Hook__th-importing-tiddler.tid b/editions/dev/tiddlers/new/Hook__th-importing-tiddler.tid new file mode 100644 index 000000000..3ed1fb721 --- /dev/null +++ b/editions/dev/tiddlers/new/Hook__th-importing-tiddler.tid @@ -0,0 +1,23 @@ +created: 20170209130829546 +modified: 20170209145518777 +tags: HookMechanism +title: Hook: th-importing-tiddler +type: text/vnd.tiddlywiki + +This hook allows plugins to inspect or modify tiddlers before they are imported via the import mechanism. + +Hook function parameters: + +* ''tiddler'': tiddler object about to be imported + +Return value: + +* tiddler object to be imported + +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. diff --git a/editions/dev/tiddlers/new/Hook__th-relinking-tiddler.tid b/editions/dev/tiddlers/new/Hook__th-relinking-tiddler.tid new file mode 100644 index 000000000..c09b99a85 --- /dev/null +++ b/editions/dev/tiddlers/new/Hook__th-relinking-tiddler.tid @@ -0,0 +1,24 @@ +created: 20170209145637233 +modified: 20170209150007550 +tags: HookMechanism +title: Hook: th-relinking-tiddler +type: text/vnd.tiddlywiki + +This hook allows plugins to inspect tiddlers before they are relinked ("relinking" is the optional operation of relinking references to a tiddler when it is renamed). + +Hook function parameters: + +* ''newTiddler'': tiddler object incorporating the relinking +* ''oldTiddler'': optional existing tiddler object that will be overwritten + +Return value: + +* ''newTiddler'': tiddler object to be used for the relinking operation. + +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. diff --git a/editions/dev/tiddlers/new/Hook__th-renaming-tiddler.tid b/editions/dev/tiddlers/new/Hook__th-renaming-tiddler.tid new file mode 100644 index 000000000..f1805c6a8 --- /dev/null +++ b/editions/dev/tiddlers/new/Hook__th-renaming-tiddler.tid @@ -0,0 +1,24 @@ +created: 20170209145207186 +modified: 20170209145633522 +tags: HookMechanism +title: Hook: th-renaming-tiddler +type: text/vnd.tiddlywiki + +This hook allows plugins to inspect tiddlers before they are modified by the `tm-rename-tiddler` message. + +Hook function parameters: + +* ''newTiddler'': tiddler object incorporating the rename +* ''oldTiddler'': optional existing tiddler object that will be overwritten + +Return value: + +* newTiddler: tiddler object to be used for the renaming operation. + +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. diff --git a/editions/dev/tiddlers/new/th-opening-default-tiddlers-list.tid b/editions/dev/tiddlers/new/th-opening-default-tiddlers-list.tid index 7c848aeca..2a79f5585 100644 --- a/editions/dev/tiddlers/new/th-opening-default-tiddlers-list.tid +++ b/editions/dev/tiddlers/new/th-opening-default-tiddlers-list.tid @@ -1,10 +1,17 @@ created: 20141122200310516 -modified: 20141122200310516 +modified: 20170209115548070 +tags: HookMechanism 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. -The function takes a list of tiddlers as its only argument and returns a modified list of tiddler titles to display. +Hook function parameters: + +* ''list'': array of tiddler titles to be opened + +Return value: + +* modified array of tiddler titles to be opened Note that this hook is invoked with the tiddler titles that are generated from the filter in [[$:/DefaultTiddlers]]. Any added entries must be tiddler titles, not filter expressions. diff --git a/editions/dev/tiddlers/new/th-saving-tiddler.tid b/editions/dev/tiddlers/new/th-saving-tiddler.tid index 82e37756e..81b0a54d5 100644 --- a/editions/dev/tiddlers/new/th-saving-tiddler.tid +++ b/editions/dev/tiddlers/new/th-saving-tiddler.tid @@ -1,9 +1,10 @@ created: 20150908150314994 -modified: 20150908150314994 +modified: 20170209145506427 +tags: HookMechanism 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. +This hook allows plugins to inspect or 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: