@jvs-milkdown/components 1.2.1 → 1.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.
@@ -665,8 +665,11 @@ const ImageViewer = defineComponent({
665
665
  const timer = ref(0);
666
666
  const showBorderPanel = ref(false);
667
667
  const isCropping = ref(false);
668
- const displayW = ref(0);
669
- const displayH = ref(0);
668
+ const baseDisplayW = ref(0);
669
+ const baseDisplayH = ref(0);
670
+ const liveRatio = ref(null);
671
+ const localBorderWidth = ref(null);
672
+ const localBorderColor = ref(null);
670
673
  const localCrop = ref({ top: 0, left: 0, width: 1, height: 1 });
671
674
  let resizeDir = "";
672
675
  let startY = 0;
@@ -682,7 +685,6 @@ const ImageViewer = defineComponent({
682
685
  return t > 1e-3 || l > 1e-3 || w < 0.999 || h2 < 0.999;
683
686
  };
684
687
  const onImageLoad = () => {
685
- var _a2;
686
688
  const image = imageRef.value;
687
689
  if (!image) return;
688
690
  const host = image.closest(".milkdown-image-block");
@@ -693,17 +695,16 @@ const ImageViewer = defineComponent({
693
695
  maxWidth = config.maxWidth;
694
696
  const height = image.naturalHeight;
695
697
  const width = image.naturalWidth;
696
- let transformedHeight = width < maxWidth ? height : maxWidth * (height / width);
697
- if (config.maxHeight && transformedHeight > config.maxHeight)
698
+ if (!height || !width) return;
699
+ const aspect = width / height;
700
+ let transformedWidth = width < maxWidth ? width : maxWidth;
701
+ let transformedHeight = transformedWidth / aspect;
702
+ if (config.maxHeight && transformedHeight > config.maxHeight) {
698
703
  transformedHeight = config.maxHeight;
699
- const h2 = (transformedHeight * ((_a2 = ratio.value) != null ? _a2 : 1)).toFixed(2);
700
- image.dataset.origin = transformedHeight.toFixed(2);
701
- image.dataset.height = h2;
702
- image.style.height = `${h2}px`;
703
- if (config.maxWidth) image.style.maxWidth = `${config.maxWidth}px`;
704
- const dw = width < maxWidth ? width : maxWidth;
705
- displayW.value = dw;
706
- displayH.value = parseFloat(h2);
704
+ transformedWidth = transformedHeight * aspect;
705
+ }
706
+ baseDisplayH.value = transformedHeight;
707
+ baseDisplayW.value = transformedWidth;
707
708
  };
708
709
  const onToggleCaption = (e) => {
709
710
  e.preventDefault();
@@ -729,9 +730,8 @@ const ImageViewer = defineComponent({
729
730
  setAttr("caption", value);
730
731
  };
731
732
  const onResizeHandlePointerMove = (e) => {
733
+ var _a2;
732
734
  e.preventDefault();
733
- const image = imageRef.value;
734
- if (!image) return;
735
735
  const deltaY = e.clientY - startY;
736
736
  const deltaX = e.clientX - startX;
737
737
  const aspect = startHeight / (startWidth || 1);
@@ -743,39 +743,54 @@ const ImageViewer = defineComponent({
743
743
  dhX = -deltaX * aspect;
744
744
  }
745
745
  const dh = Math.abs(dhX) > Math.abs(dhY) ? dhX : dhY;
746
- let height = startHeight + dh;
747
- if (height < 20) height = 20;
748
- if (config.maxHeight && height > config.maxHeight)
749
- height = config.maxHeight;
750
- const h2 = Number(height).toFixed(2);
751
- image.dataset.height = h2;
752
- image.style.height = `${h2}px`;
746
+ let newWrapperHeight = startHeight + dh;
747
+ let newWrapperWidth = newWrapperHeight * (startWidth / (startHeight || 1));
748
+ if (newWrapperHeight < 20) {
749
+ newWrapperHeight = 20;
750
+ newWrapperWidth = newWrapperHeight * (startWidth / (startHeight || 1));
751
+ }
752
+ if (newWrapperWidth < 20) {
753
+ newWrapperWidth = 20;
754
+ newWrapperHeight = newWrapperWidth / (startWidth / (startHeight || 1));
755
+ }
756
+ const image = imageRef.value;
757
+ const host = image == null ? void 0 : image.closest(".milkdown-image-block");
758
+ const hostWidth = host == null ? void 0 : host.getBoundingClientRect().width;
759
+ const maxW = config.maxWidth && hostWidth ? Math.min(config.maxWidth, hostWidth) : hostWidth || Infinity;
760
+ if (newWrapperWidth > maxW) {
761
+ newWrapperWidth = maxW;
762
+ newWrapperHeight = newWrapperWidth / (startWidth / (startHeight || 1));
763
+ }
764
+ if (config.maxHeight && newWrapperHeight > config.maxHeight) {
765
+ newWrapperHeight = config.maxHeight;
766
+ }
767
+ const cHeight = isCropped() && !isCropping.value ? (_a2 = cropHeight.value) != null ? _a2 : 1 : 1;
768
+ const newBaseHeight = newWrapperHeight / cHeight;
769
+ const newRatio = newBaseHeight / (baseDisplayH.value || 1);
770
+ liveRatio.value = Number(newRatio.toFixed(2));
753
771
  };
754
772
  const onResizeHandlePointerUp = () => {
755
773
  window.removeEventListener("pointermove", onResizeHandlePointerMove);
756
774
  window.removeEventListener("pointerup", onResizeHandlePointerUp);
757
- const image = imageRef.value;
758
- if (!image) return;
759
- const originHeight = Number(image.dataset.origin);
760
- const currentHeight = Number(image.dataset.height);
761
- const ratio2 = Number.parseFloat(
762
- Number(currentHeight / originHeight).toFixed(2)
763
- );
764
- if (Number.isNaN(ratio2)) return;
765
- setAttr("ratio", ratio2);
775
+ if (liveRatio.value !== null) {
776
+ setAttr("ratio", liveRatio.value);
777
+ liveRatio.value = null;
778
+ }
766
779
  };
767
780
  const onResizeHandlePointerDown = (e, dir) => {
781
+ var _a2;
768
782
  if (readonly.value) return;
769
783
  e.preventDefault();
770
784
  e.stopPropagation();
771
785
  resizeDir = dir;
772
786
  startY = e.clientY;
773
787
  startX = e.clientX;
774
- const image = imageRef.value;
775
- if (image) {
776
- startHeight = image.getBoundingClientRect().height;
777
- startWidth = image.getBoundingClientRect().width;
788
+ const wrapper = wrapperRef.value;
789
+ if (wrapper) {
790
+ startHeight = wrapper.getBoundingClientRect().height;
791
+ startWidth = wrapper.getBoundingClientRect().width;
778
792
  }
793
+ liveRatio.value = (_a2 = ratio.value) != null ? _a2 : 1;
779
794
  window.addEventListener("pointermove", onResizeHandlePointerMove);
780
795
  window.addEventListener("pointerup", onResizeHandlePointerUp);
781
796
  };
@@ -911,43 +926,63 @@ const ImageViewer = defineComponent({
911
926
  cropDragType = "";
912
927
  };
913
928
  return () => {
914
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j;
929
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
915
930
  const currentBorderStyle = (_a2 = borderStyle.value) != null ? _a2 : "none";
916
- const currentBorderWidth = (_b = borderWidth.value) != null ? _b : 0;
917
- const currentBorderColor = (_c = borderColor.value) != null ? _c : "#000000";
931
+ const currentBorderWidth = localBorderWidth.value !== null ? localBorderWidth.value : (_b = borderWidth.value) != null ? _b : 0;
932
+ const currentBorderColor = localBorderColor.value !== null ? localBorderColor.value : (_c = borderColor.value) != null ? _c : "#000000";
918
933
  const cTop = (_d = cropTop.value) != null ? _d : 0;
919
934
  const cLeft = (_e = cropLeft.value) != null ? _e : 0;
920
935
  const cWidth = (_f = cropWidth.value) != null ? _f : 1;
921
936
  const cHeight = (_g = cropHeight.value) != null ? _g : 1;
922
937
  const cropped = isCropped();
923
938
  const borderStyleStr = currentBorderStyle !== "none" && currentBorderWidth > 0 ? `${currentBorderWidth}px ${currentBorderStyle} ${currentBorderColor}` : void 0;
924
- const dw = displayW.value;
925
- const dh = displayH.value;
926
- const imgEl = imageRef.value;
927
- const actualW = imgEl ? imgEl.getBoundingClientRect().width : dw;
928
- const actualH = imgEl ? imgEl.getBoundingClientRect().height : dh;
929
- const baseW = cropped && !isCropping.value && imgEl ? actualW : dw;
930
- const baseH = cropped && !isCropping.value && imgEl ? actualH : dh;
939
+ const currentRatio = liveRatio.value !== null ? liveRatio.value : (_h = ratio.value) != null ? _h : 1;
940
+ let baseW = baseDisplayW.value * currentRatio;
941
+ let baseH = baseDisplayH.value * currentRatio;
942
+ const image = imageRef.value;
943
+ const host = image == null ? void 0 : image.closest(".milkdown-image-block");
944
+ const hostWidth = host == null ? void 0 : host.getBoundingClientRect().width;
945
+ const maxW = config.maxWidth && hostWidth ? Math.min(config.maxWidth, hostWidth) : hostWidth || Infinity;
946
+ const visibleW = isCropping.value ? baseW : cropped ? baseW * cWidth : baseW;
947
+ if (visibleW > maxW && maxW > 0) {
948
+ const scale = maxW / visibleW;
949
+ baseW = baseW * scale;
950
+ baseH = baseH * scale;
951
+ }
931
952
  const cropPxW = baseW * cWidth;
932
953
  const cropPxH = baseH * cHeight;
933
954
  const cropPxL = baseW * cLeft;
934
955
  const cropPxT = baseH * cTop;
935
- const wrapperStyle = {};
956
+ const wrapperStyle = {
957
+ minWidth: "20px",
958
+ minHeight: "20px",
959
+ border: !isCropping.value ? borderStyleStr != null ? borderStyleStr : "" : ""
960
+ };
961
+ if (cropped && !isCropping.value && cropPxW > 0 && cropPxH > 0) {
962
+ wrapperStyle.width = `${cropPxW}px`;
963
+ wrapperStyle.height = `${cropPxH}px`;
964
+ }
936
965
  const cropClipStyle = cropped && !isCropping.value && baseW > 0 && baseH > 0 ? {
937
966
  width: `${cropPxW}px`,
938
967
  height: `${cropPxH}px`,
939
968
  overflow: "hidden"
940
969
  } : null;
941
- const imgStyle = {};
970
+ const imgStyle = {
971
+ minWidth: "0px",
972
+ minHeight: "0px"
973
+ };
942
974
  if (cropped && !isCropping.value && baseW > 0 && baseH > 0) {
943
975
  imgStyle.width = `${baseW}px`;
944
976
  imgStyle.height = `${baseH}px`;
945
977
  imgStyle.maxWidth = "none";
946
978
  imgStyle.marginLeft = `${-cropPxL}px`;
947
979
  imgStyle.marginTop = `${-cropPxT}px`;
948
- imgStyle.border = borderStyleStr != null ? borderStyleStr : "";
949
980
  } else {
950
- imgStyle.border = borderStyleStr != null ? borderStyleStr : "";
981
+ if (baseH > 0 && baseW > 0) {
982
+ imgStyle.width = `${baseW}px`;
983
+ imgStyle.height = `${baseH}px`;
984
+ if (config.maxWidth) imgStyle.maxWidth = `${config.maxWidth}px`;
985
+ }
951
986
  }
952
987
  return /* @__PURE__ */ h(Fragment$2, null, !isCropping.value ? /* @__PURE__ */ h(
953
988
  "div",
@@ -988,7 +1023,7 @@ const ImageViewer = defineComponent({
988
1023
  resetCrop();
989
1024
  }
990
1025
  },
991
- /* @__PURE__ */ h(Icon, { icon: (_h = config.resetCropIcon) != null ? _h : "\u21A9" })
1026
+ /* @__PURE__ */ h(Icon, { icon: (_i = config.resetCropIcon) != null ? _i : "\u21A9" })
992
1027
  ) : null,
993
1028
  config.borderIcon ? /* @__PURE__ */ h(
994
1029
  "div",
@@ -1003,7 +1038,14 @@ const ImageViewer = defineComponent({
1003
1038
  "div",
1004
1039
  {
1005
1040
  class: "setting-panel border-panel",
1006
- onClick: (e) => e.stopPropagation()
1041
+ draggable: "true",
1042
+ onClick: (e) => e.stopPropagation(),
1043
+ onMousedown: (e) => e.stopPropagation(),
1044
+ onPointerdown: (e) => e.stopPropagation(),
1045
+ onDragstart: (e) => {
1046
+ e.preventDefault();
1047
+ e.stopPropagation();
1048
+ }
1007
1049
  },
1008
1050
  /* @__PURE__ */ h("div", { class: "setting-panel-title" }, "\u8FB9\u6846\u8BBE\u7F6E"),
1009
1051
  /* @__PURE__ */ h("div", { class: "setting-row" }, /* @__PURE__ */ h("span", { class: "setting-label" }, "\u6837\u5F0F"), /* @__PURE__ */ h("div", { class: "border-style-options" }, borderStyleOptions.map((opt) => /* @__PURE__ */ h(
@@ -1020,29 +1062,51 @@ const ImageViewer = defineComponent({
1020
1062
  currentBorderStyle !== "none" ? /* @__PURE__ */ h("div", { class: "setting-row" }, /* @__PURE__ */ h("span", { class: "setting-label" }, "\u5BBD\u5EA6"), /* @__PURE__ */ h(
1021
1063
  "input",
1022
1064
  {
1065
+ draggable: "true",
1023
1066
  type: "range",
1024
1067
  min: "1",
1025
1068
  max: "10",
1026
1069
  value: currentBorderWidth,
1027
1070
  onInput: (e) => {
1071
+ const target = e.target;
1072
+ localBorderWidth.value = Number(target.value);
1073
+ },
1074
+ onChange: (e) => {
1028
1075
  const target = e.target;
1029
1076
  setAttr("borderWidth", Number(target.value));
1077
+ localBorderWidth.value = null;
1030
1078
  },
1031
1079
  onClick: (e) => e.stopPropagation(),
1032
- onPointerdown: (e) => e.stopPropagation()
1080
+ onMousedown: (e) => e.stopPropagation(),
1081
+ onPointerdown: (e) => e.stopPropagation(),
1082
+ onDragstart: (e) => {
1083
+ e.preventDefault();
1084
+ e.stopPropagation();
1085
+ }
1033
1086
  }
1034
1087
  ), /* @__PURE__ */ h("span", { class: "setting-value" }, currentBorderWidth, "px")) : null,
1035
1088
  currentBorderStyle !== "none" ? /* @__PURE__ */ h("div", { class: "setting-row" }, /* @__PURE__ */ h("span", { class: "setting-label" }, "\u989C\u8272"), /* @__PURE__ */ h(
1036
1089
  "input",
1037
1090
  {
1091
+ draggable: "true",
1038
1092
  type: "color",
1039
1093
  value: currentBorderColor,
1040
1094
  onInput: (e) => {
1095
+ const target = e.target;
1096
+ localBorderColor.value = target.value;
1097
+ },
1098
+ onChange: (e) => {
1041
1099
  const target = e.target;
1042
1100
  setAttr("borderColor", target.value);
1101
+ localBorderColor.value = null;
1043
1102
  },
1044
1103
  onClick: (e) => e.stopPropagation(),
1045
- onPointerdown: (e) => e.stopPropagation()
1104
+ onMousedown: (e) => e.stopPropagation(),
1105
+ onPointerdown: (e) => e.stopPropagation(),
1106
+ onDragstart: (e) => {
1107
+ e.preventDefault();
1108
+ e.stopPropagation();
1109
+ }
1046
1110
  }
1047
1111
  )) : null
1048
1112
  ) : null
@@ -1167,7 +1231,7 @@ const ImageViewer = defineComponent({
1167
1231
  applyCrop();
1168
1232
  }
1169
1233
  },
1170
- /* @__PURE__ */ h(Icon, { icon: (_i = config.confirmIcon) != null ? _i : "\u2713" })
1234
+ /* @__PURE__ */ h(Icon, { icon: (_j = config.confirmIcon) != null ? _j : "\u2713" })
1171
1235
  ), /* @__PURE__ */ h(
1172
1236
  "div",
1173
1237
  {
@@ -1177,7 +1241,7 @@ const ImageViewer = defineComponent({
1177
1241
  cancelCrop();
1178
1242
  }
1179
1243
  },
1180
- /* @__PURE__ */ h(Icon, { icon: (_j = config.cancelIcon) != null ? _j : "\u2715" })
1244
+ /* @__PURE__ */ h(Icon, { icon: (_k = config.cancelIcon) != null ? _k : "\u2715" })
1181
1245
  ))) : null,
1182
1246
  !isCropping.value ? /* @__PURE__ */ h(Fragment$2, null, /* @__PURE__ */ h(
1183
1247
  "div",