edit text when clicked on with text tool (#1283)

This commit is contained in:
David Luzar 2020-04-06 22:26:54 +02:00 committed by GitHub
parent d3ed5a42fc
commit e9b4700bba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 56 deletions

View File

@ -6,8 +6,7 @@ import { hasBackground, hasStroke, hasText, getTargetElement } from "../scene";
import { t } from "../i18n"; import { t } from "../i18n";
import { SHAPES } from "../shapes"; import { SHAPES } from "../shapes";
import { ToolButton } from "./ToolButton"; import { ToolButton } from "./ToolButton";
import { capitalizeString, getShortcutKey } from "../utils"; import { capitalizeString, getShortcutKey, setCursorForShape } from "../utils";
import { CURSOR_TYPE } from "../constants";
import Stack from "./Stack"; import Stack from "./Stack";
import useIsMobile from "../is-mobile"; import useIsMobile from "../is-mobile";
@ -115,8 +114,7 @@ export function ShapesSwitcher({
multiElement: null, multiElement: null,
selectedElementIds: {}, selectedElementIds: {},
}); });
document.documentElement.style.cursor = setCursorForShape(value);
value === "text" ? CURSOR_TYPE.TEXT : CURSOR_TYPE.CROSSHAIR;
setAppState({}); setAppState({});
}} }}
></ToolButton> ></ToolButton>

View File

@ -67,6 +67,7 @@ import {
resetCursor, resetCursor,
viewportCoordsToSceneCoords, viewportCoordsToSceneCoords,
sceneCoordsToViewportCoords, sceneCoordsToViewportCoords,
setCursorForShape,
} from "../utils"; } from "../utils";
import { KEYS, isArrowKey } from "../keys"; import { KEYS, isArrowKey } from "../keys";
@ -146,15 +147,6 @@ const gesture: Gesture = {
initialScale: null, initialScale: null,
}; };
function setCursorForShape(shape: string) {
if (shape === "selection") {
resetCursor();
} else {
document.documentElement.style.cursor =
shape === "text" ? CURSOR_TYPE.TEXT : CURSOR_TYPE.CROSSHAIR;
}
}
export class App extends React.Component<any, AppState> { export class App extends React.Component<any, AppState> {
canvas: HTMLCanvasElement | null = null; canvas: HTMLCanvasElement | null = null;
rc: RoughCanvas | null = null; rc: RoughCanvas | null = null;
@ -1110,10 +1102,7 @@ export class App extends React.Component<any, AppState> {
if (this.state.elementType === "selection") { if (this.state.elementType === "selection") {
resetCursor(); resetCursor();
} else { } else {
document.documentElement.style.cursor = setCursorForShape(this.state.elementType);
this.state.elementType === "text"
? CURSOR_TYPE.TEXT
: CURSOR_TYPE.CROSSHAIR;
this.setState({ selectedElementIds: {} }); this.setState({ selectedElementIds: {} });
} }
isHoldingSpace = false; isHoldingSpace = false;
@ -1483,7 +1472,11 @@ export class App extends React.Component<any, AppState> {
} }
const hasDeselectedButton = Boolean(event.buttons); const hasDeselectedButton = Boolean(event.buttons);
if (hasDeselectedButton || this.state.elementType !== "selection") { if (
hasDeselectedButton ||
(this.state.elementType !== "selection" &&
this.state.elementType !== "text")
) {
return; return;
} }
@ -1513,8 +1506,14 @@ export class App extends React.Component<any, AppState> {
y, y,
this.state.zoom, this.state.zoom,
); );
document.documentElement.style.cursor = if (this.state.elementType === "text") {
hitElement && !isOverScrollBar ? "move" : ""; document.documentElement.style.cursor = isTextElement(hitElement)
? CURSOR_TYPE.TEXT
: CURSOR_TYPE.CROSSHAIR;
} else {
document.documentElement.style.cursor =
hitElement && !isOverScrollBar ? "move" : "";
}
}; };
private handleCanvasPointerDown = ( private handleCanvasPointerDown = (
@ -1790,48 +1789,26 @@ export class App extends React.Component<any, AppState> {
return; return;
} }
const snappedToCenterPosition = event.altKey const { x, y } = viewportCoordsToSceneCoords(
? null event,
: this.getTextWysiwygSnappedToCenterPosition( this.state,
x, this.canvas,
y, window.devicePixelRatio,
this.state, );
this.canvas,
window.devicePixelRatio,
);
const element = newTextElement({ this.startTextEditing({
x: snappedToCenterPosition?.elementCenterX ?? x, x: x,
y: snappedToCenterPosition?.elementCenterY ?? y, y: y,
strokeColor: this.state.currentItemStrokeColor, clientX: event.clientX,
backgroundColor: this.state.currentItemBackgroundColor, clientY: event.clientY,
fillStyle: this.state.currentItemFillStyle, centerIfPossible: !event.altKey,
strokeWidth: this.state.currentItemStrokeWidth,
roughness: this.state.currentItemRoughness,
opacity: this.state.currentItemOpacity,
text: "",
font: this.state.currentItemFont,
}); });
globalSceneState.replaceAllElements([
...globalSceneState.getAllElements(),
element,
]);
this.handleTextWysiwyg(element, {
x: snappedToCenterPosition?.wysiwygX ?? event.clientX,
y: snappedToCenterPosition?.wysiwygY ?? event.clientY,
});
resetCursor(); resetCursor();
if (!this.state.elementLocked) { if (!this.state.elementLocked) {
this.setState({ this.setState({
editingElement: element,
elementType: "selection", elementType: "selection",
}); });
} else {
this.setState({
editingElement: element,
});
} }
return; return;
} else if ( } else if (

View File

@ -5,9 +5,9 @@ import {
} from "./types"; } from "./types";
export function isTextElement( export function isTextElement(
element: ExcalidrawElement, element: ExcalidrawElement | null,
): element is ExcalidrawTextElement { ): element is ExcalidrawTextElement {
return element.type === "text"; return element != null && element.type === "text";
} }
export function isLinearElement( export function isLinearElement(

View File

@ -1,5 +1,6 @@
import { FlooredNumber } from "./types"; import { FlooredNumber } from "./types";
import { getZoomOrigin } from "./scene"; import { getZoomOrigin } from "./scene";
import { CURSOR_TYPE } from "./constants";
export const SVG_NS = "http://www.w3.org/2000/svg"; export const SVG_NS = "http://www.w3.org/2000/svg";
@ -144,6 +145,14 @@ export function resetCursor() {
document.documentElement.style.cursor = ""; document.documentElement.style.cursor = "";
} }
export function setCursorForShape(shape: string) {
if (shape === "selection") {
resetCursor();
} else {
document.documentElement.style.cursor = CURSOR_TYPE.CROSSHAIR;
}
}
export const isFullScreen = () => export const isFullScreen = () =>
document.fullscreenElement?.nodeName === "HTML"; document.fullscreenElement?.nodeName === "HTML";