From d46872c061c23876ce7e5a5632eea6f863282143 Mon Sep 17 00:00:00 2001 From: saqimtiaz Date: Sat, 27 Jun 2020 13:32:11 +0200 Subject: [PATCH] Extend tm-import-tiddlers message (#4741) * Allow tm-import-tiddlers event to override tv-auto-open-on-import * Add autoOpenOnImport attribute to dropzone * Updated DropZone docs * Updated dropzone to support importTitle attribute * Updated navigator to support importTitle attribute on tm-import-tiddlers * Update dropzone docs for importTitle attribute * Updated docs for tm-import-tiddlers * Fixed formatting --- core/modules/widgets/dropzone.js | 10 ++++++---- core/modules/widgets/navigator.js | 16 +++++++++------- .../WidgetMessage_ tm-import-tiddlers.tid | 2 ++ .../tw5.com/tiddlers/widgets/DropzoneWidget.tid | 2 ++ 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/core/modules/widgets/dropzone.js b/core/modules/widgets/dropzone.js index 60d1ca867..08af202bb 100644 --- a/core/modules/widgets/dropzone.js +++ b/core/modules/widgets/dropzone.js @@ -113,7 +113,7 @@ DropZoneWidget.prototype.handleDragEndEvent = function(event) { DropZoneWidget.prototype.handleDropEvent = function(event) { var self = this, readFileCallback = function(tiddlerFieldsArray) { - self.dispatchEvent({type: "tm-import-tiddlers", param: JSON.stringify(tiddlerFieldsArray)}); + self.dispatchEvent({type: "tm-import-tiddlers", param: JSON.stringify(tiddlerFieldsArray), autoOpenOnImport: self.autoOpenOnImport, importTitle: self.importTitle}); }; this.leaveDrag(event); // Check for being over a TEXTAREA or INPUT @@ -149,7 +149,7 @@ DropZoneWidget.prototype.handleDropEvent = function(event) { DropZoneWidget.prototype.handlePasteEvent = function(event) { var self = this, readFileCallback = function(tiddlerFieldsArray) { - self.dispatchEvent({type: "tm-import-tiddlers", param: JSON.stringify(tiddlerFieldsArray)}); + self.dispatchEvent({type: "tm-import-tiddlers", param: JSON.stringify(tiddlerFieldsArray), autoOpenOnImport: self.autoOpenOnImport, importTitle: self.importTitle}); }; // Let the browser handle it if we're in a textarea or input box if(["TEXTAREA","INPUT"].indexOf(event.target.tagName) == -1 && !event.target.isContentEditable) { @@ -176,7 +176,7 @@ DropZoneWidget.prototype.handlePasteEvent = function(event) { if($tw.log.IMPORT) { console.log("Importing string '" + str + "', type: '" + type + "'"); } - self.dispatchEvent({type: "tm-import-tiddlers", param: JSON.stringify([tiddlerFields])}); + self.dispatchEvent({type: "tm-import-tiddlers", param: JSON.stringify([tiddlerFields]), autoOpenOnImport: self.autoOpenOnImport, importTitle: self.importTitle}); }); } } @@ -193,6 +193,8 @@ DropZoneWidget.prototype.execute = function() { this.dropzoneClass = this.getAttribute("class"); this.dropzoneDeserializer = this.getAttribute("deserializer"); this.dropzoneEnable = (this.getAttribute("enable") || "yes") === "yes"; + this.autoOpenOnImport = this.getAttribute("autoOpenOnImport"); + this.importTitle = this.getAttribute("importTitle"); // Make child widgets this.makeChildWidgets(); }; @@ -202,7 +204,7 @@ Selectively refreshes the widget if needed. Returns true if the widget or any of */ DropZoneWidget.prototype.refresh = function(changedTiddlers) { var changedAttributes = this.computeAttributes(); - if(changedAttributes.enable) { + if(changedAttributes.enable || changedAttributes.autoOpenOnImport || changedAttributes.importTitle || changedAttributes.deserializer || changedAttributes.class) { this.refreshSelf(); return true; } diff --git a/core/modules/widgets/navigator.js b/core/modules/widgets/navigator.js index 4e7a871e0..df7e2475b 100755 --- a/core/modules/widgets/navigator.js +++ b/core/modules/widgets/navigator.js @@ -494,10 +494,11 @@ NavigatorWidget.prototype.handleImportTiddlersEvent = function(event) { } catch(e) { } // Get the current $:/Import tiddler - var importTiddler = this.wiki.getTiddler(IMPORT_TITLE), - importData = this.wiki.getTiddlerData(IMPORT_TITLE,{}), + var importTitle = event.importTitle ? event.importTitle : IMPORT_TITLE, + importTiddler = this.wiki.getTiddler(importTitle), + importData = this.wiki.getTiddlerData(importTitle,{}), newFields = new Object({ - title: IMPORT_TITLE, + title: importTitle, type: "application/json", "plugin-type": "import", "status": "pending" @@ -528,15 +529,16 @@ NavigatorWidget.prototype.handleImportTiddlersEvent = function(event) { newFields.text = JSON.stringify(importData,null,$tw.config.preferences.jsonSpaces); this.wiki.addTiddler(new $tw.Tiddler(importTiddler,newFields)); // Update the story and history details - if(this.getVariable("tv-auto-open-on-import") !== "no") { + var autoOpenOnImport = event.autoOpenOnImport ? event.autoOpenOnImport : this.getVariable("tv-auto-open-on-import"); + if(autoOpenOnImport !== "no") { var storyList = this.getStoryList(), history = []; // Add it to the story - if(storyList && storyList.indexOf(IMPORT_TITLE) === -1) { - storyList.unshift(IMPORT_TITLE); + if(storyList && storyList.indexOf(importTitle) === -1) { + storyList.unshift(importTitle); } // And to history - history.push(IMPORT_TITLE); + history.push(importTitle); // Save the updated story and history this.saveStoryList(storyList); this.addToHistory(history); diff --git a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-import-tiddlers.tid b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-import-tiddlers.tid index bc7220a50..dd1581322 100644 --- a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-import-tiddlers.tid +++ b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-import-tiddlers.tid @@ -9,6 +9,8 @@ The `tm-import-tiddlers` message inserts a list of tiddlers into the pending imp |!Name |!Description | |param |JSON text of the array of tiddlers to be imported | +|autoOpenOnImport |<<.from-version "5.1.23">> Optional value "no" or "yes" that can override tv-auto-open-on-import | +|importTitle|<<.from-version "5.1.23">> optional tiddler title to use for import process instead of ~$:/Import | The import tiddlers message is usually generated with the DropzoneWidget or the BrowseWidget, and is handled by the NavigatorWidget. diff --git a/editions/tw5.com/tiddlers/widgets/DropzoneWidget.tid b/editions/tw5.com/tiddlers/widgets/DropzoneWidget.tid index 225823ec6..1794cf98d 100644 --- a/editions/tw5.com/tiddlers/widgets/DropzoneWidget.tid +++ b/editions/tw5.com/tiddlers/widgets/DropzoneWidget.tid @@ -17,6 +17,8 @@ It sends a [[WidgetMessage: tm-import-tiddlers]] carrying a JSON representation |deserializer |<<.from-version "5.1.15">> Optional name of deserializer to be used (by default the deserializer is derived from the file extension) | |enable |<<.from-version "5.1.22">> Optional value "no" to disable the dropzone functionality (defaults to "yes") | |class |<<.from-version "5.1.22">> Optional CSS class to be assigned to the dropzone (defaults to "tc-drag-over") | +|autoOpenOnImport |<<.from-version "5.1.23">> Optional value "no" or "yes" that can override tv-auto-open-on-import | +|importTitle|<<.from-version "5.1.23">> optional tiddler title to use for import process instead of ~$:/Import | The list of available deserializers can be inspected by executing `Object.keys($tw.Wiki.tiddlerDeserializerModules).sort().join("\n")` in the browser JavaScript console.