forked from Kenshin/simpread
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstylesheet.js
More file actions
213 lines (195 loc) · 5.54 KB
/
stylesheet.js
File metadata and controls
213 lines (195 loc) · 5.54 KB
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
console.log( "=== simpread stylesheet load ===" )
/**
* Clone from puread/plugin/stylesheet.js except iconPath
*/
import {browser} from 'browser';
const [ bgcolorstyl, bgcls ] = [ "background-color", ".simpread-focus-root" ];
let origin_read_style = "", html_style_bal = "-1";
/**
* Get chrome extension icon path
* @param {string} icon name
*/
function iconPath( name ) {
return browser.extension.getURL( `assets/images/${name}.png` )
}
/**
* Get background color value for focus mode
*
* @param {string} background-color, e.g. rgba(235, 235, 235, 0.901961)
* @return {string} e.g. 235, 235, 235
*/
function getColor( value ) {
const arr = value ? value.match( /[0-9]+, /ig ) : [];
if ( arr.length > 0 ) {
return arr.join( "" ).replace( /, $/, "" );
} else {
return null;
}
};
/**
* Set focus mode background color for focus mode
*
* @param {string} background color
* @param {number} background opacity
* @return {string} new background color
*/
function backgroundColor( bgcolor, opacity ) {
const color = getColor( bgcolor ),
newval = `rgba(${color}, ${opacity / 100})`;
$( bgcls ).css( bgcolorstyl, newval );
return newval;
}
/**
* Set background opacity for focus mode
*
* @param {string} opacity
* @return {string} new background color or null
*/
function opacity( opacity ) {
const bgcolor = $( bgcls ).css( bgcolorstyl ),
color = getColor( bgcolor ),
newval = `rgba(${color}, ${opacity / 100})`;
if ( color ) {
$( bgcls ).css( bgcolorstyl, newval );
return newval;
} else {
return null;
}
}
/**
* Set read mode font family for read mode
*
* @param {string} font family name e.g. PingFang SC; Microsoft Yahei
*/
function fontFamily( family ) {
$( "sr-read" ).css( "font-family", family == "default" ? "" : family );
}
/**
* Set read mode font size for read mode
*
* @param {string} font size, e.g. 70% 62.5%
*/
function fontSize( value ) {
if ( html_style_bal == "-1" ) {
html_style_bal = $( "html" ).attr( "style" );
html_style_bal == undefined && ( html_style_bal = "" );
}
value ? $( "html" ).attr( "style", `font-size: ${value}!important;${html_style_bal}` ) : $( "html" ).attr( "style", html_style_bal );
}
/**
* Set read mode layout width for read mode
*
* @param {string} layout width
*/
function layout( width ) {
$( "sr-read" ).css( "margin", width ? `20px ${width}` : "" );
}
/**
* Add custom css to <head> for read mode
*
* @param {string} read.custom[type]
* @param {object} read.custom
*/
function custom( type, props ) {
const format = ( name ) => {
return name.replace( /[A-Z]/, name => { return `-${name.toLowerCase()}` } );
},
arr = Object.keys( props ).map( v => {
return props[v] && `${format( v )}: ${ props[v] };`
});
let styles = arr.join( "" );
switch ( type ) {
case "title":
styles = `sr-rd-title {${styles}}`;
break;
case "desc":
styles = `sr-rd-desc {${styles}}`;
break;
case "art":
styles = `sr-rd-content *, sr-rd-content p, sr-rd-content div {${styles}}`;
break;
case "pre":
styles = `sr-rd-content pre {${styles}}`;
break;
case "code":
styles = `sr-rd-content pre code, sr-rd-content pre code * {${styles}}`;
break;
}
const $target = $( "head" ).find( `style#simpread-custom-${type}` );
if ( $target.length == 0 ) {
$( "head" ).append(`<style type="text/css" id="simpread-custom-${type}">${styles}</style>`);
} else {
$target.html( styles );
}
}
/**
* Add css to <head> for read mode
*
* @param {string} read.custom.css
* @param {object} read.custom.css value
*/
function css( type, styles ) {
const $target = $( "head" ).find( `style#simpread-custom-${type}` );
if ( $target.length == 0 ) {
$( "head" ).append(`<style type="text/css" id="simpread-custom-${type}">${styles}</style>`);
} else {
$target.html( styles );
}
}
/**
* Add/Remove current site styles( string ) to head for read mdoe
*
* @param {string} styles
*/
function siteCSS( styles ) {
styles ? $( "head" ).append(`<style type="text/css" id="simpread-site-css">${styles}</style>`) :
$( "#simpread-site-css" ).remove();
}
/**
* Add custom to .preview tag
*
* @param {object} read.custom
* @param {string} theme backgroud color
*/
function preview( styles ) {
Object.keys( styles ).forEach( v => {
v != "css" && custom( v, styles[v] );
});
css( "css", styles["css"] );
}
/**
* Verify custom is exist
*
* @param {string} verify type
* @param {object} read.custom value
*/
function vfyCustom( type, styles ) {
switch( type ) {
case "layout":
case "margin":
case "fontfamily":
case "custom":
return styles.css != "";
case "fontsize":
return styles.title.fontSize != "" ||
styles.desc.fontSize != "" ||
styles.art.fontSize != "" ||
styles.css != "";
case "theme":
return styles.css.search( "simpread-theme-root" ) != -1;
}
}
export {
iconPath as IconPath,
getColor as GetColor,
backgroundColor as BgColor,
opacity as Opacity,
fontFamily as FontFamily,
fontSize as FontSize,
layout as Layout,
siteCSS as SiteCSS,
preview as Preview,
custom as Custom,
css as CSS,
vfyCustom as VerifyCustom,
}