inertia-bootstrap-forms 3.0.1 → 4.0.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/README.fa.md +86 -6
- package/README.md +95 -8
- package/dist/chunks/UppyInput-Dt8CPfgC.js +210 -0
- package/dist/chunks/UppyInput-ldc834tZ.cjs +1 -0
- package/dist/chunks/_plugin-vue_export-helper-13TPlHFF.js +81 -0
- package/dist/chunks/_plugin-vue_export-helper-BHyUve4F.cjs +1 -0
- package/dist/chunks/http-CDPNmbjF.cjs +1 -0
- package/dist/chunks/http-DV3dRBe5.js +158 -0
- package/dist/country-codes.cjs +1 -0
- package/dist/country-codes.js +245 -0
- package/dist/dropzone.cjs +1 -0
- package/dist/dropzone.js +81 -0
- package/dist/editor.cjs +1 -0
- package/dist/editor.js +52 -0
- package/dist/index.cjs +1 -0
- package/dist/index.js +3128 -0
- package/dist/location.cjs +1 -0
- package/dist/location.js +84 -0
- package/dist/persian-date.cjs +1 -0
- package/dist/persian-date.js +73 -0
- package/dist/tel.cjs +1 -0
- package/dist/tel.js +85 -0
- package/dist/uppy-dashboard.cjs +1 -0
- package/dist/uppy-dashboard.js +31 -0
- package/dist/uppy.cjs +1 -0
- package/dist/uppy.js +4 -0
- package/index.d.ts +44 -30
- package/package.json +99 -16
- package/src/CheckboxToggle.vue +7 -1
- package/src/ComboboxInput.vue +495 -496
- package/src/PasswordInput.vue +124 -123
- package/src/StarRatingInput.vue +1 -1
- package/src/UppyDashboardInput.vue +45 -0
- package/src/UppyInput.vue +36 -3
- package/src/VoiceRecorderInput.vue +657 -0
- package/src/entries/country-codes.js +1 -0
- package/src/entries/dropzone.js +1 -0
- package/src/entries/editor.js +1 -0
- package/src/entries/location.js +1 -0
- package/src/entries/persian-date.js +1 -0
- package/src/entries/tel.js +1 -0
- package/src/entries/uppy-dashboard.js +1 -0
- package/src/entries/uppy.js +1 -0
- package/src/index.js +3 -19
- package/types/country-codes.d.ts +1 -0
- package/types/dropzone.d.ts +2 -0
- package/types/editor.d.ts +2 -0
- package/types/location.d.ts +2 -0
- package/types/persian-date.d.ts +2 -0
- package/types/tel.d.ts +2 -0
- package/types/uppy-dashboard.d.ts +12 -0
- package/types/uppy.d.ts +10 -0
- package/dist/inertia-bootstrap-forms.cjs.js +0 -1
- package/dist/inertia-bootstrap-forms.es.js +0 -3529
- package/src/RangeSliderInput.vue +0 -103
- package/src/Select2Input.vue +0 -251
|
@@ -0,0 +1,657 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import {computed, defineComponent, onMounted, reactive, ref, watch} from 'vue';
|
|
3
|
+
import {fieldProps, useField} from './composables/useField.js';
|
|
4
|
+
import {useDisposables} from './composables/useDisposables.js';
|
|
5
|
+
import {useI18n} from './i18n.js';
|
|
6
|
+
import {laravelHeaders} from './utils/http.js';
|
|
7
|
+
|
|
8
|
+
const MESSAGES = {
|
|
9
|
+
en: {
|
|
10
|
+
start: 'Record voice',
|
|
11
|
+
stop: 'Stop recording',
|
|
12
|
+
idle: 'Click the microphone to record',
|
|
13
|
+
starting: 'Waiting for microphone permission…',
|
|
14
|
+
recording: 'Recording…',
|
|
15
|
+
uploading: 'Uploading…',
|
|
16
|
+
retry: 'Retry',
|
|
17
|
+
remove: 'Remove',
|
|
18
|
+
denied: 'Microphone access is blocked. Allow the microphone for this site (lock icon in the address bar, or the browser settings) and try again.',
|
|
19
|
+
noDevice: 'No microphone was found on this device. Connect a microphone and try again.',
|
|
20
|
+
busy: 'The microphone cannot be used. It may be in use by another app or tab. Close it and try again.',
|
|
21
|
+
insecure: 'Voice recording only works on secure pages (HTTPS).',
|
|
22
|
+
unsupported: 'This browser does not support voice recording.',
|
|
23
|
+
recorder: 'Recording failed. Please try again.',
|
|
24
|
+
disconnected: 'The microphone was disconnected. The audio recorded so far was kept.',
|
|
25
|
+
tooShort: 'The recording is too short.',
|
|
26
|
+
unknown: 'Could not start the recording.',
|
|
27
|
+
uploadFailed: 'Upload failed',
|
|
28
|
+
},
|
|
29
|
+
fa: {
|
|
30
|
+
start: 'ضبط صدا',
|
|
31
|
+
stop: 'پایان ضبط',
|
|
32
|
+
idle: 'برای ضبط صدا اینجا کلیک کنید',
|
|
33
|
+
starting: 'در انتظار اجازه دسترسی به میکروفن…',
|
|
34
|
+
recording: 'در حال ضبط…',
|
|
35
|
+
uploading: 'در حال ارسال…',
|
|
36
|
+
retry: 'تلاش مجدد',
|
|
37
|
+
remove: 'حذف',
|
|
38
|
+
denied: 'دسترسی به میکروفن مسدود است. از آیکن قفل کنار آدرس (یا تنظیمات مرورگر) اجازه استفاده از میکروفن را برای این سایت فعال کنید و دوباره تلاش کنید.',
|
|
39
|
+
noDevice: 'میکروفنی روی این دستگاه پیدا نشد. یک میکروفن وصل کنید و دوباره تلاش کنید.',
|
|
40
|
+
busy: 'امکان استفاده از میکروفن نیست. ممکن است برنامه یا تب دیگری از آن استفاده کند. آن را ببندید و دوباره تلاش کنید.',
|
|
41
|
+
insecure: 'ضبط صدا فقط در صفحههای امن (HTTPS) کار میکند.',
|
|
42
|
+
unsupported: 'این مرورگر از ضبط صدا پشتیبانی نمیکند.',
|
|
43
|
+
recorder: 'ضبط با خطا مواجه شد. دوباره تلاش کنید.',
|
|
44
|
+
disconnected: 'میکروفن قطع شد. صدایی که تا این لحظه ضبط شده بود نگه داشته شد.',
|
|
45
|
+
tooShort: 'مدت ضبط خیلی کوتاه است.',
|
|
46
|
+
unknown: 'شروع ضبط ممکن نشد.',
|
|
47
|
+
uploadFailed: 'خطا در ارسال',
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// Opus is the lightest speech codec every browser can record; the container depends on the browser
|
|
52
|
+
// (Firefox: ogg, Chrome / Edge: webm, Safari: mp4)
|
|
53
|
+
const MIME_CANDIDATES = ['audio/ogg;codecs=opus', 'audio/webm;codecs=opus', 'audio/mp4', 'audio/webm', 'audio/ogg'];
|
|
54
|
+
|
|
55
|
+
let counter = 0;
|
|
56
|
+
|
|
57
|
+
function pickMime() {
|
|
58
|
+
if (typeof MediaRecorder === 'undefined' || !MediaRecorder.isTypeSupported) return '';
|
|
59
|
+
return MIME_CANDIDATES.find((type) => MediaRecorder.isTypeSupported(type)) || '';
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function extensionOf(type = '') {
|
|
63
|
+
if (/ogg/.test(type)) return 'ogg';
|
|
64
|
+
if (/webm/.test(type)) return 'webm';
|
|
65
|
+
if (/mp4|aac|m4a/.test(type)) return 'm4a';
|
|
66
|
+
if (/mpeg|mp3/.test(type)) return 'mp3';
|
|
67
|
+
if (/wav/.test(type)) return 'wav';
|
|
68
|
+
return 'audio';
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Maps getUserMedia / MediaRecorder errors to one of our message keys. */
|
|
72
|
+
function classify(error) {
|
|
73
|
+
switch (error?.name) {
|
|
74
|
+
case 'NotAllowedError':
|
|
75
|
+
case 'PermissionDeniedError':
|
|
76
|
+
return 'denied';
|
|
77
|
+
case 'SecurityError':
|
|
78
|
+
return typeof window !== 'undefined' && window.isSecureContext === false ? 'insecure' : 'denied';
|
|
79
|
+
case 'NotFoundError':
|
|
80
|
+
case 'DevicesNotFoundError':
|
|
81
|
+
case 'OverconstrainedError':
|
|
82
|
+
case 'ConstraintNotSatisfiedError':
|
|
83
|
+
return 'noDevice';
|
|
84
|
+
case 'NotReadableError':
|
|
85
|
+
case 'TrackStartError':
|
|
86
|
+
case 'AbortError':
|
|
87
|
+
return 'busy';
|
|
88
|
+
default:
|
|
89
|
+
return 'unknown';
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const pad = (n) => String(Math.floor(n)).padStart(2, '0');
|
|
94
|
+
const formatTime = (s) => `${pad(s / 60)}:${pad(s % 60)}`;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Voice recorder.
|
|
98
|
+
* - variant="box" a full-width Bootstrap card with the record button, status and timer
|
|
99
|
+
* - variant="button" a single button that takes your own classes (`button-class`, `recording-class`)
|
|
100
|
+
* Every recording is uploaded to `url` and the result is stored in the form; recordings are listed in a
|
|
101
|
+
* Bootstrap list-group with a player. No extra libraries: it uses the browser's MediaRecorder (needs HTTPS).
|
|
102
|
+
*
|
|
103
|
+
* Endpoint contract (same as FileInput): POST multipart `file` → JSON { data: { path } } (or { path });
|
|
104
|
+
* DELETE with the raw `path` as request body.
|
|
105
|
+
*/
|
|
106
|
+
export default defineComponent({
|
|
107
|
+
name: 'VoiceRecorderInput',
|
|
108
|
+
emits: ['update:modelValue', 'change', 'start', 'stop', 'recorded', 'progress', 'uploaded', 'removed', 'error'],
|
|
109
|
+
props: {
|
|
110
|
+
...fieldProps,
|
|
111
|
+
/** box | button */
|
|
112
|
+
variant: {type: String, default: 'box', validator: (v) => ['box', 'button'].includes(v)},
|
|
113
|
+
url: {type: String, default: '/upload'},
|
|
114
|
+
/** Name of the multipart field. */
|
|
115
|
+
fieldName: {type: String, default: 'file'},
|
|
116
|
+
multiple: {type: Boolean, default: false},
|
|
117
|
+
headers: {type: Object, default: () => ({})},
|
|
118
|
+
/** Stop automatically after this many seconds. */
|
|
119
|
+
maxDuration: {type: Number, default: null},
|
|
120
|
+
/** Bitrate of the recording; 32 kbps opus is plenty for speech and keeps files tiny. */
|
|
121
|
+
audioBitsPerSecond: {type: Number, default: 32000},
|
|
122
|
+
audioConstraints: {type: Object, default: () => ({echoCancellation: true, noiseSuppression: true, autoGainControl: true})},
|
|
123
|
+
/** Classes of the button in `variant="button"` (idle) … */
|
|
124
|
+
buttonClass: {type: [String, Array, Object], default: 'btn-solid theme-primary'},
|
|
125
|
+
problemClass: {type: [String, Array, Object], default: 'btn-solid theme-danger'},
|
|
126
|
+
/** … and while recording. */
|
|
127
|
+
recordingClass: {type: [String, Array, Object], default: 'btn-solid theme-danger'},
|
|
128
|
+
showList: {type: Boolean, default: true},
|
|
129
|
+
/** Ask the server to delete the file (DELETE {url}) when a recording is removed. */
|
|
130
|
+
deleteOnRemove: {type: Boolean, default: true},
|
|
131
|
+
/** (storedValue) => playable URL — used for recordings that come from the form data (edit pages). */
|
|
132
|
+
playbackUrl: {type: Function, default: (value) => value},
|
|
133
|
+
startText: {type: String, default: null},
|
|
134
|
+
stopText: {type: String, default: null},
|
|
135
|
+
locale: {type: String, default: null},
|
|
136
|
+
},
|
|
137
|
+
setup(props, {emit, expose}) {
|
|
138
|
+
const field = useField(props, emit);
|
|
139
|
+
const {locale} = useI18n(() => props.locale);
|
|
140
|
+
const d = useDisposables();
|
|
141
|
+
|
|
142
|
+
const t = (key) => (MESSAGES[locale.value] || MESSAGES.en)[key] ?? MESSAGES.en[key] ?? key;
|
|
143
|
+
|
|
144
|
+
const root = ref(null);
|
|
145
|
+
const items = ref([]);
|
|
146
|
+
const recording = ref(false);
|
|
147
|
+
const starting = ref(false);
|
|
148
|
+
const seconds = ref(0);
|
|
149
|
+
const problem = ref(null); // { code, message }
|
|
150
|
+
const supported = ref(true);
|
|
151
|
+
|
|
152
|
+
const requests = new Map();
|
|
153
|
+
let recorder = null;
|
|
154
|
+
let stream = null;
|
|
155
|
+
let chunks = [];
|
|
156
|
+
let ticker = null;
|
|
157
|
+
let startedAt = 0;
|
|
158
|
+
let discard = false;
|
|
159
|
+
|
|
160
|
+
const locked = computed(() => props.readonly || field.isDisabled.value);
|
|
161
|
+
const time = computed(() => formatTime(seconds.value));
|
|
162
|
+
const uploading = computed(() => items.value.some((i) => i.status === 'uploading'));
|
|
163
|
+
const blocked = computed(() => !supported.value || locked.value);
|
|
164
|
+
|
|
165
|
+
const statusText = computed(() => {
|
|
166
|
+
if (starting.value) return t('starting');
|
|
167
|
+
if (recording.value) return t('recording');
|
|
168
|
+
if (uploading.value) return t('uploading');
|
|
169
|
+
return t('idle');
|
|
170
|
+
});
|
|
171
|
+
const buttonLabel = computed(() =>
|
|
172
|
+
recording.value ? props.stopText ?? t('stop') : props.startText ?? t('start')
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
// ---------- problems ----------
|
|
176
|
+
function fail(code, original, item = null) {
|
|
177
|
+
problem.value = {code, message: t(code)};
|
|
178
|
+
emit('error', {code, message: problem.value.message, error: original ?? null, item});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const clearProblem = () => {
|
|
182
|
+
problem.value = null;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// ---------- model ----------
|
|
186
|
+
const modelPaths = () => {
|
|
187
|
+
const v = field.model.value;
|
|
188
|
+
if (props.multiple) return Array.isArray(v) ? v : v ? [v] : [];
|
|
189
|
+
return v ? [v] : [];
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
function publish(event) {
|
|
193
|
+
const paths = items.value.filter((i) => i.path).map((i) => i.path);
|
|
194
|
+
const next = props.multiple ? paths : (paths[0] ?? null);
|
|
195
|
+
field.model.value = next;
|
|
196
|
+
field.emitChange(next, event);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function updateFormProgress() {
|
|
200
|
+
const form = field.form.value;
|
|
201
|
+
if (!form) return;
|
|
202
|
+
const active = items.value.filter((i) => i.status === 'uploading');
|
|
203
|
+
form.uploading = active.length
|
|
204
|
+
? Math.min(99, Math.round(active.reduce((s, i) => s + i.percent, 0) / active.length))
|
|
205
|
+
: null;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function release(item) {
|
|
209
|
+
if (item.local && item.src) URL.revokeObjectURL(item.src);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// recordings that come from the form data (edit pages) and external resets
|
|
213
|
+
function syncFromModel() {
|
|
214
|
+
const paths = modelPaths();
|
|
215
|
+
|
|
216
|
+
if (!paths.length && items.value.some((i) => i.path)) {
|
|
217
|
+
items.value.filter((i) => i.path).forEach(release);
|
|
218
|
+
items.value = items.value.filter((i) => !i.path);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const known = new Set(items.value.map((i) => i.path).filter(Boolean));
|
|
222
|
+
paths.forEach((path) => {
|
|
223
|
+
if (known.has(path)) return;
|
|
224
|
+
items.value.push(
|
|
225
|
+
reactive({id: `v${++counter}`, path, src: props.playbackUrl(path), status: 'done', percent: 100, seconds: null, error: '', local: false, file: null})
|
|
226
|
+
);
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
onMounted(() => {
|
|
231
|
+
supported.value =
|
|
232
|
+
typeof navigator !== 'undefined' && !!navigator.mediaDevices?.getUserMedia && typeof MediaRecorder !== 'undefined';
|
|
233
|
+
|
|
234
|
+
if (!supported.value) {
|
|
235
|
+
fail(typeof window !== 'undefined' && window.isSecureContext === false ? 'insecure' : 'unsupported');
|
|
236
|
+
} else {
|
|
237
|
+
watchPermission();
|
|
238
|
+
}
|
|
239
|
+
syncFromModel();
|
|
240
|
+
});
|
|
241
|
+
watch(() => field.model.value, syncFromModel, {deep: true});
|
|
242
|
+
|
|
243
|
+
// tell the user up-front when the microphone is already blocked, and react when they unblock it
|
|
244
|
+
async function watchPermission() {
|
|
245
|
+
try {
|
|
246
|
+
const status = await navigator.permissions?.query({name: 'microphone'});
|
|
247
|
+
if (!status || d.isDisposed()) return;
|
|
248
|
+
|
|
249
|
+
const apply = () => {
|
|
250
|
+
if (status.state === 'denied') fail('denied');
|
|
251
|
+
else if (problem.value?.code === 'denied') clearProblem();
|
|
252
|
+
};
|
|
253
|
+
apply();
|
|
254
|
+
status.addEventListener('change', apply);
|
|
255
|
+
d.add(() => status.removeEventListener('change', apply));
|
|
256
|
+
} catch (e) {
|
|
257
|
+
/* the Permissions API does not know "microphone" in every browser */
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// ---------- recording ----------
|
|
262
|
+
function stopTracks() {
|
|
263
|
+
stream?.getTracks().forEach((track) => track.stop());
|
|
264
|
+
stream = null;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function tick() {
|
|
268
|
+
seconds.value = (Date.now() - startedAt) / 1000;
|
|
269
|
+
if (props.maxDuration && seconds.value >= props.maxDuration) stop();
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async function start() {
|
|
273
|
+
if (recording.value || starting.value || blocked.value) return;
|
|
274
|
+
clearProblem();
|
|
275
|
+
|
|
276
|
+
starting.value = true;
|
|
277
|
+
let media;
|
|
278
|
+
try {
|
|
279
|
+
media = await navigator.mediaDevices.getUserMedia({
|
|
280
|
+
audio: Object.keys(props.audioConstraints || {}).length ? props.audioConstraints : true,
|
|
281
|
+
});
|
|
282
|
+
} catch (error) {
|
|
283
|
+
starting.value = false;
|
|
284
|
+
return fail(classify(error), error);
|
|
285
|
+
}
|
|
286
|
+
starting.value = false;
|
|
287
|
+
|
|
288
|
+
if (d.isDisposed()) {
|
|
289
|
+
media.getTracks().forEach((track) => track.stop());
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const mimeType = pickMime();
|
|
294
|
+
let rec;
|
|
295
|
+
try {
|
|
296
|
+
rec = new MediaRecorder(media, {...(mimeType ? {mimeType} : {}), audioBitsPerSecond: props.audioBitsPerSecond});
|
|
297
|
+
} catch (e) {
|
|
298
|
+
try {
|
|
299
|
+
rec = new MediaRecorder(media);
|
|
300
|
+
} catch (error) {
|
|
301
|
+
media.getTracks().forEach((track) => track.stop());
|
|
302
|
+
return fail('recorder', error);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
stream = media;
|
|
307
|
+
recorder = rec;
|
|
308
|
+
chunks = [];
|
|
309
|
+
discard = false;
|
|
310
|
+
|
|
311
|
+
rec.ondataavailable = (event) => {
|
|
312
|
+
if (event.data?.size) chunks.push(event.data);
|
|
313
|
+
};
|
|
314
|
+
rec.onerror = (event) => {
|
|
315
|
+
discard = true;
|
|
316
|
+
fail('recorder', event.error);
|
|
317
|
+
stop();
|
|
318
|
+
};
|
|
319
|
+
rec.onstop = () => finish(rec);
|
|
320
|
+
|
|
321
|
+
// microphone unplugged / revoked while recording: keep what we have
|
|
322
|
+
media.getAudioTracks().forEach((track) =>
|
|
323
|
+
track.addEventListener('ended', () => {
|
|
324
|
+
if (recording.value) {
|
|
325
|
+
fail('disconnected');
|
|
326
|
+
stop();
|
|
327
|
+
}
|
|
328
|
+
})
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
rec.start(500);
|
|
332
|
+
startedAt = Date.now();
|
|
333
|
+
seconds.value = 0;
|
|
334
|
+
recording.value = true;
|
|
335
|
+
ticker = setInterval(tick, 250);
|
|
336
|
+
emit('start');
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function stop() {
|
|
340
|
+
clearInterval(ticker);
|
|
341
|
+
ticker = null;
|
|
342
|
+
if (recorder && recorder.state !== 'inactive') {
|
|
343
|
+
try {
|
|
344
|
+
recorder.stop();
|
|
345
|
+
} catch (e) {
|
|
346
|
+
finish(recorder);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
recording.value = false;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function finish(rec) {
|
|
353
|
+
clearInterval(ticker);
|
|
354
|
+
ticker = null;
|
|
355
|
+
recording.value = false;
|
|
356
|
+
const length = (Date.now() - startedAt) / 1000;
|
|
357
|
+
const data = chunks;
|
|
358
|
+
chunks = [];
|
|
359
|
+
stopTracks();
|
|
360
|
+
recorder = null;
|
|
361
|
+
|
|
362
|
+
if (discard) return;
|
|
363
|
+
emit('stop', length);
|
|
364
|
+
|
|
365
|
+
const type = rec.mimeType || pickMime() || 'audio/webm';
|
|
366
|
+
const blob = new Blob(data, {type});
|
|
367
|
+
if (!blob.size || length < 0.4) return fail('tooShort');
|
|
368
|
+
|
|
369
|
+
add(blob, length);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function toggle() {
|
|
373
|
+
return recording.value ? stop() : start();
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// ---------- list + upload ----------
|
|
377
|
+
function add(blob, length) {
|
|
378
|
+
// single mode: the new recording replaces the previous one
|
|
379
|
+
if (!props.multiple) items.value.slice().forEach((old) => remove(old, false));
|
|
380
|
+
|
|
381
|
+
const file = new File([blob], `voice-${Date.now()}.${extensionOf(blob.type)}`, {type: blob.type});
|
|
382
|
+
const item = reactive({
|
|
383
|
+
id: `v${++counter}`,
|
|
384
|
+
file,
|
|
385
|
+
src: URL.createObjectURL(file),
|
|
386
|
+
seconds: Math.round(length),
|
|
387
|
+
status: 'uploading',
|
|
388
|
+
percent: 0,
|
|
389
|
+
path: null,
|
|
390
|
+
error: '',
|
|
391
|
+
local: true,
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
items.value = [...items.value, item];
|
|
395
|
+
emit('recorded', item);
|
|
396
|
+
upload(item);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function upload(item) {
|
|
400
|
+
item.status = 'uploading';
|
|
401
|
+
item.percent = 0;
|
|
402
|
+
item.error = '';
|
|
403
|
+
updateFormProgress();
|
|
404
|
+
|
|
405
|
+
const body = new FormData();
|
|
406
|
+
body.append(props.fieldName, item.file, item.file.name);
|
|
407
|
+
|
|
408
|
+
const xhr = new XMLHttpRequest();
|
|
409
|
+
requests.set(item.id, xhr);
|
|
410
|
+
xhr.open('POST', props.url);
|
|
411
|
+
Object.entries(laravelHeaders(props.headers)).forEach(([k, v]) => xhr.setRequestHeader(k, v));
|
|
412
|
+
|
|
413
|
+
xhr.upload.onprogress = (e) => {
|
|
414
|
+
if (!e.lengthComputable) return;
|
|
415
|
+
item.percent = Math.round((e.loaded / e.total) * 100);
|
|
416
|
+
emit('progress', item, item.percent);
|
|
417
|
+
updateFormProgress();
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
const failUpload = (message) => {
|
|
421
|
+
requests.delete(item.id);
|
|
422
|
+
item.status = 'error';
|
|
423
|
+
item.error = message || t('uploadFailed');
|
|
424
|
+
updateFormProgress();
|
|
425
|
+
fail('uploadFailed', null, item);
|
|
426
|
+
problem.value = {code: 'uploadFailed', message: item.error};
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
xhr.onerror = () => failUpload();
|
|
430
|
+
xhr.onload = () => {
|
|
431
|
+
let json = null;
|
|
432
|
+
try {
|
|
433
|
+
json = JSON.parse(xhr.responseText);
|
|
434
|
+
} catch (e) { /* not json */
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
if (xhr.status < 200 || xhr.status >= 300) {
|
|
438
|
+
const first = json?.errors ? Object.values(json.errors)[0] : null;
|
|
439
|
+
return failUpload(json?.message || (Array.isArray(first) ? first[0] : first));
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const result = json?.data ?? json;
|
|
443
|
+
const stored = result?.path ?? result?.url;
|
|
444
|
+
if (!stored) return failUpload();
|
|
445
|
+
|
|
446
|
+
requests.delete(item.id);
|
|
447
|
+
item.path = stored;
|
|
448
|
+
item.status = 'done';
|
|
449
|
+
item.percent = 100;
|
|
450
|
+
updateFormProgress();
|
|
451
|
+
clearProblem();
|
|
452
|
+
publish();
|
|
453
|
+
emit('uploaded', item, json);
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
xhr.send(body);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
async function remove(item, notify = true) {
|
|
460
|
+
requests.get(item.id)?.abort();
|
|
461
|
+
requests.delete(item.id);
|
|
462
|
+
release(item);
|
|
463
|
+
items.value = items.value.filter((i) => i.id !== item.id);
|
|
464
|
+
updateFormProgress();
|
|
465
|
+
|
|
466
|
+
if (item.path) {
|
|
467
|
+
publish();
|
|
468
|
+
if (props.deleteOnRemove) {
|
|
469
|
+
try {
|
|
470
|
+
await fetch(props.url, {
|
|
471
|
+
method: 'DELETE',
|
|
472
|
+
headers: laravelHeaders(props.headers),
|
|
473
|
+
credentials: 'same-origin',
|
|
474
|
+
body: item.path,
|
|
475
|
+
});
|
|
476
|
+
} catch (e) { /* the recording is already detached from the form */
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
if (notify) emit('removed', item);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// only one player at a time
|
|
484
|
+
function onPlay(event) {
|
|
485
|
+
root.value?.querySelectorAll('audio').forEach((audio) => {
|
|
486
|
+
if (audio !== event.target) audio.pause();
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// ---------- cleanup ----------
|
|
491
|
+
d.add(() => {
|
|
492
|
+
discard = true;
|
|
493
|
+
clearInterval(ticker);
|
|
494
|
+
if (recorder && recorder.state !== 'inactive') {
|
|
495
|
+
try {
|
|
496
|
+
recorder.stop();
|
|
497
|
+
} catch (e) { /* noop */
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
stopTracks();
|
|
501
|
+
requests.forEach((xhr) => xhr.abort());
|
|
502
|
+
requests.clear();
|
|
503
|
+
items.value.forEach(release);
|
|
504
|
+
if (field.form.value) field.form.value.uploading = null;
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
expose({start, stop, toggle});
|
|
508
|
+
|
|
509
|
+
return {
|
|
510
|
+
...field,
|
|
511
|
+
t,
|
|
512
|
+
root,
|
|
513
|
+
items,
|
|
514
|
+
recording,
|
|
515
|
+
starting,
|
|
516
|
+
seconds,
|
|
517
|
+
time,
|
|
518
|
+
problem,
|
|
519
|
+
supported,
|
|
520
|
+
blocked,
|
|
521
|
+
statusText,
|
|
522
|
+
buttonLabel,
|
|
523
|
+
toggle,
|
|
524
|
+
upload,
|
|
525
|
+
remove,
|
|
526
|
+
onPlay,
|
|
527
|
+
formatTime,
|
|
528
|
+
};
|
|
529
|
+
},
|
|
530
|
+
});
|
|
531
|
+
</script>
|
|
532
|
+
|
|
533
|
+
<template>
|
|
534
|
+
<div ref="root" class="voice-recorder" :id="inputId" :class="{ 'is-invalid': isInvalid }">
|
|
535
|
+
<!-- variant: box (full-width Bootstrap card) -->
|
|
536
|
+
<div v-if="variant === 'box'" class="card w-100">
|
|
537
|
+
<div class="card-body">
|
|
538
|
+
<div class="row gx-5">
|
|
539
|
+
<div class="col-auto mx-auto" v-if="problem">
|
|
540
|
+
<div class="alert theme-danger p-1 text-center mb-4" role="alert" :data-code="problem.code">{{ problem.message }}</div>
|
|
541
|
+
</div>
|
|
542
|
+
<div class="col-12 text-center">
|
|
543
|
+
<div class="btn btn-subtle theme-inverse mx-auto" @click="toggle">
|
|
544
|
+
<div class="row gx-5 align-items-center">
|
|
545
|
+
<div class="col-auto">
|
|
546
|
+
<button
|
|
547
|
+
type="button"
|
|
548
|
+
class="btn"
|
|
549
|
+
:class="recording ? recordingClass : buttonClass"
|
|
550
|
+
:disabled="blocked || starting"
|
|
551
|
+
:aria-label="buttonLabel"
|
|
552
|
+
:aria-pressed="recording"
|
|
553
|
+
>
|
|
554
|
+
<span v-if="starting" class="spinner-border spinner-sm" role="status" aria-hidden="true"></span>
|
|
555
|
+
<svg v-else-if="recording" width="18" height="18" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true">
|
|
556
|
+
<rect x="3.5" y="3.5" width="9" height="9" rx="1.5"/>
|
|
557
|
+
</svg>
|
|
558
|
+
<svg v-else width="18" height="18" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"
|
|
559
|
+
stroke-linejoin="round"
|
|
560
|
+
aria-hidden="true">
|
|
561
|
+
<rect x="5.5" y="1.5" width="5" height="8" rx="2.5"/>
|
|
562
|
+
<path d="M3 7.5a5 5 0 0 0 10 0M8 12.5v2"/>
|
|
563
|
+
</svg>
|
|
564
|
+
</button>
|
|
565
|
+
</div>
|
|
566
|
+
<div class="col">
|
|
567
|
+
<div class="flex-grow-1 text-center">
|
|
568
|
+
<div class="fw-semibold" role="status" aria-live="polite">{{ statusText }}</div>
|
|
569
|
+
<div class="fg-secondary fanum" dir="ltr">{{ time }}
|
|
570
|
+
<template v-if="maxDuration"> / {{ formatTime(maxDuration) }}</template>
|
|
571
|
+
</div>
|
|
572
|
+
</div>
|
|
573
|
+
</div>
|
|
574
|
+
<div class="col-auto" v-if="recording">
|
|
575
|
+
<span class="spinner-grow spinner-sm fg-danger" role="status" aria-hidden="true"></span>
|
|
576
|
+
</div>
|
|
577
|
+
</div>
|
|
578
|
+
</div>
|
|
579
|
+
</div>
|
|
580
|
+
</div>
|
|
581
|
+
<ul v-if="showList && items.length" class="list-group mt-2">
|
|
582
|
+
<li v-for="item in items" :key="item.id" class="list-group-item">
|
|
583
|
+
<div class="row gx-3 align-items-center">
|
|
584
|
+
<div class="col-12 sm:col-4">
|
|
585
|
+
<audio class="align-middle w-100" controls preload="metadata" :src="item.src" @play="onPlay"></audio>
|
|
586
|
+
</div>
|
|
587
|
+
<div class="col-auto" v-if="item.seconds !== null">
|
|
588
|
+
<span class="fg-secondary fanum" dir="ltr">{{ formatTime(item.seconds) }}</span>
|
|
589
|
+
</div>
|
|
590
|
+
<div class="col">
|
|
591
|
+
<div v-if="item.status === 'uploading'" class="progress" role="progressbar" :aria-valuenow="item.percent" aria-valuemin="0" aria-valuemax="100">
|
|
592
|
+
<div class="progress-bar" :style="{ width: item.percent + '%' }"></div>
|
|
593
|
+
</div>
|
|
594
|
+
<div v-if="item.status === 'error'" class="fg-danger" role="alert">{{ item.error }}</div>
|
|
595
|
+
</div>
|
|
596
|
+
<div class="col-auto" v-if="item.status === 'error'">
|
|
597
|
+
<button
|
|
598
|
+
type="button"
|
|
599
|
+
class="btn-text theme-secondary btn-icon"
|
|
600
|
+
:aria-label="t('retry')"
|
|
601
|
+
@click="upload(item)"
|
|
602
|
+
>
|
|
603
|
+
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"
|
|
604
|
+
aria-hidden="true">
|
|
605
|
+
<path d="M13.5 3v3.5H10"/>
|
|
606
|
+
<path d="M3.2 6.5A5.5 5.5 0 0 1 13 6.5M12.8 9.5A5.5 5.5 0 0 1 3 9.5M2.5 13V9.5H6"/>
|
|
607
|
+
</svg>
|
|
608
|
+
</button>
|
|
609
|
+
</div>
|
|
610
|
+
<div class="col-auto">
|
|
611
|
+
<button
|
|
612
|
+
v-if="!locked"
|
|
613
|
+
type="button"
|
|
614
|
+
class="btn-text theme-danger btn-icon"
|
|
615
|
+
:aria-label="t('remove')"
|
|
616
|
+
@click="remove(item)"
|
|
617
|
+
>
|
|
618
|
+
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"
|
|
619
|
+
aria-hidden="true">
|
|
620
|
+
<path d="M2.5 4.5h11M6 4.5V3a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1v1.5M4 4.5l.6 8.1a1 1 0 0 0 1 .9h4.8a1 1 0 0 0 1-.9l.6-8.1"/>
|
|
621
|
+
</svg>
|
|
622
|
+
</button>
|
|
623
|
+
</div>
|
|
624
|
+
</div>
|
|
625
|
+
</li>
|
|
626
|
+
</ul>
|
|
627
|
+
</div>
|
|
628
|
+
</div>
|
|
629
|
+
<!-- variant: button (your own classes) -->
|
|
630
|
+
<button
|
|
631
|
+
v-else
|
|
632
|
+
type="button"
|
|
633
|
+
:disabled="blocked || starting"
|
|
634
|
+
:aria-pressed="recording"
|
|
635
|
+
:class="{
|
|
636
|
+
[recordingClass]: recording,
|
|
637
|
+
[buttonClass]: !problem && !recording,
|
|
638
|
+
[problemClass]: problem,
|
|
639
|
+
}"
|
|
640
|
+
@click="toggle"
|
|
641
|
+
>
|
|
642
|
+
<slot :recording="recording" :starting="starting" :seconds="seconds" :time="time">
|
|
643
|
+
<span v-if="starting" class="spinner-border spinner-sm me-2" role="status" aria-hidden="true"></span>
|
|
644
|
+
<svg v-else-if="recording" class="me-2" width="16" height="16" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true">
|
|
645
|
+
<rect x="3.5" y="3.5" width="9" height="9" rx="1.5"/>
|
|
646
|
+
</svg>
|
|
647
|
+
<svg v-else class="me-2" width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"
|
|
648
|
+
stroke-linejoin="round" aria-hidden="true">
|
|
649
|
+
<rect x="5.5" y="1.5" width="5" height="8" rx="2.5"/>
|
|
650
|
+
<path d="M3 7.5a5 5 0 0 0 10 0M8 12.5v2"/>
|
|
651
|
+
</svg>
|
|
652
|
+
{{ buttonLabel }}
|
|
653
|
+
<span v-if="recording" class="ms-2 fanum" dir="ltr">{{ time }}</span>
|
|
654
|
+
</slot>
|
|
655
|
+
</button>
|
|
656
|
+
</div>
|
|
657
|
+
</template>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as countryCodes } from '../countryCodes.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as DropzoneInput } from '../DropzoneInput.vue';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as EditorInput } from '../EditorInput.vue';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as LocationInput } from '../LocationInput.vue';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as PersianDatePickerInput } from '../PersianDatePickerInput.vue';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as TelInput } from '../TelInput.vue';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as UppyDashboardInput } from '../UppyDashboardInput.vue';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as UppyInput } from '../UppyInput.vue';
|