Skip to content

Commit

Permalink
Let labels overwrite static edit control dimension
Browse files Browse the repository at this point in the history
By default, use a static dimension for edit labels, but let SModel elements
overwrite this static dimensions.
This change also fixes the key listeners (Esc to cancel editing) didn't
work anymore.
  • Loading branch information
planger committed Apr 3, 2020
1 parent afbd851 commit b8a6e44
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/svg/src/di.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class RectangleWithEditableLabel extends RectangularNode implements WithE

export class EditableForeignObjectElement extends ForeignObjectElement implements EditableLabel {
readonly isMultiLine = true;
readonly editControlBoundsCorrection = { x: 5, y: 3, height: -7.5, width: -7.5 };
get editControlDimension() { return { width: this.bounds.width, height: this.bounds.height }; }

get text(): string {
return this.code;
Expand Down
14 changes: 7 additions & 7 deletions src/features/edit/edit-label-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ export class EditLabelUI extends AbstractUIExtension {
element.style.position = 'absolute';
element.style.top = '0px';
element.style.left = '0px';
element.onkeyup = (event) => this.validateLabelIfContentChange(event, this.inputElement.value);
element.onkeydown = (event) => this.hideIfEscapeEvent(event);
element.onblur = () => window.setTimeout(() => this.applyLabelEdit(), 200);
element.addEventListener('keydown', (event) => this.hideIfEscapeEvent(event));
element.addEventListener('keyup', (event) => this.validateLabelIfContentChange(event, this.inputElement.value));
element.addEventListener('blur', () => window.setTimeout(() => this.applyLabelEdit(), 200));
containerElement.appendChild(element);
}

Expand Down Expand Up @@ -212,10 +212,10 @@ export class EditLabelUI extends AbstractUIExtension {
if (this.label) {
const zoom = getZoom(this.label);
const bounds = getAbsoluteClientBounds(this.label, this.domHelper, this.viewerOptions);
x = bounds.x + (this.label.editControlBoundsCorrection ? this.label.editControlBoundsCorrection.x * zoom : 0);
y = bounds.y + (this.label.editControlBoundsCorrection ? this.label.editControlBoundsCorrection.y * zoom : 0);
height = bounds.height + (this.label.editControlBoundsCorrection ? this.label.editControlBoundsCorrection.height * zoom : 0);
width = bounds.width + (this.label.editControlBoundsCorrection ? this.label.editControlBoundsCorrection.width * zoom : 0);
x = bounds.x + (this.label.editControlPositionCorrection ? this.label.editControlPositionCorrection.x : 0) * zoom;
y = bounds.y + (this.label.editControlPositionCorrection ? this.label.editControlPositionCorrection.y : 0) * zoom;
height = (this.label.editControlDimension ? this.label.editControlDimension.height : height) * zoom;
width = (this.label.editControlDimension ? this.label.editControlDimension.width : width) * zoom;
}

containerElement.style.left = `${x}px`;
Expand Down
5 changes: 3 additions & 2 deletions src/features/edit/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { SModelElement } from '../../base/model/smodel';
import { SRoutableElement } from '../routing/model';
import { SModelExtension } from '../../base/model/smodel-extension';
import { Bounds } from '../../utils/geometry';
import { Point, Dimension } from '../../utils/geometry';

export const editFeature = Symbol('editFeature');

Expand All @@ -30,7 +30,8 @@ export const editLabelFeature = Symbol('editLabelFeature');
export interface EditableLabel extends SModelExtension {
text: string;
readonly isMultiLine?: boolean;
readonly editControlBoundsCorrection?: Bounds;
readonly editControlDimension?: Dimension;
readonly editControlPositionCorrection?: Point;
}

export function isEditableLabel<T extends SModelElement>(element: T): element is T & EditableLabel {
Expand Down
3 changes: 0 additions & 3 deletions src/lib/generic-views.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { VNode } from "snabbdom/vnode";
import { IView, RenderingContext } from "../base/views/view";
import { setNamespace, setAttr } from "../base/views/vnode-utils";
import { ForeignObjectElement, PreRenderedElement } from "./model";
import { getSubType } from "../base/model/smodel-utils";

@injectable()
export class PreRenderedView implements IView {
Expand Down Expand Up @@ -55,8 +54,6 @@ export class ForeignObjectView implements IView {
{context.renderChildren(model)}
</g>;
setAttr(node, 'class', model.type);
const subType = getSubType(model);
if (subType) setAttr(node, 'class', subType);
setNamespace(foreignObjectContents, model.namespace);
return node;
}
Expand Down

0 comments on commit b8a6e44

Please sign in to comment.