forked from Kenshin/simpread
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfocus.js
More file actions
218 lines (194 loc) · 7.05 KB
/
focus.js
File metadata and controls
218 lines (194 loc) · 7.05 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
214
215
216
217
218
console.log( "=== simpread focus load ===" );
var storage = require( "storage" ).storage,
util = require( "util" ),
highlight= require( "highlight" ),
fcontrol = require( "controlbar" ),
tooltip = require( "tooltip" ),
waves = require( "waves" ),
focus = ( function () {
var $parent,
tag,
focuscls = "simpread-focus-highlight",
focusstyle = "z-index: 2147483646; overflow: visible; position: relative;",
maskcls = "simpread-focus-mask",
maskstyle = "z-index: auto; opacity: 1; overflow: visible; transform: none; animation: none; position: relative;",
bgcls = "simpread-focus-root",
bgtmpl = "<div class=" + bgcls + "></div>",
ctrlbar = "sr-controlbar-bg",
ctrlbarbg = "<div class=" + ctrlbar + "></div>",
ctrlbarjq = "." + ctrlbar,
bgclsjq = "." + bgcls;
function Focus() { this.$target = null; }
/**
* Add focus mode
*
* @param {jquery} jquery object
* @param {string} background color style
*/
Focus.prototype.Render = function( $target, bgcolor ) {
console.log( "=== simpread focus add ===" );
this.$target = $target;
// set include style
includeStyle( $target, focusstyle, focuscls, "add" );
// set exclude style
excludeStyle( $target, "delete" );
// add simpread-focus-mask
$parent = $target.parent();
tag = $parent[0].tagName;
while ( tag.toLowerCase() != "body" ) {
includeStyle( $parent, maskstyle, maskcls, "add" );
$parent = $parent.parent();
tag = $parent[0].tagName;
}
// add background
$( "body" ).append( bgtmpl );
$( "html" ).append( ctrlbarbg );
// add background color
$( bgclsjq )
.css({ "background-color" : bgcolor })
.sreffect({ opacity: 1 });
// add control bar
fcontrol.Render( ctrlbarjq, bgclsjq, dom => {
storage.pr.dom = dom;
Focus.prototype.Render( $(dom), storage.current.bgcolor );
});
// add tooltip and waves
tooltip.Render( bgclsjq );
waves.Render({ root: bgclsjq });
storage.Statistics( "focus" );
// click mask remove it
$( bgclsjq ).on( "click", function( event, data ) {
if ( ( event.target.tagName.toLowerCase() == "i" && event.target.id !="exit" ) ||
$( event.currentTarget ).attr("class") != bgcls ||
( !storage.current.mask && !data )) return;
$( bgclsjq ).sreffect({ opacity: 0 }, {
complete: ()=> {
includeStyle( $target, focusstyle, focuscls, "delete" );
excludeStyle( $target, "add" );
tooltip.Exit( bgclsjq );
$( ctrlbarjq ).remove();
$( bgclsjq ).remove();
$( bgclsjq ).off( "click" );
}
});
// remove simpread-focus-mask style
$parent = $target.parent();
tag = $parent[0].tagName;
while ( tag && tag.toLowerCase() != "body" ) {
includeStyle( $parent, maskstyle, maskcls, "delete" );
$parent = $parent.parent();
tag = $parent[0].tagName;
}
console.log( "=== simpread focus remove ===" );
});
}
/**
* Get focus
*
* @param {jquery} focus jquery object
* @param {string} storage.current.site.include
* @return {jquery} focus jquery object or undefined
*/
Focus.prototype.GetFocus = function( $focus, include ) {
var dtd = $.Deferred(),
sel, range, node, tag;
if ( storage.current.highlight && $focus.length == 0 ) {
new Notify().Render( "已启动手动聚焦模式,请移动鼠标进行选择,支持 ESC 退出。" );
highlight.Start().done( result => {
$focus = $( result );
dtd.resolve( $focus );
});
} else {
while ( $focus.length == 0 ) {
if ( $( "body" ).find( "article" ).length > 0 ) {
$focus = $( "body" ).find( "article" );
}
else {
try {
sel = window.getSelection();
range = sel.getRangeAt( sel.rangeCount - 1 );
node = range.startContainer.nodeName;
if ( node.toLowerCase() === "body" ) throw( "selection area is body tag." );
$focus = $( range.startContainer.parentNode );
} catch ( error ) {
console.log( sel, range, node )
console.error( error )
return dtd.reject();
}
}
}
return dtd.resolve( $focus );
}
return dtd;
}
/**
* Exist
*
* @param {boolean} when true, call fcontrol.Click()
* @return {boolen} true: exist; false: not exist
*/
Focus.prototype.Exist = function( action ) {
if ( $( "body" ).find( "." + focuscls ).length > 0 ) {
if (action) fcontrol.elem.onAction( undefined, "setting" );
return true;
} else {
return false;
}
}
/**
* Exit
*/
Focus.prototype.Exit = function() {
$( bgclsjq ).trigger( "click", "okay" );
}
return new Focus();
})();
/**
* Set include style
*
* @param {jquery} jquery object
* @param {string} set style string
* @param {string} set class string
* @param {string} include 'add' and 'delete'
*/
function includeStyle( $target, style, cls, type ) {
$target.each(function(index){
var bakstyle;
var selector = $(this);
if ( type === "add" ) {
bakstyle = selector.attr( "style" ) == undefined ? "" : selector.attr( "style" );
selector.attr( "style", bakstyle + style ).addClass( cls );
} else if ( type === "delete" ) {
bakstyle = selector.attr( "style" );
bakstyle = bakstyle.replace( style, "" );
selector.attr( "style", bakstyle ).removeClass( cls );
}
});
}
/**
* Set exclude style
*
* @param {jquery} jquery object
* @param {string} include: 'add' 'delete'
*/
function excludeStyle( $target, type ) {
const tags = storage.pr.Exclude( $target );
if ( type == "delete" ) $target.find( tags ).hide();
else if ( type == "add" ) $target.find( tags ).show();
}
/**
* Fix $focus get bad tag, get good tag and return
* Good tag include: div, article
*
* @param {jquery} jquery object
* @return {jquery} jquery object
*/
function fixFocus( $focus ) {
var tag = $focus[0].tagName.toLowerCase();
while ( [ "p", "span", "strong", "ul", "li", "code", "pre", "pre" ].includes( tag )) {
$focus = $focus.parent();
tag = $focus[0].tagName.toLowerCase();
}
return $focus;
}
exports.focus = focus;