Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FS-8673 Add alt text to images #550

Merged
merged 16 commits into from
Apr 19, 2024
Merged
2 changes: 1 addition & 1 deletion .github/workflows/deploy_beta.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: filestack-js-beta
on:
push:
branches: [ develop ]
branches: [ sc-add-alt-text ]
jobs:
build:
runs-on: ubuntu-latest
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ If you're using [Sentry](https://sentry.io/welcome/) to monitor your application
## Versioning

We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags](https://github.com/filestack/filestack-js/tags) on this repository.

## Contributing

We follow the [conventional commits](https://conventionalcommits.org/) specification to ensure consistent commit messages and changelog formatting.
3 changes: 1 addition & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
/**
* @private
*/
const PICKER_VERSION = '1.26.2';

const PICKER_VERSION = 'beta';
/**
* @private
*/
Expand Down
2 changes: 2 additions & 0 deletions src/lib/api/upload/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export class File {

public uploadTags: UploadTags;

public alt: string;

constructor(private readonly _file: FileInstance, private readonly _sanitizeOptions?: SanitizeOptions) {
this._file.name = sanitizeName(this._file.name, this._sanitizeOptions);
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/api/upload/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ export interface UploadOptions {
* @memberof UploadOptions
*/
tags?: UploadTags;

altText?: string;
}

export type StoreUploadOptions = StoreBaseParams & {
Expand Down
7 changes: 6 additions & 1 deletion src/lib/api/upload/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,18 @@ export class Upload extends EventEmitter {
* Upload single file
*
* @param {(InputFile)} file
* @param {(string)} altText
* @returns {Promise<any>}
* @memberof Upload
*/
async upload(input: InputFile): Promise<any> {
async upload(input: InputFile, altText?: string): Promise<any> {

const f = await getFile(input, this.sanitizerOptions);
f.customName = this.overrideFileName;
if (altText) {
f.alt = altText;
}

this.uploader.addFile(f);

this.startProgressInterval();
Expand Down
3 changes: 2 additions & 1 deletion src/lib/api/upload/uploaders/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export class S3Uploader extends UploaderAbstract {
location_url: payload.location_url,
upload_id: payload.upload_id,
region: payload.region,
alt: payload.file.alt,
};

if (this.uploadMode === UploadMode.INTELLIGENT || (this.uploadMode === UploadMode.FALLBACK && fiiFallback)) {
Expand Down Expand Up @@ -687,7 +688,7 @@ export class S3Uploader extends UploaderAbstract {
return FsRequest.post(
`${this.getUploadUrl(id)}/multipart/complete`,
{
...this.getDefaultFields(id, ['apikey', 'policy', 'signature', 'uri', 'region', 'upload_id', 'fii'], true),
...this.getDefaultFields(id, ['apikey', 'policy', 'signature', 'uri', 'region', 'upload_id', 'fii', 'alt'], true),
// method specific keys
filename: payload.file.name,
mimetype: payload.file.type,
Expand Down
27 changes: 25 additions & 2 deletions src/lib/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,30 @@ describe('client', () => {

expect(Upload.prototype.setToken).toHaveBeenCalledWith(token);
expect(Upload.prototype.setSecurity).toHaveBeenCalledWith(defaultSecurity);
expect(Upload.prototype.upload).toHaveBeenCalledWith(file);
expect(Upload.prototype.upload).toHaveBeenCalledWith(file, undefined);
});

it('should be able to upload file with alt text', async () => {
const client = new Client(defaultApikey);
const file = 'anyFile';
const uploadOptions = {
altText: 'alt',
};
const storeOptions = {};
const token = {};

jest.spyOn(Upload.prototype, 'upload').mockImplementation(() => Promise.resolve());

await client.upload(file, uploadOptions, storeOptions, token, defaultSecurity);

expect(Upload.prototype.setSession).toHaveBeenCalledWith({
apikey: defaultApikey,
urls: sessionURls,
});

expect(Upload.prototype.setToken).toHaveBeenCalledWith(token);
expect(Upload.prototype.setSecurity).toHaveBeenCalledWith(defaultSecurity);
expect(Upload.prototype.upload).toHaveBeenCalledWith(file, uploadOptions.altText);
});

it('should be able to upload file without token and security', async () => {
Expand All @@ -228,7 +251,7 @@ describe('client', () => {
urls: sessionURls,
});

expect(Upload.prototype.upload).toHaveBeenCalledWith(file);
expect(Upload.prototype.upload).toHaveBeenCalledWith(file, undefined);
});

it('should emit error', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ export class Client extends EventEmitter {
this.emit('upload.error', e);
});

return upload.upload(file);
return upload.upload(file, options.altText);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/lib/picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ export interface PickerFileMetadata {
* The Filestack CDN URL for the uploaded file.
*/
url: string;
/**
* Alt text for images
*/
alt: string;
}

export interface CustomAuthTextOptions {
Expand Down Expand Up @@ -683,6 +687,10 @@ export interface PickerOptions {
* Specify options for images passed to the crop UI.
*/
transformations?: PickerTransformationOptions;
/**
* Whether to use the new transformations UI. Defaults to `false`.
*/
useNewTransformer?: boolean;
/**
* Options for local file uploads.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/schema/picker.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ export const PickerParamsSchema = {
useSentryBreadcrumbs: {
type: 'boolean',
},
useNewTransformer: {
type: 'boolean',
},
pasteMode: {
type: 'object',
additionalProperties: false,
Expand Down
4 changes: 4 additions & 0 deletions src/schema/upload.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,9 @@ export const UploadParamsSchema = {
maxlength: 256,
},
},
altText: {
type: ['string', 'null'],
maxLength: 60,
},
},
};
Loading