canvas-editor-engine 2.1.31 → 2.2.2

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,21 +1,21 @@
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 {
12
- private _canvas;
12
+ canvas: HTMLCanvasElement | null;
13
13
  constructor();
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
@@ -29,6 +29,120 @@ var __assign = function() {
29
29
  return __assign.apply(this, arguments);
30
30
  };
31
31
 
32
+ function __awaiter(thisArg, _arguments, P, generator) {
33
+ function adopt(value) {
34
+ return value instanceof P ? value : new P((function(resolve) {
35
+ resolve(value);
36
+ }));
37
+ }
38
+ return new (P || (P = Promise))((function(resolve, reject) {
39
+ function fulfilled(value) {
40
+ try {
41
+ step(generator.next(value));
42
+ } catch (e) {
43
+ reject(e);
44
+ }
45
+ }
46
+ function rejected(value) {
47
+ try {
48
+ step(generator["throw"](value));
49
+ } catch (e) {
50
+ reject(e);
51
+ }
52
+ }
53
+ function step(result) {
54
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
55
+ }
56
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
57
+ }));
58
+ }
59
+
60
+ function __generator(thisArg, body) {
61
+ var _ = {
62
+ label: 0,
63
+ sent: function() {
64
+ if (t[0] & 1) throw t[1];
65
+ return t[1];
66
+ },
67
+ trys: [],
68
+ ops: []
69
+ }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
70
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
71
+ return this;
72
+ }), g;
73
+ function verb(n) {
74
+ return function(v) {
75
+ return step([ n, v ]);
76
+ };
77
+ }
78
+ function step(op) {
79
+ if (f) throw new TypeError("Generator is already executing.");
80
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
81
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y),
82
+ 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
83
+ if (y = 0, t) op = [ op[0] & 2, t.value ];
84
+ switch (op[0]) {
85
+ case 0:
86
+ case 1:
87
+ t = op;
88
+ break;
89
+
90
+ case 4:
91
+ _.label++;
92
+ return {
93
+ value: op[1],
94
+ done: false
95
+ };
96
+
97
+ case 5:
98
+ _.label++;
99
+ y = op[1];
100
+ op = [ 0 ];
101
+ continue;
102
+
103
+ case 7:
104
+ op = _.ops.pop();
105
+ _.trys.pop();
106
+ continue;
107
+
108
+ default:
109
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
110
+ _ = 0;
111
+ continue;
112
+ }
113
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
114
+ _.label = op[1];
115
+ break;
116
+ }
117
+ if (op[0] === 6 && _.label < t[1]) {
118
+ _.label = t[1];
119
+ t = op;
120
+ break;
121
+ }
122
+ if (t && _.label < t[2]) {
123
+ _.label = t[2];
124
+ _.ops.push(op);
125
+ break;
126
+ }
127
+ if (t[2]) _.ops.pop();
128
+ _.trys.pop();
129
+ continue;
130
+ }
131
+ op = body.call(thisArg, _);
132
+ } catch (e) {
133
+ op = [ 6, e ];
134
+ y = 0;
135
+ } finally {
136
+ f = t = 0;
137
+ }
138
+ if (op[0] & 5) throw op[1];
139
+ return {
140
+ value: op[0] ? op[1] : void 0,
141
+ done: true
142
+ };
143
+ }
144
+ }
145
+
32
146
  function __spreadArray(to, from, pack) {
33
147
  if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
34
148
  if (ar || !(i in from)) {
@@ -69,7 +183,7 @@ var AppConfig = function(_super) {
69
183
  __extends(AppConfig, _super);
70
184
  function AppConfig() {
71
185
  var _this = _super.call(this) || this;
72
- _this._canvas = null;
186
+ _this.canvas = null;
73
187
  _this._CANVAS_SIZE = {
74
188
  width: 300,
75
189
  height: 150
@@ -88,8 +202,7 @@ var AppConfig = function(_super) {
88
202
  return _this;
89
203
  }
90
204
  AppConfig.prototype.bindCanvas = function(canvasElement) {
91
- var canvas = canvasElement.querySelector("canvas");
92
- this._canvas = canvas;
205
+ this.canvas = canvasElement.querySelector("canvas");
93
206
  };
94
207
  Object.defineProperty(AppConfig.prototype, "CANVAS_SIZE", {
95
208
  get: function() {
@@ -98,9 +211,9 @@ var AppConfig = function(_super) {
98
211
  set: function(value) {
99
212
  if (!!value && !!(value === null || value === void 0 ? void 0 : value.width) && !!(value === null || value === void 0 ? void 0 : value.height)) {
100
213
  this._CANVAS_SIZE = value;
101
- if (!!this._canvas) {
102
- this._canvas.width = value.width;
103
- this._canvas.height = value.height;
214
+ if (!!this.canvas) {
215
+ this.canvas.width = value.width;
216
+ this.canvas.height = value.height;
104
217
  }
105
218
  } else {
106
219
  console.warn("CANVAS_SIZE denied");
@@ -1245,6 +1358,90 @@ var CropService = function() {
1245
1358
  return CropService;
1246
1359
  }();
1247
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
+
1248
1445
  var HistoryState = function() {
1249
1446
  function HistoryState(throughHistoryService) {
1250
1447
  this.throughHistoryService = throughHistoryService;
@@ -1407,13 +1604,15 @@ var ImageState = function() {
1407
1604
  }();
1408
1605
 
1409
1606
  var Store = function() {
1410
- function Store(imageState, historyState) {
1607
+ function Store(imageState, historyState, drawLayersState) {
1411
1608
  this.imageState = imageState;
1412
1609
  this.historyState = historyState;
1610
+ this.drawLayersState = drawLayersState;
1413
1611
  }
1414
1612
  Store.prototype.reset = function() {
1415
1613
  this.imageState.reset();
1416
1614
  this.historyState.reset();
1615
+ this.drawLayersState.reset();
1417
1616
  };
1418
1617
  return Store;
1419
1618
  }();
@@ -1422,16 +1621,21 @@ var AppStore = function() {
1422
1621
  function AppStore(throughHistoryService, appStoreRepository) {
1423
1622
  this.throughHistoryService = throughHistoryService;
1424
1623
  this.appStoreRepository = appStoreRepository;
1425
- 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));
1426
1625
  }
1427
1626
  AppStore.prototype.subscribe = function(to, completeIt) {
1428
1627
  if (to === "history") {
1429
1628
  this.appStoreRepository.store.historyState.emerge(completeIt);
1629
+ } else if (to === "layers") {
1630
+ this.appStoreRepository.store.drawLayersState.emerge(completeIt);
1430
1631
  }
1431
1632
  };
1432
1633
  AppStore.prototype.getHistoryLines = function() {
1433
1634
  return this.appStoreRepository.store.historyState.historyLines;
1434
1635
  };
1636
+ AppStore.prototype.getLayers = function() {
1637
+ return this.appStoreRepository.store.drawLayersState.layers;
1638
+ };
1435
1639
  return AppStore;
1436
1640
  }();
1437
1641
 
@@ -1911,41 +2115,251 @@ var VagueFilter = function(_super) {
1911
2115
  return VagueFilter;
1912
2116
  }(Filter);
1913
2117
 
2118
+ var DrawAccumulator = function() {
2119
+ function DrawAccumulator(appConfig, appStoreRepository, eventService, drawLayersService) {
2120
+ this.appConfig = appConfig;
2121
+ this.appStoreRepository = appStoreRepository;
2122
+ this.eventService = eventService;
2123
+ this.drawLayersService = drawLayersService;
2124
+ this.painters = new Proxy([], {
2125
+ get: function(target, name) {
2126
+ return target[name];
2127
+ },
2128
+ set: function(target, name, value) {
2129
+ try {
2130
+ if (typeof (value === null || value === void 0 ? void 0 : value.object) !== "undefined") {
2131
+ target[name] = value.object;
2132
+ value.update();
2133
+ return true;
2134
+ } else {
2135
+ if (name != "length") {
2136
+ console.warn("Proxy set error: object denied");
2137
+ return false;
2138
+ } else {
2139
+ var MIN_LENGTH = -1;
2140
+ var MAX_LENGTH = __spreadArray([], Object.keys(target), true);
2141
+ if (value <= Math.max.apply(Math, __spreadArray([ MIN_LENGTH ], MAX_LENGTH, false)) + 1) {
2142
+ target.length = value;
2143
+ }
2144
+ }
2145
+ return true;
2146
+ }
2147
+ } catch (error) {
2148
+ console.error("Proxy set error:", error);
2149
+ return false;
2150
+ }
2151
+ }
2152
+ });
2153
+ }
2154
+ DrawAccumulator.prototype.add = function(layerId, drawType, options) {
2155
+ return __awaiter(this, void 0, void 0, (function() {
2156
+ var id, painter, inStore;
2157
+ return __generator(this, (function(_a) {
2158
+ id = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
2159
+ painter = {
2160
+ drawService: new DrawService(this.appConfig, this.appStoreRepository, this.eventService),
2161
+ drawType,
2162
+ id
2163
+ };
2164
+ painter.drawService.bindOptions(drawType, options);
2165
+ this.painters.push({
2166
+ object: painter,
2167
+ update: this.update.bind(this)
2168
+ });
2169
+ this.drawLayersService.addToLayer(layerId, painter);
2170
+ inStore = true;
2171
+ this.invokePainter(drawType, painter.drawService, inStore);
2172
+ return [ 2 ];
2173
+ }));
2174
+ }));
2175
+ };
2176
+ DrawAccumulator.prototype.remove = function(id) {
2177
+ return __awaiter(this, void 0, void 0, (function() {
2178
+ var painter;
2179
+ return __generator(this, (function(_a) {
2180
+ painter = this.painters.find((function(painter) {
2181
+ return painter.id === id;
2182
+ }));
2183
+ if (!!painter) {
2184
+ this.painters = this.painters.filter((function(painter) {
2185
+ return painter.id !== id;
2186
+ }));
2187
+ }
2188
+ return [ 2 ];
2189
+ }));
2190
+ }));
2191
+ };
2192
+ DrawAccumulator.prototype.update = function() {
2193
+ this.clearCanvas();
2194
+ this.invokePaintersOnLayers();
2195
+ };
2196
+ Object.defineProperty(DrawAccumulator.prototype, "gradient", {
2197
+ get: function() {
2198
+ var _this = this;
2199
+ var gradient = [];
2200
+ this.painters.forEach((function(painter) {
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
+ }
2207
+ }));
2208
+ return gradient.sort((function(a, b) {
2209
+ return a - b;
2210
+ }));
2211
+ },
2212
+ enumerable: false,
2213
+ configurable: true
2214
+ });
2215
+ DrawAccumulator.prototype.clearCanvas = function() {
2216
+ var ctx = this.appConfig.canvas.getContext("2d");
2217
+ ctx.clearRect(0, 0, this.appConfig.CANVAS_SIZE.width, this.appConfig.CANVAS_SIZE.height);
2218
+ };
2219
+ DrawAccumulator.prototype.invokePaintersOnLayers = function() {
2220
+ var _this = this;
2221
+ var stash = [];
2222
+ this.gradient.forEach((function(order) {
2223
+ _this.painters.forEach((function(painter) {
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) {
2229
+ _this.invokePainter(painter.drawType, painter.drawService);
2230
+ stash.push(painter);
2231
+ }
2232
+ }));
2233
+ }));
2234
+ };
2235
+ DrawAccumulator.prototype.invokePainter = function(drawType_1, painter_1) {
2236
+ return __awaiter(this, arguments, void 0, (function(drawType, painter, inStore) {
2237
+ var _a, reduceData, message, isAddToStore, _b, reduceData, message, isAddToStore;
2238
+ if (inStore === void 0) {
2239
+ inStore = false;
2240
+ }
2241
+ return __generator(this, (function(_c) {
2242
+ switch (_c.label) {
2243
+ case 0:
2244
+ if (!(drawType === "image")) return [ 3, 2 ];
2245
+ return [ 4, painter.drawImage(inStore) ];
2246
+
2247
+ case 1:
2248
+ _a = _c.sent(), reduceData = _a.reduceData, message = _a.message;
2249
+ isAddToStore = !!reduceData && !!message && inStore;
2250
+ if (isAddToStore) {
2251
+ this.appStoreRepository.store.imageState.reduce(reduceData, message);
2252
+ }
2253
+ return [ 3, 4 ];
2254
+
2255
+ case 2:
2256
+ if (!(drawType === "project")) return [ 3, 4 ];
2257
+ return [ 4, painter.drawProject(inStore) ];
2258
+
2259
+ case 3:
2260
+ _b = _c.sent(), reduceData = _b.reduceData, message = _b.message;
2261
+ isAddToStore = !!reduceData && !!message && inStore;
2262
+ if (isAddToStore) {
2263
+ this.appStoreRepository.store.imageState.reduce(reduceData, message);
2264
+ }
2265
+ _c.label = 4;
2266
+
2267
+ case 4:
2268
+ return [ 2 ];
2269
+ }
2270
+ }));
2271
+ }));
2272
+ };
2273
+ return DrawAccumulator;
2274
+ }();
2275
+
1914
2276
  var DrawService = function() {
1915
2277
  function DrawService(appConfig, appStoreRepository, eventService) {
1916
2278
  this.appConfig = appConfig;
1917
2279
  this.appStoreRepository = appStoreRepository;
1918
2280
  this.eventService = eventService;
2281
+ this.options = {
2282
+ image: null,
2283
+ project: null
2284
+ };
2285
+ this.id = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
1919
2286
  }
1920
- DrawService.prototype.drawImage = function(ctx, src, options) {
1921
- var _this = this;
1922
- this.imageProcessor = new SCImage(this.appConfig, src, ctx);
1923
- this.imageProcessor.draw(options).then((function() {
1924
- var filter = new Filter(_this.appConfig, ctx);
1925
- var zeroPosition = {
1926
- x: 0,
1927
- y: 0
1928
- };
1929
- var imageData = filter.copy(zeroPosition);
1930
- console.log(_this);
1931
- _this.appStoreRepository.store.imageState.reduce({
1932
- tempImageData: imageData,
1933
- position: zeroPosition,
1934
- size: _this.appConfig.CANVAS_SIZE
1935
- }, "Loaded image");
2287
+ DrawService.prototype.bindOptions = function(drawType, options) {
2288
+ this.options[drawType] = options;
2289
+ };
2290
+ DrawService.prototype.drawImage = function() {
2291
+ return __awaiter(this, arguments, void 0, (function(isReturnReduceData) {
2292
+ var _a, src, drawImageArgs, ctx;
2293
+ var _this = this;
2294
+ if (isReturnReduceData === void 0) {
2295
+ isReturnReduceData = false;
2296
+ }
2297
+ return __generator(this, (function(_b) {
2298
+ switch (_b.label) {
2299
+ case 0:
2300
+ _a = this.options.image, src = _a.src, drawImageArgs = _a.drawImageArgs;
2301
+ ctx = this.appConfig.canvas.getContext("2d");
2302
+ this.imageProcessor = new ImageObject(this.appConfig, src, ctx);
2303
+ return [ 4, this.imageProcessor.draw(drawImageArgs).then((function() {
2304
+ var responseData = {
2305
+ reduceData: null,
2306
+ message: null
2307
+ };
2308
+ if (!isReturnReduceData) return responseData;
2309
+ var zeroPosition = {
2310
+ x: 0,
2311
+ y: 0
2312
+ };
2313
+ var filter = new Filter(_this.appConfig, ctx);
2314
+ var imageData = filter.copy(zeroPosition);
2315
+ responseData.reduceData = {
2316
+ tempImageData: imageData,
2317
+ position: zeroPosition,
2318
+ size: _this.appConfig.CANVAS_SIZE
2319
+ };
2320
+ responseData.message = "Image loaded";
2321
+ return responseData;
2322
+ })) ];
2323
+
2324
+ case 1:
2325
+ return [ 2, _b.sent() ];
2326
+ }
2327
+ }));
1936
2328
  }));
1937
2329
  };
1938
- DrawService.prototype.drawProject = function(ctx, project) {
1939
- var imageData = project.state.current.imageData;
1940
- var position = project.state.current.position;
1941
- var size = project.state.current.size;
1942
- this.imageProcessor = new PCImage(this.appConfig, project, ctx);
1943
- this.imageProcessor.draw();
1944
- this.appStoreRepository.store.imageState.reduce({
1945
- tempImageData: imageData,
1946
- position,
1947
- size
1948
- }, "Loaded project");
2330
+ DrawService.prototype.drawProject = function() {
2331
+ return __awaiter(this, arguments, void 0, (function(isReturnReduceData) {
2332
+ var project, ctx, imageData, position, size;
2333
+ var _this = this;
2334
+ if (isReturnReduceData === void 0) {
2335
+ isReturnReduceData = false;
2336
+ }
2337
+ return __generator(this, (function(_a) {
2338
+ project = this.options.project;
2339
+ ctx = this.appConfig.canvas.getContext("2d");
2340
+ imageData = project.state.current.imageData;
2341
+ position = project.state.current.position;
2342
+ size = project.state.current.size;
2343
+ this.imageProcessor = new ProjectImageObject(this.appConfig, project, ctx);
2344
+ return [ 2, new Promise((function(resolve) {
2345
+ _this.imageProcessor.draw();
2346
+ resolve(null);
2347
+ })).then((function() {
2348
+ var responseData = {
2349
+ reduceData: null,
2350
+ message: null
2351
+ };
2352
+ if (!isReturnReduceData) return responseData;
2353
+ responseData.reduceData = {
2354
+ tempImageData: imageData,
2355
+ position,
2356
+ size
2357
+ };
2358
+ responseData.message = "Project loaded";
2359
+ return responseData;
2360
+ })) ];
2361
+ }));
2362
+ }));
1949
2363
  };
1950
2364
  DrawService.prototype.drawSmoothImage = function(useStore, options, filterOptions) {
1951
2365
  var _this = this;
@@ -1998,14 +2412,14 @@ var DrawService = function() {
1998
2412
  return DrawService;
1999
2413
  }();
2000
2414
 
2001
- var SCImage = function() {
2002
- function SCImage(appConfig, src, ctx) {
2415
+ var ImageObject = function() {
2416
+ function ImageObject(appConfig, src, ctx) {
2003
2417
  this.appConfig = appConfig;
2004
2418
  this.img = new Image;
2005
2419
  this.img.src = src;
2006
2420
  this.ctx = ctx;
2007
2421
  }
2008
- SCImage.prototype.draw = function(options) {
2422
+ ImageObject.prototype.draw = function(options) {
2009
2423
  var _this = this;
2010
2424
  var proto = this;
2011
2425
  var protoImg = this.img;
@@ -2031,20 +2445,20 @@ var SCImage = function() {
2031
2445
  }
2032
2446
  }));
2033
2447
  };
2034
- SCImage.prototype.vague = function(options, filterOptions) {
2448
+ ImageObject.prototype.vague = function(options, filterOptions) {
2035
2449
  var filter = new VagueFilter(this.appConfig, this.ctx, options);
2036
2450
  return filter.on("pixel", filterOptions);
2037
2451
  };
2038
- return SCImage;
2452
+ return ImageObject;
2039
2453
  }();
2040
2454
 
2041
- var PCImage = function() {
2042
- function PCImage(appConfig, project, ctx) {
2455
+ var ProjectImageObject = function() {
2456
+ function ProjectImageObject(appConfig, project, ctx) {
2043
2457
  this.appConfig = appConfig;
2044
2458
  this.project = project;
2045
2459
  this.ctx = ctx;
2046
2460
  }
2047
- PCImage.prototype.draw = function(options) {
2461
+ ProjectImageObject.prototype.draw = function(options) {
2048
2462
  var _this = this;
2049
2463
  var _a = this.project.state.current, imageData = _a.imageData, position = _a.position;
2050
2464
  return new Promise((function(resolve, reject) {
@@ -2058,11 +2472,11 @@ var PCImage = function() {
2058
2472
  }
2059
2473
  }));
2060
2474
  };
2061
- PCImage.prototype.vague = function(options, filterOptions) {
2475
+ ProjectImageObject.prototype.vague = function(options, filterOptions) {
2062
2476
  var filter = new VagueFilter(this.appConfig, this.ctx, options);
2063
2477
  return filter.on("pixel", filterOptions);
2064
2478
  };
2065
- return PCImage;
2479
+ return ProjectImageObject;
2066
2480
  }();
2067
2481
 
2068
2482
  var DownloadService = function() {
@@ -2102,6 +2516,63 @@ var ResizeService = function() {
2102
2516
  return ResizeService;
2103
2517
  }();
2104
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 = layerName || painter ? "".concat(painter.drawType, "-").concat(painter.id) : "New Layer";
2532
+ var painters = painter ? [ painter ] : [];
2533
+ var sortedLayers = (_a = this.layerList) === null || _a === void 0 ? void 0 : _a.sort((function(a, b) {
2534
+ return a.order - b.order;
2535
+ }));
2536
+ var order = (sortedLayers === null || sortedLayers === void 0 ? void 0 : sortedLayers.length) ? sortedLayers.at(-1).order + 1 : 1;
2537
+ var layer = {
2538
+ id,
2539
+ painters,
2540
+ order,
2541
+ name
2542
+ };
2543
+ this.layerList.push(layer);
2544
+ this.appStoreRepository.store.drawLayersState.reduce("ADD_LAYER", layer);
2545
+ };
2546
+ DrawLayersService.prototype.getLayerByOrder = function(order) {
2547
+ return this.layerList.find((function(layer) {
2548
+ return layer.order === order;
2549
+ }));
2550
+ };
2551
+ DrawLayersService.prototype.addToLayer = function(id, painter) {
2552
+ var layerIndex = this.layerList.findIndex((function(layer) {
2553
+ return layer.id === id;
2554
+ }));
2555
+ if (layerIndex === -1) return;
2556
+ var layer = this.layerList[layerIndex];
2557
+ layer.painters.push(painter);
2558
+ this.appStoreRepository.store.drawLayersState.reduce("UPDATE_LAYER", layer);
2559
+ };
2560
+ DrawLayersService.prototype.updateLayer = function(id, updateData) {
2561
+ var layerIndex = this.layerList.findIndex((function(layer) {
2562
+ return layer.id === id;
2563
+ }));
2564
+ var fields = Object.keys(updateData);
2565
+ if (!fields.length || layerIndex === -1) return;
2566
+ var layer = this.layerList[layerIndex];
2567
+ fields.forEach((function(field) {
2568
+ layer[field] = updateData[field];
2569
+ }));
2570
+ this.layerList[layerIndex] = layer;
2571
+ this.appStoreRepository.store.drawLayersState.reduce("UPDATE_LAYER", layer);
2572
+ };
2573
+ return DrawLayersService;
2574
+ }();
2575
+
2105
2576
  reflect();
2106
2577
 
2107
2578
  var WebComponentWrapper = function() {
@@ -2207,6 +2678,8 @@ var WebComponent = function(_super) {
2207
2678
  this.appStore = new AppStore(this.throughHistoryService, this.appStoreRepository);
2208
2679
  this.pullProjectService = new PullProjectService(this.throughHistoryService, this.appStoreRepository);
2209
2680
  this.drawService = new DrawService(this.appConfig, this.appStoreRepository, this.eventService);
2681
+ this.drawLayersService = new DrawLayersService(this.appStoreRepository);
2682
+ this.drawAccumulator = new DrawAccumulator(this.appConfig, this.appStoreRepository, this.eventService, this.drawLayersService);
2210
2683
  this.downloadService = new DownloadService(this.canvasComponent);
2211
2684
  var _a = this.canvasComponent.getComponent(), canvasTemplate = _a.canvasTemplate, canvasStyle = _a.canvasStyle;
2212
2685
  this.canvasElement = this.webComponentWrapper.editorWrap.add(canvasTemplate, canvasStyle);
@@ -2234,6 +2707,13 @@ var WebComponent = function(_super) {
2234
2707
  canvasSelector: this.canvasComponent.getCanvasSelector()
2235
2708
  };
2236
2709
  };
2710
+ WebComponent.prototype.restoreJSONProjects = function(layerId, jsonProjects) {
2711
+ var projectProcessor = this.projectsService.on("File");
2712
+ var serializer = projectProcessor.getSerializerInstance(jsonProjects);
2713
+ var projects = serializer.getProjects();
2714
+ this.drawAccumulator.add(layerId, "project", projects[0]);
2715
+ this.throughHistoryService.recovery(projects[0]);
2716
+ };
2237
2717
  return WebComponent;
2238
2718
  }(HTMLElement);
2239
2719
 
@@ -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,21 +1,55 @@
1
1
  import AppConfig from "../config";
2
+ import { IImageStateReduce } from "../store/image.state";
2
3
  import AppStoreRepository from "../store/storeRepository";
4
+ import { ILayer } from "../types/draw-layers";
5
+ import { IPainter, TDrawType } from "../types/draw-service";
3
6
  import type { IDrawImageArgs, IDrawImageProcessor, IFilterOptions, IImageLoggingDataVague, IImageOptions } from "../types/image";
4
7
  import { Project } from "../types/project";
8
+ import DrawLayersService from "./draw-leayers.service";
5
9
  import EventService from "./event.service";
10
+ export declare class DrawAccumulator {
11
+ private appConfig;
12
+ private appStoreRepository;
13
+ private eventService;
14
+ private drawLayersService;
15
+ painters: IPainter[];
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>;
18
+ remove(id: string): Promise<void>;
19
+ private update;
20
+ private get gradient();
21
+ private clearCanvas;
22
+ private invokePaintersOnLayers;
23
+ private invokePainter;
24
+ }
6
25
  export default class DrawService {
7
26
  private appConfig;
8
27
  private appStoreRepository;
9
28
  private eventService;
29
+ id: string;
10
30
  imageProcessor: IDrawImageProcessor;
31
+ options: {
32
+ image: {
33
+ src: string;
34
+ drawImageArgs: IDrawImageArgs;
35
+ } | null;
36
+ project: Project | null;
37
+ };
11
38
  constructor(appConfig: AppConfig, appStoreRepository: AppStoreRepository, eventService: EventService);
12
- drawImage(ctx: CanvasRenderingContext2D, src: string, options: IDrawImageArgs): void;
13
- drawProject(ctx: CanvasRenderingContext2D, project: Project): void;
39
+ bindOptions<DrawType extends TDrawType>(drawType: DrawType, options: DrawService['options'][DrawType]): void;
40
+ drawImage(isReturnReduceData?: boolean): Promise<{
41
+ reduceData: IImageStateReduce | null;
42
+ message: string | null;
43
+ }>;
44
+ drawProject(isReturnReduceData?: boolean): Promise<{
45
+ reduceData: IImageStateReduce | null;
46
+ message: string | null;
47
+ }>;
14
48
  drawSmoothImage(useStore: boolean, options: IDrawImageArgs, filterOptions: IFilterOptions): void;
15
49
  updateImageStateAfterVague(data: IImageLoggingDataVague): void;
16
50
  getFilterArgs(useStore: boolean, options: IDrawImageArgs): IImageOptions;
17
51
  }
18
- export declare class SCImage implements IDrawImageProcessor {
52
+ export declare class ImageObject implements IDrawImageProcessor {
19
53
  private appConfig;
20
54
  private img;
21
55
  private ctx;
@@ -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';
@@ -0,0 +1,7 @@
1
+ import DrawService from "../services/draw.service";
2
+ export type TDrawType = keyof DrawService['options'];
3
+ export interface IPainter {
4
+ drawService: DrawService;
5
+ drawType: TDrawType;
6
+ id: string;
7
+ }
@@ -15,9 +15,11 @@ import ThroughHistoryService from "./services/through-history.service";
15
15
  import AppStoreRepository from "./store/storeRepository";
16
16
  import ProjectsService from "./services/projects.service";
17
17
  import PullProjectService from "./services/pull-project.service";
18
- import DrawService from "./services/draw.service";
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;
@@ -62,6 +64,8 @@ export default class WebComponent extends HTMLElement {
62
64
  drawService: DrawService;
63
65
  downloadService: DownloadService;
64
66
  resizeService: ResizeService;
67
+ drawAccumulator: DrawAccumulator;
68
+ drawLayersService: DrawLayersService;
65
69
  constructor();
66
70
  init(appConfig: AppConfig): {
67
71
  editorElement: HTMLDivElement;
@@ -71,4 +75,5 @@ export default class WebComponent extends HTMLElement {
71
75
  editorElement: HTMLDivElement;
72
76
  canvasSelector: string;
73
77
  };
78
+ restoreJSONProjects(layerId: ILayer['id'], jsonProjects: string): void;
74
79
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canvas-editor-engine",
3
- "version": "2.1.31",
3
+ "version": "2.2.2",
4
4
  "description": "CanvasEditorEngine library, use: [typescript] [canvas]",
5
5
  "main": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",