|
1 | 1 | <script lang="ts">
|
2 | 2 | import { onDestroy, onMount } from "svelte";
|
3 |
| - import { version } from "@/api"; |
4 |
| - import { showMessage } from "siyuan"; |
5 |
| - import Typo from "@/libs/b3-typography.svelte"; |
| 3 | + import { version, sql as query } from "@/api"; |
| 4 | + import { showMessage, fetchPost, Protyle } from "siyuan"; |
6 | 5 |
|
7 |
| - export let name: string; |
8 |
| - export let i18n: any; |
| 6 | + export let app; |
9 | 7 |
|
10 |
| - let time; |
11 |
| - let ver; |
| 8 | + let time: string = ""; |
| 9 | + let ver: string; |
12 | 10 |
|
13 |
| - let intv1 = setInterval(async () => { |
14 |
| - time = new Date(); |
15 |
| - }, 1000); |
| 11 | + let divProtyle: HTMLDivElement; |
| 12 | + let protyle: any; |
| 13 | + let blockID: string = ''; |
16 | 14 |
|
17 | 15 | onMount(async () => {
|
18 |
| - time = new Date(); |
19 | 16 | ver = await version();
|
| 17 | + fetchPost("/api/system/currentTime", {}, (response) => { |
| 18 | + time = new Date(response.data).toString(); |
| 19 | + }); |
| 20 | + protyle = await initProtyle(); |
20 | 21 | });
|
21 | 22 |
|
22 |
| - /** |
23 |
| - * You must call this function when the component is destroyed. |
24 |
| - */ |
25 | 23 | onDestroy(() => {
|
26 | 24 | showMessage("Hello panel closed");
|
27 |
| - clearInterval(intv1); |
| 25 | + protyle.destroy(); |
28 | 26 | });
|
29 | 27 |
|
30 |
| - $: time_str = new Date(time).toLocaleTimeString(); |
31 |
| -
|
| 28 | + async function initProtyle() { |
| 29 | + let sql = "SELECT * FROM blocks ORDER BY RANDOM () LIMIT 1;"; |
| 30 | + let blocks: Block[] = await query(sql); |
| 31 | + blockID = blocks[0].id; |
| 32 | + return new Protyle(app, divProtyle, { |
| 33 | + blockId: blockID |
| 34 | + }); |
| 35 | + } |
32 | 36 | </script>
|
33 | 37 |
|
34 |
| -<div id="hello"> |
35 |
| - <div class="fn__flex"> |
36 |
| - <div class="fn__flex-1"> |
37 |
| - <h2>Hello {name} v{ver}</h2> |
38 |
| - </div> |
39 |
| - <div class="fn__flex-1 b3-label__text __text-right"> |
40 |
| - {time_str} |
41 |
| - </div> |
| 38 | +<div class="b3-dialog__content"> |
| 39 | + <div>API demo:</div> |
| 40 | + <div class="fn__hr" /> |
| 41 | + <div class="plugin-sample__time"> |
| 42 | + System current time: <span id="time">{time}</span> |
42 | 43 | </div>
|
43 |
| - |
44 |
| - <Typo> |
45 |
| - <h2>Wellcome to plugin sample with vite & svelte</h2> |
46 |
| - <p>{@html i18n.makesure}</p> |
47 |
| - </Typo> |
48 |
| - |
| 44 | + <div class="fn__hr" /> |
| 45 | + <div class="fn__hr" /> |
| 46 | + <div>Protyle demo: id = {blockID}</div> |
| 47 | + <div class="fn__hr" /> |
| 48 | + <div id="protyle" style="height: 360px;" bind:this={divProtyle}/> |
49 | 49 | </div>
|
50 | 50 |
|
51 |
| -<style lang="scss"> |
52 |
| - #hello { |
53 |
| - margin: 20px; |
54 |
| - div { |
55 |
| - margin-bottom: 10px; |
56 |
| - } |
57 |
| - } |
58 |
| - .__text-right { |
59 |
| - text-align: right; |
60 |
| - } |
61 |
| -</style> |
0 commit comments