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