@iyulab/components 2.0.3 → 2.1.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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.1.0] - 2026-10-06
4
+
5
+ ### Added
6
+
7
+ - **`copyFromKey(text)` / `pasteFromKey()`** — Ctrl/Cmd + C, X and V for something that is not a text
8
+ field (a grid's selected cells, a list's selected rows), working in every browser. They use the
9
+ browser's `copy`/`cut`/`paste` event and its `clipboardData` when the browser fires one — no
10
+ permission, any page — and the async Clipboard API when it does not: Safari fires no `copy` event
11
+ without a text selection. `copyFromKey` resolves `true` only once the text is on the clipboard, so a
12
+ cut can clear its selection safely. Call them from the shortcut's `keydown` without preventing the
13
+ key.
14
+
3
15
  ## [2.0.3] - 2026-10-06
4
16
 
5
17
  ### Fixed
package/dist/index.d.ts CHANGED
@@ -60,6 +60,7 @@ export * from './events/RemoveEvent';
60
60
  export * from './events/ShiftEvent';
61
61
  export * from './events/ShowEvent';
62
62
  export * from './utilities/BrowserStorage.js';
63
+ export * from './utilities/clipboard.js';
63
64
  export * from './utilities/converters.js';
64
65
  export * from './utilities/devWarning.js';
65
66
  export * from './utilities/Dialog.js';
package/dist/index.js CHANGED
@@ -59,8 +59,9 @@ import { UTextarea } from "./components/textarea/UTextarea.js";
59
59
  import { UTreeItem } from "./components/tree-item/UTreeItem.js";
60
60
  import { UTree } from "./components/tree/UTree.js";
61
61
  import { BrowserStorage } from "./utilities/BrowserStorage.js";
62
+ import { copyFromKey, pasteFromKey } from "./utilities/clipboard.js";
62
63
  import { Dialog } from "./utilities/Dialog.js";
63
64
  import { Theme } from "./utilities/Theme.js";
64
65
  import { Toast } from "./utilities/Toast.js";
65
66
  import { decodeTsv, encodeTsv } from "./utilities/tsv.js";
