import * as React from 'react'; import Srl from "../data/Srl"; import SrlAbsolute from "./SrlAbsolutePosition/SrlAbsolute"; import TokenAbsolute from "./SrlAbsolutePosition/TokenAbsolute"; import LabelAbsolute from "./SrlAbsolutePosition/LabelAbsolute"; import Frame from "../data/Frame"; import DefaultInks from "./Inks"; import TokensSelection from "./Selection/TokensSelection"; import RoleSelection from "./Selection/RoleSelection"; import {isTokenSelected, isRoleSelected} from "./Selection/isSelected"; import DragingSelection from "./Selection/DragingSelection"; import { LINE_HEIGHT, LABEL_FONT_SIZE, LABEL_FONT_FAMILY, TOKEN_FONT_SIZE, TRIANGLE_SIZE, SELECTION_BG, TOKEN_FONT_FAMILY } from './LayoutConf'; import SrlVisualizerNoSelection from "./SrlVisualizerNoSelection"; import * as Styles from "./SrlVisualizerStyles"; export type Selection = null|TokensSelection|RoleSelection|DragingSelection; type Props = { srl: Srl, noArrow?: boolean, inks?: typeof DefaultInks, onRequestSelectionChange?: (selection:Selection)=>void, onLayoutChange?: (layout:SrlAbsolute|null)=>void, selection?: Selection, width?: number, }; type State = { srlAbsolute: null|SrlAbsolute, selection: Selection, }; class SrlVisualizer extends React.PureComponent { constructor(props:Props) { super(props); this.state = {srlAbsolute: null, selection: null}; } handleSelect = (selection:Selection):void => { if(typeof(this.props.selection)!=='undefined') { setTimeout(()=>this.props.onRequestSelectionChange && this.props.onRequestSelectionChange(selection),0); } else { this.setState({selection:selection}); } }; getSelection ():null|Selection { if(typeof(this.props.selection) === 'undefined') return this.state.selection; return this.props.selection; } handleLayoutChange = (srlAbsolute:null|SrlAbsolute)=> { this.setState({srlAbsolute}); setTimeout(()=>this.props.onLayoutChange && this.props.onLayoutChange(srlAbsolute),0); }; componentWillReceiveProps(newProps:Props) { if(newProps.srl === this.props.srl && newProps.noArrow === this.props.noArrow) return; this.setState({srlAbsolute: null}); } render() { const {srl, noArrow, inks, width} = this.props; return
{this.renderSvg()}
} renderSvg() { const srlAbsolute = this.state.srlAbsolute; if(srlAbsolute == null) return; return {this.renderSvgTokens(srlAbsolute.tokens)} {this.renderSvgLabels(srlAbsolute.labels)} } renderSvgTokens(tokenAbsolutes:TokenAbsolute[]) { const tokenStrs:string[] = this.props.srl.tokens; return tokenAbsolutes.map(token=>{ const commonTextStyle = { x:token.x, y:token.y+token.h, fontSize:TOKEN_FONT_SIZE, fontFamily:TOKEN_FONT_FAMILY }; if(isTokenSelected(this.getSelection(), token.index, this.props.srl.frames)) { return {tokenStrs[token.index]} } return null; }) } renderSvgLabels(labelAbsolutes:LabelAbsolute[]) { const frames:Frame[] = this.props.srl.frames; return labelAbsolutes.map(label=>{ const key = `${label.frameIndex},${label.roleIndex},${label.labelIndex}`; const commonTextStyle = {x:label.x,y:label.y+label.h,fontSize:LABEL_FONT_SIZE,fontFamily:LABEL_FONT_FAMILY}; const textContent = frames[label.frameIndex].roles[label.roleIndex].name; if(isRoleSelected(this.getSelection(), label.frameIndex, label.roleIndex)) { return {textContent} } return null; }) } } export default SrlVisualizer;