Skip to content

Commit

Permalink
feat: add selectBookmark method with scroll config (#166)
Browse files Browse the repository at this point in the history
* feat: add selectBookmark method with scroll config

* refactor: remove unused imports from navigation fields

* fix: use our custom selectBookmark and fix insertion inside bookmarks

* fix: content navigation

---------

Co-authored-by: Itallo <dornelaszxd@gmail.com>
  • Loading branch information
dsouza95 and ItalloDornelas authored Jul 31, 2024
1 parent 24ccc55 commit 2a22ddb
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 158 deletions.
44 changes: 14 additions & 30 deletions src/syncfusion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,19 @@ export class IaraSyncfusionAdapter
);

if (text.length) this.insertText(text, true);
else if (inference.isFinal) {
this._documentEditor.selection.selectBookmark(
this._selectionManager.initialSelectionData.bookmarkId,
false
);
this._documentEditor.editor.delete();

if (inference.isFinal) {
if (text.length) {
this._selectionManager.moveSelectionToAfterBookmarkEdge(
this._selectionManager.initialSelectionData.bookmarkId
);
} else {
this._selectionManager.selectBookmark(
this._selectionManager.initialSelectionData.bookmarkId,
false
);
this._documentEditor.editor.delete();
}
}
}

Expand Down Expand Up @@ -552,9 +559,7 @@ export class IaraSyncfusionAdapter
this._updateSelectedNavigationField(this._documentEditor.selection.text);
const hadSelectedText = this._documentEditor.selection.text.length

if (hadSelectedText) {
this._documentEditor.editor.delete();
}
if (hadSelectedText) this._documentEditor.editor.delete();

this._selectionManager = new IaraSyncfusionSelectionManager(
this._documentEditor,
Expand Down Expand Up @@ -631,25 +636,4 @@ export class IaraSyncfusionAdapter

return false;
}

protected _onIaraCommand(command: string): void {
// When using the speech command, we may get an empty bookmark.
// If that is the case, remove it before processing the command
let selectionBookmarks = this._documentEditor.selection.getBookmarks();
const emptyBookmark = selectionBookmarks.find(bookmark => {
this._documentEditor.selection.selectBookmark(bookmark);
return this._documentEditor.selection.text.length === 0;
});

if (emptyBookmark) {
this._documentEditor.editor.delete();
} else {
this._selectionManager?.resetSelection();
this._selectionManager?.moveSelectionToAfterBookmarkEdge(
this._selectionManager.initialSelectionData.bookmarkId
);
}

super._onIaraCommand(command);
}
}
3 changes: 2 additions & 1 deletion src/syncfusion/inferenceBookmarks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
IaraSpeechRecognition,
IaraSpeechRecognitionDetail,
} from "../../speech";
import { IaraSyncfusionSelectionManager } from "../selection";