66
- export { ALERT_STATUSES, BrowserStorage, Dialog, IconCache, IconRegistry, Locale, OverlayManager, Theme, Toast, UAlert, UAvatar, UBadge, UBreadcrumb, UBreadcrumbItem, UButton, UButtonGroup, UCard, UCarousel, UCheckbox, UChip, UCopyButton, UDatePicker, UDateRangePicker, UDialog, UDivider, UDrawer, UElement, UExpander, UField, UFileInput, UFloatingElement, UForm, UFormControlElement, UIcon, UIconButton, UInput, UMenu, UMenuItem, UOption, UOverlayElement, UPanel, UPopover, UProgressBar, UProgressRing, URadio, URating, USelect, USkeleton, USlider, USpinner, USplitPanel, USteps, USwitch, UTab, UTabPanel, UTag, UText, UTextarea, UTooltip, UTree, UTreeItem, arrayAttrConverter, booleanAttrConverter, createDevWarner, dateAttrConverter, dateTextPattern, decodeTsv, devWarnOnce, encodeTsv, formatCurrency, formatDate, formatDateRange, formatDateRangeText, formatDateText, formatDateTimeRangeText, formatDateTimeText, formatNumber, getDefaultBaseUrl, getParentElement, isCoarsePointer, isFocusCandidate, isImeComposing, jsonAttrConverter, parseDate, parseDateRange, parseDateTime, parseDateTimeRange, parseNumber, querySelectorAllWithin, querySelectorDeep, querySelectorWithin, resetDevWarnings, setDefaultBaseUrl, urlAttrConverter };
67
+ export { ALERT_STATUSES, BrowserStorage, Dialog, IconCache, IconRegistry, Locale, OverlayManager, Theme, Toast, UAlert, UAvatar, UBadge, UBreadcrumb, UBreadcrumbItem, UButton, UButtonGroup, UCard, UCarousel, UCheckbox, UChip, UCopyButton, UDatePicker, UDateRangePicker, UDialog, UDivider, UDrawer, UElement, UExpander, UField, UFileInput, UFloatingElement, UForm, UFormControlElement, UIcon, UIconButton, UInput, UMenu, UMenuItem, UOption, UOverlayElement, UPanel, UPopover, UProgressBar, UProgressRing, URadio, URating, USelect, USkeleton, USlider, USpinner, USplitPanel, USteps, USwitch, UTab, UTabPanel, UTag, UText, UTextarea, UTooltip, UTree, UTreeItem, arrayAttrConverter, booleanAttrConverter, copyFromKey, createDevWarner, dateAttrConverter, dateTextPattern, decodeTsv, devWarnOnce, encodeTsv, formatCurrency, formatDate, formatDateRange, formatDateRangeText, formatDateText, formatDateTimeRangeText, formatDateTimeText, formatNumber, getDefaultBaseUrl, getParentElement, isCoarsePointer, isFocusCandidate, isImeComposing, jsonAttrConverter, parseDate, parseDateRange, parseDateTime, parseDateTimeRange, parseNumber, pasteFromKey, querySelectorAllWithin, querySelectorDeep, querySelectorWithin, resetDevWarnings, setDefaultBaseUrl, urlAttrConverter };
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Copy and paste for a keyboard shortcut on something that is not a text field — a grid's selected
3
+ * cells, a list's selected rows.
4
+ *
5
+ * Neither browser mechanism works everywhere on its own:
6
+ * - The browser's `copy`/`cut`/`paste` events carry `clipboardData`, need no permission and work on
7
+ * any page — but Safari fires `copy`/`cut` only when the page has a text selection, so a grid
8
+ * whose selection is its own state never gets one there.
9
+ * - The async Clipboard API (`navigator.clipboard`) works in Safari inside the key press — but it
10
+ * needs a secure context, may be refused, and `readText()` asks the person (or is missing).
11
+ *
12
+ * These helpers use the event when the browser fires it and the Clipboard API when it does not. Call
13
+ * them from the `keydown` handler of the shortcut and **do not `preventDefault()` that key** — the
14
+ * browser's own copy or paste is what fires the event.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * onKeydown(e: KeyboardEvent) {
19
+ * if ((e.ctrlKey || e.metaKey) && e.key === 'x') {
20
+ * const range = this.selection;
21
+ * copyFromKey(this.selectionAsTsv()).then((ok) => { if (ok) this.clear(range); });
22
+ * }
23
+ * }
24
+ * ```
25
+ */
26
+ /**
27
+ * Puts `text` on the clipboard for the copy or cut key being pressed. Resolves `true` once the text
28
+ * is on the clipboard, `false` when neither the browser's copy event nor the Clipboard API took it —
29
+ * clear a cut selection only on `true`.
30
+ */
31
+ export declare function copyFromKey(text: string): Promise<boolean>;
32
+ /**
33
+ * Reads the clipboard text for the paste key being pressed — from the browser's paste event, or, when
34
+ * none comes, from the Clipboard API. Rejects with the Clipboard API's error when neither gives text.
35
+ */
36
+ export declare function pasteFromKey(): Promise<string>;
@@ -0,0 +1,87 @@
1
+ //#region src/utilities/clipboard.ts
2
+ /**
3
+ * Copy and paste for a keyboard shortcut on something that is not a text field — a grid's selected
4
+ * cells, a list's selected rows.
5
+ *
6
+ * Neither browser mechanism works everywhere on its own:
7
+ * - The browser's `copy`/`cut`/`paste` events carry `clipboardData`, need no permission and work on
8
+ * any page — but Safari fires `copy`/`cut` only when the page has a text selection, so a grid
9
+ * whose selection is its own state never gets one there.
10
+ * - The async Clipboard API (`navigator.clipboard`) works in Safari inside the key press — but it
11
+ * needs a secure context, may be refused, and `readText()` asks the person (or is missing).
12
+ *
13
+ * These helpers use the event when the browser fires it and the Clipboard API when it does not. Call
14
+ * them from the `keydown` handler of the shortcut and **do not `preventDefault()` that key** — the
15
+ * browser's own copy or paste is what fires the event.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * onKeydown(e: KeyboardEvent) {
20
+ * if ((e.ctrlKey || e.metaKey) && e.key === 'x') {
21
+ * const range = this.selection;
22
+ * copyFromKey(this.selectionAsTsv()).then((ok) => { if (ok) this.clear(range); });
23
+ * }
24
+ * }
25
+ * ```
26
+ */
27
+ /**
28
+ * Puts `text` on the clipboard for the copy or cut key being pressed. Resolves `true` once the text
29
+ * is on the clipboard, `false` when neither the browser's copy event nor the Clipboard API took it —
30
+ * clear a cut selection only on `true`.
31
+ */
32
+ function copyFromKey(text) {
33
+ return new Promise((resolve) => {
34
+ let settled = false;
35
+ const finish = (ok) => {
36
+ if (settled) return;
37
+ settled = true;
38
+ window.removeEventListener("copy", onEvent, true);
39
+ window.removeEventListener("cut", onEvent, true);
40
+ resolve(ok);
41
+ };
42
+ const onEvent = (e) => {
43
+ if (!e.clipboardData) return;
44
+ e.preventDefault();
45
+ e.clipboardData.setData("text/plain", text);
46
+ finish(true);
47
+ };
48
+ window.addEventListener("copy", onEvent, true);
49
+ window.addEventListener("cut", onEvent, true);
50
+ const write = navigator.clipboard?.writeText?.(text);
51
+ if (!write) {
52
+ setTimeout(() => finish(false));
53
+ return;
54
+ }
55
+ write.then(() => finish(true), () => setTimeout(() => finish(false)));
56
+ });
57
+ }
58
+ /**
59
+ * Reads the clipboard text for the paste key being pressed — from the browser's paste event, or, when
60
+ * none comes, from the Clipboard API. Rejects with the Clipboard API's error when neither gives text.
61
+ */
62
+ function pasteFromKey() {
63
+ return new Promise((resolve, reject) => {
64
+ let settled = false;
65
+ const onEvent = (e) => {
66
+ if (settled || !e.clipboardData) return;
67
+ settled = true;
68
+ window.removeEventListener("paste", onEvent, true);
69
+ e.preventDefault();
70
+ resolve(e.clipboardData.getData("text/plain"));
71
+ };
72
+ window.addEventListener("paste", onEvent, true);
73
+ setTimeout(() => {
74
+ if (settled) return;
75
+ settled = true;
76
+ window.removeEventListener("paste", onEvent, true);
77
+ const read = navigator.clipboard?.readText?.();
78
+ if (!read) {
79
+ reject(/* @__PURE__ */ new Error("Clipboard read is not available"));
80
+ return;
81
+ }
82
+ read.then(resolve, reject);
83
+ });
84
+ });
85
+ }
86
+ //#endregion
87
+ export { copyFromKey, pasteFromKey };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@iyulab/components",
3
3
  "description": "web-components library based on lit-element made by iyulab",
