Skip to content

Commit

Permalink
Merge pull request #3 from iarahealth/tinymce
Browse files Browse the repository at this point in the history
feat: add simple tinyMCE adapter
  • Loading branch information
dsouza95 authored Jul 7, 2023
2 parents 3aad70d + c80c9f2 commit 6f765b3
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 20 deletions.
6 changes: 5 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@
"vitest": "^0.29.8"
},
"peerDependencies": {
"@syncfusion/ej2-documenteditor": ">=22.1"
"@syncfusion/ej2-documenteditor": ">=22.1",
"tinymce": ">=6.5"
},
"peerDependenciesMeta": {
"@syncfusion/ej2-documenteditor": {
"optional": true
},
"tinymce": {
"optional": true
}
}
}
2 changes: 1 addition & 1 deletion src/editor/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IaraInference } from "../speech";

export abstract class EditorAdapter {
constructor(protected _recognition: any) {
constructor(protected _editor: any, protected _recognition: any) {
_recognition.addEventListener(
"iaraSpeechRecognitionResult",
(event: { detail: IaraInference }) => {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./syncfusion";
export * from "./tinymce";
23 changes: 6 additions & 17 deletions src/syncfusion/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import type {
DocumentEditor,
Editor,
EditorHistory,
} from "@syncfusion/ej2-documenteditor";
import type { Editor, EditorHistory } from "@syncfusion/ej2-documenteditor";
import { EditorAdapter } from "../editor";
import { IaraInference } from "../speech";

Expand All @@ -12,31 +8,24 @@ export class IaraSyncfusionAdapter
{
private _initialUndoStackSize = 0;

private get _editor(): Editor {
return this._documentEditor.editor;
private get _editorAPI(): Editor {
return this._editor.editor;
}

private get _editorHistory(): EditorHistory {
return this._documentEditor.editorHistory;
}

constructor(
protected _recognition: any,
private _documentEditor: DocumentEditor
) {
super(_recognition);
return this._editor.editorHistory;
}

getUndoStackSize(): number {
return this._editorHistory.undoStack?.length || 0;
}

insertParagraph() {
this._editor.insertText("\n");
this._editorAPI.insertText("\n");
}

insertText(text: string) {
this._editor.insertText(text);
this._editorAPI.insertText(text);
}

insertInference(inference: IaraInference) {
Expand Down
49 changes: 49 additions & 0 deletions src/tinymce/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { EditorAdapter } from "../editor";
import { IaraInference } from "../speech";

export class IaraTinyMCEAdapter extends EditorAdapter implements EditorAdapter {
private _initialUndoStackSize = 0;

private get _editorAPI() {
return this._editor.activeEditor;
}

getUndoStackSize(): number {
return this._editorAPI.undoManager.data.length || 0;
}

insertInference(inference: IaraInference): void {
if (inference.isFirst) {
this._initialUndoStackSize = this.getUndoStackSize();
} else {
const undoStackSize = this.getUndoStackSize();
for (let i = 0; i < undoStackSize - this._initialUndoStackSize; i++)
this.undo();
}

const text = inference.richTranscript
.replace(/^<div>/, "")
.replace(/<\/div>$/, "");
const [firstLine, ...lines]: string[] = text.split("</div><div>");

this.insertText(`${firstLine}\n`);

lines.forEach(line => {
this.insertParagraph();
line = line.trim();
if (line) this.insertText(line);
});
}

insertParagraph() {
this._editorAPI.execCommand("InsertParagraph");
}

insertText(text: string) {
this._editorAPI.insertContent(text);
}

undo() {
this._editorAPI.undoManager.undo();
}
}

0 comments on commit 6f765b3

Please sign in to comment.