Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/prt 1830 #58

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export abstract class EditorAdapter {
abstract clearReport(): void;
abstract copyReport(): Promise<void>;
abstract insertInference(inference: IaraSpeechRecognitionDetail): void;
abstract getEditorContent(): Promise<[string, string, string]>;
abstract getEditorContent(): Promise<[string, string, string, string?]>;

beginReport(): string | void {
if (!this._shouldSaveReport) return;
Expand Down
28 changes: 27 additions & 1 deletion src/syncfusion/content.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
import { DocumentEditor } from "@syncfusion/ej2-documenteditor";
import { IaraSpeechRecognition } from "../speech";

export enum IaraSyncfusionContentTypes {
SFDT = "SFDT",
HTML = "HTML",
RTF = "RTF",
}

export class IaraSFDT {
public html: string | undefined;
public rtf: string | undefined;

constructor(public value: string, private _authHeaders: HeadersInit) {}

static detectContentType(content: string): IaraSyncfusionContentTypes {
if (content.startsWith("{\\rtf")) return IaraSyncfusionContentTypes.RTF;
else if (content.startsWith("{")) return IaraSyncfusionContentTypes.SFDT;
else if (content.startsWith("<")) return IaraSyncfusionContentTypes.HTML;
else throw new Error("Content type not recognized.");
}

static async fromContent(content: string, authHeaders: HeadersInit) {
const contentType = IaraSFDT.detectContentType(content);
switch (contentType) {
case IaraSyncfusionContentTypes.SFDT:
return new IaraSFDT(content, authHeaders);
case IaraSyncfusionContentTypes.HTML:
return IaraSFDT.fromHtml(content, authHeaders);
case IaraSyncfusionContentTypes.RTF:
return IaraSFDT.fromRtf(content, authHeaders);
}
}

static async fromEditor(editor: DocumentEditor, authHeaders: HeadersInit) {
const value: string = await editor
.saveAsBlob("Sfdt")
Expand Down Expand Up @@ -115,12 +140,13 @@ export class IaraSyncfusionEditorContentManager {
};
}

async getContent(): Promise<[string, string, string]> {
async getContent(): Promise<[string, string, string, string]> {
const sfdt = await this._getSfdtContent();
return Promise.all([
this.getPlainTextContent(),
sfdt.toHtml(),
sfdt.toRtf(),
sfdt.value,
]);
}

Expand Down
54 changes: 29 additions & 25 deletions src/syncfusion/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { DocumentEditorContainer } from "@syncfusion/ej2-documenteditor";
import { ListView, SelectedCollection } from "@syncfusion/ej2-lists";
import {
Dialog,
createSpinner,
showSpinner,
hideSpinner,
showSpinner,
} 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";
Expand All @@ -19,16 +19,15 @@ export class IaraSyncfusionAdapter
implements EditorAdapter
{
private _contentManager: IaraSyncfusionEditorContentManager;
private _contentDate?: Date;
private _cursorSelection?: IaraSyncfusionSelectionManager;
private _debouncedSaveReport: () => void;
private _initialUndoStackSize = 0;
private _resetSelection = false;
private _selectionManager?: IaraSyncfusionSelectionManager;
private _shortcutsManager: IaraSyncfusionShortcutsManager;
private _toolbarManager: IaraSyncfusionToolbarManager;

private _resetSelection = false;

private _cursorSelection?: IaraSyncfusionSelectionManager;

protected _styleManager: IaraSyncfusionStyleManager;

public savingReportSpan = document.createElement("span");
Expand All @@ -49,7 +48,7 @@ export class IaraSyncfusionAdapter
this._contentManager = new IaraSyncfusionEditorContentManager(
_editorContainer.documentEditor,
_recognition,
() => (this._shouldSaveReport ? this._onContentChange() : undefined)
() => (this._shouldSaveReport ? this._debouncedSaveReport() : undefined)
);

this._shortcutsManager = new IaraSyncfusionShortcutsManager(
Expand Down Expand Up @@ -108,9 +107,8 @@ export class IaraSyncfusionAdapter
this._editorContainer.documentEditor.focusIn();
this._editorContainer.documentEditor.selection.selectAll();
showSpinner(this._editorContainer.editorContainer);
this._recognition.automation.copyText(
...(await this._contentManager.getContent())
);
const content = await this._contentManager.getContent();
this._recognition.automation.copyText(content[0], content[1], content[2]);
hideSpinner(this._editorContainer.editorContainer);
this._editorContainer.documentEditor.selection.moveNextPosition();
}
Expand All @@ -120,7 +118,7 @@ export class IaraSyncfusionAdapter
this._editorContainer.documentEditor.editor.delete();
}

getEditorContent(): Promise<[string, string, string]> {
getEditorContent(): Promise<[string, string, string, string]> {
return this._contentManager.getContent();
}

Expand All @@ -134,9 +132,12 @@ export class IaraSyncfusionAdapter
this._editorContainer.documentEditor.editor.insertText("\n");
}

async insertTemplate(html: string, replaceAllContent = false): Promise<void> {
const sfdt = await IaraSFDT.fromHtml(
html,
async insertTemplate(
content: string,
replaceAllContent = false
): Promise<void> {
const sfdt = await IaraSFDT.fromContent(
content,
this._recognition.internal.iaraAPIMandatoryHeaders as HeadersInit
);
if (replaceAllContent)
Expand Down Expand Up @@ -227,17 +228,20 @@ export class IaraSyncfusionAdapter
this._editorContainer.documentEditor.editorHistory.undo();
}

private _debounce = (func: () => unknown) => {
private _debounce(func: () => unknown) {
let timer: ReturnType<typeof setTimeout>;
return () => {
clearTimeout(timer);
timer = setTimeout(() => {
func();
}, 1000);
};
};
}

private async _saveReport(): Promise<void> {
const contentDate = new Date();
this._contentDate = contentDate;

private async _onContentChange(): Promise<void> {
const element = document.querySelector(".e-de-status-bar");
if (element) {
this.savingReportSpan.style.margin = "10px";
Expand All @@ -248,14 +252,14 @@ export class IaraSyncfusionAdapter
this.savingReportSpan.innerText = "Salvando...";
element.insertBefore(this.savingReportSpan, element.firstChild);
}
this._debouncedSaveReport();
}

private async _saveReport(): Promise<void> {
this._updateReport(
await this._contentManager.getPlainTextContent(),
await this._contentManager.getHtmlContent()
);
const content: string[] = await Promise.all([
this._contentManager.getPlainTextContent(),
this._contentManager.getHtmlContent(),
]);
if (contentDate !== this._contentDate) return;

await this._updateReport(content[0], content[1]);
this.savingReportSpan.innerText = "Salvo";
}

Expand Down