open.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import * as React from 'react';
  7. import { ParamDefinition as PD } from 'molstar/lib/mol-util/param-definition';
  8. import { StateAction, StateTransform } from 'molstar/lib/mol-state';
  9. import { PluginStateObject } from 'molstar/lib/mol-plugin-state/objects';
  10. import { PluginContext } from 'molstar/lib/mol-plugin/context';
  11. import { Task } from 'molstar/lib/mol-task';
  12. import { ApplyActionControl } from 'molstar/lib/mol-plugin-ui/state/apply-action';
  13. import { PluginUIComponent } from 'molstar/lib/mol-plugin-ui/base';
  14. import { StructureViewerState } from '../types';
  15. const OpenFileAction = StateAction.build({
  16. display: { name: 'Open mmCIF File', description: 'Load a file and create its default visuals' },
  17. from: PluginStateObject.Root,
  18. params: (a, ctx: PluginContext) => {
  19. return {
  20. file: PD.File({ accept: '.cif, .mcif, .mmcif, .bcif, .gz, .zip' })
  21. }
  22. }
  23. })(({ params, state }, ctx: PluginContext) => Task.create('Open File', async taskCtx => {
  24. if (params.file.type !== 'cif' && params.file.type !== 'bcif') {
  25. throw new Error(`unsupported file format '${params.file.type}`)
  26. }
  27. await StructureViewerState(ctx).modelLoader.load({ fileOrUrl: params.file, format: params.file.type, })
  28. }));
  29. export class OpenFile extends PluginUIComponent<{ initiallyCollapsed?: boolean }> {
  30. render() {
  31. return <ApplyActionControl key={`${OpenFileAction.id}`} state={this.plugin.state.data} action={OpenFileAction} nodeRef={StateTransform.RootRef} initiallyCollapsed={this.props.initiallyCollapsed} />
  32. }
  33. }