Skip to content

Commit

Permalink
chore: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Conrad Chan committed Oct 26, 2020
1 parent 2dfadfb commit 39d928a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 45 deletions.
59 changes: 31 additions & 28 deletions src/lib/AnnotationControlsFSM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,43 +51,46 @@ export default class AnnotationControlsFSM {

public getState = (): AnnotationState => this.currentState;

private notify = (): void => this.subscriptions.forEach(callback => callback(this.currentState));

public subscribe = (callback: Subscription): void => this.subscriptions.push(callback);
public subscribe = (callback: Subscription): void => {
this.subscriptions.push(callback);
};

public transition = (input: AnnotationInput, mode: AnnotationMode = AnnotationMode.NONE): AnnotationMode => {
if (input === AnnotationInput.CLICK) {
this.currentState = mode === stateModeMap[this.currentState] ? AnnotationState.NONE : modeStateMap[mode];
this.notify();
return stateModeMap[this.currentState];
}

if (input === AnnotationInput.RESET) {
this.currentState = AnnotationState.NONE;
this.notify();
return stateModeMap[this.currentState];
}
let newState = this.currentState;

switch (this.currentState) {
case AnnotationState.NONE:
if (input === AnnotationInput.CREATE || input === AnnotationInput.STARTED) {
this.currentState = modeTempStateMap[mode] || AnnotationState.NONE;
}
break;
case AnnotationState.HIGHLIGHT_TEMP:
case AnnotationState.REGION_TEMP:
if (input === AnnotationInput.CANCEL || input === AnnotationInput.SUCCESS) {
this.currentState = AnnotationState.NONE;
}
break;
default:
if (input === AnnotationInput.CLICK) {
newState = mode === stateModeMap[this.currentState] ? AnnotationState.NONE : modeStateMap[mode];
} else if (input === AnnotationInput.RESET) {
newState = AnnotationState.NONE;
} else {
switch (this.currentState) {
case AnnotationState.NONE:
if (input === AnnotationInput.CREATE || input === AnnotationInput.STARTED) {
newState = modeTempStateMap[mode] || AnnotationState.NONE;
}
break;
case AnnotationState.HIGHLIGHT_TEMP:
case AnnotationState.REGION_TEMP:
if (input === AnnotationInput.CANCEL || input === AnnotationInput.SUCCESS) {
newState = AnnotationState.NONE;
}
break;
default:
}
}

this.notify();
this.setState(newState);
return stateModeMap[this.currentState];
};

public unsubscribe = (callback: Subscription): void => {
this.subscriptions = this.subscriptions.filter(subscription => subscription !== callback);
};

private notify = (): void => this.subscriptions.forEach(callback => callback(this.currentState));

private setState = (state: AnnotationState): void => {
this.currentState = state;
this.notify();
};
}
2 changes: 2 additions & 0 deletions src/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export const CLASS_IS_BUFFERING = 'bp-is-buffering';
export const CLASS_DARK = 'bp-dark';
export const CLASS_CRAWLER = 'bp-crawler';

export const DISCOVERABILITY_ATTRIBUTE = 'data-resin-discoverability';

export const SELECTOR_BOX_PREVIEW_CONTAINER = `.${CLASS_BOX_PREVIEW_CONTAINER}`;
export const SELECTOR_BOX_PREVIEW = `.${CLASS_BOX_PREVIEW}`;
export const SELECTOR_BOX_PREVIEW_CONTENT = `.${CLASS_BOX_PREVIEW_CONTENT}`;
Expand Down
12 changes: 5 additions & 7 deletions src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
CLASS_CRAWLER,
CLASS_HIDDEN,
CLASS_IS_SCROLLABLE,
DISCOVERABILITY_ATTRIBUTE,
DOC_STATIC_ASSETS_VERSION,
ENCODING_TYPES,
PERMISSION_DOWNLOAD,
Expand Down Expand Up @@ -100,8 +101,6 @@ class DocBaseViewer extends BaseViewer {
constructor(options) {
super(options);

this.annotationControlsFSM.subscribe(this.handleFSMChange);

// Bind context for callbacks
this.emitMetric = this.emitMetric.bind(this);
this.handleAssetAndRepLoad = this.handleAssetAndRepLoad.bind(this);
Expand All @@ -121,8 +120,11 @@ class DocBaseViewer extends BaseViewer {
this.setPage = this.setPage.bind(this);
this.throttledScrollHandler = this.getScrollHandler().bind(this);
this.toggleThumbnails = this.toggleThumbnails.bind(this);
this.updateDiscoverabilityResinTag = this.updateDiscoverabilityResinTag.bind(this);
this.zoomIn = this.zoomIn.bind(this);
this.zoomOut = this.zoomOut.bind(this);

this.annotationControlsFSM.subscribe(this.updateDiscoverabilityResinTag);
}

/**
Expand Down Expand Up @@ -1625,10 +1627,6 @@ class DocBaseViewer extends BaseViewer {
this.processAnnotationModeChange(this.annotationControlsFSM.transition(status, type));
}

handleFSMChange = () => {
this.updateDiscoverabilityResinTag();
};

updateDiscoverabilityResinTag() {
if (!this.containerEl) {
return;
Expand All @@ -1640,7 +1638,7 @@ class DocBaseViewer extends BaseViewer {

// For tracking purposes, set property to true when the annotation controls are in a state
// in which the default discoverability experience is enabled
this.containerEl.setAttribute('data-resin-discoverability', isUsingDiscoverability);
this.containerEl.setAttribute(DISCOVERABILITY_ATTRIBUTE, isUsingDiscoverability);
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/lib/viewers/image/ImageViewer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ImageBaseViewer from './ImageBaseViewer';
import AnnotationControls, { AnnotationMode } from '../../AnnotationControls';
import AnnotationControlsFSM, { AnnotationInput, AnnotationState, stateModeMap } from '../../AnnotationControlsFSM';
import { CLASS_INVISIBLE } from '../../constants';
import { CLASS_INVISIBLE, DISCOVERABILITY_ATTRIBUTE } from '../../constants';
import { ICON_FULLSCREEN_IN, ICON_FULLSCREEN_OUT, ICON_ROTATE_LEFT } from '../../icons/icons';
import './Image.scss';

Expand All @@ -14,19 +14,23 @@ class ImageViewer extends ImageBaseViewer {
constructor(options) {
super(options);
this.api = options.api;
this.rotateLeft = this.rotateLeft.bind(this);
this.updatePannability = this.updatePannability.bind(this);

// Bind context for callbacks
this.getViewportDimensions = this.getViewportDimensions.bind(this);
this.handleAnnotationControlsClick = this.handleAnnotationControlsClick.bind(this);
this.handleAnnotationCreateEvent = this.handleAnnotationCreateEvent.bind(this);
this.handleAssetAndRepLoad = this.handleAssetAndRepLoad.bind(this);
this.handleImageDownloadError = this.handleImageDownloadError.bind(this);
this.getViewportDimensions = this.getViewportDimensions.bind(this);
this.handleZoomEvent = this.handleZoomEvent.bind(this);
this.rotateLeft = this.rotateLeft.bind(this);
this.updateDiscoverabilityResinTag = this.updateDiscoverabilityResinTag.bind(this);
this.updatePannability = this.updatePannability.bind(this);

this.annotationControlsFSM = new AnnotationControlsFSM(
this.options.enableAnnotationsImageDiscoverability ? AnnotationState.REGION_TEMP : AnnotationState.NONE,
);

this.annotationControlsFSM.subscribe(this.handleFSMChange);
this.annotationControlsFSM.subscribe(this.updateDiscoverabilityResinTag);

if (this.isMobile) {
this.handleOrientationChange = this.handleOrientationChange.bind(this);
Expand Down Expand Up @@ -529,10 +533,6 @@ class ImageViewer extends ImageBaseViewer {
}
}

handleFSMChange = () => {
this.updateDiscoverabilityResinTag();
};

updateDiscoverabilityResinTag() {
if (!this.containerEl) {
return;
Expand All @@ -543,7 +543,7 @@ class ImageViewer extends ImageBaseViewer {

// For tracking purposes, set property to true when the annotation controls are in a state
// in which the default discoverability experience is enabled
this.containerEl.setAttribute('data-resin-discoverability', isUsingDiscoverability);
this.containerEl.setAttribute(DISCOVERABILITY_ATTRIBUTE, isUsingDiscoverability);
}
}

Expand Down

0 comments on commit 39d928a

Please sign in to comment.