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
127 lines (111 loc) · 4.49 KB
/
controlbar.jsx
File metadata and controls
127 lines (111 loc) · 4.49 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
console.log( "=== simpread focus controlbar load ===" )
import * as modals from 'modals';
import * as se from 'siteeditor';
import * as conf from 'config';
import { storage } from 'storage';
import * as output from 'output';
import * as watch from 'watch';
import {browser,br}from 'browser';
import * as msg from 'message';
import * as highlight from 'highlight';
import Fab from 'fab';
let timer, $root, selector, callback;
const tooltip_options = {
target : "name",
position : "bottom",
delay : 50,
};
class FControl extends React.Component {
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 );
}
});
}
onAction( event, type ) {
console.log( "fab type is =", type )
const action = ( event, type ) => {
switch ( type ) {
case "exit":
ReactDOM.unmountComponentAtNode( getRoot() );
break;
case "top":
$( "html, body" ).animate({ scrollTop: 0 }, "normal" );
break;
case "setting":
modals.Render( ()=>setTimeout( ()=>se.Render(), 500 ));
break;
case "siteeditor":
se.Render();
break;
case "remove":
new Notify().Render( "移动鼠标选择不想显示的内容,只针对本次有效。" );
highlight.Start().done( dom => {
$(dom).remove();
});
break;
case "highlight":
new Notify().Render( "移动鼠标选择高亮区域,以便生成阅读模式,将会在页面刷新后失效。" );
ReactDOM.unmountComponentAtNode( getRoot() );
highlight.Start().done( dom => {
callback && callback( dom );
});
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 ] = [ $( "head title" ).text().trim(), "", $( ".simpread-focus-highlight" ).html().trim() ];
output.Action( type, title, desc, content );
}
};
}
![ "exit", "top" ].includes( type ) ? 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 );
}
componentWillMount() {
if ( storage.current.site.name.startsWith( "metaread::" ) || storage.current.site.name.startsWith( "txtread::" ) ) {
delete conf.focusItems.option;
}
}
componentWillUnmount() {
$(this.refs.target).remove();
$( "body" ).find( selector ).trigger( "click", "okay" );
}
render() {
return (
<sr-rd-crlbar class={ this.props.show ? "" : "controlbar" } style={{ "zIndex": 2147483647 }}>
<Fab ref="target" tooltip={ tooltip_options } waves="md-waves-effect md-waves-circle md-waves-float" items={ conf.focusItems } onAction={ (event, type)=>this.onAction(event, type ) } />
</sr-rd-crlbar>
)
}
}
const fcontrol = new FControl();
/**
* Render
*
* @param {string} class name, e.g. .xxx
* @param {string} class name, e.g. .xxx
* @param {function} callback
*/
function Render( root, finder, cb ) {
callback = cb;
selector = finder;
$root = $(root);
ReactDOM.render( <FControl show={ storage.current.controlbar } />, getRoot() );
}
/**
* Get root html
*
* @return {object} root html
*/
function getRoot() {
return $root[0];
}
export { fcontrol as elem, Render };