sard 1.2.4 → 1.2.5

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.
@@ -1,7 +1,7 @@
1
1
  import { type Ref } from 'vue';
2
2
  import { type DefaultProps } from '../config';
3
3
  export interface TextShrinkProps {
4
- /** 要渲染的纯文本,不解析 HTML */
4
+ /** 要渲染的纯文本,不解析 HTML;提供默认插槽时被插槽内容取代 */
5
5
  text?: string;
6
6
  /** 允许渲染的最大行数,1 表示单行不换行 */
7
7
  maxLines?: number;
@@ -15,6 +15,8 @@ export interface TextShrinkProps {
15
15
  }
16
16
  export declare const defaultTextShrinkProps: DefaultProps<TextShrinkProps>;
17
17
  export interface TextShrinkSlots {
18
+ /** 自定义内容,与 text 属性二选一,插槽优先;内容变化时自动重新适配 */
19
+ default?(props: Record<string, never>): any;
18
20
  }
19
21
  export interface TextShrinkEmits {
20
22
  }
@@ -1,8 +1,7 @@
1
1
  import { createBem } from "../../utils/bem.js";
2
2
  import { defaultTextShrinkProps } from "./common.js";
3
- import { computed, createElementBlock, createElementVNode, createTextVNode, defineComponent, mergeDefaults, normalizeClass, normalizeStyle, onBeforeUnmount, onMounted, openBlock, ref, toDisplayString, unref, useTemplateRef, watch } from "vue";
3
+ import { computed, createElementBlock, createElementVNode, createTextVNode, defineComponent, mergeDefaults, normalizeClass, normalizeStyle, onBeforeUnmount, onMounted, openBlock, ref, renderSlot, toDisplayString, unref, useTemplateRef, watch } from "vue";
4
4
  //#region packages/sard/components/text-shrink/text-shrink.vue
5
- const _hoisted_1 = ["textContent"];
6
5
  const MIN_FONT_SIZE = 1;
7
6
  /** 二分搜索收敛精度(px) */
8
7
  const SEARCH_EPSILON = .05;
@@ -16,6 +15,8 @@ const SIZE_TOLERANCE = .05;
16
15
  const WIDTH_TOLERANCE = .5;
17
16
  /** 单行按宽度外推分割点的次数上限,之后回到二分保证收敛 */
18
17
  const INTERPOLATION_LIMIT = 2;
18
+ /** 数行时判定两个行盒在同一行的 top 容差(px) */
19
+ const LINE_TOP_TOLERANCE = .5;
19
20
  /** 首次读到的 computed font-size 不可信时的兜底基准 */
20
21
  const FALLBACK_BASE_FONT_SIZE = 16;
21
22
  /** 复用同一个 Range,避免每次测量都新建对象 */
@@ -87,19 +88,18 @@ var text_shrink_default = /*@__PURE__*/ defineComponent({
87
88
  /**
88
89
  * 以指定字号测量一次:写入字号后只读一次几何。
89
90
  * 单行读 Range 宽度(测量层此时不换行,宽度可以超出容器);
90
- * 多行读 Range 的 rect 个数,一个文本节点每行对应一个 rect。
91
+ * 多行数 Range 的独立行数(行盒按 top 去重,兼容内容含多个行内元素)。
91
92
  * 读写之间会强制一次布局,所以不需要等 Vue 更新。
92
93
  */
93
94
  function probeAt(size, available) {
94
95
  const measure = measureRef.value;
95
- const node = measure?.firstChild;
96
96
  const selection = getRange();
97
- if (!measure || !node || node.nodeType !== 3 || !selection) return {
97
+ if (!measure || !measure.hasChildNodes() || !selection) return {
98
98
  fits: true,
99
99
  width: 0
100
100
  };
101
101
  measure.style.fontSize = `${size}px`;
102
- selection.selectNodeContents(node);
102
+ selection.selectNodeContents(measure);
103
103
  if (singleLine.value) {
104
104
  const { width } = selection.getBoundingClientRect();
105
105
  return {
@@ -108,10 +108,21 @@ var text_shrink_default = /*@__PURE__*/ defineComponent({
108
108
  };
109
109
  }
110
110
  return {
111
- fits: selection.getClientRects().length <= lines.value,
111
+ fits: countLines(selection) <= lines.value,
112
112
  width: 0
113
113
  };
114
114
  }
115
+ /** 数 Range 覆盖的独立行数:同一行的多个行内片段 top 相同,按 top 去重 */
116
+ function countLines(selection) {
117
+ const rects = selection.getClientRects();
118
+ const tops = [];
119
+ for (let index = 0; index < rects.length; index += 1) {
120
+ const rect = rects[index];
121
+ if (rect.width === 0 && rect.height === 0) continue;
122
+ if (!tops.some((top) => Math.abs(top - rect.top) < LINE_TOP_TOLERANCE)) tops.push(rect.top);
123
+ }
124
+ return tops.length;
125
+ }
115
126
  /**
116
127
  * 在 (floorFontSize, baseFontSize] 区间内搜索能完整显示的最大字号。
117
128
  * 单行先用「宽度 ∝ 字号」外推两次把区间夹紧,多行直接二分;
@@ -174,6 +185,7 @@ var text_shrink_default = /*@__PURE__*/ defineComponent({
174
185
  let rafId = 0;
175
186
  let disposed = false;
176
187
  let resizeObserver;
188
+ let mutationObserver;
177
189
  /** 上一次适配时的宽度,用来跳过 ResizeObserver 的重复回调 */
178
190
  let observedWidth = -1;
179
191
  /** 同一帧内的多次触发合并成一次适配 */
@@ -204,6 +216,14 @@ var text_shrink_default = /*@__PURE__*/ defineComponent({
204
216
  });
205
217
  resizeObserver.observe(rootRef.value);
206
218
  }
219
+ if (typeof MutationObserver !== "undefined" && measureRef.value) {
220
+ mutationObserver = new MutationObserver(scheduleFit);
221
+ mutationObserver.observe(measureRef.value, {
222
+ childList: true,
223
+ characterData: true,
224
+ subtree: true
225
+ });
226
+ }
207
227
  if (typeof document !== "undefined" && document.fonts && document.fonts.status !== "loaded") document.fonts.ready.then(() => {
208
228
  if (!disposed) scheduleFit();
209
229
  });
@@ -212,6 +232,8 @@ var text_shrink_default = /*@__PURE__*/ defineComponent({
212
232
  disposed = true;
213
233
  resizeObserver?.disconnect();
214
234
  resizeObserver = void 0;
235
+ mutationObserver?.disconnect();
236
+ mutationObserver = void 0;
215
237
  if (rafId !== 0) {
216
238
  cancelFrame(rafId);
217
239
  rafId = 0;
@@ -242,9 +264,8 @@ var text_shrink_default = /*@__PURE__*/ defineComponent({
242
264
  }, [createElementVNode("div", {
243
265
  ref: "measure",
244
266
  class: normalizeClass(unref(measureClass)),
245
- "aria-hidden": "true",
246
- textContent: toDisplayString(props.text)
247
- }, null, 10, _hoisted_1), createTextVNode(" " + toDisplayString(props.text), 1)], 6)], 2);
267
+ "aria-hidden": "true"
268
+ }, [renderSlot(_ctx.$slots, "default", {}, () => [createTextVNode(toDisplayString(props.text), 1)])], 2), renderSlot(_ctx.$slots, "default", {}, () => [createTextVNode(toDisplayString(props.text), 1)])], 6)], 2);
248
269
  };
249
270
  }
250
271
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sard",
3
- "version": "1.2.4",
3
+ "version": "1.2.5",
4
4
  "description": "sard 是基于 Vue3 的 Web 移动端 UI 组件库",
5
5
  "keywords": [
6
6
  "UI",