@gradio/imageeditor 0.15.5 → 0.16.1

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.
@@ -255,7 +255,7 @@ export class ImageEditor {
255
255
  this.init();
256
256
  }
257
257
 
258
- private get context(): ImageEditorContext {
258
+ get context(): ImageEditorContext {
259
259
  const editor = this;
260
260
  return {
261
261
  app: this.app,
@@ -290,6 +290,10 @@ export class ImageEditor {
290
290
  }).observe(this.target_element);
291
291
 
292
292
  this.app = new Application();
293
+
294
+ if (!this.dark) {
295
+ (globalThis as any).__PIXI_APP__ = this.app;
296
+ }
293
297
  await this.app.init({
294
298
  width: container_box.width,
295
299
  height: container_box.height,
@@ -304,7 +308,7 @@ export class ImageEditor {
304
308
  const canvas = this.app.canvas as HTMLCanvasElement;
305
309
  canvas.style.background = "transparent";
306
310
 
307
- await this.setup_containers();
311
+ this.setup_containers();
308
312
 
309
313
  this.layer_manager.create_background_layer(this.width, this.height);
310
314
 
@@ -439,7 +443,7 @@ export class ImageEditor {
439
443
  this.ready_resolve();
440
444
  }
441
445
 
442
- private async setup_containers(): Promise<void> {
446
+ private setup_containers(): void {
443
447
  this.image_container = new Container({
444
448
  eventMode: "static",
445
449
  sortableChildren: true
@@ -586,7 +590,7 @@ export class ImageEditor {
586
590
  }
587
591
 
588
592
  async execute_command(command: Command): Promise<void> {
589
- await this.command_manager.execute(command);
593
+ await this.command_manager.execute(command, this.context);
590
594
  }
591
595
 
592
596
  undo(): void {
@@ -595,7 +599,7 @@ export class ImageEditor {
595
599
  }
596
600
 
597
601
  redo(): void {
598
- this.command_manager.redo();
602
+ this.command_manager.redo(this.context);
599
603
  this.notify("change");
600
604
  }
601
605
 
@@ -619,7 +623,6 @@ export class ImageEditor {
619
623
  * @param url The URL of the image to add
620
624
  */
621
625
  async add_image_from_url(url: string): Promise<void> {
622
- this.command_manager.reset();
623
626
  const image_tool = this.tools.get("image") as ImageTool;
624
627
  const texture = await Assets.load(url);
625
628
  await image_tool.add_image({
@@ -658,6 +661,7 @@ export class ImageEditor {
658
661
  this.layer_manager.reset_layers(this.width, this.height);
659
662
  this.background_image = undefined;
660
663
  this.background_image_present.set(false);
664
+ this.command_manager.reset();
661
665
 
662
666
  await this.set_image_properties({
663
667
  width: this.width,
@@ -667,8 +671,6 @@ export class ImageEditor {
667
671
  animate: false
668
672
  });
669
673
 
670
- this.command_manager = new CommandManager();
671
-
672
674
  this.layer_manager.create_background_layer(this.width, this.height);
673
675
 
674
676
  for (const tool of this.tools.values()) {
@@ -694,7 +696,7 @@ export class ImageEditor {
694
696
  make_active: true
695
697
  });
696
698
 
697
- this.command_manager.execute(add_layer_command);
699
+ this.execute_command(add_layer_command);
698
700
  this.notify("change");
699
701
  }
700
702
 
@@ -707,6 +709,7 @@ export class ImageEditor {
707
709
  this.command_manager.reset();
708
710
  const _layers = this.layer_manager.get_layers();
709
711
  _layers.forEach((l) => this.layer_manager.delete_layer(l.id));
712
+
710
713
  if (layer_urls === undefined || layer_urls.length === 0) {
711
714
  this.layer_manager.create_layer({
712
715
  width: this.width,
@@ -717,11 +720,17 @@ export class ImageEditor {
717
720
  return;
718
721
  }
719
722
 
723
+ const created_layer_ids: string[] = [];
720
724
  for await (const url of layer_urls) {
721
- await this.layer_manager.add_layer_from_url(url);
725
+ const layer_id = await this.layer_manager.add_layer_from_url(url);
726
+ if (layer_id) {
727
+ created_layer_ids.push(layer_id);
728
+ }
722
729
  }
723
730
 
724
- this.layer_manager.set_active_layer(_layers[0].id);
731
+ if (created_layer_ids.length > 0) {
732
+ this.layer_manager.set_active_layer(created_layer_ids[0]);
733
+ }
725
734
 
726
735
  const resize_tool = this.tools.get("resize") as any;
727
736
  if (resize_tool && typeof resize_tool.set_border_region === "function") {
@@ -743,13 +752,13 @@ export class ImageEditor {
743
752
  id,
744
753
  direction
745
754
  );
746
- this.command_manager.execute(reorder_layer_command);
755
+ this.execute_command(reorder_layer_command);
747
756
  this.notify("change");
748
757
  }
749
758
 
750
759
  delete_layer(id: string): void {
751
760
  const remove_layer_command = new RemoveLayerCommand(this.context, id);
752
- this.command_manager.execute(remove_layer_command);
761
+ this.execute_command(remove_layer_command);
753
762
  this.notify("change");
754
763
  }
755
764
 
@@ -746,7 +746,16 @@ export class LayerManager {
746
746
  return null;
747
747
  })
748
748
  ),
749
- composite: await get_canvas_blob(this.app.renderer, this.image_container)
749
+ composite: await get_canvas_blob(
750
+ this.app.renderer,
751
+ this.image_container,
752
+ {
753
+ width,
754
+ height,
755
+ x: 0,
756
+ y: 0
757
+ }
758
+ )
750
759
  };
751
760
 
752
761
  return blobs;
@@ -794,13 +803,16 @@ export class LayerManager {
794
803
  for (const layer of this.layers) {
795
804
  this.delete_layer(layer.id);
796
805
  }
806
+ let i = 0;
797
807
  for (const layer of this.layer_options.layers) {
798
808
  this.create_layer({
799
809
  width,
800
810
  height,
801
811
  layer_name: layer,
802
- user_created: false
812
+ user_created: false,
813
+ layer_id: `layer-${i}`
803
814
  });
815
+ i++;
804
816
  }
805
817
 
806
818
  this.active_layer = this.layers[0].container;
@@ -852,7 +864,11 @@ export class AddLayerCommand implements Command {
852
864
  this.previous_active_layer = current_active?.id || null;
853
865
  }
854
866
 
855
- async execute(): Promise<void> {
867
+ async execute(context?: ImageEditorContext): Promise<void> {
868
+ if (context) {
869
+ this.context = context;
870
+ }
871
+
856
872
  this.context.layer_manager.create_layer({
857
873
  width: this.width,
858
874
  height: this.height,
@@ -949,7 +965,11 @@ export class RemoveLayerCommand implements Command {
949
965
  }
950
966
  }
951
967
 
952
- async execute(): Promise<void> {
968
+ async execute(context?: ImageEditorContext): Promise<void> {
969
+ if (context) {
970
+ this.context = context;
971
+ }
972
+
953
973
  this.context.layer_manager.delete_layer(this.layer_data.id);
954
974
  }
955
975
 
@@ -1043,7 +1063,11 @@ export class ReorderLayerCommand implements Command {
1043
1063
  }
1044
1064
  }
1045
1065
 
1046
- async execute(): Promise<void> {
1066
+ async execute(context?: ImageEditorContext): Promise<void> {
1067
+ if (context) {
1068
+ this.context = context;
1069
+ }
1070
+
1047
1071
  this.context.layer_manager.move_layer(this.layer_id, this.direction);
1048
1072
  }
1049
1073
 
@@ -204,7 +204,11 @@ export class AddImageCommand implements BgImageCommand {
204
204
  return [this.current_canvas_size.width, this.current_canvas_size.height];
205
205
  }
206
206
 
207
- async execute(): Promise<void> {
207
+ async execute(context?: ImageEditorContext): Promise<void> {
208
+ if (context) {
209
+ this.context = context;
210
+ }
211
+
208
212
  await this.start();
209
213
 
210
214
  if (this.sprite === null) {
@@ -45,7 +45,11 @@ export class ResizeCommand implements Command {
45
45
  this.reset_ui = reset_ui;
46
46
  }
47
47
 
48
- async execute(): Promise<void> {
48
+ async execute(context?: ImageEditorContext): Promise<void> {
49
+ if (context) {
50
+ this.context = context;
51
+ }
52
+
49
53
  if (!this.context.background_image) return;
50
54
 
51
55
  this.context.background_image.width = this.new_width;