@ibodr/editor 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1259,6 +1259,12 @@ type DrWheelEventInfo = DrBaseEventInfo & {
1259
1259
  name: 'wheel';
1260
1260
  delta: VecModel;
1261
1261
  point: VecModel;
1262
+ /** Сырой WheelEvent.deltaMode. Нет у синтетических событий. */
1263
+ deltaMode?: number;
1264
+ rawDeltaX?: number;
1265
+ rawDeltaY?: number;
1266
+ /** Ctrl без свёртки Cmd в ctrlKey. */
1267
+ rawCtrlKey?: boolean;
1262
1268
  };
1263
1269
  /** @public */
1264
1270
  interface DrCancelEventInfo {
@@ -2491,6 +2497,31 @@ declare function loadSnapshot(store: DrStore, _snapshot: Partial<DrEditorSnapsho
2491
2497
  /** @public */
2492
2498
  declare function getSnapshot(store: DrStore): DrEditorSnapshot;
2493
2499
 
2500
+ /** @public */
2501
+ type DrCameraWheelAction = 'zoom' | 'pan' | 'pan-x' | 'none';
2502
+ /** @public */
2503
+ interface DrCameraWheelGesture {
2504
+ ctrlKey: boolean;
2505
+ metaKey: boolean;
2506
+ altKey: boolean;
2507
+ shiftKey: boolean;
2508
+ deltaX: number;
2509
+ deltaY: number;
2510
+ deltaZ: number;
2511
+ deltaMode: number;
2512
+ rawDeltaX: number;
2513
+ rawDeltaY: number;
2514
+ trackpad: boolean;
2515
+ }
2516
+ /** @public */
2517
+ type DrCameraWheelResolver = (gesture: DrCameraWheelGesture) => DrCameraWheelAction | null;
2518
+ /** @public */
2519
+ interface DrCameraGestures {
2520
+ resolveWheel?: DrCameraWheelResolver | null;
2521
+ /** `undefined` — средняя кнопка. `null` — кнопка панорамирования выключена. */
2522
+ panButton?: number | null;
2523
+ }
2524
+
2494
2525
  /** @public */
2495
2526
  interface DrContent {
2496
2527
  shapes: DrShape[];
@@ -6288,6 +6319,23 @@ declare class Editor extends EventEmitter<DrEventMap> {
6288
6319
  *
6289
6320
  * @public */
6290
6321
  setCameraOptions(opts: Partial<DrCameraOptions>): this;
6322
+ private _cameraGestures;
6323
+ /**
6324
+ * Скорость и режим колеса без сдвига камеры и без остановки слежения.
6325
+ *
6326
+ * @public
6327
+ */
6328
+ setCameraTuning(opts: Partial<Pick<DrCameraOptions, 'wheelBehavior' | 'panSpeed' | 'zoomSpeed'>>): this;
6329
+ /**
6330
+ * Внешний реестр горячих клавиш решает, что делает колесо и какая кнопка панорамирует.
6331
+ * `null` возвращает встроенное поведение.
6332
+ *
6333
+ * @public
6334
+ */
6335
+ setCameraGestures(gestures: DrCameraGestures | null): this;
6336
+ /** @public */
6337
+ getPanPointerButton(): number | null;
6338
+ private isCameraPanButton;
6291
6339
  /** @internal */
6292
6340
  private getConstrainedCamera;
6293
6341
  /** @internal */
package/dist/index.mjs CHANGED
@@ -594,6 +594,17 @@ function useCanvasEvents() {
594
594
  function canvasEvents() {
595
595
  let isSecondaryClickPointerDown = false;
596
596
  const pointerDownButtons = /* @__PURE__ */ new Map();
597
+ function isAcceptedButton(button) {
598
+ if (button === 0 || button === 1 || button === 2 || button === 5) return true;
599
+ const pan = editor.getPanPointerButton();
600
+ return pan !== null && button === pan;
601
+ }
602
+ function onMouseDown(e) {
603
+ const pan = editor.getPanPointerButton();
604
+ if (e.button === 1 || pan !== null && e.button === pan) {
605
+ e.preventDefault();
606
+ }
607
+ }
597
608
  function onPointerDown(e) {
598
609
  if (editor.wasEventAlreadyHandled(e)) return;
599
610
  const button = getPointerEventButton(e);
@@ -607,7 +618,7 @@ function useCanvasEvents() {
607
618
  });
608
619
  return;
609
620
  }
610
- if (button !== 0 && button !== 1 && button !== 2 && button !== 5) return;
621
+ if (!isAcceptedButton(button)) return;
611
622
  const isPenDirect = isDirectDisplayPen(e);
612
623
  setPointerCapture(e.currentTarget, e);
613
624
  const isPenMode = editor.getInstanceState().isPenMode;
@@ -626,7 +637,7 @@ function useCanvasEvents() {
626
637
  if (editor.wasEventAlreadyHandled(e)) return;
627
638
  pointerDownButtons.delete(e.pointerId);
628
639
  const button = isSecondaryClickPointerDown ? 2 : getPointerEventButton(e);
629
- if (button !== 0 && button !== 1 && button !== 2 && button !== 5) return;
640
+ if (!isAcceptedButton(button)) return;
630
641
  const rightClickPanning = editor.options.rightClickPanning;
631
642
  const wasRightClickPanning = rightClickPanning && button === 2 && editor.inputs.getIsPanning();
632
643
  releasePointerCapture(e.currentTarget, e);
@@ -748,6 +759,7 @@ function useCanvasEvents() {
748
759
  preventDefault(e);
749
760
  }
750
761
  return {
762
+ onMouseDown,
751
763
  onPointerDown,
752
764
  onPointerUp,
753
765
  onPointerCancel,
@@ -1657,7 +1669,11 @@ function useGestureEvents(ref) {
1657
1669
  altKey: event.altKey,
1658
1670
  ctrlKey: event.metaKey || event.ctrlKey,
1659
1671
  metaKey: event.metaKey,
1660
- accelKey: isAccelKey(event)
1672
+ accelKey: isAccelKey(event),
1673
+ deltaMode: event.deltaMode,
1674
+ rawDeltaX: event.deltaX,
1675
+ rawDeltaY: event.deltaY,
1676
+ rawCtrlKey: event.ctrlKey
1661
1677
  };
1662
1678
  editor.dispatch(info);
1663
1679
  }
@@ -7761,6 +7777,33 @@ var LEFT_MOUSE_BUTTON = 0;
7761
7777
  var RIGHT_MOUSE_BUTTON = 2;
7762
7778
  var MIDDLE_MOUSE_BUTTON = 1;
7763
7779
  var STYLUS_ERASER_BUTTON = 5;
7780
+
7781
+ // src/lib/editor/camera/wheelPolicy.ts
7782
+ function isTrackpadWheel(info) {
7783
+ if ((info.deltaMode ?? 0) !== 0) return false;
7784
+ if (info.rawDeltaX === void 0 && info.rawDeltaY === void 0) return false;
7785
+ const ax = Math.abs(info.rawDeltaX ?? 0);
7786
+ const ay = Math.abs(info.rawDeltaY ?? 0);
7787
+ if (ax === 0 && ay === 0) return false;
7788
+ return ax < 50 && ay < 50;
7789
+ }
7790
+ function nativeWheelAction(wheelBehavior, inputMode, ctrlKey) {
7791
+ let behavior = wheelBehavior;
7792
+ if (inputMode !== null) behavior = inputMode === "trackpad" ? "pan" : "zoom";
7793
+ const action = ctrlKey ? behavior === "pan" ? "zoom" : "pan" : behavior;
7794
+ return { action, zoomFromDeltaY: action === "zoom" && behavior === "zoom" };
7795
+ }
7796
+ function selectWheelAction(input) {
7797
+ const { wheelBehavior, inputMode, ctrlKey, trackpad, resolved } = input;
7798
+ if (wheelBehavior === "none") return { action: "none", zoomFromDeltaY: false };
7799
+ if (resolved === void 0) return nativeWheelAction(wheelBehavior, inputMode, ctrlKey);
7800
+ if (inputMode === "trackpad" || inputMode !== "mouse" && trackpad) {
7801
+ return { action: ctrlKey ? "zoom" : "pan", zoomFromDeltaY: false };
7802
+ }
7803
+ if (resolved === null) return nativeWheelAction(wheelBehavior, null, ctrlKey);
7804
+ if (resolved === "zoom") return { action: "zoom", zoomFromDeltaY: true };
7805
+ return { action: resolved, zoomFromDeltaY: false };
7806
+ }
7764
7807
  function fetchCache(cb, init) {
7765
7808
  const cache = new LruCache(100);
7766
7809
  return async function fetchCached(url) {
@@ -17969,6 +18012,44 @@ var Editor = class extends EventEmitter {
17969
18012
  this.setCamera(this.getCamera());
17970
18013
  return this;
17971
18014
  }
18015
+ _cameraGestures = null;
18016
+ /**
18017
+ * Скорость и режим колеса без сдвига камеры и без остановки слежения.
18018
+ *
18019
+ * @public
18020
+ */
18021
+ setCameraTuning(opts) {
18022
+ const current = this._cameraOptions.__unsafe__getWithoutCapture();
18023
+ const next = { ...current, ...opts };
18024
+ if (next.wheelBehavior === current.wheelBehavior && next.panSpeed === current.panSpeed && next.zoomSpeed === current.zoomSpeed) {
18025
+ return this;
18026
+ }
18027
+ this._cameraOptions.set(next);
18028
+ return this;
18029
+ }
18030
+ /**
18031
+ * Внешний реестр горячих клавиш решает, что делает колесо и какая кнопка панорамирует.
18032
+ * `null` возвращает встроенное поведение.
18033
+ *
18034
+ * @public
18035
+ */
18036
+ setCameraGestures(gestures) {
18037
+ this._cameraGestures = gestures;
18038
+ return this;
18039
+ }
18040
+ /** @public */
18041
+ getPanPointerButton() {
18042
+ const configured = this._cameraGestures?.panButton;
18043
+ if (configured === void 0) return MIDDLE_MOUSE_BUTTON;
18044
+ if (configured === null || configured === LEFT_MOUSE_BUTTON || configured === RIGHT_MOUSE_BUTTON || configured === STYLUS_ERASER_BUTTON) {
18045
+ return null;
18046
+ }
18047
+ return configured;
18048
+ }
18049
+ isCameraPanButton(button) {
18050
+ const pan = this.getPanPointerButton();
18051
+ return pan !== null && button === pan;
18052
+ }
17972
18053
  /** @internal */
17973
18054
  getConstrainedCamera(point, opts) {
17974
18055
  const currentCamera = this.getCamera();
@@ -23088,6 +23169,11 @@ var Editor = class extends EventEmitter {
23088
23169
  if (!this.getIsFocused()) return this;
23089
23170
  this.focusManager.blur({ blurContainer });
23090
23171
  this.updateInstanceState({ isFocused: false });
23172
+ if (this.inputs.getIsPanning()) {
23173
+ this.inputs.setIsPanning(false);
23174
+ this.inputs.setIsSpacebarPanning(false);
23175
+ this.setCursor({ type: this._prevCursor, rotation: 0 });
23176
+ }
23091
23177
  return this;
23092
23178
  }
23093
23179
  getIsFocused() {
@@ -23684,55 +23770,74 @@ var Editor = class extends EventEmitter {
23684
23770
  if (cameraOptions.isLocked) return;
23685
23771
  this.inputs.updateFromEvent(info);
23686
23772
  const { panSpeed, zoomSpeed } = cameraOptions;
23687
- let wheelBehavior = cameraOptions.wheelBehavior;
23688
23773
  const inputMode = this.user.getUserPreferences().inputMode;
23689
- if (inputMode !== null && wheelBehavior !== "none") {
23690
- wheelBehavior = inputMode === "trackpad" ? "pan" : "zoom";
23691
- }
23692
- if (wheelBehavior !== "none") {
23774
+ const trackpad = isTrackpadWheel(info);
23775
+ const resolver = this._cameraGestures?.resolveWheel;
23776
+ const gesture = {
23777
+ ctrlKey: info.rawCtrlKey ?? (info.ctrlKey && !info.metaKey),
23778
+ metaKey: info.metaKey,
23779
+ altKey: info.altKey,
23780
+ shiftKey: info.shiftKey,
23781
+ deltaX: info.delta.x,
23782
+ deltaY: info.delta.y,
23783
+ deltaZ: info.delta.z ?? 0,
23784
+ deltaMode: info.deltaMode ?? 0,
23785
+ rawDeltaX: info.rawDeltaX ?? 0,
23786
+ rawDeltaY: info.rawDeltaY ?? 0,
23787
+ trackpad
23788
+ };
23789
+ const resolved = resolver ? resolver(gesture) : void 0;
23790
+ const { action, zoomFromDeltaY } = selectWheelAction({
23791
+ wheelBehavior: cameraOptions.wheelBehavior,
23792
+ inputMode,
23793
+ ctrlKey: info.ctrlKey,
23794
+ trackpad,
23795
+ resolved
23796
+ });
23797
+ if (action !== "none") {
23693
23798
  this.stopCameraAnimation();
23694
23799
  if (instanceState.followingUserId) {
23695
23800
  this.stopFollowingUser();
23696
23801
  }
23697
23802
  const { x: cx, y: cy, z: cz } = unsafe__withoutCapture(() => this.getCamera());
23698
23803
  const { x: dx, y: dy, z: dz = 0 } = info.delta;
23699
- let behavior = wheelBehavior;
23700
- if (info.ctrlKey) behavior = wheelBehavior === "pan" ? "zoom" : "pan";
23701
- switch (behavior) {
23702
- case "zoom": {
23703
- const { x, y } = this.inputs.getCurrentScreenPoint();
23704
- let delta = dz;
23705
- if (wheelBehavior === "zoom") {
23706
- if (Math.abs(dy) > 10) {
23707
- delta = 10 * Math.sign(dy) / 100;
23708
- } else {
23709
- delta = dy / 100;
23710
- }
23804
+ if (action === "zoom") {
23805
+ const { x, y } = this.inputs.getCurrentScreenPoint();
23806
+ let delta = dz;
23807
+ if (zoomFromDeltaY) {
23808
+ if (Math.abs(dy) > 10) {
23809
+ delta = 10 * Math.sign(dy) / 100;
23810
+ } else {
23811
+ delta = dy / 100;
23711
23812
  }
23712
- const isZoomDirectionInverted = (this.user.getUserPreferences().isZoomDirectionInverted && inputMode === "mouse") ?? false;
23713
- const deltaValue = delta ?? 0;
23714
- const finalDelta = isZoomDirectionInverted ? -deltaValue : deltaValue;
23715
- const zoom = cz + finalDelta * zoomSpeed * cz;
23716
- this._setCamera(new Vec(cx + x / zoom - x / cz, cy + y / zoom - y / cz, zoom), {
23717
- immediate: true
23718
- });
23719
- this.maybeTrackPerformance("Zooming");
23720
- this.performance._notifyCameraOperation("zooming");
23721
- this.root.handleEvent(info);
23722
- this.emit("event", info);
23723
- return;
23724
- }
23725
- case "pan": {
23726
- this._setCamera(new Vec(cx + dx * panSpeed / cz, cy + dy * panSpeed / cz, cz), {
23727
- immediate: true
23728
- });
23729
- this.maybeTrackPerformance("Panning");
23730
- this.performance._notifyCameraOperation("panning");
23731
- this.root.handleEvent(info);
23732
- this.emit("event", info);
23733
- return;
23734
23813
  }
23814
+ const isZoomDirectionInverted = (this.user.getUserPreferences().isZoomDirectionInverted && inputMode === "mouse") ?? false;
23815
+ const deltaValue = delta ?? 0;
23816
+ const finalDelta = isZoomDirectionInverted ? -deltaValue : deltaValue;
23817
+ const zoom = cz + finalDelta * zoomSpeed * cz;
23818
+ this._setCamera(new Vec(cx + x / zoom - x / cz, cy + y / zoom - y / cz, zoom), {
23819
+ immediate: true
23820
+ });
23821
+ this.maybeTrackPerformance("Zooming");
23822
+ this.performance._notifyCameraOperation("zooming");
23823
+ this.root.handleEvent(info);
23824
+ this.emit("event", info);
23825
+ return;
23735
23826
  }
23827
+ let panX = dx;
23828
+ let panY = dy;
23829
+ if (action === "pan-x" && Math.abs(panX) < Math.abs(panY)) {
23830
+ panX = panY;
23831
+ panY = 0;
23832
+ }
23833
+ this._setCamera(new Vec(cx + panX * panSpeed / cz, cy + panY * panSpeed / cz, cz), {
23834
+ immediate: true
23835
+ });
23836
+ this.maybeTrackPerformance("Panning");
23837
+ this.performance._notifyCameraOperation("panning");
23838
+ this.root.handleEvent(info);
23839
+ this.emit("event", info);
23840
+ return;
23736
23841
  }
23737
23842
  break;
23738
23843
  }
@@ -23776,7 +23881,7 @@ var Editor = class extends EventEmitter {
23776
23881
  this._restoreToolId = this.getCurrentToolId();
23777
23882
  this.complete();
23778
23883
  this.setCurrentTool("eraser");
23779
- } else if (info.button === MIDDLE_MOUSE_BUTTON) {
23884
+ } else if (this.isCameraPanButton(info.button)) {
23780
23885
  if (!this.inputs.getIsPanning()) {
23781
23886
  this._prevCursor = this.getInstanceState().cursor.type;
23782
23887
  }
@@ -23881,6 +23986,23 @@ var Editor = class extends EventEmitter {
23881
23986
  this._didCaptureSelectionAtPointerDown = false;
23882
23987
  return this;
23883
23988
  }
23989
+ default: {
23990
+ if (!this.isCameraPanButton(info.button)) break;
23991
+ if (this.inputs.keys.has("Space")) {
23992
+ this.setCursor({ type: "grab", rotation: 0 });
23993
+ } else {
23994
+ this.setCursor({ type: this._prevCursor, rotation: 0 });
23995
+ }
23996
+ if (slideSpeed > 0) {
23997
+ this.slideCamera({
23998
+ speed: slideSpeed,
23999
+ direction: { x: slideDirection.x, y: slideDirection.y, z: 0 }
24000
+ });
24001
+ }
24002
+ this._selectedShapeIdsAtPointerDown = [];
24003
+ this._didCaptureSelectionAtPointerDown = false;
24004
+ return this;
24005
+ }
23884
24006
  }
23885
24007
  if (slideSpeed > 0) {
23886
24008
  this.slideCamera({
@@ -23955,7 +24077,8 @@ var Editor = class extends EventEmitter {
23955
24077
  inputs.keys.delete(info.code);
23956
24078
  if (this.options.spacebarPanning) {
23957
24079
  if (info.code === "Space") {
23958
- if (this.inputs.buttons.has(MIDDLE_MOUSE_BUTTON)) ; else {
24080
+ const panButton = this.getPanPointerButton();
24081
+ if (panButton !== null && this.inputs.buttons.has(panButton)) ; else {
23959
24082
  this.inputs.setIsPanning(false);
23960
24083
  this.inputs.setIsSpacebarPanning(false);
23961
24084
  this.setCursor({ type: this._prevCursor, rotation: 0 });
@@ -26851,7 +26974,7 @@ function getFontsFromRichText(editor, richText, initialState2) {
26851
26974
  // src/index.ts
26852
26975
  registerDrawLibraryVersion(
26853
26976
  "@ibodr/editor",
26854
- "0.3.0",
26977
+ "0.3.1",
26855
26978
  "esm"
26856
26979
  );
26857
26980
  /*!