Skip to content

Commit

Permalink
Merge pull request #24 from slatejack/feature/status-type
Browse files Browse the repository at this point in the history
Feature/status-type
  • Loading branch information
slatejack authored Jul 17, 2024
2 parents ada9a92 + 7b588e1 commit 4bd904d
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 17 deletions.
13 changes: 13 additions & 0 deletions .idea/material_theme_project_new.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions src/bulletScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import ReactDOM from 'react-dom';
import {defaultOptions, getContainer} from '@/utils/bulletHelper';
import {BulletStyle, pushItem, screenElement, ScreenOpsTypes} from '@/interface/screen';
import {AnimationPlayState, BulletStyle, pushItem, screenElement, ScreenOpsTypes} from '@/interface/screen';
import {isPlainObject} from '@/utils/utils';
import StyledBullet from './styleBullet';
import {ANIMATION_PLAY_STATE, TRACK_STATUS} from '@/constants/common';


type queueType = [pushItem, HTMLElement, (BulletStyle | undefined)];
Expand Down Expand Up @@ -39,7 +40,7 @@ class BulletScreen {
*/
initBulletTrack(trackHeight: number) {
const {height} = this.target.getBoundingClientRect();
this.tracks = new Array(Math.floor(height / trackHeight)).fill('idle'); // idle代表闲置状态的轨道
this.tracks = new Array(Math.floor(height / trackHeight)).fill(TRACK_STATUS.free);
const {position} = getComputedStyle(this.target);
if (position === 'static') {
this.target.style.position = 'relative';
Expand Down Expand Up @@ -123,25 +124,25 @@ class BulletScreen {
return <></>;
}

_toggleAnimateStatus = (id: string | null, status = 'paused') => {
_toggleAnimateStatus = (id: string | null, status: AnimationPlayState = 'paused') => {
const currItem = this.bullets.find(item => item.id == id);
if (currItem) {
currItem.style.animationPlayState = status;
return;
}

this.allPaused = status === 'paused';
this.allPaused = status === ANIMATION_PLAY_STATE.paused;
this.bullets.forEach(item => {
item.style.animationPlayState = status;
});
};

pause(id: string | null = null) {
this._toggleAnimateStatus(id, 'paused');
this._toggleAnimateStatus(id, ANIMATION_PLAY_STATE.paused as AnimationPlayState);
}

resume(id: string | null = null) {
this._toggleAnimateStatus(id, 'running');
this._toggleAnimateStatus(id, ANIMATION_PLAY_STATE.running as AnimationPlayState);
}

hide() {
Expand Down Expand Up @@ -173,7 +174,7 @@ class BulletScreen {
item.remove();
});
const {height} = this.target.getBoundingClientRect();
this.tracks = new Array(Math.floor(height / this.options.trackHeight)).fill('idle');
this.tracks = new Array(Math.floor(height / this.options.trackHeight)).fill(TRACK_STATUS.free);
this.queues = [];
this.bullets = [];
}
Expand All @@ -196,7 +197,7 @@ class BulletScreen {
let idx = -1;
// 优先取空闲状态的
this.tracks.forEach((status, index) => {
if (status === 'idle') {
if (status === TRACK_STATUS.free) {
readyIdxs.push(index);
}
});
Expand All @@ -206,7 +207,7 @@ class BulletScreen {
if (idx === -1) {
// 其次是可以接上状态的
this.tracks.forEach((status, index) => {
if (status === 'feed') {
if (status === TRACK_STATUS.feed) {
readyIdxs.push(index);
}
});
Expand All @@ -216,7 +217,7 @@ class BulletScreen {
}
// 如果此时状态值不等于-1,则说明该轨道在占用中
if (idx !== -1) {
this.tracks[idx] = 'running';
this.tracks[idx] = TRACK_STATUS.occupied;
}
return idx;
}
Expand Down Expand Up @@ -257,7 +258,7 @@ class BulletScreen {
}
} else {
if (typeof (trackIdx) !== 'undefined') {
this.tracks[trackIdx] = 'feed';
this.tracks[trackIdx] = TRACK_STATUS.feed;
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/constants/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const TRACK_STATUS = {
free: 'idle', // 空闲状态
feed: 'feed', // 占用结束状态
occupied: 'running', // 占用中
};

export const ANIMATION_PLAY_STATE = {
paused: 'paused',
running: 'running',
};
5 changes: 5 additions & 0 deletions src/interface/screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ interface BulletStyle {
bottom?: string;
}

type AnimationPlayState = 'running' | 'paused';
type TrackStatus = 'idle' | 'feed' | 'running';

export type {
pushItem,
screenElement,
ScreenOpsTypes,
pushItemObj,
BulletStyle,
AnimationPlayState,
TrackStatus,
};
4 changes: 2 additions & 2 deletions typings/bulletScreen.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BulletStyle, pushItem, screenElement, ScreenOpsTypes } from '@/interface/screen';
import { AnimationPlayState, BulletStyle, pushItem, screenElement, ScreenOpsTypes } from '@/interface/screen';
declare type queueType = [pushItem, HTMLElement, (BulletStyle | undefined)];
declare class BulletScreen {
target: HTMLElement;
Expand Down Expand Up @@ -30,7 +30,7 @@ declare class BulletScreen {
* @param Item
*/
getRenderDom(Item: pushItem): JSX.Element;
_toggleAnimateStatus: (id: string | null, status?: string) => void;
_toggleAnimateStatus: (id: string | null, status?: AnimationPlayState) => void;
pause(id?: string | null): void;
resume(id?: string | null): void;
hide(): void;
Expand Down
9 changes: 9 additions & 0 deletions typings/constants/common.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export declare const TRACK_STATUS: {
free: string;
feed: string;
occupied: string;
};
export declare const ANIMATION_PLAY_STATE: {
paused: string;
running: string;
};
4 changes: 3 additions & 1 deletion typings/interface/screen.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ interface BulletStyle {
top?: string;
bottom?: string;
}
export type { pushItem, screenElement, ScreenOpsTypes, pushItemObj, BulletStyle, };
declare type AnimationPlayState = 'running' | 'paused';
declare type TrackStatus = 'idle' | 'feed' | 'running';
export type { pushItem, screenElement, ScreenOpsTypes, pushItemObj, BulletStyle, AnimationPlayState, TrackStatus };
4 changes: 2 additions & 2 deletions typings/src/bulletScreen.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BulletStyle, pushItem, screenElement, ScreenOpsTypes } from '@/interface/screen';
import { AnimationPlayState, BulletStyle, pushItem, screenElement, ScreenOpsTypes } from '@/interface/screen';
declare type queueType = [pushItem, HTMLElement, (BulletStyle | undefined)];
declare class BulletScreen {
target: HTMLElement;
Expand Down Expand Up @@ -30,7 +30,7 @@ declare class BulletScreen {
* @param Item
*/
getRenderDom(Item: pushItem): JSX.Element;
_toggleAnimateStatus: (id: string | null, status?: string) => void;
_toggleAnimateStatus: (id: string | null, status?: AnimationPlayState) => void;
pause(id?: string | null): void;
resume(id?: string | null): void;
hide(): void;
Expand Down
9 changes: 9 additions & 0 deletions typings/src/constants/common.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export declare const TRACK_STATUS: {
free: string;
feed: string;
occupied: string;
};
export declare const ANIMATION_PLAY_STATE: {
paused: string;
running: string;
};
4 changes: 3 additions & 1 deletion typings/src/interface/screen.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ interface BulletStyle {
top?: string;
bottom?: string;
}
export type { pushItem, screenElement, ScreenOpsTypes, pushItemObj, BulletStyle, };
declare type AnimationPlayState = 'running' | 'paused';
declare type TrackStatus = 'idle' | 'feed' | 'running';
export type { pushItem, screenElement, ScreenOpsTypes, pushItemObj, BulletStyle, AnimationPlayState, TrackStatus };

0 comments on commit 4bd904d

Please sign in to comment.