Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

feat: annotation pin #338

Merged
merged 30 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5aecf6b
feat: add component to annotation pin
carlossantos74 Sep 25, 2023
39b8ed2
Merge branch 'feat/comments-component' of github.com:SuperViz/sdk int…
carlossantos74 Sep 25, 2023
0ec5574
Merge branch 'feat/comments-component' of https://github.com/SuperViz…
carlossantos74 Sep 25, 2023
fd5fb78
feat(wip): turn pin in to mouse cursor when it in canvas
carlossantos74 Sep 25, 2023
07341f5
Merge branch 'feat/comments-component' of github.com:SuperViz/sdk int…
carlossantos74 Sep 26, 2023
c6d4e25
feat(wip): add canvas adapter to handler pin in to canvas
carlossantos74 Sep 27, 2023
025ef5d
refactor: improve and fix some bugs
Sep 26, 2023
5c58ebb
Merge branch 'refactor/improve-and-fix-some-bugs' into feat/add-new-f…
Sep 27, 2023
7ed832a
feat: open and close comments function
Sep 27, 2023
59b280b
refactor: creation of annotations and comments
Sep 27, 2023
a1659cb
test: add new tests in annotations
Sep 27, 2023
f88c0fa
docs: add example to pin adapters to lib.md
carlossantos74 Sep 27, 2023
8f94b44
Merge pull request #344 from SuperViz/feat/add-new-functions-to-comments
carlossantos74 Sep 27, 2023
2adfb56
feat(wip): add button to toggle canvas
carlossantos74 Sep 27, 2023
50db95a
Merge branch 'feat/comments-pin' of github.com:SuperViz/sdk into feat…
carlossantos74 Sep 27, 2023
1ca550f
feat: add toggle button to open/close comments sidebar
carlossantos74 Sep 27, 2023
8f168ce
feat: active pin adapter only if comments sibar is open
carlossantos74 Sep 28, 2023
33cd4dc
feat: create annotation with canvas cordinates
carlossantos74 Sep 28, 2023
55f24c7
feat: update annotation list on pin adapter when it's updated on real…
carlossantos74 Sep 28, 2023
956770d
feat(wip): render annotations on canvas
carlossantos74 Sep 28, 2023
84e543c
feat: render pins into canvas
carlossantos74 Sep 28, 2023
cc80ccf
feat: create temporary pin while a new annotation is creating
carlossantos74 Sep 28, 2023
92330e5
fix: fix coordinates to save
carlossantos74 Sep 28, 2023
ff8c5d1
feat: remove all pins when isActive turned false
carlossantos74 Sep 28, 2023
65c8ac1
fix: annotations styles
carlossantos74 Sep 28, 2023
6d38698
fix: coordinates spell check
carlossantos74 Sep 28, 2023
154fa10
test: improve test coverage
carlossantos74 Sep 28, 2023
86352f2
test: fix annotation-pin tests
carlossantos74 Sep 28, 2023
5ab9481
feat: select annotation by pin
carlossantos74 Sep 30, 2023
3b67ca9
refactor: pin option case and unecessary imports
carlossantos74 Oct 2, 2023
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
6 changes: 5 additions & 1 deletion __mocks__/comments.mock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export const MOCK_ANNOTATION = {
uuid: 'any_uuid',
position: 'any_position',
position: JSON.stringify({
x: 100,
y: 100,
type: 'canvas',
}),
resolved: false,
comments: [
{
Expand Down
22 changes: 18 additions & 4 deletions lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
```ts
import { SuperViz } from '@superviz/sdk';
import { VideoComponent } from '@superviz/sdk/components/video.ts';
import { CommentsComponent } from '@superviz/sdk/components/comments.ts';
import { MatterportComponent } from '@superviz/matterport';
import { CommentsComponent, CanvasAdapter } from '@superviz/sdk/components/comments.ts';
import { Presence3D, CommentsAdapter } from '@superviz/matterport';
import { Presence3D, CommentsAdapter } from '@superviz/three';
import { PresenceComponent } from '@superviz/sdk/components/presence.ts';

const SuperViz = await Manager(DEVELOPER_KEY, {
Expand All @@ -21,10 +22,23 @@ const SuperViz = await Manager(DEVELOPER_KEY, {

const video = new VideoComponent();

const mpPresence = new Presence3D(mpInstance);
SuperViz.addComponent(mpPresence);

// Initialize comments with canvas
const canvasAdapter = new CanvasAdapter('canvas-id');
const comments = new CommentsComponent(canvasAdapter);
SuperViz.addComponent(comments);

// Initialize coments with matterport 3d component
const canvasAdapter = new CommentsAdapter(mpInstance);
const comments = new CommentsComponent(canvasAdapter);
SuperViz.addComponent(comments);

// const mpCommentsAdapter = new CommentsAdapter(mpInstance);

// add configs
SuperViz.addComponent(video);
SuperViz.addComponent(presence);
SuperViz.addComponent(comments);
SuperViz.addComponent(mpPresence);

// PubSub
Expand Down
204 changes: 204 additions & 0 deletions src/components/comments/canvas-pin-adapter/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
import { MOCK_ANNOTATION } from '../../../../__mocks__/comments.mock';

carlossantos74 marked this conversation as resolved.
Show resolved Hide resolved
import { CanvasPinAdapter } from '.';

describe('CanvasPinAdapter', () => {
beforeEach(() => {
document.body.innerHTML = `
<canvas id="canvas"></canvas>
`;
});

afterEach(() => {
document.body.innerHTML = '';
});

test('should create a new instance of CanvasPinAdapter', () => {
const canvasPinAdapter = new CanvasPinAdapter('canvas');
canvasPinAdapter.setActive(true);
expect(canvasPinAdapter).toBeInstanceOf(CanvasPinAdapter);
});

test('should throw an error if no canvas element is found', () => {
expect(() => new CanvasPinAdapter('not-found-canvas')).toThrowError(
'Canvas with id not-found-canvas not found',
);
});

test('should add event listeners to the canvas element', () => {
const canvasPinAdapter = new CanvasPinAdapter('canvas');
canvasPinAdapter.setActive(true);
const addEventListenerSpy = jest.spyOn(canvasPinAdapter['canvas'], 'addEventListener');
canvasPinAdapter['addListeners']();
expect(addEventListenerSpy).toHaveBeenCalledTimes(4);
});

test('should destroy the canvas pin adapter', () => {
const canvasPinAdapter = new CanvasPinAdapter('canvas');
const removeEventListenerSpy = jest.spyOn(canvasPinAdapter['canvas'], 'removeEventListener');

canvasPinAdapter.destroy();

expect(canvasPinAdapter['mouseElement']).toBeNull();
expect(removeEventListenerSpy).toHaveBeenCalledTimes(4);
});

test('when mouse enters canvas, should create a new mouse element', () => {
const canvasPinAdapter = new CanvasPinAdapter('canvas');
canvasPinAdapter.setActive(true);
const mock = jest.fn().mockImplementation(() => document.createElement('div'));
canvasPinAdapter['createMouseElement'] = mock;

canvasPinAdapter['canvas'].dispatchEvent(new MouseEvent('mouseenter'));

expect(canvasPinAdapter['createMouseElement']).toHaveBeenCalledTimes(1);
});

test('when mouse leaves canvas, should remove the mouse element', () => {
const canvasPinAdapter = new CanvasPinAdapter('canvas');
canvasPinAdapter.setActive(true);

canvasPinAdapter['canvas'].dispatchEvent(new MouseEvent('mouseenter'));
canvasPinAdapter['canvas'].dispatchEvent(new MouseEvent('mouseout'));

expect(canvasPinAdapter['mouseElement']).toBeNull();
});

test('when mouse clicks canvas', () => {
const canvasPinAdapter = new CanvasPinAdapter('canvas');
canvasPinAdapter.setActive(true);

canvasPinAdapter['canvas'].dispatchEvent(new MouseEvent('mouseenter'));
canvasPinAdapter['onClick']({ x: 100, y: 100 } as unknown as MouseEvent);
});

test('should remove annotation pin', () => {
const canvasPinAdapter = new CanvasPinAdapter('canvas');
canvasPinAdapter.setActive(true);

canvasPinAdapter.updateAnnotations([MOCK_ANNOTATION]);

expect(canvasPinAdapter['pins'].size).toEqual(1);

canvasPinAdapter.removeAnnotationPin(MOCK_ANNOTATION.uuid);

expect(canvasPinAdapter['pins'].size).toEqual(0);
});

test('should not remove annotation pin if it does not exist', () => {
const canvasPinAdapter = new CanvasPinAdapter('canvas');
canvasPinAdapter.setActive(true);

canvasPinAdapter.updateAnnotations([MOCK_ANNOTATION]);

expect(canvasPinAdapter['pins'].size).toEqual(1);

canvasPinAdapter.removeAnnotationPin('not_found_uuid');

expect(canvasPinAdapter['pins'].size).toEqual(1);
});

test('should not render annotations if the adapter is not active', () => {
const canvasPinAdapter = new CanvasPinAdapter('canvas');
canvasPinAdapter.setActive(false);

canvasPinAdapter.updateAnnotations([MOCK_ANNOTATION]);

expect(canvasPinAdapter['pins'].size).toEqual(0);
});

test('should remove pins when isActive is turned to false', () => {
const canvasPinAdapter = new CanvasPinAdapter('canvas');
canvasPinAdapter.setActive(true);

canvasPinAdapter.updateAnnotations([MOCK_ANNOTATION]);

expect(canvasPinAdapter['pins'].size).toEqual(1);

canvasPinAdapter.setActive(false);

expect(canvasPinAdapter['pins'].size).toEqual(0);
});

test('should not render annotation if the coordinate type is not canvas', () => {
const canvasPinAdapter = new CanvasPinAdapter('canvas');
canvasPinAdapter.setActive(true);

canvasPinAdapter.updateAnnotations([
{
...MOCK_ANNOTATION,
uuid: 'not-canvas',
position: JSON.stringify({
x: 100,
y: 100,
type: 'not-canvas',
}),
},
]);

expect(canvasPinAdapter['pins'].has('not-canvas')).toBeFalsy();
});

test('should select annotation pin', async () => {
const canvasPinAdapter = new CanvasPinAdapter('canvas');
canvasPinAdapter.setActive(true);

canvasPinAdapter.updateAnnotations([MOCK_ANNOTATION]);

expect(canvasPinAdapter['selectedPin']).not.toBeDefined();

canvasPinAdapter['annotationSelected'](
new CustomEvent('select-annotation', {
detail: {
uuid: MOCK_ANNOTATION.uuid,
},
}),
);

expect(
[...canvasPinAdapter['pins'].values()].some((pin) => pin.hasAttribute('active')),
).toBeTruthy();
});

test('should not select annotation pin if uuid is not defined', async () => {
const canvasPinAdapter = new CanvasPinAdapter('canvas');
canvasPinAdapter.setActive(true);

canvasPinAdapter.updateAnnotations([MOCK_ANNOTATION]);

expect(canvasPinAdapter['selectedPin']).not.toBeDefined();

canvasPinAdapter['annotationSelected'](
new CustomEvent('select-annotation', {
detail: {
uuid: undefined,
},
}),
);

expect(
[...canvasPinAdapter['pins'].values()].some((pin) => pin.hasAttribute('active')),
).toBeFalsy();
});

test('should not select annotation pin if it does not exist', async () => {
const canvasPinAdapter = new CanvasPinAdapter('canvas');
canvasPinAdapter.setActive(true);

canvasPinAdapter.updateAnnotations([MOCK_ANNOTATION]);

expect(canvasPinAdapter['selectedPin']).not.toBeDefined();

canvasPinAdapter['annotationSelected'](
new CustomEvent('select-annotation', {
detail: {
uuid: 'not-found',
},
}),
);

expect(
[...canvasPinAdapter['pins'].values()].some((pin) => pin.hasAttribute('active')),
).toBeFalsy();
});
});
Loading
Loading