ochreui 0.0.0-stage → 0.0.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.
- package/LICENSE +21 -0
- package/README.md +153 -2
- package/cli/ochreui.mjs +601 -0
- package/mcp/core.mjs +126 -0
- package/mcp/docs.json +906 -0
- package/mcp/server.mjs +23 -0
- package/package.json +393 -3
- package/registry/agent-team.ts +350 -0
- package/registry/avatar.ts +300 -0
- package/registry/badge.ts +207 -0
- package/registry/breadcrumb.ts +435 -0
- package/registry/button.ts +238 -0
- package/registry/card.ts +221 -0
- package/registry/change-summary.ts +200 -0
- package/registry/checkbox.ts +204 -0
- package/registry/code-block.ts +332 -0
- package/registry/color-picker.ts +720 -0
- package/registry/comment-thread.ts +503 -0
- package/registry/context-menu.ts +281 -0
- package/registry/cover-flow.ts +623 -0
- package/registry/date-picker.ts +799 -0
- package/registry/decision-inbox.ts +340 -0
- package/registry/dialog.ts +577 -0
- package/registry/dropdown-menu.ts +864 -0
- package/registry/email-draft.ts +916 -0
- package/registry/empty-calendar.ts +199 -0
- package/registry/empty-state.ts +164 -0
- package/registry/enrichment-orbit.ts +164 -0
- package/registry/filter-menu.ts +541 -0
- package/registry/form-field.ts +308 -0
- package/registry/frost-cloud.ts +381 -0
- package/registry/ghosted-chat.ts +210 -0
- package/registry/glow-cloud.ts +402 -0
- package/registry/grid-background.ts +164 -0
- package/registry/hover-card.ts +401 -0
- package/registry/icon-button.ts +133 -0
- package/registry/index.json +2124 -0
- package/registry/integration-beam.ts +588 -0
- package/registry/integration-grid.ts +209 -0
- package/registry/kbd.ts +176 -0
- package/registry/layer-stack.ts +351 -0
- package/registry/lead-sourcing.ts +285 -0
- package/registry/logo-cluster.ts +239 -0
- package/registry/logo-marquee.ts +505 -0
- package/registry/message-actions.ts +833 -0
- package/registry/model-orbit.ts +583 -0
- package/registry/model-wheel.ts +372 -0
- package/registry/notification-stack.ts +301 -0
- package/registry/number-input.ts +552 -0
- package/registry/number-ticker.ts +261 -0
- package/registry/ochre.css +917 -0
- package/registry/otp-input.ts +704 -0
- package/registry/overlay.ts +312 -0
- package/registry/pagination.ts +362 -0
- package/registry/popover.ts +296 -0
- package/registry/profile-menu.ts +332 -0
- package/registry/progress-tracker.ts +291 -0
- package/registry/rating.ts +461 -0
- package/registry/reaction-bar.ts +322 -0
- package/registry/region-map.ts +537 -0
- package/registry/resizable.ts +489 -0
- package/registry/scroll-area.ts +271 -0
- package/registry/segmented-control.ts +433 -0
- package/registry/select.ts +769 -0
- package/registry/separator.ts +97 -0
- package/registry/shimmer-loader.ts +211 -0
- package/registry/side-folder.ts +152 -0
- package/registry/skeleton.ts +136 -0
- package/registry/skill/INSTRUCTIONS.md +11 -0
- package/registry/skill/SKILL.md +65 -0
- package/registry/skill/checklist.md +45 -0
- package/registry/skill/references/agent-team.md +105 -0
- package/registry/skill/references/avatar.md +88 -0
- package/registry/skill/references/badge.md +92 -0
- package/registry/skill/references/breadcrumb.md +99 -0
- package/registry/skill/references/button.md +82 -0
- package/registry/skill/references/card.md +111 -0
- package/registry/skill/references/catalog.md +114 -0
- package/registry/skill/references/change-summary.md +103 -0
- package/registry/skill/references/checkbox.md +105 -0
- package/registry/skill/references/code-block.md +109 -0
- package/registry/skill/references/color-picker.md +138 -0
- package/registry/skill/references/comment-thread.md +164 -0
- package/registry/skill/references/context-menu.md +205 -0
- package/registry/skill/references/cover-flow.md +128 -0
- package/registry/skill/references/date-picker.md +154 -0
- package/registry/skill/references/decision-inbox.md +126 -0
- package/registry/skill/references/dialog.md +155 -0
- package/registry/skill/references/dropdown-menu.md +144 -0
- package/registry/skill/references/email-draft.md +182 -0
- package/registry/skill/references/empty-calendar.md +74 -0
- package/registry/skill/references/empty-state.md +118 -0
- package/registry/skill/references/enrichment-orbit.md +71 -0
- package/registry/skill/references/filter-menu.md +143 -0
- package/registry/skill/references/form-field.md +118 -0
- package/registry/skill/references/frost-cloud.md +80 -0
- package/registry/skill/references/ghosted-chat.md +75 -0
- package/registry/skill/references/glow-cloud.md +84 -0
- package/registry/skill/references/grid-background.md +76 -0
- package/registry/skill/references/hover-card.md +125 -0
- package/registry/skill/references/icon-button.md +99 -0
- package/registry/skill/references/integration-beam.md +107 -0
- package/registry/skill/references/integration-grid.md +100 -0
- package/registry/skill/references/kbd.md +65 -0
- package/registry/skill/references/layer-stack.md +115 -0
- package/registry/skill/references/lead-sourcing.md +92 -0
- package/registry/skill/references/logo-cluster.md +92 -0
- package/registry/skill/references/logo-marquee.md +123 -0
- package/registry/skill/references/message-actions.md +146 -0
- package/registry/skill/references/model-orbit.md +106 -0
- package/registry/skill/references/model-wheel.md +84 -0
- package/registry/skill/references/notification-stack.md +138 -0
- package/registry/skill/references/number-input.md +117 -0
- package/registry/skill/references/number-ticker.md +73 -0
- package/registry/skill/references/otp-input.md +142 -0
- package/registry/skill/references/pagination.md +112 -0
- package/registry/skill/references/popover.md +143 -0
- package/registry/skill/references/profile-menu.md +123 -0
- package/registry/skill/references/progress-tracker.md +122 -0
- package/registry/skill/references/rating.md +111 -0
- package/registry/skill/references/reaction-bar.md +128 -0
- package/registry/skill/references/region-map.md +109 -0
- package/registry/skill/references/resizable.md +127 -0
- package/registry/skill/references/scroll-area.md +113 -0
- package/registry/skill/references/segmented-control.md +96 -0
- package/registry/skill/references/select.md +122 -0
- package/registry/skill/references/separator.md +69 -0
- package/registry/skill/references/shimmer-loader.md +83 -0
- package/registry/skill/references/side-folder.md +75 -0
- package/registry/skill/references/skeleton.md +95 -0
- package/registry/skill/references/smart-recommendation.md +132 -0
- package/registry/skill/references/spinner.md +84 -0
- package/registry/skill/references/switch.md +79 -0
- package/registry/skill/references/tab-overload.md +70 -0
- package/registry/skill/references/tag-input.md +126 -0
- package/registry/skill/references/team-invite.md +94 -0
- package/registry/skill/references/testimonial-wall.md +110 -0
- package/registry/skill/references/text-area.md +115 -0
- package/registry/skill/references/text-field.md +126 -0
- package/registry/skill/references/text-reveal.md +71 -0
- package/registry/skill/references/thinking-loader.md +87 -0
- package/registry/skill/references/time-picker.md +122 -0
- package/registry/skill/references/tips-panel.md +115 -0
- package/registry/skill/references/toggle-group.md +98 -0
- package/registry/skill/references/tool-call-card.md +151 -0
- package/registry/skill/references/tooltip.md +94 -0
- package/registry/skill/references/tree-view.md +172 -0
- package/registry/skill/theming.md +57 -0
- package/registry/smart-recommendation.ts +415 -0
- package/registry/spinner.ts +67 -0
- package/registry/switch.ts +146 -0
- package/registry/tab-overload.ts +211 -0
- package/registry/tag-input.ts +620 -0
- package/registry/team-invite.ts +248 -0
- package/registry/testimonial-wall.ts +521 -0
- package/registry/text-area.ts +432 -0
- package/registry/text-field.ts +415 -0
- package/registry/text-reveal.ts +175 -0
- package/registry/theme.ts +68 -0
- package/registry/thinking-loader.ts +302 -0
- package/registry/time-picker.ts +828 -0
- package/registry/tips-panel.ts +312 -0
- package/registry/toggle-group.ts +357 -0
- package/registry/tool-call-card.ts +578 -0
- package/registry/tooltip.ts +486 -0
- package/registry/tree-view.ts +454 -0
- package/styles/base.css +500 -0
- package/styles/tokens.css +415 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// Ochre (https://ochreui.dev), MIT License, Copyright (c) 2026 Ochre. Keep this notice in copies of this file.
|
|
2
|
+
/**
|
|
3
|
+
* Checkbox, from Ochre (https://ochreui.dev).
|
|
4
|
+
* Needs ochre.css (the --ui-* tokens and shared keyframes), which `npx ochreui init` adds once.
|
|
5
|
+
*
|
|
6
|
+
* <ui-checkbox label="Mentions" description="…" [error]="…"><input type="checkbox" uiCheckbox [formField]="form.mentions" /></ui-checkbox>
|
|
7
|
+
* Your own checkbox goes inside, so Angular forms, reset and native submission all work on it.
|
|
8
|
+
*
|
|
9
|
+
* Public styling hooks (set them on the element or any ancestor):
|
|
10
|
+
* --ui-checkbox-edge unchecked edge (default: the strong border; var(--ui-border-input) gives 3:1)
|
|
11
|
+
* --ui-checkbox-edge-hover unchecked edge on hover (default: a step darker)
|
|
12
|
+
* --ui-checkbox-fill checked and indeterminate fill (default: ink)
|
|
13
|
+
* --ui-checkbox-check the check and the dash (default: the text color on ink)
|
|
14
|
+
* --ui-checkbox-radius box corners (default 4.5px; Classic 5px)
|
|
15
|
+
*/
|
|
16
|
+
import {
|
|
17
|
+
Component,
|
|
18
|
+
Directive,
|
|
19
|
+
ElementRef,
|
|
20
|
+
afterNextRender,
|
|
21
|
+
booleanAttribute,
|
|
22
|
+
computed,
|
|
23
|
+
contentChild,
|
|
24
|
+
effect,
|
|
25
|
+
inject,
|
|
26
|
+
input,
|
|
27
|
+
isDevMode,
|
|
28
|
+
signal,
|
|
29
|
+
} from '@angular/core';
|
|
30
|
+
|
|
31
|
+
export type CheckboxVariant = 'plain' | 'card';
|
|
32
|
+
|
|
33
|
+
let nextId = 0;
|
|
34
|
+
|
|
35
|
+
/** Put it on the `<input type="checkbox">` inside a `<ui-checkbox>`. Write `type="checkbox"` yourself, so Angular forms bind it. */
|
|
36
|
+
@Directive({
|
|
37
|
+
selector: 'input[type=checkbox][uiCheckbox]',
|
|
38
|
+
host: {
|
|
39
|
+
style: 'position: absolute; inset: calc(var(--ui-space-1-5) * -1); z-index: 1; margin: 0; opacity: 0; cursor: inherit',
|
|
40
|
+
'[id]': 'id()',
|
|
41
|
+
'[attr.aria-labelledby]': 'labelledBy',
|
|
42
|
+
'[attr.aria-describedby]': 'describedBy()',
|
|
43
|
+
'[attr.aria-invalid]': 'field.invalid() || null',
|
|
44
|
+
'(change)': 'sync()',
|
|
45
|
+
},
|
|
46
|
+
})
|
|
47
|
+
export class UiCheckboxInput {
|
|
48
|
+
readonly id = input(`ui-checkbox-${nextId++}`);
|
|
49
|
+
/** "Some of them": shows a dash and reads as mixed. Kept after a click until you change it. */
|
|
50
|
+
readonly indeterminate = input(false, { transform: booleanAttribute });
|
|
51
|
+
/** Your own descriptions, kept alongside the checkbox's. Set it as `aria-describedby="…"`, not `[attr.aria-describedby]`. */
|
|
52
|
+
readonly ariaDescribedby = input<string>(undefined, { alias: 'aria-describedby' });
|
|
53
|
+
|
|
54
|
+
readonly el = inject<ElementRef<HTMLInputElement>>(ElementRef).nativeElement;
|
|
55
|
+
protected readonly field = inject(UiCheckbox);
|
|
56
|
+
private readonly named = this.el.hasAttribute('aria-label') || this.el.hasAttribute('aria-labelledby');
|
|
57
|
+
protected get labelledBy() {
|
|
58
|
+
return !this.named && this.field.label() ? this.field.titleId : null;
|
|
59
|
+
}
|
|
60
|
+
protected readonly describedBy = computed(
|
|
61
|
+
() => [this.ariaDescribedby(), ...this.field.describers()].filter(Boolean).join(' ') || null,
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
constructor() {
|
|
65
|
+
effect(() => this.sync());
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** A click clears the native flag; put it back until the app says otherwise. */
|
|
69
|
+
protected sync() {
|
|
70
|
+
this.el.indeterminate = this.indeterminate();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
@Component({
|
|
75
|
+
selector: 'ui-checkbox',
|
|
76
|
+
host: {
|
|
77
|
+
'[attr.data-variant]': 'variant()',
|
|
78
|
+
'[attr.data-invalid]': 'invalid() || null',
|
|
79
|
+
},
|
|
80
|
+
template: `
|
|
81
|
+
<label class="row">
|
|
82
|
+
<span class="ctl" style="position: relative; display: grid; place-items: center; block-size: var(--_lh)">
|
|
83
|
+
<ng-content select="input" />
|
|
84
|
+
<span class="box" aria-hidden="true" [attr.data-nudge]="nudge() || null" (animationend)="nudge.set(false)" (animationcancel)="nudge.set(false)">
|
|
85
|
+
<svg viewBox="0 0 16 16" fill="none" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" style="position: absolute; inset: 50% auto auto 50%; translate: -50% -50%; inline-size: var(--_g); block-size: var(--_g); stroke: var(--ui-checkbox-check, var(--ui-text-on-neutral))">
|
|
86
|
+
<path class="tick" pathLength="1" stroke-dasharray="1" stroke-dashoffset="1" opacity="0" d="M4.6 8.4l2.3 2.3 4.5-5.2" />
|
|
87
|
+
<path pathLength="1" stroke-dasharray="1" stroke-dashoffset="1" opacity="0" d="M8 8h3" />
|
|
88
|
+
<path pathLength="1" stroke-dasharray="1" stroke-dashoffset="1" opacity="0" d="M8 8H5" />
|
|
89
|
+
</svg>
|
|
90
|
+
</span>
|
|
91
|
+
</span>
|
|
92
|
+
@if (label() || description()) {
|
|
93
|
+
<span style="display: grid; min-inline-size: 0; overflow-wrap: anywhere">
|
|
94
|
+
@if (label()) {<span [id]="titleId" [style.font-weight]="variant() === 'card' ? 'var(--ui-weight-medium)' : null">{{ label() }}</span>}
|
|
95
|
+
@if (description()) {<span class="desc" [id]="descId">{{ description() }}</span>}
|
|
96
|
+
</span>
|
|
97
|
+
}
|
|
98
|
+
</label>
|
|
99
|
+
<p [class.ui-enter]="ready" [id]="errorId" aria-live="polite" [hidden]="!error()">{{ error() }}</p>
|
|
100
|
+
`,
|
|
101
|
+
styles: `
|
|
102
|
+
:host {
|
|
103
|
+
--_t: var(--ui-look-tray, 0);
|
|
104
|
+
--_s: var(--ui-space-4);
|
|
105
|
+
--_lh: var(--ui-leading-base);
|
|
106
|
+
--_on: var(--ui-checkbox-fill, var(--ui-bg-neutral-solid));
|
|
107
|
+
--_e: var(--ui-checkbox-edge, var(--ui-border-strong));
|
|
108
|
+
--_bg: var(--ui-bg-surface);
|
|
109
|
+
--_x: var(--ui-space-2-5);
|
|
110
|
+
--_d: color-mix(in srgb, var(--ui-gray-1000) 6%, transparent);
|
|
111
|
+
--_cb: var(--ui-bg-surface);
|
|
112
|
+
--_g: var(--ui-space-4);
|
|
113
|
+
--_l: 0 0 transparent;
|
|
114
|
+
display: flex;
|
|
115
|
+
flex-direction: column;
|
|
116
|
+
gap: var(--ui-space-1);
|
|
117
|
+
color: var(--ui-text);
|
|
118
|
+
font: calc(var(--ui-text-base) + 0.5px * var(--_t)) / var(--_lh) var(--ui-font-sans);
|
|
119
|
+
}
|
|
120
|
+
:host(:has(:disabled)) { opacity: var(--ui-disabled-opacity); cursor: not-allowed; }
|
|
121
|
+
:host(:has(:disabled)) .row { pointer-events: none; }
|
|
122
|
+
:host([data-variant=card]:has(:checked)) { --_ce: var(--ui-border); --_cb: light-dark(var(--ui-gray-25), var(--ui-bg-subtle)); }
|
|
123
|
+
.row { display: flex; align-items: flex-start; gap: var(--_x); inline-size: fit-content; cursor: pointer; }
|
|
124
|
+
@media (hover: hover) { .row:hover { --_e: var(--ui-checkbox-edge-hover, color-mix(in srgb, var(--ui-text) 35%, var(--_bg))); --_l: 0 4px 12px -4px color-mix(in srgb, var(--ui-gray-1000) 14%, transparent); } }
|
|
125
|
+
.box {
|
|
126
|
+
position: relative;
|
|
127
|
+
box-sizing: border-box;
|
|
128
|
+
inline-size: var(--_s);
|
|
129
|
+
block-size: var(--_s);
|
|
130
|
+
border: 1px solid var(--_e);
|
|
131
|
+
border-radius: var(--ui-checkbox-radius, calc(var(--ui-radius-sm) - 1.5px + 0.5px * var(--_t) + var(--_tr, 0px)));
|
|
132
|
+
background: var(--_bg);
|
|
133
|
+
box-shadow: 0 1px 1px var(--_d);
|
|
134
|
+
transition: var(--ui-duration-2) var(--ui-ease-out);
|
|
135
|
+
transition-property: background-color, border-color, scale;
|
|
136
|
+
}
|
|
137
|
+
:host(:has(:checked, :indeterminate)) .box { --_e: var(--_on); --_bg: var(--_on); }
|
|
138
|
+
.row:active .box { scale: 0.9; }
|
|
139
|
+
.ctl:has(:focus-visible) .box { outline: var(--ui-focus-width) solid var(--ui-focus-ring); outline-offset: var(--ui-focus-offset); }
|
|
140
|
+
:host([data-invalid]) .box { --_e: var(--ui-danger); }
|
|
141
|
+
.box[data-nudge] { animation: ui-nudge calc(320ms * var(--ui-motion-scale)) var(--ui-ease-out); }
|
|
142
|
+
:host(:has(:checked:not(:indeterminate))) .tick { stroke-dashoffset: 0; opacity: 1; transition: stroke-dashoffset calc(260ms * var(--ui-motion-scale)) var(--ui-ease-out) calc(40ms * var(--ui-motion-scale)); }
|
|
143
|
+
:host(:has(:indeterminate)) path:not(.tick) { stroke-dashoffset: 0; opacity: 1; transition: stroke-dashoffset var(--ui-duration-3) var(--ui-ease-out); }
|
|
144
|
+
.desc, p { font-size: calc(var(--ui-text-sm) + 0.5px * var(--_t)); line-height: calc(var(--ui-leading-xs) + 1px * var(--_t)); }
|
|
145
|
+
.desc { color: var(--ui-text-muted); }
|
|
146
|
+
p { margin: 0; padding-inline-start: calc(var(--_s) + var(--_x)); color: var(--ui-danger); }
|
|
147
|
+
:host([data-variant=card]) .row {
|
|
148
|
+
inline-size: auto;
|
|
149
|
+
padding: calc(var(--ui-space-2-5) + 2px * var(--_t)) calc(var(--ui-space-3) + 2px * var(--_t));
|
|
150
|
+
border-radius: calc(var(--ui-radius-md) + 4px * var(--_t));
|
|
151
|
+
background: var(--_cb);
|
|
152
|
+
box-shadow: 0 0 0 calc(0.5px + 0.5px * var(--_t)) var(--_ce, var(--ui-hairline)), 0 1px 2px var(--_d), var(--_l);
|
|
153
|
+
transition: var(--ui-duration-3) var(--ui-ease-out);
|
|
154
|
+
}
|
|
155
|
+
@media (pointer: coarse) {
|
|
156
|
+
:host { --_s: var(--ui-space-5); --_g: 18px; --_tr: 1px; --_lh: calc(var(--ui-leading-base) + 2px); font-size: calc(var(--ui-text-base) + 2px); }
|
|
157
|
+
}
|
|
158
|
+
@media (prefers-contrast: more) {
|
|
159
|
+
:host { --_e: var(--ui-checkbox-edge, var(--ui-border-input)); }
|
|
160
|
+
}
|
|
161
|
+
@media (forced-colors: active) {
|
|
162
|
+
:host { --_on: Highlight; --ui-checkbox-check: HighlightText; --_bg: Canvas; }
|
|
163
|
+
.box { forced-color-adjust: none; }
|
|
164
|
+
:host([data-invalid]) .box { border-width: 2px; }
|
|
165
|
+
:host([data-variant=card]) .row { border: 1px solid; }
|
|
166
|
+
}
|
|
167
|
+
`,
|
|
168
|
+
})
|
|
169
|
+
export class UiCheckbox {
|
|
170
|
+
/** Shown next to the box. Clicking it toggles. */
|
|
171
|
+
readonly label = input<string>();
|
|
172
|
+
/** A quiet line under the label. Screen readers hear it as the checkbox's description. */
|
|
173
|
+
readonly description = input<string>();
|
|
174
|
+
/** Marks the checkbox invalid and shows this message under it. */
|
|
175
|
+
readonly error = input<string | null | undefined>();
|
|
176
|
+
/** `card` makes the whole row a bordered card that toggles anywhere. */
|
|
177
|
+
readonly variant = input<CheckboxVariant>('plain');
|
|
178
|
+
|
|
179
|
+
protected readonly control = contentChild(UiCheckboxInput);
|
|
180
|
+
readonly titleId = `ui-checkbox-title-${nextId++}`;
|
|
181
|
+
readonly descId = `${this.titleId}-desc`;
|
|
182
|
+
protected readonly errorId = `${this.titleId}-error`;
|
|
183
|
+
|
|
184
|
+
readonly invalid = computed(() => !!this.error());
|
|
185
|
+
readonly describers = computed(() => [this.description() ? this.descId : '', this.invalid() ? this.errorId : '']);
|
|
186
|
+
protected readonly nudge = signal(false);
|
|
187
|
+
protected ready = false;
|
|
188
|
+
|
|
189
|
+
constructor() {
|
|
190
|
+
let was: boolean | undefined;
|
|
191
|
+
effect(() => {
|
|
192
|
+
const now = this.invalid();
|
|
193
|
+
if (now && was === false) this.nudge.set(true);
|
|
194
|
+
was = now;
|
|
195
|
+
});
|
|
196
|
+
afterNextRender(() => {
|
|
197
|
+
this.ready = true;
|
|
198
|
+
const el = this.control()?.el;
|
|
199
|
+
if (isDevMode() && el && !this.label() && !el.ariaLabel && !el.hasAttribute('aria-labelledby')) {
|
|
200
|
+
console.warn('ui-checkbox: give it a label, or an aria-label on the input.', el);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
// Ochre (https://ochreui.dev), MIT License, Copyright (c) 2026 Ochre. Keep this notice in copies of this file.
|
|
2
|
+
/**
|
|
3
|
+
* Code Block, from Ochre (https://ochreui.dev).
|
|
4
|
+
* Needs ochre.css (`npx ochreui init`).
|
|
5
|
+
*
|
|
6
|
+
* <ui-code-block language="ts" filename="agents/reviewer.ts" lineNumbers [highlightLines]="[8, 9]" [code]="source" />
|
|
7
|
+
* Code to read and copy: a header with the file name and a copy button, optional line numbers and highlighted
|
|
8
|
+
* lines. A small built-in colorizer handles TypeScript / JavaScript, JSON, shell and Python.
|
|
9
|
+
*
|
|
10
|
+
* Public styling hooks (set them on the element or any ancestor):
|
|
11
|
+
* --ui-code-block-radius outer corners (default 10px; Classic 12px)
|
|
12
|
+
* --ui-code-block-bg the block (default: the surface; Classic: the subtle tray)
|
|
13
|
+
* --ui-code-block-well behind the code (default: the surface)
|
|
14
|
+
* --ui-code-block-mark tint of highlighted lines (default: a faint info blue)
|
|
15
|
+
* --ui-code-block-mark-edge the 2px edge at the start of highlighted lines
|
|
16
|
+
*/
|
|
17
|
+
import {
|
|
18
|
+
Component,
|
|
19
|
+
DOCUMENT,
|
|
20
|
+
DestroyRef,
|
|
21
|
+
ElementRef,
|
|
22
|
+
afterNextRender,
|
|
23
|
+
booleanAttribute,
|
|
24
|
+
computed,
|
|
25
|
+
inject,
|
|
26
|
+
input,
|
|
27
|
+
isDevMode,
|
|
28
|
+
output,
|
|
29
|
+
signal,
|
|
30
|
+
viewChild,
|
|
31
|
+
} from '@angular/core';
|
|
32
|
+
|
|
33
|
+
export interface CodeBlockLabels {
|
|
34
|
+
/** The copy button's name and tooltip. */
|
|
35
|
+
copy: string;
|
|
36
|
+
/** The button's name and the announcement after a copy. */
|
|
37
|
+
copied: string;
|
|
38
|
+
/** Names the block when it has no file name or language, and ends the scroll region's name. */
|
|
39
|
+
code: string;
|
|
40
|
+
/** Announced when the clipboard refused; the code is selected instead. */
|
|
41
|
+
copyFailed: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The languages that get colors. Any other string shows as plain text, with its name in the header. */
|
|
45
|
+
export type CodeBlockLanguage = 'ts' | 'tsx' | 'js' | 'jsx' | 'json' | 'bash' | 'sh' | 'shell' | 'zsh' | 'python' | 'py' | (string & {});
|
|
46
|
+
|
|
47
|
+
const LABELS: CodeBlockLabels = {
|
|
48
|
+
copy: 'Copy code',
|
|
49
|
+
copied: 'Copied',
|
|
50
|
+
code: 'Code',
|
|
51
|
+
copyFailed: 'Selected. Press Ctrl+C or ⌘C to copy.',
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const NAMES: Record<string, string> = {
|
|
55
|
+
ts: 'TypeScript', typescript: 'TypeScript', tsx: 'TSX', js: 'JavaScript', javascript: 'JavaScript', jsx: 'JSX',
|
|
56
|
+
json: 'JSON', bash: 'Bash', sh: 'Shell', shell: 'Shell', zsh: 'Zsh', py: 'Python', python: 'Python',
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
type Kind = '' | 'k' | 's' | 'f' | 'n' | 'c';
|
|
60
|
+
interface Token { k: Kind; v: string }
|
|
61
|
+
type Rules = [RegExp, Kind][];
|
|
62
|
+
|
|
63
|
+
const STR = String.raw`"(?:\\.|[^"\\\n])*"|'(?:\\.|[^'\\\n])*'`;
|
|
64
|
+
const words = (list: string) => new RegExp(String.raw`\b(?:${list})\b`, 'y');
|
|
65
|
+
|
|
66
|
+
const TS: Rules = [
|
|
67
|
+
[/\/\/.*|\/\*[\s\S]*?(?:\*\/|$)/y, 'c'],
|
|
68
|
+
[/`(?:\\[\s\S]|[^`\\])*`?/y, 's'],
|
|
69
|
+
[new RegExp(STR, 'y'), 's'],
|
|
70
|
+
[words('abstract|as|async|await|break|case|catch|class|const|continue|debugger|declare|default|delete|do|else|enum|export|extends|finally|for|from|function|if|implements|import|in|instanceof|interface|keyof|let|new|of|private|protected|public|readonly|return|satisfies|static|super|switch|this|throw|try|type|typeof|var|void|while|yield'), 'k'],
|
|
71
|
+
[/\b(?:true|false|null|undefined|NaN|Infinity)\b|\b0[xXbBoO][\da-fA-F_]+n?\b|\b\d[\d_]*(?:\.\d+)?(?:[eE][+-]?\d+)?n?\b/y, 'n'],
|
|
72
|
+
[/[A-Za-z_$][\w$]*(?=\s*(?:<[^<>()]*>)?\()/y, 'f'],
|
|
73
|
+
[/[A-Za-z_$][\w$]*/y, ''],
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
const RULES: Record<string, Rules> = {
|
|
77
|
+
ts: TS,
|
|
78
|
+
json: [
|
|
79
|
+
[/"(?:\\.|[^"\\\n])*"(?=\s*:)/y, 'k'],
|
|
80
|
+
[/"(?:\\.|[^"\\\n])*"/y, 's'],
|
|
81
|
+
[/-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?|\b(?:true|false|null)\b/y, 'n'],
|
|
82
|
+
],
|
|
83
|
+
sh: [
|
|
84
|
+
[/(?<=^|\s)#.*/my, 'c'],
|
|
85
|
+
[new RegExp(STR, 'y'), 's'],
|
|
86
|
+
[/\$\{[^}\n]*\}|\$\w+|\$[@#?$!*-]/y, 'k'],
|
|
87
|
+
[/(?<=(?:^[ \t]*(?:\$[ \t]+)?)|(?:[|;&(][ \t]*))[\w./~+-]+(?![\w./~+-]*=)/my, 'f'],
|
|
88
|
+
[/[^\s|;&<>()"'$`]+/y, ''],
|
|
89
|
+
],
|
|
90
|
+
py: [
|
|
91
|
+
[/#.*/y, 'c'],
|
|
92
|
+
[/(?:[rRbBuUfF]{1,2})?(?:"""[\s\S]*?(?:"""|$)|'''[\s\S]*?(?:'''|$)|"(?:\\.|[^"\\\n])*"|'(?:\\.|[^'\\\n])*')/y, 's'],
|
|
93
|
+
[/@[\w.]+/y, 'k'],
|
|
94
|
+
[words('and|as|assert|async|await|break|class|continue|def|del|elif|else|except|finally|for|from|global|if|import|in|is|lambda|nonlocal|not|or|pass|raise|return|try|while|with|yield'), 'k'],
|
|
95
|
+
[/\b(?:True|False|None)\b|\b\d[\d_]*(?:\.\d+)?(?:[eE][+-]?\d+)?j?\b/y, 'n'],
|
|
96
|
+
[/[A-Za-z_]\w*(?=\()/y, 'f'],
|
|
97
|
+
[/[A-Za-z_]\w*/y, ''],
|
|
98
|
+
],
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const ALIASES: Record<string, string> = {
|
|
102
|
+
ts: 'ts', tsx: 'ts', js: 'ts', jsx: 'ts', mjs: 'ts', cjs: 'ts', typescript: 'ts', javascript: 'ts',
|
|
103
|
+
json: 'json', jsonc: 'json', bash: 'sh', sh: 'sh', shell: 'sh', zsh: 'sh', console: 'sh', py: 'py', python: 'py',
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/** Splits code into lines of colored tokens; it reruns only when `code` or `language` change. */
|
|
107
|
+
function tokenize(code: string, language = ''): Token[][] {
|
|
108
|
+
const rules = RULES[ALIASES[language.toLowerCase()]] ?? [];
|
|
109
|
+
const lines: Token[][] = [[]];
|
|
110
|
+
const push = (k: Kind, v: string) =>
|
|
111
|
+
v.split('\n').forEach((part, i) => {
|
|
112
|
+
if (i) lines.push([]);
|
|
113
|
+
const line = lines[lines.length - 1];
|
|
114
|
+
const last = line[line.length - 1];
|
|
115
|
+
if (!part) return;
|
|
116
|
+
if (last?.k === k) last.v += part;
|
|
117
|
+
else line.push({ k, v: part });
|
|
118
|
+
});
|
|
119
|
+
let pos = 0;
|
|
120
|
+
next: while (pos < code.length) {
|
|
121
|
+
for (const [re, k] of rules) {
|
|
122
|
+
re.lastIndex = pos;
|
|
123
|
+
const m = re.exec(code)?.[0];
|
|
124
|
+
if (m) {
|
|
125
|
+
push(k, m);
|
|
126
|
+
pos += m.length;
|
|
127
|
+
continue next;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
push('', code[pos++]);
|
|
131
|
+
}
|
|
132
|
+
return lines;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const HUES: Record<Kind, string | null> = {
|
|
136
|
+
'': null,
|
|
137
|
+
k: 'var(--ui-purple)',
|
|
138
|
+
s: 'light-dark(oklch(from var(--ui-hue-teal) calc(l - .14) c h), var(--ui-hue-teal))',
|
|
139
|
+
f: 'var(--ui-code-number)',
|
|
140
|
+
n: 'var(--ui-code-attr)',
|
|
141
|
+
c: 'var(--ui-code-comment)',
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
let nextId = 0;
|
|
145
|
+
|
|
146
|
+
@Component({
|
|
147
|
+
selector: 'ui-code-block',
|
|
148
|
+
host: {
|
|
149
|
+
'[attr.data-numbers]': 'lineNumbers() || null',
|
|
150
|
+
'[attr.data-wrap]': 'wrap() || null',
|
|
151
|
+
},
|
|
152
|
+
template: `
|
|
153
|
+
<figure [attr.aria-labelledby]="name() ? nameId : null" [attr.aria-label]="name() ? null : t().code" style="position: relative; display: flex; flex-direction: column; margin: 0; padding: calc(3px * var(--_t)); overflow: hidden; border-radius: var(--_r); background: var(--ui-code-block-bg, color-mix(in srgb, var(--ui-bg-subtle) calc(100% * var(--_t)), var(--ui-bg-surface))); box-shadow: 0 0 0 calc(0.5px - 0.5px * var(--_t)) var(--ui-hairline); color: var(--ui-text)">
|
|
154
|
+
@if (name() || copyable()) {
|
|
155
|
+
<figcaption class="head" style="display: flex; flex: none; align-items: center; gap: var(--ui-space-2); block-size: calc(var(--ui-space-8) + 4px - 4px * var(--_t)); padding-inline: calc(var(--ui-space-3) + 2px - 1px * var(--_t)) calc(var(--ui-space-1-5) - 3px * var(--_t)); box-shadow: inset 0 calc(-0.5px + 0.5px * var(--_t)) var(--ui-hairline); font: var(--ui-weight-medium) calc(var(--ui-text-sm) + 0.5px * var(--_t)) / var(--ui-leading-xs) var(--ui-font-sans)">
|
|
156
|
+
<span [id]="nameId" dir="auto" [attr.title]="name() || null" style="flex: 1; min-inline-size: 0; overflow: hidden; color: var(--ui-text-muted); text-overflow: ellipsis; white-space: nowrap">{{ name() }}</span>
|
|
157
|
+
@if (copyable()) {
|
|
158
|
+
<button type="button" class="ui-btn-icon copy" [attr.data-copied]="done() || null" [attr.aria-label]="done() ? t().copied : t().copy" [title]="done() ? t().copied : t().copy" (click)="copy()">
|
|
159
|
+
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M10.5 4.5V4A1.5 1.5 0 0 0 9 2.5H4A1.5 1.5 0 0 0 2.5 4v5A1.5 1.5 0 0 0 4 10.5h.5" /><rect x="5.5" y="5.5" width="8" height="8" rx="1.75" style="fill: currentColor; fill-opacity: calc(1 - var(--_t))" /></svg>
|
|
160
|
+
<svg viewBox="0 0 16 16" aria-hidden="true" style="stroke-width: calc(var(--ui-icon-stroke) + 0.3px)"><path d="M3.5 8.5l3 3 6-7" /></svg>
|
|
161
|
+
</button>
|
|
162
|
+
}
|
|
163
|
+
</figcaption>
|
|
164
|
+
}
|
|
165
|
+
@if (copyable()) {<span class="ui-sr" role="status">{{ status() }}</span>}
|
|
166
|
+
<div #scroller class="scroll" dir="ltr" [style.max-height]="maxH()" [attr.tabindex]="scrolls() ? 0 : null" [attr.role]="scrolls() ? 'region' : null" [attr.aria-label]="scrolls() ? regionName() : null" style="flex: 1 1 auto; min-block-size: 0; overflow: auto; overscroll-behavior-x: contain; scrollbar-width: thin; border-radius: calc(var(--_r) - 4px + 1px * var(--_t)); background: var(--_w); box-shadow: 0 0 0 calc(1px * var(--_t)) color-mix(in srgb, var(--ui-text) calc(7% * var(--_t)), transparent), 0 calc(1px * var(--_t)) calc(2px * var(--_t)) color-mix(in srgb, var(--ui-gray-1000) calc(7% * var(--_t)), transparent)">
|
|
167
|
+
<pre style="margin: 0; padding-block: calc(var(--ui-space-2-5) + 2px * var(--_t)); font: calc(var(--ui-text-code) - 0.5px + 0.5px * var(--_t)) / var(--ui-leading-code) var(--ui-font-mono); tab-size: 2"><code #body [style.--_d]="digits()" [style.inline-size]="wrap() ? null : 'max-content'" style="display: block; min-inline-size: 100%; font: inherit">@for (line of lines(); track $index) {<span class="ln" [attr.data-marked]="marks().has($index + 1) || null">@if (lineNumbers()) {<span class="no" aria-hidden="true">{{ $index + 1 }}</span>}<span class="tx">@for (tok of line; track $index) {<span [style.color]="hues[tok.k]">{{ tok.v }}</span>}{{ nl }}</span></span>}</code></pre>
|
|
168
|
+
</div>
|
|
169
|
+
</figure>
|
|
170
|
+
`,
|
|
171
|
+
styles: `
|
|
172
|
+
:host {
|
|
173
|
+
--_t: var(--ui-look-tray, 0);
|
|
174
|
+
--_r: var(--ui-code-block-radius, calc(var(--ui-radius-md) + 2px + 2px * var(--_t)));
|
|
175
|
+
--_w: var(--ui-code-block-well, var(--ui-bg-surface));
|
|
176
|
+
--_mk: var(--ui-code-block-mark, color-mix(in srgb, var(--ui-info) 8%, transparent));
|
|
177
|
+
display: block;
|
|
178
|
+
min-inline-size: 0;
|
|
179
|
+
}
|
|
180
|
+
.copy {
|
|
181
|
+
position: relative;
|
|
182
|
+
border-radius: calc(var(--ui-radius-sm) + 1px * var(--_t));
|
|
183
|
+
transition-duration: calc((140ms + 20ms * var(--_t)) * var(--ui-motion-scale));
|
|
184
|
+
}
|
|
185
|
+
.copy:focus-visible { outline-offset: var(--ui-focus-offset); }
|
|
186
|
+
.copy[data-copied] { color: var(--ui-icon-active); }
|
|
187
|
+
.copy > svg {
|
|
188
|
+
grid-area: 1 / 1;
|
|
189
|
+
inline-size: calc(var(--ui-icon-md) - 1px);
|
|
190
|
+
block-size: calc(var(--ui-icon-md) - 1px);
|
|
191
|
+
fill: none;
|
|
192
|
+
stroke: currentColor;
|
|
193
|
+
stroke-width: var(--ui-icon-stroke);
|
|
194
|
+
stroke-linecap: round;
|
|
195
|
+
stroke-linejoin: round;
|
|
196
|
+
transition: opacity var(--ui-duration-3) ease-out, filter var(--ui-duration-3) ease-out, scale calc(320ms * var(--ui-motion-scale)) var(--ui-ease-out);
|
|
197
|
+
}
|
|
198
|
+
.copy > svg + svg, .copy[data-copied] > svg:first-child { opacity: 0; scale: 0.6; filter: blur(2px); }
|
|
199
|
+
.copy[data-copied] > svg + svg { opacity: 1; scale: 1; filter: blur(0); }
|
|
200
|
+
.scroll:focus-visible { outline: var(--ui-focus-width) solid var(--ui-focus-ring); outline-offset: var(--ui-focus-offset-inset); }
|
|
201
|
+
.ln { display: flex; padding-inline: calc(var(--ui-space-3) + 2px + 2px * var(--_t)); }
|
|
202
|
+
:host([data-numbers]) .ln { padding-inline-start: 0; }
|
|
203
|
+
.tx { flex: 1; white-space: pre; }
|
|
204
|
+
:host([data-wrap]) .tx { min-inline-size: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
|
|
205
|
+
.no {
|
|
206
|
+
position: sticky;
|
|
207
|
+
left: 0;
|
|
208
|
+
flex: none;
|
|
209
|
+
box-sizing: content-box;
|
|
210
|
+
min-inline-size: calc(var(--_d) * 1ch);
|
|
211
|
+
padding-inline: calc(var(--ui-space-4) + 2px * var(--_t)) calc(var(--ui-space-3) + 2px);
|
|
212
|
+
background: var(--_w);
|
|
213
|
+
color: var(--ui-text-placeholder);
|
|
214
|
+
font-variant-numeric: tabular-nums;
|
|
215
|
+
text-align: end;
|
|
216
|
+
user-select: none;
|
|
217
|
+
}
|
|
218
|
+
[data-marked] { background: var(--_mk); }
|
|
219
|
+
[data-marked], [data-marked] .no { box-shadow: inset 2px 0 var(--ui-code-block-mark-edge, color-mix(in srgb, var(--ui-info) 55%, var(--_w))); }
|
|
220
|
+
[data-marked] .no { background: linear-gradient(var(--_mk), var(--_mk)) var(--_w); color: var(--ui-text-muted); }
|
|
221
|
+
@media (pointer: coarse) {
|
|
222
|
+
.copy { --_s: calc(var(--ui-space-6) + 2px * var(--_t)); }
|
|
223
|
+
.copy::after { content: ''; position: absolute; inset: calc(-6px + 1px * var(--_t)); }
|
|
224
|
+
}
|
|
225
|
+
@media (forced-colors: active) {
|
|
226
|
+
figure { border: 1px solid; }
|
|
227
|
+
.head { border-block-end: 1px solid; }
|
|
228
|
+
[data-marked] { forced-color-adjust: none; background: Mark; color: MarkText; }
|
|
229
|
+
[data-marked] *, [data-marked] .no { background: Mark; color: inherit !important; }
|
|
230
|
+
}
|
|
231
|
+
`,
|
|
232
|
+
})
|
|
233
|
+
export class UiCodeBlock {
|
|
234
|
+
/** The code, copied exactly as given. One trailing newline is not drawn as an empty last line. */
|
|
235
|
+
readonly code = input.required<string>();
|
|
236
|
+
/** Colors TypeScript / JavaScript, JSON, shell and Python; named in the header when there is no `filename`. */
|
|
237
|
+
readonly language = input<CodeBlockLanguage>('');
|
|
238
|
+
/** Shown in the header instead of the language. Long names end in "…", with the full name as a tooltip. */
|
|
239
|
+
readonly filename = input('');
|
|
240
|
+
/** A number before every line. It stays put while long lines scroll, and is left out of selections. */
|
|
241
|
+
readonly lineNumbers = input(false, { transform: booleanAttribute });
|
|
242
|
+
/** Lines to mark, counted from 1. */
|
|
243
|
+
readonly highlightLines = input<readonly number[]>([]);
|
|
244
|
+
/** Long lines wrap instead of scrolling sideways. */
|
|
245
|
+
readonly wrap = input(false, { transform: booleanAttribute });
|
|
246
|
+
/** Past this height the code scrolls inside: a number is px, a string is any CSS length. */
|
|
247
|
+
readonly maxHeight = input<number | string | null>(null);
|
|
248
|
+
/** The copy button. Without it, a file name or a language, there is no header. */
|
|
249
|
+
readonly copyable = input(true, { transform: booleanAttribute });
|
|
250
|
+
/** Words for the button and the announcements, to translate them. */
|
|
251
|
+
readonly labels = input<Partial<CodeBlockLabels>>({});
|
|
252
|
+
/** After the code reached the clipboard, with the code. */
|
|
253
|
+
readonly copied = output<string>();
|
|
254
|
+
|
|
255
|
+
protected readonly t = computed(() => ({ ...LABELS, ...this.labels() }));
|
|
256
|
+
protected readonly name = computed(() => this.filename() || NAMES[this.language().toLowerCase()] || this.language());
|
|
257
|
+
protected readonly regionName = computed(() => (this.name() ? `${this.name()}, ${this.t().code}` : this.t().code));
|
|
258
|
+
protected readonly lines = computed(() => tokenize(this.code().replace(/\r\n?/g, '\n').replace(/\n$/, ''), this.language()));
|
|
259
|
+
protected readonly digits = computed(() => String(this.lines().length).length);
|
|
260
|
+
protected readonly marks = computed(() => new Set(this.highlightLines()));
|
|
261
|
+
protected readonly maxH = computed(() => {
|
|
262
|
+
const h = this.maxHeight();
|
|
263
|
+
return h == null || h === '' ? null : typeof h === 'number' ? `${h}px` : h;
|
|
264
|
+
});
|
|
265
|
+
protected readonly nl = '\n';
|
|
266
|
+
protected readonly hues = HUES;
|
|
267
|
+
protected readonly nameId = `ui-code-block-${nextId++}`;
|
|
268
|
+
|
|
269
|
+
protected readonly scrolls = signal(false);
|
|
270
|
+
protected readonly done = signal(false);
|
|
271
|
+
protected readonly status = signal('');
|
|
272
|
+
|
|
273
|
+
private readonly scroller = viewChild.required<ElementRef<HTMLElement>>('scroller');
|
|
274
|
+
private readonly body = viewChild.required<ElementRef<HTMLElement>>('body');
|
|
275
|
+
private readonly doc = inject(DOCUMENT);
|
|
276
|
+
private timer?: ReturnType<typeof setTimeout>;
|
|
277
|
+
private warned = false;
|
|
278
|
+
|
|
279
|
+
constructor() {
|
|
280
|
+
const destroy = inject(DestroyRef);
|
|
281
|
+
afterNextRender(() => {
|
|
282
|
+
const el = this.scroller().nativeElement;
|
|
283
|
+
const view = this.doc.defaultView;
|
|
284
|
+
const measure = () => {
|
|
285
|
+
const over = el.scrollWidth > el.clientWidth + 1 || el.scrollHeight > el.clientHeight + 1;
|
|
286
|
+
this.scrolls.set(over);
|
|
287
|
+
if (isDevMode() && over && !this.name() && !this.warned) {
|
|
288
|
+
this.warned = true;
|
|
289
|
+
console.warn('ui-code-block: it scrolls, so it is a keyboard stop; give it a filename or a language to name it.', el);
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
measure();
|
|
293
|
+
const ro = view?.ResizeObserver ? new view.ResizeObserver(measure) : undefined;
|
|
294
|
+
ro?.observe(el);
|
|
295
|
+
ro?.observe(this.body().nativeElement);
|
|
296
|
+
destroy.onDestroy(() => ro?.disconnect());
|
|
297
|
+
});
|
|
298
|
+
destroy.onDestroy(() => clearTimeout(this.timer));
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Copies the code. If the clipboard refuses, selects it and says how to copy by hand. */
|
|
302
|
+
async copy() {
|
|
303
|
+
const code = this.code();
|
|
304
|
+
try {
|
|
305
|
+
const clipboard = this.doc.defaultView?.navigator.clipboard;
|
|
306
|
+
if (!clipboard) throw new Error('No clipboard');
|
|
307
|
+
await clipboard.writeText(code);
|
|
308
|
+
} catch {
|
|
309
|
+
const range = this.doc.createRange();
|
|
310
|
+
range.selectNodeContents(this.body().nativeElement);
|
|
311
|
+
const sel = this.doc.getSelection();
|
|
312
|
+
sel?.removeAllRanges();
|
|
313
|
+
sel?.addRange(range);
|
|
314
|
+
this.status.set(this.t().copyFailed);
|
|
315
|
+
if (!this.done()) this.settle();
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
this.copied.emit(code);
|
|
319
|
+
this.status.set(this.t().copied);
|
|
320
|
+
if (this.done()) return;
|
|
321
|
+
this.done.set(true);
|
|
322
|
+
this.settle();
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
private settle() {
|
|
326
|
+
clearTimeout(this.timer);
|
|
327
|
+
this.timer = setTimeout(() => {
|
|
328
|
+
this.done.set(false);
|
|
329
|
+
this.status.set('');
|
|
330
|
+
}, 1400);
|
|
331
|
+
}
|
|
332
|
+
}
|