xctc-utils 1.6.66 → 1.6.68

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/README.md CHANGED
@@ -1,7 +1,4 @@
1
- #npm config set registry https://registry.npm.taobao.org/
2
- npm install -g pnpm@9.6.0
3
- pnpm install
4
- pnpm run build:pro
1
+
5
2
 
6
3
  ### 项目中常用的方法集合
7
4
  #### email: dybself@163.com
@@ -33,6 +33,7 @@ export default class CanvasHooks {
33
33
  constructor(config: any);
34
34
  get canvasConfigData(): any;
35
35
  createCanvasFn(config?: any): void;
36
+ initCanvasDataFn(imgUrl: string, canvasId: any, data: any): void;
36
37
  disposeCanvasFn(): void;
37
38
  canvasConfigDataFn(config: any): void;
38
39
  registerCallBackEventFn(config?: any): void;
@@ -32,8 +32,9 @@ var index_2 = require("../event/index");
32
32
  var defaultConfig = __assign({}, config_1.configDefaultValue.canvasDefaultValue);
33
33
  var CanvasHooks = /** @class */ (function () {
34
34
  function CanvasHooks(config) {
35
+ var _this = this;
35
36
  this.fabricSource = null;
36
- this.fabricUrlSource = "https://cdn.jsdelivr.net/npm/fabric";
37
+ this.fabricUrlSource = "https://cdn.bootcss.com/fabric.js/4.0.0-beta.5/fabric.min.js";
37
38
  this.canvasDataSource = null;
38
39
  this.zoomDataSource = "100";
39
40
  this.canvasBackgroundImageTarget = null;
@@ -63,9 +64,15 @@ var CanvasHooks = /** @class */ (function () {
63
64
  this.mouseWheelStatus = false;
64
65
  this.defaultCreateDataListStatus = false;
65
66
  this.mouseCanvasInRangeStatus = false;
67
+ var url = this.fabricUrlSource;
66
68
  if (config === null || config === void 0 ? void 0 : config.init) {
67
- // 是否默认加载画布
68
- this.createCanvasFn();
69
+ (0, utils_1.loadScript)(url, function () {
70
+ // 是否默认加载画布
71
+ _this.createCanvasFn();
72
+ });
73
+ }
74
+ else {
75
+ (0, utils_1.loadScript)(url);
69
76
  }
70
77
  }
71
78
  Object.defineProperty(CanvasHooks.prototype, "canvasConfigData", {
@@ -102,7 +109,6 @@ var CanvasHooks = /** @class */ (function () {
102
109
  data['height'] = _.elementSizeFn("canvasContainer").height;
103
110
  }
104
111
  var imgUrl = (data === null || data === void 0 ? void 0 : data.backgroundImgUrl) || "";
105
- console.log("canvas--options", data);
106
112
  canvasId = data.id;
107
113
  var url = _.fabricUrlSource;
108
114
  if ((_a = _.canvasConfig) === null || _a === void 0 ? void 0 : _a.fabricUrlSource) {
@@ -111,80 +117,89 @@ var CanvasHooks = /** @class */ (function () {
111
117
  if (config === null || config === void 0 ? void 0 : config.fabricUrlSource) {
112
118
  url = config.fabricUrlSource;
113
119
  }
114
- (0, utils_1.loadScript)(url, function () {
115
- var win = window;
116
- _.fabricSource = (win === null || win === void 0 ? void 0 : win.fabric) || {};
117
- if (imgUrl) {
118
- _.fabricSource.Image.fromURL(imgUrl, function (img) {
119
- _.renderCanvasStatus = 2;
120
- _.canvasBackgroundImageTarget = img;
121
- var _a = _.canvasConfig.actions, actions = _a === void 0 ? "" : _a;
122
- var zoomValue = (actions === null || actions === void 0 ? void 0 : actions.zoom) || 1;
123
- if (!_.canvasDataSource) {
124
- _.canvasDataSource = new _.fabricSource.Canvas(canvasId, __assign({}, data));
125
- if (zoomValue != 1) {
126
- _.canvasDataSource.setZoom(zoomValue);
127
- }
128
- }
129
- var canvasWidth = _.canvasDataSource.getWidth();
130
- var canvasHeight = _.canvasDataSource.getHeight();
131
- var imgWidth = img.width;
132
- var imgHeight = img.height;
133
- var scaleX = 1;
134
- var scaleY = 1;
135
- if (canvasWidth && canvasHeight && imgWidth && imgHeight) {
136
- var imageAspectRatio = imgWidth / imgHeight;
137
- var canvasAspectRatio = canvasWidth / canvasHeight;
138
- if (imageAspectRatio > canvasAspectRatio) {
139
- // 图片更宽,基于高度缩放
140
- scaleY = canvasHeight / imgHeight;
141
- scaleX = scaleY * imageAspectRatio;
142
- }
143
- else {
144
- // 图片更高或宽高比相同,基于宽度缩放
145
- scaleX = canvasWidth / imgWidth;
146
- scaleY = scaleX / imageAspectRatio;
147
- }
148
- }
149
- console.log("原始比例:", { scaleX: scaleX, scaleY: scaleY, width: imgWidth, height: imgHeight });
150
- _.canvasDataSource.setBackgroundImage(img, _.canvasDataSource.renderAll.bind(_.canvasDataSource), {
151
- originX: 'left',
152
- originY: 'top',
153
- left: 0,
154
- top: 0,
155
- // scaleX: scaleX,
156
- // scaleY: scaleY,
157
- width: imgWidth,
158
- height: imgHeight,
159
- });
160
- _.renderCanvasStatus = 3;
161
- _.registerAllEventHandleFn();
162
- _.canvasDataSource.renderAll();
163
- callBack(_.canvasDataSource);
164
- });
165
- }
166
- else {
120
+ if (!(0, utils_1.isScriptLoaded)(url)) {
121
+ (0, utils_1.loadScript)(url, function () {
122
+ _.initCanvasDataFn(imgUrl, canvasId, data);
123
+ });
124
+ }
125
+ else {
126
+ _.initCanvasDataFn(imgUrl, canvasId, data);
127
+ }
128
+ };
129
+ CanvasHooks.prototype.initCanvasDataFn = function (imgUrl, canvasId, data) {
130
+ var _ = this;
131
+ var win = window;
132
+ _.fabricSource = (win === null || win === void 0 ? void 0 : win.fabric) || {};
133
+ if (imgUrl) {
134
+ _.fabricSource.Image.fromURL(imgUrl, function (img) {
135
+ _.renderCanvasStatus = 2;
136
+ _.canvasBackgroundImageTarget = img;
137
+ var _a = _.canvasConfig.actions, actions = _a === void 0 ? "" : _a;
138
+ var zoomValue = (actions === null || actions === void 0 ? void 0 : actions.zoom) || 1;
167
139
  if (!_.canvasDataSource) {
168
140
  _.canvasDataSource = new _.fabricSource.Canvas(canvasId, __assign({}, data));
169
- _.renderCanvasStatus = 3;
170
- _.registerAllEventHandleFn();
171
- _.canvasDataSource.renderAll();
172
- callBack(_.canvasDataSource);
141
+ if (zoomValue != 1) {
142
+ _.canvasDataSource.setZoom(zoomValue);
143
+ }
173
144
  }
174
- }
175
- // 如果配置里面有回调函数,则将当前画布数据对象返回
176
- function callBack(data) {
177
- var _a;
178
- var cb = ((_a = _.canvasConfig) === null || _a === void 0 ? void 0 : _a.cb) || "";
179
- // 获取DOM元素
180
- var targetElement = (0, utils_1.getDomFn)("canvasContainer");
181
- // 添加事件监听器
182
- document.addEventListener('mousemove', (0, index_2.checkMouseInElement)(targetElement));
183
- if (cb instanceof Function) {
184
- cb(data);
145
+ var canvasWidth = _.canvasDataSource.getWidth();
146
+ var canvasHeight = _.canvasDataSource.getHeight();
147
+ var imgWidth = img.width;
148
+ var imgHeight = img.height;
149
+ var scaleX = 1;
150
+ var scaleY = 1;
151
+ if (canvasWidth && canvasHeight && imgWidth && imgHeight) {
152
+ var imageAspectRatio = imgWidth / imgHeight;
153
+ var canvasAspectRatio = canvasWidth / canvasHeight;
154
+ if (imageAspectRatio > canvasAspectRatio) {
155
+ // 图片更宽,基于高度缩放
156
+ scaleY = canvasHeight / imgHeight;
157
+ scaleX = scaleY * imageAspectRatio;
158
+ }
159
+ else {
160
+ // 图片更高或宽高比相同,基于宽度缩放
161
+ scaleX = canvasWidth / imgWidth;
162
+ scaleY = scaleX / imageAspectRatio;
163
+ }
185
164
  }
165
+ console.log("原始比例:", { scaleX: scaleX, scaleY: scaleY, width: imgWidth, height: imgHeight });
166
+ _.canvasDataSource.setBackgroundImage(img, _.canvasDataSource.renderAll.bind(_.canvasDataSource), {
167
+ originX: 'left',
168
+ originY: 'top',
169
+ left: 0,
170
+ top: 0,
171
+ // scaleX: scaleX,
172
+ // scaleY: scaleY,
173
+ width: imgWidth,
174
+ height: imgHeight,
175
+ });
176
+ _.renderCanvasStatus = 3;
177
+ _.registerAllEventHandleFn();
178
+ _.canvasDataSource.renderAll();
179
+ callBack(_.canvasDataSource);
180
+ });
181
+ }
182
+ else {
183
+ if (!_.canvasDataSource) {
184
+ _.canvasDataSource = new _.fabricSource.Canvas(canvasId, __assign({}, data));
185
+ _.renderCanvasStatus = 3;
186
+ _.registerAllEventHandleFn();
187
+ _.canvasDataSource.renderAll();
188
+ callBack(_.canvasDataSource);
186
189
  }
187
- });
190
+ }
191
+ // 如果配置里面有回调函数,则将当前画布数据对象返回
192
+ function callBack(data) {
193
+ var _a;
194
+ var cb = ((_a = _.canvasConfig) === null || _a === void 0 ? void 0 : _a.cb) || "";
195
+ // 获取DOM元素
196
+ var targetElement = (0, utils_1.getDomFn)("canvasContainer");
197
+ // 添加事件监听器
198
+ document.addEventListener('mousemove', (0, index_2.checkMouseInElement)(targetElement));
199
+ if (cb instanceof Function) {
200
+ cb(data);
201
+ }
202
+ }
188
203
  };
189
204
  // 销毁画布
190
205
  CanvasHooks.prototype.disposeCanvasFn = function () {
package/dist/utils.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { UeditorHeightOption } from "./types";
2
2
  export declare function isMobile(): boolean;
3
3
  export declare function loadScript(url: string, callback?: any): void;
4
+ export declare const isScriptLoaded: (src: string) => boolean;
4
5
  /**
5
6
  * 判断当前传入的字符串是否为 json格式 字符串
6
7
  * @param str
package/dist/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.handleCopyValue = exports.weixinBrowser = exports.deviceEnvironment = exports.getIframeContentHeight = exports.checkIframeContentHeight = exports.getDomFn = exports.isJson = exports.loadScript = exports.isMobile = void 0;
3
+ exports.handleCopyValue = exports.weixinBrowser = exports.deviceEnvironment = exports.getIframeContentHeight = exports.checkIframeContentHeight = exports.getDomFn = exports.isJson = exports.isScriptLoaded = exports.loadScript = exports.isMobile = void 0;
4
4
  var is_1 = require("./is");
5
5
  var win = window;
6
6
  // 是否是手机端环境
@@ -14,6 +14,9 @@ function loadScript(url, callback) {
14
14
  if (!url) {
15
15
  return;
16
16
  }
17
+ if ((0, exports.isScriptLoaded)(url)) {
18
+ return;
19
+ }
17
20
  var script = document.createElement('script');
18
21
  script.type = 'text/javascript';
19
22
  if (script === null || script === void 0 ? void 0 : script.readyState) { // IE
@@ -29,11 +32,27 @@ function loadScript(url, callback) {
29
32
  callback();
30
33
  };
31
34
  }
35
+ // 错误处理
36
+ script.onerror = function (error) {
37
+ console.error('Error loading script:', error);
38
+ };
32
39
  script.src = url;
33
40
  var head = document.getElementsByTagName('head')[0];
34
41
  head.appendChild(script);
35
42
  }
36
43
  exports.loadScript = loadScript;
44
+ var isScriptLoaded = function (src) {
45
+ if (!src)
46
+ return false;
47
+ var scripts = document.scripts;
48
+ for (var i = 0; i < scripts.length; i++) {
49
+ if (scripts[i].src === src) {
50
+ return true;
51
+ }
52
+ }
53
+ return false;
54
+ };
55
+ exports.isScriptLoaded = isScriptLoaded;
37
56
  /**
38
57
  * 判断当前传入的字符串是否为 json格式 字符串
39
58
  * @param str
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "xctc-utils",
3
3
  "private": false,
4
- "version": "1.6.66",
4
+ "version": "1.6.68",
5
5
  "description": "localStorage存储\r ```\r sessionStorage存储\r ```\r crypto-js加密、解密\r ```\r 微信授权登录、微信分享\r ```\r 设备环境获取\r ```\r 是否是微信浏览器\r ```\r 时间戳转时间,字符串转时间戳",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",