@vouchfor/embeds 0.0.0-experiment.29a8d8f → 0.0.0-experiment.2ab669a
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/es/components/Embed/controllers/event-forwarder.d.ts +14 -0
- package/dist/es/components/Embed/controllers/fetcher.d.ts +13 -0
- package/dist/es/components/Embed/controllers/tracking.d.ts +28 -0
- package/dist/es/components/Embed/index.d.ts +62 -0
- package/dist/es/embeds.js +1218 -13
- package/dist/es/embeds.js.map +1 -1
- package/dist/es/index.d.ts +1 -1
- package/dist/es/utils/env.d.ts +18 -0
- package/dist/es/utils/events.d.ts +2 -0
- package/dist/iife/embeds.iife.js +390 -319
- package/dist/iife/embeds.iife.js.map +1 -1
- package/package.json +7 -6
- package/src/components/Embed/Embed.stories.ts +66 -0
- package/src/components/Embed/controllers/event-forwarder.ts +47 -0
- package/src/components/Embed/controllers/fetcher.ts +88 -0
- package/src/components/Embed/controllers/tracking.ts +261 -0
- package/src/components/Embed/index.ts +197 -0
- package/src/index.ts +1 -1
- package/src/utils/env.ts +78 -0
- package/src/utils/events.ts +13 -0
- package/dist/es/components/InlineEmbed.d.ts +0 -17
- package/dist/es/components/index.d.ts +0 -2
- package/src/components/InlineEmbed.stories.ts +0 -119
- package/src/components/InlineEmbed.ts +0 -24
- package/src/components/index.ts +0 -2
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@vouchfor/embeds",
|
3
|
-
"version": "0.0.0-experiment.
|
3
|
+
"version": "0.0.0-experiment.2ab669a",
|
4
4
|
"license": "MIT",
|
5
5
|
"author": "Aaron Williams",
|
6
6
|
"main": "dist/es/embeds.js",
|
@@ -8,8 +8,7 @@
|
|
8
8
|
"type": "module",
|
9
9
|
"types": "dist/es/index.d.ts",
|
10
10
|
"exports": {
|
11
|
-
".": "./dist/es/embeds.js"
|
12
|
-
"./embed": "./dist/iife/embeds.iife.js"
|
11
|
+
".": "./dist/es/embeds.js"
|
13
12
|
},
|
14
13
|
"files": ["dist", "src"],
|
15
14
|
"publishConfig": {
|
@@ -27,7 +26,7 @@
|
|
27
26
|
"lint:staged": "lint-staged",
|
28
27
|
"prepublishOnly": "yarn build",
|
29
28
|
"size": "size-limit",
|
30
|
-
"storybook": "yarn prebuild && storybook dev -p
|
29
|
+
"storybook": "yarn prebuild && storybook dev -p 6007",
|
31
30
|
"prebuild": "yarn build:deps && yarn generate:manifest",
|
32
31
|
"test": "true"
|
33
32
|
},
|
@@ -36,10 +35,12 @@
|
|
36
35
|
"**/*.{md,json,yml}": "prettier --write"
|
37
36
|
},
|
38
37
|
"dependencies": {
|
39
|
-
"@
|
38
|
+
"@lit/task": "^1.0.0",
|
39
|
+
"@vouchfor/media-player": "0.0.0-experiment.2ab669a",
|
40
|
+
"uuid": "^9.0.1"
|
40
41
|
},
|
41
42
|
"peerDependencies": {
|
42
|
-
"lit": "^
|
43
|
+
"lit": "^3.0.2"
|
43
44
|
},
|
44
45
|
"devDependencies": {
|
45
46
|
"@esm-bundle/chai": "^4.3.4-fix.0",
|
@@ -0,0 +1,66 @@
|
|
1
|
+
import { html } from 'lit';
|
2
|
+
import { ifDefined } from 'lit/directives/if-defined.js';
|
3
|
+
|
4
|
+
import type { EmbedProps } from './';
|
5
|
+
import type { Meta, StoryObj } from '@storybook/web-components';
|
6
|
+
|
7
|
+
import './';
|
8
|
+
|
9
|
+
type EmbedArgs = EmbedProps & {
|
10
|
+
showVouch?: boolean;
|
11
|
+
};
|
12
|
+
|
13
|
+
const _Embed = ({ vouchId, templateId, preload, autoplay, env, apiKey, controls, aspectRatio }: EmbedArgs) => {
|
14
|
+
return html`
|
15
|
+
<div style="height: 100vh">
|
16
|
+
<vouch-embed
|
17
|
+
env=${ifDefined(env)}
|
18
|
+
apiKey=${ifDefined(apiKey)}
|
19
|
+
vouchId=${ifDefined(vouchId)}
|
20
|
+
templateId=${ifDefined(templateId)}
|
21
|
+
.controls=${controls}
|
22
|
+
?autoplay=${autoplay}
|
23
|
+
preload=${ifDefined(preload)}
|
24
|
+
aspectRatio=${ifDefined(aspectRatio)}
|
25
|
+
></vouch-embed>
|
26
|
+
</div>
|
27
|
+
`;
|
28
|
+
};
|
29
|
+
|
30
|
+
// More on how to set up stories at: https://storybook.js.org/docs/web-components/writing-stories/introduction
|
31
|
+
const meta = {
|
32
|
+
title: 'Embed',
|
33
|
+
tags: ['autodocs'],
|
34
|
+
render: (args) => _Embed(args),
|
35
|
+
component: 'vouch-embed'
|
36
|
+
} satisfies Meta<EmbedProps>;
|
37
|
+
|
38
|
+
type Story = StoryObj<EmbedArgs>;
|
39
|
+
|
40
|
+
const Embed: Story = {
|
41
|
+
args: {
|
42
|
+
env: 'dev',
|
43
|
+
apiKey: 'TVik9uTMgE-PD25UTHIS6gyl0hMBWC7AT4dkpdlLBT4VIfDWZJrQiCk6Ak7m1',
|
44
|
+
vouchId: '6JQEIPeStt',
|
45
|
+
templateId: '7d0113f7-3f9a-4bdd-97e3-07ee6eec5730',
|
46
|
+
aspectRatio: 0,
|
47
|
+
preload: 'none',
|
48
|
+
autoplay: false
|
49
|
+
},
|
50
|
+
argTypes: {
|
51
|
+
env: {
|
52
|
+
control: 'radio',
|
53
|
+
options: ['prod', 'staging', 'dev']
|
54
|
+
},
|
55
|
+
preload: {
|
56
|
+
control: 'radio',
|
57
|
+
options: ['auto', 'none']
|
58
|
+
}
|
59
|
+
},
|
60
|
+
parameters: {
|
61
|
+
layout: 'fullscreen'
|
62
|
+
}
|
63
|
+
};
|
64
|
+
|
65
|
+
export default meta;
|
66
|
+
export { Embed };
|
@@ -0,0 +1,47 @@
|
|
1
|
+
import { createRef, ref } from 'lit/directives/ref.js';
|
2
|
+
|
3
|
+
import type { Embed } from '..';
|
4
|
+
import type { ReactiveController, ReactiveControllerHost } from 'lit';
|
5
|
+
import type { Ref } from 'lit/directives/ref.js';
|
6
|
+
|
7
|
+
import { forwardEvent } from '~/utils/events';
|
8
|
+
|
9
|
+
type EmbedHost = ReactiveControllerHost & Embed;
|
10
|
+
|
11
|
+
class EventForwardController implements ReactiveController {
|
12
|
+
host: EmbedHost;
|
13
|
+
|
14
|
+
private _events: string[] = [];
|
15
|
+
private _cleanup: (() => void)[] = [];
|
16
|
+
private _forwardElementRef: Ref<HTMLElement> = createRef();
|
17
|
+
|
18
|
+
constructor(host: EmbedHost, events: string[]) {
|
19
|
+
this.host = host;
|
20
|
+
this._events = events;
|
21
|
+
host.addController(this);
|
22
|
+
}
|
23
|
+
|
24
|
+
register() {
|
25
|
+
return ref(this._forwardElementRef);
|
26
|
+
}
|
27
|
+
|
28
|
+
hostConnected() {
|
29
|
+
// Guaranteed to have the element ref in the RAF callback because of how the lit lifecycle works
|
30
|
+
requestAnimationFrame(() => {
|
31
|
+
this._events.forEach((event) => {
|
32
|
+
if (this._forwardElementRef.value) {
|
33
|
+
this._cleanup.push(forwardEvent(event, this._forwardElementRef.value, this.host));
|
34
|
+
}
|
35
|
+
});
|
36
|
+
});
|
37
|
+
}
|
38
|
+
|
39
|
+
hostDisconnected() {
|
40
|
+
this._cleanup.forEach((fn) => {
|
41
|
+
fn();
|
42
|
+
});
|
43
|
+
this._cleanup = [];
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
export { EventForwardController };
|
@@ -0,0 +1,88 @@
|
|
1
|
+
import { Task } from '@lit/task';
|
2
|
+
|
3
|
+
import type { Embed, EmbedProps } from '..';
|
4
|
+
import type { ReactiveControllerHost } from 'lit';
|
5
|
+
import type { Environment } from '~/utils/env';
|
6
|
+
|
7
|
+
import { getEnvUrls } from '~/utils/env';
|
8
|
+
|
9
|
+
type EmbedHost = ReactiveControllerHost & Embed;
|
10
|
+
|
11
|
+
type TaskDeps = [
|
12
|
+
EmbedProps['env'],
|
13
|
+
EmbedProps['apiKey'],
|
14
|
+
EmbedProps['data'],
|
15
|
+
EmbedProps['vouchId'],
|
16
|
+
EmbedProps['templateId']
|
17
|
+
];
|
18
|
+
|
19
|
+
class FetcherController {
|
20
|
+
host: EmbedHost;
|
21
|
+
|
22
|
+
private _fetching = false;
|
23
|
+
|
24
|
+
set fetching(value) {
|
25
|
+
if (this._fetching !== value) {
|
26
|
+
this._fetching = value;
|
27
|
+
this.host.requestUpdate();
|
28
|
+
}
|
29
|
+
}
|
30
|
+
get fetching() {
|
31
|
+
return this._fetching;
|
32
|
+
}
|
33
|
+
|
34
|
+
private async getVouch(env: Environment, apiKey: string, vouchId: string) {
|
35
|
+
const { embedApiUrl } = getEnvUrls(env);
|
36
|
+
|
37
|
+
return fetch(`${embedApiUrl}/vouches/${vouchId}`, {
|
38
|
+
method: 'GET',
|
39
|
+
headers: [['X-Api-Key', apiKey]]
|
40
|
+
}).then((response) => response.json());
|
41
|
+
}
|
42
|
+
|
43
|
+
private async getTemplate(env: Environment, apiKey: string, templateId: string) {
|
44
|
+
const { embedApiUrl } = getEnvUrls(env);
|
45
|
+
|
46
|
+
return fetch(`${embedApiUrl}/templates/${templateId}`, {
|
47
|
+
method: 'GET',
|
48
|
+
headers: [['X-Api-Key', apiKey]]
|
49
|
+
}).then((response) => response.json());
|
50
|
+
}
|
51
|
+
|
52
|
+
constructor(host: EmbedHost) {
|
53
|
+
this.host = host;
|
54
|
+
new Task<TaskDeps, void>(
|
55
|
+
this.host,
|
56
|
+
async ([env, apiKey, data, vouchId, templateId]: TaskDeps) => {
|
57
|
+
try {
|
58
|
+
host.vouch = undefined;
|
59
|
+
host.template = undefined;
|
60
|
+
|
61
|
+
if (data) {
|
62
|
+
let template;
|
63
|
+
if (templateId) {
|
64
|
+
this.fetching = true;
|
65
|
+
template = await this.getTemplate(env, apiKey, templateId);
|
66
|
+
}
|
67
|
+
host.vouch = data;
|
68
|
+
host.template = template ?? data?.settings?.template?.instance;
|
69
|
+
} else if (vouchId) {
|
70
|
+
this.fetching = true;
|
71
|
+
|
72
|
+
const [vouch, template] = await Promise.all([
|
73
|
+
this.getVouch(env, apiKey, vouchId),
|
74
|
+
templateId ? this.getTemplate(env, apiKey, templateId) : null
|
75
|
+
]);
|
76
|
+
host.vouch = vouch;
|
77
|
+
host.template = template ?? vouch?.settings?.template?.instance;
|
78
|
+
}
|
79
|
+
} finally {
|
80
|
+
this.fetching = false;
|
81
|
+
}
|
82
|
+
},
|
83
|
+
() => [host.env, host.apiKey, host.data, host.vouchId, host.templateId]
|
84
|
+
);
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
export { FetcherController };
|
@@ -0,0 +1,261 @@
|
|
1
|
+
import { v4 as uuidv4 } from 'uuid';
|
2
|
+
|
3
|
+
import type { Embed } from '..';
|
4
|
+
import type { VideoEventDetail } from '@vouchfor/media-player';
|
5
|
+
import type { ReactiveController, ReactiveControllerHost } from 'lit';
|
6
|
+
|
7
|
+
import { getEnvUrls } from '~/utils/env';
|
8
|
+
|
9
|
+
const STREAMED_THROTTLE = 2000;
|
10
|
+
|
11
|
+
type EmbedHost = ReactiveControllerHost & Embed;
|
12
|
+
|
13
|
+
type TrackingEvent = 'VOUCH_LOADED' | 'VOUCH_RESPONSE_VIEWED' | 'VIDEO_PLAYED' | 'VIDEO_STREAMED';
|
14
|
+
type TrackingPayload = {
|
15
|
+
vouchId: string;
|
16
|
+
answerId?: string;
|
17
|
+
streamStart?: number;
|
18
|
+
streamEnd?: number;
|
19
|
+
};
|
20
|
+
|
21
|
+
type TimeMap = {
|
22
|
+
[key: string]: number;
|
23
|
+
};
|
24
|
+
|
25
|
+
type BooleanMap = {
|
26
|
+
[key: string]: boolean;
|
27
|
+
};
|
28
|
+
|
29
|
+
class TrackingController implements ReactiveController {
|
30
|
+
host: EmbedHost;
|
31
|
+
|
32
|
+
private _tabId: string | undefined = undefined;
|
33
|
+
private _clientId: string | undefined = undefined;
|
34
|
+
private _visitorId: string | undefined = undefined;
|
35
|
+
|
36
|
+
private _hasPlayed = false;
|
37
|
+
private _hasLoaded: BooleanMap = {};
|
38
|
+
private _answersViewed: BooleanMap = {};
|
39
|
+
private _streamedTime: TimeMap = {};
|
40
|
+
private _streamedPrevTimestamp: TimeMap = {};
|
41
|
+
|
42
|
+
constructor(host: EmbedHost) {
|
43
|
+
this.host = host;
|
44
|
+
host.addController(this);
|
45
|
+
}
|
46
|
+
|
47
|
+
private _findVouchId() {
|
48
|
+
if (this.host.data) {
|
49
|
+
if ('uuid' in this.host.data) {
|
50
|
+
return this.host.data.uuid;
|
51
|
+
}
|
52
|
+
return this.host.data.id;
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
private _createVisitor = (visitorId: string) => {
|
57
|
+
const { publicApiUrl } = getEnvUrls(this.host.env);
|
58
|
+
window.localStorage?.setItem?.('vouch-uid-visitor', visitorId);
|
59
|
+
navigator.sendBeacon(`${publicApiUrl}/api/visitor`, JSON.stringify({ visitorId }));
|
60
|
+
return visitorId;
|
61
|
+
};
|
62
|
+
|
63
|
+
private _getUids() {
|
64
|
+
if (typeof window === 'undefined') {
|
65
|
+
return {
|
66
|
+
client: null,
|
67
|
+
tab: null,
|
68
|
+
request: uuidv4()
|
69
|
+
};
|
70
|
+
}
|
71
|
+
|
72
|
+
// Persisted for a user for the same device + browser, so we can e.g. search for all logs related to that browser
|
73
|
+
const visitorId =
|
74
|
+
this._visitorId || window.localStorage?.getItem?.('vouch-uid-visitor') || this._createVisitor(uuidv4());
|
75
|
+
// Persisted for a user for the same device + browser, so we can e.g. search for all logs related to that browser
|
76
|
+
const clientId = this._clientId || window.localStorage?.getItem?.('vouch-uid-client') || uuidv4();
|
77
|
+
// Persisted in session storage, so we can search for everything the user has done in a specific tab
|
78
|
+
const tabId = this._tabId || window.sessionStorage?.getItem?.('vouch-uid-tab') || uuidv4();
|
79
|
+
// Not persisted, allows us to search for any logs related to a single FE request
|
80
|
+
// E.g. BE should pass this request ID through all other services to be able to group logs
|
81
|
+
const requestId = uuidv4();
|
82
|
+
|
83
|
+
// Cache and persist uids
|
84
|
+
if (visitorId !== this._visitorId) {
|
85
|
+
this._visitorId = visitorId;
|
86
|
+
window.localStorage?.setItem?.('vouch-uid-visitor', visitorId);
|
87
|
+
}
|
88
|
+
|
89
|
+
if (clientId !== this._clientId) {
|
90
|
+
this._clientId = clientId;
|
91
|
+
window.localStorage?.setItem?.('vouch-uid-client', clientId);
|
92
|
+
}
|
93
|
+
|
94
|
+
if (tabId !== this._tabId) {
|
95
|
+
this._tabId = tabId;
|
96
|
+
window.sessionStorage?.setItem?.('vouch-uid-tab', tabId);
|
97
|
+
}
|
98
|
+
|
99
|
+
return {
|
100
|
+
client: clientId,
|
101
|
+
tab: tabId,
|
102
|
+
request: requestId,
|
103
|
+
visitor: visitorId
|
104
|
+
};
|
105
|
+
}
|
106
|
+
|
107
|
+
private _getReportingMetadata = () => {
|
108
|
+
const [country, region] = Intl.DateTimeFormat().resolvedOptions().timeZone?.split?.('/') ?? [];
|
109
|
+
|
110
|
+
const utmParams: any = {};
|
111
|
+
[...new URLSearchParams(location.search).entries()].forEach(([key, value]) => {
|
112
|
+
if (/utm/.test(key)) {
|
113
|
+
const param = key.toLowerCase().replace(/[-_][a-z0-9]/g, (group) => group.slice(-1).toUpperCase());
|
114
|
+
utmParams[param] = value;
|
115
|
+
}
|
116
|
+
});
|
117
|
+
|
118
|
+
return {
|
119
|
+
source: 'media_player',
|
120
|
+
time: new Date(),
|
121
|
+
region,
|
122
|
+
country,
|
123
|
+
screenHeight: window.screen.height,
|
124
|
+
screenWidth: window.screen.width,
|
125
|
+
referrer: document.referrer,
|
126
|
+
currentUrl: location.href,
|
127
|
+
...utmParams
|
128
|
+
};
|
129
|
+
};
|
130
|
+
|
131
|
+
private _sendTrackingEvent = (event: TrackingEvent, payload: TrackingPayload) => {
|
132
|
+
const { publicApiUrl } = getEnvUrls(this.host.env);
|
133
|
+
const { client, tab, request, visitor } = this._getUids();
|
134
|
+
|
135
|
+
// Don't send tracking if we don't have a source
|
136
|
+
navigator.sendBeacon(
|
137
|
+
`${publicApiUrl}/api/events`,
|
138
|
+
JSON.stringify({
|
139
|
+
event,
|
140
|
+
payload,
|
141
|
+
context: {
|
142
|
+
'x-uid-client': client,
|
143
|
+
'x-uid-tab': tab,
|
144
|
+
'x-uid-request': request,
|
145
|
+
'x-uid-visitor': visitor,
|
146
|
+
'x-reporting-metadata': this._getReportingMetadata()
|
147
|
+
}
|
148
|
+
})
|
149
|
+
);
|
150
|
+
};
|
151
|
+
|
152
|
+
private _handleVouchLoaded = ({ detail: vouchId }: CustomEvent<string>) => {
|
153
|
+
if (!vouchId) {
|
154
|
+
return;
|
155
|
+
}
|
156
|
+
|
157
|
+
// Only send loaded event once per session
|
158
|
+
if (!this._hasLoaded[vouchId]) {
|
159
|
+
this._sendTrackingEvent('VOUCH_LOADED', {
|
160
|
+
vouchId
|
161
|
+
});
|
162
|
+
this._hasLoaded[vouchId] = true;
|
163
|
+
}
|
164
|
+
};
|
165
|
+
|
166
|
+
private _handlePlay = () => {
|
167
|
+
const vouchId = this._findVouchId();
|
168
|
+
|
169
|
+
if (!vouchId) {
|
170
|
+
return;
|
171
|
+
}
|
172
|
+
|
173
|
+
// Only send the video played event once per session
|
174
|
+
if (!this._hasPlayed) {
|
175
|
+
this._sendTrackingEvent('VIDEO_PLAYED', {
|
176
|
+
vouchId,
|
177
|
+
streamStart: this.host.currentTime
|
178
|
+
});
|
179
|
+
this._hasPlayed = true;
|
180
|
+
}
|
181
|
+
};
|
182
|
+
|
183
|
+
private _handleVideoPlay = ({ detail: { id, node } }: CustomEvent<VideoEventDetail>) => {
|
184
|
+
const vouchId = this._findVouchId();
|
185
|
+
if (!vouchId) {
|
186
|
+
return;
|
187
|
+
}
|
188
|
+
// Only increment play count once per session
|
189
|
+
if (!this._answersViewed[id]) {
|
190
|
+
this._sendTrackingEvent('VOUCH_RESPONSE_VIEWED', {
|
191
|
+
vouchId,
|
192
|
+
answerId: id
|
193
|
+
});
|
194
|
+
this._answersViewed[id] = true;
|
195
|
+
}
|
196
|
+
this._streamedTime[id] = node.currentTime;
|
197
|
+
this._streamedPrevTimestamp[id] = Date.now();
|
198
|
+
};
|
199
|
+
|
200
|
+
private _handleVideoTimeUpdate = ({ detail: { id, node } }: CustomEvent<VideoEventDetail>) => {
|
201
|
+
const vouchId = this._findVouchId();
|
202
|
+
if (!vouchId) {
|
203
|
+
return;
|
204
|
+
}
|
205
|
+
const currentTimestamp = Date.now();
|
206
|
+
if (
|
207
|
+
!this.host.paused &&
|
208
|
+
// Only fire the video seeked event when this video is the active one
|
209
|
+
id === this.host.scene?.video?.id &&
|
210
|
+
// Throttle the frequency that we send streamed events while playing
|
211
|
+
currentTimestamp - this._streamedPrevTimestamp[id] > STREAMED_THROTTLE
|
212
|
+
) {
|
213
|
+
this._sendTrackingEvent('VIDEO_STREAMED', {
|
214
|
+
vouchId,
|
215
|
+
answerId: id,
|
216
|
+
streamStart: this._streamedTime[id],
|
217
|
+
streamEnd: node.currentTime
|
218
|
+
});
|
219
|
+
this._streamedTime[id] = node.currentTime;
|
220
|
+
this._streamedPrevTimestamp[id] = currentTimestamp;
|
221
|
+
}
|
222
|
+
};
|
223
|
+
|
224
|
+
private _handleVideoPause = ({ detail: { id, node } }: CustomEvent<VideoEventDetail>) => {
|
225
|
+
const vouchId = this._findVouchId();
|
226
|
+
if (!vouchId) {
|
227
|
+
return;
|
228
|
+
}
|
229
|
+
// Send a video streamed event any time the video pauses then reset the streamed state
|
230
|
+
// We do this to capture the last bit of time that the video was played between the previous
|
231
|
+
// stream event and the video being paused manually or stopping because it ended
|
232
|
+
this._sendTrackingEvent('VIDEO_STREAMED', {
|
233
|
+
vouchId,
|
234
|
+
answerId: id,
|
235
|
+
streamStart: this._streamedTime[id],
|
236
|
+
streamEnd: node.currentTime
|
237
|
+
});
|
238
|
+
delete this._streamedTime[id];
|
239
|
+
delete this._streamedPrevTimestamp[id];
|
240
|
+
};
|
241
|
+
|
242
|
+
hostConnected() {
|
243
|
+
requestAnimationFrame(() => {
|
244
|
+
this.host.addEventListener('vouch:loaded', this._handleVouchLoaded);
|
245
|
+
this.host.mediaPlayer?.addEventListener('play', this._handlePlay);
|
246
|
+
this.host.mediaPlayer?.addEventListener('video:play', this._handleVideoPlay);
|
247
|
+
this.host.mediaPlayer?.addEventListener('video:pause', this._handleVideoPause);
|
248
|
+
this.host.mediaPlayer?.addEventListener('video:timeupdate', this._handleVideoTimeUpdate);
|
249
|
+
});
|
250
|
+
}
|
251
|
+
|
252
|
+
hostDisconnected() {
|
253
|
+
this.host.removeEventListener('vouch:loaded', this._handleVouchLoaded);
|
254
|
+
this.host.mediaPlayer?.removeEventListener('play', this._handlePlay);
|
255
|
+
this.host.mediaPlayer?.removeEventListener('video:play', this._handleVideoPlay);
|
256
|
+
this.host.mediaPlayer?.removeEventListener('video:pause', this._handleVideoPause);
|
257
|
+
this.host.mediaPlayer?.removeEventListener('video:timeupdate', this._handleVideoTimeUpdate);
|
258
|
+
}
|
259
|
+
}
|
260
|
+
|
261
|
+
export { TrackingController };
|
@@ -0,0 +1,197 @@
|
|
1
|
+
import { html, LitElement } from 'lit';
|
2
|
+
import { customElement, property, state } from 'lit/decorators.js';
|
3
|
+
import { ifDefined } from 'lit/directives/if-defined.js';
|
4
|
+
import { createRef, ref } from 'lit/directives/ref.js';
|
5
|
+
|
6
|
+
import type { Scene, TemplateInstance } from '@vouchfor/canvas-video';
|
7
|
+
import type { MediaPlayer, MediaPlayerProps } from '@vouchfor/media-player';
|
8
|
+
import type { Ref } from 'lit/directives/ref.js';
|
9
|
+
import type { Environment } from '~/utils/env';
|
10
|
+
|
11
|
+
import { EventForwardController } from './controllers/event-forwarder';
|
12
|
+
import { FetcherController } from './controllers/fetcher';
|
13
|
+
import { TrackingController } from './controllers/tracking';
|
14
|
+
|
15
|
+
import '@vouchfor/media-player';
|
16
|
+
|
17
|
+
type EmbedProps = Pick<MediaPlayerProps, 'data' | 'aspectRatio' | 'preload' | 'autoplay' | 'controls'> & {
|
18
|
+
env: Environment;
|
19
|
+
apiKey: string;
|
20
|
+
vouchId?: string;
|
21
|
+
templateId?: string;
|
22
|
+
};
|
23
|
+
|
24
|
+
@customElement('vouch-embed')
|
25
|
+
class Embed extends LitElement {
|
26
|
+
private _mediaPlayerRef: Ref<MediaPlayer> = createRef();
|
27
|
+
|
28
|
+
@property({ type: Object, attribute: 'data' }) data: EmbedProps['data'];
|
29
|
+
@property({ type: String }) vouchId: EmbedProps['vouchId'];
|
30
|
+
@property({ type: String }) templateId: EmbedProps['templateId'];
|
31
|
+
|
32
|
+
@property({ type: String }) env: EmbedProps['env'] = 'prod';
|
33
|
+
@property({ type: String }) apiKey: EmbedProps['apiKey'] = '';
|
34
|
+
|
35
|
+
@property({ type: Array }) controls: EmbedProps['controls'];
|
36
|
+
@property({ type: String }) preload: EmbedProps['preload'] = 'auto';
|
37
|
+
@property({ type: Boolean }) autoplay: EmbedProps['autoplay'] = false;
|
38
|
+
@property({ type: Number }) aspectRatio: EmbedProps['aspectRatio'] = 0;
|
39
|
+
|
40
|
+
private eventController = new EventForwardController(this, [
|
41
|
+
'durationchange',
|
42
|
+
'ended',
|
43
|
+
'error',
|
44
|
+
'loadeddata',
|
45
|
+
'pause',
|
46
|
+
'stalled',
|
47
|
+
'play',
|
48
|
+
'playing',
|
49
|
+
'ratechange',
|
50
|
+
'scenechange',
|
51
|
+
'seeking',
|
52
|
+
'seeked',
|
53
|
+
'timeupdate',
|
54
|
+
'volumechange',
|
55
|
+
'waiting',
|
56
|
+
|
57
|
+
'video:loadeddata',
|
58
|
+
'video:seeked',
|
59
|
+
'video:play',
|
60
|
+
'video:playing',
|
61
|
+
'video:pause',
|
62
|
+
'video:stalled',
|
63
|
+
'video:timeupdate',
|
64
|
+
'video:ended',
|
65
|
+
'video:error'
|
66
|
+
]);
|
67
|
+
private _fetcherController = new FetcherController(this);
|
68
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
69
|
+
// @ts-ignore
|
70
|
+
private _trackingController = new TrackingController(this);
|
71
|
+
|
72
|
+
@state() vouch: EmbedProps['data'];
|
73
|
+
@state() template: TemplateInstance | undefined;
|
74
|
+
|
75
|
+
get fetching() {
|
76
|
+
return this._fetcherController.fetching;
|
77
|
+
}
|
78
|
+
|
79
|
+
get waiting() {
|
80
|
+
return this._mediaPlayerRef.value?.waiting;
|
81
|
+
}
|
82
|
+
|
83
|
+
get seeking() {
|
84
|
+
return this._mediaPlayerRef.value?.seeking;
|
85
|
+
}
|
86
|
+
|
87
|
+
get paused() {
|
88
|
+
return this._mediaPlayerRef.value?.paused;
|
89
|
+
}
|
90
|
+
|
91
|
+
get captions() {
|
92
|
+
return this._mediaPlayerRef.value?.captions;
|
93
|
+
}
|
94
|
+
|
95
|
+
get fullscreen() {
|
96
|
+
return this._mediaPlayerRef.value?.fullscreen;
|
97
|
+
}
|
98
|
+
|
99
|
+
get duration() {
|
100
|
+
return this._mediaPlayerRef.value?.duration;
|
101
|
+
}
|
102
|
+
|
103
|
+
set currentTime(value: number) {
|
104
|
+
if (this._mediaPlayerRef.value) {
|
105
|
+
this._mediaPlayerRef.value.currentTime = value;
|
106
|
+
}
|
107
|
+
}
|
108
|
+
get currentTime() {
|
109
|
+
return this._mediaPlayerRef.value?.currentTime ?? 0;
|
110
|
+
}
|
111
|
+
|
112
|
+
set playbackRate(value: number) {
|
113
|
+
if (this._mediaPlayerRef.value) {
|
114
|
+
this._mediaPlayerRef.value.playbackRate = value;
|
115
|
+
}
|
116
|
+
}
|
117
|
+
get playbackRate() {
|
118
|
+
return this._mediaPlayerRef.value?.playbackRate ?? 1;
|
119
|
+
}
|
120
|
+
|
121
|
+
set volume(value: number) {
|
122
|
+
if (this._mediaPlayerRef.value) {
|
123
|
+
this._mediaPlayerRef.value.volume = value;
|
124
|
+
}
|
125
|
+
}
|
126
|
+
get volume() {
|
127
|
+
return this._mediaPlayerRef.value?.volume ?? 1;
|
128
|
+
}
|
129
|
+
|
130
|
+
set muted(value: boolean) {
|
131
|
+
if (this._mediaPlayerRef.value) {
|
132
|
+
this._mediaPlayerRef.value.muted = value;
|
133
|
+
}
|
134
|
+
}
|
135
|
+
get muted() {
|
136
|
+
return this._mediaPlayerRef.value?.muted ?? false;
|
137
|
+
}
|
138
|
+
|
139
|
+
get scene(): Scene | null {
|
140
|
+
return this._mediaPlayerRef.value?.scene ?? null;
|
141
|
+
}
|
142
|
+
|
143
|
+
get scenes(): Scene[] {
|
144
|
+
return this._mediaPlayerRef.value?.scenes ?? [];
|
145
|
+
}
|
146
|
+
|
147
|
+
get videoState() {
|
148
|
+
return this._mediaPlayerRef.value?.videoState;
|
149
|
+
}
|
150
|
+
|
151
|
+
get mediaPlayer() {
|
152
|
+
return this._mediaPlayerRef.value;
|
153
|
+
}
|
154
|
+
|
155
|
+
play() {
|
156
|
+
this._mediaPlayerRef.value?.play();
|
157
|
+
}
|
158
|
+
|
159
|
+
pause() {
|
160
|
+
this._mediaPlayerRef.value?.pause();
|
161
|
+
}
|
162
|
+
|
163
|
+
setScene(index: number) {
|
164
|
+
this._mediaPlayerRef.value?.setScene(index);
|
165
|
+
}
|
166
|
+
|
167
|
+
render() {
|
168
|
+
return html`
|
169
|
+
<vmp-new-media-player
|
170
|
+
${ref(this._mediaPlayerRef)}
|
171
|
+
${this.eventController.register()}
|
172
|
+
?autoplay=${this.autoplay}
|
173
|
+
?loading=${this.fetching}
|
174
|
+
.data=${this.vouch}
|
175
|
+
.template=${this.template}
|
176
|
+
aspectRatio=${ifDefined(this.aspectRatio)}
|
177
|
+
preload=${ifDefined(this.preload)}
|
178
|
+
.controls=${this.controls}
|
179
|
+
></vmp-new-media-player>
|
180
|
+
`;
|
181
|
+
}
|
182
|
+
}
|
183
|
+
|
184
|
+
declare global {
|
185
|
+
interface HTMLElementTagNameMap {
|
186
|
+
'vouch-embed': Embed;
|
187
|
+
}
|
188
|
+
|
189
|
+
namespace JSX {
|
190
|
+
interface IntrinsicElements {
|
191
|
+
'vouch-embed': Embed;
|
192
|
+
}
|
193
|
+
}
|
194
|
+
}
|
195
|
+
|
196
|
+
export { Embed };
|
197
|
+
export type { EmbedProps };
|
package/src/index.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export {
|
1
|
+
export { Embed } from '~/components/Embed';
|