@edc-motor/ui 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edc-motor/ui",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "EdC Motor — componentes públicos Vue 3 + tokens SCSS para webs de juegos de mesa (paquete fuente: lo compila el consumidor con Vite)",
5
5
  "license": "GPL-3.0-only",
6
6
  "type": "module",
@@ -19,8 +19,11 @@ const props = withDefaults(
19
19
  locales: Locale[]
20
20
  label?: string
21
21
  required?: boolean
22
- /** Sube el fichero y devuelve la URL pública que se guarda. */
23
- upload: (file: File) => Promise<string>
22
+ /** Sube el fichero (con la URL a la que sustituye, para que el backend
23
+ * borre la anterior) y devuelve la URL pública que se guarda. */
24
+ upload: (file: File, replaces?: string | null) => Promise<string>
25
+ /** Borra el fichero al pulsar "quitar" (opcional). */
26
+ removeFile?: (url: string) => void | Promise<void>
24
27
  error?: string
25
28
  }>(),
26
29
  { modelValue: () => ({}), required: false },
@@ -52,17 +55,23 @@ function setUrl(url: string | null) {
52
55
 
53
56
  async function onFile(file: File | null) {
54
57
  if (!file) {
55
- setUrl(null)
58
+ onRemove()
56
59
  return
57
60
  }
58
61
  uploading.value = true
59
62
  try {
60
- setUrl(await props.upload(file))
63
+ setUrl(await props.upload(file, currentUrl.value))
61
64
  } finally {
62
65
  uploading.value = false
63
66
  }
64
67
  }
65
68
 
69
+ function onRemove() {
70
+ const url = currentUrl.value
71
+ if (url && props.removeFile) Promise.resolve(props.removeFile(url)).catch(() => {})
72
+ setUrl(null)
73
+ }
74
+
66
75
  function onClickOutside(e: MouseEvent) {
67
76
  if (dropdownRef.value && !dropdownRef.value.contains(e.target as Node)) open.value = false
68
77
  }
@@ -112,7 +121,7 @@ onBeforeUnmount(() => document.removeEventListener('click', onClickOutside))
112
121
  :current-url="currentUrl"
113
122
  :error="uploading ? undefined : error"
114
123
  @update:model-value="onFile"
115
- @remove="setUrl(null)"
124
+ @remove="onRemove"
116
125
  />
117
126
 
118
127
  <p v-if="error" class="form-field__error">{{ error }}</p>