forked from Kenshin/simpread
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrolbar.jsx
More file actions
170 lines (151 loc) · 7.39 KB
/
controlbar.jsx
File metadata and controls
170 lines (151 loc) · 7.39 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
console.log( "=== simpread read controlbar load ===" )
import * as ss from 'stylesheet';
import {browser,br}from 'browser';
import * as msg from 'message';
import th from 'theme';
import * as conf from 'config';
import * as output from 'output';
import * as watch from 'watch';
import * as kbd from 'keyboard';
import { storage } from 'storage';
import ReadOpt from 'readopt';
import Actionbar from 'actionbar';
import Pluginbar from 'pluginbar';
import Sitebar from 'sitebar';
import Fab from 'fab';
import Fap from 'fap'
import * as ttips from 'tooltip';
let notify;
const tooltip_options = {
target : "name",
position : "bottom",
delay : 50,
};
/**
* Read controlbar
*
* @class
*/
export default class ReadCtlbar extends React.Component {
static propTypes = {
onAction: React.PropTypes.func,
}
verify( type ) {
if ( ss.VerifyCustom( type, storage.current.custom ) ) {
!notify && ( notify = new Notify().Render({ state: "holdon", content: '由于已使用自定义样式,设定 <b style="color: #fff;">有可能无效</b>,详细说明 <a href="https://github.com/Kenshin/simpread/wiki/自定义样式" target="_blank">请看这里</a>', callback:()=>notify=undefined }));
}
}
componentDidMount() {
browser.runtime.onMessage.addListener( ( request, sender, sendResponse ) => {
if ( request.type == msg.MESSAGE_ACTION.export ) {
console.log( "controlbar runtime Listener", request );
new Notify().Render( "已重新授权成功!" );
br.isFirefox() ? new Notify().Render( "请刷新本页才能生效。" ) : this.onAction( undefined, request.value.type );
}
});
kbd.Listen( combo => {
this.onAction( undefined, combo )
});
}
onAction( event, type ) {
console.log( "fab type is =", type )
this.verify( type.split( "_" )[0] );
const action = ( event, type ) => {
this.props.multi &&
[ "markdown", "dropbox", "yinxiang","evernote", "onenote", "gdrive" ].includes( type ) &&
new Notify().Render( "当前为论坛类页面,不建议使用导出服务,有可能出现未知错误。" );
switch ( true ) {
case [ "exit", "setting", "siteeditor", "remove", "highlight" ].includes( type ):
this.props.onAction( type );
break;
case type.indexOf( "_" ) > 0 && ( type.startsWith( "fontfamily" ) || type.startsWith( "fontsize" ) || type.startsWith( "layout" )):
const [ key, value ] = [ type.split( "_" )[0], type.split( "_" )[1] ];
Object.keys( ss ).forEach( (name)=>name.toLowerCase() == key && ss[name]( value ));
this.props.onAction && this.props.onAction( key, value );
break;
case type.indexOf( "_" ) > 0 && type.startsWith( "theme" ):
let i = th.names.indexOf( th.theme );
i = type.endsWith( "prev" ) ? --i : ++i;
i >= th.names.length && ( i = 0 );
i < 0 && ( i = th.names.length - 1 );
th.Change( th.names[i] );
this.props.onAction && this.props.onAction( type.split( "_" )[0], th.theme );
break;
case type.startsWith( "dyslexia" ):
output.Action( type, $( "sr-rd-title" ).text(), "", $( "sr-rd-content" ).text() );
break;
case type == "tempread":
if ( storage.pr.state != "temp" ) {
new Notify().Render( "此功能只限临时阅读模式时使用。" );
return;
}
// hack code
const news = { ...storage.pr.current.site };
storage.pr.dom && ( news.include = storage.pr.dom.outerHTML.replace( storage.pr.dom.innerHTML, "" ).replace( /<\/\S+>$/i, "" ));
delete news.html;
browser.runtime.sendMessage( msg.Add( msg.MESSAGE_ACTION.temp_site, { url: location.href, site: news, uid: storage.user.uid, type: "temp" }));
break;
default:
if ( type.indexOf( "_" ) > 0 && type.startsWith( "share" ) ||
[ "save", "markdown", "png", "epub", "pdf", "kindle", "temp", "html", "dropbox", "pocket", "instapaper", "linnk", "yinxiang","evernote", "onenote", "gdrive" ].includes( type )) {
const [ title, desc, content ] = [ $( "sr-rd-title" ).text().trim(), $( "sr-rd-desc" ).text().trim(), $( "sr-rd-content" ).html().trim() ];
output.Action( type, title, desc, content );
}
}
};
type != "exit" ? watch.Verify( ( state, result ) => {
if ( state ) {
console.log( "watch.Lock()", result );
new Notify().Render( "配置文件已更新,刷新当前页面后才能生效。", "刷新", ()=>location.href = location.href );
} else action( event, type );
}) : action( event, type );
}
onChange( type, custom ) {
const [ key, value ] = [ type.split( "_" )[0], type.split( "_" )[1] ];
this.props.onAction && this.props.onAction( key, value, custom );
this.verify( key );
}
onPop( type ) {
type == "open" ? ttips.Render( ".simpread-read-root", "panel" ) : ttips.Exit( ".simpread-read-root", "panel" );
}
componentWillMount() {
if ( storage.current.fap ) {
delete conf.readItems.exit;
delete conf.readItems.option.items.setting;
}
if ( this.props.type.startsWith( "txtread::" ) && this.props.type.endsWith( "::local" )) {
delete conf.readItems.download;
delete conf.readItems.readlater;
delete conf.readItems.send;
delete conf.readItems.share;
delete conf.readItems.option;
}
if ( this.props.type.startsWith( "metaread::" ) || this.props.type.startsWith( "txtread::" ) ) {
delete conf.readItems.option;
}
// hack code
!/chrome/ig.test( navigator.userAgent ) && ( delete conf.readItems.dyslexia );
}
constructor( props ) {
super( props );
}
render() {
const Controlbar = storage.current.fap ?
<Fap items={ [ "样式", "动作", "站点", "插件" ] } autoHide={ false }
waves="md-waves-effect md-waves-circle md-waves-float"
onOpen={ ()=> this.onPop( "open" ) } onClose={ ()=> this.onPop( "close" ) }
onAction={ (event, type)=>this.onAction(event, type ) }>
<ReadOpt option={ storage.current } onChange={ (t,c)=>this.onChange(t,c)}/>
<Actionbar items={ conf.readItems } onAction={ (type)=>this.onAction(undefined, type ) }/>
<Sitebar />
<Pluginbar />
</Fap>
:
<Fab items={ conf.readItems } tooltip={ tooltip_options } waves="md-waves-effect md-waves-circle md-waves-float" onAction={ (event, type)=>this.onAction(event, type ) } />
return (
<sr-rd-crlbar class={ this.props.show ? "" : "controlbar" } style={{ "zIndex": "2" }}>
{ Controlbar }
</sr-rd-crlbar>
)
}
}