DEV: adds `touchMove` support for widgets (#22635)

No tests as widgets are in maintenance mode until we can remove it, and this is also hard to test.
This commit is contained in:
Joffrey JAFFEUX 2023-07-17 12:49:00 +02:00 committed by GitHub
parent c042978778
commit a610c86302
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -17,6 +17,7 @@ const MOUSE_OVER_ATTRIBUTE_NAME = "_discourse_mouse_over_widget";
const MOUSE_OUT_ATTRIBUTE_NAME = "_discourse_mouse_out_widget";
const TOUCH_START_ATTRIBUTE_NAME = "_discourse_touch_start_widget";
const TOUCH_END_ATTRIBUTE_NAME = "_discourse_touch_end_widget";
const TOUCH_MOVE_ATTRIBUTE_NAME = "_discourse_touch_move_widget";
class WidgetBaseHook {
constructor(widget) {
@ -73,6 +74,9 @@ export const WidgetTouchEndHook = buildHook(TOUCH_END_ATTRIBUTE_NAME);
function touchStartHandler(e) {
return e.currentTarget[TOUCH_START_ATTRIBUTE_NAME].touchStart(e);
}
function touchMoveHandler(e) {
return e.currentTarget[TOUCH_MOVE_ATTRIBUTE_NAME].touchMove(e);
}
export class WidgetTouchStartHook extends WidgetBaseHook {
hook(node, propertyName, previousValue) {
@ -92,6 +96,24 @@ export class WidgetTouchStartHook extends WidgetBaseHook {
}
}
}
export class WidgetTouchMoveHook extends WidgetBaseHook {
hook(node, propertyName, previousValue) {
node[TOUCH_MOVE_ATTRIBUTE_NAME] = this.widget;
if (!previousValue) {
// Element added to DOM
node.addEventListener("touchmove", touchMoveHandler, {
passive: false,
});
}
}
unhook(node, propertyName, newValue) {
if (!newValue) {
// Element removed from DOM
node.removeEventListener("touchmove", touchMoveHandler);
}
}
}
let _currentlyDraggingElement;

View File

@ -14,6 +14,7 @@ import {
WidgetMouseOverHook,
WidgetMouseUpHook,
WidgetTouchEndHook,
WidgetTouchMoveHook,
WidgetTouchStartHook,
} from "discourse/widgets/hooks";
import DecoratorHelper from "discourse/widgets/decorator-helper";
@ -481,6 +482,10 @@ export default class Widget {
properties["widget-touch-end"] = new WidgetTouchEndHook(this);
}
if (this.touchMove) {
properties["widget-touch-move"] = new WidgetTouchMoveHook(this);
}
const attributes = properties["attributes"] || {};
properties.attributes = attributes;