xctc-utils 1.6.49 → 1.6.50

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.
@@ -12,33 +12,24 @@ var __assign = (this && this.__assign) || function () {
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  var fabric_1 = require("fabric");
15
+ var config_1 = require("../config");
16
+ var utils_1 = require("../utils");
17
+ var is_1 = require("../is");
18
+ var has_1 = require("../is/has");
19
+ var register_1 = require("../handle/register");
20
+ var drop_1 = require("../event/drop");
21
+ var mouse_down_1 = require("../event/mouse-down");
22
+ var mouse_move_1 = require("../event/mouse-move");
23
+ var mouse_up_1 = require("../event/mouse-up");
24
+ var mouse_wheel_1 = require("../event/mouse-wheel");
25
+ var index_1 = require("../handle/index");
26
+ var drop_2 = require("../event/drop");
27
+ var create_1 = require("../handle/create");
28
+ var query_1 = require("../handle/query");
29
+ var update_1 = require("../handle/update");
30
+ var changed_1 = require("../event/changed");
15
31
  // 初始化画布创建时需要的参数-默认值
16
- var defaultConfig = {
17
- width: 1000,
18
- height: 800,
19
- fireRightClick: true,
20
- stopContextMenu: true,
21
- backgroundImgUrl: "",
22
- left: 0,
23
- top: 0,
24
- // repeat: 'no-repeat',
25
- angle: 0,
26
- selection: true,
27
- selectable: true,
28
- strokeWidth: 4,
29
- scaleX: 1,
30
- scaleY: 1,
31
- stroke: "",
32
- fill: "",
33
- transparentCorners: false,
34
- cornerColor: "",
35
- hasControls: true,
36
- hasRotatingPoint: true,
37
- hasBorders: true,
38
- lockMovementX: true,
39
- lockMovementY: true,
40
- lockScalingFlip: true // 防止翻转
41
- };
32
+ var defaultConfig = __assign({}, config_1.configDefaultValue.canvasDefaultValue);
42
33
  var CanvasHooks = /** @class */ (function () {
43
34
  function CanvasHooks(config) {
44
35
  this.canvasDataSource = null;
@@ -88,23 +79,23 @@ var CanvasHooks = /** @class */ (function () {
88
79
  _.renderCanvasStatus = 1;
89
80
  var canvasId = "";
90
81
  // config 配置数据必须有画布ID参数
91
- if (config && Object.keys(config).length && !config.hasOwnProperty("id")) {
82
+ if ((0, is_1.isObject)(config) && !(0, has_1.hasProperty)(config, "id")) {
92
83
  console.error("createCanvasFn函数接收的参数必须包含画布对应的id");
93
84
  return;
94
85
  }
95
- if (config && config.hasOwnProperty("id")) {
86
+ if ((0, is_1.isObject)(config) && (0, has_1.hasProperty)(config, "id")) {
96
87
  _.canvasConfig = config;
97
- if (!_.getDomFn(_.canvasConfig.id)) {
88
+ if (!(0, utils_1.getDomFn)(_.canvasConfig.id)) {
98
89
  console.error("未查询到id=" + _.canvasConfig.id + "的canvas元素");
99
90
  return;
100
91
  }
101
92
  }
102
93
  // 初始化画布渲染时的配置参数
103
94
  var data = __assign(__assign({}, defaultConfig), _.canvasConfig);
104
- if (!_.canvasConfig.hasOwnProperty("width")) {
95
+ if (!(0, has_1.hasProperty)(_.canvasConfig, "width")) {
105
96
  data['width'] = _.elementSizeFn("canvasContainer").width;
106
97
  }
107
- if (!_.canvasConfig.hasOwnProperty("height")) {
98
+ if (!(0, has_1.hasProperty)(_.canvasConfig, "height")) {
108
99
  data['height'] = _.elementSizeFn("canvasContainer").height;
109
100
  }
110
101
  var imgUrl = (data === null || data === void 0 ? void 0 : data.backgroundImgUrl) || "";
@@ -196,13 +187,7 @@ var CanvasHooks = /** @class */ (function () {
196
187
  };
197
188
  // 注册回调事件--页面生成画布实例的时候,调用该函数
198
189
  CanvasHooks.prototype.registerCallBackEventFn = function (config) {
199
- console.log("canvasDataSource--", this.canvasDataSource);
200
- if (!this.canvasDataSource) {
201
- return;
202
- }
203
- if (config) {
204
- this.callBackEventConfig = config;
205
- }
190
+ new register_1.RegisterCanvasEventFn(config);
206
191
  };
207
192
  // 执行注册事件
208
193
  CanvasHooks.prototype.registerAllEventHandleFn = function () {
@@ -247,353 +232,55 @@ var CanvasHooks = /** @class */ (function () {
247
232
  };
248
233
  // 注册画布发生缩放监听事件
249
234
  CanvasHooks.prototype.registerCanvasZoomChangeEventFn = function () {
250
- var _ = this;
251
- var zoom = _.canvasDataSource.getZoom();
235
+ new register_1.RegisterCanvasZoomChangeEventFn();
252
236
  };
253
237
  // 拖拽事件注册
254
238
  CanvasHooks.prototype.registerMouseDropEventFn = function (options) {
255
- console.log("registerMouseDropEventFn");
256
- var _ = this;
257
- // 画布元素距离浏览器左侧和顶部的距离
258
- var offset = {
259
- left: _.canvasDataSource.getSelectionElement().getBoundingClientRect().left,
260
- top: _.canvasDataSource.getSelectionElement().getBoundingClientRect().top
261
- };
262
- // 鼠标坐标转换成画布的坐标(未经过缩放和平移的坐标)
263
- var point = {
264
- x: options.e.x - offset.left,
265
- y: options.e.y - offset.top,
266
- };
267
- // 转换后的坐标,restorePointerVpt 不受视窗变换的影响
268
- var pointerVpt = _.canvasDataSource.restorePointerVpt(point);
269
- //创建元素
270
- _.mousePointerDataSource = pointerVpt;
271
- console.log("_.mouseDragStatus=", _.mouseDragStatus);
272
- _.createDragElementFn();
273
- if (!_.mouseDragStatus) {
274
- _.mouseDragStatus = true;
275
- }
239
+ new drop_1.RegisterMouseDropEventFn(options);
276
240
  };
277
241
  // 鼠标进入画布
278
242
  CanvasHooks.prototype.registerMouseOverEventFn = function (options) {
279
- console.log("registerMouseOutEventFn");
243
+ // console.log("registerMouseOutEventFn");
280
244
  var _ = this;
281
245
  _.mouseCanvasInRangeStatus = true;
282
246
  };
283
247
  // 鼠标移出画布
284
248
  CanvasHooks.prototype.registerMouseOutEventFn = function (options) {
285
- console.log("registerMouseOutEventFn");
249
+ // console.log("registerMouseOutEventFn");
286
250
  var _ = this;
287
251
  _.mouseCanvasInRangeStatus = false;
288
252
  };
289
253
  // 注册鼠标按下事件
290
254
  CanvasHooks.prototype.registerMouseDownEventFn = function (options) {
291
- console.log("registerMouseDownEventFn");
292
- var _ = this;
293
- _.mouseCanvasInRangeStatus = true;
294
- _.mouseWheelPointerDataSource = options;
295
- var event = options.e;
296
- var activeTarget = options.target || options.activeTarget;
297
- console.log("activeTarget==", activeTarget);
298
- _.mouseDragStatus = false;
299
- _.mouseWheelStatus = false;
300
- _.mouseClickType = (options === null || options === void 0 ? void 0 : options.button) || 1; // 鼠标点击类型 1 左键 2中间滚轮 3右键
301
- console.log("mouseClickType==", _.mouseClickType);
302
- if (_.mouseRightClickStatus) {
303
- // 如果当前处于右键状态,则不再继续执行任何操作
304
- // 如果菜单属于显示状态,则关闭菜单
305
- if (_.canvasMenuShowStatus) {
306
- _.canvasMenuShowStatus = false;
307
- _.mouseRightClickStatus = false;
308
- _.hiddenRightClickMenuFn();
309
- }
310
- return;
311
- }
312
- // 鼠标左键点击操作
313
- if (_.mouseClickType == 1) {
314
- // 开启全选操作,禁止画布拖拽
315
- if (_.canvasAllSelectionStatus) {
316
- _.dragLastPosDataSource.open = false;
317
- _.hiddenRightClickMenuFn();
318
- return;
319
- }
320
- // 自定义绘制选择框选状态--开始绘制
321
- if (_.canvasSelectionStatus) {
322
- console.log("自定义绘制选择框选状态--开始绘制");
323
- _.createSelectionStatus = 1;
324
- var pointer = _.canvasDataSource.getPointer(options.e);
325
- _.mousePointerDataSource = { x: pointer.x, y: pointer.y };
326
- _.createSelectionRectFn(options);
327
- return;
328
- }
329
- // 如果是鼠标左键点击画布,且此时处于右键状态,则关闭右键菜单,并将菜单状态初始化
330
- if (_.mouseRightClickStatus) {
331
- console.log("关闭右键菜单,并将菜单状态初始化");
332
- _.mouseRightClickStatus = false;
333
- _.canvasMenuShowStatus = false;
334
- _.dragLastPosDataSource.open = true;
335
- _.dragLastPosDataSource.lastPosX = event.clientX;
336
- _.dragLastPosDataSource.lastPosY = event.clientY;
337
- _.canvasDragStatus = true;
338
- _.hiddenRightClickMenuFn();
339
- return;
340
- }
341
- // 点击画布上的对象执行操作
342
- if (activeTarget) {
343
- // 画布上的数据对象被点击
344
- if ((activeTarget === null || activeTarget === void 0 ? void 0 : activeTarget.elementKey) == "data") {
345
- _.mouseClickTargetObjectStatus = 1;
346
- return;
347
- }
348
- // 画布上的自定义绘制框被点击
349
- if ((activeTarget === null || activeTarget === void 0 ? void 0 : activeTarget.renderType) == "frameSelection") {
350
- _.mouseClickTargetObjectStatus = 1;
351
- return;
352
- }
353
- }
354
- else {
355
- if (!_.canvasDragStatus) {
356
- setTimeout(function () {
357
- _.canvasDragStatus = true;
358
- }, 0);
359
- }
360
- }
361
- // 画布可拖拽状态
362
- if (_.canvasDragStatus) {
363
- // 如果当前点击了底层画布,则允许底层画布可以拖拽
364
- console.log("允许底层画布可以拖拽");
365
- if (!activeTarget) {
366
- // 如果没有获取到具体的目标对象,则默认是点击了画布底图,即开启画布的拖拽
367
- _.dragLastPosDataSource.open = true;
368
- _.dragLastPosDataSource.lastPosX = event.clientX;
369
- _.dragLastPosDataSource.lastPosY = event.clientY;
370
- return;
371
- }
372
- }
373
- else {
374
- console.log("3333");
375
- _.dragLastPosDataSource.open = false;
376
- return;
377
- }
378
- }
379
- // 中间滚轮操作,仅仅允许缩放,禁止拖拽操作
380
- if (_.mouseClickType == 2) {
381
- _.dragLastPosDataSource.open = false;
382
- }
383
- // 鼠标右键操作
384
- if (_.mouseClickType == 3) {
385
- // 激活右键状态
386
- _.mouseRightClickStatus = true;
387
- _.canvasMenuShowStatus = true;
388
- // 创建右键选择菜单列表
389
- _.createRightClickMenuFn(options);
390
- }
255
+ new mouse_down_1.RegisterMouseDownEventFn(options);
391
256
  };
392
257
  // 注册鼠标移动事件
393
258
  CanvasHooks.prototype.registerMouseMoveEventFn = function (options) {
394
- var _ = this;
395
- // 监听鼠标是否在画布范围内移动
396
- var pointer = _.canvasDataSource.getPointer(options.e);
397
- var inRange = pointer.x >= 0 && pointer.x <= _.canvasDataSource.width &&
398
- pointer.y >= 0 && pointer.y <= _.canvasDataSource.height;
399
- _.mouseCanvasInRangeStatus = inRange;
400
- // 鼠标不在画布内,终止行为
401
- if (!inRange) {
402
- return;
403
- }
404
- var activeTarget = options.target || options.activeTarget;
405
- _.mouseDragStatus = true;
406
- _.mouseWheelStatus = true;
407
- _.mouseWheelPointerDataSource = options;
408
- if (_.mouseRightClickStatus) {
409
- // 如果当前处于右键状态,则不再继续执行任何操作
410
- return;
411
- }
412
- if (_.dragLastPosDataSource.open) {
413
- var evt = options.e;
414
- var vpt = _.canvasDataSource.viewportTransform;
415
- vpt[4] += evt.clientX - _.dragLastPosDataSource.lastPosX;
416
- vpt[5] += evt.clientY - _.dragLastPosDataSource.lastPosY;
417
- _.canvasDataSource.requestRenderAll(); // 异步更新画板,提升性能
418
- _.dragLastPosDataSource.lastPosX = evt.clientX;
419
- _.dragLastPosDataSource.lastPosY = evt.clientY;
420
- return;
421
- }
422
- // 画布上的对象被点击
423
- if ((activeTarget === null || activeTarget === void 0 ? void 0 : activeTarget.elementKey) == "data") {
424
- _.mouseClickTargetObjectStatus = 2;
425
- return;
426
- }
427
- // 画布上的自定义绘制框被点击
428
- if ((activeTarget === null || activeTarget === void 0 ? void 0 : activeTarget.renderType) == "frameSelection") {
429
- _.mouseClickTargetObjectStatus = 2;
430
- return;
431
- }
432
- // 更新自定义绘制图像数据
433
- if (_.canvasSelectionStatus) {
434
- _.createSelectionStatus = 2;
435
- _.updateSelectionRectFn(options);
436
- return;
437
- }
259
+ new mouse_move_1.RegisterMouseMoveEventFn(options);
438
260
  };
439
261
  // 注册鼠标抬起事件
440
262
  CanvasHooks.prototype.registerMouseUpEventFn = function (options) {
441
- console.log("registerMouseUpEventFn");
442
- var _ = this;
443
- // 鼠标不在画布内,终止行为
444
- if (!_.mouseCanvasInRangeStatus) {
445
- return;
446
- }
447
- var activeTarget = options.target || options.activeTarget;
448
- var _a = _.callBackEventConfig || {}, activeTargetElementBackFn = _a.activeTargetElementBackFn, activeTargetSelectionRectBackFn = _a.activeTargetSelectionRectBackFn;
449
- _.mouseWheelPointerDataSource = options;
450
- console.log("activeTarget==", activeTarget);
451
- console.log("_.canvasDataSource==", _.canvasDataSource.backgroundImage);
452
- setTimeout(function () {
453
- _.mouseWheelStatus = false;
454
- }, 0);
455
- if (_.mouseRightClickStatus) {
456
- // 如果当前处于右键状态,则不再继续执行任何操作
457
- return;
458
- }
459
- // 画布上的对象被点击
460
- if ((activeTarget === null || activeTarget === void 0 ? void 0 : activeTarget.elementKey) == "data") {
461
- // 如果是点击对象操作,并未执行拖动,则执行点击的回调函数
462
- if (_.mouseClickTargetObjectStatus == 1) {
463
- if (activeTargetElementBackFn instanceof Function) {
464
- activeTargetElementBackFn(activeTarget);
465
- }
466
- }
467
- setTimeout(function () {
468
- _.mouseClickTargetObjectStatus = 3;
469
- }, 0);
470
- return;
471
- }
472
- if ((activeTarget === null || activeTarget === void 0 ? void 0 : activeTarget.renderType) == "frameSelection") {
473
- // 点击了画布上自定义绘制的选择框
474
- if (_.mouseClickTargetObjectStatus == 1) {
475
- if (activeTargetSelectionRectBackFn instanceof Function) {
476
- activeTargetSelectionRectBackFn(activeTarget);
477
- }
478
- }
479
- setTimeout(function () {
480
- _.mouseClickTargetObjectStatus = 3;
481
- }, 0);
482
- return;
483
- }
484
- // 允许画布拖动操作
485
- if (_.dragLastPosDataSource.open) {
486
- _.canvasDataSource.setViewportTransform(_.canvasDataSource.viewportTransform);
487
- _.dragLastPosDataSource.open = false;
488
- return;
489
- }
490
- // 右键选择自定义绘制框,并且绘制完成,抬起鼠标,结束当次绘制
491
- if (_.canvasSelectionStatus && _.mouseClickType == 1) {
492
- _.createSelectionRectFinishFn(options);
493
- return;
494
- }
495
- // _.canvasDataSource.selection = true;
496
- setTimeout(function () {
497
- _.mouseDragStatus = false;
498
- }, 0);
263
+ new mouse_up_1.RegisterMouseUpEventFn(options);
499
264
  };
500
265
  // 注册鼠标滚动事件
501
266
  CanvasHooks.prototype.registerMouseWheelEventFn = function (options) {
502
- var _a;
503
- console.log("registerMouseWheelEventFn");
504
- var _ = this;
505
- _.mouseWheelPointerDataSource = options;
506
- if (!_.canvasDataSource) {
507
- return;
508
- }
509
- if ((_a = _.canvasConfig) === null || _a === void 0 ? void 0 : _a.zoom) {
510
- _.registerZoomHandleFn(options);
511
- }
267
+ new mouse_wheel_1.RegisterMouseWheelEventFn(options);
512
268
  };
513
269
  // 滚动事件操作--画布缩放监听
514
270
  CanvasHooks.prototype.registerZoomHandleFn = function (options) {
515
- var _a;
516
- console.log("registerZoomHandleFn");
517
- var _ = this;
518
- if (!_.canvasDataSource) {
519
- return;
520
- }
521
- // 滚轮,向上滚一下是 -100,向下滚一下是 100
522
- var delta = options.e.deltaY;
523
- var zoom = _.canvasDataSource.getZoom(); // 获取画布当前缩放值
524
- console.log("registerZoomHandleFn--zoom=", zoom);
525
- zoom += 0.001 * delta;
526
- if (zoom > 10)
527
- zoom = 10; // 限制最大缩放级别
528
- if (zoom < 0.2)
529
- zoom = 0.2; // 限制最小缩放级别
530
- _.zoomDataSource = (zoom * 100).toFixed(0);
531
- // 以鼠标所在位置为原点缩放
532
- _.canvasDataSource.zoomToPoint({
533
- x: options.e.offsetX,
534
- y: options.e.offsetY
535
- }, zoom // 传入修改后的缩放级别
536
- );
537
- var fn = ((_a = _.callBackEventConfig) === null || _a === void 0 ? void 0 : _a.zoomListenEvent) || "";
538
- if (fn && fn instanceof Function) {
539
- fn(zoom, _.zoomDataSource);
540
- }
271
+ new index_1.RegisterZoomHandleFn(options);
541
272
  };
542
273
  // 页面上发生拖拽时,拖拽完成后的数据存储函数
543
274
  CanvasHooks.prototype.dragCreateElementDataFn = function (data) {
544
- var _ = this;
545
- if (!_.canvasDataSource) {
546
- return;
547
- }
548
- _.dragElementDataSource = data;
549
- _.targetObjectDataSource = data;
275
+ new drop_2.DragCreateElementDataFn(data);
550
276
  };
551
277
  // 拖拽进入画布元素创建
552
278
  CanvasHooks.prototype.createDragElementFn = function () {
553
- var _a, _b, _c, _d, _e;
554
- var _ = this;
555
- if (!_.canvasDataSource) {
556
- return;
557
- }
558
- var type = ((_a = _.dragElementDataSource) === null || _a === void 0 ? void 0 : _a.renderType) || "image";
559
- var top = (_b = _.mousePointerDataSource) === null || _b === void 0 ? void 0 : _b.y;
560
- var left = ((_c = _.mousePointerDataSource) === null || _c === void 0 ? void 0 : _c.x) - (((_d = _.dragElementDataSource) === null || _d === void 0 ? void 0 : _d.width) / 2);
561
- // 同一个对象是否允许多次创建 createMode = multiple 允许 传其他或不传不允许
562
- _.targetObjectDataSource = _.dragElementDataSource;
563
- if (!((_e = _.targetObjectDataSource) === null || _e === void 0 ? void 0 : _e.id)) {
564
- console.error("拖入画布的元素没有唯一标识id字段,不能创建!");
565
- return;
566
- }
567
- var item = __assign(__assign({}, _.dragElementDataSource), { top: top, left: left });
568
- // 拖拽数据类型为SVG图片对象
569
- if (type == "svg") {
570
- _.createCanvasSvgElementFn(item);
571
- }
572
- else {
573
- _.createCanvasImageElementFn(item);
574
- }
279
+ new create_1.CreateDragElementFn();
575
280
  };
576
281
  // 默认加载一个列表数据到画布
577
282
  CanvasHooks.prototype.initElement2CanvasFn = function (list) {
578
- var _ = this;
579
- _.defaultCreateDataListStatus = false;
580
- if (!_.canvasDataSource) {
581
- return;
582
- }
583
- if (list === null || list === void 0 ? void 0 : list.length) {
584
- _.defaultCreateDataList = list;
585
- list.forEach(function (item, index) {
586
- if (item.hasOwnProperty("left") && item.hasOwnProperty("top")) {
587
- var type = (item === null || item === void 0 ? void 0 : item.renderType) || "image";
588
- if (type == "svg") {
589
- _.createCanvasSvgElementFn(item, index);
590
- }
591
- else {
592
- _.createCanvasImageElementFn(item, index);
593
- }
594
- }
595
- });
596
- }
283
+ new create_1.InitElement2CanvasFn(list);
597
284
  };
598
285
  CanvasHooks.prototype.getCreateElementStatusFn = function (index) {
599
286
  var _ = this;
@@ -608,479 +295,80 @@ var CanvasHooks = /** @class */ (function () {
608
295
  };
609
296
  // 创建SVG到画布
610
297
  CanvasHooks.prototype.createCanvasSvgElementFn = function (item, index) {
611
- var _ = this;
612
- if (!_.canvasDataSource) {
613
- return;
614
- }
615
- var createDragElementBackFn = (_.callBackEventConfig || {}).createDragElementBackFn;
616
- fabric_1.fabric.loadSVGFromURL(item === null || item === void 0 ? void 0 : item.address, function (objects) {
617
- var svgObject = (objects === null || objects === void 0 ? void 0 : objects.length) ? objects[0] : "";
618
- if (!svgObject) {
619
- return;
620
- }
621
- console.log("createCanvasSvgElementFn-item=", item);
622
- svgObject.set(__assign({ evented: true, originX: 'left', originY: 'top' }, item));
623
- _.canvasDataSource.add(svgObject);
624
- _.canvasDataSource.requestRenderAll();
625
- _.mousePointerDataSource = null;
626
- _.canvasCreateElementsDataSource.push(svgObject);
627
- _.canvasCreateTargetElementsDataSource.push(svgObject);
628
- _.getCreateElementStatusFn(index);
629
- if (createDragElementBackFn instanceof Function) {
630
- createDragElementBackFn(_.dragElementDataSource);
631
- }
632
- setTimeout(function () {
633
- _.mouseDragStatus = false;
634
- }, 0);
635
- });
298
+ new create_1.CreateCanvasSvgElementFn(fabric_1.fabric, item, index);
636
299
  };
637
300
  // 创建图片到画布
638
301
  CanvasHooks.prototype.createCanvasImageElementFn = function (item, index) {
639
- var _a;
640
- var _ = this;
641
- if (!_.canvasDataSource) {
642
- return;
643
- }
644
- var createDragElementBackFn = (_.callBackEventConfig || {}).createDragElementBackFn;
645
- fabric_1.fabric.Image.fromURL((_a = _.dragElementDataSource) === null || _a === void 0 ? void 0 : _a.address, function (img) {
646
- img.set(__assign({}, item));
647
- _.canvasDataSource.add(img);
648
- _.canvasCreateElementsDataSource.push(img);
649
- _.canvasCreateTargetElementsDataSource.push(img);
650
- _.mousePointerDataSource = null;
651
- _.canvasDataSource.requestRenderAll();
652
- _.getCreateElementStatusFn(index);
653
- setTimeout(function () {
654
- _.mouseDragStatus = false;
655
- }, 0);
656
- if (createDragElementBackFn instanceof Function) {
657
- createDragElementBackFn(_.dragElementDataSource);
658
- }
659
- }, function (exception) {
660
- // 图片加载失败后的操作
661
- console.error('createElement-exception-exception: ', exception);
662
- // 这里可以显示文案或者进行其他错误处理
663
- });
302
+ new create_1.CreateCanvasImageElementFn(fabric_1.fabric, item, index);
664
303
  };
665
304
  // 获取到所有被框选的对象
666
305
  CanvasHooks.prototype.getSelectionAllElementsFn = function () {
667
- var _ = this;
668
- if (!_.canvasDataSource) {
669
- return;
670
- }
671
- var dataList = [];
672
- var selection = _.canvasSelectionElementsDataSource || [];
673
- // 检查哪些图标在选择框内
674
- _.canvasDataSource.forEachObject(function (obj) {
675
- var isTarget = false;
676
- selection.map(function (item) {
677
- if (obj.intersectsWithObject(item)) {
678
- isTarget = true;
679
- }
680
- });
681
- if (isTarget && (obj === null || obj === void 0 ? void 0 : obj.elementKey) == "data") {
682
- dataList.push(obj);
683
- }
684
- });
685
- return dataList;
306
+ new query_1.QuerySelectionAllElementsFn();
686
307
  };
687
308
  // 获取到指定选择框内被框选的对象
688
309
  CanvasHooks.prototype.getSelectionElementsFn = function (item) {
689
- var _ = this;
690
- if (!_.canvasDataSource) {
691
- return;
692
- }
693
- var dataList = [];
694
- // 检查哪些图标在选择框内
695
- _.canvasDataSource.forEachObject(function (obj) {
696
- if (item && obj.intersectsWithObject(item) && (obj === null || obj === void 0 ? void 0 : obj.elementKey) == "data") {
697
- dataList.push(obj);
698
- }
699
- });
700
- return dataList;
310
+ new query_1.QuerySelectionElementsFn(item);
701
311
  };
702
312
  // 创建自定义图像
703
313
  CanvasHooks.prototype.createSelectionRectFn = function () {
704
- var _a, _b;
705
- var _ = this;
706
- if (!_.canvasDataSource) {
707
- return;
708
- }
709
- _.createSelectionRectDataSource = new fabric_1.fabric.Rect({
710
- left: (_a = _.mousePointerDataSource) === null || _a === void 0 ? void 0 : _a.x,
711
- top: (_b = _.mousePointerDataSource) === null || _b === void 0 ? void 0 : _b.y,
712
- width: 0,
713
- height: 0,
714
- fill: 'rgba(0,0,255,0.1)',
715
- stroke: 'blue',
716
- // fill:"transparent",
717
- strokeWidth: 1,
718
- hasControls: true,
719
- hasBorders: true,
720
- selectable: true,
721
- evented: true,
722
- hoverCursor: 'move',
723
- renderType: "frameSelection", // 代表框选
724
- });
725
- _.canvasDataSource.add(_.createSelectionRectDataSource);
726
- _.canvasDataSource.requestRenderAll();
314
+ new create_1.CreateSelectionRectFn(fabric_1.fabric);
727
315
  };
728
316
  // 创建全选框
729
317
  CanvasHooks.prototype.createAllSelectionRectFn = function () {
730
- var _ = this;
731
- if (!_.canvasDataSource) {
732
- return;
733
- }
734
- _.createSelectionRectDataSource = new fabric_1.fabric.Rect({
735
- left: 0,
736
- top: 0,
737
- width: _.canvasDataSource.getWidth(),
738
- height: _.canvasDataSource.getHeight(),
739
- fill: 'rgba(0,0,255,0.1)',
740
- stroke: 'blue',
741
- // fill:"transparent",
742
- strokeWidth: 1,
743
- hasControls: false,
744
- hasBorders: false,
745
- selectable: false,
746
- evented: true,
747
- hoverCursor: 'move',
748
- renderType: "frameSelection", // 代表框选
749
- });
750
- _.canvasDataSource.add(_.createSelectionRectDataSource);
751
- _.canvasDataSource.requestRenderAll();
318
+ new create_1.CreateAllSelectionRectFn(fabric_1.fabric);
752
319
  };
753
320
  // 删除全选对象选择框
754
321
  CanvasHooks.prototype.removeAllSelectionRectFn = function () {
755
- var _ = this;
756
- if (!_.canvasDataSource) {
757
- return;
758
- }
759
- _.canvasDataSource.remove(_.createSelectionRectDataSource);
760
- _.canvasDataSource.requestRenderAll();
322
+ new update_1.RemoveAllSelectionRectFn();
761
323
  };
762
324
  // 自定义选择框绘制数据更新
763
325
  CanvasHooks.prototype.updateSelectionRectFn = function (options) {
764
- var _a, _b;
765
- var _ = this;
766
- if (!_.canvasDataSource) {
767
- return;
768
- }
769
- if (_.createSelectionRectDataSource) {
770
- var pointer = _.canvasDataSource.getPointer(options.e);
771
- var width = pointer.x - ((_a = _.mousePointerDataSource) === null || _a === void 0 ? void 0 : _a.x);
772
- var height = pointer.y - ((_b = _.mousePointerDataSource) === null || _b === void 0 ? void 0 : _b.y);
773
- _.createSelectionRectDataSource.set({ width: Math.abs(width), height: Math.abs(height) });
774
- // _.createSelectionRectDataSource.setCoords(); // 更新控制点坐标
775
- _.canvasDataSource.requestRenderAll();
776
- _.createSelectionStatus = 3;
777
- setTimeout(function () {
778
- _.createSelectionStatus = 0;
779
- }, 0);
780
- }
326
+ new update_1.UpdateSelectionRectFn(options);
781
327
  };
782
328
  // 自定义选择框绘制完成
783
- CanvasHooks.prototype.createSelectionRectFinishFn = function (options) {
784
- var _ = this;
785
- if (!_.canvasDataSource) {
786
- return;
787
- }
788
- _.canvasSelectionStatus = false;
789
- _.isRightClick = false;
790
- _.canvasDragStatus = true;
791
- _.mouseRightClickStatus = false;
792
- if (_.createSelectionRectDataSource) {
793
- _.canvasSelectionElementsDataSource.push(_.createSelectionRectDataSource);
794
- _.canvasCreateElementsDataSource.push(_.createSelectionRectDataSource);
795
- _.createSelectionRectDataSource.setCoords(); // 更新控制点坐标
796
- _.createSelectionRectDataSource = null; // 清除引用,结束绘制
797
- }
798
- };
799
- // 获取对应的DOM元素
800
- CanvasHooks.prototype.getDomFn = function (id) {
801
- if (!id)
802
- return false;
803
- var dom = document.getElementById(id);
804
- if (id && dom instanceof HTMLElement) {
805
- return dom;
806
- }
807
- return false;
329
+ CanvasHooks.prototype.createSelectionRectFinishFn = function () {
330
+ new update_1.CreateSelectionRectFinishFn();
808
331
  };
809
332
  CanvasHooks.prototype.init = function () {
810
333
  var _ = this;
811
334
  };
812
335
  // 获取画布容器的宽高
813
336
  CanvasHooks.prototype.elementSizeFn = function (id) {
814
- var _ = this;
815
- var box = _.getDomFn(id);
816
- if (!box) {
817
- console.error("canvas外层必须套一层id=canvasContainer的DOM元素");
818
- }
819
- var height = box.clientHeight;
820
- var width = box.clientWidth;
821
- return {
822
- height: height,
823
- width: width
824
- };
337
+ new query_1.QueryElementSizeFn(id);
825
338
  };
826
339
  // 右键菜单管理
827
340
  CanvasHooks.prototype.createRightClickMenuFn = function (options) {
828
- var _ = this;
829
- if (!_.canvasDataSource) {
830
- return;
831
- }
832
- var rightButtonsDataSource = (_.canvasHandleConfig || {}).rightButtonsDataSource;
833
- var rightButtonMenuClickBackFn = (_.callBackEventConfig || {}).rightButtonMenuClickBackFn;
834
- if (!(rightButtonsDataSource === null || rightButtonsDataSource === void 0 ? void 0 : rightButtonsDataSource.length)) {
835
- return;
836
- }
837
- var canvasContainer = _.getDomFn("canvasContainer");
838
- var oldMenuBox = _.getDomFn("menuBox");
839
- if (canvasContainer && oldMenuBox) {
840
- canvasContainer === null || canvasContainer === void 0 ? void 0 : canvasContainer.removeChild(oldMenuBox);
841
- }
842
- var menuBox = _.createElementFn("menuBox", "div");
843
- if (rightButtonsDataSource === null || rightButtonsDataSource === void 0 ? void 0 : rightButtonsDataSource.length) {
844
- var ulBox_1 = _.createElementFn("ulBox", "ul");
845
- ulBox_1.style = "\n list-style-type: none;\n padding:0px;\n margin:0px;\n ";
846
- var liStype_1 = "\n padding:8px 12px;\n cursor: pointer;\n border-radius:4px;\n font-size:16px;\n ";
847
- rightButtonsDataSource.forEach(function (item, index) {
848
- var liItem = _.createElementFn("li", "liBox".concat(index + 1), index);
849
- liItem.innerHTML = (item === null || item === void 0 ? void 0 : item.label) || "请传入label值";
850
- liItem.style = liStype_1;
851
- liItem.className = "right-menu-button";
852
- liItem.addEventListener("click", function (event) {
853
- // 点击右键菜单回调函数
854
- console.log("rightButtonMenuClickBackFn==", rightButtonMenuClickBackFn);
855
- if (rightButtonMenuClickBackFn instanceof Function) {
856
- rightButtonMenuClickBackFn(event.target['data-index']);
857
- }
858
- _.canvasMenuShowStatus = false;
859
- _.mouseRightClickStatus = false;
860
- _.hiddenRightClickMenuFn();
861
- });
862
- liItem.addEventListener('mousemove', function () {
863
- var lists = document.getElementsByClassName("right-menu-button");
864
- for (var i = 0; i < lists.length; i++) {
865
- var element = lists[i];
866
- element.style.backgroundColor = "#fff";
867
- element.style.color = "#333";
868
- }
869
- liItem.style.backgroundColor = '#0AC496';
870
- liItem.style.color = '#fff';
871
- liItem.style.fontWeight = "bold";
872
- });
873
- ulBox_1.appendChild(liItem);
874
- });
875
- menuBox.appendChild(ulBox_1);
876
- }
877
- if (menuBox) {
878
- menuBox.id = "menuBox";
879
- // 显示菜单,设置右键菜单位置
880
- // 获取菜单组件的宽高
881
- var menuWidth = menuBox.offsetWidth;
882
- var menuHeight = menuBox.offsetHeight;
883
- // 当前鼠标位置
884
- var pointX = options.pointer.x;
885
- var pointY = options.pointer.y;
886
- // 计算菜单出现的位置
887
- // 如果鼠标靠近画布右侧,菜单就出现在鼠标指针左侧
888
- if (_.canvasDataSource.width - pointX <= menuWidth) {
889
- pointX -= menuWidth;
890
- }
891
- // 如果鼠标靠近画布底部,菜单就出现在鼠标指针上方
892
- if (_.canvasDataSource.height - pointY <= menuHeight) {
893
- pointY -= menuHeight;
894
- }
895
- // 将菜单展示出来
896
- menuBox.style = "\n visibility: visible;\n left: ".concat(pointX, "px;\n top: ").concat(pointY, "px;\n z-index: 10000000;\n padding:16px 12px;\n border-radius:4px;\n background-color:#fff;\n position: absolute;\n box-shadow: h-shadow v-shadow blur spread color inset;\n ");
897
- if (canvasContainer) {
898
- canvasContainer.appendChild(menuBox);
899
- _.isRightClick = true;
900
- _.canvasDragStatus = false; // 进行右键操作时,先禁用画布拖拽操作功能
901
- }
902
- else {
903
- console.error("请在canvas元素外层包一个DIV容器,且id='canvasContainer'");
904
- }
905
- }
341
+ new create_1.CreateRightClickMenuFn(options);
906
342
  };
907
343
  CanvasHooks.prototype.hiddenRightClickMenuFn = function () {
908
- var _ = this;
909
- var canvasContainer = _.getDomFn("canvasContainer");
910
- var menuBox = _.getDomFn("menuBox");
911
- if (canvasContainer && menuBox) {
912
- canvasContainer === null || canvasContainer === void 0 ? void 0 : canvasContainer.removeChild(menuBox);
913
- }
344
+ new update_1.HiddenRightClickMenuFn();
914
345
  };
915
346
  // 在画布上动态创建对象
916
347
  CanvasHooks.prototype.createElementFn = function (domVal, id, index) {
917
- var _ = this;
918
- var element = _.getDomFn(id);
919
- if (element) {
920
- return;
921
- }
922
- else {
923
- if (domVal) {
924
- var dom = document.createElement(domVal);
925
- dom.id = id;
926
- if (typeof index == "undefined") {
927
- index = 0;
928
- }
929
- if (index >= 0) {
930
- dom['data-index'] = index;
931
- }
932
- return dom;
933
- }
934
- return;
935
- }
348
+ new create_1.CreateElementFn(domVal, id, index);
936
349
  };
937
350
  // 执行返回上一步操作
938
351
  CanvasHooks.prototype.previousStepFn = function () {
939
- var _ = this;
940
- if (!_.canvasDataSource) {
941
- return;
942
- }
943
- var list = _.canvasCreateElementsDataSource;
944
- var len = list.length;
945
- if (len) {
946
- var targetElement_1 = list[len - 1];
947
- if (targetElement_1) {
948
- _.canvasDataSource.remove(targetElement_1);
949
- _.canvasCreateElementsDataSource.splice(len - 1, 1);
950
- var index = _.canvasCreateTargetElementsDataSource.findIndex(function (citem) { return citem.id == targetElement_1.id; });
951
- if (index >= 0) {
952
- _.canvasCreateTargetElementsDataSource.splice(index, 1);
953
- }
954
- }
955
- _.canvasDataSource.requestRenderAll();
956
- }
352
+ new changed_1.PreviousStepFn();
957
353
  };
958
354
  // 移除画布上指定对象
959
355
  CanvasHooks.prototype.removeElementFn = function (id) {
960
- if (!id) {
961
- return;
962
- }
963
- var _ = this;
964
- if (!_.canvasDataSource) {
965
- return;
966
- }
967
- var list = _.canvasCreateElementsDataSource;
968
- var targetElement = list.find(function (obj) {
969
- return obj.id === id;
970
- });
971
- if (targetElement) {
972
- _.canvasDataSource.remove(targetElement);
973
- }
974
- _.canvasDataSource.requestRenderAll();
356
+ new update_1.RemoveElementFn(id);
975
357
  };
976
358
  // 清空画布上所有对象
977
359
  CanvasHooks.prototype.removeAllElementFn = function () {
978
- var _ = this;
979
- if (!_.canvasDataSource) {
980
- return;
981
- }
982
- var list = _.canvasCreateElementsDataSource;
983
- _.createSelectionRectDataSource = [];
984
- _.canvasCreateElementsDataSource = [];
985
- _.createSelectionRectDataSource = [];
986
- _.dragElementDataSource = [];
987
- _.canvasSelectionElementsDataSource = [];
988
- _.canvasCreateTargetElementsDataSource = [];
989
- _.defaultCreateDataList = [];
990
- list.map(function (item) {
991
- if (item) {
992
- _.canvasDataSource.remove(item);
993
- }
994
- });
995
- _.canvasDataSource.requestRenderAll();
360
+ new update_1.RemoveAllElementFn();
996
361
  };
997
362
  // 对于画布出现缩放的情况,加载进入的数据会产生偏移处理
998
363
  // 问题未解决,备注
999
364
  CanvasHooks.prototype.resetElementSizeFn = function () {
1000
- var _a;
1001
- var _ = this;
1002
- if (!_.canvasDataSource) {
1003
- return;
1004
- }
1005
- var _b = _.canvasConfig.actions, actions = _b === void 0 ? "" : _b;
1006
- var backgroundImage = ((_a = _.canvasDataSource) === null || _a === void 0 ? void 0 : _a.backgroundImage) || "";
1007
- var zoomValue = (actions === null || actions === void 0 ? void 0 : actions.zoom) || 1;
1008
- console.log("resetElementSizeFn-zoomValue=", zoomValue);
1009
- var scaleX = (backgroundImage === null || backgroundImage === void 0 ? void 0 : backgroundImage.scaleX) || 1;
1010
- var scaleY = (backgroundImage === null || backgroundImage === void 0 ? void 0 : backgroundImage.scaleY) || 1;
1011
- if (zoomValue != 1) {
1012
- // _.canvasDataSource.zoom = zoomValue
1013
- scaleX = scaleX * zoomValue;
1014
- scaleY = scaleY * zoomValue;
1015
- }
1016
- if (backgroundImage) {
1017
- // backgroundImage.set({
1018
- // scaleX:scaleX,
1019
- // scaleY:scaleY,
1020
- // })
1021
- }
1022
- // 默认加载完对象后,如果自动放大画布,则画布上元素的坐标需要做与画布同级别的缩放处理
1023
- _.canvasDataSource.forEachObject(function (obj) {
1024
- if (obj.elementType === 'icon') {
1025
- // 重新计算位置
1026
- // obj.set({
1027
- // left: obj.left * zoomValue,
1028
- // top: obj.top * zoomValue,
1029
- // })
1030
- // obj.setCoords(); // 更新对象的坐标
1031
- }
1032
- });
1033
- _.canvasDataSource.requestRenderAll();
365
+ new changed_1.ResetElementSizeFn();
1034
366
  };
1035
367
  CanvasHooks.prototype.setCanvasBaseConfigFn = function (zoom) {
1036
- var _a;
1037
- var _ = this;
1038
- if (!_.canvasDataSource) {
1039
- return;
1040
- }
1041
- if (!zoom) {
1042
- return;
1043
- }
1044
- var x = 0;
1045
- var y = 0;
1046
- if ((_a = _.mouseWheelPointerDataSource) === null || _a === void 0 ? void 0 : _a.e) {
1047
- var _b = _.mouseWheelPointerDataSource.e, _c = _b.offsetX, offsetX = _c === void 0 ? 0 : _c, _d = _b.offsetY, offsetY = _d === void 0 ? 0 : _d;
1048
- x = offsetX;
1049
- y = offsetY;
1050
- }
1051
- _.canvasDataSource.zoomToPoint({
1052
- x: x,
1053
- y: y
1054
- }, zoom // 传入修改后的缩放级别
1055
- );
1056
- _.canvasDataSource.requestRenderAll();
368
+ new index_1.SetCanvasBaseConfigFn(zoom);
1057
369
  };
1058
370
  CanvasHooks.prototype.getCanvasActionDataFn = function () {
1059
- var _a, _b, _c, _d, _e;
1060
- var _ = this;
1061
- if (!_.canvasDataSource) {
1062
- return;
1063
- }
1064
- var zoom = _.canvasDataSource.getZoom();
1065
- console.log("getCanvasActionDataFn--zoom=", zoom);
1066
- var x = 0;
1067
- var y = 0;
1068
- var left = 0;
1069
- var top = 0;
1070
- if ((_a = _.mouseWheelPointerDataSource) === null || _a === void 0 ? void 0 : _a.e) {
1071
- var _f = _.mouseWheelPointerDataSource.e, _g = _f.offsetX, offsetX = _g === void 0 ? 0 : _g, _h = _f.offsetY, offsetY = _h === void 0 ? 0 : _h;
1072
- x = offsetX;
1073
- y = offsetY;
1074
- left = ((_c = (_b = _.mouseWheelPointerDataSource) === null || _b === void 0 ? void 0 : _b.absolutePointer) === null || _c === void 0 ? void 0 : _c.x) || 0;
1075
- top = ((_e = (_d = _.mouseWheelPointerDataSource) === null || _d === void 0 ? void 0 : _d.absolutePointer) === null || _e === void 0 ? void 0 : _e.y) || 0;
1076
- }
1077
- return {
1078
- zoom: zoom,
1079
- x: x,
1080
- y: y,
1081
- left: left,
1082
- top: top,
1083
- };
371
+ new query_1.QueryCanvasActionDataFn();
1084
372
  };
1085
373
  return CanvasHooks;
1086
374
  }());