Compare commits

..

3 Commits

Author SHA1 Message Date
zsviczian
34daf09b4a merge master changes (#7105)
* refactor: DRY out tool typing (#7086)

* feat: Export `iconFillColor()` (#6996)

* feat: initial Laser Pointer MVP (#6739)

* feat: initial Laser pointer mvp

* feat: add laser-pointer package and integrate it with collab

* chore: fix yarn.lock

* feat: update laser-pointer package, prevent panning from showing

* feat: add laser pointer tool button when collaborating, migrate to official package

* feat: reduce laser tool button size

* update icon

* fix icon & rotate

* fix: lock zoom level

* fix icon

* add `selected` state, simplify and reduce api

* set up pointer callbacks in viewMode if laser tool active

* highlight extra-tools button if one of the nested tools active

* add shortcut to laser pointer

* feat: don't update paths if nothing changed

* ensure we reset flag if no rAF scheduled

* move `lastUpdate` to instance to optimize

* return early

* factor out into constants and add doc

* skip iteration instead of exit

* fix naming

* feat: remove testing variable on window

* destroy on editor unmount

* fix incorrectly resetting `lastUpdate` in `stop()`

---------

Co-authored-by: dwelle <luzar.david@gmail.com>

* fix: ensure we do not stop laser update prematurely (#7100)

---------

Co-authored-by: David Luzar <luzar.david@gmail.com>
Co-authored-by: DanielJGeiger <1852529+DanielJGeiger@users.noreply.github.com>
Co-authored-by: Are <artur@excalidraw.com>
2023-10-06 14:25:20 +02:00
zsviczian
2bca4c258d use commonBounds instead of boundingBox 2023-10-05 06:09:21 +00:00
zsviczian
b0ca8f8126 fix grid jumping when multiple elements being dragged 2023-10-05 04:58:23 +00:00
8 changed files with 50 additions and 117 deletions

View File

@@ -18,7 +18,6 @@ import {
getDefaultAppState, getDefaultAppState,
isEraserActive, isEraserActive,
isHandToolActive, isHandToolActive,
isLaserPointerActive,
} from "../appState"; } from "../appState";
import { DEFAULT_CANVAS_BACKGROUND_PICKS } from "../colors"; import { DEFAULT_CANVAS_BACKGROUND_PICKS } from "../colors";
import { Bounds } from "../element/bounds"; import { Bounds } from "../element/bounds";
@@ -440,44 +439,3 @@ export const actionToggleHandTool = register({
}, },
keyTest: (event) => event.key === KEYS.H, keyTest: (event) => event.key === KEYS.H,
}); });
export const actionToggleLaserPointer = register({
name: "toggleLaserPointerTool",
viewMode: true,
trackEvent: { category: "menu" },
perform(elements, appState, _, app) {
let activeTool: AppState["activeTool"];
if (isLaserPointerActive(appState)) {
activeTool = updateActiveTool(appState, {
...(appState.activeTool.lastActiveTool || {
type: appState.viewModeEnabled ? "hand" : "selection",
}),
lastActiveToolBeforeEraser: null,
});
setCursor(
app.interactiveCanvas,
appState.viewModeEnabled ? CURSOR_TYPE.GRAB : CURSOR_TYPE.POINTER,
);
} else {
activeTool = updateActiveTool(appState, {
type: "laser",
lastActiveToolBeforeEraser: appState.activeTool,
});
setCursor(app.interactiveCanvas, CURSOR_TYPE.CROSSHAIR);
}
return {
appState: {
...appState,
selectedElementIds: {},
selectedGroupIds: {},
activeEmbeddable: null,
activeTool,
},
commitToHistory: true,
};
},
checked: (appState) => appState.activeTool.type === "laser",
contextItemLabel: "labels.laser",
});

View File

