canvas-editor-engine 2.2.1 → 2.2.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/config.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { ICanvasSize } from "./types/canvas";
2
- export interface ILayer {
2
+ export interface IConfigLayer {
3
3
  name: string;
4
4
  index: number;
5
5
  }
6
6
  export declare class ConfigFabric {
7
7
  protected _CANVAS_SIZE: ICanvasSize;
8
- protected _LAYERS: ILayer[];
8
+ protected _LAYERS: IConfigLayer[];
9
9
  protected _ZOOM: number;
10
10
  }
11
11
  export default class AppConfig extends ConfigFabric {
@@ -14,8 +14,8 @@ export default class AppConfig extends ConfigFabric {
14
14
  bindCanvas(canvasElement: HTMLDivElement): void;
15
15
  get CANVAS_SIZE(): ICanvasSize;
16
16
  set CANVAS_SIZE(value: ICanvasSize | undefined);
17
- get LAYERS(): ILayer[];
18
- set LAYERS(value: ILayer[]);
17
+ get LAYERS(): IConfigLayer[];
18
+ set LAYERS(value: IConfigLayer[]);
19
19
  get ZOOM(): number;
20
20
  set ZOOM(value: number);
21
21
  }
package/dist/index.mjs CHANGED
@@ -1358,6 +1358,90 @@ var CropService = function() {
1358
1358
  return CropService;
1359
1359
  }();
1360
1360
 
1361
+ var DrawLayersState = function() {
1362
+ function DrawLayersState(appStoreRepository) {
1363
+ this.appStoreRepository = appStoreRepository;
1364
+ this.default = {
1365
+ layers: []
1366
+ };
1367
+ this._layers = this.default.layers;
1368
+ this.reset();
1369
+ }
1370
+ Object.defineProperty(DrawLayersState.prototype, "layers", {
1371
+ get: function() {
1372
+ return this._layers;
1373
+ },
1374
+ enumerable: false,
1375
+ configurable: true
1376
+ });
1377
+ DrawLayersState.prototype.reduce = function(name, payload) {
1378
+ var reducer = new Reducer$1(this);
1379
+ switch (name) {
1380
+ case "SET_LAYERS":
1381
+ reducer.setLayers(payload);
1382
+ break;
1383
+
1384
+ case "UPDATE_LAYER":
1385
+ reducer.updateLayer(payload);
1386
+ break;
1387
+
1388
+ case "ADD_LAYER":
1389
+ reducer.addLayer(payload);
1390
+ break;
1391
+ }
1392
+ };
1393
+ DrawLayersState.prototype.emerge = function(completeIt) {
1394
+ this._emergeCompleteIt = completeIt;
1395
+ };
1396
+ DrawLayersState.prototype.reset = function() {
1397
+ this.reduce("SET_LAYERS", this.default);
1398
+ };
1399
+ return DrawLayersState;
1400
+ }();
1401
+
1402
+ var Reducer$1 = function() {
1403
+ function Reducer(state) {
1404
+ this.state = state;
1405
+ }
1406
+ Reducer.prototype.setLayers = function(payload) {
1407
+ var isUpdate = false;
1408
+ if (!!(payload === null || payload === void 0 ? void 0 : payload.layers)) {
1409
+ this.state._layers = payload.layers;
1410
+ isUpdate = true;
1411
+ }
1412
+ if (isUpdate && !!this.state._emergeCompleteIt) {
1413
+ this.state._emergeCompleteIt(this.state._layers);
1414
+ }
1415
+ };
1416
+ Reducer.prototype.updateLayer = function(payload) {
1417
+ var isUpdate = false;
1418
+ if (!!payload) {
1419
+ this.state._layers.push(payload);
1420
+ isUpdate = true;
1421
+ }
1422
+ if (isUpdate && !!this.state._emergeCompleteIt) {
1423
+ this.state._emergeCompleteIt(this.state._layers);
1424
+ }
1425
+ };
1426
+ Reducer.prototype.addLayer = function(payload) {
1427
+ var isUpdate = false;
1428
+ if (!!payload) {
1429
+ this.state._layers.unshift(payload);
1430
+ isUpdate = true;
1431
+ }
1432
+ if (isUpdate && !!this.state._emergeCompleteIt) {
1433
+ this.state._emergeCompleteIt(this.state._layers);
1434
+ }
1435
+ };
1436
+ Reducer.prototype.popLayer = function() {
1437
+ this.state._layers.pop();
1438
+ if (!!this.state._emergeCompleteIt) {
1439
+ this.state._emergeCompleteIt(this.state._layers);
1440
+ }
1441
+ };
1442
+ return Reducer;
1443
+ }();
1444
+
1361
1445
  var HistoryState = function() {
1362
1446
  function HistoryState(throughHistoryService) {
1363
1447
  this.throughHistoryService = throughHistoryService;
@@ -1520,13 +1604,15 @@ var ImageState = function() {
1520
1604
  }();
1521
1605
 
1522
1606
  var Store = function() {
1523
- function Store(imageState, historyState) {
1607
+ function Store(imageState, historyState, drawLayersState) {
1524
1608
  this.imageState = imageState;
1525
1609
  this.historyState = historyState;
1610
+ this.drawLayersState = drawLayersState;
1526
1611
  }
1527
1612
  Store.prototype.reset = function() {
1528
1613
  this.imageState.reset();
1529
1614
  this.historyState.reset();
1615
+ this.drawLayersState.reset();
1530
1616
  };
1531
1617
  return Store;
1532
1618
  }();
@@ -1535,16 +1621,21 @@ var AppStore = function() {
1535
1621
  function AppStore(throughHistoryService, appStoreRepository) {
1536
1622
  this.throughHistoryService = throughHistoryService;
1537
1623
  this.appStoreRepository = appStoreRepository;
1538
- this.appStoreRepository.store = new Store(new ImageState(this.appStoreRepository), new HistoryState(this.throughHistoryService));
1624
+ this.appStoreRepository.store = new Store(new ImageState(this.appStoreRepository), new HistoryState(this.throughHistoryService), new DrawLayersState(this.appStoreRepository));
1539
1625
  }
1540
1626
  AppStore.prototype.subscribe = function(to, completeIt) {
1541
1627
  if (to === "history") {
1542
1628
  this.appStoreRepository.store.historyState.emerge(completeIt);
1629
+ } else if (to === "layers") {
1630
+ this.appStoreRepository.store.drawLayersState.emerge(completeIt);
1543
1631
  }
1544
1632
  };
1545
1633
  AppStore.prototype.getHistoryLines = function() {
1546
1634
  return this.appStoreRepository.store.historyState.historyLines;
1547
1635
  };
1636
+ AppStore.prototype.getLayers = function() {
1637
+ return this.appStoreRepository.store.drawLayersState.layers;
1638
+ };
1548
1639
  return AppStore;
1549
1640
  }();
1550
1641
 
@@ -2025,10 +2116,11 @@ var VagueFilter = function(_super) {
2025
2116
  }(Filter);
2026
2117
 
2027
2118
  var DrawAccumulator = function() {
2028
- function DrawAccumulator(appConfig, appStoreRepository, eventService) {
2119
+ function DrawAccumulator(appConfig, appStoreRepository, eventService, drawLayersService) {
2029
2120
  this.appConfig = appConfig;
2030
2121
  this.appStoreRepository = appStoreRepository;
2031
2122
  this.eventService = eventService;
2123
+ this.drawLayersService = drawLayersService;
2032
2124
  this.painters = new Proxy([], {
2033
2125
  get: function(target, name) {
2034
2126
  return target[name];
@@ -2044,13 +2136,11 @@ var DrawAccumulator = function() {
2044
2136
  console.warn("Proxy set error: object denied");
2045
2137
  return false;
2046
2138
  } else {
2047
- var numSkipped = void 0;
2048
2139
  var MIN_LENGTH = -1;
2049
2140
  var MAX_LENGTH = __spreadArray([], Object.keys(target), true);
2050
2141
  if (value <= Math.max.apply(Math, __spreadArray([ MIN_LENGTH ], MAX_LENGTH, false)) + 1) {
2051
2142
  target.length = value;
2052
2143
  }
2053
- numSkipped = 0;
2054
2144
  }
2055
2145
  return true;
2056
2146
  }
@@ -2061,7 +2151,7 @@ var DrawAccumulator = function() {
2061
2151
  }
2062
2152
  });
2063
2153
  }
2064
- DrawAccumulator.prototype.add = function(layer, drawType, options) {
2154
+ DrawAccumulator.prototype.add = function(layerId, drawType, options) {
2065
2155
  return __awaiter(this, void 0, void 0, (function() {
2066
2156
  var id, painter, inStore;
2067
2157
  return __generator(this, (function(_a) {
@@ -2071,11 +2161,12 @@ var DrawAccumulator = function() {
2071
2161
  drawType,
2072
2162
  id
2073
2163
  };
2074
- painter.drawService.bindOptions(drawType, layer, options);
2164
+ painter.drawService.bindOptions(drawType, options);
2075
2165
  this.painters.push({
2076
2166
  object: painter,
2077
2167
  update: this.update.bind(this)
2078
2168
  });
2169
+ this.drawLayersService.addToLayer(layerId, painter);
2079
2170
  inStore = true;
2080
2171
  this.invokePainter(drawType, painter.drawService, inStore);
2081
2172
  return [ 2 ];
@@ -2104,9 +2195,15 @@ var DrawAccumulator = function() {
2104
2195
  };
2105
2196
  Object.defineProperty(DrawAccumulator.prototype, "gradient", {
2106
2197
  get: function() {
2198
+ var _this = this;
2107
2199
  var gradient = [];
2108
2200
  this.painters.forEach((function(painter) {
2109
- gradient.push(painter.drawService.layer);
2201
+ var layer = _this.drawLayersService.layerList.find((function(layer) {
2202
+ return layer.painters.includes(painter);
2203
+ }));
2204
+ if (layer) {
2205
+ gradient.push(layer.order);
2206
+ }
2110
2207
  }));
2111
2208
  return gradient.sort((function(a, b) {
2112
2209
  return a - b;
@@ -2124,7 +2221,11 @@ var DrawAccumulator = function() {
2124
2221
  var stash = [];
2125
2222
  this.gradient.forEach((function(order) {
2126
2223
  _this.painters.forEach((function(painter) {
2127
- if (!stash.includes(painter) && painter.drawService.layer === order) {
2224
+ var layer = _this.drawLayersService.layerList.find((function(layer) {
2225
+ return layer.painters.includes(painter);
2226
+ }));
2227
+ if (!layer) return;
2228
+ if (!stash.includes(painter) && layer.order === order) {
2128
2229
  _this.invokePainter(painter.drawType, painter.drawService);
2129
2230
  stash.push(painter);
2130
2231
  }
@@ -2183,8 +2284,7 @@ var DrawService = function() {
2183
2284
  };
2184
2285
  this.id = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
2185
2286
  }
2186
- DrawService.prototype.bindOptions = function(drawType, layer, options) {
2187
- this.layer = layer;
2287
+ DrawService.prototype.bindOptions = function(drawType, options) {
2188
2288
  this.options[drawType] = options;
2189
2289
  };
2190
2290
  DrawService.prototype.drawImage = function() {
@@ -2416,6 +2516,70 @@ var ResizeService = function() {
2416
2516
  return ResizeService;
2417
2517
  }();
2418
2518
 
2519
+ var DrawLayersService = function() {
2520
+ function DrawLayersService(appStoreRepository) {
2521
+ this.appStoreRepository = appStoreRepository;
2522
+ this.layerList = [];
2523
+ this.addLayer({
2524
+ layerName: "Base Layer"
2525
+ });
2526
+ }
2527
+ DrawLayersService.prototype.addLayer = function(layerOptions) {
2528
+ var _a;
2529
+ var layerName = layerOptions.layerName, painter = layerOptions.painter;
2530
+ var id = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
2531
+ var name = function() {
2532
+ if (layerName) return layerName;
2533
+ if (painter) {
2534
+ return "".concat(painter.drawType, "-").concat(painter.id);
2535
+ } else {
2536
+ return "New Layer";
2537
+ }
2538
+ }();
2539
+ var painters = painter ? [ painter ] : [];
2540
+ var sortedLayers = (_a = this.layerList) === null || _a === void 0 ? void 0 : _a.sort((function(a, b) {
2541
+ return a.order - b.order;
2542
+ }));
2543
+ var order = (sortedLayers === null || sortedLayers === void 0 ? void 0 : sortedLayers.length) ? sortedLayers.at(-1).order + 1 : 1;
2544
+ var layer = {
2545
+ id,
2546
+ painters,
2547
+ order,
2548
+ name
2549
+ };
2550
+ this.layerList.push(layer);
2551
+ this.appStoreRepository.store.drawLayersState.reduce("ADD_LAYER", layer);
2552
+ };
2553
+ DrawLayersService.prototype.getLayerByOrder = function(order) {
2554
+ return this.layerList.find((function(layer) {
2555
+ return layer.order === order;
2556
+ }));
2557
+ };
2558
+ DrawLayersService.prototype.addToLayer = function(id, painter) {
2559
+ var layerIndex = this.layerList.findIndex((function(layer) {
2560
+ return layer.id === id;
2561
+ }));
2562
+ if (layerIndex === -1) return;
2563
+ var layer = this.layerList[layerIndex];
2564
+ layer.painters.push(painter);
2565
+ this.appStoreRepository.store.drawLayersState.reduce("UPDATE_LAYER", layer);
2566
+ };
2567
+ DrawLayersService.prototype.updateLayer = function(id, updateData) {
2568
+ var layerIndex = this.layerList.findIndex((function(layer) {
2569
+ return layer.id === id;
2570
+ }));
2571
+ var fields = Object.keys(updateData);
2572
+ if (!fields.length || layerIndex === -1) return;
2573
+ var layer = this.layerList[layerIndex];
2574
+ fields.forEach((function(field) {
2575
+ layer[field] = updateData[field];
2576
+ }));
2577
+ this.layerList[layerIndex] = layer;
2578
+ this.appStoreRepository.store.drawLayersState.reduce("UPDATE_LAYER", layer);
2579
+ };
2580
+ return DrawLayersService;
2581
+ }();
2582
+
2419
2583
  reflect();
2420
2584
 
2421
2585
  var WebComponentWrapper = function() {
@@ -2521,7 +2685,8 @@ var WebComponent = function(_super) {
2521
2685
  this.appStore = new AppStore(this.throughHistoryService, this.appStoreRepository);
2522
2686
  this.pullProjectService = new PullProjectService(this.throughHistoryService, this.appStoreRepository);
2523
2687
  this.drawService = new DrawService(this.appConfig, this.appStoreRepository, this.eventService);
2524
- this.drawAccumulator = new DrawAccumulator(this.appConfig, this.appStoreRepository, this.eventService);
2688
+ this.drawLayersService = new DrawLayersService(this.appStoreRepository);
2689
+ this.drawAccumulator = new DrawAccumulator(this.appConfig, this.appStoreRepository, this.eventService, this.drawLayersService);
2525
2690
  this.downloadService = new DownloadService(this.canvasComponent);
2526
2691
  var _a = this.canvasComponent.getComponent(), canvasTemplate = _a.canvasTemplate, canvasStyle = _a.canvasStyle;
2527
2692
  this.canvasElement = this.webComponentWrapper.editorWrap.add(canvasTemplate, canvasStyle);
@@ -2549,14 +2714,11 @@ var WebComponent = function(_super) {
2549
2714
  canvasSelector: this.canvasComponent.getCanvasSelector()
2550
2715
  };
2551
2716
  };
2552
- WebComponent.prototype.restoreJSONProjects = function(jsonProjects) {
2553
- var _this = this;
2717
+ WebComponent.prototype.restoreJSONProjects = function(layerId, jsonProjects) {
2554
2718
  var projectProcessor = this.projectsService.on("File");
2555
2719
  var serializer = projectProcessor.getSerializerInstance(jsonProjects);
2556
2720
  var projects = serializer.getProjects();
2557
- projects.forEach((function(project, index) {
2558
- _this.drawAccumulator.add(index, "project", project);
2559
- }));
2721
+ this.drawAccumulator.add(layerId, "project", projects[0]);
2560
2722
  this.throughHistoryService.recovery(projects[0]);
2561
2723
  };
2562
2724
  return WebComponent;
@@ -0,0 +1,15 @@
1
+ import AppStoreRepository from "../store/storeRepository";
2
+ import { ILayer, ILayerUpdate } from "../types/draw-layers";
3
+ import { IPainter } from "../types/draw-service";
4
+ export default class DrawLayersService {
5
+ private appStoreRepository;
6
+ layerList: ILayer[];
7
+ constructor(appStoreRepository: AppStoreRepository);
8
+ addLayer(layerOptions?: {
9
+ layerName?: string;
10
+ painter?: IPainter;
11
+ }): void;
12
+ getLayerByOrder(order: number): ILayer;
13
+ addToLayer(id: ILayer['id'], painter: IPainter): void;
14
+ updateLayer(id: ILayer['id'], updateData?: ILayerUpdate): void;
15
+ }
@@ -1,17 +1,20 @@
1
1
  import AppConfig from "../config";
2
2
  import { IImageStateReduce } from "../store/image.state";
3
3
  import AppStoreRepository from "../store/storeRepository";
4
+ import { ILayer } from "../types/draw-layers";
4
5
  import { IPainter, TDrawType } from "../types/draw-service";
5
6
  import type { IDrawImageArgs, IDrawImageProcessor, IFilterOptions, IImageLoggingDataVague, IImageOptions } from "../types/image";
6
7
  import { Project } from "../types/project";
8
+ import DrawLayersService from "./draw-leayers.service";
7
9
  import EventService from "./event.service";
8
10
  export declare class DrawAccumulator {
9
11
  private appConfig;
10
12
  private appStoreRepository;
11
13
  private eventService;
14
+ private drawLayersService;
12
15
  painters: IPainter[];
13
- constructor(appConfig: AppConfig, appStoreRepository: AppStoreRepository, eventService: EventService);
14
- add<DrawType extends TDrawType>(layer: number, drawType: DrawType, options: DrawService['options'][DrawType]): Promise<void>;
16
+ constructor(appConfig: AppConfig, appStoreRepository: AppStoreRepository, eventService: EventService, drawLayersService: DrawLayersService);
17
+ add<DrawType extends TDrawType>(layerId: ILayer['id'], drawType: DrawType, options: DrawService['options'][DrawType]): Promise<void>;
15
18
  remove(id: string): Promise<void>;
16
19
  private update;
17
20
  private get gradient();
@@ -24,7 +27,6 @@ export default class DrawService {
24
27
  private appStoreRepository;
25
28
  private eventService;
26
29
  id: string;
27
- layer: number;
28
30
  imageProcessor: IDrawImageProcessor;
29
31
  options: {
30
32
  image: {
@@ -34,7 +36,7 @@ export default class DrawService {
34
36
  project: Project | null;
35
37
  };
36
38
  constructor(appConfig: AppConfig, appStoreRepository: AppStoreRepository, eventService: EventService);
37
- bindOptions<DrawType extends TDrawType>(drawType: DrawType, layer: number, options: DrawService['options'][DrawType]): void;
39
+ bindOptions<DrawType extends TDrawType>(drawType: DrawType, options: DrawService['options'][DrawType]): void;
38
40
  drawImage(isReturnReduceData?: boolean): Promise<{
39
41
  reduceData: IImageStateReduce | null;
40
42
  message: string | null;
@@ -0,0 +1,18 @@
1
+ import { StateService } from "../services/store.service";
2
+ import { ILayer } from "../types/draw-layers";
3
+ import { TReducerNames } from "../types/draw-layers";
4
+ import AppStoreRepository from "./storeRepository";
5
+ export interface IDrawLayersState {
6
+ layers: ILayer[];
7
+ }
8
+ export declare class DrawLayersState implements StateService {
9
+ private appStoreRepository;
10
+ private default;
11
+ private _emergeCompleteIt;
12
+ private _layers;
13
+ get layers(): IDrawLayersState['layers'];
14
+ constructor(appStoreRepository: AppStoreRepository);
15
+ reduce(name: TReducerNames, payload?: ILayer | IDrawLayersState): void;
16
+ emerge(completeIt: (layers: IDrawLayersState['layers']) => void): void;
17
+ reset(): void;
18
+ }
@@ -1,18 +1,21 @@
1
1
  import { StoreService } from "../services/store.service";
2
2
  import ThroughHistoryService from "../services/through-history.service";
3
+ import { DrawLayersState } from "./draw-layers.state";
3
4
  import { HistoryState } from "./history.state";
4
5
  import { ImageState } from "./image.state";
5
6
  import AppStoreRepository from "./storeRepository";
6
7
  export declare class Store implements StoreService {
7
8
  imageState: ImageState;
8
9
  historyState: HistoryState;
9
- constructor(imageState: ImageState, historyState: HistoryState);
10
+ drawLayersState: DrawLayersState;
11
+ constructor(imageState: ImageState, historyState: HistoryState, drawLayersState: DrawLayersState);
10
12
  reset(): void;
11
13
  }
12
14
  export default class AppStore {
13
15
  private throughHistoryService;
14
16
  private appStoreRepository;
15
17
  constructor(throughHistoryService: ThroughHistoryService, appStoreRepository: AppStoreRepository);
16
- subscribe(to: 'history', completeIt: (...args: any) => void): void;
18
+ subscribe(to: 'history' | 'layers', completeIt: (...args: any) => void): void;
17
19
  getHistoryLines(): import("../types/history").IHistoryLine[];
20
+ getLayers(): import("../types/draw-layers").ILayer[];
18
21
  }
@@ -0,0 +1,13 @@
1
+ import { IPainter } from "./draw-service";
2
+ export interface ILayer {
3
+ id: string;
4
+ painters: IPainter[];
5
+ order: number;
6
+ name: string;
7
+ }
8
+ export interface ILayerUpdate {
9
+ painters?: ILayer['painters'];
10
+ order?: ILayer['order'];
11
+ name?: ILayer['name'];
12
+ }
13
+ export type TReducerNames = 'SET_LAYERS' | 'UPDATE_LAYER' | 'ADD_LAYER';
@@ -18,6 +18,8 @@ import PullProjectService from "./services/pull-project.service";
18
18
  import DrawService, { DrawAccumulator } from "./services/draw.service";
19
19
  import DownloadService from "./services/download.service";
20
20
  import ResizeService from "./services/serize.service";
21
+ import DrawLayersService from "./services/draw-leayers.service";
22
+ import { ILayer } from "./types/draw-layers";
21
23
  export declare class WebComponentWrapper {
22
24
  baseElement: HTMLDivElement;
23
25
  editorWrapElement: HTMLDivElement;
@@ -63,6 +65,7 @@ export default class WebComponent extends HTMLElement {
63
65
  downloadService: DownloadService;
64
66
  resizeService: ResizeService;
65
67
  drawAccumulator: DrawAccumulator;
68
+ drawLayersService: DrawLayersService;
66
69
  constructor();
67
70
  init(appConfig: AppConfig): {
68
71
  editorElement: HTMLDivElement;
@@ -72,5 +75,5 @@ export default class WebComponent extends HTMLElement {
72
75
  editorElement: HTMLDivElement;
73
76
  canvasSelector: string;
74
77
  };
75
- restoreJSONProjects(jsonProjects: string): void;
78
+ restoreJSONProjects(layerId: ILayer['id'], jsonProjects: string): void;
76
79
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canvas-editor-engine",
3
- "version": "2.2.1",
3
+ "version": "2.2.3",
4
4
  "description": "CanvasEditorEngine library, use: [typescript] [canvas]",
5
5
  "main": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",