-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [lb-component] Add TagToolInstanceAdaptor to implement toolInst…
…ance
- Loading branch information
1 parent
7ab27c2
commit 85cd646
Showing
8 changed files
with
322 additions
and
18 deletions.
There are no files selected for viewing
42 changes: 33 additions & 9 deletions
42
packages/lb-components/src/components/videoAnnotate/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
packages/lb-components/src/components/videoPlayer/TagToolInstanceAdaptor.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/** | ||
* @file 视频标签工具实现标签工具的方法 | ||
* @author lijingchi <[email protected]> | ||
* @date 2022-05-31 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { CommonToolUtils, uuid } from '@labelbee/lb-annotation'; | ||
import StepUtils from '@/utils/StepUtils'; | ||
import { jsonParser } from '@/utils'; | ||
import { VideoPlayer } from './index'; | ||
import { VideoTagLayer } from './VideoTagLayer'; | ||
import { IStepInfo } from '@/types/step'; | ||
|
||
export interface IZProp { | ||
imgIndex: number; | ||
imgList: any[]; | ||
pageForward: () => void; | ||
pageJump: (page: number) => void; | ||
pageBackward: () => void; | ||
onMounted: (instance: TagToolInstanceAdaptor) => void; | ||
onUnmounted: () => void; | ||
step: number; | ||
stepList: IStepInfo[]; | ||
} | ||
export interface IZState { | ||
tagResult: any[]; | ||
labelSelectedList: any; | ||
} | ||
|
||
export class TagToolInstanceAdaptor extends React.Component<IZProp, IZState> { | ||
constructor(props: IZProp) { | ||
super(props); | ||
this.state = { | ||
tagResult: [], | ||
labelSelectedList: [], | ||
}; | ||
} | ||
|
||
get config() { | ||
const stepInfo = StepUtils.getCurrentStepInfo(this.props.step, this.props.stepList); | ||
return jsonParser(stepInfo?.config); | ||
} | ||
|
||
// TODO 标签记录 | ||
get labelSelectedList() { | ||
return []; | ||
} | ||
|
||
get history() { | ||
return { initRecord: () => {} }; | ||
} | ||
|
||
get currentTagResult() { | ||
return this.state.tagResult; | ||
} | ||
|
||
set labelSelectedList(labelSelectedList: any) { | ||
this.setState({ | ||
labelSelectedList, | ||
}); | ||
} | ||
|
||
public clearResult = (sendMsg: boolean, value?: string) => { | ||
const newTag = value | ||
? this.state.tagResult.map((v) => { | ||
if (v?.result[value]) { | ||
delete v.result[value]; | ||
} | ||
return v; | ||
}) | ||
: []; | ||
|
||
this.setState({ | ||
tagResult: newTag, | ||
}); | ||
}; | ||
|
||
public exportData = () => { | ||
return [this.state.tagResult, { valid: true }]; | ||
}; | ||
|
||
public singleOn() {} | ||
|
||
public getTagResultByCode(num1: number, num2?: number) { | ||
try { | ||
const inputList = this.config?.inputList ?? []; | ||
const mulitTags = inputList.length > 1; | ||
const keycode1 = num2 !== undefined ? num1 : 0; | ||
const keycode2 = num2 !== undefined ? num2 : num1; | ||
const primaryTagConfig = mulitTags ? inputList[keycode1] : inputList[0]; | ||
const secondaryTagConfig = (primaryTagConfig.subSelected ?? [])[keycode2]; | ||
|
||
if (primaryTagConfig && secondaryTagConfig) { | ||
return { | ||
value: { | ||
key: primaryTagConfig.value, | ||
value: secondaryTagConfig.value, | ||
}, | ||
isMulti: primaryTagConfig.isMulti, | ||
}; | ||
} | ||
} catch { | ||
return; | ||
} | ||
} | ||
|
||
public setLabelBySelectedList(num1: number, num2?: number) { | ||
const newTagConfig = this.getTagResultByCode(num1, num2); | ||
const currentRes = this.state.tagResult.length > 0 ? this.state.tagResult[0].result : {}; | ||
|
||
if (newTagConfig) { | ||
const inputValue = { [newTagConfig.value.key]: newTagConfig.value.value }; | ||
// todo: 合并输入逻辑 | ||
const tagRes = newTagConfig.isMulti ? Object.assign(currentRes, inputValue) : inputValue; | ||
|
||
const tagResult = [ | ||
{ | ||
sourceID: CommonToolUtils.getSourceID(), | ||
id: uuid(8, 62), | ||
result: tagRes, | ||
}, | ||
]; | ||
|
||
this.setState({ | ||
tagResult, | ||
}); | ||
} | ||
} | ||
|
||
public setResult = (tagResult: any[]) => { | ||
this.setState({ | ||
tagResult, | ||
}); | ||
}; | ||
|
||
public setLabel = (num1: number, num2: number) => { | ||
this.setLabelBySelectedList(num1, num2); | ||
}; | ||
|
||
public componentDidMount() { | ||
// document.addEventListener('keydown', this.keydown); | ||
this.props.onMounted(this); | ||
} | ||
|
||
public componentWillMount() { | ||
// document.addEventListener('keydown', this.keydown); | ||
this.props.onUnmounted(); | ||
} | ||
|
||
public shouldComponentUpdate({ imgIndex, imgList, step }: IZProp) { | ||
if (imgIndex !== this.props.imgIndex) { | ||
this.setState({ | ||
tagResult: jsonParser(imgList[imgIndex].result)[`step_${step}`]?.result ?? [], | ||
}); | ||
} | ||
return true; | ||
} | ||
|
||
public render() { | ||
const { imgIndex, imgList, pageForward, pageJump, pageBackward } = this.props; | ||
const { tagResult } = this.state; | ||
|
||
return ( | ||
<div style={{ height: '100%', width: '100%', position: 'relative' }}> | ||
<VideoPlayer | ||
imgIndex={imgIndex} | ||
imgList={imgList} | ||
pageBackward={pageBackward} | ||
pageForward={pageForward} | ||
pageJump={pageJump} | ||
/> | ||
<VideoTagLayer result={tagResult} inputList={this.config?.inputList} /> | ||
</div> | ||
); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
packages/lb-components/src/components/videoPlayer/VideoTagLayer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React from 'react'; | ||
|
||
/** | ||
* 通过 key和value在inputList找到对应的标签 | ||
* @param key | ||
* @param value | ||
* @param inputList | ||
*/ | ||
const findTagLabel = (key: string, value: string, inputList: any[]) => { | ||
const primaryTagConfig = inputList.find((i) => i.value === key); | ||
const secondaryTagConfig = primaryTagConfig.subSelected.find((i) => i.value === value); | ||
return { keyLabel: primaryTagConfig.key, valueLabel: secondaryTagConfig.key }; | ||
}; | ||
|
||
const result2LabelKey = (result: any[], inputList: any[]) => { | ||
try { | ||
return ( | ||
result?.reduce( | ||
( | ||
exitsTags: Array<{ keyLabel: string; valueLabel: string }>, | ||
res: { result: { [key: string]: string } }, | ||
) => { | ||
Object.keys(res.result).forEach((key) => { | ||
const value = res.result[key]; | ||
const { keyLabel, valueLabel } = findTagLabel(key, value, inputList); | ||
if (!exitsTags.find((i) => i.keyLabel === keyLabel && i.valueLabel === valueLabel)) { | ||
exitsTags.push({ keyLabel, valueLabel }); | ||
} | ||
}); | ||
return exitsTags; | ||
}, | ||
[], | ||
) ?? [] | ||
); | ||
} catch (error) { | ||
return []; | ||
} | ||
}; | ||
|
||
export const VideoTagLayer = ({ | ||
result, | ||
inputList, | ||
}: { | ||
result: Array<{ result: { [key: string]: string } }>; | ||
inputList: any[]; | ||
}) => { | ||
const cssProperty: React.CSSProperties = { | ||
position: 'absolute', | ||
zIndex: 20, | ||
padding: '0 20px', | ||
color: 'white', | ||
fontSize: 15, | ||
lineHeight: '32px', | ||
background: 'rgba(0, 255, 255, 0.32)', | ||
top: 0, | ||
right: 0, | ||
maxHeight: 'calc(100% - 80px)', | ||
overflowY: 'scroll', | ||
}; | ||
|
||
const tags: Array<{ keyLabel: string; valueLabel: string }> = result2LabelKey(result, inputList); | ||
|
||
return ( | ||
<div style={cssProperty}> | ||
<table> | ||
<tbody> | ||
{tags.map(({ keyLabel, valueLabel }) => ( | ||
<tr key={keyLabel}> | ||
<td style={{ paddingRight: 8 }}>{`${keyLabel}:`}</td> | ||
<td>{`${valueLabel}`}</td> | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.