radames commited on
Commit
5e671ec
β€’
1 Parent(s): 07fde23

redesign results page

Browse files
frontend/src/lib/Result.svelte CHANGED
@@ -2,6 +2,10 @@
2
  import { colors, cheersMessages } from '$lib/utils';
3
  import type { Board } from '../types';
4
  import { fade } from 'svelte/transition';
 
 
 
 
5
  export let board: Board;
6
  export let currentRowIndex: number;
7
  export let imagePaths: string[];
@@ -11,26 +15,29 @@
11
  const imageBaseUrl = import.meta.env.MODE === 'development' ? 'http://localhost:7860/' : '';
12
 
13
  let elToShare: HTMLDivElement;
14
- // let domToImage: DomToImage;
15
- let copyState = false;
16
- // onMount(async () => {
17
- // domToImage = (await import('dom-to-image')) as unknown as DomToImage;
18
- // });
19
- async function writeClipDOM(node: HTMLDivElement) {
20
  try {
21
- await navigator.clipboard.write([
22
- new ClipboardItem({
23
- 'image/png': domtoimage.toBlob(node, { bgcolor: '#000' })
24
- })
25
- ]);
26
- copyState = true;
27
- setTimeout(() => (copyState = false), 1000);
28
- console.log('Fetched image copied.');
 
 
 
 
 
 
 
29
  } catch (err) {
30
  console.log(err.name, err.message);
31
  }
32
  }
33
-
34
  const s = 10;
35
  const p = 1;
36
  const rx = s / 10;
@@ -41,6 +48,12 @@
41
  <div class="message">
42
  <div class="border-0">
43
  <div class="p-3" bind:this={elToShare}>
 
 
 
 
 
 
44
  <h2 class="text-center uppercase tracking-widest font-extrabold">{message}</h2>
45
  <div class="grid grid-cols-3 gap-2 p-3">
