-
-
Notifications
You must be signed in to change notification settings - Fork 622
/
LitMoveable.ts
95 lines (84 loc) · 2.73 KB
/
LitMoveable.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { LitElement, html } from "lit";
import { customElement, property } from "lit/decorators.js";
import VanillaMoveable, {
PROPERTIES,
EVENTS,
METHODS,
MoveableOptions,
MoveableInterface,
} from "moveable";
import { Properties, withMethods, MethodInterface } from "framework-utils";
import { camelize, isUndefined } from "@daybrush/utils";
import { LitMoveableOptions } from "./types";
@Properties(PROPERTIES as any, (prototype, name) => {
if (name === "draggable") {
property()(prototype, "mvDraggable");
property()(prototype, "litDraggable");
} else {
property()(prototype, name);
}
})
@customElement("lit-moveable")
export class LitMoveable extends LitElement {
@withMethods(METHODS as any)
private moveable!: VanillaMoveable;
public dragStartMoveable(e: MouseEvent | TouchEvent) {
return this.moveable.dragStart(e);
}
public litDragStart(e: MouseEvent | TouchEvent) {
return this.moveable.dragStart(e);
}
public firstUpdated() {
const options: Partial<MoveableOptions> = {};
PROPERTIES.forEach(name => {
let value: any;
if (name === "draggable") {
value = this.mvDraggable ?? this.litDraggable;
} else {
value = this[name];
}
if (!isUndefined(value)) {
options[name as any] = value;
}
});
this.moveable = new VanillaMoveable(this, {
warpSelf: true,
...options,
});
const moveable = this.moveable;
EVENTS.forEach((name, i) => {
moveable.on(name as any, e => {
const result = this.dispatchEvent(new CustomEvent(camelize(`lit ${name}`), {
detail: { ...e },
}));
if (result === false) {
(e as any).stop();
}
});
});
}
public render() {
return html`<slot></slot>`;
}
public updated(changedProperties) {
const moveable = this.moveable;
changedProperties.forEach((oldValue, propName) => {
const litName = propName === "mvDraggable" || propName === "litMoveable"
? "draggable"
: propName;
if (PROPERTIES.indexOf(litName) > -1) {
moveable[litName] = this[propName];
}
});
}
public disconnectedCallback() {
super.disconnectedCallback();
this.moveable.destroy();
}
}
export interface LitMoveable extends LitMoveableOptions, MethodInterface<MoveableInterface, VanillaMoveable, LitMoveable> { }
declare global {
interface HTMLElementTagNameMap {
"lit-moveable": LitMoveable;
}
}