plugin.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**
  2. * Copyright (c) 2018-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { List } from 'immutable';
  8. import * as React from 'react';
  9. import { formatTime } from '../mol-util';
  10. import { LogEntry } from '../mol-util/log-entry';
  11. import { PluginReactContext, PluginUIComponent } from './base';
  12. import { AnimationViewportControls, DefaultStructureTools, LociLabels, StateSnapshotViewportControls, TrajectoryViewportControls, SelectionViewportControls } from './controls';
  13. import { LeftPanelControls } from './left-panel';
  14. import { SequenceView } from './sequence';
  15. import { BackgroundTaskProgress, OverlayTaskProgress } from './task';
  16. import { Toasts } from './toast';
  17. import { Viewport, ViewportControls } from './viewport';
  18. import { PluginCommands } from '../mol-plugin/commands';
  19. import { PluginUIContext } from './context';
  20. import { OpenFiles } from '../mol-plugin-state/actions/file';
  21. import { Asset } from '../mol-util/assets';
  22. import { BehaviorSubject } from 'rxjs';
  23. import { useBehavior } from './hooks/use-behavior';
  24. export class Plugin extends React.Component<{ plugin: PluginUIContext }, {}> {
  25. region(kind: 'left' | 'right' | 'bottom' | 'main', element: JSX.Element) {
  26. return <div className={`msp-layout-region msp-layout-${kind}`}>
  27. <div className='msp-layout-static'>
  28. {element}
  29. </div>
  30. </div>;
  31. }
  32. render() {
  33. return <PluginReactContext.Provider value={this.props.plugin}>
  34. <Layout />
  35. </PluginReactContext.Provider>;
  36. }
  37. }
  38. export class PluginContextContainer extends React.Component<{ plugin: PluginUIContext }> {
  39. render() {
  40. return <PluginReactContext.Provider value={this.props.plugin}>
  41. <div className='msp-plugin'>
  42. {this.props.children}
  43. </div>
  44. </PluginReactContext.Provider>;
  45. }
  46. }
  47. type RegionKind = 'top' | 'left' | 'right' | 'bottom' | 'main'
  48. class Layout extends PluginUIComponent {
  49. componentDidMount() {
  50. this.subscribe(this.plugin.layout.events.updated, () => this.forceUpdate());
  51. }
  52. region(kind: RegionKind, Element?: React.ComponentClass) {
  53. return <div className={`msp-layout-region msp-layout-${kind}`}>
  54. <div className='msp-layout-static'>
  55. {Element ? <Element /> : null}
  56. </div>
  57. </div>;
  58. }
  59. get layoutVisibilityClassName() {
  60. const layout = this.plugin.layout.state;
  61. const controls = this.plugin.spec.components?.controls ?? {};
  62. const classList: string[] = [];
  63. if (controls.top === 'none' || !layout.showControls || layout.regionState.top === 'hidden') {
  64. classList.push('msp-layout-hide-top');
  65. }
  66. if (controls.left === 'none' || !layout.showControls || layout.regionState.left === 'hidden') {
  67. classList.push('msp-layout-hide-left');
  68. } else if (layout.regionState.left === 'collapsed') {
  69. classList.push('msp-layout-collapse-left');
  70. }
  71. if (controls.right === 'none' || !layout.showControls || layout.regionState.right === 'hidden') {
  72. classList.push('msp-layout-hide-right');
  73. }
  74. if (controls.bottom === 'none' || !layout.showControls || layout.regionState.bottom === 'hidden') {
  75. classList.push('msp-layout-hide-bottom');
  76. }
  77. return classList.join(' ');
  78. }
  79. get layoutClassName() {
  80. const layout = this.plugin.layout.state;
  81. const classList: string[] = ['msp-plugin-content'];
  82. if (layout.isExpanded) {
  83. classList.push('msp-layout-expanded');
  84. } else {
  85. classList.push('msp-layout-standard', `msp-layout-standard-${layout.controlsDisplay}`);
  86. }
  87. return classList.join(' ');
  88. }
  89. onDrop = (ev: React.DragEvent<HTMLDivElement>) => {
  90. ev.preventDefault();
  91. const files: File[] = [];
  92. if (ev.dataTransfer.items) {
  93. // Use DataTransferItemList interface to access the file(s)
  94. for (let i = 0; i < ev.dataTransfer.items.length; i++) {
  95. if (ev.dataTransfer.items[i].kind !== 'file') continue;
  96. const file = ev.dataTransfer.items[i].getAsFile();
  97. if (file) files.push(file);
  98. }
  99. } else {
  100. for (let i = 0; i < ev.dataTransfer.files.length; i++) {
  101. const file = ev.dataTransfer.files[0];
  102. if (file) files.push(file);
  103. }
  104. }
  105. const sessions = files.filter(f => {
  106. const fn = f.name.toLowerCase();
  107. return fn.endsWith('.molx') || fn.endsWith('.molj');
  108. });
  109. if (sessions.length > 0) {
  110. PluginCommands.State.Snapshots.OpenFile(this.plugin, { file: sessions[0] });
  111. } else {
  112. this.plugin.runTask(this.plugin.state.data.applyAction(OpenFiles, {
  113. files: files.map(f => Asset.File(f)),
  114. format: { name: 'auto', params: {} },
  115. visuals: true
  116. }));
  117. }
  118. }
  119. onDragOver = (ev: React.DragEvent<HTMLDivElement>) => {
  120. ev.preventDefault();
  121. }
  122. private showDragOverlay = new BehaviorSubject(false);
  123. onDragEnter = () => this.showDragOverlay.next(true);
  124. render() {
  125. const layout = this.plugin.layout.state;
  126. const controls = this.plugin.spec.components?.controls || {};
  127. const viewport = this.plugin.spec.components?.viewport?.view || DefaultViewport;
  128. return <div className='msp-plugin'>
  129. <div className={this.layoutClassName} onDragEnter={this.onDragEnter}>
  130. <div className={this.layoutVisibilityClassName}>
  131. {this.region('main', viewport)}
  132. {layout.showControls && controls.top !== 'none' && this.region('top', controls.top || SequenceView)}
  133. {layout.showControls && controls.left !== 'none' && this.region('left', controls.left || LeftPanelControls)}
  134. {layout.showControls && controls.right !== 'none' && this.region('right', controls.right || ControlsWrapper)}
  135. {layout.showControls && controls.bottom !== 'none' && this.region('bottom', controls.bottom || Log)}
  136. </div>
  137. {!this.plugin.spec.components?.hideTaskOverlay && <OverlayTaskProgress />}
  138. <DragOverlay plugin={this.plugin} showDragOverlay={this.showDragOverlay} />
  139. </div>
  140. </div>;
  141. }
  142. }
  143. function dropFiles(ev: React.DragEvent<HTMLDivElement>, plugin: PluginUIContext, showDragOverlay: BehaviorSubject<boolean>) {
  144. ev.preventDefault();
  145. ev.stopPropagation();
  146. showDragOverlay.next(false);
  147. const files: File[] = [];
  148. if (ev.dataTransfer.items) {
  149. // Use DataTransferItemList interface to access the file(s)
  150. for (let i = 0; i < ev.dataTransfer.items.length; i++) {
  151. if (ev.dataTransfer.items[i].kind !== 'file') continue;
  152. const file = ev.dataTransfer.items[i].getAsFile();
  153. if (file) files.push(file);
  154. }
  155. } else {
  156. for (let i = 0; i < ev.dataTransfer.files.length; i++) {
  157. const file = ev.dataTransfer.files[0];
  158. if (file) files.push(file);
  159. }
  160. }
  161. const sessions = files.filter(f => {
  162. const fn = f.name.toLowerCase();
  163. return fn.endsWith('.molx') || fn.endsWith('.molj');
  164. });
  165. if (sessions.length > 0) {
  166. PluginCommands.State.Snapshots.OpenFile(plugin, { file: sessions[0] });
  167. } else {
  168. plugin.runTask(plugin.state.data.applyAction(OpenFiles, {
  169. files: files.map(f => Asset.File(f)),
  170. format: { name: 'auto', params: {} },
  171. visuals: true
  172. }));
  173. }
  174. }
  175. function DragOverlay({ plugin, showDragOverlay }: { plugin: PluginUIContext, showDragOverlay: BehaviorSubject<boolean> }) {
  176. const show = useBehavior(showDragOverlay);
  177. const preventDrag = (e: React.DragEvent) => {
  178. e.dataTransfer.dropEffect = 'copy';
  179. e.preventDefault();
  180. e.stopPropagation();
  181. };
  182. return <div
  183. className='msp-drag-drop-overlay'
  184. style={{ display: show ? 'flex' : 'none' }}
  185. onDragEnter={preventDrag}
  186. onDragOver={preventDrag}
  187. onDragLeave={() => showDragOverlay.next(false)}
  188. onDrop={e => dropFiles(e, plugin, showDragOverlay)}
  189. >
  190. Upload File(s)
  191. </div>;
  192. }
  193. export class ControlsWrapper extends PluginUIComponent {
  194. render() {
  195. const StructureTools = this.plugin.spec.components?.structureTools || DefaultStructureTools;
  196. return <div className='msp-scrollable-container'>
  197. <StructureTools />
  198. </div>;
  199. }
  200. }
  201. export class DefaultViewport extends PluginUIComponent {
  202. render() {
  203. const VPControls = this.plugin.spec.components?.viewport?.controls || ViewportControls;
  204. return <>
  205. <Viewport />
  206. <div className='msp-viewport-top-left-controls'>
  207. <AnimationViewportControls />
  208. <TrajectoryViewportControls />
  209. <StateSnapshotViewportControls />
  210. </div>
  211. <SelectionViewportControls />
  212. <VPControls />
  213. <BackgroundTaskProgress />
  214. <div className='msp-highlight-toast-wrapper'>
  215. <LociLabels />
  216. <Toasts />
  217. </div>
  218. </>;
  219. }
  220. }
  221. export class Log extends PluginUIComponent<{}, { entries: List<LogEntry> }> {
  222. private wrapper = React.createRef<HTMLDivElement>();
  223. componentDidMount() {
  224. this.subscribe(this.plugin.events.log, () => this.setState({ entries: this.plugin.log.entries }));
  225. }
  226. componentDidUpdate() {
  227. this.scrollToBottom();
  228. }
  229. state = { entries: this.plugin.log.entries };
  230. private scrollToBottom() {
  231. const log = this.wrapper.current;
  232. if (log) log.scrollTop = log.scrollHeight - log.clientHeight - 1;
  233. }
  234. render() {
  235. // TODO: ability to show full log
  236. // showing more entries dramatically slows animations.
  237. const maxEntries = 10;
  238. const xs = this.state.entries, l = xs.size;
  239. const entries: JSX.Element[] = [];
  240. for (let i = Math.max(0, l - maxEntries), o = 0; i < l; i++) {
  241. const e = xs.get(i);
  242. entries.push(<li key={o++}>
  243. <div className={'msp-log-entry-badge msp-log-entry-' + e!.type} />
  244. <div className='msp-log-timestamp'>{formatTime(e!.timestamp)}</div>
  245. <div className='msp-log-entry'>{e!.message}</div>
  246. </li>);
  247. }
  248. return <div ref={this.wrapper} className='msp-log' style={{ position: 'absolute', top: '0', right: '0', bottom: '0', left: '0', overflowY: 'auto' }}>
  249. <ul className='msp-list-unstyled'>{entries}</ul>
  250. </div>;
  251. }
  252. }