occulite-ui 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +171 -2
- package/THIRD_PARTY_NOTICES.md +36 -0
- package/dist/clipboard.d.ts +26 -0
- package/dist/clipboard.js +70 -0
- package/dist/components/ContextMenu.svelte +158 -0
- package/dist/components/ContextMenu.svelte.d.ts +14 -0
- package/dist/components/DataTable.svelte +1872 -0
- package/dist/components/DataTable.svelte.d.ts +122 -0
- package/dist/components/Dialog.svelte +420 -0
- package/dist/components/Dialog.svelte.d.ts +55 -0
- package/dist/components/GithubLink.svelte +74 -0
- package/dist/components/GithubLink.svelte.d.ts +16 -0
- package/dist/components/Icon.svelte +51 -0
- package/dist/components/Icon.svelte.d.ts +12 -0
- package/dist/components/LanguageSwitch.svelte +63 -0
- package/dist/components/LanguageSwitch.svelte.d.ts +17 -0
- package/dist/components/Loader.svelte +65 -0
- package/dist/components/Loader.svelte.d.ts +9 -0
- package/dist/components/MultiSelect.svelte +467 -0
- package/dist/components/MultiSelect.svelte.d.ts +28 -0
- package/dist/components/Notices.svelte +146 -0
- package/dist/components/Notices.svelte.d.ts +16 -0
- package/dist/components/PrimaryToolbarButton.svelte +189 -0
- package/dist/components/PrimaryToolbarButton.svelte.d.ts +34 -0
- package/dist/components/Tabs.svelte +118 -0
- package/dist/components/Tabs.svelte.d.ts +16 -0
- package/dist/components/ThemeSwitch.svelte +46 -0
- package/dist/components/ThemeSwitch.svelte.d.ts +11 -0
- package/dist/components/Toolbar.svelte +57 -0
- package/dist/components/Toolbar.svelte.d.ts +11 -0
- package/dist/components/ToolbarButton.svelte +126 -0
- package/dist/components/ToolbarButton.svelte.d.ts +22 -0
- package/dist/components/Tooltip.svelte +106 -0
- package/dist/components/Tooltip.svelte.d.ts +31 -0
- package/dist/components/TooltipBubble.svelte +65 -0
- package/dist/components/TooltipBubble.svelte.d.ts +18 -0
- package/dist/components/contextMenu.d.ts +19 -0
- package/dist/components/contextMenu.js +6 -0
- package/dist/components/dialogGeometry.d.ts +91 -0
- package/dist/components/dialogGeometry.js +140 -0
- package/dist/components/index.d.ts +20 -0
- package/dist/components/index.js +20 -0
- package/dist/components/multiSelect.d.ts +27 -0
- package/dist/components/multiSelect.js +26 -0
- package/dist/components/tooltip.d.ts +29 -0
- package/dist/components/tooltip.js +27 -0
- package/dist/css/app.css +3 -0
- package/dist/css/base.css +84 -0
- package/dist/css/tokens.css +170 -0
- package/dist/dialog/index.d.ts +6 -0
- package/dist/dialog/index.js +6 -0
- package/dist/embedding/ShellEmbedding.svelte.d.ts +27 -0
- package/dist/embedding/ShellEmbedding.svelte.js +79 -0
- package/dist/embedding/constants.d.ts +23 -0
- package/dist/embedding/constants.js +54 -0
- package/dist/embedding/index.d.ts +2 -0
- package/dist/embedding/index.js +2 -0
- package/dist/externalLink.d.ts +15 -0
- package/dist/externalLink.js +20 -0
- package/dist/format.d.ts +6 -0
- package/dist/format.js +16 -0
- package/dist/i18n/i18n.svelte.d.ts +112 -0
- package/dist/i18n/i18n.svelte.js +211 -0
- package/dist/i18n/index.d.ts +3 -0
- package/dist/i18n/index.js +3 -0
- package/dist/i18n/messages.d.ts +9 -0
- package/dist/i18n/messages.js +57 -0
- package/dist/i18n/translator.d.ts +65 -0
- package/dist/i18n/translator.js +99 -0
- package/dist/icons/index.d.ts +17 -0
- package/dist/icons/index.js +11 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +16 -0
- package/dist/notices/NoticesStore.svelte.d.ts +64 -0
- package/dist/notices/NoticesStore.svelte.js +127 -0
- package/dist/notices/index.d.ts +1 -0
- package/dist/notices/index.js +1 -0
- package/dist/storage.d.ts +11 -0
- package/dist/storage.js +27 -0
- package/dist/table/LocalColumnWidths.svelte.d.ts +32 -0
- package/dist/table/LocalColumnWidths.svelte.js +91 -0
- package/dist/table/columnMeasure.d.ts +24 -0
- package/dist/table/columnMeasure.js +82 -0
- package/dist/table/columnWidths.d.ts +60 -0
- package/dist/table/columnWidths.js +86 -0
- package/dist/table/dataTableContext.d.ts +26 -0
- package/dist/table/dataTableContext.js +16 -0
- package/dist/table/index.d.ts +6 -0
- package/dist/table/index.js +6 -0
- package/dist/table/metrics.d.ts +10 -0
- package/dist/table/metrics.js +10 -0
- package/dist/table/tableModel.d.ts +245 -0
- package/dist/table/tableModel.js +367 -0
- package/dist/theme/choices.d.ts +8 -0
- package/dist/theme/choices.js +6 -0
- package/dist/theme/index.d.ts +2 -0
- package/dist/theme/index.js +2 -0
- package/dist/theme/theme.svelte.d.ts +60 -0
- package/dist/theme/theme.svelte.js +108 -0
- package/package.json +137 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sebastian Raff
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,172 @@
|
|
|
1
|
-
#
|
|
1
|
+
# occulite-ui
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
openccu-lite's look for apps and addons: a Svelte 5 UI kit with the design tokens, a light and a
|
|
4
|
+
blue-grey dark theme, German and English strings, a virtualised `DataTable`, dialogs, menus,
|
|
5
|
+
tooltips, toasts, toolbars and the theme and language handling - and the frame side of
|
|
6
|
+
openccu-lite's embedding contract, so a page inside the system's shell follows the shell's theme
|
|
7
|
+
and language.
|
|
8
|
+
|
|
9
|
+
Its consumers are [homematic-manager](https://github.com/hobbyquaker/homematic-manager), the
|
|
10
|
+
system's own web interface ([occulited](https://github.com/hobbyquaker/occulited)) and the addons
|
|
11
|
+
for openccu-lite and the classic CCU.
|
|
12
|
+
|
|
13
|
+
**Status: 0.x.** The API is settling; in 0.x a minor version may break (the
|
|
14
|
+
[CHANGELOG](CHANGELOG.md) says what and how to migrate), a patch never does. The CSS prefixes
|
|
15
|
+
`--hmm-*`, `hmm-*` and `ol-*` stay through 0.x and are renamed before 1.0.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
npm install occulite-ui
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`svelte` (^5.40) is a peer dependency; the package has no runtime dependencies. It is a source
|
|
24
|
+
package: the `.svelte` files are compiled by your app's Svelte, so there is one Svelte runtime and
|
|
25
|
+
your compiler options apply.
|
|
26
|
+
|
|
27
|
+
## Vite setup
|
|
28
|
+
|
|
29
|
+
Vite with `@sveltejs/vite-plugin-svelte` is all it needs - the package carries the `svelte` export
|
|
30
|
+
condition. TypeScript consumers need `"moduleResolution": "bundler"` (or `node16`/`nodenext`).
|
|
31
|
+
|
|
32
|
+
When you link a local checkout of the kit (`npm link`, a workspace), keep a single Svelte:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
// vite.config.ts
|
|
36
|
+
export default defineConfig({
|
|
37
|
+
plugins: [svelte()],
|
|
38
|
+
resolve: {dedupe: ['svelte']},
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
// main.ts
|
|
46
|
+
import 'occulite-ui/app.css';
|
|
47
|
+
import {mount} from 'svelte';
|
|
48
|
+
import {addMessages, initI18n} from 'occulite-ui/i18n';
|
|
49
|
+
import {initTheme} from 'occulite-ui/theme';
|
|
50
|
+
|
|
51
|
+
import App from './App.svelte';
|
|
52
|
+
import {MESSAGES} from './messages.js';
|
|
53
|
+
|
|
54
|
+
addMessages(MESSAGES); // your strings, over the kit's; yours win
|
|
55
|
+
initI18n({storageKey: 'myapp.language'}); // follows openccu-lite's shell while embedded
|
|
56
|
+
initTheme({storageKey: 'myapp.theme'}); // 'system' | 'light' | 'dark'
|
|
57
|
+
|
|
58
|
+
mount(App, {target: document.getElementById('app')!});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
```svelte
|
|
62
|
+
<script lang="ts">
|
|
63
|
+
import {DataTable, Notices, Tabs, ThemeSwitch} from 'occulite-ui';
|
|
64
|
+
import {NoticesStore} from 'occulite-ui/notices';
|
|
65
|
+
import {t} from 'occulite-ui/i18n';
|
|
66
|
+
import {cycleTheme, theme} from 'occulite-ui/theme';
|
|
67
|
+
|
|
68
|
+
const notices = new NoticesStore();
|
|
69
|
+
let tab = $state('devices');
|
|
70
|
+
</script>
|
|
71
|
+
|
|
72
|
+
{#if !theme.followsShell}
|
|
73
|
+
<ThemeSwitch theme={theme.choice} onclick={cycleTheme} />
|
|
74
|
+
{/if}
|
|
75
|
+
<Tabs tabs={[{id: 'devices', label: t('Devices')}]} bind:active={tab} />
|
|
76
|
+
<DataTable rows={[]} columns={[{key: 'name', label: t('Name')}]} getId={(row) => row.id} />
|
|
77
|
+
<Notices notices={notices.visible} ondismiss={(id) => notices.dismiss(id)} />
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### What is in it
|
|
81
|
+
|
|
82
|
+
| Import | What |
|
|
83
|
+
| ------------------------------------------------ | ----------------------------------------------------------------------------------------------------- |
|
|
84
|
+
| `occulite-ui` | every component and helper below (tree-shakable) |
|
|
85
|
+
| `occulite-ui/components/<Name>` | one component |
|
|
86
|
+
| `occulite-ui/app.css` | `tokens.css` + `base.css`, import once |
|
|
87
|
+
| `occulite-ui/tokens.css`, `occulite-ui/base.css` | the custom properties alone; the global rules alone |
|
|
88
|
+
| `occulite-ui/i18n` | `t`, `has`, `translations`, `addMessages`, `initI18n`, `setLanguage`, `i18n.language`, the translator |
|
|
89
|
+
| `occulite-ui/theme` | `theme`, `initTheme`, `setTheme`, `cycleTheme`, `isDark`, `resolvedTheme`, `applyTheme` |
|
|
90
|
+
| `occulite-ui/embedding` | `ShellEmbedding`, `shellEmbedding()`, the contract's constants |
|
|
91
|
+
| `occulite-ui/table` | the table model, column widths, `LocalColumnWidths`, `setDataTableEnvironment`, `ROW_HEIGHT` |
|
|
92
|
+
| `occulite-ui/dialog` | `Dialog` and its geometry helpers |
|
|
93
|
+
| `occulite-ui/notices` | `NoticesStore` |
|
|
94
|
+
| `occulite-ui/icons` | icons as data (`IconData`), drawn by `Icon` and the toolbar buttons |
|
|
95
|
+
| `occulite-ui/format`, `occulite-ui/clipboard` | time and duration formatting; copying text |
|
|
96
|
+
|
|
97
|
+
Every visible text of a component is a prop; when it is absent, the component falls back to the
|
|
98
|
+
kit's catalogue in the page's language, and re-renders when the language changes.
|
|
99
|
+
|
|
100
|
+
A denser app sets `--hmm-row-height` to another height (26 px, say) and passes the same number to
|
|
101
|
+
`DataTable`'s `rowHeight`: the virtualiser needs it as a number.
|
|
102
|
+
|
|
103
|
+
### Inside openccu-lite's shell
|
|
104
|
+
|
|
105
|
+
The system frames an addon page as a same-origin iframe, puts `theme=` and `lang=` on the frame
|
|
106
|
+
URL and posts `{type: 'openccu-lite:theme', theme, lang}` to it on every change. With
|
|
107
|
+
`follow: 'shell'` (the default) `initTheme` and `initI18n` follow that, and `theme.followsShell`
|
|
108
|
+
tells a page to hide its own theme switch. A page opened on its own (a tab, a classic CCU, a
|
|
109
|
+
desktop app) keeps its own choice; `follow: 'none'` ignores the shell altogether. The kit reads the
|
|
110
|
+
shell's `ol-lang` cookie but never writes it.
|
|
111
|
+
|
|
112
|
+
### No browser dialogs
|
|
113
|
+
|
|
114
|
+
Never `window.confirm`, `prompt` or `alert`: they block the page, cannot be styled or translated
|
|
115
|
+
and are suppressed in some frames. Ask through a dialog, tell through a notice. The kit's own
|
|
116
|
+
eslint config bans the three; the same two rules in an app's config:
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
// eslint.config.js, in a config object's `rules`
|
|
120
|
+
'no-restricted-globals': ['error', 'confirm', 'prompt', 'alert'],
|
|
121
|
+
'no-restricted-properties': [
|
|
122
|
+
'error',
|
|
123
|
+
{object: 'window', property: 'confirm'},
|
|
124
|
+
{object: 'window', property: 'prompt'},
|
|
125
|
+
{object: 'window', property: 'alert'},
|
|
126
|
+
],
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Support
|
|
130
|
+
|
|
131
|
+
- Svelte `^5.40` (CI tests the floor and the current version), Node 22 and 24 for the tooling.
|
|
132
|
+
- Evergreen browsers (Chromium, Firefox, Safari of the last two years).
|
|
133
|
+
|
|
134
|
+
## Development
|
|
135
|
+
|
|
136
|
+
```sh
|
|
137
|
+
npm ci
|
|
138
|
+
npx playwright install --with-deps chromium # once: the component tests run in a real chromium
|
|
139
|
+
npm run lint && npm run typecheck && npm test
|
|
140
|
+
npm run build && npm run publint && npm run pack:check && npm run size
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
`npm run test:jsdom` runs the same tests without a browser, as a fallback; CI reports on the
|
|
144
|
+
browser run. `npm run size` builds three typical import sets with Vite and fails when one is more
|
|
145
|
+
than 10 % over `size-budget.json` (`node scripts/size-report.mjs --update` rewrites the budget).
|
|
146
|
+
|
|
147
|
+
## Releasing
|
|
148
|
+
|
|
149
|
+
The first publish is done by hand, because npm's trusted publishing can only be configured for a
|
|
150
|
+
package that exists:
|
|
151
|
+
|
|
152
|
+
1. On a clean checkout of `main` with green CI: `npm ci && npm test && npm run build && npm run pack:check`.
|
|
153
|
+
2. `npm login` (an account with publish rights; two-factor authentication on), then
|
|
154
|
+
`npm publish --access public`. The `prepack` script builds `dist/` again; the tarball is the one
|
|
155
|
+
`npm run pack:check` listed.
|
|
156
|
+
3. Tag the commit: `git tag -a v0.1.0 -m v0.1.0 && git push origin v0.1.0`, and create the GitHub
|
|
157
|
+
release with the title `v0.1.0` and the CHANGELOG section as its notes.
|
|
158
|
+
4. On npmjs.com, package settings → _Trusted publishing_: add GitHub Actions with the owner
|
|
159
|
+
`hobbyquaker`, the repository `occulite-ui` and the workflow `release.yml`. Then, under
|
|
160
|
+
_Publishing access_, require two-factor authentication and disallow tokens.
|
|
161
|
+
5. In the GitHub repository, set the Actions variable `NPM_TRUSTED_PUBLISHING` to `true`.
|
|
162
|
+
|
|
163
|
+
From then on a release is: bump `version` in `package.json`, move the CHANGELOG's section from
|
|
164
|
+
"unreleased" to the date, commit, push a `v<version>` tag. `release.yml` tests, checks that the tag
|
|
165
|
+
matches the version, publishes with provenance (`next` for a prerelease, `latest` otherwise) and
|
|
166
|
+
creates the GitHub release, titled with the tag.
|
|
167
|
+
|
|
168
|
+
## License
|
|
169
|
+
|
|
170
|
+
MIT, see [LICENSE](LICENSE). The code was written for homematic-manager and the openccu-lite
|
|
171
|
+
web interface by the same author and is relicensed MIT here. The vendored GitHub mark is listed in
|
|
172
|
+
[THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
occulite-ui is MIT licensed (see `LICENSE`). It ships no third-party code. It does contain one
|
|
4
|
+
vendored drawing:
|
|
5
|
+
|
|
6
|
+
## GitHub mark (Octicons `mark-github`)
|
|
7
|
+
|
|
8
|
+
`src/lib/icons/index.ts` (`iconGithub`) and `src/lib/components/GithubLink.svelte` draw the path of
|
|
9
|
+
Octicons' `mark-github`, from https://github.com/primer/octicons.
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
MIT License
|
|
13
|
+
|
|
14
|
+
Copyright (c) GitHub Inc.
|
|
15
|
+
|
|
16
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
17
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
18
|
+
in the Software without restriction, including without limitation the rights
|
|
19
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
20
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
21
|
+
furnished to do so, subject to the following conditions:
|
|
22
|
+
|
|
23
|
+
The above copyright notice and this permission notice shall be included in all
|
|
24
|
+
copies or substantial portions of the Software.
|
|
25
|
+
|
|
26
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
27
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
28
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
29
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
30
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
31
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
32
|
+
SOFTWARE.
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The GitHub name and mark are trademarks of GitHub, Inc.; the mark is used only to link to a
|
|
36
|
+
project's GitHub page.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A text onto the clipboard, from the copy button of a grid cell.
|
|
3
|
+
*
|
|
4
|
+
* `navigator.clipboard` exists only in a secure context - HTTPS, `localhost`, the Electron window.
|
|
5
|
+
* The CCU addon is usually opened over plain HTTP (`http://<ccu>/addons/hmm/`), where the property
|
|
6
|
+
* is simply missing, and an embedding frame without `clipboard-write` makes `writeText` refuse. So
|
|
7
|
+
* the old way is kept behind it: the text in a field of its own, selected, and the browser's `copy`
|
|
8
|
+
* command, which still works from the click that asked for it.
|
|
9
|
+
*/
|
|
10
|
+
/** Which way the text went: the Clipboard API, the selected field, or neither. */
|
|
11
|
+
export type CopyOutcome = 'clipboard' | 'selection' | 'failed';
|
|
12
|
+
export interface CopyOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Where the fallback's field is put for the moment of the copy - inside the grid, so a modal
|
|
15
|
+
* dialog or an `inert` elsewhere cannot stand between the field and the command. The body when
|
|
16
|
+
* absent.
|
|
17
|
+
*/
|
|
18
|
+
readonly host?: HTMLElement | undefined;
|
|
19
|
+
}
|
|
20
|
+
export declare function copyText(text: string, options?: CopyOptions): Promise<CopyOutcome>;
|
|
21
|
+
/**
|
|
22
|
+
* The fallback: a read-only field holding the text, selected, `document.execCommand('copy')`, and
|
|
23
|
+
* the field gone again. The focus goes back where it was - the copy button - so the grid's keyboard
|
|
24
|
+
* position is not lost.
|
|
25
|
+
*/
|
|
26
|
+
export declare function copyBySelection(text: string, host?: HTMLElement): boolean;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A text onto the clipboard, from the copy button of a grid cell.
|
|
3
|
+
*
|
|
4
|
+
* `navigator.clipboard` exists only in a secure context - HTTPS, `localhost`, the Electron window.
|
|
5
|
+
* The CCU addon is usually opened over plain HTTP (`http://<ccu>/addons/hmm/`), where the property
|
|
6
|
+
* is simply missing, and an embedding frame without `clipboard-write` makes `writeText` refuse. So
|
|
7
|
+
* the old way is kept behind it: the text in a field of its own, selected, and the browser's `copy`
|
|
8
|
+
* command, which still works from the click that asked for it.
|
|
9
|
+
*/
|
|
10
|
+
/** Read at the moment of the copy: the property is absent where the page is not a secure context. */
|
|
11
|
+
function clipboardOf() {
|
|
12
|
+
return globalThis.navigator?.clipboard;
|
|
13
|
+
}
|
|
14
|
+
export async function copyText(text, options = {}) {
|
|
15
|
+
const clipboard = clipboardOf();
|
|
16
|
+
if (typeof clipboard?.writeText === 'function') {
|
|
17
|
+
try {
|
|
18
|
+
await clipboard.writeText(text);
|
|
19
|
+
return 'clipboard';
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
// refused - no permission, a frame without clipboard-write: the selection below still can
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return copyBySelection(text, options.host) ? 'selection' : 'failed';
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The fallback: a read-only field holding the text, selected, `document.execCommand('copy')`, and
|
|
29
|
+
* the field gone again. The focus goes back where it was - the copy button - so the grid's keyboard
|
|
30
|
+
* position is not lost.
|
|
31
|
+
*/
|
|
32
|
+
export function copyBySelection(text, host = document.body) {
|
|
33
|
+
const owner = host.ownerDocument;
|
|
34
|
+
const previous = owner.activeElement;
|
|
35
|
+
const field = owner.createElement('textarea');
|
|
36
|
+
field.value = text;
|
|
37
|
+
field.readOnly = true;
|
|
38
|
+
field.tabIndex = -1;
|
|
39
|
+
field.setAttribute('aria-hidden', 'true');
|
|
40
|
+
// in the window, or `select()` does nothing in some browsers; invisible, and out of the layout
|
|
41
|
+
Object.assign(field.style, {
|
|
42
|
+
position: 'fixed',
|
|
43
|
+
top: '0',
|
|
44
|
+
left: '0',
|
|
45
|
+
width: '1px',
|
|
46
|
+
height: '1px',
|
|
47
|
+
padding: '0',
|
|
48
|
+
border: '0',
|
|
49
|
+
opacity: '0',
|
|
50
|
+
pointerEvents: 'none',
|
|
51
|
+
});
|
|
52
|
+
host.append(field);
|
|
53
|
+
try {
|
|
54
|
+
field.focus({ preventScroll: true });
|
|
55
|
+
field.select();
|
|
56
|
+
// iOS selects nothing in a read-only field without the explicit range
|
|
57
|
+
field.setSelectionRange(0, text.length);
|
|
58
|
+
// eslint-disable-next-line @typescript-eslint/no-deprecated -- the only copy there is outside a secure context
|
|
59
|
+
return owner.execCommand('copy');
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
field.remove();
|
|
66
|
+
if (previous instanceof HTMLElement || previous instanceof SVGElement) {
|
|
67
|
+
previous.focus({ preventScroll: true });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import {t as kitT} from '../i18n/i18n.svelte.js';
|
|
3
|
+
import {clampMenuPosition, type ContextMenuItem} from './contextMenu.js';
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
items: ContextMenuItem[];
|
|
7
|
+
open?: boolean;
|
|
8
|
+
x?: number;
|
|
9
|
+
y?: number;
|
|
10
|
+
onselect?: ((id: string) => void) | undefined;
|
|
11
|
+
onclose?: (() => void) | undefined;
|
|
12
|
+
label?: string | undefined;
|
|
13
|
+
testId?: string | undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let {
|
|
17
|
+
items,
|
|
18
|
+
open = $bindable(false),
|
|
19
|
+
x = 0,
|
|
20
|
+
y = 0,
|
|
21
|
+
onselect = undefined,
|
|
22
|
+
onclose = undefined,
|
|
23
|
+
label: labelProp = undefined,
|
|
24
|
+
testId = undefined,
|
|
25
|
+
}: Props = $props();
|
|
26
|
+
|
|
27
|
+
const label = $derived(labelProp ?? kitT('Menu'));
|
|
28
|
+
|
|
29
|
+
let menu = $state<HTMLDivElement | undefined>(undefined);
|
|
30
|
+
let position = $state({x: 0, y: 0});
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The per-row menu the 2.x grids reached through the pager buttons only. Everything it offers
|
|
34
|
+
* is also on the toolbar, so a browser that swallows the context menu loses nothing.
|
|
35
|
+
*/
|
|
36
|
+
$effect(() => {
|
|
37
|
+
if (!open || !menu) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const rect = menu.getBoundingClientRect();
|
|
41
|
+
position = clampMenuPosition(
|
|
42
|
+
x,
|
|
43
|
+
y,
|
|
44
|
+
{width: rect.width || 180, height: rect.height || 0},
|
|
45
|
+
{width: window.innerWidth, height: window.innerHeight},
|
|
46
|
+
);
|
|
47
|
+
const first = menu.querySelector<HTMLButtonElement>('button:not(:disabled)');
|
|
48
|
+
first?.focus();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
function close(): void {
|
|
52
|
+
open = false;
|
|
53
|
+
onclose?.();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function choose(item: ContextMenuItem): void {
|
|
57
|
+
if (item.disabled === true || item.separator === true) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
close();
|
|
61
|
+
onselect?.(item.id);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Escape closes the open menu, and only an open one. The listener is on the window for as long
|
|
66
|
+
* as the page is mounted; when it took every Escape, its `preventDefault` kept the key from the
|
|
67
|
+
* browser, a modal `<dialog>` never got its `cancel` event, and no dialog closed on Escape while
|
|
68
|
+
* the Devices or Links tab was shown.
|
|
69
|
+
*/
|
|
70
|
+
function onKeyDown(event: KeyboardEvent): void {
|
|
71
|
+
if (open && event.key === 'Escape') {
|
|
72
|
+
event.preventDefault();
|
|
73
|
+
close();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
</script>
|
|
77
|
+
|
|
78
|
+
<svelte:window
|
|
79
|
+
onkeydown={onKeyDown}
|
|
80
|
+
onmousedown={(event) => {
|
|
81
|
+
if (open && menu && event.target instanceof Node && !menu.contains(event.target)) {
|
|
82
|
+
close();
|
|
83
|
+
}
|
|
84
|
+
}}
|
|
85
|
+
/>
|
|
86
|
+
|
|
87
|
+
{#if open}
|
|
88
|
+
<div
|
|
89
|
+
class="hmm-context-menu"
|
|
90
|
+
bind:this={menu}
|
|
91
|
+
role="menu"
|
|
92
|
+
aria-label={label}
|
|
93
|
+
data-testid={testId}
|
|
94
|
+
style:left={`${position.x}px`}
|
|
95
|
+
style:top={`${position.y}px`}
|
|
96
|
+
>
|
|
97
|
+
{#each items as item (item.id)}
|
|
98
|
+
{#if item.separator === true}
|
|
99
|
+
<div class="hmm-context-separator" role="separator"></div>
|
|
100
|
+
{:else}
|
|
101
|
+
<button
|
|
102
|
+
type="button"
|
|
103
|
+
class="hmm-context-item"
|
|
104
|
+
class:hmm-context-danger={item.danger === true}
|
|
105
|
+
role="menuitem"
|
|
106
|
+
disabled={item.disabled === true}
|
|
107
|
+
onclick={() => choose(item)}
|
|
108
|
+
>
|
|
109
|
+
{item.label ?? item.id}
|
|
110
|
+
</button>
|
|
111
|
+
{/if}
|
|
112
|
+
{/each}
|
|
113
|
+
</div>
|
|
114
|
+
{/if}
|
|
115
|
+
|
|
116
|
+
<style>
|
|
117
|
+
.hmm-context-menu {
|
|
118
|
+
position: fixed;
|
|
119
|
+
z-index: 100;
|
|
120
|
+
min-width: 180px;
|
|
121
|
+
padding: 2px 0;
|
|
122
|
+
border: 1px solid var(--hmm-border-strong);
|
|
123
|
+
border-radius: var(--hmm-radius);
|
|
124
|
+
background: var(--hmm-bg);
|
|
125
|
+
box-shadow: var(--hmm-shadow-menu);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
.hmm-context-item {
|
|
129
|
+
display: block;
|
|
130
|
+
width: 100%;
|
|
131
|
+
padding: 3px 10px;
|
|
132
|
+
border: none;
|
|
133
|
+
background: none;
|
|
134
|
+
font: inherit;
|
|
135
|
+
text-align: left;
|
|
136
|
+
cursor: pointer;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
.hmm-context-item:hover:not(:disabled) {
|
|
140
|
+
background: var(--hmm-row-selected);
|
|
141
|
+
color: var(--hmm-row-selected-text);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.hmm-context-item:disabled {
|
|
145
|
+
opacity: 0.45;
|
|
146
|
+
cursor: default;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
.hmm-context-danger {
|
|
150
|
+
color: var(--hmm-error);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
.hmm-context-separator {
|
|
154
|
+
height: 1px;
|
|
155
|
+
margin: 2px 0;
|
|
156
|
+
background: var(--hmm-border-muted);
|
|
157
|
+
}
|
|
158
|
+
</style>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type ContextMenuItem } from './contextMenu.js';
|
|
2
|
+
interface Props {
|
|
3
|
+
items: ContextMenuItem[];
|
|
4
|
+
open?: boolean;
|
|
5
|
+
x?: number;
|
|
6
|
+
y?: number;
|
|
7
|
+
onselect?: ((id: string) => void) | undefined;
|
|
8
|
+
onclose?: (() => void) | undefined;
|
|
9
|
+
label?: string | undefined;
|
|
10
|
+
testId?: string | undefined;
|
|
11
|
+
}
|
|
12
|
+
declare const ContextMenu: import("svelte").Component<Props, {}, "open">;
|
|
13
|
+
type ContextMenu = ReturnType<typeof ContextMenu>;
|
|
14
|
+
export default ContextMenu;
|