@@ -3,58 +3,35 @@ let scores_pie_chart_instance = null;
33
44
55
6-
7- // Loader : NE PAS écraser les fill="currentColor"
8- async function load_score_SVG ( score ) {
9- if ( ! Number . isInteger ( score ) || score < 1 || score > 5 ) return document . createElement ( "span" ) ;
10- const res = await fetch ( `assets/pixels/score_${ score } .svg` ) ;
11- const text = await res . text ( ) ;
12- const doc = new DOMParser ( ) . parseFromString ( text , "image/svg+xml" ) ;
13- const svg = doc . querySelector ( "svg" ) ;
14- return svg ;
6+ async function update_svg_color ( score , color ) {
7+ const svg = document . querySelector ( `#color${ score } ` ) . parentElement . querySelector ( "svg" ) ;
8+ if ( svg ) {
9+ svg . style . color = color ;
10+ }
11+ png_settings . colors [ score ] = color ;
1512}
1613
1714
18- async function load_colored_score_SVG ( score ) {
19- const svg = await load_score_SVG ( score ) ;
20- svg . style . color = png_settings . colors [ score ] ;
21- return svg ;
22- }
23-
2415async function setup_palette_settings ( ) {
25- const colors = get_image_settings ( ) . colors ;
26- const grid = document . createElement ( "div" ) ;
27- grid . className = "palette-grid" ;
28- palette_grid . innerHTML = "" ;
29-
30- for ( let score = 1 ; score <= 5 ; score ++ ) {
31- const cell = document . createElement ( "div" ) ;
32- cell . className = "color-cell" ;
33-
34- const svg = await load_score_SVG ( score ) ;
35- svg . classList . add ( "color-icon" ) ;
36- svg . style . color = colors [ score ] ;
37-
38- const input = document . createElement ( "input" ) ;
39- input . type = "color" ;
40- input . id = `color${ score } ` ;
41- input . value = colors [ score ] ;
42- input . className = "color-picker-overlay" ;
43-
44- input . addEventListener ( "input" , ( ) => {
45- svg . style . color = input . value ;
46- png_settings . colors [ score ] = input . value ;
47- } ) ;
16+ const colors = get_image_settings ( ) . colors ;
4817
49- cell . appendChild ( svg ) ;
50- cell . appendChild ( input ) ;
51- grid . appendChild ( cell ) ;
52- }
18+ for ( let score = 1 ; score <= 5 ; score ++ ) {
19+ const cell = document . querySelector ( `#color${ score } ` ) . parentElement ;
20+ const input = document . getElementById ( `color${ score } ` ) ;
21+
22+ input . classList . add ( "color-picker-overlay" ) ;
5323
54- palette_grid . appendChild ( grid ) ;
55- }
24+ const svg = await load_colored_score_SVG ( score ) ;
25+ svg . classList . add ( "color-icon" ) ;
26+ svg . style . color = colors [ score ] ;
5627
28+ input . addEventListener ( "input" , ( ) => {
29+ update_svg_color ( score , input . value ) ;
30+ } ) ;
5731
32+ cell . appendChild ( svg ) ;
33+ }
34+ }
5835
5936
6037function calculate_streaks ( ) {
@@ -68,7 +45,7 @@ function calculate_streaks() {
6845 const [ year , month , day ] = dateStr . split ( "-" ) . map ( Number ) ;
6946 return new Date ( Date . UTC ( year , month - 1 , day ) ) ;
7047 } )
71- . sort ( ( a , b ) => a - b ) ;
48+ . sort ( ( a , b ) => a - b ) ;
7249
7350 let bestStreak = 1 ;
7451 let currentStreak = 1 ;
@@ -77,7 +54,7 @@ function calculate_streaks() {
7754 const previous_date = dates [ i - 1 ] ;
7855 const current_date = dates [ i ] ;
7956 const diffDays = ( current_date - previous_date ) / ( 1000 * 60 * 60 * 24 ) ;
80-
57+
8158 currentStreak ++ ;
8259 if ( diffDays !== 1 ) {
8360 currentStreak = 1 ;
@@ -105,31 +82,31 @@ function calculate_and_display_stats() {
10582 } ) ;
10683
10784 stats_array = [
108- { title : "Number of Pixels" , value : `<p>${ current_data . filter ( entry => entry . scores . length > 0 ) . length } </p>` } ,
109- { title : "Average score" , value : `<p>${ average ( allScores ) . toFixed ( 2 ) } </p>` } ,
110- { title : "Streaks" , value : `<p>Last: ${ streaks . currentStreak } | Best: ${ streaks . bestStreak } </p>` } ,
111- { title : "Score distribution" , value : "<canvas title='Update your colors in the \"Export Pixel image\" settings' id='scoresPieChart' class='pie-chart' width='100' height='100'></canvas>" } ,
85+ { title : "Number of Pixels" , value : `<p>${ current_data . filter ( entry => entry . scores . length > 0 ) . length } </p>` } ,
86+ { title : "Average score" , value : `<p>${ average ( allScores ) . toFixed ( 2 ) } </p>` } ,
87+ { title : "Streaks" , value : `<p>Last: ${ streaks . currentStreak } | Best: ${ streaks . bestStreak } </p>` } ,
88+ { title : "Score distribution" , value : "<canvas title='Update your colors in the \"Export Pixel image\" settings' id='scoresPieChart' class='pie-chart' width='100' height='100'></canvas>" } ,
11289 ] ;
11390
114- stats_container . innerHTML = stats_array . map ( ( { title, value} ) => `
91+ stats_container . innerHTML = stats_array . map ( ( { title, value } ) => `
11592 <div class="stat-card">
11693 <h3>${ title } </h3>
11794 ${ value }
11895 </div>
11996 ` ) . join ( "" ) ;
120-
97+
12198 setup_palette_settings ( ) ;
12299 create_scores_pie_chart ( ) ;
123100}
124101
125102
126103async function create_scores_pie_chart ( ) {
127104 const rawScores = current_data
128- . flatMap ( entry => entry . scores )
129- . reduce ( ( acc , score ) => {
130- acc [ score ] = ( acc [ score ] || 0 ) + 1 ;
131- return acc ;
132- } , { } ) ;
105+ . flatMap ( entry => entry . scores )
106+ . reduce ( ( acc , score ) => {
107+ acc [ score ] = ( acc [ score ] || 0 ) + 1 ;
108+ return acc ;
109+ } , { } ) ;
133110
134111 const scoresCount = Object . keys ( rawScores ) . map ( Number ) ;
135112 const values = scoresCount . map ( score => rawScores [ score ] || 0 ) ;
@@ -308,9 +285,9 @@ function get_word_frequency() {
308285 searchTextLower = searchTextLower . replace ( / \* \* / g, "*" ) ;
309286 }
310287 const pattern = searchTextLower
311- . split ( "*" )
312- . map ( escape_regex )
313- . join ( "(\\w+)" ) ;
288+ . split ( "*" )
289+ . map ( escape_regex )
290+ . join ( "(\\w+)" ) ;
314291 searchPattern = new RegExp ( pattern , "gi" ) ;
315292 }
316293 }
@@ -342,24 +319,24 @@ function get_word_frequency() {
342319 let words = wordRegexSearch
343320 ? [ ]
344321 : notesLower
345- . replace ( / [ ^ \p{ L} \p{ N} \p{ Extended_Pictographic} \u200D \uFE0F ] + / gu, " " )
346- . split ( / \s + / )
347- . filter ( word =>
348- ( word . replace ( / [ ^ a - z A - Z ] / g, "" ) . length >= 3 || / \p{ Extended_Pictographic} / u. test ( word ) ) && // Word is at least 3 letters long or emoji
349- ( ! STOP_WORDS . has ( word ) ) && // Word is not a stop word
350- ( ( searchWords . length === 0 ) || ( word !== searchTextLower ) ) && // Word is not the search term (avoid duplicates)
351- (
352- ( searchWords . length === 0 ) || // Either no search words or
353- searchWords . some ( ( sw , i ) => {
354- if ( i === searchWords . length - 1 ) { // Last search word can be a prefix or exact match
355- return ( word . startsWith ( sw ) || sw . startsWith ( word ) ) ;
356- }
357- else { // Other search words must be exact matches
358- return ( word === sw ) ;
359- }
360- } )
361- )
362- ) ;
322+ . replace ( / [ ^ \p{ L} \p{ N} \p{ Extended_Pictographic} \u200D \uFE0F ] + / gu, " " )
323+ . split ( / \s + / )
324+ . filter ( word =>
325+ ( word . replace ( / [ ^ a - z A - Z ] / g, "" ) . length >= 3 || / \p{ Extended_Pictographic} / u. test ( word ) ) && // Word is at least 3 letters long or emoji
326+ ( ! STOP_WORDS . has ( word ) ) && // Word is not a stop word
327+ ( ( searchWords . length === 0 ) || ( word !== searchTextLower ) ) && // Word is not the search term (avoid duplicates)
328+ (
329+ ( searchWords . length === 0 ) || // Either no search words or
330+ searchWords . some ( ( sw , i ) => {
331+ if ( i === searchWords . length - 1 ) { // Last search word can be a prefix or exact match
332+ return ( word . startsWith ( sw ) || sw . startsWith ( word ) ) ;
333+ }
334+ else { // Other search words must be exact matches
335+ return ( word === sw ) ;
336+ }
337+ } )
338+ )
339+ ) ;
363340
364341 // If searchPattern is defined, find words that match the pattern
365342 if ( searchPattern ) {
@@ -403,7 +380,7 @@ function get_word_frequency() {
403380 words_data [ word ] . count += 1 ;
404381 words_data [ word ] . scores . push ( average_score ) ;
405382 } ) ;
406-
383+
407384 } ) ;
408385
409386
@@ -432,18 +409,18 @@ async function create_word_frequency_section() {
432409 if ( words_filtered . length > 0 ) {
433410 word_freq_container . innerHTML = `
434411 ${ words_filtered . map ( word => {
435- let isWordSearched = false ;
436- if ( wordSearchText ) {
437- isWordSearched = ( normalize_string ( word . word ) === normalize_string ( wordSearchText ) ) ||
438- wordSearchText . split ( / \s + / )
439- . some ( term => normalize_string ( word . word ) === ( normalize_string ( term ) ) ) ;
440- }
441- return `<div class="word-card ${ isWordSearched ? "searched-word" : "" } ">
412+ let isWordSearched = false ;
413+ if ( wordSearchText ) {
414+ isWordSearched = ( normalize_string ( word . word ) === normalize_string ( wordSearchText ) ) ||
415+ wordSearchText . split ( / \s + / )
416+ . some ( term => normalize_string ( word . word ) === ( normalize_string ( term ) ) ) ;
417+ }
418+ return `<div class="word-card ${ isWordSearched ? "searched-word" : "" } ">
442419 <h4>${ capitalize ( word . word ) } </h4>
443420 <p title="Number of apearance">count: ${ wordDisplayPercentage ? ( 100 * word . count / current_data . length ) . toFixed ( 1 ) + "%" : word . count } </p>
444421 <p title="Average score">score: ${ ( word . avg_score ) . toFixed ( 2 ) } </p>
445422 </div>`
446- }
423+ }
447424 ) . join ( "" ) } `
448425 }
449426 else { word_freq_container . innerHTML = "<p>No word frequency data available. Try to change search words, or lower the minimum count.</p>" ; }
@@ -489,7 +466,7 @@ async function update_wordcloud() {
489466 if ( ! wordOrderByScore ) {
490467 const minimumCount = words [ words . length - 1 ] [ 1 ] ;
491468 adjustedWords = words . map ( ( [ word , count ] ) => {
492- const adjustedCount = Math . pow ( count - minimumCount + 1 , ( 3 / ( wordcloudCompression + 2 ) ) ) + 1 ;
469+ const adjustedCount = Math . pow ( count - minimumCount + 1 , ( 3 / ( wordcloudCompression + 2 ) ) ) + 1 ;
493470 return [ word , adjustedCount ] ;
494471 } ) ;
495472 }
0 commit comments