Skip to content

Commit

Permalink
Add customizable side-by-side mode to HTMLIntegration
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Jul 7, 2023
1 parent 6aeeed8 commit df10f49
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
21 changes: 14 additions & 7 deletions src/annotator/integrations/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
FeatureFlags,
Integration,
SidebarLayout,
SideBySideOptions,
} from '../../types/annotator';
import type { Selector } from '../../types/api';
import { anchor, describe } from '../anchoring/html';
Expand Down Expand Up @@ -36,8 +37,9 @@ export class HTMLIntegration extends TinyEmitter implements Integration {
private _htmlMeta: HTMLMetadata;
private _prevURI: string;

/** Whether to attempt to resize the document to fit alongside sidebar. */
private _sideBySideEnabled: boolean;
/** Controls how we resize the document to fit alongside sidebar. */
private _sideBySideOptions: SideBySideOptions;
private _sideBySideFlagEnabled: boolean;

/**
* Whether the document is currently being resized to fit alongside an
Expand All @@ -53,9 +55,11 @@ export class HTMLIntegration extends TinyEmitter implements Integration {
constructor({
features,
container = document.body,
sideBySideOptions,
}: {
features: FeatureFlags;
container?: HTMLElement;
sideBySideOptions?: SideBySideOptions;
}) {
super();

Expand All @@ -65,7 +69,9 @@ export class HTMLIntegration extends TinyEmitter implements Integration {
this._htmlMeta = new HTMLMetadata();
this._prevURI = this._htmlMeta.uri();

this._sideBySideEnabled = this.features.flagEnabled('html_side_by_side');
this._sideBySideFlagEnabled =
this.features.flagEnabled('html_side_by_side');
this._sideBySideOptions = sideBySideOptions ?? { mode: 'auto' };
this._sideBySideActive = false;
this._lastLayout = null;

Expand All @@ -92,9 +98,9 @@ export class HTMLIntegration extends TinyEmitter implements Integration {
});

this._flagsChanged = () => {
const sideBySide = features.flagEnabled('html_side_by_side');
if (sideBySide !== this._sideBySideEnabled) {
this._sideBySideEnabled = sideBySide;
const sideBySideEnabled = features.flagEnabled('html_side_by_side');
if (sideBySideEnabled !== this._sideBySideFlagEnabled) {
this._sideBySideFlagEnabled = sideBySideEnabled;

// `fitSideBySide` is normally called by Guest when the sidebar layout
// changes. When the feature flag changes, we need to re-run the method.
Expand Down Expand Up @@ -156,7 +162,8 @@ export class HTMLIntegration extends TinyEmitter implements Integration {

const maximumWidthToFit = window.innerWidth - layout.width;
const active =
this._sideBySideEnabled &&
this._sideBySideFlagEnabled &&
this._sideBySideOptions.mode === 'auto' &&
layout.expanded &&
maximumWidthToFit >= MIN_HTML_WIDTH;

Expand Down
16 changes: 14 additions & 2 deletions src/annotator/integrations/test/html-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ describe('HTMLIntegration', () => {
$imports.$restore();
});

function createIntegration() {
return new HTMLIntegration({ features });
function createIntegration(sideBySideOptions) {
return new HTMLIntegration({ features, sideBySideOptions });
}

it('implements `anchor` and `destroy` using HTML anchoring', async () => {
Expand Down Expand Up @@ -380,6 +380,18 @@ describe('HTMLIntegration', () => {
features.update({ html_side_by_side: false });
assert.isFalse(isSideBySideActive());
});

it('manual side-by-side is not changed by enabled feature flag', () => {
features.update({ html_side_by_side: true });
const integration = createIntegration({ mode: 'manual' });

integration.fitSideBySide({ expanded: true, width: sidebarWidth });
assert.isFalse(isSideBySideActive());

// Even if the feature flag is enabled, side-by-side stays disabled/manual
features.update({ html_side_by_side: true });
assert.isFalse(isSideBySideActive());
});
});

describe('#getMetadata', () => {
Expand Down
13 changes: 13 additions & 0 deletions src/types/annotator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,16 @@ export type DocumentInfo = {
*/
persistent: boolean;
};

/**
* `auto`: The client will decide if side-by-side is enabled. If enabled, it
* will apply some heuristics to determine how the content is affected.
* This is default value.
* `manual`: The host app wants to manually take full control of side-by-side,
* effectively disabling the logic in client.
*/
export type SideBySideMode = 'auto' | 'manual';

export type SideBySideOptions = {
mode: SideBySideMode;
};

0 comments on commit df10f49

Please sign in to comment.