Skip to content

Commit

Permalink
Rename beforeCreateAnnotation to createAnnotation
Browse files Browse the repository at this point in the history
This is the event that creates the annotation, so it is better to name
it in a more obvious way.

Context:
#3829 (comment)
  • Loading branch information
esanzgar committed Oct 14, 2021
1 parent 7bb7b76 commit 957b44f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/annotator/annotation-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class AnnotationSync {
return;
}
this.bridge.call(
guestToSidebarEvents.BEFORE_CREATE_ANNOTATION,
guestToSidebarEvents.CREATE_ANNOTATION,
this._format(annotation)
);
});
Expand Down
2 changes: 1 addition & 1 deletion src/annotator/test/annotation-sync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('AnnotationSync', () => {
assert.called(fakeBridge.call);
assert.calledWith(
fakeBridge.call,
guestToSidebarEvents.BEFORE_CREATE_ANNOTATION,
guestToSidebarEvents.CREATE_ANNOTATION,
{
msg: ann,
tag: ann.$tag,
Expand Down
6 changes: 3 additions & 3 deletions src/shared/bridge-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ export const hostToSidebarEvents = {
/**
* Events that the guest sends to the sidebar
*
* @typedef {'beforeCreateAnnotation'|'closeSidebar'|'focusAnnotations'|'openSidebar'|'showAnnotations'|'sync'|'toggleAnnotationSelection'} GuestToSidebarEvent
* @type {Record<'BEFORE_CREATE_ANNOTATION'|'CLOSE_SIDEBAR'|'FOCUS_ANNOTATIONS'|'OPEN_SIDEBAR'|'SHOW_ANNOTATIONS'|'SYNC'|'TOGGLE_ANNOTATION_SELECTION', GuestToSidebarEvent>}
* @typedef {'createAnnotation'|'closeSidebar'|'focusAnnotations'|'openSidebar'|'showAnnotations'|'sync'|'toggleAnnotationSelection'} GuestToSidebarEvent
* @type {Record<'CREATE_ANNOTATION'|'CLOSE_SIDEBAR'|'FOCUS_ANNOTATIONS'|'OPEN_SIDEBAR'|'SHOW_ANNOTATIONS'|'SYNC'|'TOGGLE_ANNOTATION_SELECTION', GuestToSidebarEvent>}
*/
export const guestToSidebarEvents = {
/**
* The guest is asking the sidebar to create an annotation.
*/
BEFORE_CREATE_ANNOTATION: 'beforeCreateAnnotation',
CREATE_ANNOTATION: 'createAnnotation',

/**
* The guest is asking the sidebar to relay the message to open the sidebar.
Expand Down
41 changes: 19 additions & 22 deletions src/sidebar/services/frame-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,29 +147,26 @@ export class FrameSyncService {
*/
this._setupSyncFromGuests = () => {
// A new annotation, note or highlight was created in the frame
this._guestRPC.on(
guestToSidebarEvents.BEFORE_CREATE_ANNOTATION,
event => {
const annot = Object.assign({}, event.msg, { $tag: event.tag });
// If user is not logged in, we can't really create a meaningful highlight
// or annotation. Instead, we need to open the sidebar, show an error,
// and delete the (unsaved) annotation so it gets un-selected in the
// target document
if (!store.isLoggedIn()) {
this._hostRPC.call(sidebarToHostEvents.OPEN_SIDEBAR);
store.openSidebarPanel('loginPrompt');
this._guestRPC.call(
sidebarToGuestEvents.DELETE_ANNOTATION,
formatAnnot(annot)
);
return;
}
inFrame.add(event.tag);

// Create the new annotation in the sidebar.
annotationsService.create(annot);
this._guestRPC.on(guestToSidebarEvents.CREATE_ANNOTATION, event => {
const annot = Object.assign({}, event.msg, { $tag: event.tag });
// If user is not logged in, we can't really create a meaningful highlight
// or annotation. Instead, we need to open the sidebar, show an error,
// and delete the (unsaved) annotation so it gets un-selected in the
// target document
if (!store.isLoggedIn()) {
this._hostRPC.call(sidebarToHostEvents.OPEN_SIDEBAR);
store.openSidebarPanel('loginPrompt');
this._guestRPC.call(
sidebarToGuestEvents.DELETE_ANNOTATION,
formatAnnot(annot)
);
return;
}
);
inFrame.add(event.tag);

// Create the new annotation in the sidebar.
annotationsService.create(annot);
});

// Map of annotation tag to anchoring status
// ('anchored'|'orphan'|'timeout').
Expand Down
10 changes: 5 additions & 5 deletions src/sidebar/services/test/frame-sync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ describe('FrameSyncService', () => {
fakeStore.isLoggedIn.returns(true);
const ann = { target: [] };

guestBridge().emit(guestToSidebarEvents.BEFORE_CREATE_ANNOTATION, {
guestBridge().emit(guestToSidebarEvents.CREATE_ANNOTATION, {
tag: 't1',
msg: ann,
});
Expand All @@ -352,7 +352,7 @@ describe('FrameSyncService', () => {
it('should not create an annotation in the sidebar', () => {
const ann = { target: [] };

guestBridge().emit(guestToSidebarEvents.BEFORE_CREATE_ANNOTATION, {
guestBridge().emit(guestToSidebarEvents.CREATE_ANNOTATION, {
tag: 't1',
msg: ann,
});
Expand All @@ -362,7 +362,7 @@ describe('FrameSyncService', () => {

it('should open the sidebar', () => {
const ann = { target: [] };
guestBridge().emit(guestToSidebarEvents.BEFORE_CREATE_ANNOTATION, {
guestBridge().emit(guestToSidebarEvents.CREATE_ANNOTATION, {
tag: 't1',
msg: ann,
});
Expand All @@ -372,7 +372,7 @@ describe('FrameSyncService', () => {

it('should open the login prompt panel', () => {
const ann = { target: [] };
guestBridge().emit(guestToSidebarEvents.BEFORE_CREATE_ANNOTATION, {
guestBridge().emit(guestToSidebarEvents.CREATE_ANNOTATION, {
tag: 't1',
msg: ann,
});
Expand All @@ -382,7 +382,7 @@ describe('FrameSyncService', () => {

it('should send a "deleteAnnotation" message to the frame', () => {
const ann = { target: [] };
guestBridge().emit(guestToSidebarEvents.BEFORE_CREATE_ANNOTATION, {
guestBridge().emit(guestToSidebarEvents.CREATE_ANNOTATION, {
tag: 't1',
msg: ann,
});
Expand Down

0 comments on commit 957b44f

Please sign in to comment.