|
1 | 1 | <script> |
2 | 2 | import { onMount } from "svelte"; |
| 3 | + import { fly } from "svelte/transition"; |
3 | 4 | import Tailwind from "./Tailwind.svelte"; |
4 | 5 | import PDFPage from "./PDFPage.svelte"; |
5 | 6 | import Image from "./Image.svelte"; |
6 | 7 | import Text from "./Text.svelte"; |
| 8 | + import Drawing from "./Drawing.svelte"; |
| 9 | + import DrawingCanvas from "./DrawingCanvas.svelte"; |
7 | 10 | import { |
8 | 11 | readAsArrayBuffer, |
9 | 12 | readAsImage, |
|
20 | 23 | let allObjects = []; |
21 | 24 | let selectedPageIndex = -1; |
22 | 25 | let saving = false; |
| 26 | + let addingDrawing = false; |
23 | 27 | // for test purpose |
24 | | - // onMount(async () => { |
25 | | - // try { |
26 | | - // const res = await fetch("/test.pdf"); |
27 | | - // const pdfBlob = await res.blob(); |
28 | | - // await addPDF(pdfBlob); |
29 | | - // selectedPageIndex = 0; |
30 | | - // const imgBlob = await (await fetch("/test.svg")).blob(); |
31 | | - // addImage(imgBlob); |
32 | | - // addTextField("New Text Field!"); |
33 | | - // } catch (e) { |
34 | | - // console.log(e); |
35 | | - // } |
36 | | - // }); |
| 28 | + onMount(async () => { |
| 29 | + try { |
| 30 | + const res = await fetch("/test.pdf"); |
| 31 | + const pdfBlob = await res.blob(); |
| 32 | + await addPDF(pdfBlob); |
| 33 | + selectedPageIndex = 0; |
| 34 | + // const imgBlob = await (await fetch("/test.jpg")).blob(); |
| 35 | + // addImage(imgBlob); |
| 36 | + // addTextField("測試 New Text Field!"); |
| 37 | + // addDrawing(200, 100, "M30,30 L100,50 L50,70", 0.5); |
| 38 | + } catch (e) { |
| 39 | + console.log(e); |
| 40 | + } |
| 41 | + }); |
37 | 42 | async function onUploadPDF(e) { |
38 | 43 | const file = e.target.files[0]; |
39 | 44 | if (!file || file.type !== "application/pdf") return; |
|
91 | 96 | console.log(`Fail to add image.`, e); |
92 | 97 | } |
93 | 98 | } |
94 | | - async function onAddTextField() { |
| 99 | + function onAddTextField() { |
95 | 100 | if (selectedPageIndex >= 0) { |
96 | 101 | addTextField(); |
97 | 102 | } |
|
105 | 110 | size: 16, |
106 | 111 | lineHeight: 1.4, |
107 | 112 | fontFamily: "CK", |
108 | | - x: 0, |
| 113 | + x: 100, |
109 | 114 | y: 0 |
110 | 115 | }; |
111 | 116 | allObjects = allObjects.map((objects, pIndex) => |
112 | 117 | pIndex === selectedPageIndex ? [...objects, object] : objects |
113 | 118 | ); |
114 | 119 | } |
| 120 | + function onAddDrawing() { |
| 121 | + if (selectedPageIndex >= 0) { |
| 122 | + addingDrawing = true; |
| 123 | + } |
| 124 | + } |
| 125 | + function addDrawing(originWidth, originHeight, path, scale = 1) { |
| 126 | + const id = genID(); |
| 127 | + const object = { |
| 128 | + id, |
| 129 | + path, |
| 130 | + type: "drawing", |
| 131 | + x: 0, |
| 132 | + y: 0, |
| 133 | + originWidth, |
| 134 | + originHeight, |
| 135 | + width: originWidth * scale, |
| 136 | + scale |
| 137 | + }; |
| 138 | + allObjects = allObjects.map((objects, pIndex) => |
| 139 | + pIndex === selectedPageIndex ? [...objects, object] : objects |
| 140 | + ); |
| 141 | + } |
115 | 142 | function selectPage(index) { |
116 | 143 | selectedPageIndex = index; |
117 | 144 | } |
|
185 | 212 | on:click={onAddTextField}> |
186 | 213 | <img src="notes.svg" alt="An icon for adding text" /> |
187 | 214 | </label> |
188 | | - |
189 | | - <!-- coming soon --> |
190 | | - <!-- <label |
| 215 | + <label |
191 | 216 | class="flex items-center justify-center h-full w-8 hover:bg-gray-500 |
192 | 217 | cursor-pointer" |
193 | | - for="pen" |
| 218 | + on:click={onAddDrawing} |
194 | 219 | class:cursor-not-allowed={selectedPageIndex < 0}> |
195 | 220 | <img src="gesture.svg" alt="An icon for adding drawing" /> |
196 | 221 | </label> |
197 | | - <label |
| 222 | + <!-- coming soon --> |
| 223 | + <!-- <label |
198 | 224 | class="flex items-center justify-center h-full w-8 hover:bg-gray-500 |
199 | 225 | cursor-pointer" |
200 | | - for="pen" |
201 | 226 | class:cursor-not-allowed={selectedPageIndex < 0}> |
202 | 227 | <img src="add.svg" alt="An icon for create material" /> |
203 | 228 | </label> --> |
204 | 229 | </div> |
205 | 230 | <div class="justify-center mr-4 w-full max-w-xs hidden md:flex"> |
206 | 231 | <img src="/edit.svg" class="mr-2" alt="a pen, edit pdf name" /> |
207 | 232 | <input |
208 | | - placeholder="PDF file name" |
| 233 | + placeholder="Rename your PDF here" |
209 | 234 | type="text" |
210 | 235 | class="flex-grow bg-transparent" |
211 | 236 | bind:value={pdfName} /> |
|
220 | 245 | </button> |
221 | 246 |
|
222 | 247 | </div> |
| 248 | + {#if addingDrawing} |
| 249 | + <div |
| 250 | + transition:fly={{ y: -200, duration: 500 }} |
| 251 | + class="fixed z-10 top-0 left-0 right-0 border-b border-gray-300 bg-white |
| 252 | + shadow-lg" |
| 253 | + style="height: 50%;"> |
| 254 | + <DrawingCanvas |
| 255 | + on:finish={e => { |
| 256 | + const { originWidth, originHeight, path } = e.detail; |
| 257 | + const scale = 400 / originWidth; |
| 258 | + addDrawing(originWidth, originHeight, path, scale); |
| 259 | + addingDrawing = false; |
| 260 | + }} /> |
| 261 | + </div> |
| 262 | + {/if} |
223 | 263 | {#if pages.length} |
224 | 264 | <div class="flex justify-center px-5 w-full md:hidden"> |
225 | 265 | <img src="/edit.svg" class="mr-2" alt="a pen, edit pdf name" /> |
226 | 266 | <input |
| 267 | + placeholder="Rename your PDF here" |
227 | 268 | type="text" |
228 | 269 | class="flex-grow bg-transparent" |
229 | 270 | bind:value={pdfName} /> |
|
232 | 273 | {#each pages as page, pIndex (page)} |
233 | 274 | <div |
234 | 275 | class="p-5 w-full flex flex-col items-center overflow-y-hidden" |
235 | | - on:mousedown={() => selectPage(pIndex)}> |
| 276 | + on:mousedown={() => selectPage(pIndex)} |
| 277 | + on:touchstart={() => selectPage(pIndex)}> |
236 | 278 | <div |
237 | 279 | class="relative shadow-lg" |
238 | 280 | bind:clientWidth={pagesWidth[pIndex]} |
|
252 | 294 | x={object.x} |
253 | 295 | y={object.y} |
254 | 296 | width={object.width} |
255 | | - height={object.height} /> |
| 297 | + height={object.height} |
| 298 | + pageScale={pagesWidth[pIndex] / measures[pIndex]} /> |
256 | 299 | {:else if object.type === 'text'} |
257 | 300 | <Text |
258 | 301 | on:update={e => updateObject(object.id, e.detail)} |
|
261 | 304 | y={object.y} |
262 | 305 | size={object.size} |
263 | 306 | lineHeight={object.lineHeight} |
264 | | - fontFamily={object.fontFamily} /> |
| 307 | + fontFamily={object.fontFamily} |
| 308 | + pageScale={pagesWidth[pIndex] / measures[pIndex]} /> |
| 309 | + {:else if object.type === 'drawing'} |
| 310 | + <Drawing |
| 311 | + on:update={e => updateObject(object.id, e.detail)} |
| 312 | + path={object.path} |
| 313 | + x={object.x} |
| 314 | + y={object.y} |
| 315 | + width={object.width} |
| 316 | + originWidth={object.originWidth} |
| 317 | + originHeight={object.originHeight} |
| 318 | + pageScale={pagesWidth[pIndex] / measures[pIndex]} /> |
265 | 319 | {/if} |
266 | 320 | {/each} |
267 | 321 |
|
|
0 commit comments