Skip to content

Commit

Permalink
Merge branch 'dev' into feature/PRT-1761-1
Browse files Browse the repository at this point in the history
  • Loading branch information
ItalloDornelas committed Dec 21, 2023
2 parents 2c06c89 + ddef06c commit 004ea28
Show file tree
Hide file tree
Showing 11 changed files with 465 additions and 339 deletions.
495 changes: 278 additions & 217 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
},
"peerDependencies": {
"@syncfusion/ej2-documenteditor": ">=23.2",
"@syncfusion/ej2-locale": ">=23.1",
"tinymce": ">=6.5"
},
"peerDependenciesMeta": {
Expand Down
4 changes: 3 additions & 1 deletion src/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export abstract class EditorAdapter {
];

constructor(
protected _editor: DocumentEditorContainer | TinymceEditor,
protected _editorContainer: DocumentEditorContainer | TinymceEditor,
protected _recognition: IaraSpeechRecognition
) {
this._inferenceFormatter = new IaraEditorInferenceFormatter();
Expand Down Expand Up @@ -71,9 +71,11 @@ export abstract class EditorAdapter {

private _initCommands(): void {
this._recognition.commands.add("iara copiar laudo", () => {
this._recognition.stop();
this.copyReport();
});
this._recognition.commands.add("iara finalizar laudo", () => {
this._recognition.stop();
this.finishReport();
});
this._recognition.commands.add("iara negrito", () => {
Expand Down
13 changes: 5 additions & 8 deletions src/syncfusion/content.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DocumentEditorContainer } from "@syncfusion/ej2-documenteditor";
import { DocumentEditor } from "@syncfusion/ej2-documenteditor";
import { IaraSpeechRecognition } from "../speech";

export class IaraSFDT {
Expand All @@ -7,11 +7,8 @@ export class IaraSFDT {

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

static async fromEditor(
editor: DocumentEditorContainer,
authHeaders: HeadersInit
) {
const value: string = await editor.documentEditor
static async fromEditor(editor: DocumentEditor, authHeaders: HeadersInit) {
const value: string = await editor
.saveAsBlob("Sfdt")
.then((blob: Blob) => blob.text());
return new IaraSFDT(value, authHeaders);
Expand Down Expand Up @@ -108,7 +105,7 @@ export class IaraSyncfusionEditorContentManager {
private _sfdt: IaraSFDT | undefined;

constructor(
private _editor: DocumentEditorContainer,
private _editor: DocumentEditor,
private _recognition: IaraSpeechRecognition,
onContentChange: () => void
) {
Expand Down Expand Up @@ -160,7 +157,7 @@ export class IaraSyncfusionEditorContentManager {

private async _getPlainTextContent(): Promise<string> {
if (this._isPlainTextDirty) {
this._plainText = await this._editor.documentEditor
this._plainText = await this._editor
.saveAsBlob("Txt")
.then((blob: Blob) => blob.text());
this._isPlainTextDirty = false;
Expand Down
75 changes: 40 additions & 35 deletions src/syncfusion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Dialog } from "@syncfusion/ej2-popups";
import { EditorAdapter } from "../editor";
import { IaraSpeechRecognition, IaraSpeechRecognitionDetail } from "../speech";
import { IaraSFDT, IaraSyncfusionEditorContentManager } from "./content";
import { IaraSyncfusionSelectionManager } from "./selection";
import { IaraSyncfusionSelectionManager as IaraSyncfusionInferenceSelectionManager } from "./selection";
import { IaraSyncfusionShortcutsManager } from "./shortcuts";
import { IaraSyncfusionStyleManager } from "./style";
import { IaraSyncfusionToolbarManager } from "./toolbar";
Expand All @@ -16,7 +16,7 @@ export class IaraSyncfusionAdapter
private _contentManager: IaraSyncfusionEditorContentManager;
private _debouncedSaveReport: () => void;
private _initialUndoStackSize = 0;
private _selectionManager: IaraSyncfusionSelectionManager;
private _selectionManager?: IaraSyncfusionInferenceSelectionManager;
private _shortcutsManager: IaraSyncfusionShortcutsManager;
private _toolbarManager: IaraSyncfusionToolbarManager;

Expand All @@ -30,35 +30,33 @@ export class IaraSyncfusionAdapter
}

constructor(
protected _editor: DocumentEditorContainer,
protected _editorContainer: DocumentEditorContainer,
protected _recognition: IaraSpeechRecognition,
replaceToolbar = false
) {
super(_editor, _recognition);
super(_editorContainer, _recognition);
this._contentManager = new IaraSyncfusionEditorContentManager(
_editor,
_editorContainer.documentEditor,
_recognition,
this._onContentChange.bind(this)
);

this._selectionManager = new IaraSyncfusionSelectionManager(_editor);
this._shortcutsManager = new IaraSyncfusionShortcutsManager(
_editor,
_editorContainer.documentEditor,
_recognition,
this.onTemplateSelectedAtShortCut.bind(this)
);
this._shortcutsManager.init();
this._styleManager = new IaraSyncfusionStyleManager(
_editor,
this._selectionManager
_editorContainer.documentEditor
);
this._toolbarManager = new IaraSyncfusionToolbarManager(_editor);
this._toolbarManager = new IaraSyncfusionToolbarManager(_editorContainer);

if (replaceToolbar) this._toolbarManager.init();

this._debouncedSaveReport = this._debounce(this._saveReport.bind(this));

this._editor.addEventListener(
this._editorContainer.addEventListener(
"destroyed",
this._onEditorDestroyed.bind(this)
);
Expand All @@ -69,47 +67,58 @@ export class IaraSyncfusionAdapter
if (wrapper) wrapper.style.cursor = status ? "not-allowed" : "auto";
}

copyReport(): void {
this._selectionManager.selection.selectAll();
this._selectionManager.selection.copySelectedContent(false);
async copyReport(): Promise<void> {
this._editorContainer.documentEditor.focusIn();
this._editorContainer.documentEditor.selection.selectAll();
this._recognition.automation.copyText(
...(await this._contentManager.getContent())
);
this._editorContainer.documentEditor.selection.moveNextPosition();
}

clearReport(): void {
this._selectionManager.selection.selectAll();
this._editor.documentEditor.editor.delete();
this._editorContainer.documentEditor.selection.selectAll();
this._editorContainer.documentEditor.editor.delete();
}

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

getUndoStackSize(): number {
return this._editor.documentEditor.editorHistory.undoStack?.length || 0;
return (
this._editorContainer.documentEditor.editorHistory.undoStack?.length || 0
);
}

insertParagraph(): void {
this._editor.documentEditor.editor.insertText("\n");
this._editorContainer.documentEditor.editor.insertText("\n");
}

async insertTemplate(html: string, replaceAllContent = false): Promise<void> {
const sfdt = await IaraSFDT.fromHtml(
html,
this._recognition.internal.iaraAPIMandatoryHeaders as HeadersInit
);
if (replaceAllContent) this._editor.documentEditor.open(sfdt.value);
else this._editor.documentEditor.editor.paste(sfdt.value);
if (replaceAllContent)
this._editorContainer.documentEditor.open(sfdt.value);
else this._editorContainer.documentEditor.editor.paste(sfdt.value);
}

insertText(text: string): void {
this._editor.documentEditor.editor.insertText(text);
this._editorContainer.documentEditor.editor.insertText(text);
}

insertInference(inference: IaraSpeechRecognitionDetail): void {
if (inference.richTranscriptModifiers?.length && !inference.isFinal) return;

if (inference.isFirst) {
if (this._selectionManager.selection.text.length)
this._editor.documentEditor.editor.delete();
this._selectionManager = new IaraSyncfusionInferenceSelectionManager(
this._editorContainer.documentEditor
);

if (this._editorContainer.documentEditor.selection.text.length)
this._editorContainer.documentEditor.editor.delete();
this._initialUndoStackSize = this.getUndoStackSize();
} else {
const undoStackSize = this.getUndoStackSize();
Expand All @@ -125,17 +134,11 @@ export class IaraSyncfusionAdapter
return;
}

// Syncfusion formatter
const initialSelectionOffsets = {
end: this._selectionManager.selection.endOffset,
start: this._selectionManager.selection.startOffset,
};
const wordBefore = this._selectionManager.getWordBeforeSelection(
initialSelectionOffsets
);
const wordAfter = this._selectionManager.getWordAfterSelection(
initialSelectionOffsets
);
if (!this._selectionManager) return;

const wordBefore = this._selectionManager.getWordBeforeSelection();
const wordAfter = this._selectionManager.getWordAfterSelection();
this._selectionManager.resetSelection();

const text = this._inferenceFormatter.format(
inference,
Expand All @@ -150,10 +153,12 @@ export class IaraSyncfusionAdapter
line = line.trimStart();
if (line) this.insertText(line);
});

if (inference.isFinal) this._selectionManager = undefined;
}

undo(): void {
this._editor.documentEditor.editorHistory.undo();
this._editorContainer.documentEditor.editorHistory.undo();
}

private _debounce = (func: () => unknown) => {
Expand Down
90 changes: 73 additions & 17 deletions src/syncfusion/selection.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,87 @@
import type {
DocumentEditorContainer,
Selection,
BaselineAlignment,
DocumentEditor,
HighlightColor,
Strikethrough,
Underline,
} from "@syncfusion/ej2-documenteditor";

interface SelectionOffsets {
end: string;
start: string;
interface SelectionData {
characterFormat: SelectionCharacterFormatData;
endOffset: string;
startOffset: string;
}

interface SelectionCharacterFormatData {
allCaps: boolean;
baselineAlignment: BaselineAlignment;
bold: boolean;
fontColor: string;
fontFamily: string;
fontSize: number;
highlightColor: HighlightColor;
italic: boolean;
strikethrough: Strikethrough;
underline: Underline;
}

export class IaraSyncfusionSelectionManager {
public get selection(): Selection {
return this._editor.documentEditor.selection;
}
private _initialSelectionData: SelectionData;

constructor(private _editor: DocumentEditorContainer) {}
constructor(private _editor: DocumentEditor) {
const characterFormat = this._editor.selection.characterFormat;
this._initialSelectionData = {
characterFormat: {
allCaps: characterFormat.allCaps,
baselineAlignment: characterFormat.baselineAlignment,
bold: characterFormat.bold,
fontColor: characterFormat.fontColor,
fontFamily: characterFormat.fontFamily,
fontSize: characterFormat.fontSize,
highlightColor: characterFormat.highlightColor,
italic: characterFormat.italic,
strikethrough: characterFormat.strikethrough,
underline: characterFormat.underline,
},
endOffset: this._editor.selection.endOffset,
startOffset: this._editor.selection.startOffset,
};
}

public getWordAfterSelection(selectionOffsets: SelectionOffsets): string {
this.selection.extendToWordEnd();
const wordAfter = this.selection.text.trimEnd();
this.selection.select(selectionOffsets.start, selectionOffsets.end);
public getWordAfterSelection(): string {
this._editor.selection.extendToWordEnd();
const wordAfter = this._editor.selection.text.trimEnd();
return wordAfter;
}

public getWordBeforeSelection(selectionOffsets: SelectionOffsets): string {
this.selection.extendToWordStart();
const wordBefore = this.selection.text.trimStart();
this.selection.select(selectionOffsets.start, selectionOffsets.end);
public getWordBeforeSelection(): string {
this._editor.selection.extendToWordStart();
const wordBefore = this._editor.selection.text.trimStart();
return wordBefore;
}

public resetSelection() {
this._editor.selection.select(
this._initialSelectionData.startOffset,
this._initialSelectionData.endOffset
);

const charFormatProps: (keyof SelectionCharacterFormatData)[] = [
"allCaps",
"baselineAlignment",
"bold",
"fontColor",
"fontFamily",
"fontSize",
"highlightColor",
"italic",
"strikethrough",
"underline",
];

charFormatProps.forEach(prop => {
(this._editor.selection.characterFormat as any)[prop] =
this._initialSelectionData.characterFormat[prop];
});
}
}
6 changes: 3 additions & 3 deletions src/syncfusion/shortcuts/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
DocumentEditorContainer,
DocumentEditor,
DocumentEditorKeyDownEventArgs,
} from "@syncfusion/ej2-documenteditor";
import { ListView } from "@syncfusion/ej2-lists";
Expand All @@ -9,7 +9,7 @@ import { IaraSyncfusionTemplateSearch } from "./templateSearch";

export class IaraSyncfusionShortcutsManager {
constructor(
private _editor: DocumentEditorContainer,
private _editor: DocumentEditor,
private _recognition: IaraSpeechRecognition,
private onTemplateSelected: (
listViewInstance: ListView,
Expand All @@ -18,7 +18,7 @@ export class IaraSyncfusionShortcutsManager {
) {}

init(): void {
this._editor.documentEditor.keyDown = this.onKeyDown.bind(this);
this._editor.keyDown = this.onKeyDown.bind(this);
}
onKeyDown(args: DocumentEditorKeyDownEventArgs): void {
const key: string = args.event.key;
Expand Down
Loading

0 comments on commit 004ea28

Please sign in to comment.