Commit 9c82dbd0 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Move bucket utilities module into util dir

parent 6f846ffb
...@@ -3,7 +3,7 @@ $ = require('jquery') ...@@ -3,7 +3,7 @@ $ = require('jquery')
scrollIntoView = require('scroll-into-view') scrollIntoView = require('scroll-into-view')
{ findClosestOffscreenAnchor, constructPositionPoints, buildBuckets } = require('./bucket-bar-js') { findClosestOffscreenAnchor, constructPositionPoints, buildBuckets } = require('../util/buckets')
highlighter = require('../highlighter') highlighter = require('../highlighter')
......
...@@ -5,7 +5,7 @@ import { getBoundingClientRect } from '../highlighter'; ...@@ -5,7 +5,7 @@ import { getBoundingClientRect } from '../highlighter';
*/ */
/** /**
* A structured Array representing either the top (`startOrEnd` = 1) or the * A tuple representing either the top (`startOrEnd` = 1) or the
* bottom (`startOrEnd` = -1) of an anchor's highlight bounding box. * bottom (`startOrEnd` = -1) of an anchor's highlight bounding box.
* *
* @typedef {[pixelPosition: number, startOrEnd: (-1 | 1), anchor: Anchor]} PositionPoint * @typedef {[pixelPosition: number, startOrEnd: (-1 | 1), anchor: Anchor]} PositionPoint
...@@ -98,7 +98,7 @@ export function findClosestOffscreenAnchor(anchors, direction) { ...@@ -98,7 +98,7 @@ export function findClosestOffscreenAnchor(anchors, direction) {
export function constructPositionPoints(anchors) { export function constructPositionPoints(anchors) {
const aboveScreenAnchors = new Set(); const aboveScreenAnchors = new Set();
const belowScreenAnchors = new Set(); const belowScreenAnchors = new Set();
const points = /** @type {PositionPoint[]} */ (new Array()); const points = /** @type {PositionPoint[]} */ ([]);
for (let anchor of anchors) { for (let anchor of anchors) {
if (!anchor.highlights?.length) { if (!anchor.highlights?.length) {
...@@ -147,14 +147,14 @@ export function constructPositionPoints(anchors) { ...@@ -147,14 +147,14 @@ export function constructPositionPoints(anchors) {
* @return {BucketInfo} * @return {BucketInfo}
*/ */
export function buildBuckets(points) { export function buildBuckets(points) {
const buckets = /** @type {Array<Anchor[]>} */ (new Array()); const buckets = /** @type {Array<Anchor[]>} */ ([]);
const bucketPositions = /** @type {number[]} */ (new Array()); const bucketPositions = /** @type {number[]} */ ([]);
// Anchors that are part of the currently-being-built bucket, and a correspon- // Anchors that are part of the currently-being-built bucket, and a correspon-
// ding count of unclosed top edges seen for that anchor // ding count of unclosed top edges seen for that anchor
const current = /** @type {{anchors: Anchor[], counts: number[] }} */ ({ const current = /** @type {{anchors: Anchor[], counts: number[] }} */ ({
anchors: new Array(), anchors: [],
counts: new Array(), counts: [],
}); });
points.forEach((point, index) => { points.forEach((point, index) => {
...@@ -223,12 +223,12 @@ export function buildBuckets(points) { ...@@ -223,12 +223,12 @@ export function buildBuckets(points) {
// from the buckets collection, and re-add 1 merged bucket (always) // from the buckets collection, and re-add 1 merged bucket (always)
// Always pop off the last bucket // Always pop off the last bucket
const ultimateBucket = buckets.pop() || new Array(); const ultimateBucket = buckets.pop() || [];
// If there is a previous/penultimate bucket, pop that off, as well // If there is a previous/penultimate bucket, pop that off, as well
let penultimateBucket = new Array(); let penultimateBucket = [];
if (buckets[buckets.length - 1]?.length) { if (buckets[buckets.length - 1]?.length) {
penultimateBucket = buckets.pop() || new Array(); penultimateBucket = buckets.pop() || [];
// Because we're removing two buckets but only re-adding one below, // Because we're removing two buckets but only re-adding one below,
// we'll end up with a misalignment in the `bucketPositions` collection. // we'll end up with a misalignment in the `bucketPositions` collection.
// Remove the last entry here, as it corresponds to the ultimate bucket, // Remove the last entry here, as it corresponds to the ultimate bucket,
......
...@@ -2,8 +2,8 @@ import { ...@@ -2,8 +2,8 @@ import {
findClosestOffscreenAnchor, findClosestOffscreenAnchor,
constructPositionPoints, constructPositionPoints,
buildBuckets, buildBuckets,
} from '../bucket-bar-js'; } from '../buckets';
import { $imports } from '../bucket-bar-js'; import { $imports } from '../buckets';
function fakeAnchorFactory() { function fakeAnchorFactory() {
let highlightIndex = 0; let highlightIndex = 0;
...@@ -14,7 +14,7 @@ function fakeAnchorFactory() { ...@@ -14,7 +14,7 @@ function fakeAnchorFactory() {
}; };
} }
describe('annotator/plugin/bucket-bar-js', () => { describe('annotator/util/buckets', () => {
let fakeGetBoundingClientRect; let fakeGetBoundingClientRect;
beforeEach(() => { beforeEach(() => {
...@@ -175,7 +175,7 @@ describe('annotator/plugin/bucket-bar-js', () => { ...@@ -175,7 +175,7 @@ describe('annotator/plugin/bucket-bar-js', () => {
assert.deepEqual(positionPoints.points[3], [351, -1, fakeAnchors[3]]); assert.deepEqual(positionPoints.points[3], [351, -1, fakeAnchors[3]]);
}); });
it('sorts on-screen points based on position type secondarily', () => { it('sorts on-screen points based on position primarily, type secondarily', () => {
fakeGetBoundingClientRect.callsFake(() => { fakeGetBoundingClientRect.callsFake(() => {
return { return {
top: 250, top: 250,
...@@ -200,6 +200,7 @@ describe('annotator/plugin/bucket-bar-js', () => { ...@@ -200,6 +200,7 @@ describe('annotator/plugin/bucket-bar-js', () => {
} }
}); });
}); });
describe('buildBuckets', () => { describe('buildBuckets', () => {
it('should return empty buckets if points array is empty', () => { it('should return empty buckets if points array is empty', () => {
const bucketInfo = buildBuckets([]); const bucketInfo = buildBuckets([]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment