Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/view-lookup-typings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@joint/core": patch
---

types - view lookups (`cell.findView()`, `paper.findViewByModel()`, `paper.getCellView()`) now infer the view type from the model and include the absent-view case in the return type
16 changes: 14 additions & 2 deletions packages/joint-core/types/dia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ export class Cell<A extends ObjectHash = Cell.Attributes, S extends mvc.ModelSet

addTo(graph: Graph, opt?: Graph.Options): this;

findView(paper: Paper): CellView;
findView(paper: Paper): CellView | undefined;

isLink(): this is Link;

Expand Down Expand Up @@ -918,6 +918,8 @@ export class Element<A extends ObjectHash = Element.Attributes, S extends mvc.Mo

constructor(attributes?: DeepPartial<A>, opt?: Element.ConstructorOptions);

findView(paper: Paper): ElementView | undefined;

translate(tx: number, ty?: number, opt?: Element.TranslateOptions): this;

position(opt?: Element.PositionOptions): g.Point;
Expand Down Expand Up @@ -1053,6 +1055,8 @@ export class Link<A extends ObjectHash = Link.Attributes, S extends mvc.ModelSet
*/
labelMarkup?: string | MarkupJSON; // default label markup

findView(paper: Paper): LinkView | undefined;

disconnect(): this;

source(): Link.EndJSON;
Expand Down Expand Up @@ -2070,8 +2074,16 @@ export class Paper extends mvc.View<Graph> {

findView<T extends ElementView | LinkView>(element: mvc.$SVGElement): T;

findViewByModel<T extends ElementView | LinkView>(model: Graph.CellRef): T;
findViewByModel(model: Element): ElementView | undefined;
findViewByModel(model: Link): LinkView | undefined;
findViewByModel(model: Cell | Cell.ID): CellView | undefined;
/** @deprecated Pass the model and let the view type be inferred. */
findViewByModel<T extends ElementView | LinkView>(model: Graph.CellRef): T | undefined;

getCellView(model: Element): ElementView | null;
getCellView(model: Link): LinkView | null;
getCellView(model: Cell | Cell.ID): CellView | null;
/** @deprecated Pass the model and let the view type be inferred. */
getCellView<T extends ElementView | LinkView>(model: Graph.CellRef): T | null;

/**
Expand Down
1 change: 0 additions & 1 deletion packages/joint-react/src/store/clear-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export function clearConnectedLinkViews(
linkView._sourceMagnet = null;
// @ts-expect-error we use private jointjs api method
linkView._targetMagnet = null;
// @ts-expect-error we use private jointjs api method
linkView.requestConnectionUpdate({ async: true });
changes.set(String(link.id), { type: 'change', data: link });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ function Main() {
useOnElementsMeasured(({ isInitial, paper }) => {
if (!isInitial) return;
for (const element of graph.getElements()) {
StatusList.add(element.findView(paper), 'root', 'status', {
const elementView = element.findView(paper);
if (!elementView) continue;
StatusList.add(elementView, 'root', 'status', {
attribute: 'status',
position: 'top-right',
margin: { right: 5, top: 5 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ function addElementControls(paper: dia.Paper) {
const factory = controlMap[type];
if (!factory) continue;
const toolsView = new dia.ToolsView({ tools: [factory()] });
element.findView(paper).addTools(toolsView);
element.findView(paper)?.addTools(toolsView);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ function Selection({ selectedId }: { selectedId: CellId | null }) {
if (!cell) return;

const view = paper.findViewByModel(cell);
if (!view) return;
highlighters.mask.add(view, 'root', 'selection', {
padding: 8,
layer: dia.Paper.Layers.FRONT,
Expand Down
Loading