export interface IaraInferenceBookmark {
content: string;
Expand Down Expand Up @@ -34,7 +35,7 @@ export class IaraSyncfusionInferenceBookmarksManager {

private _updateBookmarkContent(bookmarkName: string): void {
if (!(bookmarkName in this._bookmarks)) return;
this._documentEditor.selection.selectBookmark(bookmarkName, true);
IaraSyncfusionSelectionManager.selectBookmark(this._documentEditor, bookmarkName, true);
this._bookmarks[bookmarkName]["content"] =
this._documentEditor.selection.text;
}
Expand Down
145 changes: 49 additions & 96 deletions src/syncfusion/navigationFields/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import {
BookmarkElementBox,
Dictionary,
DocumentEditor,
TextPosition,
DocumentEditor
} from "@syncfusion/ej2-documenteditor";
import { v4 as uuidv4 } from "uuid";
import { IaraSyncfusionConfig } from "..";
import { IaraEditorNavigationFieldManager } from "../../editor/navigationFields";
import { IaraSpeechRecognition } from "../../speech";
import { v4 as uuidv4 } from "uuid";
import { IaraNavigationBookmark } from "./navigationBookmark";
import { IaraSyncfusionAdditiveFieldModal } from "./additiveFieldModal";
import { IaraSyncfusionLanguageManager } from "../language";
import { IaraSyncfusionSelectionManager } from "../selection";
import { IaraSyncfusionAdditiveFieldModal } from "./additiveFieldModal";
import { IaraNavigationBookmark } from "./navigationBookmark";

export class IaraSyncfusionNavigationFieldManager extends IaraEditorNavigationFieldManager{
export class IaraSyncfusionNavigationFieldManager extends IaraEditorNavigationFieldManager {
previousBookmark: IaraNavigationBookmark = {} as IaraNavigationBookmark;
nextBookmark: IaraNavigationBookmark = {} as IaraNavigationBookmark;
currentSelectionOffset: {
Expand Down Expand Up @@ -92,22 +90,18 @@ export class IaraSyncfusionNavigationFieldManager extends IaraEditorNavigationFi
if (!content.includes("?")) this._documentEditor.editor.insertText(`?`);
}
this.getBookmarks();
this.insertedBookmark = this.bookmarks.filter(bookmark =>
bookmark.name === `${type}-${bookmarksCount}`)[0];
this.isFirstNextNavigation = true;
this.isFirstPreviousNavigation = true;
this.getOffsetsAndSelect(`${type}-${bookmarksCount}`, true);
this.selectBookmark(`${type}-${bookmarksCount}`, true);
this.selectTitle(title, `${type}-${bookmarksCount}`);
}

getBookmarks(setColor = true): void {
const editorBookmarks = this._documentEditor.getBookmarks();
this.updateBookmark(editorBookmarks);
this.removeEmptyField(editorBookmarks);
if (this.isFirstNextNavigation || this.isFirstPreviousNavigation) {
this.insertedBookmark = this.bookmarks.filter(
bookmark =>
bookmark.name === editorBookmarks[editorBookmarks.length - 1]
)[0];
}
this.sortByPosition();

if (setColor) this.setColor();
Expand All @@ -124,10 +118,10 @@ export class IaraSyncfusionNavigationFieldManager extends IaraEditorNavigationFi
bookmark.title !== ""
);
if (bookmarks.length === 1) {
this.getOffsetsAndSelect(bookmarks[0].name);
this.selectBookmark(bookmarks[0].name);
this._previousBookmarksTitles = [];
} else if (bookmarks.length > 1) {
this.getOffsetsAndSelect(
this.selectBookmark(
bookmarks[this._previousBookmarksTitles.length - 1].name
);
} else throw new Error("Method not implemented.");
Expand All @@ -147,7 +141,7 @@ export class IaraSyncfusionNavigationFieldManager extends IaraEditorNavigationFi
this.insertedBookmark.name
);
else {
this.getOffsetsAndSelect(this.nextBookmark.name);
this.selectBookmark(this.nextBookmark.name);
}
this.isFirstNextNavigation = false;
this.currentSelectionOffset = {
Expand All @@ -167,7 +161,7 @@ export class IaraSyncfusionNavigationFieldManager extends IaraEditorNavigationFi
if (isShortcutNavigation && this.isFirstPreviousNavigation)
this.selectTitle(this.insertedBookmark.title, this.insertedBookmark.name);
else {
this.getOffsetsAndSelect(this.previousBookmark.name);
this.selectBookmark(this.previousBookmark.name);
}
this.isFirstPreviousNavigation = false;
this.currentSelectionOffset = {
Expand All @@ -177,7 +171,7 @@ export class IaraSyncfusionNavigationFieldManager extends IaraEditorNavigationFi
}

selectContent(title: string, content: string, bookmarkName: string): void {
this.getOffsetsAndSelect(bookmarkName, true);
this.selectBookmark(bookmarkName, true);
this._documentEditor.selection.select(
this._documentEditor.selection.startOffset,
this._documentEditor.selection.startOffset
Expand All @@ -204,7 +198,7 @@ export class IaraSyncfusionNavigationFieldManager extends IaraEditorNavigationFi
bookmarkName: string,
selectAllTitle?: boolean
): void {
this.getOffsetsAndSelect(bookmarkName, true);
this.selectBookmark(bookmarkName, true);
this._documentEditor.selection.select(
this._documentEditor.selection.startOffset,
this._documentEditor.selection.startOffset
Expand Down Expand Up @@ -411,7 +405,7 @@ export class IaraSyncfusionNavigationFieldManager extends IaraEditorNavigationFi

updateBookmark(editorBookmarks: string[]): void {
editorBookmarks.map(bookmark => {
this.getOffsetsAndSelect(bookmark);
this.selectBookmark(bookmark);
const bookmarkContent = this._documentEditor.selection.text;
const { title, content } = this.getTitleAndContent(bookmarkContent);
this.popAndUpdate(bookmark, content, title);
Expand All @@ -436,24 +430,28 @@ export class IaraSyncfusionNavigationFieldManager extends IaraEditorNavigationFi
const nextField = index === -1 ? this.bookmarks[0] : this.bookmarks[index];
const previousField = this.checkIsSelectedAndUpdatePrevious(previousIndex);

this.previousBookmark = previousField ? previousField : {
name: "",
content: "",
title: "",
offset: {
start: "",
end: "",
},
};
this.nextBookmark = nextField ? nextField : {
name: "",
content: "",
title: "",
offset: {
start: "",
end: "",
},
};
this.previousBookmark = previousField
? previousField
: {
name: "",
content: "",
title: "",
offset: {
start: "",
end: "",
},
};
this.nextBookmark = nextField
? nextField
: {
name: "",
content: "",
title: "",
offset: {
start: "",
end: "",
},
};
}

findCurrentIndex(
Expand All @@ -480,8 +478,6 @@ export class IaraSyncfusionNavigationFieldManager extends IaraEditorNavigationFi
checkIsSelectedAndUpdatePrevious(
previousIndex: number
): IaraNavigationBookmark {
let selected = this.bookmarks[previousIndex];

const compareCurrentOffsetWithPreviousOffset =
this.currentSelectionOffset.start &&
this.currentSelectionOffset.start ===
Expand All @@ -492,72 +488,21 @@ export class IaraSyncfusionNavigationFieldManager extends IaraEditorNavigationFi
this.currentSelectionOffset.start &&
this.currentSelectionOffset.start === this.nextBookmark.offset.start &&
this.currentSelectionOffset.end === this.nextBookmark.offset.end;

const compareCurrentOffsetWithSelectedOffset =
this.previousBookmark.offset.start &&
this.previousBookmark.name !== selected.name &&
previousIndex !== 0;

if (
compareCurrentOffsetWithPreviousOffset ||
compareCurrentOffsetWithNextOffset ||
compareCurrentOffsetWithSelectedOffset
compareCurrentOffsetWithNextOffset
) {
selected = this.bookmarks[previousIndex - 1];
return previousIndex <= 0
? this.bookmarks[this.bookmarks.length - 1]
: this.bookmarks[previousIndex - 1];
}
return this.bookmarks[previousIndex];
}

getOffsetsAndSelect(name: string, excludeBookmarkStartEnd?: boolean): void {
const bookmarks: Dictionary<string, BookmarkElementBox> =
this._documentEditor.documentHelper.bookmarks;

if (bookmarks.containsKey(name)) {
//bookmark start element
const bookmrkElmnt: BookmarkElementBox = bookmarks.get(name);

let offset: number = bookmrkElmnt.line.getOffset(bookmrkElmnt, 0);

if (excludeBookmarkStartEnd) {
offset++;
}

const startPosition: TextPosition = new TextPosition(
this._documentEditor
);
startPosition.setPositionParagraph(bookmrkElmnt.line, offset);

//bookmark end element
let bookmrkEnd: BookmarkElementBox = bookmrkElmnt.reference;
if (
bookmrkElmnt.reference &&
bookmrkElmnt.reference.line.paragraph.bodyWidget == null
) {
bookmrkEnd = bookmrkElmnt;
}

let endoffset: number = bookmrkEnd.line.getOffset(bookmrkEnd, 1);

if (excludeBookmarkStartEnd) {
endoffset--;
}
const endPosition: TextPosition = new TextPosition(this._documentEditor);
endPosition.setPositionParagraph(bookmrkEnd.line, endoffset);
//selects the bookmark range
this._documentEditor.documentHelper.selection.selectRange(
startPosition,
endPosition
);
}
}

clearReportToCopyContent(): void {
this.getBookmarks(false);
this.bookmarks.filter(field => {
this.getOffsetsAndSelect(field.name);
this.selectBookmark(field.name);
if (field.name.includes("Field")) {
if (field.content && field.content !== "Escreva uma dica de texto") {
this._documentEditor.editor.insertText(field.content);
Expand All @@ -583,7 +528,7 @@ export class IaraSyncfusionNavigationFieldManager extends IaraEditorNavigationFi
);
if (mandatoriesFields.length) {
if (mandatoriesFields[0].title) {
this.getOffsetsAndSelect(mandatoriesFields[0].name);
this.selectBookmark(mandatoriesFields[0].name);
}
return true;
}
Expand All @@ -596,4 +541,12 @@ export class IaraSyncfusionNavigationFieldManager extends IaraEditorNavigationFi
}
return this.requiredFields();
}

selectBookmark(bookmarkId: string, excludeBookmarkStartEnd?: boolean): void {
IaraSyncfusionSelectionManager.selectBookmark(
this._documentEditor,
bookmarkId,
excludeBookmarkStartEnd
);
}
}
Loading

0 comments on commit 2a22ddb

Please sign in to comment.