mce 0.14.0 → 0.14.1

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/index.js CHANGED
@@ -4394,8 +4394,14 @@ const _frame = definePlugin((editor) => {
4394
4394
  el.style.backgroundColor = "#ffffff";
4395
4395
  return {
4396
4396
  move: (move) => {
4397
- el.style.width = Math.abs(move.x - start.x);
4398
- el.style.height = Math.abs(move.y - start.y);
4397
+ const minX = Math.min(move.x, start.x);
4398
+ const minY = Math.min(move.y, start.y);
4399
+ const maxX = Math.max(move.x, start.x);
4400
+ const maxY = Math.max(move.y, start.y);
4401
+ el.style.left = minX;
4402
+ el.style.top = minY;
4403
+ el.style.width = maxX - minX;
4404
+ el.style.height = maxY - minY;
4399
4405
  },
4400
4406
  end: () => {
4401
4407
  setActiveDrawingTool(void 0);
@@ -8275,7 +8281,7 @@ function round(cx, cy, sx, sy, ex, ey, verts, clockwise) {
8275
8281
  }
8276
8282
  class Curve {
8277
8283
  arcLengthDivision = 200;
8278
- _arcLengths;
8284
+ _lengths = [];
8279
8285
  getPointAt(u, output = new Vector2()) {
8280
8286
  return this.getPoint(this.getUToTMapping(u), output);
8281
8287
  }
@@ -8353,10 +8359,10 @@ class Curve {
8353
8359
  return lengths[lengths.length - 1];
8354
8360
  }
8355
8361
  getLengths() {
8356
- if (!this._arcLengths || this._arcLengths.length !== this.arcLengthDivision + 1) {
8362
+ if (this._lengths.length !== this.arcLengthDivision + 1) {
8357
8363
  this.updateLengths();
8358
8364
  }
8359
- return this._arcLengths;
8365
+ return this._lengths;
8360
8366
  }
8361
8367
  updateLengths() {
8362
8368
  const divisions = this.arcLengthDivision;
@@ -8367,7 +8373,7 @@ class Curve {
8367
8373
  arcLengths.push(sum);
8368
8374
  prev = current;
8369
8375
  }
8370
- this._arcLengths = arcLengths;
8376
+ this._lengths = arcLengths;
8371
8377
  }
8372
8378
  getUToTMapping(u, distance2) {
8373
8379
  const lengths = this.getLengths();
@@ -8392,14 +8398,14 @@ class Curve {
8392
8398
  break;
8393
8399
  }
8394
8400
  }
8395
- i = high;
8401
+ i = Math.max(0, high);
8396
8402
  if (lengths[i] === targetLen) {
8397
8403
  return i / (lengthsLen - 1);
8398
8404
  }
8399
8405
  const before = lengths[i];
8400
8406
  const after = lengths[i + 1];
8401
8407
  const segmentLength = after - before;
8402
- const segmentFraction = (targetLen - before) / segmentLength;
8408
+ const segmentFraction = Math.max(0, (targetLen - before) / segmentLength);
8403
8409
  return (i + segmentFraction) / (lengthsLen - 1);
8404
8410
  }
8405
8411
  getTangent(t, output = new Vector2()) {
@@ -9061,13 +9067,19 @@ class CompositeCurve extends Curve {
9061
9067
  }
9062
9068
  return output;
9063
9069
  }
9070
+ getLengths() {
9071
+ if (this._lengths.length !== this.curves.length) {
9072
+ this.updateLengths();
9073
+ }
9074
+ return this._lengths;
9075
+ }
9064
9076
  updateLengths() {
9065
- const arcLengths = [];
9077
+ const lengths = [];
9066
9078
  for (let i = 0, sum = 0, len = this.curves.length; i < len; i++) {
9067
9079
  sum += this.curves[i].getLength();
9068
- arcLengths.push(sum);
9080
+ lengths.push(sum);
9069
9081
  }
9070
- this._arcLengths = arcLengths;
9082
+ this._lengths = lengths;
9071
9083
  }
9072
9084
  getControlPointRefs() {
9073
9085
  return this.curves.flatMap((curve) => curve.getControlPointRefs());
@@ -9739,9 +9751,11 @@ class Path2D extends CompositeCurve {
9739
9751
  return this;
9740
9752
  }
9741
9753
  addPath(path) {
9742
- const index = this.curves.findIndex((v) => v === this.currentCurve);
9743
- if (index > -1) {
9744
- this.curves.splice(index, 1);
9754
+ if (!this.currentCurve.curves.length) {
9755
+ const index = this.curves.findIndex((v) => v === this.currentCurve);
9756
+ if (index > -1) {
9757
+ this.curves.splice(index, 1);
9758
+ }
9745
9759
  }
9746
9760
  if (path instanceof Path2D) {
9747
9761
  this.curves.push(
@@ -9750,17 +9764,15 @@ class Path2D extends CompositeCurve {
9750
9764
  } else if (path.curves.length) {
9751
9765
  this.curves.push(path);
9752
9766
  }
9753
- this.curves.push(this.currentCurve);
9767
+ this.currentCurve = this.curves[this.curves.length - 1];
9754
9768
  return this;
9755
9769
  }
9756
9770
  closePath() {
9757
9771
  const startPoint = this.startPoint;
9758
- if (startPoint) {
9772
+ if (startPoint && this.currentCurve.curves.length) {
9759
9773
  this.currentCurve.closePath();
9760
- if (this.currentCurve.curves.length) {
9761
- this.currentCurve = new CurvePath().moveTo(startPoint.x, startPoint.y);
9762
- this.curves.push(this.currentCurve);
9763
- }
9774
+ this.currentCurve = new CurvePath().moveTo(startPoint.x, startPoint.y);
9775
+ this.curves.push(this.currentCurve);
9764
9776
  }
9765
9777
  return this;
9766
9778
  }
@@ -10073,30 +10085,43 @@ class Path2D extends CompositeCurve {
10073
10085
  }
10074
10086
  const _pen = definePlugin((editor) => {
10075
10087
  const {
10076
- addElement
10088
+ addElement,
10089
+ renderEngine,
10090
+ activeDrawingTool,
10091
+ getGlobalPointer,
10092
+ state
10077
10093
  } = editor;
10078
- let pening = false;
10079
10094
  let el;
10080
- let path;
10095
+ let currentPath;
10096
+ let currentLine;
10097
+ const update = () => {
10098
+ if (el && currentPath) {
10099
+ el.shape.paths = [
10100
+ { data: currentPath.toData() }
10101
+ ];
10102
+ const box = currentPath.getBoundingBox();
10103
+ el.style.left = box.left;
10104
+ el.style.top = box.top;
10105
+ el.style.width = box.width;
10106
+ el.style.height = box.height;
10107
+ }
10108
+ };
10081
10109
  return {
10082
10110
  name: "mce:pen",
10083
10111
  drawingTools: [
10084
10112
  {
10085
10113
  name: "pen",
10086
10114
  handle: (start) => {
10087
- if (pening) {
10088
- path.lineTo(start.x, start.y);
10089
- path.moveTo(start.x, start.y);
10090
- el.shape.paths = [
10091
- { data: path.toData() }
10092
- ];
10093
- const box = path.getBoundingBox();
10094
- el.style.left = box.left;
10095
- el.style.top = box.top;
10096
- el.style.width = box.width;
10097
- el.style.height = box.height;
10115
+ if (el) {
10116
+ if (currentPath) {
10117
+ currentLine = new LineCurve(
10118
+ new Vector2(start.x, start.y),
10119
+ new Vector2(start.x, start.y)
10120
+ );
10121
+ currentPath.currentCurve.addCurve(currentLine);
10122
+ update();
10123
+ }
10098
10124
  } else {
10099
- pening = true;
10100
10125
  el = addElement({
10101
10126
  name: "pen",
10102
10127
  outline: {
@@ -10109,11 +10134,30 @@ const _pen = definePlugin((editor) => {
10109
10134
  inPptIs: "Shape"
10110
10135
  }
10111
10136
  }, {
10112
- active: true,
10113
10137
  position: start
10114
10138
  });
10115
- path = new Path2D();
10116
- path.moveTo(start.x, start.y);
10139
+ currentPath = new Path2D();
10140
+ currentLine = new LineCurve(
10141
+ new Vector2(start.x, start.y),
10142
+ new Vector2(start.x, start.y)
10143
+ );
10144
+ currentPath.currentCurve.addCurve(currentLine);
10145
+ const onMove = () => {
10146
+ const move = getGlobalPointer();
10147
+ if (currentLine && move) {
10148
+ currentLine.p2.x = move.x;
10149
+ currentLine.p2.y = move.y;
10150
+ update();
10151
+ }
10152
+ };
10153
+ renderEngine.value.on("pointermove", onMove);
10154
+ const stop = watch([state, activeDrawingTool], () => {
10155
+ renderEngine.value.off("pointermove", onMove);
10156
+ stop();
10157
+ el = void 0;
10158
+ currentPath = void 0;
10159
+ currentLine = void 0;
10160
+ });
10117
10161
  }
10118
10162
  }
10119
10163
  },
@@ -10134,22 +10178,20 @@ const _pen = definePlugin((editor) => {
10134
10178
  }, {
10135
10179
  position: start
10136
10180
  });
10137
- const path2 = new Path2D();
10138
- path2.moveTo(start.x, start.y);
10181
+ const path = new Path2D();
10182
+ path.moveTo(start.x, start.y);
10139
10183
  return {
10140
10184
  move: (move) => {
10141
- path2.lineTo(move.x, move.y);
10142
- path2.moveTo(move.x, move.y);
10185
+ path.lineTo(move.x, move.y);
10186
+ path.moveTo(move.x, move.y);
10143
10187
  el2.shape.paths = [
10144
- { data: path2.toData() }
10188
+ { data: path.toData() }
10145
10189
  ];
10146
- const box = path2.getBoundingBox();
10190
+ const box = path.getBoundingBox();
10147
10191
  el2.style.left = box.left;
10148
10192
  el2.style.top = box.top;
10149
10193
  el2.style.width = box.width;
10150
10194
  el2.style.height = box.height;
10151
- },
10152
- end: () => {
10153
10195
  }
10154
10196
  };
10155
10197
  }
@@ -10928,8 +10970,14 @@ const _shape = definePlugin((editor) => {
10928
10970
  });
10929
10971
  return {
10930
10972
  move: (move) => {
10931
- el.style.width = Math.abs(move.x - start.x);
10932
- el.style.height = Math.abs(move.y - start.y);
10973
+ const minX = Math.min(move.x, start.x);
10974
+ const minY = Math.min(move.y, start.y);
10975
+ const maxX = Math.max(move.x, start.x);
10976
+ const maxY = Math.max(move.y, start.y);
10977
+ el.style.left = minX;
10978
+ el.style.top = minY;
10979
+ el.style.width = maxX - minX;
10980
+ el.style.height = maxY - minY;
10933
10981
  },
10934
10982
  end: () => {
10935
10983
  setActiveDrawingTool(void 0);
@@ -15111,7 +15159,7 @@ const _sfc_main$1 = /* @__PURE__ */ defineComponent({
15111
15159
  return (_ctx, _cache) => {
15112
15160
  return openBlock(), createBlock(_sfc_main$5, {
15113
15161
  class: normalizeClass(["mce-editor", [
15114
- `mce-editor--${unref(state)}`,
15162
+ unref(state) && `mce-editor--${unref(state)}`,
15115
15163
  unref(activeDrawingTool) && `mce-editor--drawing-tool-${unref(activeDrawingTool).name}`,
15116
15164
  grabbing.value && `mce-editor--grabbing`
15117
15165
  ]])
@@ -1,6 +1,7 @@
1
1
  declare global {
2
2
  namespace Mce {
3
3
  interface DrawingTools {
4
+ pen: [];
4
5
  pencil: [];
5
6
  }
6
7
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mce",
3
3
  "type": "module",
4
- "version": "0.14.0",
4
+ "version": "0.14.1",
5
5
  "description": "The headless canvas editor framework. only the ESM.",
6
6
  "author": "wxm",
7
7
  "license": "MIT",