@mce/bigesj 0.16.6 → 0.16.7

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.
Files changed (2) hide show
  1. package/dist/index.js +233 -241
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -5554,238 +5554,6 @@ function convertSvgLinearGradient(value) {
5554
5554
  return `linear-gradient(${args.join(", ")})`;
5555
5555
  }
5556
5556
  }
5557
- async function convertImageElementToUrl(el) {
5558
- const {
5559
- transform = {},
5560
- style = {},
5561
- maskUrl,
5562
- imageEffects = [],
5563
- imageEffectsRatio = 1
5564
- } = el;
5565
- const url = el.clipUrl || el.url;
5566
- const {
5567
- translateX = 0,
5568
- translateY = 0,
5569
- zoom = 1
5570
- } = transform ?? {};
5571
- const {
5572
- scaleX = 1,
5573
- scaleY = 1,
5574
- filter
5575
- } = style;
5576
- if (translateX === 0 && translateY === 0 && zoom === 1 && scaleX === 1 && scaleY === 1 && !maskUrl && !filter && !imageEffects.length) {
5577
- return url;
5578
- }
5579
- const img = await assets.fetchImageBitmap(url);
5580
- const {
5581
- originWidth = img.width,
5582
- originHeight = img.height,
5583
- imageWidth = originWidth,
5584
- imageHeight = originHeight
5585
- } = transform;
5586
- const {
5587
- width = originWidth,
5588
- height = originHeight
5589
- } = style;
5590
- const dpr = window.devicePixelRatio || 1;
5591
- const [canvas, ctx] = createCanvas(width, height, dpr);
5592
- if (filter)
5593
- ctx.filter = filter;
5594
- ctx.scale(scaleX, scaleY);
5595
- ctx.translate(scaleX < 0 ? -width : 0, scaleY < 0 ? -height : 0);
5596
- if (maskUrl) {
5597
- const mask = await assets.fetchImageBitmap(maskUrl);
5598
- ctx.drawImage(mask, 0, 0, mask.width, mask.height, 0, 0, width, height);
5599
- mask.close();
5600
- ctx.globalCompositeOperation = "source-in";
5601
- }
5602
- const dw = imageWidth * zoom;
5603
- const dh = imageHeight * zoom;
5604
- const dx = -(dw / 2 - imageWidth / 2) + translateX;
5605
- const dy = -(dh / 2 - imageHeight / 2) + translateY;
5606
- ctx.drawImage(img, 0, 0, img.width, img.height, dx, dy, dw, dh);
5607
- img.close();
5608
- ctx.globalCompositeOperation = "source-over";
5609
- if (imageEffects.length > 0) {
5610
- const scale = 0.9;
5611
- const center = {
5612
- x: (width - width * scale) / 2,
5613
- y: (height - height * scale) / 2
5614
- };
5615
- const canvasBitmap = await createImageBitmap(canvas);
5616
- ctx.clearRect(0, 0, canvas.width, canvas.height);
5617
- ctx.scale(scale, scale);
5618
- for (let i = imageEffects.length - 1; i >= 0; i--) {
5619
- const { filling, offset, stroke } = imageEffects[i];
5620
- let effectCanvas = canvasBitmap;
5621
- if (filling) {
5622
- const [canvas1, ctx1] = createCanvas(width, height, dpr);
5623
- ctx1.drawImage(effectCanvas, 0, 0, width, height);
5624
- ctx1.globalCompositeOperation = "source-in";
5625
- if (filling.color) {
5626
- const [canvas2, ctx2] = createCanvas(width, height, dpr);
5627
- ctx2.fillStyle = filling.color;
5628
- ctx2.fillRect(0, 0, width, height);
5629
- ctx1.drawImage(canvas2, 0, 0, width, height);
5630
- } else if (filling.imageContent?.image) {
5631
- const img2 = await assets.fetchImageBitmap(filling.imageContent.image);
5632
- ctx1.drawImage(img2, 0, 0, width, height);
5633
- img2.close();
5634
- }
5635
- effectCanvas = canvas1;
5636
- }
5637
- stroke?.forEach(({ width: width2, color }) => {
5638
- effectCanvas = new ImageStroke().use((ctx2, image, options) => {
5639
- const [, ctx1] = createCanvas(image.width, image.height);
5640
- ctx1.drawImage(image, 0, 0);
5641
- const paths = getContours(ctx1);
5642
- const x = options.thickness;
5643
- const y = options.thickness;
5644
- ctx2.strokeStyle = options.color;
5645
- ctx2.lineWidth = options.thickness * 2;
5646
- ctx2.lineJoin = "round";
5647
- paths.forEach((path) => {
5648
- ctx2.beginPath();
5649
- ctx2.moveTo(x + path[0].x, y + path[1].y);
5650
- for (let i2 = 1; i2 < path.length; i2++) {
5651
- ctx2.lineTo(x + path[i2].x, y + path[i2].y);
5652
- }
5653
- ctx2.closePath();
5654
- });
5655
- ctx2.stroke();
5656
- }).make(effectCanvas, {
5657
- color,
5658
- thickness: width2 / 50 * imageEffectsRatio
5659
- });
5660
- });
5661
- if (offset) {
5662
- let { x, y } = offset;
5663
- x = x / 50 * imageEffectsRatio * 200;
5664
- y = y / 50 * imageEffectsRatio * 200;
5665
- ctx.drawImage(effectCanvas, x + center.x, y + center.y, width, height);
5666
- } else {
5667
- ctx.drawImage(effectCanvas, center.x, center.y, width, height);
5668
- }
5669
- }
5670
- canvasBitmap.close();
5671
- }
5672
- return await new Promise((resolve) => {
5673
- canvas.toBlob((blob) => {
5674
- try {
5675
- resolve(URL.createObjectURL(blob));
5676
- } catch (e) {
5677
- console.error(`Failed to URL.createObjectURL, url: ${url}`, e);
5678
- resolve(url);
5679
- }
5680
- });
5681
- });
5682
- }
5683
- function createCanvas(width, height, ratio = 1) {
5684
- const canvas = document.createElement("canvas");
5685
- canvas.width = width * ratio;
5686
- canvas.height = height * ratio;
5687
- canvas.style.width = `${width}px`;
5688
- canvas.style.height = `${height}px`;
5689
- const ctx = canvas.getContext("2d");
5690
- ctx.scale(ratio, ratio);
5691
- return [canvas, ctx];
5692
- }
5693
- class ImageStroke {
5694
- canvas = document.createElement("canvas");
5695
- method;
5696
- use(method) {
5697
- this.method = method;
5698
- return this;
5699
- }
5700
- make(image, options) {
5701
- const { canvas } = this;
5702
- const ctx = this.canvas.getContext("2d");
5703
- const strokeSize = options.thickness * 2;
5704
- const [resultWidth, resultHeight] = [image.width, image.height].map((val) => val + strokeSize);
5705
- if (resultWidth !== canvas.width || resultHeight !== canvas.height) {
5706
- canvas.width = resultWidth;
5707
- canvas.height = resultHeight;
5708
- }
5709
- ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
5710
- this.method(ctx, image, options);
5711
- ctx.drawImage(image, options.thickness, options.thickness);
5712
- return canvas;
5713
- }
5714
- }
5715
- function getContours(ctx) {
5716
- const cx = 0;
5717
- const cy = 0;
5718
- const canvasWidth = ctx.canvas.width;
5719
- const canvasHeight = ctx.canvas.height;
5720
- const paths = [];
5721
- let lastPos = 3;
5722
- const alpha = 100;
5723
- const trace = () => {
5724
- const path = [];
5725
- const data = new Uint32Array(ctx.getImageData(cx, cy, canvasWidth, canvasHeight).data.buffer);
5726
- let x;
5727
- let y;
5728
- let startX;
5729
- let startY;
5730
- let startPos = -1;
5731
- let step;
5732
- let prevStep = 9;
5733
- const steps = [9, 0, 3, 3, 2, 0, 9, 3, 1, 9, 1, 1, 2, 0, 2, 9];
5734
- function getState(x2, y2) {
5735
- return x2 >= 0 && y2 >= 0 && x2 < canvasWidth && y2 < canvasHeight ? data[y2 * canvasWidth + x2] >>> 24 > alpha : false;
5736
- }
5737
- function getNextStep(x2, y2) {
5738
- let v = 0;
5739
- if (getState(x2 - 1, y2 - 1)) {
5740
- v += 1;
5741
- }
5742
- if (getState(x2, y2 - 1)) {
5743
- v += 2;
5744
- }
5745
- if (getState(x2 - 1, y2)) {
5746
- v += 4;
5747
- }
5748
- if (getState(x2, y2)) {
5749
- v += 8;
5750
- }
5751
- if (v === 6)
5752
- return prevStep === 0 ? 2 : 3;
5753
- else if (v === 9)
5754
- return prevStep === 3 ? 0 : 1;
5755
- else
5756
- return steps[v];
5757
- }
5758
- for (let i = lastPos; i < data.length; i++) {
5759
- if (data[i] >>> 24 > alpha) {
5760
- startPos = lastPos = i;
5761
- break;
5762
- }
5763
- }
5764
- if (startPos >= 0) {
5765
- x = startX = startPos % canvasWidth;
5766
- y = startY = Math.floor(startPos / canvasWidth);
5767
- do {
5768
- step = getNextStep(x, y);
5769
- if (step === 0)
5770
- y--;
5771
- else if (step === 1)
5772
- y++;
5773
- else if (step === 2)
5774
- x--;
5775
- else if (step === 3)
5776
- x++;
5777
- if (step !== prevStep) {
5778
- path.push({ x: x + cx, y: y + cy });
5779
- prevStep = step;
5780
- }
5781
- } while (x !== startX || y !== startY);
5782
- }
5783
- paths.push(path);
5784
- return path;
5785
- };
5786
- trace();
5787
- return paths;
5788
- }
5789
5557
  async function convertSvgElementToUrl(el) {
5790
5558
  const {
5791
5559
  id,
@@ -5849,15 +5617,7 @@ async function convertSvgElementToUrl(el) {
5849
5617
  svg.setAttribute("width", String(style.width * 2));
5850
5618
  if (style.height)
5851
5619
  svg.setAttribute("height", String(style.height * 2));
5852
- return await convertImageElementToUrl({
5853
- ...el,
5854
- transform: {
5855
- ...el.transform,
5856
- originWidth: style.width,
5857
- originHeight: style.height
5858
- },
5859
- url: `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg.outerHTML)}`
5860
- });
5620
+ return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg.outerHTML)}`;
5861
5621
  }
5862
5622
  const highlightReferImage = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2ZXJzaW9uPSIxLjEiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgNzIgNzIiIHN0eWxlPSJlbmFibGUtYmFja2dyb3VuZDpuZXcgMCAwIDcyIDcyOyIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+CjxnPgoJPHBhdGggZD0iTTE5LjMsMzguMWMwLjIsMC41LDAuNCwwLjksMC43LDEuNGMwLjIsMC4zLDAuNCwwLjQsMC44LDAuNGMwLjMsMCwwLjUtMC4xLDAuNi0wLjRjMC4xLTAuMywwLjEtMC42LDAuMi0wLjkgICBjMC0wLjIsMC4xLTAuNCwwLjMtMC41YzAuMi0wLjEsMC40LTAuMiwwLjYtMC4xYzAuMiwwLDAuNCwwLjEsMC41LDAuM2MwLjEsMC4yLDAuMiwwLjQsMC4xLDAuNmMtMC4xLDAuNi0wLjIsMS4yLTAuNCwxLjcgICBjLTAuMywwLjgtMC45LDEuMi0xLjcsMS4yYy0wLjksMC0xLjYtMC40LTIuMS0xLjJjLTAuMy0wL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AtMS42YzAtMC4yLDAuMS0wLjQsMC4yLTAuNmMwLjItMC4yLDAuMy0wLjIsMC42LTAuMiAgIHMwLjQsMC4xLDAuNiwwLjJjMC4yLDAuMiwwLjIsMC40LDAuMiwwLjZWNDAuN3ogTTUyLjgsNDAuN2MwLDAuMi0wLjEsMC40LTAuMiwwLjZjLTAuMiwwLjEtMC40LDAuMi0wLjYsMC4yICAgYy0wLjIsMC0wLjQtMC4xLTAuNi0wLjJjLTAuMi0wLjEtMC4yLTAuMy0wLjItMC42bDAtMS42YzAtMC4yLDAuMS0wLjQsMC4yLTAuNmMwLjItMC4yLDAuMy0wLjIsMC42LTAuMmMwLjIsMCwwLjQsMC4xLDAuNiwwLjIgICBjMC4yLDAuMiwwLjIsMC40LDAuMiwwLjZWNDAuN3ogTTU2LjYsNDAuM2MwLjIsMC40LDAuMSwwLjgtMC4zLDEuMWMtMC41LDAuMy0wLjgsMC4yLTEuMS0wLjNsLTEtMS43Yy0wLjItMC41LTAuMS0wLjgsMC4zLTEuMSAgIGMwLjUtMC4yLDAuOS0wLjEsMS4xLDAuM0w1Ni42LDQwLjN6Ii8+CjwvZz4KPC9zdmc+";
5863
5623
  function convertTextStyle(el, isByWord = false) {
@@ -6336,6 +6096,238 @@ async function convertDoc(doc, options = {}) {
6336
6096
  }
6337
6097
  };
6338
6098
  }
6099
+ async function convertImageElementToUrl(el) {
6100
+ const {
6101
+ transform = {},
6102
+ style = {},
6103
+ maskUrl,
6104
+ imageEffects = [],
6105
+ imageEffectsRatio = 1
6106
+ } = el;
6107
+ const url = el.clipUrl || el.url;
6108
+ const {
6109
+ translateX = 0,
6110
+ translateY = 0,
6111
+ zoom = 1
6112
+ } = transform ?? {};
6113
+ const {
6114
+ scaleX = 1,
6115
+ scaleY = 1,
6116
+ filter
6117
+ } = style;
6118
+ if (translateX === 0 && translateY === 0 && zoom === 1 && scaleX === 1 && scaleY === 1 && !maskUrl && !filter && !imageEffects.length) {
6119
+ return url;
6120
+ }
6121
+ const img = await assets.fetchImageBitmap(url);
6122
+ const {
6123
+ originWidth = img.width,
6124
+ originHeight = img.height,
6125
+ imageWidth = originWidth,
6126
+ imageHeight = originHeight
6127
+ } = transform;
6128
+ const {
6129
+ width = originWidth,
6130
+ height = originHeight
6131
+ } = style;
6132
+ const dpr = window.devicePixelRatio || 1;
6133
+ const [canvas, ctx] = createCanvas(width, height, dpr);
6134
+ if (filter)
6135
+ ctx.filter = filter;
6136
+ ctx.scale(scaleX, scaleY);
6137
+ ctx.translate(scaleX < 0 ? -width : 0, scaleY < 0 ? -height : 0);
6138
+ if (maskUrl) {
6139
+ const mask = await assets.fetchImageBitmap(maskUrl);
6140
+ ctx.drawImage(mask, 0, 0, mask.width, mask.height, 0, 0, width, height);
6141
+ mask.close();
6142
+ ctx.globalCompositeOperation = "source-in";
6143
+ }
6144
+ const dw = imageWidth * zoom;
6145
+ const dh = imageHeight * zoom;
6146
+ const dx = -(dw / 2 - imageWidth / 2) + translateX;
6147
+ const dy = -(dh / 2 - imageHeight / 2) + translateY;
6148
+ ctx.drawImage(img, 0, 0, img.width, img.height, dx, dy, dw, dh);
6149
+ img.close();
6150
+ ctx.globalCompositeOperation = "source-over";
6151
+ if (imageEffects.length > 0) {
6152
+ const scale = 0.9;
6153
+ const center = {
6154
+ x: (width - width * scale) / 2,
6155
+ y: (height - height * scale) / 2
6156
+ };
6157
+ const canvasBitmap = await createImageBitmap(canvas);
6158
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
6159
+ ctx.scale(scale, scale);
6160
+ for (let i = imageEffects.length - 1; i >= 0; i--) {
6161
+ const { filling, offset, stroke } = imageEffects[i];
6162
+ let effectCanvas = canvasBitmap;
6163
+ if (filling) {
6164
+ const [canvas1, ctx1] = createCanvas(width, height, dpr);
6165
+ ctx1.drawImage(effectCanvas, 0, 0, width, height);
6166
+ ctx1.globalCompositeOperation = "source-in";
6167
+ if (filling.color) {
6168
+ const [canvas2, ctx2] = createCanvas(width, height, dpr);
6169
+ ctx2.fillStyle = filling.color;
6170
+ ctx2.fillRect(0, 0, width, height);
6171
+ ctx1.drawImage(canvas2, 0, 0, width, height);
6172
+ } else if (filling.imageContent?.image) {
6173
+ const img2 = await assets.fetchImageBitmap(filling.imageContent.image);
6174
+ ctx1.drawImage(img2, 0, 0, width, height);
6175
+ img2.close();
6176
+ }
6177
+ effectCanvas = canvas1;
6178
+ }
6179
+ stroke?.forEach(({ width: width2, color }) => {
6180
+ effectCanvas = new ImageStroke().use((ctx2, image, options) => {
6181
+ const [, ctx1] = createCanvas(image.width, image.height);
6182
+ ctx1.drawImage(image, 0, 0);
6183
+ const paths = getContours(ctx1);
6184
+ const x = options.thickness;
6185
+ const y = options.thickness;
6186
+ ctx2.strokeStyle = options.color;
6187
+ ctx2.lineWidth = options.thickness * 2;
6188
+ ctx2.lineJoin = "round";
6189
+ paths.forEach((path) => {
6190
+ ctx2.beginPath();
6191
+ ctx2.moveTo(x + path[0].x, y + path[1].y);
6192
+ for (let i2 = 1; i2 < path.length; i2++) {
6193
+ ctx2.lineTo(x + path[i2].x, y + path[i2].y);
6194
+ }
6195
+ ctx2.closePath();
6196
+ });
6197
+ ctx2.stroke();
6198
+ }).make(effectCanvas, {
6199
+ color,
6200
+ thickness: width2 / 50 * imageEffectsRatio
6201
+ });
6202
+ });
6203
+ if (offset) {
6204
+ let { x, y } = offset;
6205
+ x = x / 50 * imageEffectsRatio * 200;
6206
+ y = y / 50 * imageEffectsRatio * 200;
6207
+ ctx.drawImage(effectCanvas, x + center.x, y + center.y, width, height);
6208
+ } else {
6209
+ ctx.drawImage(effectCanvas, center.x, center.y, width, height);
6210
+ }
6211
+ }
6212
+ canvasBitmap.close();
6213
+ }
6214
+ return await new Promise((resolve) => {
6215
+ canvas.toBlob((blob) => {
6216
+ try {
6217
+ resolve(URL.createObjectURL(blob));
6218
+ } catch (e) {
6219
+ console.error(`Failed to URL.createObjectURL, url: ${url}`, e);
6220
+ resolve(url);
6221
+ }
6222
+ });
6223
+ });
6224
+ }
6225
+ function createCanvas(width, height, ratio = 1) {
6226
+ const canvas = document.createElement("canvas");
6227
+ canvas.width = width * ratio;
6228
+ canvas.height = height * ratio;
6229
+ canvas.style.width = `${width}px`;
6230
+ canvas.style.height = `${height}px`;
6231
+ const ctx = canvas.getContext("2d");
6232
+ ctx.scale(ratio, ratio);
6233
+ return [canvas, ctx];
6234
+ }
6235
+ class ImageStroke {
6236
+ canvas = document.createElement("canvas");
6237
+ method;
6238
+ use(method) {
6239
+ this.method = method;
6240
+ return this;
6241
+ }
6242
+ make(image, options) {
6243
+ const { canvas } = this;
6244
+ const ctx = this.canvas.getContext("2d");
6245
+ const strokeSize = options.thickness * 2;
6246
+ const [resultWidth, resultHeight] = [image.width, image.height].map((val) => val + strokeSize);
6247
+ if (resultWidth !== canvas.width || resultHeight !== canvas.height) {
6248
+ canvas.width = resultWidth;
6249
+ canvas.height = resultHeight;
6250
+ }
6251
+ ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
6252
+ this.method(ctx, image, options);
6253
+ ctx.drawImage(image, options.thickness, options.thickness);
6254
+ return canvas;
6255
+ }
6256
+ }
6257
+ function getContours(ctx) {
6258
+ const cx = 0;
6259
+ const cy = 0;
6260
+ const canvasWidth = ctx.canvas.width;
6261
+ const canvasHeight = ctx.canvas.height;
6262
+ const paths = [];
6263
+ let lastPos = 3;
6264
+ const alpha = 100;
6265
+ const trace = () => {
6266
+ const path = [];
6267
+ const data = new Uint32Array(ctx.getImageData(cx, cy, canvasWidth, canvasHeight).data.buffer);
6268
+ let x;
6269
+ let y;
6270
+ let startX;
6271
+ let startY;
6272
+ let startPos = -1;
6273
+ let step;
6274
+ let prevStep = 9;
6275
+ const steps = [9, 0, 3, 3, 2, 0, 9, 3, 1, 9, 1, 1, 2, 0, 2, 9];
6276
+ function getState(x2, y2) {
6277
+ return x2 >= 0 && y2 >= 0 && x2 < canvasWidth && y2 < canvasHeight ? data[y2 * canvasWidth + x2] >>> 24 > alpha : false;
6278
+ }
6279
+ function getNextStep(x2, y2) {
6280
+ let v = 0;
6281
+ if (getState(x2 - 1, y2 - 1)) {
6282
+ v += 1;
6283
+ }
6284
+ if (getState(x2, y2 - 1)) {
6285
+ v += 2;
6286
+ }
6287
+ if (getState(x2 - 1, y2)) {
6288
+ v += 4;
6289
+ }
6290
+ if (getState(x2, y2)) {
6291
+ v += 8;
6292
+ }
6293
+ if (v === 6)
6294
+ return prevStep === 0 ? 2 : 3;
6295
+ else if (v === 9)
6296
+ return prevStep === 3 ? 0 : 1;
6297
+ else
6298
+ return steps[v];
6299
+ }
6300
+ for (let i = lastPos; i < data.length; i++) {
6301
+ if (data[i] >>> 24 > alpha) {
6302
+ startPos = lastPos = i;
6303
+ break;
6304
+ }
6305
+ }
6306
+ if (startPos >= 0) {
6307
+ x = startX = startPos % canvasWidth;
6308
+ y = startY = Math.floor(startPos / canvasWidth);
6309
+ do {
6310
+ step = getNextStep(x, y);
6311
+ if (step === 0)
6312
+ y--;
6313
+ else if (step === 1)
6314
+ y++;
6315
+ else if (step === 2)
6316
+ x--;
6317
+ else if (step === 3)
6318
+ x++;
6319
+ if (step !== prevStep) {
6320
+ path.push({ x: x + cx, y: y + cy });
6321
+ prevStep = step;
6322
+ }
6323
+ } while (x !== startX || y !== startY);
6324
+ }
6325
+ paths.push(path);
6326
+ return path;
6327
+ };
6328
+ trace();
6329
+ return paths;
6330
+ }
6339
6331
  function bidTidLoader(editor, api) {
6340
6332
  const {
6341
6333
  config,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mce/bigesj",
3
3
  "type": "module",
4
- "version": "0.16.6",
4
+ "version": "0.16.7",
5
5
  "description": "Plugin for mce",
6
6
  "author": "wxm",
7
7
  "license": "MIT",
@@ -49,7 +49,7 @@
49
49
  "modern-openxml": "^1.10.1"
50
50
  },
51
51
  "devDependencies": {
52
- "mce": "0.16.6"
52
+ "mce": "0.16.7"
53
53
  },
54
54
  "peerDependencies": {
55
55
  "mce": "^0"