File size: 3,796 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React from 'react';
import { useOption, useUiTranslator } from '../../core/components/hooks';
import ToggleEdit from './ToggleEdit/index';
import ToggleInsert from './ToggleInsert/index';
import ToggleLayout from './ToggleLayout/index';
import TogglePreview from './TogglePreview/index';
import ToggleResize from './ToggleResize/index';
import UndoRedo from './UndoRedo';
import Zoom from './Zoom';

const getStickyNessstyle = (stickyness?: StickyNess): React.CSSProperties => {
  if (
    !stickyness ||
    (!stickyness.shouldStickToBottom && !stickyness.shouldStickToTop)
  ) {
    return {
      position: 'fixed',
      right: stickyness?.rightOffsetFixed || 0,
    };
  }

  return {
    position: 'absolute',
    bottom: stickyness.shouldStickToBottom ? 0 : 'auto',
    top: stickyness.shouldStickToTop ? 0 : 'auto',
    right: -stickyness.rightOffset || 0,
  };
};

function notEmpty<TValue>(value: TValue | null | undefined): value is TValue {
  return value !== null && value !== undefined;
}

export type StickyNess = {
  shouldStickToTop: boolean;
  shouldStickToBottom: boolean;
  rightOffset: number;
  rightOffsetFixed: number;
  stickyElRef?: React.Ref<HTMLDivElement>;
};
export const Sidebar: React.FC<{
  stickyNess?: StickyNess;
}> = ({ stickyNess }) => {
  const { t } = useUiTranslator();
  const zoomEnabled = useOption('zoomEnabled');
  const undoRedoEnabled = useOption('undoRedoEnabled');
  const editEnabled = useOption('editEnabled');
  const insertEnabled = useOption('insertEnabled');
  const layoutEnabled = useOption('layoutEnabled');
  const resizeEnabled = useOption('resizeEnabled');
  const previewEnabled = useOption('previewEnabled');
  const defaultLabels = {
    edit: 'Edit blocks',
    insert: 'Add blocks',
    layout: 'Move blocks',
    resize: 'Resize blocks',
    preview: 'Preview page',
  };

  const customOptions = useOption('customOptions');

  const actions = [
    // eslint-disable-next-line react/jsx-key
    undoRedoEnabled
      ? { action: <UndoRedo labelRedo="redo" labelUndo="undo" /> }
      : null,
    zoomEnabled
      ? { action: <Zoom labelZoomIn="zoom in" labelZoomOut="zoom out" /> }
      : null,
    editEnabled
      ? { action: <ToggleEdit label={t(defaultLabels.edit) ?? ''} /> }
      : null,
    insertEnabled
      ? { action: <ToggleInsert label={t(defaultLabels.insert) ?? ''} /> }
      : null,
    layoutEnabled
      ? { action: <ToggleLayout label={t(defaultLabels.layout) ?? ''} /> }
      : null,
    resizeEnabled
      ? { action: <ToggleResize label={t(defaultLabels.resize) ?? ''} /> }
      : null,
    previewEnabled
      ? { action: <TogglePreview label={t(defaultLabels.preview) ?? ''} /> }
      : null,
    ...(customOptions?.map((CustomOption) => ({ action: <CustomOption /> })) ?? []),
  ].filter(notEmpty);
  return (
    <div
      className="react-page-controls-mode-toggle-control-group"
      style={{
        position: 'fixed',
        zIndex: 10001,
        bottom: 0,
        right: 0,
        display: 'flex',
        maxHeight: '100%',
        ...getStickyNessstyle(stickyNess),
      }}
    >
      <div
        ref={stickyNess?.stickyElRef}
        style={{
          padding: 16,
          position: 'relative',

          flexFlow: 'column wrap',
          direction: 'rtl',

          display: 'flex',
        }}
      >
        {actions.map(({ action }, index) => (
          <div
            key={index}
            className="react-page-controls-mode-toggle-control"
            style={{
              animationDelay: (actions.length - index) * 150 + 'ms',
            }}
          >
            <>
              {action}
              <div className="react-page-controls-mode-toggle-clearfix" />
            </>
          </div>
        ))}
      </div>
    </div>
  );
};