Skip to content

Commit

Permalink
fix: redefine the style to get the style of the previous inference (#145
Browse files Browse the repository at this point in the history
)

* fix: redefine the style to get the style of the previous inference

* remove log

* fix: remove unusual import
  • Loading branch information
ItalloDornelas authored Jun 24, 2024
1 parent 85d58c0 commit 9b5b146
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 75 deletions.
19 changes: 3 additions & 16 deletions src/syncfusion/bookmarks/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
import { DocumentEditor } from "@syncfusion/ej2-documenteditor";
import { IaraSyncfusionConfig } from "..";
import { v4 as uuidv4 } from "uuid";

export class IaraSyncfusionBookmarkManager {
private _bookmarks: { name: string; content: string; inferenceId: string }[] =
[];

constructor(
private _documentEditor: DocumentEditor,
private _config: IaraSyncfusionConfig
) {}
constructor(private _documentEditor: DocumentEditor) {}

insertInferenceField(
isFirstInference: boolean,
isFinalInference: boolean
): void {
if (isFirstInference) {
this._documentEditor.editor.insertText(" ");
this._documentEditor.editor.insertBookmark(`inferenceId_${uuidv4()}`);
if (this._config.highlightInference) {
this._config.darkMode
? // eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
(this._documentEditor.selection.characterFormat.highlightColor =
"#0e5836")
: // eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
(this._documentEditor.selection.characterFormat.highlightColor =
"#ccffe5");
}
this._documentEditor.selection.movePreviousPosition();
}
if (isFinalInference) this.getBookmarks();
}
Expand Down
57 changes: 17 additions & 40 deletions src/syncfusion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ export class IaraSyncfusionAdapter
);

this._bookmarkManager = new IaraSyncfusionBookmarkManager(
this._documentEditor,
this._config
this._documentEditor
);

if (this._config.replaceToolbar && this._editorContainer) {
Expand Down Expand Up @@ -269,9 +268,8 @@ export class IaraSyncfusionAdapter
hideSpinner(this._documentEditor.editor.documentHelper.viewerContainer);
}

insertParagraph(setStyle = false): void {
insertParagraph(): void {
this._documentEditor.editor.insertText("\n");
if (setStyle) this._inferencehighlightColor();
}

async insertTemplate(
Expand Down Expand Up @@ -305,11 +303,12 @@ export class IaraSyncfusionAdapter
this._navigationFieldManager.nextField();
}

insertText(text: string): void {
insertText(text: string, resetSytle = false): void {
if (resetSytle) this._selectionManager?.resetStyles();
const [firstLine, ...lines]: string[] = text.split("\n");
this._documentEditor.editor.insertText(firstLine);
lines.forEach(line => {
this.insertParagraph(true);
this.insertParagraph();
line = line.trimStart();
if (line) this._documentEditor.editor.insertText(line);
});
Expand All @@ -323,28 +322,12 @@ export class IaraSyncfusionAdapter
if (inference.isFirst) {
this._handleFirstInference();
} else if (this._selectionManager) {
let initialStartOffset =
this._selectionManager.initialSelectionData.startOffset;

if (this._selectionManager.isAtStartOfLine) {
if (initialStartOffset.split(";")[2] === "0") {
initialStartOffset = `${initialStartOffset.split(";")[0]};${
initialStartOffset.split(";")[1]
};1`;
}
}

this._documentEditor.selection.select(
initialStartOffset,
this._selectionManager.initialSelectionData.startOffset,
this._inferenceEndOffset
);
}

this._bookmarkManager.insertInferenceField(
inference.isFirst,
inference.isFinal
);

if (!this._selectionManager) return;

if (
Expand All @@ -361,7 +344,12 @@ export class IaraSyncfusionAdapter
this._selectionManager.isAtStartOfLine
);

if (text.length) this.insertText(text);
this._bookmarkManager.insertInferenceField(
inference.isFirst,
inference.isFinal
);

if (text.length) this.insertText(text, true);
else this._documentEditor.editor.delete();

this._inferenceEndOffset = this._documentEditor.selection.endOffset;
Expand Down Expand Up @@ -484,6 +472,7 @@ export class IaraSyncfusionAdapter
if (event.button === 1) {
this._cursorSelection = new IaraSyncfusionSelectionManager(
this._documentEditor,
this._config,
false
);
}
Expand Down Expand Up @@ -523,14 +512,16 @@ export class IaraSyncfusionAdapter
this._documentEditor.editor.delete();
}
this._selectionManager = new IaraSyncfusionSelectionManager(
this._documentEditor
this._documentEditor,
this._config
);

if (this._selectionManager.wordBeforeSelection.endsWith(" ")) {
this._documentEditor.selection.extendBackward();
this._documentEditor.editor.delete();
this._selectionManager = new IaraSyncfusionSelectionManager(
this._documentEditor
this._documentEditor,
this._config
);
}
}
Expand Down Expand Up @@ -573,18 +564,4 @@ export class IaraSyncfusionAdapter

return false;
}

private _inferencehighlightColor(): void {
if (this._config.highlightInference) {
this._config.darkMode
? // eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
(this._documentEditor.selection.characterFormat.highlightColor =
"#0e5836")
: // eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
(this._documentEditor.selection.characterFormat.highlightColor =
"#ccffe5");
}
}
}
58 changes: 40 additions & 18 deletions src/syncfusion/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
Strikethrough,
Underline,
} from "@syncfusion/ej2-documenteditor";
import { IaraSyncfusionConfig } from ".";

interface SelectionData {
characterFormat: SelectionCharacterFormatData;
Expand All @@ -31,8 +32,13 @@ export class IaraSyncfusionSelectionManager {
public wordBeforeSelection = "";
public isAtStartOfLine = false;

constructor(private _editor: DocumentEditor, getSurrondingWords = true) {
constructor(
private _editor: DocumentEditor,
private _config: IaraSyncfusionConfig,
getSurrondingWords = true
) {
const characterFormat = this._editor.selection.characterFormat;
this._inferencehighlightColor();
this.initialSelectionData = {
characterFormat: {
allCaps: characterFormat.allCaps,
Expand Down Expand Up @@ -95,29 +101,45 @@ export class IaraSyncfusionSelectionManager {
return wordBefore;
}

private _inferencehighlightColor(): void {
if (this._config.highlightInference) {
this._config.darkMode
? // eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
(this._editor.selection.characterFormat.highlightColor = "#0e5836")
: // eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
(this._editor.selection.characterFormat.highlightColor = "#ccffe5");
}
}

public resetSelection(resetStyles = true): void {
this._editor.selection.select(
this.initialSelectionData.startOffset,
this.initialSelectionData.endOffset
);
if (resetStyles) {
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];
});
this.resetStyles();
}
}

public resetStyles(): void {
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];
});
}
}
2 changes: 1 addition & 1 deletion src/syncfusion/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class IaraSyncfusionStyleManager extends IaraEditorStyleManager {
});

// this.zoomInterval = setInterval(() => {
this.setZoomFactor(this._config.zoomFactor ?? "100%");
this.setZoomFactor(this._config.zoomFactor ?? "100%");
// }, 100);
}

Expand Down

0 comments on commit 9b5b146

Please sign in to comment.