Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide our own implementation of scrollCaretIntoView #2685

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"coverage": true,
".vscode/**": true,
"dist/**": true,
"stats.json": true
"stats.json": true,
"assets/**": true
},
"prettier.arrowParens": "avoid",
"prettier.singleQuote": true,
Expand Down
2 changes: 1 addition & 1 deletion demo/scripts/controlsV2/plugins/SamplePickerPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import {
PickerHelper,
PickerPlugin,
PickerSelectionChangMode,
getDOMInsertPointRect,
} from 'roosterjs-content-model-plugins';
import {
createContentModelDocument,
createEntity,
createParagraph,
getDOMInsertPointRect,
} from 'roosterjs-content-model-dom';

const itemStyle = mergeStyles({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as React from 'react';
import { ButtonKeys, Buttons } from '../utils/buttons';
import { Callout, DirectionalHint } from '@fluentui/react/lib/Callout';
import { getDOMInsertPointRect } from 'roosterjs-content-model-plugins';
import { getDOMInsertPointRect, getObjectKeys } from 'roosterjs-content-model-dom';
import { getLocalizedString } from '../../common/index';
import { getObjectKeys } from 'roosterjs-content-model-dom';
import { Icon } from '@fluentui/react/lib/Icon';
import { IconButton } from '@fluentui/react/lib/Button';
import { memoizeFunction } from '@fluentui/react/lib/Utilities';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FormatStatePane, FormatStatePaneProps, FormatStatePaneState } from './FormatStatePane';
import { getDOMInsertPointRect } from 'roosterjs-content-model-plugins';
import { getDOMInsertPointRect } from 'roosterjs-content-model-dom';
import { getFormatState } from 'roosterjs-content-model-api';
import { PluginEvent } from 'roosterjs-content-model-types';
import { SidePaneElementProps } from '../SidePaneElement';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function mergePasteContent(
{
changeSource: ChangeSource.Paste,
getChangeData: () => clipboardData,
scrollCaretIntoView: false, // TODO #2633: Make a full fix to the scroll behavior
scrollCaretIntoView: true,
apiName: 'paste',
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ChangeSource, getSelectionRootNode } from 'roosterjs-content-model-dom';
import { ChangeSource } from 'roosterjs-content-model-dom';
import { scrollCaretIntoView } from './scrollCaretIntoView';
import type {
ChangedEntity,
ContentChangedEvent,
Expand Down Expand Up @@ -31,7 +32,7 @@ export const formatContentModel: FormatContentModel = (
changeSource,
rawEvent,
selectionOverride,
scrollCaretIntoView,
scrollCaretIntoView: scroll,
} = options || {};
const model = core.api.createContentModel(core, domToModelOptions, selectionOverride);
const context: FormatContentModelContext = {
Expand Down Expand Up @@ -70,12 +71,8 @@ export const formatContentModel: FormatContentModel = (

handlePendingFormat(core, context, selection);

if (selection && scrollCaretIntoView) {
const selectionRoot = getSelectionRootNode(selection);
const rootElement =
selectionRoot && core.domHelper.findClosestElementAncestor(selectionRoot);

rootElement?.scrollIntoView();
if (scroll && (selection?.type == 'range' || selection?.type == 'image')) {
scrollCaretIntoView(core, selection);
}

const eventData: ContentChangedEvent = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { getDOMInsertPointRect } from 'roosterjs-content-model-dom';
import type { EditorCore, ImageSelection, RangeSelection } from 'roosterjs-content-model-types';

/**
* @internal
*/
export function scrollCaretIntoView(core: EditorCore, selection: RangeSelection | ImageSelection) {
const rect = getDOMInsertPointRect(
core.physicalRoot.ownerDocument,
selection.type == 'image'
? {
node: selection.image,
offset: 0,
}
: selection.isReverted
? {
node: selection.range.startContainer,
offset: selection.range.startOffset,
}
: {
node: selection.range.endContainer,
offset: selection.range.endOffset,
}
);
const visibleRect = core.api.getVisibleViewport(core);
const scrollContainer = core.domEvent.scrollContainer;

if (rect && visibleRect) {
if (rect.bottom > visibleRect.bottom) {
const zoomScale = core.domHelper.calculateZoomScale();

scrollContainer.scrollTop += (rect.bottom - visibleRect.bottom) / zoomScale;
} else if (rect.top < visibleRect.top) {
const zoomScale = core.domHelper.calculateZoomScale();

scrollContainer.scrollTop += (rect.top - visibleRect.top) / zoomScale;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as scrollCaretIntoView from '../../../lib/coreApi/formatContentModel/scrollCaretIntoView';
import * as transformColor from 'roosterjs-content-model-dom/lib/domUtils/style/transformColor';
import { ChangeSource, createImage } from 'roosterjs-content-model-dom';
import { formatContentModel } from '../../../lib/coreApi/formatContentModel/formatContentModel';
Expand All @@ -21,7 +22,6 @@ describe('formatContentModel', () => {
let hasFocus: jasmine.Spy;
let getClientWidth: jasmine.Spy;
let announce: jasmine.Spy;
let findClosestElementAncestor: jasmine.Spy;

const apiName = 'mockedApi';
const mockedContainer = 'C' as any;
Expand All @@ -43,7 +43,6 @@ describe('formatContentModel', () => {
hasFocus = jasmine.createSpy('hasFocus');
getClientWidth = jasmine.createSpy('getClientWidth');
announce = jasmine.createSpy('announce');
findClosestElementAncestor = jasmine.createSpy('findClosestElementAncestor ');

core = ({
api: {
Expand All @@ -64,7 +63,6 @@ describe('formatContentModel', () => {
domHelper: {
hasFocus,
getClientWidth,
findClosestElementAncestor,
},
} as any) as EditorCore;
});
Expand Down Expand Up @@ -554,14 +552,14 @@ describe('formatContentModel', () => {
});

it('Has scrollCaretIntoView, and callback return true', () => {
const scrollIntoViewSpy = jasmine.createSpy('scrollIntoView');
const mockedImage = { scrollIntoView: scrollIntoViewSpy } as any;
const scrollCaretIntoViewSpy = spyOn(scrollCaretIntoView, 'scrollCaretIntoView');
const mockedImage = 'IMAGE' as any;

findClosestElementAncestor.and.returnValue(mockedImage);
setContentModel.and.returnValue({
type: 'image',
image: mockedImage,
});

formatContentModel(
core,
(model, context) => {
Expand All @@ -574,8 +572,10 @@ describe('formatContentModel', () => {
}
);

expect(findClosestElementAncestor).toHaveBeenCalledWith(mockedImage);
expect(scrollIntoViewSpy).toHaveBeenCalledTimes(1);
expect(scrollCaretIntoViewSpy).toHaveBeenCalledWith(core, {
type: 'image',
image: mockedImage,
});
});
});

Expand Down
Loading
Loading