about summary refs log tree commit diff
path: root/src/lib/media/util.ts
diff options
context:
space:
mode:
authorOllie Hsieh <renahlee@outlook.com>2023-04-17 15:41:44 -0700
committerGitHub <noreply@github.com>2023-04-17 15:41:44 -0700
commit2509290fdd2b20c76c302d4962216f5d2d2b5a73 (patch)
tree455bdd7420556e80242ad245ba8d9907ec6c84ee /src/lib/media/util.ts
parent91fadadb5848404bc47b69879bbc38a9011a0c62 (diff)
downloadvoidsky-2509290fdd2b20c76c302d4962216f5d2d2b5a73.tar.zst
Split image cropping into secondary step (#473)
* Split image cropping into secondary step

* Use ImageModel and GalleryModel

* Add fix for pasting image URLs

* Move models to state folder

* Fix things that broke after rebase

* Latest -- has image display bug

* Remove contentFit

* Fix iOS display in gallery

* Tuneup the api signatures and implement compress/resize on web

* Fix await

* Lint fix and remove unused function

* Fix android image pathing

* Fix external embed x button on android

* Remove min-height from composer (no longer useful and was mispositioning the composer on android)

* Fix e2e picker

---------

Co-authored-by: Paul Frazee <pfrazee@gmail.com>
Diffstat (limited to 'src/lib/media/util.ts')
-rw-r--r--src/lib/media/util.ts40
1 files changed, 39 insertions, 1 deletions
diff --git a/src/lib/media/util.ts b/src/lib/media/util.ts
index a27c71d82..75915de6b 100644
--- a/src/lib/media/util.ts
+++ b/src/lib/media/util.ts
@@ -1,7 +1,45 @@
+import {Dimensions} from './types'
+
 export function extractDataUriMime(uri: string): string {
   return uri.substring(uri.indexOf(':') + 1, uri.indexOf(';'))
 }
 
+// Fairly accurate estimate that is more performant
+// than decoding and checking length of URI
 export function getDataUriSize(uri: string): number {
-  return Math.round((uri.length * 3) / 4) // very rough estimate
+  return Math.round((uri.length * 3) / 4)
+}
+
+export function scaleDownDimensions(
+  dim: Dimensions,
+  max: Dimensions,
+): Dimensions {
+  if (dim.width < max.width && dim.height < max.height) {
+    return dim
+  }
+  const wScale = dim.width > max.width ? max.width / dim.width : 1
+  const hScale = dim.height > max.height ? max.height / dim.height : 1
+  if (wScale < hScale) {
+    return {width: dim.width * wScale, height: dim.height * wScale}
+  }
+  return {width: dim.width * hScale, height: dim.height * hScale}
+}
+
+export function isUriImage(uri: string) {
+  return /\.(jpg|jpeg|png).*$/.test(uri)
+}
+
+export function blobToDataUri(blob: Blob): Promise<string> {
+  return new Promise((resolve, reject) => {
+    const reader = new FileReader()
+    reader.onloadend = () => {
+      if (typeof reader.result === 'string') {
+        resolve(reader.result)
+      } else {
+        reject(new Error('Failed to read blob'))
+      }
+    }
+    reader.onerror = reject
+    reader.readAsDataURL(blob)
+  })
 }