Skip to content

Commit fd4e450

Browse files
added events + exposed play/pause/stop methods with json lottie comp
1 parent e29a222 commit fd4e450

File tree

3 files changed

+87
-8
lines changed

3 files changed

+87
-8
lines changed

Diff for: client/packages/lowcoder/src/comps/comps/jsonComp/jsonLottieComp.tsx

+80-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { AnimationStyle, LottieStyle } from "comps/controls/styleControlConstant
1111
import { trans } from "i18n";
1212
import { Section, sectionNames } from "lowcoder-design";
1313
import { useContext, lazy, useEffect, useState } from "react";
14-
import { UICompBuilder, withDefault } from "../../generators";
14+
import { stateComp, UICompBuilder, withDefault } from "../../generators";
1515
import {
1616
NameConfig,
1717
NameConfigHidden,
@@ -23,6 +23,9 @@ import { AssetType, IconscoutControl } from "@lowcoder-ee/comps/controls/iconsco
2323
import { DotLottie } from "@lottiefiles/dotlottie-react";
2424
import { AutoHeightControl } from "@lowcoder-ee/comps/controls/autoHeightControl";
2525
import { useResizeDetector } from "react-resize-detector";
26+
import { eventHandlerControl } from "@lowcoder-ee/comps/controls/eventHandlerControl";
27+
import { withMethodExposing } from "@lowcoder-ee/comps/generators/withMethodExposing";
28+
import { changeChildAction } from "lowcoder-core";
2629

2730
// const Player = lazy(
2831
// () => import('@lottiefiles/react-lottie-player')
@@ -46,6 +49,10 @@ const animationStartOptions = [
4649
label: trans("jsonLottie.onHover"),
4750
value: "hover",
4851
},
52+
{
53+
label: trans("jsonLottie.onTrigger"),
54+
value: "trigger",
55+
},
4956
] as const;
5057