4
- "version": "2.0.3",
4
+ "version": "2.1.0",
5
5
  "keywords": [
6
6
  "iyulab",
7
7
  "components",
@@ -135,6 +135,7 @@ carries the control's role, not on the host element. Native nodes use native `di
135
135
  - [`Locale`](./references/utilities/locale.md) — Validation-message locale registry and lookup utility
136
136
  - [`elements`](./references/utilities/elements.md) — Shadow-DOM-aware DOM query helpers
137
137
  - [`format`](./references/utilities/format.md) — Locale-aware number, currency and date formatting
138
+ - [`clipboard`](./references/utilities/clipboard.md) — `copyFromKey` / `pasteFromKey`, Ctrl/Cmd + C · X · V for a grid or list selection in every browser (Safari fires no copy event without a text selection)
138
139
  - [`keyboard`](./references/utilities/keyboard.md) — `isImeComposing` for Enter handlers that must not fire mid-composition (Korean/Japanese/Chinese input)
139
140
  - [`tsv`](./references/utilities/tsv.md) — `encodeTsv` / `decodeTsv`, the spreadsheet clipboard format with RFC 4180 quoting
140
141
  - [`OverlayManager`](./references/utilities/overlay-manager.md) — Internal overlay stack and z-index manager
@@ -0,0 +1,48 @@
1
+ # clipboard
2
+
3
+ ```ts
4
+ import { copyFromKey, pasteFromKey } from '@iyulab/components';
5
+ // or: import { copyFromKey, pasteFromKey } from '@iyulab/components/dist/utilities/clipboard.js';
6
+ ```
7
+
8
+ Copy and paste for a keyboard shortcut on something that is not a text field — a grid's selected
9
+ cells, a list's selected rows.
10
+
11
+ Neither browser mechanism works everywhere on its own:
12
+
13
+ - The browser's `copy` / `cut` / `paste` events carry `clipboardData`, need no permission and work on
14
+ any page — but **Safari fires `copy`/`cut` only when the page has a text selection**, so a grid
15
+ whose selection is its own state never gets one there.
16
+ - The async Clipboard API works in Safari inside the key press — but it needs a secure context, may be
17
+ refused, and `readText()` asks the person (or is missing).
18
+
19
+ These helpers use the event when the browser fires it and the Clipboard API when it does not. Call
20
+ them from the shortcut's `keydown` handler and **do not `preventDefault()` that key** — the browser's
21
+ own copy or paste is what fires the event.
22
+
23
+ ## `copyFromKey(text: string): Promise<boolean>`
24
+
25
+ Puts `text` on the clipboard. Resolves `true` once it is there, `false` when neither path took it. A
26
+ cut clears its selection only on `true` — otherwise the data is gone without being on the clipboard.
27
+
28
+ ## `pasteFromKey(): Promise<string>`
29
+
30
+ Reads the clipboard text — from the paste event, or, when none comes, from `navigator.clipboard.readText()`.
31
+ Rejects with the Clipboard API's error when neither gives text.
32
+
33
+ ```ts
34
+ onKeydown(e: KeyboardEvent) {
35
+ if (!(e.ctrlKey || e.metaKey) || this.editing) return; // a text field keeps its own clipboard
36
+ const key = e.key.toLowerCase();
37
+ if (key === 'c' || key === 'x') {
38
+ const range = this.selection;
39
+ copyFromKey(encodeTsv(this.cells(range))).then((ok) => {
40
+ if (ok && key === 'x') this.clear(range);
41
+ });
42
+ } else if (key === 'v') {
43
+ pasteFromKey().then((text) => this.paste(decodeTsv(text)), (error) => this.report(error));
44
+ }
45
+ }
46
+ ```
47
+
48
+ `flex-table`, `u-simple-sheet` and `u-rich-table` use them for Ctrl/Cmd + C, X and V.