/
custom.js
361 lines (343 loc) · 16.1 KB
/
custom.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
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
console.log( "==== simpread options page: custom load ====" )
import '../assets/css/simpread.css';
import '../assets/css/options_page.css';
import '../assets/css/options_custom.css';
import 'notify_css';
import Velocity from 'velocity';
import Notify from 'notify';
import TextField from 'textfield';
import SelField from 'selectfield';
import Button from 'button';
import * as waves from 'waves';
import * as tt from 'tooltip';
import Switch from 'switch';
import { storage, STORAGE_MODE as mode } from 'storage';
import * as ss from 'stylesheet';
import * as conf from 'config';
import * as ver from 'version';
import * as watch from 'watch';
import th from 'theme';
let cur_custom;
/**
* Entry:
* - storage get data form chrome storage
* - waves.Render()
* - tooltip.Render()
*/
storage.Read( () => {
console.log( "simpread storage get success!", storage.focus, storage.read );
cur_custom = storage.read.custom;
navRender();
propertyRender();
setPreviewStyle();
tt.Render( "body" );
waves.Render({ root: "body" });
$( "body" ).velocity({ opacity: 1 }, { duration: 1000, complete: ()=> {
$( "body" ).removeAttr( "style" );
}});
});
/**
* navigation Render
*/
function navRender() {
const navClick = () => {
location.href = location.origin + "/options/options.html#labs";
};
const button = <Button waves="md-waves-effect md-waves-circle" hoverColor="transparent" icon={ ss.IconPath( "gohome_icon" ) } onClick={ ()=>navClick() } />;
ReactDOM.render( button, $( ".header .nav" )[0] );
}
/**
* Set preview style
*/
function setPreviewStyle() {
th.Change( storage.read.theme );
ss.FontFamily( storage.read.fontfamily );
ss.FontSize( storage.read.fontsize );
ss.Layout( storage.read.layout );
ss.Preview( storage.read.custom );
}
/**
* Property Render
*/
function propertyRender() {
const getThemes = ( names, values ) => {
const arr = [];
return names.map( ( name, idx ) => {
return { value: values[idx], name : name }
});
},
changeTheme = ( value, name ) => {
storage.read.theme = th.names[conf.readLabels.indexOf( name )];
th.Change( storage.read.theme );
},
change = ( type, props, value ) => {
typeof value == "string" && ( value = value.trim());
if ( type == "global" ) {
props == "Layout" && ( value = `${ 100 - value }%` );
ss[props]( value );
storage.read[props.toLowerCase()] = value;
} else if ( props ) {
cur_custom[type][props] = value;
props == "textShadow" && ( cur_custom[type]["textShadow"] = value ? "0 1px rgba(0,0,0,0.3)" : "" );
ss.Custom( type, cur_custom[type] );
} else {
cur_custom[type] = value;
ss.CSS( type, value );
}
},
click = type => {
if ( type == "save" ) {
save();
} else {
new Notify().Render( "snackbar", "是否初始化 自定义样式 的全部数据?", "同意", () => {
Object.keys( cur_custom ).forEach( v => {
v != "css" ? Object.keys( cur_custom[v] ).forEach( item => {
cur_custom[v][item] = "";
}) : cur_custom[v] = "";
});
save( "clear" );
});
}
},
save = state => {
storage.Write( () => {
watch.SendMessage( "option", true );
state == "clear" ?
new Notify().Render( "snackbar", "清除成功,此页面需刷新后才能生效!", "刷新 ", ()=>{
location.href = location.origin + location.pathname;
})
: new Notify().Render( 0, "保存成功,页面刷新后生效!" );
});
};
const doms = <div>
<group className="lab">
<h1>帮助</h1>
<group>
<p>如何自定义样式,详细 <a href="http://ksria.com/simpread/docs/#/自定义样式" target="_blank">请看这里</a><br/>
独乐乐不如众乐乐! <a href="https://github.com/Kenshin/simpread/issues/71" target="_blank">分享你的主题</a></p>
</group>
</group>
<group className="lab">
<h1>全局</h1>
<group>
<SelField waves="md-waves-effect"
floatingtext="主题"
name={ conf.readLabels[ th.names.indexOf( storage.read.theme ) ] }
items={ getThemes( conf.readLabels, th.colors )}
onChange={ (v,n)=>changeTheme(v,n) }
/>
</group>
<group>
<TextField
multi={ false }
floatingtext="字体样式"
placeholder="支持 CSS3 font-family 值"
value = { storage.read.fontfamily }
onChange={ (evt)=>change( "global", "FontFamily", evt.target.value ) }
/>
</group>
<group>
<TextField
multi={ false }
floatingtext="版面布局"
placeholder="仅支持百分比的数值,取值范围 70 ~ 100"
value = { 100 - parseInt( storage.read.layout == "" ? 20 : storage.read.layout ) }
onChange={ (evt)=>change( "global", "Layout", evt.target.value ) }
/>
</group>
</group>
<group className="lab">
<h1>标题与描述</h1>
<group>
<TextField
multi={ false }
floatingtext="标题字体样式"
placeholder="支持 CSS3 font-family 值"
value = { cur_custom.title.fontFamily }
onChange={ (evt)=>change( "title", "fontFamily", evt.target.value ) }
/>
</group>
<group>
<TextField
multi={ false }
floatingtext="标题字体大小"
placeholder="支持 CSS3 font-size 值"
value = { cur_custom.title.fontSize }
onChange={ (evt)=>change( "title", "fontSize", evt.target.value ) }
/>
</group>
<group>
<TextField
multi={ false }
floatingtext="标题颜色"
placeholder="支持 CSS3 color 颜色值"
value = { cur_custom.title.color }
onChange={ (evt)=>change( "title", "color", evt.target.value ) }
/>
</group>
<group>
<TextField
multi={ false }
floatingtext="描述字体样式"
placeholder="支持 CSS3 font-family 值"
value = { cur_custom.desc.fontFamily }
onChange={ (evt)=>change( "desc", "fontFamily", evt.target.value ) }
/>
</group>
<group>
<TextField
multi={ false }
floatingtext="描述字体大小"
placeholder="支持 CSS3 font-size 值"
value = { cur_custom.desc.fontSize }
onChange={ (evt)=>change( "desc", "fontSize", evt.target.value ) }
/>
</group>
<group>
<TextField
multi={ false }
floatingtext="描述颜色"
placeholder="支持 CSS3 color 颜色值"
value = { cur_custom.desc.color }
onChange={ (evt)=>change( "desc", "color", evt.target.value ) }
/>
</group>
</group>
<group className="lab">
<h1>正文</h1>
<group>
<TextField
multi={ false }
floatingtext="字体样式"
placeholder="支持 CSS3 font-family 值"
value = { cur_custom.art.fontFamily }
onChange={ (evt)=>change( "art", "fontFamily", evt.target.value ) }
/>
</group>
<group>
<TextField
multi={ false }
floatingtext="字体大小"
placeholder="支持 CSS3 font-size 值"
value = { cur_custom.art.fontSize }
onChange={ (evt)=>change( "art", "fontSize", evt.target.value ) }
/>
</group>
<group>
<TextField
multi={ false }
floatingtext="字体颜色"
placeholder="支持 CSS3 color 颜色值"
value = { cur_custom.art.color }
onChange={ (evt)=>change( "art", "color", evt.target.value ) }
/>
</group>
<group>
<TextField
multi={ false }
floatingtext="字重"
placeholder="支持 CSS3 font-weight 值"
value = { cur_custom.art.fontWeight }
onChange={ (evt)=>change( "art", "fontWeight", evt.target.value ) }
/>
</group>
<group>
<TextField
multi={ false }
floatingtext="单词间距"
placeholder="支持 CSS3 word-spacing 值"
value = { cur_custom.art.wordSpacing }
onChange={ (evt)=>change( "art", "wordSpacing", evt.target.value ) }
/>
</group>
<group>
<TextField
multi={ false }
floatingtext="字间距"
placeholder="支持 CSS3 letter-spacing 值"
value = { cur_custom.art.letterSpacing }
onChange={ (evt)=>change( "art", "letterSpacing", evt.target.value ) }
/>
</group>
<group>
<TextField
multi={ false }
floatingtext="行间距"
placeholder="支持 CSS3 line-height 值"
value = { cur_custom.art.lineHeight }
onChange={ (evt)=>change( "art", "lineHeight", evt.target.value ) }
/>
</group>
<group>
<TextField
multi={ false }
floatingtext="首行缩进"
placeholder="支持 CSS3 text-indent 值"
value = { cur_custom.art.textIndent }
onChange={ (evt)=>change( "art", "textIndent", evt.target.value ) }
/>
</group>
</group>
<group className="lab">
<h1>代码段</h1>
<group>
<Switch width="100%" checked={ false }
thumbedColor="#3F51B5" trackedColor="#7986CB" waves="md-waves-effect"
label="是否启用阴影"
checked = { cur_custom.pre.textShadow ? true : false }
onChange={ (s)=>change( "pre", "textShadow", s ) }
/>
</group>
<group>
<TextField
multi={ false }
floatingtext="字体样式"
placeholder="支持 CSS3 font-family 值"
value = { cur_custom.code.fontFamily }
onChange={ (evt)=>change( "code", "fontFamily", evt.target.value ) }
/>
</group>
<group>
<TextField
multi={ false }
floatingtext="字体大小"
placeholder="支持 CSS3 font-size 值"
value = { cur_custom.code.fontSize }
onChange={ (evt)=>change( "code", "fontSize", evt.target.value ) }
/>
</group>
</group>
<group className="lab">
<h1>自定义 CSS</h1>
<group>
<TextField
multi ={ true } rows ={ 10 }
value = { cur_custom.css }
onChange={ (evt)=>change( "css", "", evt.target.value ) }
/>
</group>
</group>
<group>
<group>
<Button type="raised" text="保存" width="100%"
style={{ "margin": "0" }}
icon={ ss.IconPath( "save_icon" ) }
color="#fff" backgroundColor="#3f51b5"
waves="md-waves-effect md-waves-button"
onClick={ ()=>click( "save" ) }
/>
</group>
<group>
<Button type="raised" text="清空并初始化" width="100%"
style={{ "margin": "0" }}
icon={ ss.IconPath( "clear_icon" ) }
tooltip={{ text: "不包含:主题、字体类型、字体大小、版面布局等;" }}
color="#fff" backgroundColor="#FF5252"
waves="md-waves-effect md-waves-button"
onClick={ ()=>click( "clear" ) }
/>
</group>
</group>
</div>;
ReactDOM.render( doms, $( ".custom .property" )[0] );
}