plugin.tsx 11 KB

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