HistoryLayout.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import * as React from 'react';
  2. import { DragactProps, DragactLayoutItem } from '../../src/lib/dragact-type';
  3. import { GridItemEvent } from '../../src/lib/GridItem';
  4. import { Dragact } from '../../src/lib/dragact';
  5. interface HistoryDragactState {
  6. layout: DragactLayoutItem[]
  7. }
  8. export class HistoryDragact extends React.Component<DragactProps, HistoryDragactState> {
  9. _actionsHistory: string[] = []
  10. _cacheLayouts: string;
  11. _activeItem: GridItemEvent
  12. _dragact: Dragact | null
  13. constructor(props: DragactProps) {
  14. super(props);
  15. this.state = { layout: props.layout } as any;
  16. }
  17. _cacheCurrentLayoutStart = (layoutItem: GridItemEvent) => {
  18. this._activeItem = layoutItem
  19. if (!this._dragact) {
  20. return;
  21. }
  22. this._cachingLayouts(this._dragact);
  23. }
  24. _cacheCurrentLayoutEnd = (layoutItem: GridItemEvent) => {
  25. const { GridY, GridX, h, w } = this._activeItem;
  26. if (GridX === layoutItem.GridX && GridY === layoutItem.GridY && h === layoutItem.h && w === layoutItem.w) {
  27. return;
  28. }
  29. this._storeLayoutToHistory(this._cacheLayouts)
  30. }
  31. _cachingLayouts = (d: Dragact) => {
  32. const initiateSnapShot = JSON.stringify({
  33. layout: d.getLayout(),
  34. })
  35. return this._cacheLayouts = initiateSnapShot;
  36. }
  37. goBack = () => {
  38. const mapLayoutHistory = this._actionsHistory;
  39. if (mapLayoutHistory.length > 1) {
  40. const last = mapLayoutHistory.pop();
  41. if (!last) {
  42. return;
  43. }
  44. this._changeDragactLayouts(last);
  45. }
  46. }
  47. reset = () => {
  48. if (!this._dragact) {
  49. return;
  50. }
  51. this._cachingLayouts(this._dragact);
  52. this._storeLayoutToHistory(this._cacheLayouts);
  53. const initiateSnapShot = this._actionsHistory[0];
  54. this._changeDragactLayouts(initiateSnapShot);
  55. }
  56. clear = () => {
  57. this._actionsHistory = this._actionsHistory.slice(0, 1);
  58. this._changeDragactLayouts(this._actionsHistory[0]);
  59. }
  60. onDragStart = (event: GridItemEvent) => {
  61. this._cacheCurrentLayoutStart(event)
  62. this.props.onDragStart && this.props.onDragStart(event)
  63. }
  64. onDragEnd = (event: GridItemEvent) => {
  65. this._cacheCurrentLayoutEnd(event);
  66. this.props.onDragEnd && this.props.onDragEnd(event)
  67. }
  68. _changeDragactLayouts = (snapshot: string) => {
  69. if (!this._dragact) {
  70. return;
  71. }
  72. try {
  73. const { layout } = JSON.parse(snapshot);
  74. this.setState({
  75. layout
  76. })
  77. } catch (e) {
  78. }
  79. }
  80. _storeLayoutToHistory = (layouts: string) => {
  81. this._actionsHistory.push(layouts);
  82. }
  83. componentDidMount() {
  84. if (this._dragact) {
  85. const initiateSnapShot = this._cachingLayouts(this._dragact);
  86. this._storeLayoutToHistory(initiateSnapShot)
  87. }
  88. }
  89. componentWillReceiveProps(nextProps: DragactProps) {
  90. this.setState({
  91. layout: nextProps.layout
  92. })
  93. }
  94. _dragactRefCallback = (d: Dragact) => {
  95. this._dragact = d;
  96. }
  97. get getDragact() {
  98. return this._dragact;
  99. }
  100. render() {
  101. const layout = this.state.layout;
  102. return <Dragact ref={this._dragactRefCallback} {...this.props} layout={layout} onDragStart={this.onDragStart} onDragEnd={this.onDragEnd} />
  103. }
  104. }