1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-04-06 10:46:57 +00:00

Merge 7aa82c4c57bc05016d49399e91d83781379474ee into 961e74f73d230d0028efb586db07699120eac888

This commit is contained in:
Saq Imtiaz 2025-03-25 20:34:08 +01:00 committed by GitHub
commit 66b437528d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 64 additions and 23 deletions

View File

@ -349,25 +349,29 @@ exports.collectDOMVariables = function(selectedNode,domNode,event) {
variables["tv-widgetnode-width"] = domNode.offsetWidth.toString();
variables["tv-widgetnode-height"] = domNode.offsetHeight.toString();
}
if(event) {
var eventProperties = $tw.utils.copyObjectPropertiesSafe(event);
variables["event-properties"] = JSON.stringify(eventProperties,null,2);
if(event && ("clientX" in event) && ("clientY" in event)) {
if(selectedNode) {
// Add variables for event X and Y position relative to selected node
selectedNodeRect = selectedNode.getBoundingClientRect();
variables["event-fromselected-posx"] = (event.clientX - selectedNodeRect.left).toString();
variables["event-fromselected-posy"] = (event.clientY - selectedNodeRect.top).toString();
}
if(domNode) {
// Add variables for event X and Y position relative to event catcher node
domNodeRect = domNode.getBoundingClientRect();
variables["event-fromcatcher-posx"] = (event.clientX - domNodeRect.left).toString();
variables["event-fromcatcher-posy"] = (event.clientY - domNodeRect.top).toString();
}
if(("clientX" in event) && ("clientY" in event)) {
if(selectedNode) {
// Add variables for event X and Y position relative to selected node
selectedNodeRect = selectedNode.getBoundingClientRect();
variables["event-fromselected-posx"] = (event.clientX - selectedNodeRect.left).toString();
variables["event-fromselected-posy"] = (event.clientY - selectedNodeRect.top).toString();
}
// Add variables for event X and Y position relative to the viewport
variables["event-fromviewport-posx"] = event.clientX.toString();
variables["event-fromviewport-posy"] = event.clientY.toString();
if(domNode) {
// Add variables for event X and Y position relative to event catcher node
domNodeRect = domNode.getBoundingClientRect();
variables["event-fromcatcher-posx"] = (event.clientX - domNodeRect.left).toString();
variables["event-fromcatcher-posy"] = (event.clientY - domNodeRect.top).toString();
}
// Add variables for event X and Y position relative to the viewport
variables["event-fromviewport-posx"] = event.clientX.toString();
variables["event-fromviewport-posy"] = event.clientY.toString();
}
}
return variables;
};

View File

@ -289,6 +289,48 @@ exports.extendDeepCopy = function(object,extendedProperties) {
return result;
};
exports.copyObjectPropertiesSafe = function(object) {
var seen = new Set();
function isDOMElement(value) {
return value instanceof Node || value instanceof Window;
}
function safeCopy(obj) {
//skip ciruclar references
if(seen.has(obj)) {
return "[Circular reference]";
}
//skip functions
if(typeof obj !== "object" || obj === null) {
return obj;
}
//skip DOM elements
if(isDOMElement(obj)) {
return "[DOM Element]";
}
//copy each element of the array
if(Array.isArray(obj)) {
return obj.map(safeCopy);
}
seen.add(obj);
var copy = {}, key;
for(key in obj) {
try{
copy[key] = safeCopy(obj[key]);
} catch(e) {
copy[key] = "[Unserializable]";
}
}
return copy;
}
var result = safeCopy(object);
seen.clear();
return result;
};
exports.deepFreeze = function deepFreeze(object) {
var property, key;
if(object) {

View File

@ -47,8 +47,6 @@ EventWidget.prototype.render = function(parent,nextSibling) {
actions = self.getAttribute("$"+type) || self.getAttribute("actions-"+type),
stopPropagation = self.getAttribute("stopPropagation","onaction"),
selectedNode = event.target,
selectedNodeRect,
catcherNodeRect,
variables = {};
// Firefox can fire dragover and dragenter events on text nodes instead of their parents
if(selectedNode.nodeType === 3) {
@ -67,13 +65,10 @@ EventWidget.prototype.render = function(parent,nextSibling) {
if(selectedNode === domNode) {
return false;
}
// Only set up variables if we have actions to invoke
if(actions) {
variables = $tw.utils.collectDOMVariables(selectedNode,self.domNode,event);
}
}
// Execute our actions with the variables
if(actions) {
variables = $tw.utils.collectDOMVariables(selectedNode,self.domNode,event);
// Add a variable for the modifier key
variables.modifier = $tw.keyboardManager.getEventModifierKeyDescriptor(event);
// Add a variable for the mouse button