Skip to content

Commit 5f8ff66

Browse files
author
Atterpac
committed
pass all attributes to golang on dragdrop
1 parent c92b13e commit 5f8ff66

7 files changed

Lines changed: 55 additions & 34 deletions

File tree

v3/examples/drag-n-drop/assets/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,13 @@ <h1>Wails v3 Drag-n-Drop Demo</h1>
113113

114114
wails.Events.On("frontend:FileDropInfo", (eventData) => {
115115
console.log("JS: Received 'frontend:FileDropInfo' event with data:", eventData);
116-
const { files, targetID, targetClasses, dropX, dropY } = eventData.data[0]; // Access the first element of the data array
116+
const { files, targetID, targetClasses, dropX, dropY, attributes } = eventData.data[0]; // Access the first element of the data array
117117

118118
let message = `Dropped on Element ID: ${targetID || 'N/A'}\n`;
119119
message += `Element Classes: ${targetClasses && targetClasses.length > 0 ? targetClasses.join(', ') : 'N/A'}\n`;
120+
if (attributes) {
121+
message += `Element Attributes: ${Object.entries(attributes).map(([key, value]) => `${key}="${value}"`).join(', ')}\n`;
122+
}
120123
message += `Files Dropped: ${files.join(', ')}\n`;
121124
message += `Coordinates (from Go): X=${dropX.toFixed(2)}, Y=${dropY.toFixed(2)}`;
122125

v3/examples/drag-n-drop/main.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ func (a *App) Startup(ctx context.Context) {
3434

3535
// FileDropInfo defines the payload for the file drop event sent to the frontend.
3636
type FileDropInfo struct {
37-
Files []string `json:"files"`
38-
TargetID string `json:"targetID"`
39-
TargetClasses []string `json:"targetClasses"`
40-
DropX float64 `json:"dropX"`
41-
DropY float64 `json:"dropY"`
37+
Files []string `json:"files"`
38+
TargetID string `json:"targetID"`
39+
TargetClasses []string `json:"targetClasses"`
40+
DropX float64 `json:"dropX"`
41+
DropY float64 `json:"dropY"`
42+
Attributes map[string]string `json:"attributes,omitempty"`
4243
}
4344

4445
// FilesDroppedOnTarget is called when files are dropped onto a registered drop target
@@ -124,6 +125,7 @@ func main() {
124125
TargetClasses: details.ClassList,
125126
DropX: float64(details.X),
126127
DropY: float64(details.Y),
128+
Attributes: details.Attributes, // Add the attributes
127129
}
128130
application.Get().EmitEvent("frontend:FileDropInfo", payload)
129131
},

v3/internal/assetserver/bundledassets/runtime.debug.js

Lines changed: 11 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v3/internal/assetserver/bundledassets/runtime.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v3/internal/runtime/desktop/@wailsio/runtime/src/window.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -546,21 +546,24 @@ class Window {
546546
}
547547

548548
console.log(`Wails Runtime: Drop on designated dropzone. Element at (${x}, ${y}):`, element, 'Effective dropzone:', dropzoneTarget);
549-
// The 'element' variable is already defined from the line: const element = document.elementFromPoint(x, y);
550-
// which should be before the dropzoneTarget check. The original console.log below also uses it.
551-
console.log(`Window.HandlePlatformFileDrop: Original log - Dropped files at (${x}, ${y}) on element:`, element);
552-
const elementId = element ? element.id : '';
553-
const classList = element ? Array.from(element.classList) : [];
549+
const elementDetails = {
550+
id: dropzoneTarget.id,
551+
classList: Array.from(dropzoneTarget.classList),
552+
attributes: {} as { [key: string]: string },
553+
};
554+
for (let i = 0; i < dropzoneTarget.attributes.length; i++) {
555+
const attr = dropzoneTarget.attributes[i];
556+
elementDetails.attributes[attr.name] = attr.value;
557+
}
554558

555559
const payload = {
556560
filenames,
557561
x,
558562
y,
559-
elementId,
560-
classList,
563+
elementDetails,
561564
};
562565

563-
this[callerSym](WindowDropZoneDropped,payload)
566+
this[callerSym](WindowDropZoneDropped, payload);
564567
}
565568
}
566569

v3/pkg/application/application.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,11 @@ var windowMessageBuffer = make(chan *windowMessage, 5)
227227
// DropZoneDetails contains information about the HTML element
228228
// at the location of a file drop.
229229
type DropZoneDetails struct {
230-
X int `json:"x"`
231-
Y int `json:"y"`
232-
ElementID string `json:"id"`
233-
ClassList []string `json:"classList"`
230+
X int `json:"x"`
231+
Y int `json:"y"`
232+
ElementID string `json:"id"`
233+
ClassList []string `json:"classList"`
234+
Attributes map[string]string `json:"attributes,omitempty"`
234235
}
235236

236237
type dragAndDropMessage struct {

v3/pkg/application/messageprocessor_window.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,11 @@ func (m *MessageProcessor) processWindowMethod(
446446
)
447447

448448
dropDetails := &DropZoneDetails{
449-
X: payload.X,
450-
Y: payload.Y,
451-
ElementID: payload.ElementID,
452-
ClassList: payload.ClassList,
449+
X: payload.X,
450+
Y: payload.Y,
451+
ElementID: payload.ElementDetails.ID,
452+
ClassList: payload.ElementDetails.ClassList,
453+
Attributes: payload.ElementDetails.Attributes, // Assumes DropZoneDetails struct is updated to include this field
453454
}
454455

455456
wvWindow, ok := window.(*WebviewWindow)
@@ -483,11 +484,17 @@ func (m *MessageProcessor) processWindowMethod(
483484
m.Info("Runtime call:", "method", "Window."+windowMethodNames[method])
484485
}
485486

487+
// ElementDetailsPayload holds detailed information about the drop target element.
488+
type ElementDetailsPayload struct {
489+
ID string `json:"id"`
490+
ClassList []string `json:"classList"`
491+
Attributes map[string]string `json:"attributes"`
492+
}
493+
486494
// Define a struct for the JSON payload from HandlePlatformFileDrop
487495
type fileDropPayload struct {
488-
Filenames []string `json:"filenames"`
489-
X int `json:"x"`
490-
Y int `json:"y"`
491-
ElementID string `json:"elementId"`
492-
ClassList []string `json:"classList"`
496+
Filenames []string `json:"filenames"`
497+
X int `json:"x"`
498+
Y int `json:"y"`
499+
ElementDetails ElementDetailsPayload `json:"elementDetails"`
493500
}

0 commit comments

Comments
 (0)