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

[WIP] feat(camera-tab): added aspectRatio #762

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions blocks/CameraSource/CameraSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { debounce } from '../utils/debounce.js';
import { UploadSource } from '../utils/UploadSource.js';

const DEFAULT_VIDEO_CONFIG = {

Check warning on line 8 in blocks/CameraSource/CameraSource.js

View workflow job for this annotation

GitHub Actions / build

'DEFAULT_VIDEO_CONFIG' is assigned a value but never used
width: {
ideal: 1920,
},
Expand Down Expand Up @@ -632,15 +632,15 @@

_capture = async () => {
const constraints = {
video: DEFAULT_VIDEO_CONFIG,
video: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like DEFAULT_VIDEO_CONFIG was forgotten here

aspectRatio: this.cfg.aspectRatio,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What browser will do if this type of constraint is not supported?

},
Comment on lines +635 to +637
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Validate aspectRatio before using in constraints.

The aspectRatio is directly used from config without validation. This could lead to invalid constraints if the config contains incorrect values.

Consider adding validation:

 video: {
-  aspectRatio: this.cfg.aspectRatio,
+  ...(this.cfg.aspectRatio && Object.keys(this.cfg.aspectRatio).length > 0
+    ? { aspectRatio: this.cfg.aspectRatio }
+    : DEFAULT_VIDEO_CONFIG),
 },

Committable suggestion skipped: line range outside the PR's diff.

audio: this.cfg.enableAudioRecording ? {} : false,
};

if (this._selectedCameraId) {
constraints.video = {
deviceId: {
exact: this._selectedCameraId,
},
constraints.video.deviceId = {
exact: this._selectedCameraId,
};
}

Expand Down
2 changes: 2 additions & 0 deletions blocks/Config/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const allConfigKeys = /** @type {(keyof import('../../types').ConfigType)[]} */
* 'fileValidators',
* 'collectionValidators',
* 'mediaRecorerOptions',
* 'aspectRatio',
* ]}
*/
export const complexConfigKeys = [
Expand All @@ -33,6 +34,7 @@ export const complexConfigKeys = [
'fileValidators',
'collectionValidators',
'mediaRecorerOptions',
'aspectRatio',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks too common config property name for me. Maybe something like mediaRecorderAspectRatio?

];

/** @type {(key: keyof import('../../types').ConfigType) => key is keyof import('../../types').ConfigComplexType} */
Expand Down
1 change: 1 addition & 0 deletions blocks/Config/initialConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ export const initialConfig = {
enableVideoRecording: true,
maxVideoRecordingDuration: null,
mediaRecorerOptions: null,
aspectRatio: null,
};
1 change: 1 addition & 0 deletions blocks/Config/normalizeConfigValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ const mapping = {
mediaRecorerOptions: asObject,

maxVideoRecordingDuration: asNumber,
aspectRatio: asObject,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance validation for aspectRatio values

The current validation only ensures the value is an object. Consider adding specific validation for aspect ratio format to prevent runtime issues with camera initialization.

Example implementation:

-  aspectRatio: asObject,
+  aspectRatio: (value) => {
+    const obj = asObject(value);
+    if (obj === null) return null;
+    
+    for (const [key, ratio] of Object.entries(obj)) {
+      if (!/^\d+:\d+$/.test(key) || !/^\d+(\.\d+)?$/.test(ratio)) {
+        throw new Error(
+          `Invalid aspect ratio format. Expected format: { "width:height": "decimal" }, got: {"${key}": "${ratio}"}`
+        );
+      }
+    }
+    return obj;
+  },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
aspectRatio: asObject,
aspectRatio: (value) => {
const obj = asObject(value);
if (obj === null) return null;
for (const [key, ratio] of Object.entries(obj)) {
if (!/^\d+:\d+$/.test(key) || !/^\d+(\.\d+)?$/.test(ratio)) {
throw new Error(
`Invalid aspect ratio format. Expected format: { "width:height": "decimal" }, got: {"${key}": "${ratio}"}`
);
}
}
return obj;
},

};

/**
Expand Down
5 changes: 5 additions & 0 deletions types/exported.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ export type ConfigType = {
secureDeliveryProxyUrlResolver: SecureDeliveryProxyUrlResolver | null;
iconHrefResolver: IconHrefResolver | null;

/**
* The aspect ratio of the image/video to be cropped
*/
aspectRatio: Record<string, string> | null;

fileValidators: FileValidators;
collectionValidators: CollectionValidators;

Expand Down
Loading