5158
const loopOptions = [
@@ -120,6 +127,14 @@ const ModeOptions = [
120127
{ label: "Asset Library", value: "asset-library" }
121128
] as const;
122129

130+
const EventOptions = [
131+
{ label: trans("jsonLottie.load"), value: "load", description: trans("jsonLottie.load") },
132+
{ label: trans("jsonLottie.play"), value: "play", description: trans("jsonLottie.play") },
133+
{ label: trans("jsonLottie.pause"), value: "pause", description: trans("jsonLottie.pause") },
134+
{ label: trans("jsonLottie.stop"), value: "stop", description: trans("jsonLottie.stop") },
135+
{ label: trans("jsonLottie.complete"), value: "complete", description: trans("jsonLottie.complete") },
136+
] as const;;
137+
123138
let JsonLottieTmpComp = (function () {
124139
const childrenMap = {
125140
sourceMode: dropdownControl(ModeOptions, "standard"),
@@ -140,6 +155,8 @@ let JsonLottieTmpComp = (function () {
140155
aspectRatio: withDefault(StringControl, "1/1"),
141156
fit: dropdownControl(alignOptions, "contain"),
142157
align: dropdownControl(fitOptions, "0.5,0.5"),
158+
onEvent: eventHandlerControl(EventOptions),
159+
dotLottieRef: stateComp<any | null>(null),
143160
};
144161
return new UICompBuilder(childrenMap, (props, dispatch) => {
145162
const [dotLottie, setDotLottie] = useState<DotLottie | null>(null);
@@ -161,21 +178,41 @@ let JsonLottieTmpComp = (function () {
161178
useEffect(() => {
162179
const onComplete = () => {
163180
props.keepLastFrame && dotLottie?.setFrame(100);
181+
props.onEvent('complete');
164182
}
165183

166184
const onLoad = () => {
167185
setLayoutAndResize();
186+
props.onEvent('load');
187+
}
188+
189+
const onPlay = () => {
190+
props.onEvent('play');
191+
}
192+
193+
const onPause = () => {
194+
props.onEvent('pause');
195+
}
196+
197+
const onStop = () => {
198+
props.onEvent('stop');
168199
}
169200

170201
if (dotLottie) {
171202
dotLottie.addEventListener('complete', onComplete);
172203
dotLottie.addEventListener('load', onLoad);
204+
dotLottie.addEventListener('play', onPlay);
205+
dotLottie.addEventListener('pause', onPause);
206+
dotLottie.addEventListener('stop', onStop);
173207
}
174208

175209
return () => {
176210
if (dotLottie) {
177211
dotLottie.removeEventListener('complete', onComplete);
178212
dotLottie.removeEventListener('load', onLoad);
213+
dotLottie.removeEventListener('play', onPlay);
214+
dotLottie.removeEventListener('pause', onPause);
215+
dotLottie.removeEventListener('stop', onStop);
179216
}
180217
};
181218
}, [dotLottie, props.keepLastFrame]);
@@ -212,17 +249,18 @@ let JsonLottieTmpComp = (function () {
212249
key={
213250
[props.speed, props.animationStart, props.loop, props.value, props.keepLastFrame] as any
214251
}
215-
dotLottieRefCallback={setDotLottie}
252+
dotLottieRefCallback={(lottieRef) => {
253+
setDotLottie(lottieRef);
254+
dispatch(
255+
changeChildAction("dotLottieRef", lottieRef as any, false)
256+
)
257+
}}
216258
autoplay={props.animationStart === "auto"}
217259
loop={props.loop === "single" ? false : true}
218260
speed={Number(props.speed)}
219261
data={props.sourceMode === 'standard' ? props.value as Record<string, undefined> : undefined}
220262
src={props.sourceMode === 'asset-library' ? props.iconScoutAsset?.value : undefined}
221263
style={{
222-
height: "100%",
223-
width: "100%",
224-
maxWidth: "100%",
225-
maxHeight: "100%",
226264
aspectRatio: props.aspectRatio,
227265
}}
228266
onMouseEnter={() => props.animationStart === "hover" && dotLottie?.play()}
@@ -250,11 +288,12 @@ let JsonLottieTmpComp = (function () {
250288

251289
{(useContext(EditorContext).editorModeStatus === "logic" || useContext(EditorContext).editorModeStatus === "both") && (
252290
<><Section name={sectionNames.interaction}>
291+
{children.onEvent.getPropertyView()}
253292
{children.speed.propertyView({ label: trans("jsonLottie.speed")})}
254293
{children.loop.propertyView({ label: trans("jsonLottie.loop")})}
255294
{children.animationStart.propertyView({ label: trans("jsonLottie.animationStart")})}
256-
{children.keepLastFrame.propertyView({ label: trans("jsonLottie.keepLastFrame")})}
257295
{hiddenPropertyView(children)}
296+
{children.keepLastFrame.propertyView({ label: trans("jsonLottie.keepLastFrame")})}
258297
{showDataLoadingIndicatorsPropertyView(children)}
259298
</Section>
260299
</>
@@ -291,6 +330,40 @@ JsonLottieTmpComp = class extends JsonLottieTmpComp {
291330
return this.children.autoHeight.getView();
292331
}
293332
};
333+
334+
JsonLottieTmpComp = withMethodExposing(JsonLottieTmpComp, [
335+
{
336+
method: {
337+
name: "play",
338+
description: trans("jsonLottie.play"),
339+
params: [],
340+
},
341+
execute: (comp) => {
342+
(comp.children.dotLottieRef.value as unknown as DotLottie)?.play();
343+
},
344+
},
345+
{
346+
method: {
347+
name: "pause",
348+
description: trans("jsonLottie.pause"),
349+
params: [],
350+
},
351+
execute: (comp) => {
352+
(comp.children.dotLottieRef.value as unknown as DotLottie)?.pause();
353+
},
354+
},
355+
{
356+
method: {
357+
name: "stop",
358+
description: trans("jsonLottie.stop"),
359+
params: [],
360+
},
361+
execute: (comp) => {
362+
(comp.children.dotLottieRef.value as unknown as DotLottie)?.stop();
363+
},
364+
},
365+
]);
366+
294367
export const JsonLottieComp = withExposingConfigs(JsonLottieTmpComp, [
295368
new NameConfig("value", trans("jsonLottie.valueDesc")),
296369
NameConfigHidden,

Diff for: client/packages/lowcoder/src/comps/controls/iconscoutControl.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import styled from "styled-components";
1717
import Popover from "antd/es/popover";
1818
import { CloseIcon, SearchIcon } from "icons";
1919
import Draggable from "react-draggable";
20-
import IconScoutApi from "@lowcoder-ee/api/iconScoutApi";
20+
import IconScoutApi from "@lowcoder-ee/api/iconscoutApi";
2121
import { searchAssets, getAssetLinks, SearchParams } from "@lowcoder-ee/api/iconFlowApi";
2222
import List, { ListRowProps } from "react-virtualized/dist/es/List";
2323
import { debounce } from "lodash";

Diff for: client/packages/lowcoder/src/i18n/locales/en.ts

+6
Original file line numberDiff line numberDiff line change
@@ -3761,11 +3761,17 @@ export const en = {
37613761
"loop": "Loop",
37623762
"auto": "Auto",
37633763
"onHover": "On Hover",
3764+
"onTrigger": "On Trigger",
37643765
"singlePlay": "Single Play",
37653766
"endlessLoop": "Endless Loop",
37663767
"keepLastFrame": "Keep Last Frame displayed",
37673768
"fit": "Fit",
37683769
"align": "Align",
3770+
"load": "On Load",
3771+
"play": "On Play",
3772+
"pause": "On Pause",
3773+
"stop": "On Stop",
3774+
"complete": "On Complete",
37693775
},
37703776
"timeLine": {
37713777
"titleColor": "Title Color",

0 commit comments

Comments
 (0)