plugin.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import * as React from 'react';
  7. import { PluginContext } from '../context';
  8. import { StateTree } from './state-tree';
  9. import { Viewport, ViewportControls } from './viewport';
  10. import { Controls, TrajectoryControls } from './controls';
  11. import { PluginComponent, PluginReactContext } from './base';
  12. import { CameraSnapshots } from './camera';
  13. import { StateSnapshots } from './state';
  14. import { List } from 'immutable';
  15. import { LogEntry } from 'mol-util/log-entry';
  16. import { formatTime } from 'mol-util';
  17. import { BackgroundTaskProgress } from './task';
  18. import { ApplyActionContol } from './state/apply-action';
  19. import { PluginState } from 'mol-plugin/state';
  20. import { UpdateTransformContol } from './state/update-transform';
  21. export class Plugin extends React.Component<{ plugin: PluginContext }, {}> {
  22. region(kind: 'left' | 'right' | 'bottom' | 'main', element: JSX.Element) {
  23. return <div className={`msp-layout-region msp-layout-${kind}`}>
  24. <div className='msp-layout-static'>
  25. {element}
  26. </div>
  27. </div>
  28. }
  29. render() {
  30. return <PluginReactContext.Provider value={this.props.plugin}>
  31. <div className='msp-plugin'>
  32. <div className='msp-plugin-content msp-layout-expanded'>
  33. <div className='msp-layout-hide-top'>
  34. {this.region('main', <ViewportWrapper />)}
  35. {this.region('left', <State />)}
  36. {this.region('right', <div className='msp-scrollable-container msp-right-controls'>
  37. <CurrentObject />
  38. <Controls />
  39. <CameraSnapshots />
  40. <StateSnapshots />
  41. </div>)}
  42. {this.region('bottom', <Log />)}
  43. </div>
  44. </div>
  45. </div>
  46. </PluginReactContext.Provider>;
  47. }
  48. }
  49. export class ViewportWrapper extends PluginComponent {
  50. render() {
  51. return <>
  52. <Viewport />
  53. <div style={{ position: 'absolute', left: '10px', top: '10px', height: '100%', color: 'white' }}>
  54. <TrajectoryControls />
  55. </div>
  56. <ViewportControls />
  57. <div style={{ position: 'absolute', left: '10px', bottom: '10px', color: 'white' }}>
  58. <BackgroundTaskProgress />
  59. </div>
  60. </>;
  61. }
  62. }
  63. export class State extends PluginComponent {
  64. componentDidMount() {
  65. this.subscribe(this.plugin.state.behavior.kind, () => this.forceUpdate());
  66. }
  67. set(kind: PluginState.Kind) {
  68. // TODO: do command for this?
  69. this.plugin.state.setKind(kind);
  70. }
  71. render() {
  72. const kind = this.plugin.state.behavior.kind.value;
  73. return <>
  74. <button onClick={() => this.set('data')} style={{ fontWeight: kind === 'data' ? 'bold' : 'normal'}}>Data</button>
  75. <button onClick={() => this.set('behavior')} style={{ fontWeight: kind === 'behavior' ? 'bold' : 'normal'}}>Behavior</button>
  76. <StateTree state={kind === 'data' ? this.plugin.state.dataState : this.plugin.state.behaviorState} />
  77. </>
  78. }
  79. }
  80. export class Log extends PluginComponent<{}, { entries: List<LogEntry> }> {
  81. private wrapper = React.createRef<HTMLDivElement>();
  82. componentDidMount() {
  83. this.subscribe(this.plugin.events.log, e => this.setState({ entries: this.state.entries.push(e) }));
  84. }
  85. componentDidUpdate() {
  86. this.scrollToBottom();
  87. }
  88. state = { entries: List<LogEntry>() };
  89. private scrollToBottom() {
  90. const log = this.wrapper.current;
  91. if (log) log.scrollTop = log.scrollHeight - log.clientHeight - 1;
  92. }
  93. render() {
  94. return <div ref={this.wrapper} style={{ position: 'absolute', top: '0', right: '0', bottom: '0', left: '0', overflowY: 'scroll' }}>
  95. <ul style={{ listStyle: 'none' }}>
  96. {this.state.entries.map((e, i) => <li key={i} style={{ borderBottom: '1px solid #999', padding: '3px' }}>
  97. [{e!.type}] [{formatTime(e!.timestamp)}] {e!.message}
  98. </li>)}
  99. </ul>
  100. </div>;
  101. }
  102. }
  103. export class CurrentObject extends PluginComponent {
  104. get current() {
  105. return this.plugin.state.behavior.currentObject.value;
  106. }
  107. componentDidMount() {
  108. this.subscribe(this.plugin.state.behavior.currentObject, o => {
  109. this.forceUpdate();
  110. });
  111. this.subscribe(this.plugin.events.state.object.updated, ({ ref, state }) => {
  112. const current = this.current;
  113. if (current.ref !== ref || current.state !== state) return;
  114. this.forceUpdate();
  115. });
  116. }
  117. render() {
  118. const current = this.current;
  119. const ref = current.ref;
  120. // const n = this.props.plugin.state.data.tree.nodes.get(ref)!;
  121. const obj = current.state.cells.get(ref)!;
  122. const type = obj && obj.obj ? obj.obj.type : void 0;
  123. const transform = current.state.transforms.get(ref);
  124. const actions = type
  125. ? current.state.actions.fromType(type)
  126. : []
  127. return <>
  128. <div className='msp-section-header'>
  129. {obj.obj ? obj.obj.label : ref}
  130. </div>
  131. <UpdateTransformContol state={current.state} transform={transform} />
  132. {
  133. actions.map((act, i) => <ApplyActionContol plugin={this.plugin} key={`${act.id}`} state={current.state} action={act} nodeRef={ref} />)
  134. }
  135. </>;
  136. }
  137. }