10000 fix: re-add lowercase gesture handling and add deprecation notice when using non-string events by edusperoni · Pull Request #10581 · NativeScript/NativeScript · GitHub
[go: up one dir, main page]

Skip to content

fix: re-add lowercase gesture handling and add deprecation notice when using non-string events #10581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load 8000 comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion packages/core/ui/core/view/view-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { setupAccessibleView } from '../../../accessibility';

import { PercentLength } from '../../styling/style-properties';

import { observe as gestureObserve, GesturesObserver, GestureTypes, GestureEventData, fromString as gestureFromString, TouchManager, TouchAnimationOptions, VisionHoverOptions } from '../../gestures';
import { observe as gestureObserve, GesturesObserver, GestureTypes, GestureEventData, fromString as gestureFromString, toString as gestureToString, TouchManager, TouchAnimationOptions, VisionHoverOptions } from '../../gestures';

import { CSSUtils } from '../../../css/system-classes';
import { Builder } from '../../builder';
Expand Down Expand Up @@ -72,6 +72,9 @@ export const _rootModalViews = new Array<ViewBase>();

type InteractiveTransitionState = { began?: boolean; cancelled?: boolean; options?: SharedTransitionInteractiveOptions };

// TODO: remove once we fully switch to the new event system
const warnedEvent = new Set<string>();

export abstract class ViewCommon extends ViewBase implements ViewDefinition {
public static layoutChangedEvent = 'layoutChanged';
public static shownModallyEvent = 'shownModally';
Expand Down Expand Up @@ -300,6 +303,17 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
public addEventListener(eventNames: string, callback: (data: EventData) => void, thisArg?: any) {
thisArg = thisArg || undefined;

// TODO: Remove this once we fully switch to the new event system
if (typeof eventNames === 'number') {
// likely a gesture type from a plugin
const gestureName = gestureToString(eventNames);
if (!warnedEvent.has(gestureName)) {
console.warn(`Using a gesture type (${gestureName}) as an event name is deprecated. Please use the event name instead.`);
warnedEvent.add(gestureName);
}
eventNames = gestureName;
}

// Normalize "ontap" -> "tap"
const normalizedName = getEventOrGestureName(eventNames);

Expand All @@ -319,6 +333,17 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
public removeEventListener(eventNames: string, callback?: (data: EventData) => void, thisArg?: any) {
thisArg = thisArg || undefined;

// TODO: Remove this once we fully switch to the new event system
if (typeof eventNames === 'number') {
// likely a gesture type from a plugin
const gestureName = gestureToString(eventNames);
if (!warnedEvent.has(gestureName)) {
console.warn(`Using a gesture type (${gestureName}) as an event name is deprecated. Please use the event name instead.`);
warnedEvent.add(gestureName);
}
eventNames = gestureName;
}

// Normalize "ontap" -> "tap"
const normalizedName = getEventOrGestureName(eventNames);

Expand Down
23 changes: 22 additions & 1 deletion packages/core/ui/gestures/gestures-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,28 @@ export function toString(type: GestureTypes): (typeof GestureTypes)[GestureTypes
* @param type - A string representation of a single gesture type (e.g. "tap").
*/
export function fromString(type: (typeof GestureTypes)[GestureTypes]): GestureTypes | undefined {
return GestureTypes[type];
const t = type.trim().toLowerCase();

switch (t) {
case 'tap':
return GestureTypes.tap;
case 'doubletap':
return GestureTypes.doubleTap;
case 'pinch':
return GestureTypes.pinch;
case 'pan':
return GestureTypes.pan;
case 'swipe':
return GestureTypes.swipe;
case 'rotation':
return GestureTypes.rotation;
case 'longpress':
return GestureTypes.longPress;
case 'touch':
return GestureTypes.touch;
}

return undefined;
}

export abstract class GesturesObserverBase implements GesturesObserverDefinition {
Expand Down
Loading
0