From 065bf2df404e75e07ea0d8855856989369d4e049 Mon Sep 17 00:00:00 2001 From: xdan Date: Sun, 19 May 2024 22:55:52 +0300 Subject: [PATCH] Try to fix Unable to use Speech Recognition #1139 Issue: https://github.com/xdan/jodit/issues/1139 --- src/plugins/speech-recognize/config.ts | 6 +++++- src/plugins/speech-recognize/helpers/recognize-manager.ts | 6 ++++-- src/plugins/speech-recognize/interface.ts | 1 + 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/plugins/speech-recognize/config.ts b/src/plugins/speech-recognize/config.ts index 3a65ce10..82fbd43c 100644 --- a/src/plugins/speech-recognize/config.ts +++ b/src/plugins/speech-recognize/config.ts @@ -18,6 +18,7 @@ import type { ISpeechRecognizeConstructor } from './interface'; import { SpeechRecognition } from './helpers/api'; import { RecognizeManager } from './helpers/recognize-manager'; import speechRecognizeIcon from './speech-recognize.svg'; +import { attr, isString } from 'jodit/core/helpers'; declare module 'jodit/config' { interface Config { @@ -131,7 +132,10 @@ Config.prototype.controls.speechRecognize = { const nativeApi = new ApiConstructor(); api = new RecognizeManager(jodit.async, nativeApi); - api.lang = lang; + api.lang = isString(lang) + ? lang + : attr(jodit.od.documentElement, 'lang') ?? undefined; + api.continuous = continuous; api.interimResults = interimResults; api.sound = sound; diff --git a/src/plugins/speech-recognize/helpers/recognize-manager.ts b/src/plugins/speech-recognize/helpers/recognize-manager.ts index 4daf548c..eb023f88 100644 --- a/src/plugins/speech-recognize/helpers/recognize-manager.ts +++ b/src/plugins/speech-recognize/helpers/recognize-manager.ts @@ -193,8 +193,10 @@ export class RecognizeManager for (let i = 0; i < e.results.length; i++) { const resultItem = e.results.item(i); - const { transcript } = resultItem.item(0); - this.__interimResults += transcript; + if (resultItem.length) { + const { transcript } = resultItem.item(0); + this.__interimResults += transcript; + } } if (this.__interimResults) { diff --git a/src/plugins/speech-recognize/interface.ts b/src/plugins/speech-recognize/interface.ts index 0575e64e..e5e1fdf1 100644 --- a/src/plugins/speech-recognize/interface.ts +++ b/src/plugins/speech-recognize/interface.ts @@ -14,6 +14,7 @@ export interface ISpeechRecognizeResult { item(index: number): { isFinal?: boolean; item(subIndex: number): { transcript: string }; + length: number; }; length: number; };