@internetarchive/collection-browser 4.3.2-rc-webdev-8334.3 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,187 +1,214 @@
1
- import { css, CSSResultGroup, html, LitElement, nothing } from 'lit';
2
- import { customElement, property, query, state } from 'lit/decorators.js';
3
- import { ClassInfo, classMap } from 'lit/directives/class-map.js';
4
-
5
- import type { TileModel } from '../models';
6
-
7
- import {
8
- baseItemImageStyles,
9
- waveformGradientStyles,
10
- } from '../styles/item-image-styles';
11
- import { searchIcon } from '../assets/img/icons/mediatype/search';
12
-
13
- @customElement('item-image')
14
- export class ItemImage extends LitElement {
15
- @property({ type: Object }) model?: TileModel;
16
-
17
- @property({ type: String }) baseImageUrl?: string;
18
-
19
- @property({ type: Boolean }) isListTile = false;
20
-
21
- @property({ type: Boolean }) isCompactTile = false;
22
-
23
- @property({ type: Boolean }) loggedIn = false;
24
-
25
- @property({ type: Boolean }) suppressBlurring = false;
26
-
27
- @state() private isWaveform = false;
28
-
29
- @state() private isNotFound = false;
30
-
31
- @query('img') private baseImage!: HTMLImageElement;
32
-
33
- render() {
34
- return html`
35
- <div class=${classMap(this.itemBaseClass)}>${this.imageTemplate}</div>
36
- `;
37
- }
38
-
39
- private get imageTemplate() {
40
- if (this.model?.mediatype === 'search') {
41
- return html`${searchIcon}`;
42
- }
43
-
44
- return html`
45
- <img
46
- class=${classMap(this.itemImageClass)}
47
- src="${this.imageSrc}"
48
- alt=""
49
- @load=${this.onLoad}
50
- @error=${this.onError}
51
- />
52
- `;
53
- }
54
-
55
- /**
56
- * Helpers
57
- */
58
- private get imageSrc() {
59
- if (this.isNotFound) return this.notFoundSrc;
60
-
61
- // Use the correct image for web capture tiles, if possible
62
- if (this.model?.captureDates && this.model.identifier) {
63
- try {
64
- const url = new URL(this.model.identifier);
65
- const domain = encodeURIComponent(url.hostname);
66
- return this.baseImageUrl
67
- ? `https://web.archive.org/thumb/${domain}?generate=1`
68
- : nothing;
69
- } catch {
70
- return `${this.baseImageUrl}/images/notfound.png`;
71
- }
72
- }
73
-
74
- // Use the thumbnail URL specified in the model if it exists
75
- if (this.model?.thumbnailUrl) return this.model.thumbnailUrl;
76
-
77
- // Don't try to load invalid image URLs
78
- return this.baseImageUrl && this.model?.identifier
79
- ? `${this.baseImageUrl}/services/img/${this.model.identifier}`
80
- : nothing;
81
- }
82
-
83
- private get notFoundSrc() {
84
- return this.baseImageUrl
85
- ? `${this.baseImageUrl}/images/notfound.png`
86
- : nothing;
87
- }
88
-
89
- private get hashBasedGradient() {
90
- if (!this.model?.identifier) {
91
- return 'waveform-grad0';
92
- }
93
- const gradient = this.hashStrToInt(this.model.identifier) % 6; // returns 0-5
94
- return `waveform-grad${gradient}`;
95
- }
96
-
97
- private hashStrToInt(str: string): number {
98
- return str
99
- .split('')
100
- .reduce((acc: number, char: string) => acc + char.charCodeAt(0), 0);
101
- }
102
-
103
- /**
104
- * Classes
105
- */
106
- private get itemBaseClass(): ClassInfo {
107
- return {
108
- 'drop-shadow': true,
109
- 'list-box': this.isListTile,
110
- 'search-image': this.model?.mediatype === 'search',
111
- [this.hashBasedGradient]: this.isWaveform,
112
- };
113
- }
114
-
115
- private get itemImageClass(): ClassInfo {
116
- const hasSensitiveContent = !!(
117
- this.model?.contentWarning || this.model?.loginRequired
118
- );
119
- const shouldBlur = hasSensitiveContent && !this.suppressBlurring;
120
-
121
- return {
122
- contain: !this.isCompactTile && !this.isWaveform,
123
- cover: this.isCompactTile,
124
- blur: shouldBlur,
125
- waveform: this.isWaveform,
126
- 'account-image': this.isAccountImage, // for account tile image
127
- 'collection-image': this.model?.mediatype === 'collection', // for collection tile image
128
- };
129
- }
130
-
131
- /**
132
- * Helper function to determine if account tile image
133
- */
134
- private get isAccountImage() {
135
- return (
136
- this.model?.mediatype === 'account' &&
137
- !this.isCompactTile &&
138
- !this.isListTile
139
- );
140
- }
141
-
142
- /**
143
- * Event listener sets isWaveform true if image is waveform
144
- */
145
- private onLoad() {
146
- if (
147
- (this.model?.mediatype === 'audio' ||
148
- this.model?.mediatype === 'etree') &&
149
- this.baseImage.naturalWidth / this.baseImage.naturalHeight === 4
150
- ) {
151
- this.isWaveform = true;
152
- }
153
- }
154
-
155
- private onError() {
156
- this.isNotFound = true;
157
- }
158
-
159
- /**
160
- * CSS
161
- */
162
- static get styles(): CSSResultGroup {
163
- return [
164
- baseItemImageStyles,
165
- waveformGradientStyles,
166
- css`
167
- img {
168
- height: var(--imgHeight, 16rem);
169
- width: var(--imgWidth, 16rem);
170
- }
171
-
172
- .search-image {
173
- display: flex;
174
- align-items: center;
175
- justify-content: center;
176
- background: rgb(245, 245, 247);
177
- border-radius: 4px;
178
- }
179
-
180
- svg {
181
- height: 10rem;
182
- width: 10rem;
183
- }
184
- `,
185
- ];
186
- }
187
- }
1
+ import {
2
+ css,
3
+ CSSResultGroup,
4
+ html,
5
+ LitElement,
6
+ nothing,
7
+ PropertyValues,
8
+ } from 'lit';
9
+ import { customElement, property, query, state } from 'lit/decorators.js';
10
+ import { ClassInfo, classMap } from 'lit/directives/class-map.js';
11
+
12
+ import type { TileModel } from '../models';
13
+
14
+ import {
15
+ baseItemImageStyles,
16
+ waveformGradientStyles,
17
+ } from '../styles/item-image-styles';
18
+ import { searchIcon } from '../assets/img/icons/mediatype/search';
19
+
20
+ @customElement('item-image')
21
+ export class ItemImage extends LitElement {
22
+ /**
23
+ * Map to cache which identifiers have waveform-style thumbnails, so that
24
+ * they can have their waveform styling applied immediately, rather than
25
+ * waiting for the image content to load before applying it (which can
26
+ * cause noticeable flicker when such tiles refresh).
27
+ */
28
+ private static readonly waveformByIdentifier = new Map<string, boolean>();
29
+
30
+ @property({ type: Object }) model?: TileModel;
31
+
32
+ @property({ type: String }) baseImageUrl?: string;
33
+
34
+ @property({ type: Boolean }) isListTile = false;
35
+
36
+ @property({ type: Boolean }) isCompactTile = false;
37
+
38
+ @property({ type: Boolean }) loggedIn = false;
39
+
40
+ @property({ type: Boolean }) suppressBlurring = false;
41
+
42
+ @state() private isWaveform = false;
43
+
44
+ @state() private isNotFound = false;
45
+
46
+ @query('img') private baseImage!: HTMLImageElement;
47
+
48
+ protected willUpdate(changed: PropertyValues): void {
49
+ if (changed.has('model')) {
50
+ // If this identifier is known to have a waveform image, then set isWaveform upfront
51
+ const identifier = this.model?.identifier;
52
+ this.isWaveform =
53
+ ItemImage.waveformByIdentifier.get(identifier as string) === true;
54
+ }
55
+ }
56
+
57
+ render() {
58
+ return html`
59
+ <div class=${classMap(this.itemBaseClass)}>${this.imageTemplate}</div>
60
+ `;
61
+ }
62
+
63
+ private get imageTemplate() {
64
+ if (this.model?.mediatype === 'search') {
65
+ return html`${searchIcon}`;
66
+ }
67
+
68
+ return html`
69
+ <img
70
+ class=${classMap(this.itemImageClass)}
71
+ src="${this.imageSrc}"
72
+ alt=""
73
+ @load=${this.onLoad}
74
+ @error=${this.onError}
75
+ />
76
+ `;
77
+ }
78
+
79
+ /**
80
+ * Helpers
81
+ */
82
+ private get imageSrc() {
83
+ if (this.isNotFound) return this.notFoundSrc;
84
+
85
+ // Use the correct image for web capture tiles, if possible
86
+ if (this.model?.captureDates && this.model.identifier) {
87
+ try {
88
+ const url = new URL(this.model.identifier);
89
+ const domain = encodeURIComponent(url.hostname);
90
+ return this.baseImageUrl
91
+ ? `https://web.archive.org/thumb/${domain}?generate=1`
92
+ : nothing;
93
+ } catch {
94
+ return `${this.baseImageUrl}/images/notfound.png`;
95
+ }
96
+ }
97
+
98
+ // Use the thumbnail URL specified in the model if it exists
99
+ if (this.model?.thumbnailUrl) return this.model.thumbnailUrl;
100
+
101
+ // Don't try to load invalid image URLs
102
+ return this.baseImageUrl && this.model?.identifier
103
+ ? `${this.baseImageUrl}/services/img/${this.model.identifier}`
104
+ : nothing;
105
+ }
106
+
107
+ private get notFoundSrc() {
108
+ return this.baseImageUrl
109
+ ? `${this.baseImageUrl}/images/notfound.png`
110
+ : nothing;
111
+ }
112
+
113
+ private get hashBasedGradient() {
114
+ if (!this.model?.identifier) {
115
+ return 'waveform-grad0';
116
+ }
117
+ const gradient = this.hashStrToInt(this.model.identifier) % 6; // returns 0-5
118
+ return `waveform-grad${gradient}`;
119
+ }
120
+
121
+ private hashStrToInt(str: string): number {
122
+ return str
123
+ .split('')
124
+ .reduce((acc: number, char: string) => acc + char.charCodeAt(0), 0);
125
+ }
126
+
127
+ /**
128
+ * Classes
129
+ */
130
+ private get itemBaseClass(): ClassInfo {
131
+ return {
132
+ 'drop-shadow': true,
133
+ 'list-box': this.isListTile,
134
+ 'search-image': this.model?.mediatype === 'search',
135
+ [this.hashBasedGradient]: this.isWaveform,
136
+ };
137
+ }
138
+
139
+ private get itemImageClass(): ClassInfo {
140
+ const hasSensitiveContent = !!(
141
+ this.model?.contentWarning || this.model?.loginRequired
142
+ );
143
+ const shouldBlur = hasSensitiveContent && !this.suppressBlurring;
144
+
145
+ return {
146
+ contain: !this.isCompactTile && !this.isWaveform,
147
+ cover: this.isCompactTile,
148
+ blur: shouldBlur,
149
+ waveform: this.isWaveform,
150
+ 'account-image': this.isAccountImage, // for account tile image
151
+ 'collection-image': this.model?.mediatype === 'collection', // for collection tile image
152
+ };
153
+ }
154
+
155
+ /**
156
+ * Helper function to determine if account tile image
157
+ */
158
+ private get isAccountImage() {
159
+ return (
160
+ this.model?.mediatype === 'account' &&
161
+ !this.isCompactTile &&
162
+ !this.isListTile
163
+ );
164
+ }
165
+
166
+ /**
167
+ * Event listener sets isWaveform true if image is waveform
168
+ */
169
+ private onLoad() {
170
+ if (
171
+ (this.model?.mediatype === 'audio' ||
172
+ this.model?.mediatype === 'etree') &&
173
+ this.baseImage.naturalWidth / this.baseImage.naturalHeight === 4
174
+ ) {
175
+ this.isWaveform = true;
176
+ if (this.model?.identifier) {
177
+ ItemImage.waveformByIdentifier.set(this.model.identifier, true);
178
+ }
179
+ }
180
+ }
181
+
182
+ private onError() {
183
+ this.isNotFound = true;
184
+ }
185
+
186
+ /**
187
+ * CSS
188
+ */
189
+ static get styles(): CSSResultGroup {
190
+ return [
191
+ baseItemImageStyles,
192
+ waveformGradientStyles,
193
+ css`
194
+ img {
195
+ height: var(--imgHeight, 16rem);
196
+ width: var(--imgWidth, 16rem);
197
+ }
198
+
199
+ .search-image {
200
+ display: flex;
201
+ align-items: center;
202
+ justify-content: center;
203
+ background: rgb(245, 245, 247);
204
+ border-radius: 4px;
205
+ }
206
+
207
+ svg {
208
+ height: 10rem;
209
+ width: 10rem;
210
+ }
211
+ `,
212
+ ];
213
+ }
214
+ }
package/tsconfig.json CHANGED
@@ -1,25 +1,25 @@
1
- {
2
- "compilerOptions": {
3
- "target": "esnext",
4
- "module": "esnext",
5
- "moduleResolution": "bundler",
6
- "noEmitOnError": true,
7
- "lib": [
8
- "ESNext",
9
- "dom",
10
- "dom.iterable"
11
- ],
12
- "strict": true,
13
- "esModuleInterop": false,
14
- "allowSyntheticDefaultImports": true,
15
- "experimentalDecorators": true,
16
- "importHelpers": true,
17
- "outDir": "dist",
18
- "sourceMap": true,
19
- "inlineSources": true,
20
- "rootDir": "./",
21
- "declaration": true,
22
- "useDefineForClassFields": false,
23
- },
24
- "include": ["src", "test", "index.ts", "types"],
25
- }
1
+ {
2
+ "compilerOptions": {
3
+ "target": "esnext",
4
+ "module": "esnext",
5
+ "moduleResolution": "bundler",
6
+ "noEmitOnError": true,
7
+ "lib": [
8
+ "ESNext",
9
+ "dom",
10
+ "dom.iterable"
11
+ ],
12
+ "strict": true,
13
+ "esModuleInterop": false,
14
+ "allowSyntheticDefaultImports": true,
15
+ "experimentalDecorators": true,
16
+ "importHelpers": true,
17
+ "outDir": "dist",
18
+ "sourceMap": true,
19
+ "inlineSources": true,
20
+ "rootDir": "./",
21
+ "declaration": true,
22
+ "useDefineForClassFields": false,
23
+ },
24
+ "include": ["src", "test", "index.ts", "types"],
25
+ }
@@ -1,30 +1,30 @@
1
- // import { hmrPlugin, presets } from '@open-wc/dev-server-hmr';
2
-
3
- /** Use Hot Module replacement by adding --hmr to the start command */
4
- const hmr = process.argv.includes('--hmr');
5
-
6
- export default /** @type {import('@web/dev-server').DevServerConfig} */ ({
7
- nodeResolve: true,
8
- open: '/',
9
- watch: !hmr,
10
-
11
- /** Compile JS for older browsers. Requires @web/dev-server-esbuild plugin */
12
- // esbuildTarget: 'auto'
13
-
14
- /** Set appIndex to enable SPA routing */
15
- // appIndex: 'demo/index.html',
16
-
17
- /** Confgure bare import resolve plugin */
18
- // nodeResolve: {
19
- // exportConditions: ['browser', 'development']
20
- // },
21
-
22
- plugins: [
23
- /** Use Hot Module Replacement by uncommenting. Requires @open-wc/dev-server-hmr plugin */
24
- // hmr && hmrPlugin({ exclude: ['**/*/node_modules/**/*'], presets: [presets.litElement] }),
25
- ],
26
-
27
- http2: true,
28
- sslCert: './local.archive.org.cert',
29
- sslKey: './local.archive.org.key',
30
- });
1
+ // import { hmrPlugin, presets } from '@open-wc/dev-server-hmr';
2
+
3
+ /** Use Hot Module replacement by adding --hmr to the start command */
4
+ const hmr = process.argv.includes('--hmr');
5
+
6
+ export default /** @type {import('@web/dev-server').DevServerConfig} */ ({
7
+ nodeResolve: true,
8
+ open: '/',
9
+ watch: !hmr,
10
+
11
+ /** Compile JS for older browsers. Requires @web/dev-server-esbuild plugin */
12
+ // esbuildTarget: 'auto'
13
+
14
+ /** Set appIndex to enable SPA routing */
15
+ // appIndex: 'demo/index.html',
16
+
17
+ /** Confgure bare import resolve plugin */
18
+ // nodeResolve: {
19
+ // exportConditions: ['browser', 'development']
20
+ // },
21
+
22
+ plugins: [
23
+ /** Use Hot Module Replacement by uncommenting. Requires @open-wc/dev-server-hmr plugin */
24
+ // hmr && hmrPlugin({ exclude: ['**/*/node_modules/**/*'], presets: [presets.litElement] }),
25
+ ],
26
+
27
+ http2: true,
28
+ sslCert: './local.archive.org.cert',
29
+ sslKey: './local.archive.org.key',
30
+ });
@@ -1,52 +1,52 @@
1
- import rollupImage from '@rollup/plugin-image';
2
- import { rollupAdapter } from '@web/dev-server-rollup';
3
- // import { playwrightLauncher } from '@web/test-runner-playwright';
4
-
5
- const filteredLogs = ['Running in dev mode', 'lit-html is in dev mode'];
6
-
7
- export default /** @type {import("@web/test-runner").TestRunnerConfig} */ ({
8
- /** Test files to run */
9
- files: 'dist/test/**/*.test.js',
10
-
11
- /** Resolve bare module imports */
12
- nodeResolve: {
13
- exportConditions: ['browser', 'development'],
14
- },
15
-
16
- mimeTypes: {
17
- '**/*.scss': 'js',
18
- '**/*.css': 'js',
19
- '**/*.svg': 'js',
20
- '**/*.json': 'js',
21
- },
22
-
23
- /** Filter out lit dev mode logs */
24
- filterBrowserLogs(log) {
25
- for (const arg of log.args) {
26
- if (typeof arg === 'string' && filteredLogs.some(l => arg.includes(l))) {
27
- return false;
28
- }
29
- }
30
- return true;
31
- },
32
-
33
- plugins: [rollupAdapter(rollupImage())],
34
-
35
- /** Compile JS for older browsers. Requires @web/dev-server-esbuild plugin */
36
- // esbuildTarget: 'auto',
37
-
38
- /** Amount of browsers to run concurrently */
39
- // concurrentBrowsers: 2,
40
-
41
- /** Amount of test files per browser to test concurrently */
42
- // concurrency: 1,
43
-
44
- /** Browsers to run tests on */
45
- // browsers: [
46
- // playwrightLauncher({ product: 'chromium' }),
47
- // playwrightLauncher({ product: 'firefox' }),
48
- // playwrightLauncher({ product: 'webkit' }),
49
- // ],
50
-
51
- // See documentation for all available options
52
- });
1
+ import rollupImage from '@rollup/plugin-image';
2
+ import { rollupAdapter } from '@web/dev-server-rollup';
3
+ // import { playwrightLauncher } from '@web/test-runner-playwright';
4
+
5
+ const filteredLogs = ['Running in dev mode', 'lit-html is in dev mode'];
6
+
7
+ export default /** @type {import("@web/test-runner").TestRunnerConfig} */ ({
8
+ /** Test files to run */
9
+ files: 'dist/test/**/*.test.js',
10
+
11
+ /** Resolve bare module imports */
12
+ nodeResolve: {
13
+ exportConditions: ['browser', 'development'],
14
+ },
15
+
16
+ mimeTypes: {
17
+ '**/*.scss': 'js',
18
+ '**/*.css': 'js',
19
+ '**/*.svg': 'js',
20
+ '**/*.json': 'js',
21
+ },
22
+
23
+ /** Filter out lit dev mode logs */
24
+ filterBrowserLogs(log) {
25
+ for (const arg of log.args) {
26
+ if (typeof arg === 'string' && filteredLogs.some(l => arg.includes(l))) {
27
+ return false;
28
+ }
29
+ }
30
+ return true;
31
+ },
32
+
33
+ plugins: [rollupAdapter(rollupImage())],
34
+
35
+ /** Compile JS for older browsers. Requires @web/dev-server-esbuild plugin */
36
+ // esbuildTarget: 'auto',
37
+
38
+ /** Amount of browsers to run concurrently */
39
+ // concurrentBrowsers: 2,
40
+
41
+ /** Amount of test files per browser to test concurrently */
42
+ // concurrency: 1,
43
+
44
+ /** Browsers to run tests on */
45
+ // browsers: [
46
+ // playwrightLauncher({ product: 'chromium' }),
47
+ // playwrightLauncher({ product: 'firefox' }),
48
+ // playwrightLauncher({ product: 'webkit' }),
49
+ // ],
50
+
51
+ // See documentation for all available options
52
+ });