-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] feat: revert selection format after checking for boundaries
- Loading branch information
Showing
2 changed files
with
112 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}); | ||
} | ||
} |