-
Notifications
You must be signed in to change notification settings - Fork 480
/
textInput.jsx
364 lines (344 loc) · 9.84 KB
/
textInput.jsx
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import $ from 'jquery';
import PropTypes from 'prop-types';
import React from 'react';
import PropertyRow from './PropertyRow';
import BooleanPropertyRow from './BooleanPropertyRow';
import ColorPickerPropertyRow from './ColorPickerPropertyRow';
import ZOrderRow from './ZOrderRow';
import EventHeaderRow from './EventHeaderRow';
import EventRow from './EventRow';
import EnumPropertyRow from './EnumPropertyRow';
import FontFamilyPropertyRow from './FontFamilyPropertyRow';
import BorderProperties from './BorderProperties';
import * as elementUtils from './elementUtils';
import designMode from '../designMode';
import {
defaultFontSizeStyle,
fontFamilyStyles,
themeOptions,
CLASSIC_THEME_INDEX
} from '../constants';
import themeColor from '../themeColor';
import elementLibrary from './library';
import experiments from '../../util/experiments';
class TextInputProperties extends React.Component {
static propTypes = {
element: PropTypes.instanceOf(HTMLElement).isRequired,
handleChange: PropTypes.func.isRequired,
onDepthChange: PropTypes.func.isRequired
};
render() {
const element = this.props.element;
return (
<div id="propertyRowContainer">
<PropertyRow
desc={'id'}
initialValue={elementUtils.getId(element)}
handleChange={this.props.handleChange.bind(this, 'id')}
isIdRow
/>
<PropertyRow
desc={'placeholder'}
initialValue={element.getAttribute('placeholder') || ''}
handleChange={this.props.handleChange.bind(this, 'placeholder')}
/>
<PropertyRow
desc={'width (px)'}
isNumber
initialValue={parseInt(element.style.width, 10)}
handleChange={this.props.handleChange.bind(this, 'style-width')}
/>
<PropertyRow
desc={'height (px)'}
isNumber
initialValue={parseInt(element.style.height, 10)}
handleChange={this.props.handleChange.bind(this, 'style-height')}
/>
<PropertyRow
desc={'x position (px)'}
isNumber
initialValue={parseInt(element.style.left, 10)}
handleChange={this.props.handleChange.bind(this, 'left')}
/>
<PropertyRow
desc={'y position (px)'}
isNumber
initialValue={parseInt(element.style.top, 10)}
handleChange={this.props.handleChange.bind(this, 'top')}
/>
<ColorPickerPropertyRow
desc={'text color'}
initialValue={elementUtils.rgb2hex(element.style.color)}
handleChange={this.props.handleChange.bind(this, 'textColor')}
/>
<ColorPickerPropertyRow
desc={'background color'}
initialValue={elementUtils.rgb2hex(element.style.backgroundColor)}
handleChange={this.props.handleChange.bind(this, 'backgroundColor')}
/>
<FontFamilyPropertyRow
initialValue={designMode.fontFamilyOptionFromStyle(
element.style.fontFamily
)}
handleChange={this.props.handleChange.bind(this, 'fontFamily')}
/>
<PropertyRow
desc={'font size (px)'}
isNumber
initialValue={parseInt(element.style.fontSize, 10)}
handleChange={this.props.handleChange.bind(this, 'fontSize')}
/>
<EnumPropertyRow
desc={'text alignment'}
initialValue={element.style.textAlign || 'left'}
options={['left', 'right', 'center', 'justify']}
handleChange={this.props.handleChange.bind(this, 'textAlign')}
/>
<BorderProperties
element={element}
handleBorderWidthChange={this.props.handleChange.bind(
this,
'borderWidth'
)}
handleBorderColorChange={this.props.handleChange.bind(
this,
'borderColor'
)}
handleBorderRadiusChange={this.props.handleChange.bind(
this,
'borderRadius'
)}
/>
<BooleanPropertyRow
desc={'hidden'}
initialValue={$(element).hasClass('design-mode-hidden')}
handleChange={this.props.handleChange.bind(this, 'hidden')}
/>
<ZOrderRow
element={this.props.element}
onDepthChange={this.props.onDepthChange}
/>
</div>
);
}
}
class TextInputEvents extends React.Component {
static propTypes = {
element: PropTypes.instanceOf(HTMLElement).isRequired,
handleChange: PropTypes.func.isRequired,
onInsertEvent: PropTypes.func.isRequired
};
getChangeEventCode() {
const id = elementUtils.getId(this.props.element);
const code =
'onEvent("' +
id +
'", "change", function(event) {\n' +
' console.log("' +
id +
' entered text: " + getText("' +
id +
'"));\n' +
'});\n';
return code;
}
insertChange = () => {
this.props.onInsertEvent(this.getChangeEventCode());
};
getInputEventCode() {
const id = elementUtils.getId(this.props.element);
const code =
'onEvent("' +
id +
'", "input", function(event) {\n' +
' console.log("' +
id +
' current text: " + getText("' +
id +
'"));\n' +
'});\n';
return code;
}
insertInput = () => {
this.props.onInsertEvent(this.getInputEventCode());
};
render() {
const element = this.props.element;
const changeName = 'Change';
const changeDesc =
'Triggered when the text input loses focus if the text has changed.';
const inputName = 'Input';
const inputDesc =
'Triggered immediately every time the text input contents change.';
return (
<div id="eventRowContainer">
<PropertyRow
desc={'id'}
initialValue={elementUtils.getId(element)}
handleChange={this.props.handleChange.bind(this, 'id')}
isIdRow
/>
<EventHeaderRow />
<EventRow
name={changeName}
desc={changeDesc}
handleInsert={this.insertChange}
/>
<EventRow
name={inputName}
desc={inputDesc}
handleInsert={this.insertInput}
/>
</div>
);
}
}
export default {
PropertyTab: TextInputProperties,
EventTab: TextInputEvents,
themeValues: {
backgroundColor: {
type: 'color',
...themeColor.textInputBackground
},
borderRadius: {
default: 4,
orange: 0,
citrus: 4,
ketchupAndMustard: 5,
lemonade: 4,
forest: 4,
watermelon: 0,
area51: 10,
polar: 4,
glowInTheDark: 0,
bubblegum: 4,
millennial: 4,
robot: 0,
classic: 0
},
borderWidth: {
default: 1,
orange: 1,
citrus: 1,
ketchupAndMustard: 1,
lemonade: 1,
forest: 1,
watermelon: 2,
area51: 1,
polar: 1,
glowInTheDark: 1,
bubblegum: 1,
millennial: 2,
robot: 1,
classic: 1
},
borderColor: {
type: 'color',
...themeColor.textInputBorder
},
textColor: {
type: 'color',
...themeColor.textInput
},
fontFamily: {
default: 'Arial',
orange: 'Arial',
citrus: 'Palatino',
ketchupAndMustard: 'Tahoma',
lemonade: 'Arial',
forest: 'Arial',
watermelon: 'Georgia',
area51: 'Trebuchet',
polar: 'Verdana',
glowInTheDark: 'Tahoma',
bubblegum: 'Trebuchet',
millennial: 'Arial',
robot: 'Tahoma',
classic: 'Arial'
},
fontSize: {
default: 13,
orange: 13,
citrus: 13,
ketchupAndMustard: 13,
lemonade: 13,
forest: 13,
watermelon: 13,
area51: 13,
polar: 13,
glowInTheDark: 13,
bubblegum: 13,
millennial: 13,
robot: 13,
classic: 14
}
},
create: function() {
const element = document.createElement('input');
element.style.margin = '0px';
element.style.width = '200px';
element.style.height = '30px';
if (experiments.isEnabled('applabThemes')) {
element.style.borderStyle = 'solid';
elementLibrary.applyCurrentTheme(element, designMode.activeScreen());
} else {
element.style.fontFamily = fontFamilyStyles[0];
element.style.fontSize = defaultFontSizeStyle;
element.style.color = themeColor.textInput.classic;
element.style.backgroundColor = '';
elementUtils.setDefaultBorderStyles(element, {
forceDefaults: true,
textInput: true
});
}
return element;
},
onDeserialize: function(element) {
// Set border styles for older projects that didn't set them on create:
elementUtils.setDefaultBorderStyles(element, {textInput: true});
// Set the font family for older projects that didn't set it on create:
elementUtils.setDefaultFontFamilyStyle(element);
if (experiments.isEnabled('applabThemes')) {
// Set the background color for older projects that didn't set it on create:
if (element.style.backgroundColor === '') {
element.style.backgroundColor = this.themeValues.backgroundColor[
themeOptions[CLASSIC_THEME_INDEX]
];
}
}
$(element).on('mousedown', function(e) {
if (!Applab.isRunning()) {
// Disable clicking into text input unless running
e.preventDefault();
}
});
// swallow keydown unless we're running
$(element).on('keydown', function(e) {
if (!Applab.isRunning()) {
e.preventDefault();
}
});
},
onPropertyChange: function(element, name, value) {
switch (name) {
case 'value':
element.value = value;
break;
case 'text':
element.value = value;
break;
default:
return false;
}
return true;
},
readProperty: function(element, name) {
switch (name) {
case 'value':
return element.value;
default:
throw `unknown property name ${name}`;
}
}
};