-
Notifications
You must be signed in to change notification settings - Fork 0
/
TagEditorView.js
104 lines (92 loc) · 2.82 KB
/
TagEditorView.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
import React from 'react';
import { View, StyleSheet } from 'react-native';
// Local Imports
import TextModal from './TextModal';
import TagContainer from './TagContainer';
'use strict';
export default class TagEditorView extends React.Component {
constructor(props) {
super(props);
this.state = {
modalKey: 0
}
}
stringToItems = (text) => {
let trimText = text.trim().replace(/\#\s\n\r/g, '');
if (!trimText) {
return new Array();
}
return trimText.split(/[\#\s\n\r]+/).filter((x) => x);
}
intersectItems = (items, compareItems) => {
return items.reduce((acc, item) => {
let lowerItem = item.toLowerCase();
if (compareItems.find(x => { return lowerItem === x.text.toLowerCase() })) {
acc.intersection.push(item);
} else {
acc.unique.push(item);
}
return acc;
}, { intersection: new Array(), unique: new Array() });
}
onHashtagsAdded = (text) => {
let items = this.stringToItems(text);
let uniqueItems = items.reduce((acc, item) => {
let lowerItem = item.toLowerCase();
if (!acc.find(x => { return lowerItem === x.toLowerCase() })) {
acc.push(item);
}
return acc;
}, new Array());
let intersectionSet = this.props.items ? this.props.items : new Array();
let intersection = this.intersectItems(uniqueItems, intersectionSet);
console.log("Adding " + intersection.unique.map(x => "#" + x).join(' '));
this.props.onAddItems(intersection.unique);
return true;
}
onFilterText = (text) => {
let items = this.stringToItems(text);
let intersectionSet = this.props.items ? this.props.items : new Array();
let intersection = this.intersectItems(items, intersectionSet);
switch (intersection.intersection.length) {
case 0:
if (intersection.unique.length) {
return undefined;
} else {
return "Enter hashtag";
}
case 1:
return "Hashtag " + intersection.intersection + " already in the list";
default:
return "Hashtags already in the list: " + intersection.intersection.join(', ');
}
}
onReorderItems = (items) => {
// We're going to trust that the items are all there. Right?
this.setState({ items });
};
render() {
return (
<View>
<TagContainer
{...this.props}
style={{ width: '100%', height: '100%' }} />
<TextModal
visible={this.props.editModalVisible}
onRequestClose={() => { this.setState({ modalKey: this.state.modalKey + 1 }); this.props.onRequestModalClose() }}
onTextAccepted={this.onHashtagsAdded}
onFilterText={this.onFilterText} />
</View>
);
}
};
const styles = StyleSheet.create({
fab: {
margin: 16,
},
fabKAV: {
position: 'absolute',
right: 0,
bottom: 0,
}
});