-
Notifications
You must be signed in to change notification settings - Fork 471
/
Copy pathEditorContext.tsx
168 lines (140 loc) · 4.92 KB
/
EditorContext.tsx
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { create } from 'zustand';
import debounce from '@mui/material/utils/debounce';
import getConfiguration from '../../getConfiguration';
import { TEditorConfiguration } from './core';
type TValue = {
document: TEditorConfiguration;
documentHistory: Array<TEditorConfiguration>,
documentHistoryIndex: number,
selectedBlockId: string | null;
selectedSidebarTab: 'block-configuration' | 'styles';
selectedMainTab: 'editor' | 'preview' | 'json' | 'html';
selectedScreenSize: 'desktop' | 'mobile';
inspectorDrawerOpen: boolean;
samplesDrawerOpen: boolean;
};
const editorStateStore = create<TValue>(() => ({
document: getConfiguration(window.location.hash),
documentHistory: [],
documentHistoryIndex: 0,
selectedBlockId: null,
selectedSidebarTab: 'styles',
selectedMainTab: 'editor',
selectedScreenSize: 'desktop',
inspectorDrawerOpen: true,
samplesDrawerOpen: true,
}));
export function useDocument() {
return editorStateStore((s) => s.document);
}
export function useSelectedBlockId() {
return editorStateStore((s) => s.selectedBlockId);
}
export function useSelectedScreenSize() {
return editorStateStore((s) => s.selectedScreenSize);
}
export function useSelectedMainTab() {
return editorStateStore((s) => s.selectedMainTab);
}
export function setSelectedMainTab(selectedMainTab: TValue['selectedMainTab']) {
return editorStateStore.setState({ selectedMainTab });
}
export function useSelectedSidebarTab() {
return editorStateStore((s) => s.selectedSidebarTab);
}
export function useInspectorDrawerOpen() {
return editorStateStore((s) => s.inspectorDrawerOpen);
}
export function useSamplesDrawerOpen() {
return editorStateStore((s) => s.samplesDrawerOpen);
}
export function setSelectedBlockId(selectedBlockId: TValue['selectedBlockId']) {
const selectedSidebarTab = selectedBlockId === null ? 'styles' : 'block-configuration';
const options: Partial<TValue> = {};
if (selectedBlockId !== null) {
options.inspectorDrawerOpen = true;
}
return editorStateStore.setState({
selectedBlockId,
selectedSidebarTab,
...options,
});
}
export function setSidebarTab(selectedSidebarTab: TValue['selectedSidebarTab']) {
return editorStateStore.setState({ selectedSidebarTab });
}
const addDocumentToHistory = debounce(function (document: TValue['document']) {
let documentHistory = editorStateStore.getState().documentHistory;
const currentIndex = editorStateStore.getState().documentHistoryIndex;
if (currentIndex < documentHistory.length - 1) {
documentHistory = documentHistory.slice(0, currentIndex + 1);
}
documentHistory.push(document);
if (documentHistory.length > 254) {
documentHistory = documentHistory.slice(1);
}
editorStateStore.setState({
documentHistory,
documentHistoryIndex: documentHistory.length - 1,
});
}, 500);
export function resetDocument(document: TValue['document']) {
addDocumentToHistory(document);
return editorStateStore.setState({
document,
selectedSidebarTab: 'styles',
selectedBlockId: null,
});
}
export function setDocument(document: TValue['document']) {
const originalDocument = editorStateStore.getState().document;
const mergedDocument = {
...originalDocument,
...document,
};
addDocumentToHistory(mergedDocument);
return editorStateStore.setState({
document: mergedDocument,
});
}
export function useCanUndo() {
return editorStateStore(s => s.documentHistory.length > 0 && s.documentHistoryIndex > 0);
}
export function useCanRedo() {
return editorStateStore(s => s.documentHistory.length > 0 && s.documentHistoryIndex < s.documentHistory.length - 1);
}
export function undo() {
const documentHistory = editorStateStore.getState().documentHistory;
const currentIndex = editorStateStore.getState().documentHistoryIndex;
if (documentHistory.length <= 0 || currentIndex <= 0) {
return;
}
const changeToIndex = currentIndex - 1;
editorStateStore.setState({
document: documentHistory[changeToIndex],
documentHistoryIndex: changeToIndex,
});
}
export function redo() {
const documentHistory = editorStateStore.getState().documentHistory;
const currentIndex = editorStateStore.getState().documentHistoryIndex;
if (documentHistory.length <= 0 || currentIndex >= documentHistory.length - 1) {
return;
}
const changeToIndex = currentIndex + 1;
editorStateStore.setState({
document: documentHistory[changeToIndex],
documentHistoryIndex: changeToIndex,
});
}
export function toggleInspectorDrawerOpen() {
const inspectorDrawerOpen = !editorStateStore.getState().inspectorDrawerOpen;
return editorStateStore.setState({ inspectorDrawerOpen });
}
export function toggleSamplesDrawerOpen() {
const samplesDrawerOpen = !editorStateStore.getState().samplesDrawerOpen;
return editorStateStore.setState({ samplesDrawerOpen });
}
export function setSelectedScreenSize(selectedScreenSize: TValue['selectedScreenSize']) {
return editorStateStore.setState({ selectedScreenSize });
}