Spaces:
Paused

File size: 4,073 Bytes
bc9f3e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<script>
  import { t } from "../lib/stores.svelte.js";
  import "./DownloadConfirmDialog.css";

  /**
   * Lightweight download confirmation popup β€” asks "Are you sure you want
   * to download X?" before kicking off a direct download (components like
   * Poppler/Playwright or any AI model across tabs).
   *
   * Props:
   *   component β€” the download component id (e.g. "poppler", "playwright")
   *   model     β€” model id or model object { id, label, size }
   *   onclose   β€” called when the user cancels or dismisses
   *   onconfirm β€” called with component or model when confirmed
   */
  let { component, model, onclose, onconfirm } = $props();

  /** Map component id to its i18n label key. */
  const LABEL_MAP = {
    poppler: "btn_dl_poppler",
    playwright: "btn_dl_playwright",
  };

  let rootEl;
  let boxEl = $state(null);

  // ── Draggable ──────────────────────────────────────────────────
  let dragOffsetX = 0;
  let dragOffsetY = 0;
  let posX = $state(null);
  let posY = $state(null);
  let dragging = $state(false);

  function onDragStart(e) {
    if (!boxEl) return;
    if (e.button !== 0) return;
    e.preventDefault();
    const rect = boxEl.getBoundingClientRect();
    dragOffsetX = e.clientX - rect.left;
    dragOffsetY = e.clientY - rect.top;
    if (posX === null) posX = rect.left;
    if (posY === null) posY = rect.top;
    dragging = true;
  }

  function onDragMove(e) {
    if (!dragging) return;
    const w = window.innerWidth;
    const h = window.innerHeight;
    let x = e.clientX - dragOffsetX;
    let y = e.clientY - dragOffsetY;
    const bw = boxEl ? boxEl.offsetWidth : 400;
    const bh = boxEl ? boxEl.offsetHeight : 250;
    x = Math.max(0, Math.min(x, w - bw));
    y = Math.max(0, Math.min(y, h - bh));
    posX = x;
    posY = y;
  }

  function onDragEnd() {
    dragging = false;
  }

  function close() {
    onclose?.();
  }

  function confirm() {
    onconfirm?.(component || model);
  }

  // Close on Escape
  function handleKeydown(e) {
    if (e.key === "Escape") {
      e.preventDefault();
      close();
    }
  }

  const isModel = $derived(!!model);
  const modelName = $derived(
    typeof model === "object" ? (model.label || model.id || "") : (model || "")
  );
  const modelSize = $derived(
    typeof model === "object" && model.size ? `(~${model.size} GB)` : ""
  );
</script>

<!-- svelte-ignore a11y_no_static_element_interactions -->
<svelte:window
  onkeydown={handleKeydown}
  onmousemove={onDragMove}
  onmouseup={onDragEnd}
/>

<div
  class="dlcfm-overlay"
  role="presentation"
  onmousedown={(e) => {
    if (e.target === e.currentTarget) close();
  }}
>
  <div
    class="dlcfm-box"
    class:dragging
    role="dialog"
    aria-modal="true"
    bind:this={boxEl}
    style="{posX !== null ? `left:${posX}px;top:${posY}px;transform:none` : ''}"
  >
    <!-- svelte-ignore a11y_no_static_element_interactions -->
    <div
      class="dlcfm-title dlcfm-drag-handle"
      role="none"
      onmousedown={onDragStart}
    >
      {isModel ? t("model_dl_confirm_title") : t("dl_confirm_title")}
    </div>
    <div class="dlcfm-body">
      {#if isModel}
        {@html t("model_dl_confirm_body")
          .replace("{model}", `<b>${modelName}</b>`)
          .replace("{size}", modelSize ? `<b>${modelSize}</b>` : "")}
      {:else}
        {@html t("dl_confirm_body")
          .replace("{component}", `<b>${t(LABEL_MAP[component] || component)}</b>`)
          .replace("{component}", `<b>${t(LABEL_MAP[component] || component)}</b>`)}
      {/if}
    </div>
    <div class="dlcfm-actions">
      <button
        type="button"
        class="dlcfm-cancel"
        onclick={close}
        data-tooltip={t("dl_confirm_cancel")}>{t("dl_confirm_cancel")}</button
      >
      <button
        type="button"
        class="dlcfm-confirm"
        onclick={confirm}
        data-tooltip={t("dl_confirm_ok")}>{t("dl_confirm_ok")}</button
      >
    </div>
  </div>
</div>