46
  {#each imagePaths as image}
@@ -67,26 +80,14 @@
67
  {/each}
68
  {/each}
69
  </svg>
70
- <p class="text-[0.6rem] font-extralight text-gray-300 opacity-50">
71
- https://huggingface.co/spaces/huggingface-projects/wordalle
72
- </p>
73
  </div>
74
  </div>
75
- <p class="p-3 font-normal text-base">
76
- Copy the result to clipboard
77
- <button class="min-w-[6ch]" on:click={() => writeClipDOM(elToShare)}>
78
- {!copyState ? 'Copy' : 'Copied'}
79
- </button>. Then go to Twitter and Share
80
- <a
81
- class="go-tweet"
82
- target="_blank"
83
- rel="noopener noreferrer"
84
- href="https://twitter.com/intent/tweet?url=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fosanseviero%2Fwordalle&via=huggingface&hashtags=dallemini"
85
- >
86
- Share on Twitter
87
- </a>
88
- or <button class="min-w-[6ch]" on:click={() => window.location.reload()}> Try again </button>
89
- </p>
90
  </div>
91
  </div>
92
 
 
2
  import { colors, cheersMessages } from '$lib/utils';
3
  import type { Board } from '../types';
4
  import { fade } from 'svelte/transition';
5
+ import { createEventDispatcher } from 'svelte';
6
+
7
+ const dispatch = createEventDispatcher();
8
+
9
  export let board: Board;
10
  export let currentRowIndex: number;
11
  export let imagePaths: string[];
 
15
  const imageBaseUrl = import.meta.env.MODE === 'development' ? 'http://localhost:7860/' : '';
16
 
17
  let elToShare: HTMLDivElement;
18
+ let disableDownload: boolean = false;
19
+ async function saveFile(node: HTMLDivElement) {
20
+ disableDownload = true;
 
 
 
21
  try {
22
+ const blob = await domtoimage.toBlob(node, { bgcolor: '#000' });
23
+ const a = document.createElement('a');
24
+ a.download = `sucess-${Date.now()}.png`;
25
+ a.onclick = async (e) => {
26
+ disableDownload = true;
27
+ if (a.href) {
28
+ URL.revokeObjectURL(a.href);
29
+ disableDownload = false;
30
+ return;
31
+ }
32
+ a.href = URL.createObjectURL(blob);
33
+ disableDownload = false;
34
+ };
35
+ a.click();
36
+ console.log('Downloding image.');
37
  } catch (err) {
38
  console.log(err.name, err.message);
39
  }
40
  }
 
41
  const s = 10;
42
  const p = 1;
43
  const rx = s / 10;
 
48
  <div class="message">
49
  <div class="border-0">
50
  <div class="p-3" bind:this={elToShare}>
51
+ <header class="p-3 flex justify-between items-center">
52
+ <h1 class="text-xs font-bold uppercase whitespace-nowrap">WORDALLE πŸ₯‘</h1>
53
+ <span class="font-light text-[0.6rem]"
54
+ ><span class="text-gray-50 opacity-50">https://</span>hf.co/wordalle</span
55
+ >
56
+ </header>
57
  <h2 class="text-center uppercase tracking-widest font-extrabold">{message}</h2>
58
  <div class="grid grid-cols-3 gap-2 p-3">
59
  {#each imagePaths as image}
 
80
  {/each}
81
  {/each}
82
  </svg>
 
 
 
83
  </div>
84
  </div>
85
+ <div class="p-3 px-6 flex text-base">
86
+ <button disabled={disableDownload} class="min-w-[15ch] flex-1 mr-1" on:click={() => saveFile(elToShare)}>
87
+ {!disableDownload ? 'SAVE SCREENSHOT' : 'SAVING..'}
88
+ </button>
89
+ <button class="flex-1 ml-1" on:click={() => dispatch('restart')}> TRY AGAIN </button>
90
+ </div>
 
 
 
 
 
 
 
 
 
91
  </div>
92
  </div>
93
 
frontend/src/routes/index.svelte CHANGED
@@ -13,6 +13,18 @@
13
  const totalTime = 1000;
14
  const apiUrl = import.meta.env.MODE === 'development' ? 'http://localhost:7860/data' : 'data';
15
  const imageBaseUrl = import.meta.env.MODE === 'development' ? 'http://localhost:7860/' : '';
 
 
 
 
 
 
 
 
 
 
 
 
16
  // Get word of the day
17
  let answer: string;
18
  let imagePaths: string[];
@@ -27,25 +39,20 @@
27
 
28
  // Feedback state: message and shake
29
  let message = '';
30
- let grid = '';
31
  let shakeRowIndex = -1;
32
  let success = false;
33
  // Handle keyboard input.
34
  let allowInput = true;
35
 
36
- let promptsData: PromptsData;
37
- let completedPrompts: SuccessPrompt[] = [];
38
- let currPromptIndex: number;
39
-
40
- onMount(async () => {
41
- promptsData = await fetch(apiUrl).then((d) => d.json());
42
- restartBoard();
43
-
44
- window.addEventListener('keyup', onKeyup);
45
- return () => window.removeEventListener('keyup', onKeyup);
46
- });
47
-
48
  function restartBoard() {
 
 
 
 
 
 
 
 
49
  const prompts: string[] = Object.keys(promptsData);
50
  currPromptIndex = ~~(Math.random() * prompts.length);
51
  const randomPrompt: string = prompts[currPromptIndex];
@@ -171,7 +178,7 @@
171
  <Message {message} />
172
  {/if}
173
  {#if success}
174
- <Result {board} {currentRowIndex} {imagePaths} />
175
  {/if}
176
  <!-- <div class="message" transition:fade>
177
  {message}
@@ -183,7 +190,7 @@
183
  <header class="flex justify-between items-center uppercase sm:px-2 text-center">
184
  <span class="font-light flex-1 text-xs sm:text-base"> Guess the prompt!</span>
185
  <span class="sm:block hidden mx-3 flex-1 border-[0.5px] border-opacity-50 border-gray-400" />
186
- <h1 class="text-xl font-bold text-center">πŸ₯‘ WORDALLE πŸ₯‘</h1>
187
  <span class="sm:block hidden mx-3 flex-1 border-[0.5px] border-opacity-50 border-gray-400" />
188
  <span class="font-light flex-1 text-xs sm:text-base">
189
  <button
 
13
  const totalTime = 1000;
14
  const apiUrl = import.meta.env.MODE === 'development' ? 'http://localhost:7860/data' : 'data';
15
  const imageBaseUrl = import.meta.env.MODE === 'development' ? 'http://localhost:7860/' : '';
16
+
17
+ let promptsData: PromptsData;
18
+ let completedPrompts: SuccessPrompt[] = [];
19
+ let currPromptIndex: number;
20
+ onMount(async () => {
21
+ promptsData = await fetch(apiUrl).then((d) => d.json());
22
+ restartBoard();
23
+
24
+ window.addEventListener('keyup', onKeyup);
25
+ return () => window.removeEventListener('keyup', onKeyup);
26
+ });
27
+
28
  // Get word of the day
29
  let answer: string;
30
  let imagePaths: string[];
 
39
 
40
  // Feedback state: message and shake
41
  let message = '';
 
42
  let shakeRowIndex = -1;
43
  let success = false;
44
  // Handle keyboard input.
45
  let allowInput = true;
46
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  function restartBoard() {
48
+ //reset all states
49
+ success = false;
50
+ shakeRowIndex = -1;
51
+ message = '';
52
+ currentRowIndex = 0;
53
+ letterStates = {}
54
+ allowInput= true;
55
+
56
  const prompts: string[] = Object.keys(promptsData);
57
  currPromptIndex = ~~(Math.random() * prompts.length);
58
  const randomPrompt: string = prompts[currPromptIndex];
 
178
  <Message {message} />
179
  {/if}
180
  {#if success}
181
+ <Result {board} {currentRowIndex} {imagePaths} on:restart={restartBoard} />
182
  {/if}
183
  <!-- <div class="message" transition:fade>
184
  {message}
 
190
  <header class="flex justify-between items-center uppercase sm:px-2 text-center">
191
  <span class="font-light flex-1 text-xs sm:text-base"> Guess the prompt!</span>
192
  <span class="sm:block hidden mx-3 flex-1 border-[0.5px] border-opacity-50 border-gray-400" />
193
+ <h1 class="text-xl font-bold text-center whitespace-nowrap">πŸ₯‘ WORDALLE πŸ₯‘</h1>
194
  <span class="sm:block hidden mx-3 flex-1 border-[0.5px] border-opacity-50 border-gray-400" />
195
  <span class="font-light flex-1 text-xs sm:text-base">
196
  <button