@@ -36,7 +36,6 @@ export type ShortcutName =
| "flipVertical" | "flipVertical"
| "hyperlink" | "hyperlink"
| "toggleElementLock" | "toggleElementLock"
| "toggleLaserPointerTool"
> >
| "saveScene" | "saveScene"
| "imageExport"; | "imageExport";
@@ -84,7 +83,6 @@ const shortcutMap: Record<ShortcutName, string[]> = {
viewMode: [getShortcutKey("Alt+R")], viewMode: [getShortcutKey("Alt+R")],
hyperlink: [getShortcutKey("CtrlOrCmd+K")], hyperlink: [getShortcutKey("CtrlOrCmd+K")],
toggleElementLock: [getShortcutKey("CtrlOrCmd+Shift+L")], toggleElementLock: [getShortcutKey("CtrlOrCmd+Shift+L")],
toggleLaserPointerTool: [getShortcutKey("K")],
}; };
export const getShortcutFromShortcutName = (name: ShortcutName) => { export const getShortcutFromShortcutName = (name: ShortcutName) => {

View File

@@ -124,8 +124,7 @@ export type ActionName =
| "setFrameAsActiveTool" | "setFrameAsActiveTool"
| "setEmbeddableAsActiveTool" | "setEmbeddableAsActiveTool"
| "createContainerFromText" | "createContainerFromText"
| "wrapTextInContainer" | "wrapTextInContainer";
| "toggleLaserPointerTool";
export type PanelComponentProps = { export type PanelComponentProps = {
elements: readonly ExcalidrawElement[]; elements: readonly ExcalidrawElement[];

View File

@@ -266,11 +266,3 @@ export const isHandToolActive = ({
}) => { }) => {
return activeTool.type === "hand"; return activeTool.type === "hand";
}; };
export const isLaserPointerActive = ({
activeTool,
}: {
activeTool: AppState["activeTool"];
}) => {
return activeTool.type === "laser";
};

View File

@@ -46,7 +46,6 @@ import {
getDefaultAppState, getDefaultAppState,
isEraserActive, isEraserActive,
isHandToolActive, isHandToolActive,
isLaserPointerActive,
} from "../appState"; } from "../appState";
import { parseClipboard } from "../clipboard"; import { parseClipboard } from "../clipboard";
import { import {
@@ -344,11 +343,7 @@ import {
actionRemoveAllElementsFromFrame, actionRemoveAllElementsFromFrame,
actionSelectAllElementsInFrame, actionSelectAllElementsInFrame,
} from "../actions/actionFrame"; } from "../actions/actionFrame";
import { import { actionToggleHandTool, zoomToFit } from "../actions/actionCanvas";
actionToggleHandTool,
zoomToFit,
actionToggleLaserPointer,
} from "../actions/actionCanvas";
import { jotaiStore } from "../jotai"; import { jotaiStore } from "../jotai";
import { activeConfirmDialogAtom } from "./ActiveConfirmDialog"; import { activeConfirmDialogAtom } from "./ActiveConfirmDialog";
import { import {
@@ -2915,22 +2910,7 @@ class App extends React.Component<AppProps, AppState> {
return; return;
} }
if (event.key === KEYS.K && !event.altKey && !event[KEYS.CTRL_OR_CMD]) {
if (isLaserPointerActive(this.state)) {
this.setActiveTool({
type: "selection",
});
} else {
this.setActiveTool({ type: "laser" });
}
return;
}
if (this.state.viewModeEnabled) { if (this.state.viewModeEnabled) {
//revert to hand in case a key is pressed (K is handled above)
if (event.key !== KEYS.K) {
this.setActiveTool({ type: "selection" });
}
return; return;
} }
@@ -3080,6 +3060,15 @@ class App extends React.Component<AppProps, AppState> {
} }
} }
if (event.key === KEYS.K && !event.altKey && !event[KEYS.CTRL_OR_CMD]) {
if (this.state.activeTool.type === "laser") {
this.setActiveTool({ type: "selection" });
} else {
this.setActiveTool({ type: "laser" });
}
return;
}
if ( if (
event[KEYS.CTRL_OR_CMD] && event[KEYS.CTRL_OR_CMD] &&
(event.key === KEYS.BACKSPACE || event.key === KEYS.DELETE) (event.key === KEYS.BACKSPACE || event.key === KEYS.DELETE)
@@ -3623,18 +3612,6 @@ class App extends React.Component<AppProps, AppState> {
if (this.state.multiElement) { if (this.state.multiElement) {
return; return;
} }
if (this.state.viewModeEnabled) {
if (this.state.activeTool.type === "laser") {
this.setActiveTool({ type: "selection" });
setCursor(this.interactiveCanvas, CURSOR_TYPE.GRAB);
} else {
this.setActiveTool({ type: "laser" });
setCursor(this.interactiveCanvas, CURSOR_TYPE.CROSSHAIR);
}
return;
}
// we should only be able to double click when mode is selection // we should only be able to double click when mode is selection
if (this.state.activeTool.type !== "selection") { if (this.state.activeTool.type !== "selection") {
return; return;
@@ -4762,7 +4739,7 @@ class App extends React.Component<AppProps, AppState> {
(event.button === POINTER_BUTTON.WHEEL || (event.button === POINTER_BUTTON.WHEEL ||
(event.button === POINTER_BUTTON.MAIN && isHoldingSpace) || (event.button === POINTER_BUTTON.MAIN && isHoldingSpace) ||
isHandToolActive(this.state) || isHandToolActive(this.state) ||
(this.state.viewModeEnabled && !isLaserPointerActive(this.state))) this.state.viewModeEnabled)
) || ) ||
isTextElement(this.state.editingElement) isTextElement(this.state.editingElement)
) { ) {
@@ -8166,7 +8143,6 @@ class App extends React.Component<AppProps, AppState> {
actionToggleZenMode, actionToggleZenMode,
actionToggleViewMode, actionToggleViewMode,
actionToggleStats, actionToggleStats,
actionToggleLaserPointer,
]; ];
} }

View File

@@ -155,7 +155,9 @@ const InteractiveCanvas = (props: InteractiveCanvasProps) => {
onPointerCancel={props.onPointerCancel} onPointerCancel={props.onPointerCancel}
onTouchMove={props.onTouchMove} onTouchMove={props.onTouchMove}
onPointerDown={props.onPointerDown} onPointerDown={props.onPointerDown}
onDoubleClick={props.onDoubleClick} onDoubleClick={
props.appState.viewModeEnabled ? undefined : props.onDoubleClick
}
> >
{t("labels.drawingCanvas")} {t("labels.drawingCanvas")}
</canvas> </canvas>

View File

@@ -1,5 +1,5 @@
import { updateBoundElements } from "./binding"; import { updateBoundElements } from "./binding";
import { getCommonBounds } from "./bounds"; import { Bounds, getCommonBounds } from "./bounds";
import { mutateElement } from "./mutateElement"; import { mutateElement } from "./mutateElement";
import { getPerfectElementSize } from "./sizeHelpers"; import { getPerfectElementSize } from "./sizeHelpers";
import { NonDeletedExcalidrawElement } from "./types"; import { NonDeletedExcalidrawElement } from "./types";
@@ -41,14 +41,16 @@ export const dragSelectedElements = (
elementsInFrames.forEach((element) => elementsToUpdate.add(element)); elementsInFrames.forEach((element) => elementsToUpdate.add(element));
} }
const commonBounds = getCommonBounds(Array.from(elementsToUpdate));
const adjustedOffset = calculateOffset(
commonBounds,
offset,
snapOffset,
gridSize,
);
elementsToUpdate.forEach((element) => { elementsToUpdate.forEach((element) => {
updateElementCoords( updateElementCoords(pointerDownState, element, adjustedOffset);
pointerDownState,
element,
offset,
snapOffset,
gridSize,
);
// update coords of bound text only if we're dragging the container directly // update coords of bound text only if we're dragging the container directly
// (we don't drag the group that it's part of) // (we don't drag the group that it's part of)
if ( if (
@@ -66,13 +68,7 @@ export const dragSelectedElements = (
// updating its coords again // updating its coords again
(!textElement.frameId || !frames.includes(textElement.frameId)) (!textElement.frameId || !frames.includes(textElement.frameId))
) { ) {
updateElementCoords( updateElementCoords(pointerDownState, textElement, adjustedOffset);
pointerDownState,
textElement,
offset,
snapOffset,
gridSize,
);
} }
} }
updateBoundElements(element, { updateBoundElements(element, {
@@ -81,23 +77,20 @@ export const dragSelectedElements = (
}); });
}; };
const updateElementCoords = ( const calculateOffset = (
pointerDownState: PointerDownState, commonBounds: Bounds,
element: NonDeletedExcalidrawElement,
dragOffset: { x: number; y: number }, dragOffset: { x: number; y: number },
snapOffset: { x: number; y: number }, snapOffset: { x: number; y: number },
gridSize: AppState["gridSize"], gridSize: AppState["gridSize"],
) => { ): { x: number; y: number } => {
const originalElement = const [x, y] = commonBounds;
pointerDownState.originalElements.get(element.id) ?? element; let nextX = x + dragOffset.x + snapOffset.x;
let nextY = y + dragOffset.y + snapOffset.y;
let nextX = originalElement.x + dragOffset.x + snapOffset.x;
let nextY = originalElement.y + dragOffset.y + snapOffset.y;
if (snapOffset.x === 0 || snapOffset.y === 0) { if (snapOffset.x === 0 || snapOffset.y === 0) {
const [nextGridX, nextGridY] = getGridPoint( const [nextGridX, nextGridY] = getGridPoint(
originalElement.x + dragOffset.x, x + dragOffset.x,
originalElement.y + dragOffset.y, y + dragOffset.y,
gridSize, gridSize,
); );
@@ -109,6 +102,22 @@ const updateElementCoords = (
nextY = nextGridY; nextY = nextGridY;
} }
} }
return {
x: nextX - x,
y: nextY - y,
};
};
const updateElementCoords = (
pointerDownState: PointerDownState,
element: NonDeletedExcalidrawElement,
dragOffset: { x: number; y: number },
) => {
const originalElement =
pointerDownState.originalElements.get(element.id) ?? element;
const nextX = originalElement.x + dragOffset.x;
const nextY = originalElement.y + dragOffset.y;
mutateElement(element, { mutateElement(element, {
x: nextX, x: nextX,

View File

@@ -1,6 +1,5 @@
{ {
"labels": { "labels": {
"laser": "Toggle laser pointer",
"paste": "Paste", "paste": "Paste",
"pasteAsPlaintext": "Paste as plaintext", "pasteAsPlaintext": "Paste as plaintext",
"pasteCharts": "Paste charts", "pasteCharts": "Paste charts",