@elia-ori/editor 0.1.2 → 0.1.3

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.
package/dist/index.cjs CHANGED
@@ -4193,6 +4193,50 @@ var Callout = Node3.create({
4193
4193
  // src/components/Toolbar.tsx
4194
4194
  var import_react2 = require("react");
4195
4195
 
4196
+ // src/lib/compress-image.ts
4197
+ var DEFAULT_MAX_SIZE_MB = 1;
4198
+ var DEFAULT_MAX_DIMENSION = 1920;
4199
+ var DEFAULT_QUALITY = 0.85;
4200
+ async function compressImage(file, options = {}) {
4201
+ const {
4202
+ maxSizeMB = DEFAULT_MAX_SIZE_MB,
4203
+ maxDimension = DEFAULT_MAX_DIMENSION,
4204
+ quality = DEFAULT_QUALITY
4205
+ } = options;
4206
+ if (file.size <= maxSizeMB * 1024 * 1024) {
4207
+ return file;
4208
+ }
4209
+ const img = new Image();
4210
+ const canvas = document.createElement("canvas");
4211
+ const ctx = canvas.getContext("2d");
4212
+ return new Promise((resolve, reject) => {
4213
+ img.onload = () => {
4214
+ let { width, height } = img;
4215
+ if (width > maxDimension || height > maxDimension) {
4216
+ const ratio = Math.min(maxDimension / width, maxDimension / height);
4217
+ width = Math.round(width * ratio);
4218
+ height = Math.round(height * ratio);
4219
+ }
4220
+ canvas.width = width;
4221
+ canvas.height = height;
4222
+ ctx.drawImage(img, 0, 0, width, height);
4223
+ canvas.toBlob(
4224
+ (blob) => {
4225
+ if (!blob) {
4226
+ reject(new Error("\u5716\u7247\u58D3\u7E2E\u5931\u6557"));
4227
+ return;
4228
+ }
4229
+ resolve(new File([blob], file.name, { type: "image/jpeg" }));
4230
+ },
4231
+ "image/jpeg",
4232
+ quality
4233
+ );
4234
+ };
4235
+ img.onerror = () => reject(new Error("\u5716\u7247\u8F09\u5165\u5931\u6557"));
4236
+ img.src = URL.createObjectURL(file);
4237
+ });
4238
+ }
4239
+
4196
4240
  // src/ui/button.tsx
4197
4241
  var React = __toESM(require("react"), 1);
4198
4242
 
@@ -4687,27 +4731,32 @@ function Toolbar({
4687
4731
  }
4688
4732
  editor.chain().focus().extendMarkRange("link").setLink({ href: url }).run();
4689
4733
  };
4690
- const handleImageUpload = (e) => {
4734
+ const handleImageUpload = async (e) => {
4691
4735
  const file = e.target.files?.[0];
4692
4736
  if (!file) return;
4693
- const MAX_SIZE = 5 * 1024 * 1024;
4694
- if (file.size > MAX_SIZE) {
4695
- alert("\u5716\u7247\u5927\u5C0F\u4E0D\u53EF\u8D85\u904E 5MB");
4696
- e.target.value = "";
4697
- return;
4698
- }
4699
- if (onImageUpload) {
4700
- onImageUpload(file);
4701
- } else {
4702
- const reader = new FileReader();
4703
- reader.onload = () => {
4704
- const base64 = reader.result;
4705
- editor.chain().focus().setImage({ src: base64 }).run();
4706
- };
4707
- reader.onerror = () => {
4708
- console.error("\u5716\u7247\u8B80\u53D6\u5931\u6557");
4709
- };
4710
- reader.readAsDataURL(file);
4737
+ try {
4738
+ const compressed = await compressImage(file);
4739
+ if (onImageUpload) {
4740
+ const result = onImageUpload(compressed);
4741
+ if (result instanceof Promise) {
4742
+ const url = await result;
4743
+ if (url) {
4744
+ editor.chain().focus().setImage({ src: url }).run();
4745
+ }
4746
+ }
4747
+ } else {
4748
+ const reader = new FileReader();
4749
+ reader.onload = () => {
4750
+ const base64 = reader.result;
4751
+ editor.chain().focus().setImage({ src: base64 }).run();
4752
+ };
4753
+ reader.onerror = () => {
4754
+ console.error("\u5716\u7247\u8B80\u53D6\u5931\u6557");
4755
+ };
4756
+ reader.readAsDataURL(compressed);
4757
+ }
4758
+ } catch (error) {
4759
+ console.error("\u5716\u7247\u8655\u7406\u5931\u6557:", error);
4711
4760
  }
4712
4761
  e.target.value = "";
4713
4762
  };