Skip to content

Commit

Permalink
feat: await updateReport request to change span
Browse files Browse the repository at this point in the history
  • Loading branch information
ItalloDornelas committed Jan 12, 2024
2 parents a95a240 + f7a4ced commit 2e45e8c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 20 deletions.
22 changes: 12 additions & 10 deletions src/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { IaraEditorInferenceFormatter } from "./formatter";
import { IaraEditorStyleManager } from "./style";

export abstract class EditorAdapter {
public onFinishedReport?: () => void;
public onIaraCommand?: (command: string) => void;
protected _inferenceFormatter: IaraEditorInferenceFormatter;
protected abstract _styleManager: IaraEditorStyleManager;
private _listeners = [
Expand All @@ -21,6 +21,7 @@ export abstract class EditorAdapter {
this.blockEditorWhileSpeaking(true);
},
},

{
key: "iaraSpeechRecognitionStop",
callback: () => {
Expand Down Expand Up @@ -57,7 +58,7 @@ export abstract class EditorAdapter {

abstract blockEditorWhileSpeaking(status: boolean): void;
abstract clearReport(): void;
abstract copyReport(): void;
abstract copyReport(): Promise<void>;
abstract insertInference(inference: IaraSpeechRecognitionDetail): void;
abstract getEditorContent(): Promise<[string, string, string]>;

Expand All @@ -66,22 +67,23 @@ export abstract class EditorAdapter {
return this._recognition.beginReport({ richText: "", text: "" });
}

finishReport(clear = true): void {
async finishReport(): Promise<void> {
if (!this._shouldSaveReport) return;
this.copyReport();
if (clear) this.clearReport();
await this.copyReport();
this.clearReport();
this._recognition.finishReport();
this.onFinishedReport?.();
}

private _initCommands(): void {
this._recognition.commands.add("iara copiar laudo", () => {
this._recognition.commands.add("iara copiar laudo", async () => {
this._recognition.stop();
this.copyReport();
await this.copyReport();
this.onIaraCommand?.("iara copiar laudo");
});
this._recognition.commands.add("iara finalizar laudo", () => {
this._recognition.commands.add("iara finalizar laudo", async () => {
this._recognition.stop();
this.finishReport();
await this.finishReport();
this.onIaraCommand?.("iara finalizar laudo");
});
this._recognition.commands.add("iara negrito", () => {
this._styleManager.toggleBold();
Expand Down
1 change: 1 addition & 0 deletions src/speech/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,7 @@ export declare class IaraSpeechRecognitionDetail extends IaraEventDetail {
transcript: string;
richTranscript: string;
richTranscriptModifiers?: string[];
richTranscriptWithoutModifiers?: string;
rid?: string;
isFinal: boolean;
isFirst: boolean;
Expand Down
74 changes: 65 additions & 9 deletions src/syncfusion/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import type { DocumentEditorContainer } from "@syncfusion/ej2-documenteditor";
import {
createSpinner,
showSpinner,
hideSpinner,
} from "@syncfusion/ej2-popups";
import { ListView, SelectedCollection } from "@syncfusion/ej2-lists";
import { Dialog } from "@syncfusion/ej2-popups";
import { EditorAdapter } from "../editor";
import { IaraSpeechRecognition, IaraSpeechRecognitionDetail } from "../speech";
import { IaraSFDT, IaraSyncfusionEditorContentManager } from "./content";
import { IaraSyncfusionSelectionManager as IaraSyncfusionInferenceSelectionManager } from "./selection";
import { IaraSyncfusionSelectionManager } from "./selection";
import { IaraSyncfusionShortcutsManager } from "./shortcuts";
import { IaraSyncfusionStyleManager } from "./style";
import { IaraSyncfusionToolbarManager } from "./toolbar";
Expand All @@ -16,10 +21,14 @@ export class IaraSyncfusionAdapter
private _contentManager: IaraSyncfusionEditorContentManager;
private _debouncedSaveReport: () => void;
private _initialUndoStackSize = 0;
private _selectionManager?: IaraSyncfusionInferenceSelectionManager;
private _selectionManager?: IaraSyncfusionSelectionManager;
private _shortcutsManager: IaraSyncfusionShortcutsManager;
private _toolbarManager: IaraSyncfusionToolbarManager;

private _resetSelection = false;

private _cursorSelection?: IaraSyncfusionSelectionManager;

protected _styleManager: IaraSyncfusionStyleManager;

public savingReportSpan = document.createElement("span");
Expand Down Expand Up @@ -62,6 +71,32 @@ export class IaraSyncfusionAdapter
"destroyed",
this._onEditorDestroyed.bind(this)
);

createSpinner({
target: _editorContainer.editorContainer,
});

this._editorContainer.documentEditor.addEventListener(
"selectionChange",
() => {
if (this._resetSelection) {
this._resetSelection = false;
this._cursorSelection?.resetSelection();
this._cursorSelection = undefined;
}
}
);

this._editorContainer.element.addEventListener("mousedown", event => {
if (event.button === 1) {
this._resetSelection = true;
this._cursorSelection = new IaraSyncfusionSelectionManager(
this._editorContainer.documentEditor
);
event.preventDefault();
this._recognition.toggleRecording();
}
});
}

blockEditorWhileSpeaking(status: boolean): void {
Expand All @@ -72,9 +107,11 @@ export class IaraSyncfusionAdapter
async copyReport(): Promise<void> {
this._editorContainer.documentEditor.focusIn();
this._editorContainer.documentEditor.selection.selectAll();
showSpinner(this._editorContainer.editorContainer);
this._recognition.automation.copyText(
...(await this._contentManager.getContent())
);
hideSpinner(this._editorContainer.editorContainer);
this._editorContainer.documentEditor.selection.moveNextPosition();
}

Expand Down Expand Up @@ -115,7 +152,7 @@ export class IaraSyncfusionAdapter
if (inference.richTranscriptModifiers?.length && !inference.isFinal) return;

if (inference.isFirst) {
this._selectionManager = new IaraSyncfusionInferenceSelectionManager(
this._selectionManager = new IaraSyncfusionSelectionManager(
this._editorContainer.documentEditor
);

Expand All @@ -128,18 +165,37 @@ export class IaraSyncfusionAdapter
this.undo();
}

if (inference.richTranscriptModifiers?.length) {
if (
inference.richTranscriptModifiers?.length &&
inference.richTranscriptWithoutModifiers
) {
const phraseOrTemplate =
this._recognition.richTranscriptTemplates.templates[
inference.richTranscriptModifiers[0]
];
const metadata = phraseOrTemplate.metadata as { category?: string };
if (metadata.category === "Template" || !metadata.category) {
const removeDivTags = inference.richTranscript
.replace(/^<div>/, "")
const index: number | undefined =
inference.richTranscriptWithoutModifiers.match(
new RegExp(
`iara texto ${inference.richTranscriptModifiers[0]}`,
"ui"
)
)?.index;

const templatePrefix = inference.richTranscript
.slice(0, index)
.replace(/^<div>/, "");
const template = inference.richTranscript
.slice(index)
.replace(/<\/div>$/, "");
const removeDivParagraph = removeDivTags.replace(/(<\/div><div>)/, "");
this.insertTemplate(removeDivParagraph);

this.insertInference({
...inference,
...{ richTranscript: templatePrefix, richTranscriptModifiers: [] },
});
this.insertTemplate(template);

return;
}
}
Expand Down Expand Up @@ -196,11 +252,11 @@ export class IaraSyncfusionAdapter
}

private async _saveReport(): Promise<void> {
this.savingReportSpan.innerText = "Salvo";
this._updateReport(
await this._contentManager.getPlainTextContent(),
await this._contentManager.getHtmlContent()
);
this.savingReportSpan.innerText = "Salvo";
}

onTemplateSelectedAtShortCut(
Expand Down
2 changes: 1 addition & 1 deletion src/tinymce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class IaraTinyMCEAdapter extends EditorAdapter implements EditorAdapter {
this._editorContainer.undoManager.undo();
}

copyReport(): void {
copyReport(): Promise<void> {
throw new Error("Método não implementado.");
}

Expand Down

0 comments on commit 2e45e8c

Please sign in to comment.