forked from Kenshin/simpread
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsiteeditor.jsx
More file actions
157 lines (141 loc) · 6.79 KB
/
siteeditor.jsx
File metadata and controls
157 lines (141 loc) · 6.79 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
console.log( "=== simpread option siteeditor ===" )
import { storage } from 'storage';
import * as watch from 'watch';
import {browser} from 'browser';
import * as msg from 'message';
import Editor from 'editor';
import Button from 'button';
import * as tooltip from 'tooltip';
import * as waves from 'waves';
import * as dia from 'dialog';
const root = "simpread-option-root",
rootjq = `.${root}`;
let site,
state = { name: 0, url: 0, title: 0, desc: 0, include: 0, exclude: 0, avatar:{ name: 0, url: 0 }, paging: { prev:0, next: 0} }; // 0: success -1: faield -2: not empty
/**
* SiteEditor Rect component
*/
class SiteEditor extends React.Component {
close() {
dia.Close();
}
action( type ) {
watch.Verify( ( state, result ) => {
if ( state ) {
console.log( "watch.Lock()", result );
new Notify().Render( "配置文件已更新,刷新当前页面后才能生效。", "刷新", ()=>window.location.reload() );
} else {
type == "save" ? this.save() : this.delete();
}
});
}
delete() {
console.log( "siteeditor click delete button.", storage.current.site )
/*
if ( site.target != "local" ) {
new Notify().Render( 2, `只能删除 <a href='http://ksria.com/simpread/docs/#/FAQ#%E6%97%A0%E6%B3%95%E5%88%A0%E9%99%A4%E5%BD%93%E5%89%8D%E7%AB%99%E7%82%B9' target='_blank'>本地站点</a> ,如需要请使用 站点管理器 删除。` );
return;
}
*/
new Notify().Render( "是否删除当前适配站点?", "删除", () => {
//site.target != "local" ? new Notify().Render( 3, `<a href='http://ksria.com/simpread/docs/#/FAQ#%E6%97%A0%E6%B3%95%E5%88%A0%E9%99%A4%E5%BD%93%E5%89%8D%E7%AB%99%E7%82%B9' target='_blank'>无法删除</a> 当前站点,如不想显示请加入黑名单。` ) :
site.name.startsWith( "tempread::" ) ? new Notify().Render( 2, `当前站点为自动识别,无误删除。` ) :
storage.pr.Deletesite( storage.current.site.target, site.url, result => {
if ( result == -1 ) new Notify().Render( 2, `此站已被删除,请勿重复操作。` );
else {
storage.Writesite(storage.pr.sites, () => {
new Notify().Render( "删除成功,如需生效,请刷新本页。" );
watch.SendMessage( "site", true );
});
}
});
});
}
// save siteeditor focus option
save() {
console.log( "siteeditor click save button.", storage.current.site, site, state )
if ( [ "url", "name", "title", "include" ].findIndex( key => site[key] == "" ) != -1 ) {
new Notify().Render( 3, "【标识、域名、标题、高亮】不能为空。" );
}
else if ( Object.values( state ).findIndex( key => typeof key == "number" && key != 0 ) != -1 ||
( state.avatar.name != 0 || state.avatar.url != 0 ) ||
( state.paging.prev != 0 || state.paging.next != 0 )
) {
new Notify().Render( 3, "请正确填写【标识、域名、标题、高亮】后再提交。" );
} else if (( site.avatar[0].name != "" && site.avatar[1].url == "" ) || ( site.avatar[0].name == "" && site.avatar[1].url != "" )) {
new Notify().Render( 3, "【头像的名称与地址】必须同时设定。" );
} else if (( site.paging[0].prev != "" && site.paging[1].next == "" ) || ( site.paging[0].prev == "" && site.paging[1].next != "" )) {
new Notify().Render( 3, "【前一页与后一页】必须同时设定。" );
} else if ( site.name.startsWith( "tempread::" ) ) {
new Notify().Render( 2, "标识不能包含 tempread:: 请删除。" );
} else if ( site.include.trim() == "" ) {
new Notify().Render( 2, "高亮区域不能为空。" );
} else {
storage.pr.Updatesite( storage.current.site.target, storage.current.url, [ site.url, storage.pr.Cleansite(site) ]);
storage.Writesite( storage.pr.sites, () => {
new Notify().Render( 0, "更新成功,页面刷新后生效!" );
watch.SendMessage( "site", true );
});
}
}
submit() {
const news = { ...site };
delete news.html;
browser.runtime.sendMessage( msg.Add( msg.MESSAGE_ACTION.temp_site, { url: location.href, site: news, uid: storage.user.uid, type: "temp" }));
}
componentDidMount() {
waves.Render({ root: rootjq });
tooltip.Render( rootjq );
}
render() {
site = { ...storage.pr.current.site };
if ( storage.pr.state == "temp" ) {
storage.pr.dom && ( site.include = storage.pr.dom.outerHTML.replace( storage.pr.dom.innerHTML, "" ).replace( /<\/\S+>$/i, "" ));
}
return (
<dia.Dialog>
<dia.Content>
<Editor site={ site } state={ state } />
</dia.Content>
<dia.Footer>
<Button text="删 除" waves="md-waves-effect" color="#fff" backgroundColor="#F44336" onClick={ ()=>this.action( "delete" ) } />
{ storage.pr.state == "temp" ?
<Button text="提交临时阅读模式" waves="md-waves-effect" color="#fff" backgroundColor="#4CAF50" width="100%" onClick={ ()=>this.submit() } /> :
<div style={{ width: "100%" }}></div> }
<Button text="退 出" mode="secondary" waves="md-waves-effect" onClick={ ()=>this.close() } />
<Button text="保 存" waves="md-waves-effect" onClick={ ()=>this.action( "save" ) } />
</dia.Footer>
</dia.Dialog>
)
}
}
/**
* Modals Render
*/
function Render() {
switch ( true ) {
case storage.pr.state == "meta":
new Notify().Render( "当前为 <a href='http://ksria.com/simpread/docs/#/主动适配阅读模式' target='_blank'>主动适配阅读模式</a>,并不能使用设定功能。" )
break;
case storage.pr.state == "txt":
new Notify().Render( "当前为 <a href='http://ksria.com/simpread/docs/#/TXT-阅读器' target='_blank'>TXT 阅读器模式</a>,并不能使用设定功能。" )
break;
default:
!dia.Popup( rootjq ) && dia.Open( <SiteEditor/>, root );
}
}
/**
* Exist
*
* @return {boolean}
*/
function Exist() {
return dia.Popup( rootjq );
}
/**
* Exit
*/
function Exit() {
dia.Close();
}
export{ Render, Exist, Exit }