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
|
import * as React from 'react';
import "./MetadataEntryMenu.scss";
import { observer } from 'mobx-react';
import { observable, action, runInAction, trace, computed, IReactionDisposer, reaction } from 'mobx';
import { KeyValueBox } from './nodes/KeyValueBox';
import { Doc, Field, DocListCastAsync } from '../../fields/Doc';
import * as Autosuggest from 'react-autosuggest';
import { undoBatch } from '../util/UndoManager';
import { emptyFunction, emptyPath } from '../../Utils';
export type DocLike = Doc | Doc[] | Promise<Doc> | Promise<Doc[]>;
export interface MetadataEntryProps {
docs: DocLike | (() => DocLike);
onError?: () => boolean;
suggestWithFunction?: boolean;
}
@observer
export class MetadataEntryMenu extends React.Component<MetadataEntryProps>{
@observable private _currentKey: string = "";
@observable private _currentValue: string = "";
private _addChildren: boolean = false;
@observable _allSuggestions: string[] = [];
_suggestionDispser: IReactionDisposer | undefined;
private userModified = false;
private autosuggestRef = React.createRef<Autosuggest>();
@action
onKeyChange = (e: React.ChangeEvent, { newValue }: { newValue: string }) => {
this._currentKey = newValue;
if (!this.userModified) {
this.previewValue();
}
}
previewValue = async () => {
let field: Field | undefined | null = null;
let onProto: boolean = false;
let value: string | undefined = undefined;
let docs = this.props.docs;
if (typeof docs === "function") {
if (this.props.suggestWithFunction) {
docs = docs();
} else {
return;
}
}
docs = await docs;
if (docs instanceof Doc) {
await docs[this._currentKey];
value = Field.toKeyValueString(docs, this._currentKey);
} else {
for (const doc of docs) {
const v = await doc[this._currentKey];
onProto = onProto || !Object.keys(doc).includes(this._currentKey);
if (field === null) {
field = v;
} else if (v !== field) {
value = "multiple values";
}
}
}
if (value === undefined) {
if (field !== null && field !== undefined) {
value = (onProto ? "" : "= ") + Field.toScriptString(field);
} else {
value = "";
}
}
const s = value;
runInAction(() => this._currentValue = s);
}
@action
onValueChange = (e: React.ChangeEvent<HTMLInputElement>) => {
this._currentValue = e.target.value;
this.userModified = e.target.value.trim() !== "";
}
@undoBatch
@action
onValueKeyDown = async (e: React.KeyboardEvent) => {
if (e.key === "Enter") {
e.stopPropagation();
const script = KeyValueBox.CompileKVPScript(this._currentValue);
if (!script) return;
let doc = this.props.docs;
if (typeof doc === "function") {
doc = doc();
}
doc = await doc;
let success: boolean;
if (doc instanceof Doc) {
success = KeyValueBox.ApplyKVPScript(doc, this._currentKey, script);
} else {
let childSuccess = true;
if (this._addChildren) {
for (const document of doc) {
const collectionChildren = await DocListCastAsync(document.data);
if (collectionChildren) {
childSuccess = collectionChildren.every(c => KeyValueBox.ApplyKVPScript(c, this._currentKey, script));
}
}
}
success = doc.every(d => KeyValueBox.ApplyKVPScript(d, this._currentKey, script)) && childSuccess;
}
if (!success) {
if (this.props.onError) {
if (this.props.onError()) {
this.clearInputs();
}
} else {
this.clearInputs();
}
} else {
this.clearInputs();
}
}
}
@action
clearInputs = () => {
this._currentKey = "";
this._currentValue = "";
this.userModified = false;
if (this.autosuggestRef.current) {
const input: HTMLInputElement = (this.autosuggestRef.current as any).input;
input && input.focus();
}
}
getKeySuggestions = async (value: string): Promise<string[]> => {
value = value.toLowerCase();
let docs = this.props.docs;
if (typeof docs === "function") {
if (this.props.suggestWithFunction) {
docs = docs();
} else {
return [];
}
}
docs = await docs;
if (docs instanceof Doc) {
return Object.keys(docs).filter(key => key.toLowerCase().startsWith(value));
} else {
const keys = new Set<string>();
docs.forEach(doc => Doc.allKeys(doc).forEach(key => keys.add(key)));
return Array.from(keys).filter(key => key.toLowerCase().startsWith(value));
}
}
getSuggestionValue = (suggestion: string) => suggestion;
renderSuggestion = (suggestion: string) => {
return (null);
}
componentDidMount() {
this._suggestionDispser = reaction(() => this._currentKey,
() => this.getKeySuggestions(this._currentKey).then(action((s: string[]) => this._allSuggestions = s)),
{ fireImmediately: true });
}
componentWillUnmount() {
this._suggestionDispser && this._suggestionDispser();
}
onClick = (e: React.ChangeEvent<HTMLInputElement>) => {
this._addChildren = !this._addChildren;
}
private get considerChildOptions() {
let docSource = this.props.docs;
if (typeof docSource === "function") {
docSource = docSource();
}
docSource = docSource as Doc[] | Doc;
if (docSource instanceof Doc) {
if (docSource._viewType === undefined) {
return (null);
}
} else if (Array.isArray(docSource)) {
if (!docSource.every(doc => doc._viewType !== undefined)) {
return null;
}
}
return (
<div style={{ display: "flex" }}>
Children:
<input type="checkbox" onChange={this.onClick} ></input>
</div>
);
}
_ref = React.createRef<HTMLInputElement>();
render() {
return (<div className="metadataEntry-outerDiv" id="metadataEntry-outer" onPointerDown={e => e.stopPropagation()}>
<div className="metadataEntry-inputArea">
Key:
<div className="metadataEntry-autoSuggester" onClick={e => this.autosuggestRef.current!.input?.focus()} >
<Autosuggest inputProps={{ value: this._currentKey, onChange: this.onKeyChange }}
getSuggestionValue={this.getSuggestionValue}
suggestions={emptyPath}
alwaysRenderSuggestions={false}
renderSuggestion={this.renderSuggestion}
onSuggestionsFetchRequested={emptyFunction}
onSuggestionsClearRequested={emptyFunction}
ref={this.autosuggestRef} />
</div>
Value:
<input className="metadataEntry-input" ref={this._ref} value={this._currentValue} onClick={e => this._ref.current!.focus()} onChange={this.onValueChange} onKeyDown={this.onValueKeyDown} />
{this.considerChildOptions}
</div>
<div className="metadataEntry-keys" >
<ul>
{this._allSuggestions.slice().sort().map(s => <li key={s} onClick={action(() => { this._currentKey = s; this.previewValue(); })} >{s}</li>)}
</ul>
</div>
</div>
);
}
}
|