svelte-highlight 7.23.2 → 7.24.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/AnsiOutput.svelte +33 -6
- package/AnsiOutput.svelte.d.ts +8 -0
- package/CodeToolbar.svelte +144 -0
- package/CodeToolbar.svelte.d.ts +125 -0
- package/CodeWindow.svelte +19 -0
- package/CodeWindow.svelte.d.ts +10 -0
- package/CopyButton.svelte +107 -3
- package/CopyButton.svelte.d.ts +17 -1
- package/DiffFileList.svelte +523 -0
- package/DiffFileList.svelte.d.ts +94 -0
- package/DiffFileListItem.svelte +44 -0
- package/DiffMinimap.svelte +99 -0
- package/DiffMinimap.svelte.d.ts +17 -0
- package/DiffStats.svelte +43 -0
- package/DiffStats.svelte.d.ts +17 -0
- package/DiffView.svelte +1213 -0
- package/DiffView.svelte.d.ts +76 -0
- package/FileTabs.svelte +2 -5
- package/Highlight.svelte +158 -2
- package/Highlight.svelte.d.ts +50 -2
- package/HighlightAuto.svelte.d.ts +3 -2
- package/HighlightAutoLazy.svelte +112 -0
- package/HighlightAutoLazy.svelte.d.ts +66 -0
- package/HighlightDiff.svelte +162 -0
- package/HighlightDiff.svelte.d.ts +102 -0
- package/HighlightEditable.svelte +65 -756
- package/HighlightEditable.svelte.d.ts +15 -5
- package/HighlightStream.svelte +117 -218
- package/HighlightStream.svelte.d.ts +8 -0
- package/HighlightSvelte.svelte.d.ts +3 -2
- package/HighlightVirtual.svelte +41 -8
- package/HighlightVirtual.svelte.d.ts +8 -0
- package/LangTag.svelte +33 -4
- package/LangTag.svelte.d.ts +6 -2
- package/LineNumbers.svelte +25 -4
- package/LineNumbers.svelte.d.ts +8 -0
- package/MarkdownStream.svelte +1 -0
- package/README.md +735 -18
- package/ToolbarButton.svelte +90 -0
- package/ToolbarButton.svelte.d.ts +106 -0
- package/Typewriter.svelte +59 -10
- package/Typewriter.svelte.d.ts +7 -0
- package/WrapToggle.svelte +34 -0
- package/WrapToggle.svelte.d.ts +39 -0
- package/action.js +7 -3
- package/ansi.d.ts +1 -3
- package/auto-detect.d.ts +52 -0
- package/auto-detect.js +208 -0
- package/code-region.d.ts +7 -0
- package/code-region.js +20 -0
- package/css-highlights.js +115 -0
- package/css-walk.js +50 -0
- package/diff-controller.d.ts +120 -0
- package/diff-controller.js +721 -0
- package/diff-edits.d.ts +101 -0
- package/diff-edits.js +890 -0
- package/diff-html.d.ts +8 -0
- package/diff-html.js +60 -0
- package/diff-languages.d.ts +4 -0
- package/diff-languages.js +73 -0
- package/diff.d.ts +161 -0
- package/diff.js +1111 -0
- package/editable-commands.js +81 -0
- package/editable-css-paint.js +5 -3
- package/editable-dom-paint.js +1 -1
- package/editable-history.js +199 -0
- package/editable-line-dom.js +176 -1
- package/editable-painter.js +356 -0
- package/editable.d.ts +118 -0
- package/editable.js +513 -0
- package/engine.d.ts +1 -1
- package/fence.js +1 -1
- package/highlight-cascade.js +679 -0
- package/highlight-theme.js +6 -47
- package/incremental-tokenize.js +1 -1
- package/index.d.ts +39 -6
- package/index.js +30 -0
- package/languages/detect-index.d.ts +16 -0
- package/languages/detect-index.js +6 -0
- package/languages/loaders.d.ts +8 -0
- package/languages/loaders.js +370 -0
- package/package.json +50 -2
- package/preprocessor.js +20 -5
- package/search.js +2 -2
- package/stream-highlighter.js +198 -0
- package/stream.css +16 -0
- package/stream.d.ts +60 -0
- package/stream.js +283 -0
- package/style-registry.js +34 -3
- package/themes/_base-css.d.ts +1 -0
- package/themes/_base-css.js +1 -0
- package/typewriter-units.d.ts +3 -0
- package/typewriter-units.js +43 -0
- package/virtual-window.js +9 -4
package/AnsiOutput.svelte
CHANGED
|
@@ -14,22 +14,49 @@
|
|
|
14
14
|
*/
|
|
15
15
|
export let wrap = false;
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Accessible name of the scrollable output region. Inside a CodeWindow,
|
|
19
|
+
* the window's `title` or `label` is the default.
|
|
20
|
+
* @type {string | undefined}
|
|
21
|
+
*/
|
|
22
|
+
export let label = undefined;
|
|
23
|
+
|
|
24
|
+
import { getContext } from "svelte";
|
|
17
25
|
import { createAnsiOutput } from "./ansi-output.js";
|
|
26
|
+
import { CODE_REGION, NO_REGION_LABEL } from "./code-region.js";
|
|
27
|
+
|
|
28
|
+
/** @type {import("svelte/store").Readable<string | undefined>} */
|
|
29
|
+
const windowLabel = getContext(CODE_REGION) ?? NO_REGION_LABEL;
|
|
18
30
|
|
|
19
31
|
const output = createAnsiOutput();
|
|
20
32
|
// Same array each time; legacy-mode reactivity still treats it as changed.
|
|
21
33
|
$: segments = output.update(text, autoContrast);
|
|
22
34
|
</script>
|
|
23
35
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
36
|
+
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
|
|
37
|
+
<pre
|
|
38
|
+
class="ansi"
|
|
39
|
+
class:wrap
|
|
40
|
+
tabindex="0"
|
|
41
|
+
role="region"
|
|
42
|
+
aria-label={label ?? $windowLabel ?? "Terminal output"}
|
|
43
|
+
{...$$restProps}><code
|
|
44
|
+
>{#each segments as segment}{#if segment.link}<a
|
|
45
|
+
href={segment.link}
|
|
46
|
+
rel="noopener noreferrer"
|
|
47
|
+
class={segment.class}
|
|
48
|
+
style={segment.style}>{segment.text}</a
|
|
49
|
+
>{:else}<span class={segment.class} style={segment.style}
|
|
50
|
+
>{segment.text}</span
|
|
51
|
+
>{/if}{/each}</code
|
|
30
52
|
></pre>
|
|
31
53
|
|
|
32
54
|
<style>
|
|
55
|
+
/* Draw the ring inside, or a clipping parent hides it in Safari. */
|
|
56
|
+
.ansi:focus-visible {
|
|
57
|
+
outline-offset: -2px;
|
|
58
|
+
}
|
|
59
|
+
|
|
33
60
|
.ansi {
|
|
34
61
|
margin: 0;
|
|
35
62
|
overflow: auto;
|
package/AnsiOutput.svelte.d.ts
CHANGED
|
@@ -11,6 +11,14 @@ export type AnsiOutputProps = HTMLAttributes<HTMLPreElement> & {
|
|
|
11
11
|
/** Wrap long lines instead of horizontally scrolling. @default false */
|
|
12
12
|
wrap?: boolean;
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Accessible name of the scrollable output region. The region is
|
|
16
|
+
* keyboard-focusable so keyboard users can scroll it. Inside a
|
|
17
|
+
* `CodeWindow`, the window's `title` or `label` is the default.
|
|
18
|
+
* @default "Terminal output"
|
|
19
|
+
*/
|
|
20
|
+
label?: string;
|
|
21
|
+
|
|
14
22
|
/** Output block padding. @default "1em" */
|
|
15
23
|
"--ansi-padding"?: string;
|
|
16
24
|
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
/** @type {string | undefined} */
|
|
3
|
+
export let languageName = undefined;
|
|
4
|
+
|
|
5
|
+
/** @type {string} */
|
|
6
|
+
export let title = "";
|
|
7
|
+
|
|
8
|
+
/** @type {boolean} */
|
|
9
|
+
export let sticky = true;
|
|
10
|
+
|
|
11
|
+
/** @type {string | undefined} */
|
|
12
|
+
export let label = undefined;
|
|
13
|
+
|
|
14
|
+
import { setContext } from "svelte";
|
|
15
|
+
|
|
16
|
+
// CopyButton and WrapToggle read this to switch to the toolbar's
|
|
17
|
+
// inline icon-button style.
|
|
18
|
+
setContext("svelte-highlight:code-toolbar", true);
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
<div
|
|
22
|
+
role="toolbar"
|
|
23
|
+
aria-label={label}
|
|
24
|
+
{...$$restProps}
|
|
25
|
+
class={["shl-toolbar hljs", $$restProps.class].filter(Boolean).join(" ")}
|
|
26
|
+
class:shl-toolbar-sticky={sticky}
|
|
27
|
+
>
|
|
28
|
+
<div class="shl-toolbar-start">
|
|
29
|
+
{#if title}
|
|
30
|
+
<span class="shl-toolbar-title" {title}>{title}</span>
|
|
31
|
+
{/if}
|
|
32
|
+
{#if languageName}
|
|
33
|
+
<span class="shl-toolbar-badge" data-language={languageName}
|
|
34
|
+
>{languageName}</span
|
|
35
|
+
>
|
|
36
|
+
{/if}
|
|
37
|
+
<slot name="start" />
|
|
38
|
+
</div>
|
|
39
|
+
<div class="shl-toolbar-end"><slot /></div>
|
|
40
|
+
</div>
|
|
41
|
+
|
|
42
|
+
<style>
|
|
43
|
+
/* The root carries the `hljs` class, so the theme's `.hljs` rule gives
|
|
44
|
+
the bar the same background and text color as the code. This rule
|
|
45
|
+
sets neither, so the theme always wins. */
|
|
46
|
+
.shl-toolbar {
|
|
47
|
+
position: relative;
|
|
48
|
+
isolation: isolate;
|
|
49
|
+
display: flex;
|
|
50
|
+
align-items: center;
|
|
51
|
+
justify-content: space-between;
|
|
52
|
+
gap: var(--toolbar-gap, 0.75em);
|
|
53
|
+
min-height: var(--toolbar-height, 2.75em);
|
|
54
|
+
padding: var(--toolbar-padding, 0.25em 0.375em 0.25em 1em);
|
|
55
|
+
border-bottom: var(
|
|
56
|
+
--toolbar-border,
|
|
57
|
+
1px solid color-mix(in srgb, currentColor 14%, transparent)
|
|
58
|
+
);
|
|
59
|
+
font-family: var(
|
|
60
|
+
--toolbar-font-family,
|
|
61
|
+
system-ui,
|
|
62
|
+
-apple-system,
|
|
63
|
+
"Segoe UI",
|
|
64
|
+
Roboto,
|
|
65
|
+
sans-serif
|
|
66
|
+
);
|
|
67
|
+
font-size: var(--toolbar-font-size, 0.8125em);
|
|
68
|
+
font-weight: 400;
|
|
69
|
+
line-height: 1.25;
|
|
70
|
+
overflow: visible;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/* A faint wash of the text color over the theme background sets the bar
|
|
74
|
+
apart from the code on light and dark themes. */
|
|
75
|
+
.shl-toolbar::before {
|
|
76
|
+
content: "";
|
|
77
|
+
position: absolute;
|
|
78
|
+
inset: 0;
|
|
79
|
+
z-index: -1;
|
|
80
|
+
pointer-events: none;
|
|
81
|
+
background: var(
|
|
82
|
+
--toolbar-background,
|
|
83
|
+
color-mix(in srgb, currentColor 5%, transparent)
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/* Used only when no theme styles the bar. Zero specificity, so any
|
|
88
|
+
theme's `.hljs` rule overrides it. A pinned bar must be opaque. */
|
|
89
|
+
:global(:where(.shl-toolbar)) {
|
|
90
|
+
background-color: Canvas;
|
|
91
|
+
color: CanvasText;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
.shl-toolbar-sticky {
|
|
95
|
+
position: sticky;
|
|
96
|
+
top: 0;
|
|
97
|
+
z-index: var(--toolbar-z-index, 3);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
.shl-toolbar-start,
|
|
101
|
+
.shl-toolbar-end {
|
|
102
|
+
display: flex;
|
|
103
|
+
align-items: center;
|
|
104
|
+
min-width: 0;
|
|
105
|
+
color: var(--toolbar-color, inherit);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
.shl-toolbar-start {
|
|
109
|
+
gap: var(--toolbar-gap, 0.75em);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
.shl-toolbar-end {
|
|
113
|
+
flex-shrink: 0;
|
|
114
|
+
gap: 0.25em;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
.shl-toolbar-title {
|
|
118
|
+
overflow: hidden;
|
|
119
|
+
text-overflow: ellipsis;
|
|
120
|
+
white-space: nowrap;
|
|
121
|
+
font-weight: 500;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
.shl-toolbar-badge {
|
|
125
|
+
flex-shrink: 0;
|
|
126
|
+
padding: var(--langtag-padding, 0.375em 0.625em);
|
|
127
|
+
border-radius: var(--langtag-border-radius, 999px);
|
|
128
|
+
background: var(
|
|
129
|
+
--langtag-background,
|
|
130
|
+
color-mix(in srgb, currentColor 10%, transparent)
|
|
131
|
+
);
|
|
132
|
+
color: var(
|
|
133
|
+
--langtag-color,
|
|
134
|
+
color-mix(in srgb, currentColor 72%, transparent)
|
|
135
|
+
);
|
|
136
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
|
137
|
+
font-size: var(--langtag-font-size, 0.875em);
|
|
138
|
+
line-height: 1;
|
|
139
|
+
letter-spacing: 0.01em;
|
|
140
|
+
/* Trims the line box to the x-height, so the glyphs sit in the
|
|
141
|
+
optical center of the pill. Ignored where unsupported. */
|
|
142
|
+
text-box: trim-both ex alphabetic;
|
|
143
|
+
}
|
|
144
|
+
</style>
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type { SvelteComponentTyped } from "svelte";
|
|
2
|
+
import type { HTMLAttributes } from "svelte/elements";
|
|
3
|
+
|
|
4
|
+
export type CodeToolbarProps = HTMLAttributes<HTMLDivElement> & {
|
|
5
|
+
/**
|
|
6
|
+
* Language badge text. Omitted entirely when unset.
|
|
7
|
+
*/
|
|
8
|
+
languageName?: string;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* File name shown before the badge, with a `title` attribute for
|
|
12
|
+
* truncation.
|
|
13
|
+
* @default ""
|
|
14
|
+
*/
|
|
15
|
+
title?: string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Whether the toolbar sticks to the top of its scrolling ancestor via
|
|
19
|
+
* `position: sticky`.
|
|
20
|
+
* @default true
|
|
21
|
+
*/
|
|
22
|
+
sticky?: boolean;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Accessible name for the `role="toolbar"` root. Omitted when unset.
|
|
26
|
+
*/
|
|
27
|
+
label?: string;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Tint painted over the theme background.
|
|
31
|
+
* @default "color-mix(in srgb, currentColor 5%, transparent)"
|
|
32
|
+
*/
|
|
33
|
+
"--toolbar-background"?: string;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Text color of the toolbar content. Defaults to the theme's text color.
|
|
37
|
+
* @default "inherit"
|
|
38
|
+
*/
|
|
39
|
+
"--toolbar-color"?: string;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Toolbar bottom border.
|
|
43
|
+
* @default "1px solid color-mix(in srgb, currentColor 14%, transparent)"
|
|
44
|
+
*/
|
|
45
|
+
"--toolbar-border"?: string;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Toolbar padding.
|
|
49
|
+
* @default "0.25em 0.375em 0.25em 1em"
|
|
50
|
+
*/
|
|
51
|
+
"--toolbar-padding"?: string;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Gap between the title, badge and controls.
|
|
55
|
+
* @default "0.75em"
|
|
56
|
+
*/
|
|
57
|
+
"--toolbar-gap"?: string;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Minimum height of the toolbar.
|
|
61
|
+
* @default "2.75em"
|
|
62
|
+
*/
|
|
63
|
+
"--toolbar-height"?: string;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Toolbar font family.
|
|
67
|
+
* @default "system-ui, -apple-system, \"Segoe UI\", Roboto, sans-serif"
|
|
68
|
+
*/
|
|
69
|
+
"--toolbar-font-family"?: string;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Toolbar font size.
|
|
73
|
+
* @default "0.8125em"
|
|
74
|
+
*/
|
|
75
|
+
"--toolbar-font-size"?: string;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Stacking order of a sticky toolbar.
|
|
79
|
+
* @default 3
|
|
80
|
+
*/
|
|
81
|
+
"--toolbar-z-index"?: string | number;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Language badge background.
|
|
85
|
+
* @default "color-mix(in srgb, currentColor 10%, transparent)"
|
|
86
|
+
*/
|
|
87
|
+
"--langtag-background"?: string;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Language badge text color.
|
|
91
|
+
* @default "color-mix(in srgb, currentColor 72%, transparent)"
|
|
92
|
+
*/
|
|
93
|
+
"--langtag-color"?: string;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Language badge padding.
|
|
97
|
+
* @default "0.125em 0.625em"
|
|
98
|
+
*/
|
|
99
|
+
"--langtag-padding"?: string;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Language badge corner radius.
|
|
103
|
+
* @default "999px"
|
|
104
|
+
*/
|
|
105
|
+
"--langtag-border-radius"?: string;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Language badge font size.
|
|
109
|
+
* @default "0.875em"
|
|
110
|
+
*/
|
|
111
|
+
"--langtag-font-size"?: string;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export type CodeToolbarEvents = {};
|
|
115
|
+
|
|
116
|
+
export type CodeToolbarSlots = {
|
|
117
|
+
start: {};
|
|
118
|
+
default: {};
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export default class CodeToolbar extends SvelteComponentTyped<
|
|
122
|
+
CodeToolbarProps,
|
|
123
|
+
CodeToolbarEvents,
|
|
124
|
+
CodeToolbarSlots
|
|
125
|
+
> {}
|
package/CodeWindow.svelte
CHANGED
|
@@ -4,6 +4,22 @@
|
|
|
4
4
|
|
|
5
5
|
/** @type {string} */
|
|
6
6
|
export let title = "";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Accessible name for the code region inside the window. `title` wins
|
|
10
|
+
* when set. Without either, the inner block keeps its own default.
|
|
11
|
+
* @type {string | undefined}
|
|
12
|
+
*/
|
|
13
|
+
export let label = undefined;
|
|
14
|
+
|
|
15
|
+
import { setContext } from "svelte";
|
|
16
|
+
import { writable } from "svelte/store";
|
|
17
|
+
import { CODE_REGION } from "./code-region.js";
|
|
18
|
+
|
|
19
|
+
/** @type {import("svelte/store").Writable<string | undefined>} */
|
|
20
|
+
const regionLabel = writable(undefined);
|
|
21
|
+
setContext(CODE_REGION, regionLabel);
|
|
22
|
+
$: regionLabel.set(title || label);
|
|
7
23
|
</script>
|
|
8
24
|
|
|
9
25
|
<div class="code-window {variant}" {...$$restProps}>
|
|
@@ -62,6 +78,9 @@
|
|
|
62
78
|
sans-serif
|
|
63
79
|
);
|
|
64
80
|
font-size: var(--titlebar-font-size, 0.8125em);
|
|
81
|
+
/* The title clips overflow, so an inherited line-height: 1 cuts off
|
|
82
|
+
descenders. normal always fits the glyphs. */
|
|
83
|
+
line-height: var(--titlebar-line-height, normal);
|
|
65
84
|
}
|
|
66
85
|
|
|
67
86
|
.dots {
|
package/CodeWindow.svelte.d.ts
CHANGED
|
@@ -8,6 +8,13 @@ export type CodeWindowProps = HTMLAttributes<HTMLDivElement> & {
|
|
|
8
8
|
/** Title bar text, also set as its `title` tooltip. @default "" */
|
|
9
9
|
title?: string;
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Accessible name for the code region inside the window. `title` wins
|
|
13
|
+
* when set. The inner block (`Highlight`, `LineNumbers`, `AnsiOutput`,
|
|
14
|
+
* ...) takes it as its default `label`.
|
|
15
|
+
*/
|
|
16
|
+
label?: string;
|
|
17
|
+
|
|
11
18
|
/** Window body background. @default "#1e1e1e" */
|
|
12
19
|
"--window-background"?: string;
|
|
13
20
|
|
|
@@ -38,6 +45,9 @@ export type CodeWindowProps = HTMLAttributes<HTMLDivElement> & {
|
|
|
38
45
|
/** Title bar font size. @default "0.8125em" */
|
|
39
46
|
"--titlebar-font-size"?: string;
|
|
40
47
|
|
|
48
|
+
/** Title bar line height. @default "normal" */
|
|
49
|
+
"--titlebar-line-height"?: string;
|
|
50
|
+
|
|
41
51
|
/** Traffic-light dot gap. @default "0.5em" */
|
|
42
52
|
"--dot-gap"?: string;
|
|
43
53
|
|
package/CopyButton.svelte
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
/** @type {string} */
|
|
3
3
|
export let code;
|
|
4
4
|
|
|
5
|
+
import { createEventDispatcher, getContext, onMount } from "svelte";
|
|
6
|
+
|
|
7
|
+
const inToolbar = getContext("svelte-highlight:code-toolbar") === true;
|
|
8
|
+
|
|
9
|
+
/** @type {boolean} */
|
|
10
|
+
export let absolute = !inToolbar;
|
|
11
|
+
|
|
5
12
|
/** @type {(code: string) => void | Promise<void>} */
|
|
6
13
|
export let copy = (code) => navigator.clipboard.writeText(code);
|
|
7
14
|
|
|
@@ -17,7 +24,8 @@
|
|
|
17
24
|
/** @type {string} */
|
|
18
25
|
export let copiedText = "Copied!";
|
|
19
26
|
|
|
20
|
-
|
|
27
|
+
/** @type {string} */
|
|
28
|
+
export let errorText = "Copy failed";
|
|
21
29
|
|
|
22
30
|
const dispatch = createEventDispatcher();
|
|
23
31
|
|
|
@@ -27,6 +35,9 @@
|
|
|
27
35
|
/** @type {boolean} */
|
|
28
36
|
let copying = false;
|
|
29
37
|
|
|
38
|
+
/** @type {boolean} */
|
|
39
|
+
let failed = false;
|
|
40
|
+
|
|
30
41
|
/** @type {ReturnType<typeof setTimeout> | undefined} */
|
|
31
42
|
let timeoutId;
|
|
32
43
|
|
|
@@ -35,6 +46,8 @@
|
|
|
35
46
|
|
|
36
47
|
try {
|
|
37
48
|
copying = true;
|
|
49
|
+
failed = false;
|
|
50
|
+
clearTimeout(timeoutId);
|
|
38
51
|
const transformed = transform(code);
|
|
39
52
|
await copy(transformed);
|
|
40
53
|
copied = true;
|
|
@@ -43,6 +56,10 @@
|
|
|
43
56
|
copied = false;
|
|
44
57
|
}, timeout);
|
|
45
58
|
} catch (error) {
|
|
59
|
+
failed = true;
|
|
60
|
+
timeoutId = setTimeout(() => {
|
|
61
|
+
failed = false;
|
|
62
|
+
}, timeout);
|
|
46
63
|
dispatch("error", { error });
|
|
47
64
|
} finally {
|
|
48
65
|
copying = false;
|
|
@@ -55,9 +72,13 @@
|
|
|
55
72
|
<button
|
|
56
73
|
type="button"
|
|
57
74
|
aria-label={copied ? copiedText : text}
|
|
75
|
+
title={inToolbar ? (copied ? copiedText : text) : undefined}
|
|
58
76
|
on:click={handleClick}
|
|
59
77
|
on:mouseenter
|
|
60
78
|
on:mouseleave
|
|
79
|
+
class:static={!absolute}
|
|
80
|
+
class:toolbar={inToolbar}
|
|
81
|
+
class:copied
|
|
61
82
|
{...$$restProps}
|
|
62
83
|
>
|
|
63
84
|
<slot {copied} {copying}>
|
|
@@ -71,8 +92,7 @@
|
|
|
71
92
|
preserveAspectRatio="xMidYMid meet"
|
|
72
93
|
aria-hidden="true"
|
|
73
94
|
>
|
|
74
|
-
<path
|
|
75
|
-
d="M13 24 4 15 5.414 13.586 13 21.171 26.586 7.586 28 9 13 24z"
|
|
95
|
+
<path d="M13 24 4 15 5.414 13.586 13 21.171 26.586 7.586 28 9 13 24z"
|
|
76
96
|
></path>
|
|
77
97
|
</svg>
|
|
78
98
|
{:else}
|
|
@@ -93,6 +113,10 @@
|
|
|
93
113
|
{/if}
|
|
94
114
|
</slot>
|
|
95
115
|
</button>
|
|
116
|
+
<!-- Screen readers skip aria-label changes, so announce the result here. -->
|
|
117
|
+
<span class="visually-hidden" role="status"
|
|
118
|
+
>{copied ? copiedText : failed ? errorText : ""}</span
|
|
119
|
+
>
|
|
96
120
|
|
|
97
121
|
<style>
|
|
98
122
|
button {
|
|
@@ -112,4 +136,84 @@
|
|
|
112
136
|
z-index: var(--copy-z-index, 2);
|
|
113
137
|
cursor: pointer;
|
|
114
138
|
}
|
|
139
|
+
|
|
140
|
+
.visually-hidden {
|
|
141
|
+
position: absolute;
|
|
142
|
+
width: 1px;
|
|
143
|
+
height: 1px;
|
|
144
|
+
padding: 0;
|
|
145
|
+
margin: -1px;
|
|
146
|
+
overflow: hidden;
|
|
147
|
+
clip-path: inset(50%);
|
|
148
|
+
white-space: nowrap;
|
|
149
|
+
border: 0;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
button.static {
|
|
153
|
+
position: static;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/* Inside CodeToolbar: matches ToolbarButton and reads the same
|
|
157
|
+
--toolbar-button-* variables. --copy-* variables still win. */
|
|
158
|
+
button.toolbar {
|
|
159
|
+
width: auto;
|
|
160
|
+
font: inherit;
|
|
161
|
+
line-height: 1;
|
|
162
|
+
min-width: var(--copy-size, var(--toolbar-button-size, 2em));
|
|
163
|
+
height: var(--copy-size, var(--toolbar-button-size, 2em));
|
|
164
|
+
padding: var(--copy-padding, var(--toolbar-button-padding, 0.3em));
|
|
165
|
+
background: var(
|
|
166
|
+
--copy-background,
|
|
167
|
+
var(--toolbar-button-background, transparent)
|
|
168
|
+
);
|
|
169
|
+
color: var(
|
|
170
|
+
--copy-color,
|
|
171
|
+
var(
|
|
172
|
+
--toolbar-button-color,
|
|
173
|
+
color-mix(in srgb, currentColor 72%, transparent)
|
|
174
|
+
)
|
|
175
|
+
);
|
|
176
|
+
border-radius: var(
|
|
177
|
+
--copy-border-radius,
|
|
178
|
+
var(--toolbar-button-border-radius, 6px)
|
|
179
|
+
);
|
|
180
|
+
border: var(--copy-border, var(--toolbar-button-border, none));
|
|
181
|
+
transition:
|
|
182
|
+
background-color 120ms ease,
|
|
183
|
+
color 120ms ease;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
button.toolbar:hover {
|
|
187
|
+
background: var(
|
|
188
|
+
--copy-hover-background,
|
|
189
|
+
var(
|
|
190
|
+
--toolbar-button-hover-background,
|
|
191
|
+
color-mix(in srgb, currentColor 12%, transparent)
|
|
192
|
+
)
|
|
193
|
+
);
|
|
194
|
+
color: inherit;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
button.toolbar:active {
|
|
198
|
+
background: var(
|
|
199
|
+
--toolbar-button-pressed-background,
|
|
200
|
+
color-mix(in srgb, currentColor 18%, transparent)
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
button.toolbar.copied {
|
|
205
|
+
color: inherit;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
button.toolbar:focus-visible {
|
|
209
|
+
color: inherit;
|
|
210
|
+
outline: 2px solid currentColor;
|
|
211
|
+
outline-offset: 1px;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
@media (prefers-reduced-motion: reduce) {
|
|
215
|
+
button.toolbar {
|
|
216
|
+
transition: none;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
115
219
|
</style>
|
package/CopyButton.svelte.d.ts
CHANGED
|
@@ -23,9 +23,22 @@ export type CopyButtonProps = HTMLButtonAttributes & {
|
|
|
23
23
|
/** Button `aria-label`. @default "Copy" */
|
|
24
24
|
text?: string;
|
|
25
25
|
|
|
26
|
-
/**
|
|
26
|
+
/**
|
|
27
|
+
* Button `aria-label` while copied. Also announced to screen readers.
|
|
28
|
+
* @default "Copied!"
|
|
29
|
+
*/
|
|
27
30
|
copiedText?: string;
|
|
28
31
|
|
|
32
|
+
/** Announced to screen readers when the copy fails. @default "Copy failed" */
|
|
33
|
+
errorText?: string;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Overlay the container (`true`) or sit in normal flow (`false`).
|
|
37
|
+
* Defaults to `false` inside `CodeToolbar`.
|
|
38
|
+
* @default true
|
|
39
|
+
*/
|
|
40
|
+
absolute?: boolean;
|
|
41
|
+
|
|
29
42
|
/** Top offset. @default "0.5em" */
|
|
30
43
|
"--copy-top"?: string;
|
|
31
44
|
|
|
@@ -41,6 +54,9 @@ export type CopyButtonProps = HTMLButtonAttributes & {
|
|
|
41
54
|
/** Button background. @default "inherit" */
|
|
42
55
|
"--copy-background"?: string;
|
|
43
56
|
|
|
57
|
+
/** Button background on hover inside `CodeToolbar`. @default "color-mix(in srgb, currentColor 12%, transparent)" */
|
|
58
|
+
"--copy-hover-background"?: string;
|
|
59
|
+
|
|
44
60
|
/** Button color. @default "inherit" */
|
|
45
61
|
"--copy-color"?: string;
|
|
46
62
|
|