Repository navigation
Expand file tree
/
Copy pathstate.ts
More file actions
251 lines (235 loc) · 7.15 KB
/
Copy pathstate.ts
File metadata and controls
251 lines (235 loc) · 7.15 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import {
Sheet,
SheetSnippet,
SheetSnippetComponent,
SheetSnippetComponentOutput
} from './index'
import { v4 as uuid } from 'uuid'
const initialState: Sheet = {
id: uuid(),
title: 'Welcome!',
description: '',
snippets: [
{
id: uuid(),
title: 'Welcome!',
order: 0,
components: [
{
id: uuid(),
order: 0,
content: `Welcome to the JSheets Demo`,
type: 'text'
},
{
id: uuid(),
order: 1,
content: `Math.min(420, 1337)`,
type: 'code'
}
]
}
]
}
const slice = createSlice({
name: "sheet",
initialState,
reducers: {
updateSheet(state, action: PayloadAction<Sheet>) {
return action.payload
},
changeDetails(state, action: PayloadAction<{title?: string, description?: string}>) {
const {title, description} = action.payload
if (title !== undefined) {
state.title = title
}
if (description !== undefined) {
state.description = description
}
},
changeSnippetDetails(state, action: PayloadAction<{snippetId: string, title?: string}>) {
const {snippetId, title} = action.payload
const snippet = state.snippets.find(snippet => snippet.id === snippetId)
if (snippet && title !== undefined) {
snippet.title = title
}
},
addSnippet(state, action: PayloadAction<Partial<SheetSnippet>>) {
const id = action.payload.id || uuid()
state.snippets.push({
title: '',
components: [],
order: findLastOrder(state.snippets.values()),
...action.payload,
id: id
});
},
deleteSnippet(state, action: PayloadAction<Partial<{id: string}>>) {
if (action.payload.id) {
const index = state.snippets.findIndex(snippet => snippet.id === action.payload.id)
if (index >= 0) {
state.snippets.splice(index, 1)
}
}
},
moveSnippet(state, action: PayloadAction<{id: string, direction: 'up' | 'down'}>) {
const {id ,direction} = action.payload
const snippet = state.snippets.find(snippet => snippet.id === id)
if (!snippet) {
return
}
const target = direction === 'down'
? findNextInOrder(snippet.order, state.snippets)
: findLastInOrder(snippet.order, state.snippets)
if (target) {
const snippetOrder = snippet.order
snippet.order = target.order
target.order = snippetOrder
}
},
reorderSnippet(state, action: PayloadAction<{from: number, to: number}>) {
const {from, to} = action.payload
const current = state.snippets.find(snippet => snippet.order == from)
const target = state.snippets.find(snippet => snippet.order == to)
if (current && target) {
current.order = to
target.order = from
}
},
deleteComponent(state, action: PayloadAction<{snippetId: string, componentId: string}>) {
const {snippetId, componentId} = action.payload
const snippet = state.snippets.find(snippet => snippet.id === snippetId)
if (!snippet) {
return
}
const index = snippet.components.findIndex(component => component.id === componentId)
if (index >= 0) {
snippet.components.splice(index, 1)
}
},
addComponent(state, action: PayloadAction<{snippetId: string, component: Partial<SheetSnippetComponent>}>) {
const {snippetId, component} = action.payload
const snippet = state.snippets.find(snippet => snippet.id === snippetId)
if (!snippet) {
return
}
const id = component.id || uuid()
snippet.components.push({
order: findLastOrder(snippet.components.values()),
type: 'code',
content: '',
...component,
id: id,
});
},
reorderComponent(state, action: PayloadAction<ReorderComponent>) {
const {snippetId, from, to} = action.payload
const snippet = state.snippets.find(snippet => snippet.id === snippetId)
if (!snippet) {
return
}
const current = snippet.components.find(component => component.order == from)
if (!current) {
return
}
if (current.order >= to) {
addRightGap(to, snippet.components)
} else {
addLeftGap(to, snippet.components)
}
current.order = to
},
removeOutput(state, action: PayloadAction<RemoveOutput>) {
const {snippetId, componentId} = action.payload
const findComponents = () => {
if (action.payload.snippetId) {
return state.snippets.find(snippet => snippet.id === snippetId)?.components
}
if (action.payload.componentId) {
return state.snippets.flatMap(snippet =>
snippet.components.filter(component => component.id === componentId)
)
}
return state.snippets.flatMap(snippet => snippet.components)
}
findComponents()?.forEach(component => component.output = undefined)
},
reportOutput(state, action: PayloadAction<ReportOutput>) {
state.snippets.forEach(snippet => {
snippet.components
.filter(component => component.id === action.payload.componentId)
.forEach(component => {
if (component.output) {
component.output.push(action.payload.output)
} else {
component.output = [action.payload.output]
}
})
})
}
}
})
interface ReportOutput {
output: SheetSnippetComponentOutput
componentId: string
}
interface RemoveOutput {
snippetId?: string
componentId?: string
}
function addRightGap(start: number, elements: {order: number}[]) {
for (const component of elements) {
if (component.order >= start) {
component.order++
}
}
}
function addLeftGap(start: number, elements: {order: number}[]) {
for (const component of elements) {
if (component.order <= start) {
component.order--
}
}
}
export interface ReorderComponent {
snippetId: string
from: number
to: number
}
function findNextInOrder<T extends {order: number}>(current: number, elements: T[]): T | undefined {
const index = findIndexSorted(current, elements)
return index ? elements[index + 1] : undefined
}
function findLastInOrder<T extends {order: number}>(current: number, elements: T[]): T | undefined {
const index = findIndexSorted(current, elements)
return index ? elements[index - 1] : undefined
}
function findIndexSorted<T extends {order: number}>(current: number, elements: T[]): number | undefined {
return elements.sort((left, right) => left.order - right.order)
.findIndex(element => element.order === current)
}
function findLastOrder(values: IterableIterator<{order: number}>): number {
let highest = 0
for (const value of values) {
if (value.order > highest) {
highest = value.order
}
}
return highest + 1
}
export const {
updateSheet,
addSnippet,
deleteSnippet,
moveSnippet,
reorderSnippet,
changeDetails,
reorderComponent,
addComponent,
reportOutput,
deleteComponent,
removeOutput,
changeSnippetDetails
} = slice.actions
export default slice.reducer