Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenwf committed Jan 16, 2024
1 parent c3090ae commit 32ea797
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 52 deletions.
2 changes: 2 additions & 0 deletions __tests__/i18n.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @jest-environment node
*/
import { buildLocaleString, getValue } from '../src/i18n';
import { describe, test, expect } from 'vitest';

describe('i18n helper', () => {
describe('buildLocaleString()', () => {
Expand All @@ -11,6 +12,7 @@ describe('i18n helper', () => {
expect(getValue({ es: ['Testing a value'] })).toEqual('Testing a value');
expect(getValue({ en: ['This value instead'], es: ['Testing a value'] })).toEqual('This value instead');
expect(getValue({ es: ['Testing a value'], en: ['This value instead'] })).toEqual('This value instead');
expect(getValue({ en: [''], nl: ['Testing a value'] })).toEqual('Testing a value');
});

test('it can empty values', () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/paintable-helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect } from 'vitest';
import { describe, test, expect } from 'vitest';
import choice from '../fixtures/cookbook/choice.json';
import composite from '../fixtures/cookbook/composite.json';
import { Vault } from '../src/vault';
Expand Down
2 changes: 1 addition & 1 deletion __tests__/parse-selector.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test } from 'vitest';
import { describe, expect, test } from 'vitest';
import { parseSelector } from '../src/annotation-targets/parse-selector';
import { JSDOM } from 'jsdom';

Expand Down
1 change: 1 addition & 0 deletions __tests__/style.tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Vault } from '../src/vault';
import { createStylesHelper } from '../src/styles';
import { describe, expect, test } from 'vitest';

describe('style helper', function () {
test('setting styles', () => {
Expand Down
8 changes: 4 additions & 4 deletions __tests__/thumbnail-helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { thumbnailFixtures } from '../fixtures.mjs';
import { expect, test } from 'vitest';
import { describe, expect, test } from 'vitest';
import { Vault } from '../src/vault';
import { createThumbnailHelper } from '../src/thumbnail';
import { ThumbnailOutput, createThumbnailHelper } from '../src/thumbnail';
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { ManifestNormalized } from '@iiif/presentation-3-normalized';
Expand All @@ -20,7 +20,7 @@ describe('Thumbnail helper', function () {
expect.fail(`Invalid manifest`);
}

const thumbnails = [];
const thumbnails: ThumbnailOutput[] = [];
for (const canvas of manifest.items) {
thumbnails.push(helper.getBestThumbnailAtSize(canvas, { width: 256, height: 256 }));
}
Expand All @@ -42,7 +42,7 @@ describe('Thumbnail helper', function () {
expect.fail(`Invalid manifest`);
}

const thumbnails = [];
const thumbnails: ThumbnailOutput[] = [];
for (const canvas of manifest.items) {
thumbnails.push(helper.getBestThumbnailAtSize(canvas, { width: 256, height: 256 }));
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
},
"devDependencies": {
"@atlas-viewer/iiif-image-api": "^2.1.1",
"@iiif/parser": "^2.0.0",
"@iiif/parser": "^2.0.1",
"@types/jsdom": "^21.1.3",
"@types/svg-arc-to-cubic-bezier": "^3.2.0",
"@typescript-eslint/eslint-plugin": "^6.7.4",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 25 additions & 4 deletions src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ export function getClosestLanguage(
i18nLanguage: string,
languages: string[],
i18nLanguages: string[] = [],
strictFallback = false
strictFallback = false,
skipLanguages: string[] = []
) {
if (skipLanguages.length) {
languages = languages.filter((l) => skipLanguages.indexOf(l) === -1);
}

if (!languages || languages.length === 0) {
return undefined;
}
Expand Down Expand Up @@ -77,13 +82,21 @@ export function buildLocaleString(
separator?: string;
fallbackLanguages?: string[];
closest?: boolean;
skipLanguages?: string[];
} = {}
) {
const { strictFallback = false, defaultText = '', separator = '\n', fallbackLanguages = [], closest } = options;
const {
strictFallback = false,
defaultText = '',
separator = '\n',
fallbackLanguages = [],
closest,
skipLanguages,
} = options;
const languages = Object.keys(inputText || {});
const language = closest
? i18nLanguage
: getClosestLanguage(i18nLanguage as any, languages, fallbackLanguages, strictFallback);
: getClosestLanguage(i18nLanguage as any, languages, fallbackLanguages, strictFallback, skipLanguages);

if (!inputText) {
return defaultText;
Expand All @@ -94,11 +107,19 @@ export function buildLocaleString(
}

const candidateText = language ? inputText[language] : undefined;
if (candidateText) {
if (candidateText && language) {
// Slightly tolerant of typos in IIIF like: `{"en": "Some value"}`
if (typeof candidateText === 'string') {
return candidateText;
}
// Skip empty strings.
if (candidateText.length === 1 && candidateText[0] === '') {
const skip: string[] = options.skipLanguages || [];
return buildLocaleString(inputText, i18nLanguage, {
...options,
skipLanguages: [...skip, language],
});
}
return candidateText.join(separator);
}

Expand Down
63 changes: 26 additions & 37 deletions src/thumbnail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,32 @@ import { compatVault, CompatVault } from './compat';

export const imageServiceLoader = new ImageServiceLoader();

export type ThumbnailInput =
| string
| Reference<CollectionItemSchemas>
| Reference<'Collection'>
| Reference<'Manifest'>
| Reference<'Canvas'>
| Reference<'Annotation'>
| Reference<'AnnotationPage'>
| Reference<'ContentResource'>
| CollectionNormalized
| ManifestNormalized
| CanvasNormalized
| AnnotationNormalized
| AnnotationPageNormalized
| ContentResource
| undefined;

export type ThumbnailOutput = Promise<{
best: null | undefined | FixedSizeImage | FixedSizeImageService | VariableSizeImage | UnknownSizeImage;
fallback: Array<ImageCandidate>;
log: string[];
}>;

const helpers: Map<CompatVault, ReturnType<typeof createThumbnailHelper>> = new Map();
export function getThumbnail(
input:
| string
| Reference<CollectionItemSchemas>
| Reference<'Collection'>
| Reference<'Manifest'>
| Reference<'Canvas'>
| Reference<'Annotation'>
| Reference<'AnnotationPage'>
| Reference<'ContentResource'>
| CollectionNormalized
| ManifestNormalized
| CanvasNormalized
| AnnotationNormalized
| AnnotationPageNormalized
| ContentResource
| undefined,
input: ThumbnailInput,
{
vault = compatVault,
dereference = false,
Expand All @@ -60,31 +68,12 @@ export function createThumbnailHelper(
const loader = dependencies.imageServiceLoader || imageServiceLoader;

async function getBestThumbnailAtSize(
input:
| string
| Reference<CollectionItemSchemas>
| Reference<'Collection'>
| Reference<'Manifest'>
| Reference<'Canvas'>
| Reference<'Annotation'>
| Reference<'AnnotationPage'>
| Reference<'ContentResource'>
| CollectionNormalized
| ManifestNormalized
| CanvasNormalized
| AnnotationNormalized
| AnnotationPageNormalized
| ContentResource
| undefined,
input: ThumbnailInput,
request: ImageCandidateRequest,
dereference = false,
candidates: Array<ImageCandidate> = [],
dimensions?: { width: number; height: number }
): Promise<{
best: null | undefined | FixedSizeImage | FixedSizeImageService | VariableSizeImage | UnknownSizeImage;
fallback: Array<ImageCandidate>;
log: string[];
}> {
): ThumbnailOutput {
const thumbnailNotFound = () => loader.getThumbnailFromResource(undefined as any, request, dereference, candidates);

if (!input) {
Expand Down

0 comments on commit 32ea797

Please sign in to comment.