Skip to content

Commit 1964a88

Browse files
committed
chore: add tests
1 parent 6adc025 commit 1964a88

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

packages/super-editor/src/editors/v1/core/presentation-editor/PresentationEditor.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6512,6 +6512,13 @@ export class PresentationEditor extends EventEmitter {
65126512
}
65136513
}
65146514
if (dom && geometry) {
6515+
const samePage = dom.pageIndex === geometry.pageIndex;
6516+
const dx = Math.abs(dom.x - geometry.x);
6517+
const dy = Math.abs(dom.y - geometry.y);
6518+
const DOM_GEOMETRY_MISMATCH_PX = 24;
6519+
if (!samePage || dx > DOM_GEOMETRY_MISMATCH_PX || dy > DOM_GEOMETRY_MISMATCH_PX) {
6520+
return geometry;
6521+
}
65156522
return {
65166523
pageIndex: dom.pageIndex,
65176524
x: dom.x,

packages/super-editor/src/editors/v1/core/presentation-editor/tests/PresentationEditor.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { PresentationEditor } from '../PresentationEditor.js';
44
import type { Editor as EditorInstance } from '../../Editor.js';
55
import { Editor } from '../../Editor.js';
66
import { HeaderFooterEditorManager, HeaderFooterLayoutAdapter } from '../../header-footer/HeaderFooterRegistry.js';
7+
import * as CaretGeometry from '../selection/CaretGeometry.js';
8+
import * as DomSelectionGeometry from '../../../dom-observer/DomSelectionGeometry.js';
79

810
type MockedEditor = Mock<(...args: unknown[]) => EditorInstance> & {
911
mock: {
@@ -3785,6 +3787,68 @@ describe('PresentationEditor', () => {
37853787
});
37863788
});
37873789

3790+
describe('#computeCaretLayoutRect DOM/geometry mismatch fallback', () => {
3791+
it('uses DOM caret when DOM and geometry are close', () => {
3792+
const geometrySpy = vi
3793+
.spyOn(CaretGeometry, 'computeCaretLayoutRectGeometry')
3794+
.mockReturnValue({ pageIndex: 0, x: 100, y: 200, height: 18 });
3795+
const domSpy = vi
3796+
.spyOn(DomSelectionGeometry, 'computeDomCaretPageLocal')
3797+
.mockReturnValue({ pageIndex: 0, x: 108, y: 210 });
3798+
3799+
editor = new PresentationEditor({
3800+
element: container,
3801+
documentId: 'test-doc',
3802+
});
3803+
3804+
const caret = editor.computeCaretLayoutRect(5);
3805+
expect(caret).toEqual({ pageIndex: 0, x: 108, y: 210, height: 18 });
3806+
3807+
geometrySpy.mockRestore();
3808+
domSpy.mockRestore();
3809+
});
3810+
3811+
it('falls back to geometry when DOM and geometry differ significantly', () => {
3812+
const geometrySpy = vi
3813+
.spyOn(CaretGeometry, 'computeCaretLayoutRectGeometry')
3814+
.mockReturnValue({ pageIndex: 0, x: 100, y: 200, height: 18 });
3815+
const domSpy = vi
3816+
.spyOn(DomSelectionGeometry, 'computeDomCaretPageLocal')
3817+
.mockReturnValue({ pageIndex: 0, x: 180, y: 290 });
3818+
3819+
editor = new PresentationEditor({
3820+
element: container,
3821+
documentId: 'test-doc',
3822+
});
3823+
3824+
const caret = editor.computeCaretLayoutRect(5);
3825+
expect(caret).toEqual({ pageIndex: 0, x: 100, y: 200, height: 18 });
3826+
3827+
geometrySpy.mockRestore();
3828+
domSpy.mockRestore();
3829+
});
3830+
3831+
it('falls back to geometry when DOM and geometry point to different pages', () => {
3832+
const geometrySpy = vi
3833+
.spyOn(CaretGeometry, 'computeCaretLayoutRectGeometry')
3834+
.mockReturnValue({ pageIndex: 1, x: 120, y: 220, height: 16 });
3835+
const domSpy = vi
3836+
.spyOn(DomSelectionGeometry, 'computeDomCaretPageLocal')
3837+
.mockReturnValue({ pageIndex: 0, x: 120, y: 220 });
3838+
3839+
editor = new PresentationEditor({
3840+
element: container,
3841+
documentId: 'test-doc',
3842+
});
3843+
3844+
const caret = editor.computeCaretLayoutRect(5);
3845+
expect(caret).toEqual({ pageIndex: 1, x: 120, y: 220, height: 16 });
3846+
3847+
geometrySpy.mockRestore();
3848+
domSpy.mockRestore();
3849+
});
3850+
});
3851+
37883852
describe('#updateSelection DOM manipulation error handling', () => {
37893853
it('should handle DOM errors when clearing selection in viewing mode', () => {
37903854
editor = new PresentationEditor({

0 commit comments

Comments
 (0)