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

fix(edgeless): horizontal pan with mouse under the windows system #6507

Merged
merged 1 commit into from
Mar 19, 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
41 changes: 41 additions & 0 deletions packages/blocks/src/root-block/edgeless/edgeless-root-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import '../../surface-block/surface-block.js';
import './components/block-portal/frame/edgeless-frame.js';

import type { SurfaceSelection } from '@blocksuite/block-std';
import { IS_WINDOWS } from '@blocksuite/global/env';
import { assertExists, throttle } from '@blocksuite/global/utils';
import { BlockElement } from '@blocksuite/lit';
import { type BlockModel } from '@blocksuite/store';
Expand All @@ -19,6 +20,7 @@ import {
import { listenToThemeChange } from '../../_common/theme/utils.js';
import {
type EdgelessTool,
isPinchEvent,
NoteDisplayMode,
Point,
requestConnectedFrame,
Expand Down Expand Up @@ -47,6 +49,7 @@ import {
Bound,
type IBound,
type IVec,
normalizeWheelDeltaY,
serializeXYWH,
Vec,
} from '../../surface-block/index.js';
Expand Down Expand Up @@ -663,6 +666,7 @@ export class EdgelessRootBlockComponent extends BlockElement<
this._initSurface();

this._initViewport();
this._initWheelEvent();

if (this.doc.readonly) {
this.tools.setEdgelessTool({ type: 'pan', panning: true });
Expand Down Expand Up @@ -721,6 +725,43 @@ export class EdgelessRootBlockComponent extends BlockElement<
this.service.tool.mount(this);
}

private _initWheelEvent() {
this._disposables.add(
this.dispatcher.add('wheel', ctx => {
const state = ctx.get('defaultState');
const e = state.event as WheelEvent;

e.preventDefault();

const { viewport } = this.service;
// zoom
if (isPinchEvent(e)) {
const rect = this.getBoundingClientRect();
// Perform zooming relative to the mouse position
const [baseX, baseY] = this.service.viewport.toModelCoord(
e.clientX - rect.x,
e.clientY - rect.y
);

const zoom = normalizeWheelDeltaY(e.deltaY, viewport.zoom);
viewport.setZoom(zoom, new Point(baseX, baseY));
e.stopPropagation();
}
// pan
else {
const simulateHorizontalScroll = IS_WINDOWS && e.shiftKey;
const dx = simulateHorizontalScroll
? e.deltaY / viewport.zoom
: e.deltaX / viewport.zoom;
const dy = simulateHorizontalScroll ? 0 : e.deltaY / viewport.zoom;

viewport.applyDeltaCenter(dx, dy);
e.stopPropagation();
}
})
);
}

override connectedCallback() {
super.connectedCallback();
this.clipboardController.hostConnected();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import { DisposableGroup } from '@blocksuite/global/utils';
import {
type EdgelessTool,
isMiddleButtonPressed,
isPinchEvent,
NoteDisplayMode,
Point,
} from '../../../_common/utils/index.js';
import { normalizeWheelDeltaY } from '../../../surface-block/index.js';
import type { Bound } from '../../../surface-block/utils/bound.js';
import type { EdgelessToolController } from '../controllers/tools/index.js';
import type { EdgelessRootBlockComponent } from '../edgeless-root-block.js';
Expand Down Expand Up @@ -234,36 +231,6 @@ export class EdgelessToolsManager {
const event = ctx.get('defaultState');
this._onContainerContextMenu(event);
});
this._add('wheel', ctx => {
const state = ctx.get('defaultState');
const e = state.event;
if (!(e instanceof WheelEvent)) return;

e.preventDefault();

const container = this.container;
const { viewport } = this.service;
// pan
if (!isPinchEvent(e)) {
const dx = e.deltaX / viewport.zoom;
const dy = e.deltaY / viewport.zoom;
viewport.applyDeltaCenter(dx, dy);
e.stopPropagation();
}
// zoom
else {
const rect = container.getBoundingClientRect();
// Perform zooming relative to the mouse position
const [baseX, baseY] = this.service.viewport.toModelCoord(
e.clientX - rect.x,
e.clientY - rect.y
);

const zoom = normalizeWheelDeltaY(e.deltaY, viewport.zoom);
viewport.setZoom(zoom, new Point(baseX, baseY));
e.stopPropagation();
}
});
}

private _add = (name: EventName, fn: UIEventHandler) => {
Expand Down
Loading