svelte-middle-ellipsis 0.1.0 → 0.2.0
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/CHANGELOG.md +12 -1
- package/README.md +22 -6
- package/dist/MiddleEllipsis.svelte +75 -35
- package/dist/cutText.d.ts +16 -3
- package/dist/cutText.js +44 -17
- package/dist/probe.d.ts +33 -0
- package/dist/probe.js +57 -0
- package/dist/split.d.ts +3 -3
- package/dist/split.js +3 -3
- package/dist/types.d.ts +14 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,16 @@ All notable changes to `svelte-middle-ellipsis` are documented here. The format
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
+
## [0.2.0] - 2026-11-30
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- `lines` prop, which lets the text wrap over up to that many lines before it is cut in the middle, such as `lines={2}` for a card title that keeps its date. The ellipsis goes on the last line, right before the end, or right after the head with `head`. With more than one line, the component is block-level, wraps paths and URLs anywhere they have no room, and ignores `ratio`. Defaults to `1`, which keeps the single-line behaviour.
|
|
12
|
+
|
|
13
|
+
### Changed
|
|
14
|
+
|
|
15
|
+
- With a `ratio`, or a `head` with a `ratio` once the ellipsis has settled, each side of the ellipsis now keeps only what fits in its own share of the width. Before, the side before the ellipsis took whatever the other side left over, so a character more after the ellipsis could cost one before it, and the text shifted back and forth while the width grew. Now neither side loses a character as the space widens, for up to a character of unused space on each side.
|
|
16
|
+
|
|
7
17
|
## [0.1.0] - 2026-11-29
|
|
8
18
|
|
|
9
19
|
### Added
|
|
@@ -16,5 +26,6 @@ All notable changes to `svelte-middle-ellipsis` are documented here. The format
|
|
|
16
26
|
- `fileName()` helper, to keep a file's extension, optionally with a few characters before it.
|
|
17
27
|
- `otherwise` option for the helpers that take a delimiter, which sets how text without the delimiter is split, such as `tail("/", { otherwise: tail(6) })`. Without it, that text is truncated at its end.
|
|
18
28
|
|
|
19
|
-
[Unreleased]: https://github.com/finn-vgtl/svelte-middle-ellipsis/compare/v0.
|
|
29
|
+
[Unreleased]: https://github.com/finn-vgtl/svelte-middle-ellipsis/compare/v0.2.0...HEAD
|
|
30
|
+
[0.2.0]: https://github.com/finn-vgtl/svelte-middle-ellipsis/compare/v0.1.0...v0.2.0
|
|
20
31
|
[0.1.0]: https://github.com/finn-vgtl/svelte-middle-ellipsis/releases/tag/v0.1.0
|
package/README.md
CHANGED
|
@@ -30,7 +30,7 @@ Requires Svelte `^5.7.0`.
|
|
|
30
30
|
</div>
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
-
The component is an inline flex box with `max-width: 100%`. Constrain its container, or give it a `max-width` of its own, and it truncates when the text runs out of room. Font, size and colour are inherited.
|
|
33
|
+
The component is an inline flex box with `max-width: 100%`. Constrain its container, or give it a `max-width` of its own, and it truncates when the text runs out of room. Font, size and colour are inherited. To let it wrap over several lines first, see [Multiple lines](#multiple-lines).
|
|
34
34
|
|
|
35
35
|
- **Split** — `split` decides what stays visible: the last six characters by default. See [Split helpers](#split-helpers).
|
|
36
36
|
- **Tooltip** — while the text is truncated, the full text is shown in the native tooltip. Bring your own with a tooltip [attachment](#attachments) that reads `title`, or set `showTitle={false}` and read `bind:truncated`.
|
|
@@ -39,16 +39,17 @@ The component is an inline flex box with `max-width: 100%`. Constrain its contai
|
|
|
39
39
|
- **Characters** — `head` and `tail` count grapheme clusters, so an emoji, a flag or a letter with an accent is never cut in half.
|
|
40
40
|
- **Direction** — `dir="auto"` by default, so right-to-left text keeps its end on the left, and left-to-right text stays left-to-right on a right-to-left page. Only pass a `dir` that matches the text: forcing `dir="rtl"` on Latin text puts its end on the left.
|
|
41
41
|
- **Long ends** — with `tail`, when the end and the ellipsis alone are wider than the space, the start disappears and the end loses its beginning, so the ellipsis stays in front of it: `…ge.svelte`.
|
|
42
|
-
- **SSR** — the server sends the full text, truncated at its end by `text-overflow: ellipsis`. Cutting it in the middle and `truncated` wait for the browser to measure it.
|
|
42
|
+
- **SSR** — the server sends the full text, truncated at its end by `text-overflow: ellipsis`, or on its last line by `line-clamp` with `lines`. Cutting it in the middle and `truncated` wait for the browser to measure it.
|
|
43
43
|
|
|
44
44
|
## Props
|
|
45
45
|
|
|
46
46
|
| Prop | Type | Default | Description |
|
|
47
47
|
| ----------- | --------------- | --------- | ----------------------------------------------------------------------------------- |
|
|
48
|
-
| `text` | `string` | — | The text to display,
|
|
48
|
+
| `text` | `string` | — | The text to display, with whitespace preserved. Required. |
|
|
49
49
|
| `split` | `SplitFunction` | `tail(6)` | What stays visible: the end, a head, or where the ellipsis settles. See below. |
|
|
50
50
|
| `showTitle` | `boolean` | `true` | Shows the full text in the native tooltip while truncated. A `title` you pass wins. |
|
|
51
51
|
| `truncated` | `boolean` | `false` | Bindable, read-only. Whether the text is currently truncated. |
|
|
52
|
+
| `lines` | `number` | `1` | How many lines the text may wrap over before it is cut in the middle. |
|
|
52
53
|
|
|
53
54
|
All other attributes (`class`, `style`, `id`, `data-*`, event handlers) and [attachments](#attachments) are forwarded to the host `<span>`. Prop types are exported as `MiddleEllipsisProps`, `SplitFunction` and `SplitIndices`, and the helpers' options as `HeadOptions` and `TailOptions`.
|
|
54
55
|
|
|
@@ -71,14 +72,14 @@ All other attributes (`class`, `style`, `id`, `data-*`, event handlers) and [att
|
|
|
71
72
|
<!-- "INV-…EXAMPLE-CORP": the prefix up to the first "-", kept as long as it fits -->
|
|
72
73
|
<MiddleEllipsis text={id} split={head("-")} />
|
|
73
74
|
|
|
74
|
-
<!-- "sha256
|
|
75
|
-
<MiddleEllipsis text={hash} split={ratio(
|
|
75
|
+
<!-- "sha256:9f86d08…0f00a08": the ellipsis after the first two thirds, at every width -->
|
|
76
|
+
<MiddleEllipsis text={hash} split={ratio(2 / 3)} />
|
|
76
77
|
```
|
|
77
78
|
|
|
78
79
|
- `tail(count)` keeps the last `count` characters, and `tail(delimiter, options)` everything from the last `delimiter` on, the delimiter included. The middle goes first, then the end, so the ellipsis sits right before the end. `tail(0)` truncates at the end.
|
|
79
80
|
- `fileName(options)` keeps the extension, from the last `.` of the file name. It looks past `/` and `\` in paths, keeps only the last extension of `backup.tar.gz`, and treats `.bashrc` as a name without one.
|
|
80
81
|
- `head(count, options)` keeps the first `count` characters, and `head(delimiter, options)` everything up to and including the first `delimiter`. Everything after the head is cut at its beginning, so the ellipsis sits right after the head. Once nothing else is left, the head is cut at its end, like plain `text-overflow: ellipsis`: `src/…+page.svelte`, `src/…`, `sr…`. Pass `ratio` to have the ellipsis settle at a fraction of the width instead, after which both sides shrink: `head("/", { ratio: 0.5 })` keeps the head until it reaches the middle. Do that when many texts share the head, as paths in one folder do, so they don't all narrow to the same `src/…`.
|
|
81
|
-
- `ratio(fraction)` keeps the ellipsis at `fraction` of the width, from the start of the text, at every width: `0.5` in the middle, `
|
|
82
|
+
- `ratio(fraction)` keeps the ellipsis at `fraction` of the width, from the start of the text, at every width: `0.5` in the middle, `2 / 3` after the first two thirds. Both sides shrink as the space narrows. Clamped to `0`–`1`.
|
|
82
83
|
- `tail` with a delimiter and `fileName` take `before`, characters before the delimiter or extension that stay visible too.
|
|
83
84
|
- With a delimiter, `head`, `tail` and `fileName` take `otherwise`, the split for text without the delimiter or extension, such as `tail("/", { otherwise: tail(6) })`. Without it, that text is truncated at its end.
|
|
84
85
|
- Counts are in user-perceived characters, so an emoji or a flag counts as one.
|
|
@@ -91,6 +92,21 @@ All other attributes (`class`, `style`, `id`, `data-*`, event handlers) and [att
|
|
|
91
92
|
<MiddleEllipsis text={id} split={(text) => ({ head: text.indexOf(":") + 1, ratio: 0.4 })} />
|
|
92
93
|
```
|
|
93
94
|
|
|
95
|
+
## Multiple lines
|
|
96
|
+
|
|
97
|
+
Pass `lines` to let the text wrap over that many lines before it is cut in the middle:
|
|
98
|
+
|
|
99
|
+
```svelte
|
|
100
|
+
<!-- A card title over up to two lines that keeps its date -->
|
|
101
|
+
<MiddleEllipsis text={title} split={tail(10)} lines={2} />
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
- The ellipsis goes on the last line, right before the end, and stays on the end's line. When the end alone needs more than `lines` lines, it loses its beginning, as on one line.
|
|
105
|
+
- With `head`, the ellipsis goes right after the head, and what follows is cut at its beginning.
|
|
106
|
+
- The component is block-level and takes its width from its container. The text wraps at spaces, and anywhere else a line has no room, so paths and URLs wrap too.
|
|
107
|
+
- `ratio`, as a split of its own or as an option of `head`, is ignored.
|
|
108
|
+
- Before the browser measures it, as when rendered on the server, the text is clamped to `lines` lines with `line-clamp`, cut at its end.
|
|
109
|
+
|
|
94
110
|
## Attachments
|
|
95
111
|
|
|
96
112
|
Attachments (`{@attach}`, Svelte 5.29 or later) are forwarded to the host `<span>`, so tooltip, popover and observer attachments work on the component as on any element.
|
|
@@ -16,7 +16,15 @@
|
|
|
16
16
|
</script>
|
|
17
17
|
|
|
18
18
|
<script lang="ts">
|
|
19
|
-
import { type CutText, cutText,
|
|
19
|
+
import { type CutText, cutText, cutToFit } from "./cutText";
|
|
20
|
+
import {
|
|
21
|
+
breaksAfter,
|
|
22
|
+
breaksBefore,
|
|
23
|
+
ellipsisText,
|
|
24
|
+
fillProbe,
|
|
25
|
+
fitsWrapped,
|
|
26
|
+
type Glue,
|
|
27
|
+
} from "./probe";
|
|
20
28
|
import { splitText } from "./split";
|
|
21
29
|
import type { MiddleEllipsisProps } from "./types";
|
|
22
30
|
|
|
@@ -25,12 +33,24 @@
|
|
|
25
33
|
split = defaultSplit,
|
|
26
34
|
showTitle = true,
|
|
27
35
|
truncated = $bindable(false),
|
|
36
|
+
lines: linesProp = 1,
|
|
28
37
|
title,
|
|
29
38
|
class: className,
|
|
30
39
|
...rest
|
|
31
40
|
}: MiddleEllipsisProps = $props();
|
|
32
41
|
|
|
33
42
|
const parts = $derived(splitText(text, split));
|
|
43
|
+
/** The line count as a whole number of at least `1`. */
|
|
44
|
+
const lines = $derived(
|
|
45
|
+
Number.isFinite(linesProp) ? Math.max(1, Math.floor(linesProp)) : 1,
|
|
46
|
+
);
|
|
47
|
+
/**
|
|
48
|
+
* What the ellipsis sticks to. Over several lines, there is no ratio: a head keeps the ellipsis
|
|
49
|
+
* after it, and without one, the ellipsis precedes the end.
|
|
50
|
+
*/
|
|
51
|
+
const glue: Glue = $derived(
|
|
52
|
+
lines === 1 ? "none" : parts.head ? "head" : "end",
|
|
53
|
+
);
|
|
34
54
|
|
|
35
55
|
let textElement = $state<HTMLSpanElement>();
|
|
36
56
|
let probeElement = $state<HTMLSpanElement>();
|
|
@@ -40,29 +60,6 @@
|
|
|
40
60
|
/** Whether the selection includes the text, which then shows as selected as a whole. */
|
|
41
61
|
let selected = $state(false);
|
|
42
62
|
|
|
43
|
-
/**
|
|
44
|
-
* Fills the probe like the drawn text: each part isolated, so text in another direction keeps
|
|
45
|
-
* to its side of the ellipsis.
|
|
46
|
-
*/
|
|
47
|
-
const fillProbe = (probe: HTMLElement, whole: string, value?: CutText) => {
|
|
48
|
-
if (!value) {
|
|
49
|
-
probe.textContent = whole;
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
const isolate = (content: string) => {
|
|
53
|
-
const element = document.createElement("bdi");
|
|
54
|
-
element.textContent = content;
|
|
55
|
-
return element;
|
|
56
|
-
};
|
|
57
|
-
const ellipsis = document.createElement("span");
|
|
58
|
-
ellipsis.textContent = ELLIPSIS;
|
|
59
|
-
probe.replaceChildren(
|
|
60
|
-
isolate(value.before),
|
|
61
|
-
ellipsis,
|
|
62
|
-
isolate(value.after),
|
|
63
|
-
);
|
|
64
|
-
};
|
|
65
|
-
|
|
66
63
|
// The full text keeps its place in the layout, so its width only changes with the available
|
|
67
64
|
// width, which the observer sees, and drawing over it never feeds back into it. A new text or a
|
|
68
65
|
// font that finishes loading changes what fits without resizing it, so both re-measure.
|
|
@@ -71,17 +68,30 @@
|
|
|
71
68
|
const probe = probeElement;
|
|
72
69
|
if (!element || !probe) return;
|
|
73
70
|
const current = parts;
|
|
71
|
+
const currentGlue = glue;
|
|
74
72
|
const whole = current.head + current.start + current.end;
|
|
75
73
|
|
|
76
74
|
const measure = () => {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
75
|
+
let next: CutText | undefined;
|
|
76
|
+
if (currentGlue === "none") {
|
|
77
|
+
probe.style.width = "";
|
|
78
|
+
next = cutText(
|
|
79
|
+
current,
|
|
80
|
+
element.getBoundingClientRect().width,
|
|
81
|
+
(value) => {
|
|
82
|
+
fillProbe(probe, whole, value, currentGlue);
|
|
83
|
+
return probe.getBoundingClientRect().width;
|
|
84
|
+
},
|
|
85
|
+
);
|
|
86
|
+
} else {
|
|
87
|
+
// Wraps at the width of the full text, which fills the space.
|
|
88
|
+
probe.style.width = `${element.getBoundingClientRect().width}px`;
|
|
89
|
+
// A ratio is not supported over several lines and is left out.
|
|
90
|
+
next = cutToFit(current, (value) => {
|
|
91
|
+
fillProbe(probe, whole, value, currentGlue);
|
|
92
|
+
return fitsWrapped(probe);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
85
95
|
// Left in the probe, the last candidate would end up in copied text in WebKit.
|
|
86
96
|
probe.textContent = "";
|
|
87
97
|
cut = next;
|
|
@@ -139,15 +149,22 @@
|
|
|
139
149
|
dir="auto"
|
|
140
150
|
{...rest}
|
|
141
151
|
class={className ? `middle-ellipsis ${className}` : "middle-ellipsis"}
|
|
152
|
+
class:multi-line={lines > 1}
|
|
153
|
+
style:--lines={lines > 1 ? lines : undefined}
|
|
142
154
|
title={title ?? (showTitle && truncated ? text : undefined)}
|
|
143
155
|
><span class="slot" class:cut class:selected
|
|
144
156
|
><span class="text" bind:this={textElement}>{text}</span
|
|
145
157
|
>{#if cut}<span class="drawn" aria-hidden="true"
|
|
146
158
|
><span class="line"
|
|
147
|
-
><bdi class="piece" data-text={cut.before}></bdi
|
|
159
|
+
><bdi class="piece" data-text={cut.before}></bdi
|
|
160
|
+
>{#if breaksBefore(glue, cut)}<wbr />{/if}<span
|
|
148
161
|
class="piece"
|
|
149
|
-
data-text={
|
|
150
|
-
></span
|
|
162
|
+
data-text={ellipsisText(glue)}
|
|
163
|
+
></span
|
|
164
|
+
>{#if breaksAfter(glue, cut)}<wbr />{/if}<bdi
|
|
165
|
+
class="piece"
|
|
166
|
+
data-text={cut.after}
|
|
167
|
+
></bdi></span
|
|
151
168
|
></span
|
|
152
169
|
>{/if}</span
|
|
153
170
|
><span class="probe" aria-hidden="true" bind:this={probeElement}></span></span
|
|
@@ -161,6 +178,13 @@
|
|
|
161
178
|
white-space: pre;
|
|
162
179
|
}
|
|
163
180
|
|
|
181
|
+
/* Over several lines, takes its width from its container and wraps anywhere it must. */
|
|
182
|
+
.multi-line {
|
|
183
|
+
display: block;
|
|
184
|
+
white-space: pre-wrap;
|
|
185
|
+
overflow-wrap: anywhere;
|
|
186
|
+
}
|
|
187
|
+
|
|
164
188
|
/*
|
|
165
189
|
Holds the full text and the drawn text over it in one grid cell, so both are laid out alike
|
|
166
190
|
and share their baseline to the fraction of a pixel.
|
|
@@ -184,6 +208,22 @@
|
|
|
184
208
|
user-select: all;
|
|
185
209
|
}
|
|
186
210
|
|
|
211
|
+
/*
|
|
212
|
+
Clamped to the line count, which also cuts the text at its end before the browser measures
|
|
213
|
+
it. The drawn text is clamped alike, so a cut left over from a wider space, which wraps over
|
|
214
|
+
more lines until the next measure, doesn't grow the box for a frame. The probe too, so a line
|
|
215
|
+
the clamp hides shows it doesn't fit.
|
|
216
|
+
*/
|
|
217
|
+
.multi-line .text,
|
|
218
|
+
.multi-line .drawn,
|
|
219
|
+
.multi-line > .probe {
|
|
220
|
+
display: -webkit-box;
|
|
221
|
+
-webkit-box-orient: vertical;
|
|
222
|
+
-webkit-line-clamp: var(--lines);
|
|
223
|
+
line-clamp: var(--lines);
|
|
224
|
+
overflow: hidden;
|
|
225
|
+
}
|
|
226
|
+
|
|
187
227
|
.cut > .text {
|
|
188
228
|
text-overflow: clip;
|
|
189
229
|
color: transparent;
|
package/dist/cutText.d.ts
CHANGED
|
@@ -5,12 +5,25 @@ interface CutText {
|
|
|
5
5
|
before: string;
|
|
6
6
|
after: string;
|
|
7
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* Cuts a text so that `fits` accepts it, or returns `undefined` when all of it fits. Without
|
|
10
|
+
* `cut`, `fits` checks the whole text. Needs no widths, so it works for any layout, such as text
|
|
11
|
+
* wrapped over several lines, as long as a text that fits still fits with a character less. Has no
|
|
12
|
+
* ratio: with a head, the ellipsis follows the head, and without one, it precedes the end.
|
|
13
|
+
*
|
|
14
|
+
* Without a head, the start is cut at its end, so the ellipsis sits right before the end. Once the
|
|
15
|
+
* start is gone, the end is cut at its beginning, so the ellipsis stays where it was. When not even
|
|
16
|
+
* the ellipsis fits, it is drawn alone and clipped.
|
|
17
|
+
*
|
|
18
|
+
* With a head, everything after the head is cut at its beginning, so the ellipsis sits right after
|
|
19
|
+
* the head. Once nothing follows it, the head is cut at its end.
|
|
20
|
+
*/
|
|
21
|
+
declare function cutToFit({ head, start, end }: Pick<SplitText, "head" | "start" | "end">, fits: (cut?: CutText) => boolean): CutText | undefined;
|
|
8
22
|
/**
|
|
9
23
|
* Cuts a text to fit in `available` pixels, or returns `undefined` when all of it fits. When not
|
|
10
24
|
* even the ellipsis fits, it is drawn alone and clipped.
|
|
11
25
|
*
|
|
12
|
-
* Without a ratio, the
|
|
13
|
-
* start is gone, the end is cut at its beginning, so the ellipsis stays where it was.
|
|
26
|
+
* Without a ratio, the text is cut as `cutToFit` does.
|
|
14
27
|
*
|
|
15
28
|
* With a head, the head stays whole and everything after it is cut at its beginning, so the
|
|
16
29
|
* ellipsis sits right after the head. Once the ellipsis reaches `ratio` of the width, both sides
|
|
@@ -22,4 +35,4 @@ interface CutText {
|
|
|
22
35
|
*/
|
|
23
36
|
declare function cutText({ head, start, end, ratio }: SplitText, available: number, measure: (cut?: CutText) => number): CutText | undefined;
|
|
24
37
|
export type { CutText };
|
|
25
|
-
export { cutText, ELLIPSIS };
|
|
38
|
+
export { cutText, cutToFit, ELLIPSIS };
|
package/dist/cutText.js
CHANGED
|
@@ -47,12 +47,42 @@ function longestSuffix(text, fits, whole = false) {
|
|
|
47
47
|
}
|
|
48
48
|
return best;
|
|
49
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Cuts a text so that `fits` accepts it, or returns `undefined` when all of it fits. Without
|
|
52
|
+
* `cut`, `fits` checks the whole text. Needs no widths, so it works for any layout, such as text
|
|
53
|
+
* wrapped over several lines, as long as a text that fits still fits with a character less. Has no
|
|
54
|
+
* ratio: with a head, the ellipsis follows the head, and without one, it precedes the end.
|
|
55
|
+
*
|
|
56
|
+
* Without a head, the start is cut at its end, so the ellipsis sits right before the end. Once the
|
|
57
|
+
* start is gone, the end is cut at its beginning, so the ellipsis stays where it was. When not even
|
|
58
|
+
* the ellipsis fits, it is drawn alone and clipped.
|
|
59
|
+
*
|
|
60
|
+
* With a head, everything after the head is cut at its beginning, so the ellipsis sits right after
|
|
61
|
+
* the head. Once nothing follows it, the head is cut at its end.
|
|
62
|
+
*/
|
|
63
|
+
function cutToFit({ head, start, end }, fits) {
|
|
64
|
+
if (fits())
|
|
65
|
+
return undefined;
|
|
66
|
+
if (head) {
|
|
67
|
+
if (fits({ before: head, after: "" })) {
|
|
68
|
+
const after = longestSuffix(start, (suffix) => fits({ before: head, after: suffix })) ?? "";
|
|
69
|
+
return { before: head, after };
|
|
70
|
+
}
|
|
71
|
+
const before = longestPrefix(head, (prefix) => fits({ before: prefix, after: "" }), true) ?? "";
|
|
72
|
+
return { before, after: "" };
|
|
73
|
+
}
|
|
74
|
+
const before = longestPrefix(start, (prefix) => fits({ before: prefix, after: end }));
|
|
75
|
+
if (before !== undefined)
|
|
76
|
+
return { before, after: end };
|
|
77
|
+
// The ellipsis stays in front of the end, which loses its beginning instead.
|
|
78
|
+
const after = longestSuffix(end, (suffix) => fits({ before: "", after: suffix }));
|
|
79
|
+
return { before: "", after: after ?? "" };
|
|
80
|
+
}
|
|
50
81
|
/**
|
|
51
82
|
* Cuts a text to fit in `available` pixels, or returns `undefined` when all of it fits. When not
|
|
52
83
|
* even the ellipsis fits, it is drawn alone and clipped.
|
|
53
84
|
*
|
|
54
|
-
* Without a ratio, the
|
|
55
|
-
* start is gone, the end is cut at its beginning, so the ellipsis stays where it was.
|
|
85
|
+
* Without a ratio, the text is cut as `cutToFit` does.
|
|
56
86
|
*
|
|
57
87
|
* With a head, the head stays whole and everything after it is cut at its beginning, so the
|
|
58
88
|
* ellipsis sits right after the head. Once the ellipsis reaches `ratio` of the width, both sides
|
|
@@ -63,18 +93,12 @@ function longestSuffix(text, fits, whole = false) {
|
|
|
63
93
|
* and mixed directions count as they look. Without `cut`, it measures the whole text.
|
|
64
94
|
*/
|
|
65
95
|
function cutText({ head, start, end, ratio }, available, measure) {
|
|
66
|
-
if (measure() <= available + 0.01)
|
|
67
|
-
return undefined;
|
|
68
96
|
const fits = (cut) => measure(cut) <= available + 0.01;
|
|
97
|
+
if (ratio === undefined)
|
|
98
|
+
return cutToFit({ head: "", start, end }, fits);
|
|
99
|
+
if (fits())
|
|
100
|
+
return undefined;
|
|
69
101
|
const nothing = { before: "", after: "" };
|
|
70
|
-
if (ratio === undefined) {
|
|
71
|
-
const before = longestPrefix(start, (prefix) => fits({ before: prefix, after: end }));
|
|
72
|
-
if (before !== undefined)
|
|
73
|
-
return { before, after: end };
|
|
74
|
-
// The ellipsis stays in front of the end, which loses its beginning instead.
|
|
75
|
-
const after = longestSuffix(end, (suffix) => fits({ before: "", after: suffix }));
|
|
76
|
-
return after === undefined ? nothing : { before: "", after };
|
|
77
|
-
}
|
|
78
102
|
const ellipsis = measure(nothing);
|
|
79
103
|
if (ellipsis > available + 0.01)
|
|
80
104
|
return nothing;
|
|
@@ -88,14 +112,17 @@ function cutText({ head, start, end, ratio }, available, measure) {
|
|
|
88
112
|
if (after !== undefined)
|
|
89
113
|
return { before: head, after };
|
|
90
114
|
}
|
|
91
|
-
// From then on,
|
|
92
|
-
//
|
|
115
|
+
// From then on, each side keeps what fits in its own share of the space, so the ellipsis stays
|
|
116
|
+
// at the ratio to within a character. Neither side takes what the other leaves over: that would
|
|
117
|
+
// tie them together, and a character more on one side would cost the other one, so the text
|
|
118
|
+
// would shift back and forth as the space widens. Each side only grows instead, for up to a
|
|
119
|
+
// character of space left over on each.
|
|
93
120
|
const after = longestSuffix(start, (suffix) => widthOf({ before: "", after: suffix }) <= space - leading + 0.01, true) ?? "";
|
|
94
121
|
// Without a head, both sides come from the same text, so the side before stops where the
|
|
95
122
|
// side after begins.
|
|
96
123
|
const source = head || start.slice(0, start.length - after.length);
|
|
97
|
-
const before = longestPrefix(source, (prefix) =>
|
|
98
|
-
"";
|
|
124
|
+
const before = longestPrefix(source, (prefix) => widthOf({ before: prefix, after: "" }) <= leading + 0.01 &&
|
|
125
|
+
fits({ before: prefix, after }), true) ?? "";
|
|
99
126
|
return { before, after };
|
|
100
127
|
}
|
|
101
|
-
export { cutText, ELLIPSIS };
|
|
128
|
+
export { cutText, cutToFit, ELLIPSIS };
|
package/dist/probe.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type CutText } from "./cutText";
|
|
2
|
+
/**
|
|
3
|
+
* What the ellipsis sticks to when the text wraps: the end after it, the head before it, or nothing
|
|
4
|
+
* on a single line. A word joiner keeps the ellipsis on that side's line, and a `<wbr>` on its
|
|
5
|
+
* other side lets the text break there instead. Without both, the last word before the ellipsis
|
|
6
|
+
* and the end wrap as one block, so one more character can push the block to the next line while a
|
|
7
|
+
* few more let it fit again, and the search for the longest cut that fits misses it.
|
|
8
|
+
*/
|
|
9
|
+
type Glue = "none" | "end" | "head";
|
|
10
|
+
/**
|
|
11
|
+
* Whether the text may break between the part before the ellipsis and the ellipsis glued to the
|
|
12
|
+
* end. Not with nothing before it: the break would leave an empty line, and the ellipsis and the
|
|
13
|
+
* end would wrap over the lines after it, so a shorter end could need more lines than a longer
|
|
14
|
+
* one, depending on where the browser breaks it.
|
|
15
|
+
*/
|
|
16
|
+
declare function breaksBefore(glue: Glue, cut: CutText): boolean;
|
|
17
|
+
/** Like `breaksBefore`, between the ellipsis glued to the head and the part after it. */
|
|
18
|
+
declare function breaksAfter(glue: Glue, cut: CutText): boolean;
|
|
19
|
+
/** The ellipsis as drawn, with the word joiner on the side it sticks to. */
|
|
20
|
+
declare function ellipsisText(glue: Glue): string;
|
|
21
|
+
/**
|
|
22
|
+
* Fills the probe like the drawn text: each part isolated, so text in another direction keeps to
|
|
23
|
+
* its side of the ellipsis, and the ellipsis glued as `glue` says. Without `cut`, the whole text.
|
|
24
|
+
*/
|
|
25
|
+
declare function fillProbe(probe: HTMLElement, whole: string, cut: CutText | undefined, glue: Glue): void;
|
|
26
|
+
/**
|
|
27
|
+
* Whether the text in a probe that wraps it and clamps it to its line count fits: no line is
|
|
28
|
+
* hidden by the clamp and nothing sticks out at the side. Counting the lines through the clamp
|
|
29
|
+
* holds up where a fallback font, such as for emoji, makes some lines taller than others.
|
|
30
|
+
*/
|
|
31
|
+
declare function fitsWrapped(probe: HTMLElement): boolean;
|
|
32
|
+
export type { Glue };
|
|
33
|
+
export { breaksAfter, breaksBefore, ellipsisText, fillProbe, fitsWrapped };
|
package/dist/probe.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { ELLIPSIS } from "./cutText";
|
|
2
|
+
const WORD_JOINER = "";
|
|
3
|
+
/**
|
|
4
|
+
* Whether the text may break between the part before the ellipsis and the ellipsis glued to the
|
|
5
|
+
* end. Not with nothing before it: the break would leave an empty line, and the ellipsis and the
|
|
6
|
+
* end would wrap over the lines after it, so a shorter end could need more lines than a longer
|
|
7
|
+
* one, depending on where the browser breaks it.
|
|
8
|
+
*/
|
|
9
|
+
function breaksBefore(glue, cut) {
|
|
10
|
+
return glue === "end" && cut.before !== "";
|
|
11
|
+
}
|
|
12
|
+
/** Like `breaksBefore`, between the ellipsis glued to the head and the part after it. */
|
|
13
|
+
function breaksAfter(glue, cut) {
|
|
14
|
+
return glue === "head" && cut.after !== "";
|
|
15
|
+
}
|
|
16
|
+
/** The ellipsis as drawn, with the word joiner on the side it sticks to. */
|
|
17
|
+
function ellipsisText(glue) {
|
|
18
|
+
if (glue === "end")
|
|
19
|
+
return ELLIPSIS + WORD_JOINER;
|
|
20
|
+
if (glue === "head")
|
|
21
|
+
return WORD_JOINER + ELLIPSIS;
|
|
22
|
+
return ELLIPSIS;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Fills the probe like the drawn text: each part isolated, so text in another direction keeps to
|
|
26
|
+
* its side of the ellipsis, and the ellipsis glued as `glue` says. Without `cut`, the whole text.
|
|
27
|
+
*/
|
|
28
|
+
function fillProbe(probe, whole, cut, glue) {
|
|
29
|
+
if (!cut) {
|
|
30
|
+
probe.textContent = whole;
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const isolate = (content) => {
|
|
34
|
+
const element = document.createElement("bdi");
|
|
35
|
+
element.textContent = content;
|
|
36
|
+
return element;
|
|
37
|
+
};
|
|
38
|
+
const ellipsis = document.createElement("span");
|
|
39
|
+
ellipsis.textContent = ellipsisText(glue);
|
|
40
|
+
const pieces = [isolate(cut.before), ellipsis, isolate(cut.after)];
|
|
41
|
+
if (breaksBefore(glue, cut))
|
|
42
|
+
pieces.splice(1, 0, document.createElement("wbr"));
|
|
43
|
+
if (breaksAfter(glue, cut))
|
|
44
|
+
pieces.splice(2, 0, document.createElement("wbr"));
|
|
45
|
+
probe.replaceChildren(...pieces);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Whether the text in a probe that wraps it and clamps it to its line count fits: no line is
|
|
49
|
+
* hidden by the clamp and nothing sticks out at the side. Counting the lines through the clamp
|
|
50
|
+
* holds up where a fallback font, such as for emoji, makes some lines taller than others.
|
|
51
|
+
*/
|
|
52
|
+
function fitsWrapped(probe) {
|
|
53
|
+
// Both are whole pixels. A hidden line adds a whole line's height, so a pixel is to spare.
|
|
54
|
+
return (probe.scrollHeight <= probe.clientHeight + 1 &&
|
|
55
|
+
probe.scrollWidth <= probe.clientWidth + 1);
|
|
56
|
+
}
|
|
57
|
+
export { breaksAfter, breaksBefore, ellipsisText, fillProbe, fitsWrapped };
|
package/dist/split.d.ts
CHANGED
|
@@ -71,11 +71,11 @@ declare function tail(count: number): SplitFunction;
|
|
|
71
71
|
declare function tail(delimiter: string, options?: TailOptions): SplitFunction;
|
|
72
72
|
/**
|
|
73
73
|
* Keeps the ellipsis at `fraction` of the width, from the start of the text, at every width: `0.5`
|
|
74
|
-
* in the middle, `
|
|
75
|
-
* `0`–`1`.
|
|
74
|
+
* in the middle, `2 / 3` after the first two thirds. Both sides shrink as the space narrows.
|
|
75
|
+
* Clamped to `0`–`1`.
|
|
76
76
|
*
|
|
77
77
|
* ```svelte
|
|
78
|
-
* <MiddleEllipsis text={hash} split={ratio(
|
|
78
|
+
* <MiddleEllipsis text={hash} split={ratio(2 / 3)} />
|
|
79
79
|
* ```
|
|
80
80
|
*/
|
|
81
81
|
declare function ratio(fraction: number): SplitFunction;
|
package/dist/split.js
CHANGED
|
@@ -128,11 +128,11 @@ function tail(keep, { before = 0, otherwise = none } = {}) {
|
|
|
128
128
|
}
|
|
129
129
|
/**
|
|
130
130
|
* Keeps the ellipsis at `fraction` of the width, from the start of the text, at every width: `0.5`
|
|
131
|
-
* in the middle, `
|
|
132
|
-
* `0`–`1`.
|
|
131
|
+
* in the middle, `2 / 3` after the first two thirds. Both sides shrink as the space narrows.
|
|
132
|
+
* Clamped to `0`–`1`.
|
|
133
133
|
*
|
|
134
134
|
* ```svelte
|
|
135
|
-
* <MiddleEllipsis text={hash} split={ratio(
|
|
135
|
+
* <MiddleEllipsis text={hash} split={ratio(2 / 3)} />
|
|
136
136
|
* ```
|
|
137
137
|
*/
|
|
138
138
|
function ratio(fraction) {
|
package/dist/types.d.ts
CHANGED
|
@@ -26,7 +26,10 @@ interface SplitIndices {
|
|
|
26
26
|
/** Decides where to split `text`. A number is the offset where the end begins. */
|
|
27
27
|
type SplitFunction = (text: string) => number | SplitIndices;
|
|
28
28
|
interface MiddleEllipsisProps extends Omit<HTMLAttributes<HTMLSpanElement>, "children"> {
|
|
29
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* The text to display, with whitespace preserved. Rendered on a single line, or wrapped over up
|
|
31
|
+
* to `lines` lines.
|
|
32
|
+
*/
|
|
30
33
|
text: string;
|
|
31
34
|
/**
|
|
32
35
|
* Decides what stays visible, from the text itself: the end, a head at the beginning, or where
|
|
@@ -51,5 +54,15 @@ interface MiddleEllipsisProps extends Omit<HTMLAttributes<HTMLSpanElement>, "chi
|
|
|
51
54
|
* @default false
|
|
52
55
|
*/
|
|
53
56
|
truncated?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* How many lines the text may wrap over before it is cut in the middle. The ellipsis goes on
|
|
59
|
+
* the last line, right before the end, or right after the head with a `head` split. With more
|
|
60
|
+
* than one line, the component is block-level and takes its width from its container, the text
|
|
61
|
+
* wraps at spaces, and anywhere else a line has no room, and a `ratio` is ignored. A value
|
|
62
|
+
* that isn't a whole number of at least `1` is rounded down, or counts as `1`.
|
|
63
|
+
*
|
|
64
|
+
* @default 1
|
|
65
|
+
*/
|
|
66
|
+
lines?: number;
|
|
54
67
|
}
|
|
55
68
|
export type { MiddleEllipsisProps, SplitFunction, SplitIndices };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svelte-middle-ellipsis",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Middle ellipsis for Svelte 5: truncates text in the middle with CSS, keeping the end visible and the ellipsis right up against it.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Finn Voigtländer",
|