@@ -22,38 +22,54 @@ THE SOFTWARE.
2222// Thanks to all who've helped debug and discuss, especially the Mac users, nebech.
2323
2424( function HBKeyboard ( ) {
25- var docCookies = { //from developer.mozilla.org/en-US/docs/Web/API/document.cookie
25+ // see https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
26+ const docCookies = {
27+ /**
28+ * @param {string } sKey
29+ * @returns {string | null }
30+ */
2631 getItem : function ( sKey ) {
2732 return unescape ( document . cookie . replace ( new RegExp ( "(?:(?:^|.*;)\\s*" + escape ( sKey ) . replace ( / [ \- \. \+ \* ] / g, "\\$&" ) + "\\s*\\=\\s*([^;]*).*$)|^.*$" ) , "$1" ) ) || null ;
2833 } ,
34+ /**
35+ * @param {string } sKey
36+ * @param {string } sValue
37+ * @returns {boolean }
38+ */
2939 setItem : function ( sKey , sValue ) {
3040 if ( ! sKey || / ^ (?: e x p i r e s | m a x \- a g e | p a t h | d o m a i n | s e c u r e ) $ / i. test ( sKey ) ) {
3141 return false ;
3242 }
33- document . cookie = escape ( sKey ) + "=" + escape ( sValue ) + " ; expires=Fri, 31 Dec 9999 23:59:59 GMT; domain=stackexchange.com ; path=/" ;
43+ document . cookie = ` ${ escape ( sKey ) } = ${ escape ( sValue ) } ; expires=Fri, 31 Dec 9999 23:59:59 GMT; domain=${ location . hostname } ; path=/` ;
3444 return true ;
3545 } ,
46+ /**
47+ * @param {string } sKey
48+ * @returns {boolean }
49+ */
3650 hasItem : function ( sKey ) {
3751 return ( new RegExp ( "(?:^|;\\s*)" + escape ( sKey ) . replace ( / [ \- \. \+ \* ] / g, "\\$&" ) + "\\s*\\=" ) ) . test ( document . cookie ) ;
3852 } ,
3953 } ;
4054
4155 /** @type {JQuery<HTMLTextAreaElement | HTMLInputElement> } */
42- var currentTextfield = $ ( 'textarea, input[type=text]' ) ;
43- $ ( document ) . ready ( function ( ) {
44- $ ( document ) . on ( 'focus' , 'textarea, input[type=text]' , function ( ) {
56+ let currentTextfield = $ ( 'textarea, input[type=text]' ) ;
57+ $ ( document ) . ready ( function ( ) {
58+ $ ( document ) . on ( 'focus' , 'textarea, input[type=text]' , function ( ) {
4559 currentTextfield = $ ( this ) ;
4660 } ) ;
4761
48- var wh = $ ( window ) . height ( ) ,
49- ww = $ ( window ) . width ( ) ,
50- kb = createKeyboard ( ) . hide ( ) ;
62+ let wh = $ ( window ) . height ( ) ,
63+ ww = $ ( window ) . width ( ) ,
64+ kb = createKeyboard ( ) . hide ( ) ;
65+
5166 $ ( '#hbk-toggle span' ) . css ( {
5267 'padding' : '3px' ,
5368 'text-align' : 'center' ,
5469 'background-image' : "none" ,
5570 'font-weight' :'bolder'
5671 } ) ;
72+
5773 $ ( window ) . resize ( function ( ) {
5874 kb . css ( {
5975 top : '+=' + ( $ ( window ) . height ( ) - wh ) + 'px' ,
@@ -70,12 +86,12 @@ THE SOFTWARE.
7086 } ) ;
7187
7288 function createKeyboard ( ) {
73- var stand = "קראטוןםפשדגכעיחלךףזסבהנמצתץ" ,
74- alpha = "חזוהדגבאסןנםמלךכיטתשרקץצףפע" ,
75- nek = [ "שׁ" , "שׂ" , "וְ" , "וֱ" , "וֲ" , "וֳ" , "וִ" , "וֵ" , "וֶ" , "וַ" , "וָ" , "וֹ" , "וֻ" , "וּ" ] ,
76- x = 50 ,
77- y = 50 ,
78- kb = $ ( '<div class="hbkeyboard"></div>' ) . appendTo ( $ ( "body" ) ) ;
89+ const stand = "קראטוןםפשדגכעיחלךףזסבהנמצתץ" ,
90+ alpha = "חזוהדגבאסןנםמלךכיטתשרקץצףפע" ,
91+ nek = [ "שׁ" , "שׂ" , "וְ" , "וֱ" , "וֲ" , "וֳ" , "וִ" , "וֵ" , "וֶ" , "וַ" , "וָ" , "וֹ" , "וֻ" , "וּ" ] ,
92+ x = 50 ,
93+ y = 50 ,
94+ kb = $ ( '<div class="hbkeyboard"></div>' ) . appendTo ( $ ( "body" ) ) ;
7995
8096 $ . each ( alpha . split ( '' ) . concat ( nek ) , function ( i , letter ) {
8197 kb . append ( '<button type="button" class="hbkey" data-t="' + letter . slice ( - 1 ) + '">' + letter + '</button>' ) ;
@@ -151,27 +167,34 @@ THE SOFTWARE.
151167
152168 /* Event handling for buttons and checkboxes*/
153169 kb . find ( '.hbkey' ) . click ( function ( ) {
154- var t = currentTextfield [ 0 ] ;
155- var start = t . selectionStart ,
156- end = t . selectionEnd ,
157- text = t . value ,
158- chr = $ ( this ) . data ( 't' ) ;
170+ const t = currentTextfield [ 0 ] ;
171+
172+ const start = t . selectionStart ,
173+ end = t . selectionEnd ,
174+ text = t . value ;
175+
176+ let chr = $ ( this ) . data ( 't' ) ;
159177
160178 if ( chr === '' && $ ( '#rlm' ) . is ( ':checked' ) && t . id !== 'input' ) chr = '‏' ; //special case for rlm.
161- var res = text . slice ( 0 , start ) + chr + text . slice ( end ) ,
162- len = chr . length ;
179+
180+ const res = text . slice ( 0 , start ) + chr + text . slice ( end ) ,
181+ len = chr . length ;
182+
163183 $ ( t ) . val ( res ) . trigger ( 'input' ) . focus ( ) ;
164184 t . setSelectionRange ( start + len , start + len ) ;
165185 } ) ;
166186
167187 kb . find ( '.hbins' ) . click ( function ( ) {
168- var t = currentTextfield [ 0 ] ;
169- var start = t . selectionStart ,
170- end = t . selectionEnd ,
171- text = t . value ,
172- chr = $ ( this ) . data ( 't' ) ;
173- var res = text . slice ( 0 , start ) + chr + text . slice ( end ) ,
174- len = chr . length ;
188+ const t = currentTextfield [ 0 ] ;
189+
190+ const start = t . selectionStart ,
191+ end = t . selectionEnd ,
192+ text = t . value ,
193+ chr = $ ( this ) . data ( 't' ) ;
194+
195+ const res = text . slice ( 0 , start ) + chr + text . slice ( end ) ,
196+ len = chr . length ;
197+
175198 $ ( t ) . val ( res ) . trigger ( 'input' ) . focus ( ) ;
176199 t . setSelectionRange ( start + len , start + len ) ;
177200 } ) ;
@@ -202,8 +225,8 @@ THE SOFTWARE.
202225 kb . fadeToggle ( 'medium' ) ;
203226 } ) ;
204227
205- $ ( '#keylayout' ) . change ( function ( ) {
206- var layout = $ ( this ) . prop ( 'checked' ) ? stand : alpha ;
228+ $ ( '#keylayout' ) . change ( function ( ) {
229+ const layout = $ ( this ) . prop ( 'checked' ) ? stand : alpha ;
207230 $ ( '.hbkey' ) . slice ( 0 , 27 ) . each ( function ( index ) {
208231 $ ( this ) . data ( 't' , layout [ index ] ) . text ( layout [ index ] ) ;
209232 } ) ;
@@ -219,10 +242,8 @@ THE SOFTWARE.
219242 return kb ;
220243 }
221244
222-
223-
224245 //Draggability
225- var drag = {
246+ const drag = {
226247 elem : null ,
227248 x : 0 ,
228249 y : 0 ,
@@ -232,20 +253,23 @@ THE SOFTWARE.
232253 x : 0 ,
233254 y : 0
234255 } ;
256+
235257 $ ( document ) . on ( 'mousedown' , '.hbkeyboard' , function ( e ) {
236258 if ( ! drag . state ) {
237259 drag . elem = this ;
238260 drag . x = e . pageX ;
239261 drag . y = e . pageY ;
240262 drag . state = true ;
241263 }
264+
242265 return false ;
243266 } ) ;
267+
244268 $ ( document ) . mousemove ( function ( e ) {
245269 if ( drag . state ) {
246270 delta . x = e . pageX - drag . x ;
247271 delta . y = e . pageY - drag . y ;
248- var cur_offset = $ ( drag . elem ) . offset ( ) ;
272+ const cur_offset = $ ( drag . elem ) . offset ( ) ;
249273
250274 $ ( drag . elem ) . offset ( {
251275 left : ( cur_offset . left + delta . x ) ,
@@ -256,6 +280,7 @@ THE SOFTWARE.
256280 drag . y = e . pageY ;
257281 }
258282 } ) ;
283+
259284 $ ( document ) . mouseup ( function ( ) {
260285 drag . state && ( drag . state = false ) ;
261286 } ) ;
@@ -293,6 +318,7 @@ $(() => {
293318 } , 1000 ) ;
294319 } ) ;
295320 } ) ;
321+
296322 document . body . appendChild ( el ) ;
297323} ) ;
298324
@@ -301,12 +327,19 @@ $(() => {
301327
302328
303329$ ( ( ) => {
330+ /**
331+ * @param {string } term
332+ * @returns {Promise<string[]> }
333+ */
304334 const getSuggestions = async ( term ) => {
305335 const resp = await fetch ( `https://sefaria.org/api/name/${ term } ` ) ;
306336 const data = await resp . json ( ) ;
307337 return data . completions ;
308338 } ;
309339
340+ /**
341+ * @param {JQuery.ClickEvent } ev
342+ */
310343 const doReplacement = ( ev ) => {
311344 ev . preventDefault ( ) ;
312345
@@ -318,6 +351,9 @@ $(() => {
318351 $field . trigger ( 'markdown' ) ;
319352 } ;
320353
354+ /**
355+ * @param {string[] } suggestions
356+ */
321357 const createPopup = ( suggestions ) => {
322358 const $itemTemplate = $ ( `<a class="item" href="#"></a>` ) ;
323359 return suggestions . map ( ( s ) => {
@@ -340,22 +376,32 @@ $(() => {
340376 } ) ;
341377} ) ;
342378
379+
343380// ============================================================================================== //
344- /**
381+
382+
383+ /**
345384 * Calendar script, added 2021-04-01 by @luap42
346385 */
347386
348- // Use *local* time for ISO string (Date.toISOString() uses UTC).
349- // We only need the date here, so punting on time.
350- function toIsoDate ( date ) {
351- var pad = function ( num ) {
352- var norm = Math . floor ( Math . abs ( num ) ) ;
353- return ( norm < 10 ? '0' : '' ) + norm ;
354- } ;
355-
387+ /**
388+ * @param {number } num
389+ * @returns {string }
390+ */
391+ const pad = ( num ) => {
392+ const norm = Math . floor ( Math . abs ( num ) ) ;
393+ return ( norm < 10 ? '0' : '' ) + norm ;
394+ } ;
395+
396+ /**
397+ * Use *local* time when converting to ISO string (Date.toISOString() uses UTC).
398+ * @param {Date } date
399+ * @returns {string }
400+ */
401+ const toIsoDate = ( date ) => {
356402 return date . getFullYear ( ) +
357- '-' + pad ( date . getMonth ( ) + 1 ) +
358- '-' + pad ( date . getDate ( ) ) ;
403+ '-' + pad ( date . getMonth ( ) + 1 ) +
404+ '-' + pad ( date . getDate ( ) ) ;
359405}
360406
361407
0 commit comments