sync intermediate text updates (#1174)

* sync intermediate text updates

* fix initial render text position

* batch updates

* tweak onChange subscription
This commit is contained in:
David Luzar 2020-04-03 14:16:14 +02:00 committed by GitHub
parent 0c9459e9e5
commit 4912a29e75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 168 additions and 120 deletions

View file

@ -6,6 +6,7 @@ import {
} from "../element/types";
import { measureText } from "../utils";
import { randomInteger, randomId } from "../random";
import { newElementWith } from "./mutateElement";
type ElementConstructorOpts = {
x: ExcalidrawGenericElement["x"];
@ -75,17 +76,21 @@ export function newTextElement(
): ExcalidrawTextElement {
const { text, font } = opts;
const metrics = measureText(text, font);
const textElement = {
..._newElementBase<ExcalidrawTextElement>("text", opts),
text: text,
font: font,
// Center the text
x: opts.x - metrics.width / 2,
y: opts.y - metrics.height / 2,
width: metrics.width,
height: metrics.height,
baseline: metrics.baseline,
};
const textElement = newElementWith(
{
..._newElementBase<ExcalidrawTextElement>("text", opts),
isDeleted: false,
text: text,
font: font,
// Center the text
x: opts.x - metrics.width / 2,
y: opts.y - metrics.height / 2,
width: metrics.width,
height: metrics.height,
baseline: metrics.baseline,
},
{},
);
return textElement;
}