Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better developer expereience for missing views #395

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions packages/sprotty/src/base/views/view.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('base views', () => {

it('missing view', () => {
const vnode = missingView.render(emptyRoot, context);
expect(toHTML(vnode)).to.be.equal('<text class="sprotty-missing" x="0" y="0">?EMPTY?</text>');
expect(toHTML(vnode)).to.be.equal('<text class="sprotty-missing" x="0" y="0">missing &quot;NONE&quot; view</text>');
const model = new SNodeImpl();
model.bounds = {
x: 42,
Expand All @@ -57,7 +57,7 @@ describe('base views', () => {
model.id = 'foo';
model.type = 'type';
const vnode1 = missingView.render(model, context);
expect(toHTML(vnode1)).to.be.equal('<text class="sprotty-missing" x="42" y="41">?foo?</text>');
expect(toHTML(vnode1)).to.be.equal('<text class="sprotty-missing" x="42" y="41">missing &quot;type&quot; view</text>');
});
});

Expand Down
17 changes: 15 additions & 2 deletions packages/sprotty/src/base/views/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export class ViewRegistry extends InstanceRegistry<IView> {
}

override missing(key: string): IView {
console.warn(`no registered view for type '${key}', please configure a view in the ContainerModule`);
jonah-iden marked this conversation as resolved.
Show resolved Hide resolved
return new MissingView();
}
}
Expand Down Expand Up @@ -155,8 +156,20 @@ export class EmptyView implements IView {
*/
@injectable()
export class MissingView implements IView {
private static positionMap = new Map<string, Point>();

render(model: Readonly<SModelElementImpl>, context: RenderingContext): VNode {
const position: Point = (model as any).position || Point.ORIGIN;
return <text class-sprotty-missing={true} x={position.x} y={position.y}>?{model.id}?</text>;
const position: Point = (model as any).position || this.getPostion(model.type);
return <text class-sprotty-missing={true} x={position.x} y={position.y}>missing "{model.type}" view</text>;
jonah-iden marked this conversation as resolved.
Show resolved Hide resolved
}

getPostion(type: string) {
let position = MissingView.positionMap.get(type);
if (!position) {
position = Point.ORIGIN;
MissingView.positionMap.forEach(value => position = value.y >= position!.y ? {x: 0, y: value.y + 20} : position);
MissingView.positionMap.set(type, position);
}
return position;
}
}