base.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Copyright (c) 2018-2019 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 * as React from 'react';
  8. import { Observable, Subscription } from 'rxjs';
  9. import { PluginContext } from '../context';
  10. export const PluginReactContext = React.createContext(void 0 as any as PluginContext);
  11. export abstract class PluginUIComponent<P = {}, S = {}, SS = {}> extends React.Component<P, S, SS> {
  12. static contextType = PluginReactContext;
  13. readonly plugin: PluginContext;
  14. private subs: Subscription[] | undefined = void 0;
  15. protected subscribe<T>(obs: Observable<T>, action: (v: T) => void) {
  16. if (typeof this.subs === 'undefined') this.subs = []
  17. this.subs.push(obs.subscribe(action));
  18. }
  19. componentWillUnmount() {
  20. if (!this.subs) return;
  21. for (const s of this.subs) s.unsubscribe();
  22. }
  23. protected init?(): void;
  24. constructor(props: P, context?: any) {
  25. super(props, context);
  26. this.plugin = context;
  27. if (this.init) this.init();
  28. }
  29. }
  30. export abstract class PurePluginUIComponent<P = {}, S = {}, SS = {}> extends React.PureComponent<P, S, SS> {
  31. static contextType = PluginReactContext;
  32. readonly plugin: PluginContext;
  33. private subs: Subscription[] | undefined = void 0;
  34. protected subscribe<T>(obs: Observable<T>, action: (v: T) => void) {
  35. if (typeof this.subs === 'undefined') this.subs = []
  36. this.subs.push(obs.subscribe(action));
  37. }
  38. componentWillUnmount() {
  39. if (!this.subs) return;
  40. for (const s of this.subs) s.unsubscribe();
  41. }
  42. protected init?(): void;
  43. constructor(props: P, context?: any) {
  44. super(props, context);
  45. this.plugin = context;
  46. if (this.init) this.init();
  47. }
  48. }
  49. export type _Props<C extends React.Component> = C extends React.Component<infer P> ? P : never
  50. export type _State<C extends React.Component> = C extends React.Component<any, infer S> ? S : never
  51. //
  52. export type CollapsableProps = { initiallyCollapsed?: boolean, header?: string }
  53. export type CollapsableState = { isCollapsed: boolean, header: string }
  54. export abstract class CollapsableControls<P extends CollapsableProps = CollapsableProps, S extends CollapsableState = CollapsableState, SS = {}> extends PluginUIComponent<P, S, SS> {
  55. toggleCollapsed = () => {
  56. this.setState({ isCollapsed: !this.state.isCollapsed })
  57. }
  58. protected abstract defaultState(): S
  59. protected abstract renderControls(): JSX.Element | null
  60. render() {
  61. const wrapClass = this.state.isCollapsed
  62. ? 'msp-transform-wrapper msp-transform-wrapper-collapsed'
  63. : 'msp-transform-wrapper';
  64. return <div className={wrapClass}>
  65. <div className='msp-transform-header'>
  66. <button className='msp-btn msp-btn-block msp-btn-collapse' onClick={this.toggleCollapsed}>
  67. <span className={`msp-icon msp-icon-${this.state.isCollapsed ? 'expand' : 'collapse'}`} />
  68. {this.state.header}
  69. </button>
  70. </div>
  71. {!this.state.isCollapsed && this.renderControls()}
  72. </div>
  73. }
  74. constructor(props: P, context?: any) {
  75. super(props, context)
  76. const state = this.defaultState()
  77. if (props.initiallyCollapsed !== undefined) state.isCollapsed = props.initiallyCollapsed
  78. if (props.header !== undefined) state.header = props.header
  79. this.state = state
  80. }
  81. }