From 8b9723f6c7a48d1d01f35719e3a6f0f7bd85b6ea Mon Sep 17 00:00:00 2001 From: Mario Pietsch Date: Thu, 24 Oct 2024 18:51:19 +0200 Subject: [PATCH] Add action tiddler list to droppable widget (#8256) * initial POC * clean up the code * Add documentation changes * Improve docs --- core/modules/widgets/droppable.js | 31 ++++++++++--- .../tiddlers/actionTiddlerList Variable.tid | 9 ++++ .../variables/actionTiddler Variable.tid | 4 +- .../tiddlers/widgets/DroppableWidget.tid | 45 +++++++++++++------ 4 files changed, 69 insertions(+), 20 deletions(-) create mode 100644 editions/tw5.com/tiddlers/actionTiddlerList Variable.tid diff --git a/core/modules/widgets/droppable.js b/core/modules/widgets/droppable.js index 0dcba1688..ba54d8860 100644 --- a/core/modules/widgets/droppable.js +++ b/core/modules/widgets/droppable.js @@ -125,11 +125,23 @@ DroppableWidget.prototype.handleDropEvent = function(event) { // Remove highlighting $tw.utils.removeClass(this.domNodes[0],"tc-dragover"); // Try to import the various data types we understand - $tw.utils.importDataTransfer(dataTransfer,null,function(fieldsArray) { - fieldsArray.forEach(function(fields) { - self.performActions(fields.title || fields.text,event); + if(this.droppableActions) { + $tw.utils.importDataTransfer(dataTransfer,null,function(fieldsArray) { + fieldsArray.forEach(function(fields) { + self.performActions(fields.title || fields.text,event); + }); }); - }); + } + // Send a TitleList to performListActions + if(this.droppableListActions) { + $tw.utils.importDataTransfer(dataTransfer,null,function(fieldsArray) { + var titleList = []; + fieldsArray.forEach(function(fields) { + titleList.push(fields.title || fields.text); + }); + self.performListActions($tw.utils.stringifyList(titleList),event); + }); + } // Tell the browser that we handled the drop event.preventDefault(); // Stop the drop ripple up to any parent handlers @@ -137,6 +149,13 @@ DroppableWidget.prototype.handleDropEvent = function(event) { return false; }; +DroppableWidget.prototype.performListActions = function(titleList,event) { + if(this.droppableListActions) { + var modifierKey = $tw.keyboardManager.getEventModifierKeyDescriptor(event); + this.invokeActionString(this.droppableListActions,this,event,{actionTiddlerList: titleList, modifier: modifierKey}); + } +}; + DroppableWidget.prototype.performActions = function(title,event) { if(this.droppableActions) { var modifierKey = $tw.keyboardManager.getEventModifierKeyDescriptor(event); @@ -149,6 +168,7 @@ Compute the internal state of the widget */ DroppableWidget.prototype.execute = function() { this.droppableActions = this.getAttribute("actions"); + this.droppableListActions = this.getAttribute("listActions"); this.droppableEffect = this.getAttribute("effect","copy"); this.droppableTag = this.getAttribute("tag"); this.droppableEnable = (this.getAttribute("enable") || "yes") === "yes"; @@ -168,7 +188,8 @@ Selectively refreshes the widget if needed. Returns true if the widget or any of */ DroppableWidget.prototype.refresh = function(changedTiddlers) { var changedAttributes = this.computeAttributes(); - if(changedAttributes.tag || changedAttributes.enable || changedAttributes.disabledClass || changedAttributes.actions || changedAttributes.effect) { + if(changedAttributes.tag || changedAttributes.enable || changedAttributes.disabledClass || + changedAttributes.actions|| changedAttributes.listActions || changedAttributes.effect) { this.refreshSelf(); return true; } else { diff --git a/editions/tw5.com/tiddlers/actionTiddlerList Variable.tid b/editions/tw5.com/tiddlers/actionTiddlerList Variable.tid new file mode 100644 index 000000000..74b9e0386 --- /dev/null +++ b/editions/tw5.com/tiddlers/actionTiddlerList Variable.tid @@ -0,0 +1,9 @@ +created: 20240612115323606 +modified: 20240612115535069 +tags: [[Core Variables]] Variables +title: actionTiddlerList Variable +type: text/vnd.tiddlywiki + +The variable `actionTiddlerList` is used: + +* Within the ''listActions'' string of the DroppableWidget, the <<.def actionTiddlerList>> [[variable|Variables]] contains a [[Title List]] of the tiddlers being dropped. diff --git a/editions/tw5.com/tiddlers/variables/actionTiddler Variable.tid b/editions/tw5.com/tiddlers/variables/actionTiddler Variable.tid index 53e26fa6c..37677d3bb 100644 --- a/editions/tw5.com/tiddlers/variables/actionTiddler Variable.tid +++ b/editions/tw5.com/tiddlers/variables/actionTiddler Variable.tid @@ -1,5 +1,5 @@ created: 20170406083917224 -modified: 20190118084621046 +modified: 20240612115232524 tags: Variables [[Core Variables]] title: actionTiddler Variable type: text/vnd.tiddlywiki @@ -7,4 +7,4 @@ type: text/vnd.tiddlywiki The variable `actionTiddler` is used in subtly different ways by different widgets: * Within the ''actions'' string of the DroppableWidget, the <<.def actionTiddler>> [[variable|Variables]] contains the title of the tiddler being dropped. -* Within the ''startactions'' and ''endactions'' string of the DroppableWidget, the <<.def actionTiddler>> [[variable|Variables]] contains a quoted [[Title List]] of all of the titles being dragged. +* Within the ''startactions'' and ''endactions'' string of the DraggableWidget, the <<.def actionTiddler>> [[variable|Variables]] contains a quoted [[Title List]] of all of the titles being dragged. diff --git a/editions/tw5.com/tiddlers/widgets/DroppableWidget.tid b/editions/tw5.com/tiddlers/widgets/DroppableWidget.tid index 46d46860a..483c80076 100644 --- a/editions/tw5.com/tiddlers/widgets/DroppableWidget.tid +++ b/editions/tw5.com/tiddlers/widgets/DroppableWidget.tid @@ -1,22 +1,27 @@ caption: droppable created: 20170406082820317 -modified: 20231121101447359 +modified: 20240612125454153 tags: Widgets TriggeringWidgets title: DroppableWidget type: text/vnd.tiddlywiki -\define droppable-image-actions() +\procedure drop-on-tags-actions() +<$action-log actionTiddlerList=<>/> +<$action-setfield $tiddler="$:/temp/drop/TitleList" $field="text" text=<> type="text/plain"/> +\end + +\procedure droppable-image-actions() <$action-setfield $tiddler=<> $field="icon" $value=<>/> \end -\define colour-demo-body() +\procedure colour-demo-body() <$droppable actions=<>> - + \end -\define droppable-colour-actions() +\procedure droppable-colour-actions() <$action-setfield $tiddler=<> $field="color" $value=<>/> \end @@ -26,22 +31,23 @@ See DragAndDropMechanism for an overview. ! Content and Attributes -|!Attribute |!Description | -|actions |Actions to be performed when items are dropped | +|! Attribute |! Description | +|actions |Actions to be performed when items are dropped. It activates ''1 action per item'' | +|listActions |<<.from-version "5.3.4">> Actions to be performed when items are dropped. It activates ''1 action for'' a the whole ''list of items'' | |class |Optional CSS classes to assign to the draggable element. The class `tc-droppable` is added automatically, and the class `tc-dragover` is applied while an item is being dragged over the droppable element | |tag |Optional tag to override the default of a "div" element when the widget is rendered in block mode, or a "span" element when it is rendered in inline mode | |enable |<<.from-version "5.1.22">> Optional value "no" to disable the droppable functionality (defaults to "yes") | |data-* |<<.from-version "5.3.2">> Optional [[data attributes|https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes]] to be assigned to the HTML element | |style.* |<<.from-version "5.3.2">> Optional [[CSS properties|https://developer.mozilla.org/en-US/docs/Web/CSS/Reference]] to be assigned to the HTML element | -Within the action string, there are two Variables generated by the DroppableWidget: +''Within the action string'', the following variables are generated by the DroppableWidget: -* The [[actionTiddler Variable]] contains the title of the item being dropped -* The [[modifier Variable]] contains the Modifier Key held while dragging (can be normal, ctrl, shift or ctrl-shift) +|!Variables |!Description | +|`actionTiddler` |For parameter <<.param actions>>, the [[actionTiddler Variable]] contains the title of the item being dropped | +|`actionTiddlerList` |For parameter <<.param listActions>> the [[actionTiddlerList Variable]] contains a [[Title List]] of all the items being dropped | +|`modifier` |The [[modifier Variable]] contains the modifier key held while dragging (can be normal, ctrl, shift or ctrl-shift) | -If multiple items are dropped then the actions are performed repeatedly, once for each dropped item. - -<<.tip """Note that the [[actionTiddler Variable]] holds a single, unquoted title. This is unlike the DraggableWidget which uses the same variable to pass a quoted [[Title List]].""">> +<<.note """Note that the <<.var actionTiddler>> variable holds a single, unquoted title. This is unlike the DraggableWidget which uses the same variable to pass a quoted [[Title List]].""">> ! Examples @@ -53,9 +59,22 @@ This example displays a palette of icons. Dragging a tiddler onto one of the ico +--- + Similarly, this example shows a palette of colours. Dragging a tiddler onto one of the colours assigns that colour to be used for rendering the icon of the tiddler. <$list filter="LightPink Pink Crimson LavenderBlush PaleVioletRed HotPink DeepPink MediumVioletRed Orchid Thistle Plum Violet Magenta Fuchsia DarkMagenta Purple MediumOrchid DarkViolet DarkOrchid Indigo BlueViolet MediumPurple MediumSlateBlue SlateBlue DarkSlateBlue Lavender GhostWhite Blue MediumBlue MidnightBlue DarkBlue Navy RoyalBlue CornflowerBlue LightSteelBlue LightSlateGrey SlateGrey DodgerBlue AliceBlue SteelBlue LightSkyBlue SkyBlue DeepSkyBlue LightBlue PowderBlue CadetBlue Azure LightCyan PaleTurquoise Cyan Aqua DarkTurquoise DarkSlateGrey DarkCyan Teal MediumTurquoise LightSeaGreen Turquoise Aquamarine MediumAquamarine MediumSpringGreen MintCream SpringGreen MediumSeaGreen SeaGreen Honeydew LightGreen PaleGreen DarkSeaGreen LimeGreen Lime ForestGreen Green DarkGreen Chartreuse LawnGreen GreenYellow DarkOliveGreen YellowGreen OliveDrab Beige LightGoldenrodYellow Ivory LightYellow Yellow Olive DarkKhaki LemonChiffon PaleGoldenrod Khaki Gold Cornsilk Goldenrod DarkGoldenrod FloralWhite OldLace Wheat Moccasin Orange PapayaWhip BlanchedAlmond NavajoWhite AntiqueWhite Tan BurlyWood Bisque DarkOrange Linen Peru PeachPuff SandyBrown Chocolate SaddleBrown Seashell Sienna LightSalmon Coral OrangeRed DarkSalmon Tomato MistyRose Salmon Snow LightCoral RosyBrown IndianRed Red Brown FireBrick DarkRed Maroon White WhiteSmoke Gainsboro LightGrey Silver DarkGrey Grey DimGrey Black"> <> +--- + +Drag the tag pill <> or a single tiddler link [[HelloThere]] into the "Drop Area" below. It will show the "Title List" stored in $:/temp/drop/TitleList + +<<.note title:"" +_:"""<$droppable listActions=<> > +''Drop'' the tagpill or a link into this ''area''.
+~TitleList: <$transclude $tiddler="$:/temp/drop/TitleList"/> +""">> + +