extensionsComponent.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
  2. import { faEllipsisH } from '@fortawesome/free-solid-svg-icons';
  3. import * as React from "react";
  4. import { Nullable } from "babylonjs/types";
  5. import { IExplorerExtensibilityGroup } from "babylonjs/Debug/debugLayer";
  6. interface IExtensionsComponentProps {
  7. target: any,
  8. extensibilityGroups?: IExplorerExtensibilityGroup[]
  9. }
  10. export class ExtensionsComponent extends React.Component<IExtensionsComponentProps, { popupVisible: boolean }> {
  11. private _popup: Nullable<HTMLDivElement>;
  12. private extensionRef: React.RefObject<HTMLDivElement>;
  13. constructor(props: IExtensionsComponentProps) {
  14. super(props);
  15. this.state = { popupVisible: false };
  16. this.extensionRef = React.createRef();
  17. }
  18. showPopup() {
  19. this.setState({ popupVisible: true });
  20. }
  21. componentDidMount() {
  22. if (!this._popup) {
  23. return;
  24. }
  25. this._popup.focus();
  26. }
  27. componentDidUpdate() {
  28. if (!this._popup) {
  29. return;
  30. }
  31. this._popup.focus();
  32. }
  33. render() {
  34. if (!this.props.extensibilityGroups) {
  35. return null;
  36. }
  37. let options = [];
  38. for (var group of this.props.extensibilityGroups) {
  39. if (group.predicate(this.props.target)) {
  40. options.push(...group.entries);
  41. }
  42. }
  43. if (options.length === 0) {
  44. return null;
  45. }
  46. return (
  47. <div ref={this.extensionRef} className="extensions" onClick={() => this.showPopup()}>
  48. <div title="Additional options" className="icon">
  49. <FontAwesomeIcon icon={faEllipsisH} />
  50. </div>
  51. <div ref={(input) => { this._popup = input }} tabIndex={-1} className={this.state.popupVisible ? "popup show" : "popup"} onBlur={() => this.setState({ popupVisible: false })}>
  52. {
  53. options.map(extensibility => {
  54. return (
  55. <div key={extensibility.label} className="popupMenu" onClick={() => extensibility.action(this.props.target)}>
  56. {extensibility.label}
  57. </div>
  58. )
  59. })
  60. }
  61. </div>
  62. </div>
  63. )
  64. }
  65. }