UNPKG

ant-design-x-vue

Version:

Craft AI-driven interfaces effortlessly

57 lines (56 loc) 2.04 kB
import b from "../_util/hooks/useMergedState.mjs"; import { computed as i, toValue as C, ref as d, watchEffect as E, onWatcherCleanup as P } from "vue"; let a; !a && typeof window < "u" && (a = window.SpeechRecognition || window.webkitSpeechRecognition); function x(h, w) { const m = h, s = i(() => { const e = C(w); return typeof e == "object" ? { controlledRecording: e.recording, onControlledRecordingChange: e.onRecordingChange, speechInControlled: typeof e.recording == "boolean" } : { controlledRecording: void 0, onControlledRecordingChange: void 0, speechInControlled: !1 }; }), R = i(() => s.value.controlledRecording), n = i(() => s.value.onControlledRecordingChange), S = i(() => s.value.speechInControlled), u = d(null); E(() => { if (typeof navigator < "u" && "permissions" in navigator) { let e = null; navigator.permissions.query({ name: "microphone" }).then((o) => { u.value = o.state, o.onchange = function() { u.value = this.state; }, e = o; }), P(() => { e && (e.onchange = null); }); } }); const p = i(() => a && u.value !== "denied"), t = d(null), [r, v] = b(!1, { value: R }), f = d(!1), y = () => { if (p.value && !t.value) { const e = new a(); e.onstart = () => { v(!0); }, e.onend = () => { v(!1); }, e.onresult = (o) => { var l, c, g; if (!f.value) { const I = (g = (c = (l = o.results) == null ? void 0 : l[0]) == null ? void 0 : c[0]) == null ? void 0 : g.transcript; m(I); } f.value = !1; }, t.value = e; } }; return { speechPermission: p, triggerSpeech: (e) => { var o, l, c; e && !r.value || (f.value = e, S.value ? (o = n.value) == null || o.call(n, !r.value) : (y(), t.value && (r.value ? (t.value.stop(), (l = n.value) == null || l.call(n, !1)) : (t.value.start(), (c = n.value) == null || c.call(n, !0))))); }, recording: r }; } export { x as default };