Skip to content

Commit

Permalink
[WIP] feat: revert selection format after checking for boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
dsouza95 committed Dec 21, 2023
1 parent 89332c9 commit be80957
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 49 deletions.
62 changes: 29 additions & 33 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 @@ -70,49 +68,52 @@ export class IaraSyncfusionAdapter
}

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

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();
if (this._editorContainer.documentEditor.selection.text.length)
this._editorContainer.documentEditor.editor.delete();
this._initialUndoStackSize = this.getUndoStackSize();
} else {
const undoStackSize = this.getUndoStackSize();
Expand All @@ -128,18 +129,13 @@ 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
this._selectionManager = new IaraSyncfusionInferenceSelectionManager(
this._editorContainer.documentEditor
);

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

const text = this._inferenceFormatter.format(
inference,
wordBefore,
Expand All @@ -156,7 +152,7 @@ export class IaraSyncfusionAdapter
}

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

private _debounce = (func: () => unknown) => {
Expand Down
99 changes: 83 additions & 16 deletions src/syncfusion/selection.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,98 @@
import type {
DocumentEditorContainer,
Selection,
BaselineAlignment,
DocumentEditor,
HighlightColor,
SelectionCharacterFormat,
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: DocumentEditor) {
const characterFormat = this._editor.selection.characterFormat;
this._initialSelectionData = {
characterFormat: {
allCaps: characterFormat.allCaps,
baselineAlignment: characterFormat.baselineAlignment,
bold: characterFormat.allCaps,
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,
};
}

constructor(private _editor: DocumentEditorContainer) {}
public getWordAfterSelection(): string {
this._editor.selection.extendToWordEnd();
const wordAfter = this._editor.selection.text.trimEnd();

this._resetSelection();

public getWordAfterSelection(selectionOffsets: SelectionOffsets): string {
this.selection.extendToWordEnd();
const wordAfter = this.selection.text.trimEnd();
this.selection.select(selectionOffsets.start, selectionOffsets.end);
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();

this._resetSelection();

return wordBefore;
}

private _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",
];

if (this._initialSelectionData.characterFormat.bold) {
// this._initialSelectionData.characterFormat.bold = false;
}

charFormatProps.forEach(prop => {
(this._editor.selection.characterFormat as any)[prop] =
this._initialSelectionData.characterFormat[prop];
});
}
}

0 comments on commit be80957

Please sign in to comment.