-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionDrawer.js
292 lines (260 loc) · 9.05 KB
/
SelectionDrawer.js
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
'use strict';
import React, { Fragment, forwardRef, useContext, useEffect, useImperativeHandle, useState } from 'react';
import {
ActivityIndicator,
StyleSheet,
} from 'react-native';
import * as Clipboard from 'expo-clipboard';
import {
FAB,
Portal,
Snackbar,
} from 'react-native-paper';
import DraggableDrawer from './DraggableDrawer';
import TagContainer from './TagContainer';
import Settings, { SettingsContext } from './Settings';
import TagEditorView from './TagEditorView';
import Animated, { LinearTransition } from 'react-native-reanimated';
const styles = StyleSheet.create({
defaultStyle: {
position: 'absolute',
width: '100%',
bottom: 0
},
bottomFabContainer: {
position: 'absolute',
bottom: 0,
right: 0,
flexDirection: 'row',
alignItems: 'center',
alignContent: 'center',
justifyContent: 'center',
padding: 8,
},
bottomFab: {
elevation: 8,
margin: 8,
},
expanded: {
height: '100%'
},
contracted: {
height: 300
},
closed: {
height: 1
}
});
const shuffleTags = (arr, start, end) => {
for (let i = start; i < end; i++) {
const startIdx = i;
const span = end - startIdx;
const element = startIdx + Math.floor(Math.random() * span);
const temp = arr[i];
arr[i] = arr[element];
arr[element] = temp;
}
}
const getShuffledSelection = (selectedItems, shuffle, maxTags) => {
let highPriIdx = 0; // So we can shuffle low-pri first.
// Since all high priority and added tags are at the top of the list, this prefers them.
const duplicatesRemoved = selectedItems.reduce((acc, value) => {
if (acc.findIndex(x => { return value.text == x.text; }) >= 0) {
return acc;
} else {
if (value.highPriority) highPriIdx++;
return acc.concat(value);
}
}, new Array());
if (shuffle) {
// Shuffle high priority tags
shuffleTags(duplicatesRemoved, 0, highPriIdx);
// Shuffle low priority tags
shuffleTags(duplicatesRemoved, highPriIdx, duplicatesRemoved.length);
// Shuffle first n tags
const truncationLength = Math.min(maxTags, duplicatesRemoved.length);
shuffleTags(duplicatesRemoved, 0, truncationLength);
}
return duplicatesRemoved;
}
const SelectionDrawer = forwardRef((props, ref) => {
const { copyLimit } = useContext(SettingsContext);
const [shuffle, setShuffle] = useState(false);
const [maxTags, setMaxTags] = useState(copyLimit);
const [shuffledTags, setShuffledTags] = useState(new Array());
const [{ visible: copiedSnackbarVisible, message: snackbarMessage }, setSnackbar] = useState(false);
const [addedItems, setAddedItems] = useState(new Array());
const [selectedItems, setSelectedItems] = useState(new Array());
const [editModalVisible, setEditModalVisible] = useState(false);
useEffect(() => {
if (props.open) {
setMaxTags(copyLimit);
}
}, [props.open]);
// Must be called to order a reshuffle.
const selectionChanged = (added, removed) => {
const highPriColor = 'red';
const lowPriColor = '';
let items = [...selectedItems];
if (removed) {
for (const path of removed) {
items = items.filter(x => x.path.toString() != path.toString());
}
}
if (added) {
for (const { path, node } of added) {
if (node.data) {
const newItems = node.data.map(x => {
return {
text: x,
color: node.highPriority ? highPriColor : lowPriColor,
path: path,
highPriority: node.highPriority,
};
});
if (node.highPriority) {
items = newItems.concat(items);
} else {
items = items.concat(newItems);
}
}
}
}
setSelectedItems(items);
const shuffledTags = getShuffledSelection(addedItems.concat(items), shuffle, maxTags);
setShuffledTags(shuffledTags);
}
useImperativeHandle(ref, () => {
return {
selectionCancelled: () => {
setAddedItems(new Array());
setSelectedItems(new Array());
setShuffledTags(new Array());
},
selectionChanged
};
}, [selectedItems, addedItems]);
useEffect(() => {
const shuffledTags = getShuffledSelection(addedItems.concat(selectedItems), shuffle, maxTags);
setShuffledTags(shuffledTags);
}, [shuffle])
const copyTags = () => {
const firstNTags = getUnculledItems().slice(0, maxTags ? maxTags : undefined);
const tagsString = firstNTags.reduce((acc, x) => {
return acc + "#" + x.text + " ";
}, "");
console.log("Copying to clipboad:");
console.log(tagsString);
Clipboard.setStringAsync(tagsString)
.then(() => setSnackbar({ visible: true, message: "Tags copied to clipboard!" }))
.catch((e) => setSnackbar({ visible: true, message: `Failed to copy to clipboard: ${e}` }));
}
const getUnculledItems = () => {
return shuffledTags.filter(x => !x.cull);
}
const onRemove = (text) => {
const newShuffle = JSON.parse(JSON.stringify(shuffledTags));
const idx = newShuffle.findIndex((x) => x.text == text);
const addedIdx = addedItems.findIndex((x) => x.text == text);
if (addedIdx > -1) {
newShuffle.splice(idx, 1);
addedItems.splice(addedIdx, 1);
setShuffledTags(newShuffle);
return true;
} else {
const newSelection = JSON.parse(JSON.stringify(selectedItems));
const selectedIdx = newSelection.findIndex((x) => x.text == text);
const curItem = newShuffle[idx]
const selectedItem = newSelection[selectedIdx]
curItem.cull = selectedItem.cull = !curItem.cull;
if (curItem.cull) {
curItem.opacity = selectedItem.opacity = 0.3;
} else {
curItem.opacity = selectedItem.opacity = 1.0;
}
setSelectedItems(newSelection);
setShuffledTags(newShuffle);
return false;
}
}
const onAddItems = (items) => {
const newItems = items.map(x => { return { text: x, color: '#ccf', highPriority: true } });
setAddedItems(addedItems.concat(newItems));
setShuffledTags(newItems.concat(shuffledTags));
setEditModalVisible(false);
}
const onReorderItems = (items) => {
setShuffledTags(items);
};
const onCycleMaxTags = () => {
const values = Object.values(Settings.maxTagOptions);
const idx = values.indexOf(maxTags);
setMaxTags(values[(idx + 1) % values.length]);
}
const unculledItems = getUnculledItems();
const quota = unculledItems &&
unculledItems.length > maxTags ? maxTags : unculledItems.length;
const previewComponents =
<TagContainer
preview
stylePredicate={(_item, idx) => idx && maxTags > 0 && idx >= maxTags && { opacity: 0.1 }}
style={{ width: '100%', height: '100%' }}
items={unculledItems}
onReorderItems={() => { }} />
const chipComponents =
<TagEditorView
style={{ width: '100%', height: '100%' }}
items={shuffledTags}
onRemoveItem={onRemove}
onAddItems={onAddItems}
onReorderItems={onReorderItems}
editModalVisible={editModalVisible}
onRequestModalClose={() => setEditModalVisible(false)} />
return (
<Fragment>
<DraggableDrawer
layout={LinearTransition}
style={[styles.defaultStyle, props.open ? (props.expanded ? styles.expanded : styles.contracted) : styles.closed]}
expanded={props.expanded}
onExpandPressed={props.onExpandPressed}
open={props.open}
contentMin={previewComponents}
contentMax={chipComponents} />
<Portal>
<Animated.View style={styles.bottomFabContainer} layout={LinearTransition}>
{
maxTags >= 0 ?
(
props.open && (
props.expanded ?
<Fragment>
<FAB key="add" style={styles.bottomFab} icon="plus" onPress={() => setEditModalVisible(true)} />
<FAB key="shuffle" style={styles.bottomFab} icon={shuffle ? "shuffle" : "shuffle-disabled"} onPress={() => setShuffle(!shuffle)} />
<FAB key="clipboard" style={styles.bottomFab} icon="clipboard" onPress={copyTags} />
</Fragment>
:
<Fragment>
<FAB key="limit" style={styles.bottomFab} label={maxTags ? quota + "/" + maxTags : "No Limit"} icon="speedometer" onPress={onCycleMaxTags} />
<FAB key="shuffle" style={styles.bottomFab} icon={shuffle ? "shuffle" : "shuffle-disabled"} onPress={() => setShuffle(!shuffle)} />
<FAB key="clipboard" style={styles.bottomFab} disabled={!unculledItems.length} icon="clipboard" onPress={copyTags} />
</Fragment>
)
) : <ActivityIndicator />
}
</Animated.View>
<Snackbar
duration={1500}
visible={copiedSnackbarVisible}
onDismiss={() => setSnackbar({ visible: false })}
action={{
label: 'Dismiss',
onPress: () => setSnackbar({ visible: false }),
}}
>
{snackbarMessage}
</Snackbar>
</Portal>
</Fragment>
);
});
export default SelectionDrawer;