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')
scrollIntoView = require('scroll-into-view')
{ findClosestOffscreenAnchor, constructPositionPoints, buildBuckets } = require('./bucket-bar-js')
{ findClosestOffscreenAnchor, constructPositionPoints, buildBuckets } = require('../util/buckets')
highlighter = require('../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.
*
* @typedef {[pixelPosition: number, startOrEnd: (-1 | 1), anchor: Anchor]} PositionPoint
......@@ -98,7 +98,7 @@ export function findClosestOffscreenAnchor(anchors, direction) {
export function constructPositionPoints(anchors) {
const aboveScreenAnchors = new Set();
const belowScreenAnchors = new Set();
const points = /** @type {PositionPoint[]} */ (new Array());
const points = /** @type {PositionPoint[]} */ ([]);
for (let anchor of anchors) {
if (!anchor.highlights?.length) {
......@@ -147,14 +147,14 @@ export function constructPositionPoints(anchors) {
* @return {BucketInfo}
*/
export function buildBuckets(points) {
const buckets = /** @type {Array<Anchor[]>} */ (new Array());
const bucketPositions = /** @type {number[]} */ (new Array());
const buckets = /** @type {Array<Anchor[]>} */ ([]);
const bucketPositions = /** @type {number[]} */ ([]);
// Anchors that are part of the currently-being-built bucket, and a correspon-
// ding count of unclosed top edges seen for that anchor
const current = /** @type {{anchors: Anchor[], counts: number[] }} */ ({
anchors: new Array(),
counts: new Array(),
anchors: [],
counts: [],
});
points.forEach((point, index) => {
......@@ -223,12 +223,12 @@ export function buildBuckets(points) {
// from the buckets collection, and re-add 1 merged bucket (always)
// 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
let penultimateBucket = new Array();
let penultimateBucket = [];
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,
// we'll end up with a misalignment in the `bucketPositions` collection.
// Remove the last entry here, as it corresponds to the ultimate bucket,
......
......@@ -2,8 +2,8 @@ import {
findClosestOffscreenAnchor,
constructPositionPoints,
buildBuckets,
} from '../bucket-bar-js';
import { $imports } from '../bucket-bar-js';
} from '../buckets';
import { $imports } from '../buckets';
function fakeAnchorFactory() {
let highlightIndex = 0;
......@@ -14,7 +14,7 @@ function fakeAnchorFactory() {
};
}
describe('annotator/plugin/bucket-bar-js', () => {
describe('annotator/util/buckets', () => {
let fakeGetBoundingClientRect;
beforeEach(() => {
......@@ -175,7 +175,7 @@ describe('annotator/plugin/bucket-bar-js', () => {
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(() => {
return {
top: 250,
......@@ -200,6 +200,7 @@ describe('annotator/plugin/bucket-bar-js', () => {
}
});
});
describe('buildBuckets', () => {
it('should return empty buckets if points array is empty', () => {
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