Skip to content

Commit ba6fcea

Browse files
committed
fix: add a store to be able to cache thumbnails
1 parent ce85f3c commit ba6fcea

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export const UMB_IMAGING_REPOSITORY_ALIAS = 'Umb.Repository.Imaging';
2+
export const UMB_IMAGING_STORE_ALIAS = 'Umb.Store.Imaging';
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import type { UmbImagingStore } from './imaging.store.js';
2+
import { UmbContextToken } from '@umbraco-cms/backoffice/context-api';
3+
4+
export const UMB_IMAGING_STORE_CONTEXT = new UmbContextToken<UmbImagingStore>('UmbImagingStore');
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { UMB_IMAGING_STORE_CONTEXT } from './imaging.store.token.js';
2+
import type { UmbImagingModel } from './types.js';
3+
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
4+
import type { UmbApi } from '@umbraco-cms/backoffice/extension-api';
5+
import { UmbContextBase } from '@umbraco-cms/backoffice/class-api';
6+
7+
export class UmbImagingStore extends UmbContextBase<never> implements UmbApi {
8+
#data;
9+
10+
constructor(host: UmbControllerHost) {
11+
super(host, UMB_IMAGING_STORE_CONTEXT.toString());
12+
this.#data = new Map<string, Map<string, string>>();
13+
}
14+
15+
/**
16+
* Gets the data from the store.
17+
*/
18+
getData(unique: string) {
19+
return this.#data.get(unique);
20+
}
21+
22+
/**
23+
* Gets a specific crop if it exists.
24+
*/
25+
getCrop(unique: string, data?: UmbImagingModel) {
26+
return this.#data.get(unique)?.get(this.#generateCropKey(data));
27+
}
28+
29+
/**
30+
* Adds a new crop to the store.
31+
*/
32+
addCrop(unique: string, urlInfo: string, data?: UmbImagingModel) {
33+
if (!this.#data.has(unique)) {
34+
this.#data.set(unique, new Map());
35+
}
36+
this.#data.get(unique)?.set(this.#generateCropKey(data), urlInfo);
37+
}
38+
39+
/**
40+
* Generates a unique key for the crop based on the width, height and mode.
41+
*/
42+
#generateCropKey(data?: UmbImagingModel) {
43+
return data ? `${data.width}x${data.height};${data.mode}` : 'generic';
44+
}
45+
}
46+
47+
export default UmbImagingStore;
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { UMB_IMAGING_REPOSITORY_ALIAS } from './constants.js';
2-
import type { ManifestRepository, ManifestTypes } from '@umbraco-cms/backoffice/extension-registry';
1+
import { UMB_IMAGING_REPOSITORY_ALIAS, UMB_IMAGING_STORE_ALIAS } from './constants.js';
2+
import type { ManifestRepository, ManifestStore, ManifestTypes } from '@umbraco-cms/backoffice/extension-registry';
33

44
const repository: ManifestRepository = {
55
type: 'repository',
@@ -8,4 +8,11 @@ const repository: ManifestRepository = {
88
api: () => import('./imaging.repository.js'),
99
};
1010

11-
export const manifests: Array<ManifestTypes> = [repository];
11+
const store: ManifestStore = {
12+
type: 'store',
13+
alias: UMB_IMAGING_STORE_ALIAS,
14+
name: 'Imaging Store',
15+
api: () => import('./imaging.store.js'),
16+
};
17+
18+
export const manifests: Array<ManifestTypes> = [repository, store];

0 commit comments

Comments
 (0)