Double click to edit text

This commit is contained in:
hazam 2020-01-07 21:52:29 +05:00
parent bf8a288490
commit 22fd7b807e
3 changed files with 91 additions and 30 deletions

View file

@ -1,13 +1,26 @@
import { KEYS } from "../index"; import { KEYS } from "../index";
export function textWysiwyg( type TextWysiwygParams = {
x: number, initText: string;
y: number, x: number;
strokeColor: string, y: number;
onSubmit: (text: string) => void strokeColor: string;
) { fontSize: number;
const input = document.createElement("input"); fontFamily: string;
onSubmit: (text: string) => void;
};
export function textWysiwyg({
initText,
x,
y,
strokeColor,
fontSize,
fontFamily,
onSubmit
}: TextWysiwygParams) {
const input = document.createElement("input");
input.value = initText;
Object.assign(input.style, { Object.assign(input.style, {
color: strokeColor, color: strokeColor,
position: "absolute", position: "absolute",
@ -17,8 +30,8 @@ export function textWysiwyg(
boxShadow: "none", boxShadow: "none",
textAlign: "center", textAlign: "center",
width: (window.innerWidth - x) * 2 + "px", width: (window.innerWidth - x) * 2 + "px",
fontSize: "20px", fontSize: fontSize + "px",
fontFamily: "Virgil", fontFamily,
border: "none", border: "none",
background: "transparent" background: "transparent"
}); });
@ -57,4 +70,5 @@ export function textWysiwyg(
window.addEventListener("wheel", stopEvent, true); window.addEventListener("wheel", stopEvent, true);
document.body.appendChild(input); document.body.appendChild(input);
input.focus(); input.focus();
input.select();
} }

View file

@ -80,14 +80,18 @@ function resetCursor() {
document.documentElement.style.cursor = ""; document.documentElement.style.cursor = "";
} }
function addTextElement(element: ExcalidrawTextElement, text: string) { function addTextElement(
element: ExcalidrawTextElement,
text: string,
fontSize: number,
fontFamily: string
) {
resetCursor(); resetCursor();
if (text === null || text === "") { if (text === null || text === "") {
return false; return false;
} }
const fontSize = 20;
element.text = text; element.text = text;
element.font = `${fontSize}px Virgil`; element.font = `${fontSize}px ${fontFamily}`;
const font = context.font; const font = context.font;
context.font = element.font; context.font = element.font;
const textMeasure = context.measureText(element.text); const textMeasure = context.measureText(element.text);
@ -138,6 +142,8 @@ class App extends React.Component<{}, AppState> {
exportBackground: true, exportBackground: true,
currentItemStrokeColor: "#000000", currentItemStrokeColor: "#000000",
currentItemBackgroundColor: "#ffffff", currentItemBackgroundColor: "#ffffff",
currentItemFontFamily: "Virgil",
currentItemFontSize: 20,
viewBackgroundColor: "#ffffff", viewBackgroundColor: "#ffffff",
scrollX: 0, scrollX: 0,
scrollY: 0, scrollY: 0,
@ -688,21 +694,28 @@ class App extends React.Component<{}, AppState> {
} }
if (isTextElement(element)) { if (isTextElement(element)) {
textWysiwyg( textWysiwyg({
e.clientX, initText: "",
e.clientY, x: e.clientX,
this.state.currentItemStrokeColor, y: e.clientY,
text => { strokeColor: this.state.currentItemStrokeColor,
addTextElement(element, text); fontSize: this.state.currentItemFontSize,
fontFamily: this.state.currentItemFontFamily,
onSubmit: text => {
addTextElement(
element,
text,
this.state.currentItemFontSize,
this.state.currentItemFontFamily
);
elements.push(element); elements.push(element);
element.isSelected = true; element.isSelected = true;
this.setState({ this.setState({
draggingElement: null, draggingElement: null,
elementType: "selection" elementType: "selection"
}); });
} }
); });
return; return;
} }
@ -916,9 +929,12 @@ class App extends React.Component<{}, AppState> {
const x = const x =
e.clientX - CANVAS_WINDOW_OFFSET_LEFT - this.state.scrollX; e.clientX - CANVAS_WINDOW_OFFSET_LEFT - this.state.scrollX;
const y = e.clientY - CANVAS_WINDOW_OFFSET_TOP - this.state.scrollY; const y = e.clientY - CANVAS_WINDOW_OFFSET_TOP - this.state.scrollY;
const elementAtPosition = getElementAtPosition(elements, x, y);
if (getElementAtPosition(elements, x, y)) { if (elementAtPosition && !isTextElement(elementAtPosition)) {
return; return;
} else if (elementAtPosition) {
elements.splice(elements.indexOf(elementAtPosition), 1);
this.forceUpdate();
} }
const element = newElement( const element = newElement(
@ -933,21 +949,50 @@ class App extends React.Component<{}, AppState> {
100 100
); );
textWysiwyg( let initText = "";
e.clientX, let textX = e.clientX;
e.clientY, let textY = e.clientY;
this.state.currentItemStrokeColor, if (elementAtPosition) {
text => { Object.assign(element, elementAtPosition);
addTextElement(element as ExcalidrawTextElement, text); // x and y will change after calling addTextElement function
element.x = elementAtPosition.x + elementAtPosition.width / 2;
element.y =
elementAtPosition.y + elementAtPosition.actualBoundingBoxAscent;
initText = elementAtPosition.text;
textX =
this.state.scrollX +
elementAtPosition.x +
CANVAS_WINDOW_OFFSET_LEFT +
elementAtPosition.width / 2;
textY =
this.state.scrollY +
elementAtPosition.y +
CANVAS_WINDOW_OFFSET_TOP +
elementAtPosition.actualBoundingBoxAscent;
}
textWysiwyg({
initText,
x: textX,
y: textY,
strokeColor: this.state.currentItemStrokeColor,
fontSize: this.state.currentItemFontSize,
fontFamily: this.state.currentItemFontFamily,
onSubmit: text => {
addTextElement(
element as ExcalidrawTextElement,
text,
this.state.currentItemFontSize,
this.state.currentItemFontFamily
);
elements.push(element); elements.push(element);
element.isSelected = true; element.isSelected = true;
this.setState({ this.setState({
draggingElement: null, draggingElement: null,
elementType: "selection" elementType: "selection"
}); });
} }
); });
}} }}
/> />
</div> </div>

View file

@ -7,6 +7,8 @@ export type AppState = {
exportBackground: boolean; exportBackground: boolean;
currentItemStrokeColor: string; currentItemStrokeColor: string;
currentItemBackgroundColor: string; currentItemBackgroundColor: string;
currentItemFontSize: number;
currentItemFontFamily: string;
viewBackgroundColor: string; viewBackgroundColor: string;
scrollX: number; scrollX: number;
scrollY: number; scrollY: number;