@liberation-data/desk 0.36.1 → 0.38.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/README.md +53 -1
- package/dist/css/controls.css +98 -0
- package/dist/css/tokens.css +16 -0
- package/dist/css/windows.css +52 -0
- package/dist/desk.css +166 -0
- package/dist/react/codeBlock.d.ts +48 -0
- package/dist/react/codeBlock.d.ts.map +1 -0
- package/dist/react/codeBlock.js +257 -0
- package/dist/react/codeBlock.js.map +1 -0
- package/dist/react/index.d.ts +4 -0
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +2 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/popup.d.ts.map +1 -1
- package/dist/react/popup.js +24 -2
- package/dist/react/popup.js.map +1 -1
- package/dist/react/split.d.ts +25 -0
- package/dist/react/split.d.ts.map +1 -0
- package/dist/react/split.js +101 -0
- package/dist/react/split.js.map +1 -0
- package/llms.txt +15 -1
- package/package.json +1 -1
- package/src/css/controls.css +98 -0
- package/src/css/tokens.css +16 -0
- package/src/css/windows.css +52 -0
- package/src/react/codeBlock.tsx +378 -0
- package/src/react/index.ts +4 -0
- package/src/react/popup.tsx +24 -2
- package/src/react/split.tsx +161 -0
package/llms.txt
CHANGED
|
@@ -299,6 +299,10 @@ window; `lazyWindow` retries its import on reload, unlike `React.lazy`, which ca
|
|
|
299
299
|
<PaneHeader title={ReactNode} actions?={ReactNode} /> // the pane's title and its Refresh, on one row
|
|
300
300
|
<Section title={ReactNode} actions?={ReactNode}>{content}</Section> // a titled group inside a pane
|
|
301
301
|
<Toolbar label?={string} trailing?={ReactNode}>{controls}</Toolbar> // className="desk-grow" takes spare room
|
|
302
|
+
<Split side={ReactNode} at?={'start'|'end'} size?={280} min?={200} max?={'50%'} remember?={key} label?={string}>{rest}</Split>
|
|
303
|
+
// two sections of one app in one window, with a draggable edge between them (arrow keys, Home, Enter or
|
|
304
|
+
// double-click to reset); remember keeps the width in localStorage. Drag and drop between the sections is
|
|
305
|
+
// useDragSource/useDropTarget as anywhere else. One app, one window: use this, not two windows side by side.
|
|
302
306
|
<Page label?={string} fill?>{long text}</Page> // a sheet for text read at length, on --desk-page; fill = at least the room's height
|
|
303
307
|
// --desk-page-tint (4% light, 5% dark) is how much --desk-accent is mixed into the page; 0% for the neutral alone.
|
|
304
308
|
// text size by class, never px: desk-text-body | desk-text-small | desk-text-caption, plus desk-text-muted.
|
|
@@ -451,6 +455,15 @@ SearchCommand = 'desk.search' // a menu item can open i
|
|
|
451
455
|
// one caret button; summary sits beside it folded or open;
|
|
452
456
|
// the body stays mounted while folded, so fields keep their values
|
|
453
457
|
<Chip onRemove? removeLabel? className?>{a word or two}</Chip> // one thing set or chosen; × only with onRemove
|
|
458
|
+
<CodeBlock code language?={'python'|'typescript'|'javascript'|'kotlin'|'yaml'|'shell'|'json'|'text'}
|
|
459
|
+
label? copy?={true} highlight?={(code, language) => ReactNode} className? />
|
|
460
|
+
// code people copy: scrolls sideways, a named region that takes focus;
|
|
461
|
+
// Copy → "Copied", announced; no clipboard → selects it, says ⌘C/Ctrl+C;
|
|
462
|
+
// built-in highlighting as React nodes, never HTML; highlight replaces it
|
|
463
|
+
<CodeBlock variants={{value,label,language,code}[]} value? onValueChange? label? copy? highlight? />
|
|
464
|
+
// the same code in several forms: tabs (a small segmented control) and
|
|
465
|
+
// Copy in one bar across the block's top; Copy copies the one shown;
|
|
466
|
+
// uncontrolled by default; label names the tabs and is not drawn
|
|
454
467
|
```
|
|
455
468
|
|
|
456
469
|
### Waiting
|
|
@@ -576,7 +589,8 @@ Every value reads a `--desk-*` token; redefine tokens, never override selectors.
|
|
|
576
589
|
`--desk-ok|warn|bad`, `--desk-on-accent`. Spacing `--desk-space-1..7` = 4, 8, 12, 16, 24, 32, 48 px. Also
|
|
577
590
|
`--desk-radius`, `--desk-control-height`, `--desk-control-radius`, `--desk-font`, `--desk-font-mono`,
|
|
578
591
|
`--desk-ease`, `--desk-dock-size`, `--desk-dock-glass`, `--desk-dock-offset`, `--desk-dock-room`,
|
|
579
|
-
`--desk-inset-bottom`, `--desk-wizard-height`, `--desk-field-gap`.
|
|
592
|
+
`--desk-inset-bottom`, `--desk-wizard-height`, `--desk-field-gap`. Code colours (`CodeBlock`, on `--desk-page`):
|
|
593
|
+
`--desk-code-comment|string|keyword|number|key`, 4.5:1 in light and dark.
|
|
580
594
|
|
|
581
595
|
Light and dark follow `prefers-color-scheme`; `data-theme="light" | "dark"` on the root wins. The look is
|
|
582
596
|
`data-look="mac" | "gnome" | "windows"` on the root, set by the desk; don't set it yourself — pass `look`.
|
package/package.json
CHANGED
package/src/css/controls.css
CHANGED
|
@@ -705,7 +705,11 @@
|
|
|
705
705
|
z-index: 1200;
|
|
706
706
|
top: calc(100% + 4px);
|
|
707
707
|
left: 0;
|
|
708
|
+
/* As wide as the widest option, as a menu is; never narrower than the button, nor past the screen. */
|
|
709
|
+
width: max-content;
|
|
708
710
|
min-width: 100%;
|
|
711
|
+
max-width: min(360px, calc(100vw - 32px));
|
|
712
|
+
box-sizing: border-box;
|
|
709
713
|
max-height: 40vh;
|
|
710
714
|
margin: 0;
|
|
711
715
|
padding: 4px;
|
|
@@ -754,6 +758,12 @@
|
|
|
754
758
|
.desk-popup-text {
|
|
755
759
|
display: flex;
|
|
756
760
|
flex-direction: column;
|
|
761
|
+
min-width: 0;
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/* An option is one line; its description may wrap inside the list's widest. */
|
|
765
|
+
.desk-popup-label {
|
|
766
|
+
white-space: nowrap;
|
|
757
767
|
}
|
|
758
768
|
|
|
759
769
|
/* ── Tabs ── */
|
|
@@ -1484,3 +1494,91 @@
|
|
|
1484
1494
|
transition: none;
|
|
1485
1495
|
}
|
|
1486
1496
|
}
|
|
1497
|
+
|
|
1498
|
+
/*
|
|
1499
|
+
* CODE TO COPY (`CodeBlock`). On the page's ground, which is what the code tokens are measured
|
|
1500
|
+
* against, with Copy in a column of its own so it never sits over a line. Lines keep their length
|
|
1501
|
+
* and the block scrolls sideways: a wrapped command reads as two.
|
|
1502
|
+
*/
|
|
1503
|
+
.desk-code {
|
|
1504
|
+
display: flex;
|
|
1505
|
+
align-items: flex-start;
|
|
1506
|
+
min-width: 0;
|
|
1507
|
+
border: 1px solid var(--desk-rule);
|
|
1508
|
+
border-radius: var(--desk-control-radius);
|
|
1509
|
+
background: color-mix(in srgb, var(--desk-accent) var(--desk-page-tint), var(--desk-page));
|
|
1510
|
+
color: var(--desk-ink);
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
.desk-code-main {
|
|
1514
|
+
flex: 1;
|
|
1515
|
+
min-width: 0;
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
.desk-code-label {
|
|
1519
|
+
padding: var(--desk-space-2) var(--desk-space-3) 0;
|
|
1520
|
+
color: var(--desk-muted);
|
|
1521
|
+
font-family: var(--desk-font);
|
|
1522
|
+
font-size: var(--desk-text-caption);
|
|
1523
|
+
font-weight: 500;
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
.desk-code-body {
|
|
1527
|
+
margin: 0;
|
|
1528
|
+
padding: var(--desk-space-2) var(--desk-space-3);
|
|
1529
|
+
overflow-x: auto;
|
|
1530
|
+
border-radius: var(--desk-control-radius);
|
|
1531
|
+
font-family: var(--desk-font-mono);
|
|
1532
|
+
font-size: var(--desk-text-small);
|
|
1533
|
+
line-height: 1.6;
|
|
1534
|
+
white-space: pre;
|
|
1535
|
+
tab-size: 4;
|
|
1536
|
+
}
|
|
1537
|
+
|
|
1538
|
+
.desk-code-body:focus-visible {
|
|
1539
|
+
outline: 2px solid var(--desk-accent);
|
|
1540
|
+
outline-offset: -2px;
|
|
1541
|
+
}
|
|
1542
|
+
|
|
1543
|
+
.desk-code-actions {
|
|
1544
|
+
flex: none;
|
|
1545
|
+
padding: var(--desk-space-1);
|
|
1546
|
+
}
|
|
1547
|
+
|
|
1548
|
+
/* With variants the block is a column: a bar of tabs and Copy across its top, the code under it. */
|
|
1549
|
+
.desk-code[data-variants] {
|
|
1550
|
+
flex-direction: column;
|
|
1551
|
+
align-items: stretch;
|
|
1552
|
+
}
|
|
1553
|
+
|
|
1554
|
+
.desk-code-head {
|
|
1555
|
+
display: flex;
|
|
1556
|
+
align-items: center;
|
|
1557
|
+
flex-wrap: wrap;
|
|
1558
|
+
justify-content: space-between;
|
|
1559
|
+
gap: var(--desk-space-2);
|
|
1560
|
+
padding: var(--desk-space-1);
|
|
1561
|
+
border-bottom: 1px solid var(--desk-rule);
|
|
1562
|
+
border-radius: var(--desk-control-radius) var(--desk-control-radius) 0 0;
|
|
1563
|
+
background: color-mix(in srgb, var(--desk-ink) 4%, transparent);
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
.desk-code-head .desk-code-actions {
|
|
1567
|
+
padding: 0;
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
.desk-code-status {
|
|
1571
|
+
position: absolute;
|
|
1572
|
+
width: 1px;
|
|
1573
|
+
height: 1px;
|
|
1574
|
+
margin: -1px;
|
|
1575
|
+
overflow: hidden;
|
|
1576
|
+
clip-path: inset(50%);
|
|
1577
|
+
white-space: nowrap;
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
.desk-code-comment { color: var(--desk-code-comment); font-style: italic; }
|
|
1581
|
+
.desk-code-string { color: var(--desk-code-string); }
|
|
1582
|
+
.desk-code-keyword { color: var(--desk-code-keyword); }
|
|
1583
|
+
.desk-code-number { color: var(--desk-code-number); }
|
|
1584
|
+
.desk-code-key { color: var(--desk-code-key); }
|
package/src/css/tokens.css
CHANGED
|
@@ -67,6 +67,12 @@
|
|
|
67
67
|
/* How much of the accent is mixed into it, so the sheet belongs to the theme it is read in.
|
|
68
68
|
0% is the neutral alone. */
|
|
69
69
|
--desk-page-tint: 4%;
|
|
70
|
+
/* Code (`CodeBlock`), on the page's ground: each at least 4.5:1 against it, tinted or not. */
|
|
71
|
+
--desk-code-comment: #5d6676;
|
|
72
|
+
--desk-code-string: #136c2e;
|
|
73
|
+
--desk-code-keyword: #8034c8;
|
|
74
|
+
--desk-code-number: #a34400;
|
|
75
|
+
--desk-code-key: #0b5cad;
|
|
70
76
|
--desk-on-accent: #ffffff;
|
|
71
77
|
--desk-ease: cubic-bezier(.32, .72, 0, 1);
|
|
72
78
|
--desk-dock-size: 48px;
|
|
@@ -106,6 +112,11 @@
|
|
|
106
112
|
--desk-field-bg: rgb(0 0 0 / 35%);
|
|
107
113
|
--desk-page: #26282c;
|
|
108
114
|
--desk-page-tint: 5%;
|
|
115
|
+
--desk-code-comment: #9aa3b2;
|
|
116
|
+
--desk-code-string: #7ee2a0;
|
|
117
|
+
--desk-code-keyword: #d2a8ff;
|
|
118
|
+
--desk-code-number: #ffab70;
|
|
119
|
+
--desk-code-key: #7cc4ff;
|
|
109
120
|
}
|
|
110
121
|
}
|
|
111
122
|
|
|
@@ -132,4 +143,9 @@
|
|
|
132
143
|
--desk-field-bg: rgb(0 0 0 / 35%);
|
|
133
144
|
--desk-page: #26282c;
|
|
134
145
|
--desk-page-tint: 5%;
|
|
146
|
+
--desk-code-comment: #9aa3b2;
|
|
147
|
+
--desk-code-string: #7ee2a0;
|
|
148
|
+
--desk-code-keyword: #d2a8ff;
|
|
149
|
+
--desk-code-number: #ffab70;
|
|
150
|
+
--desk-code-key: #7cc4ff;
|
|
135
151
|
}
|
package/src/css/windows.css
CHANGED
|
@@ -401,6 +401,58 @@
|
|
|
401
401
|
|
|
402
402
|
/* ── The inside of a window: controls that stay, content that scrolls ── */
|
|
403
403
|
|
|
404
|
+
/* Two panels in one window, and the edge between them that moves. */
|
|
405
|
+
.desk-split {
|
|
406
|
+
display: grid;
|
|
407
|
+
grid-template-columns: var(--desk-split-size) auto minmax(0, 1fr);
|
|
408
|
+
height: 100%;
|
|
409
|
+
min-height: 0;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
.desk-split[data-at="end"] {
|
|
413
|
+
grid-template-columns: minmax(0, 1fr) auto var(--desk-split-size);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
.desk-split[data-dragging] {
|
|
417
|
+
user-select: none;
|
|
418
|
+
cursor: col-resize;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
.desk-split-panel {
|
|
422
|
+
display: flex;
|
|
423
|
+
flex-direction: column;
|
|
424
|
+
min-width: 0;
|
|
425
|
+
min-height: 0;
|
|
426
|
+
overflow: hidden;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/* A hairline to see, and a wider strip either side of it to catch. */
|
|
430
|
+
.desk-split-edge {
|
|
431
|
+
position: relative;
|
|
432
|
+
width: 1px;
|
|
433
|
+
background: var(--desk-rule);
|
|
434
|
+
cursor: col-resize;
|
|
435
|
+
touch-action: none;
|
|
436
|
+
outline: none;
|
|
437
|
+
/* Above the panels, so the strip it catches with is not covered by something positioned in them. */
|
|
438
|
+
z-index: 1;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/* 13px to catch with: a hairline you can see, and enough either side that you need not aim for it. */
|
|
442
|
+
.desk-split-edge::before {
|
|
443
|
+
content: "";
|
|
444
|
+
position: absolute;
|
|
445
|
+
inset: 0 -6px;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
.desk-split-edge:hover,
|
|
449
|
+
.desk-split-edge:focus-visible,
|
|
450
|
+
.desk-split[data-dragging] > .desk-split-edge {
|
|
451
|
+
background: var(--desk-accent);
|
|
452
|
+
box-shadow: 0 0 0 1px var(--desk-accent);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
|
|
404
456
|
.desk-pane {
|
|
405
457
|
display: flex;
|
|
406
458
|
flex-direction: column;
|
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from 'react'
|
|
2
|
+
import type { ReactNode } from 'react'
|
|
3
|
+
import { formatShortcut } from '../core/shortcuts.js'
|
|
4
|
+
import { Button, SegmentedControl } from './controls.js'
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* CODE PEOPLE COPY: a snippet to paste into a program, a command to run, a file to save.
|
|
8
|
+
*
|
|
9
|
+
* It is read exactly, so it keeps its own lines — a long one scrolls sideways rather than wrapping,
|
|
10
|
+
* because a wrapped command reads as two — and the block is a named region that takes focus, so it
|
|
11
|
+
* scrolls from the keyboard. Copy sits in the corner beside the code, never over it, and is always
|
|
12
|
+
* shown: a button that appears on hover is one a keyboard and a touch screen never find.
|
|
13
|
+
*
|
|
14
|
+
* HIGHLIGHTING IS SMALL ON PURPOSE. Comments, strings, keywords, numbers and the keys of YAML and
|
|
15
|
+
* JSON, from a scanner a few rules long per language: enough to read a snippet by, and no
|
|
16
|
+
* dependency. It is a reading aid, not a parser — a regular expression literal or a heredoc is read
|
|
17
|
+
* as code, and a block comment left open colours its own line rather than the rest of the file — and
|
|
18
|
+
* a host with a real highlighter passes `highlight` instead.
|
|
19
|
+
*
|
|
20
|
+
* NOTHING IS PARSED AS HTML. The scanner cuts the code into strings and React renders them, so a
|
|
21
|
+
* `<script>` in a snippet is text in the block, whatever the language or the highlighter.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export type CodeLanguage = 'python' | 'typescript' | 'javascript' | 'kotlin' | 'yaml' | 'shell' | 'json' | 'text'
|
|
25
|
+
|
|
26
|
+
/** One of the forms the same code comes in: the same install with pip or with uv, a client in two languages. */
|
|
27
|
+
export interface CodeVariant {
|
|
28
|
+
readonly value: string
|
|
29
|
+
/** Its tab: "Python", "pip". */
|
|
30
|
+
readonly label: string
|
|
31
|
+
readonly language: CodeLanguage
|
|
32
|
+
readonly code: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface CodeBlockCommon {
|
|
36
|
+
/**
|
|
37
|
+
* What it is: "Install", "config.yaml". It names the region and, without variants, is shown above
|
|
38
|
+
* the code; with them the tabs take that place, and it names them.
|
|
39
|
+
*/
|
|
40
|
+
readonly label?: string
|
|
41
|
+
/** A Copy button in the corner. Default true. */
|
|
42
|
+
readonly copy?: boolean
|
|
43
|
+
/** A highlighter of the host's own, e.g. CodeMirror's `runMode`, used instead of the built-in one. */
|
|
44
|
+
readonly highlight?: (code: string, language: CodeLanguage) => ReactNode
|
|
45
|
+
readonly className?: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface OneCode extends CodeBlockCommon {
|
|
49
|
+
/** The code, exactly as it is copied. One trailing newline is not drawn as an empty line. */
|
|
50
|
+
readonly code: string
|
|
51
|
+
/** How it is highlighted, and the region's name when there is no `label`. Default `text`: none. */
|
|
52
|
+
readonly language?: CodeLanguage
|
|
53
|
+
readonly variants?: never
|
|
54
|
+
readonly value?: never
|
|
55
|
+
readonly onValueChange?: never
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface VariantCode extends CodeBlockCommon {
|
|
59
|
+
/**
|
|
60
|
+
* The same code in several forms, chosen by tabs in a bar across the top of the block: one card,
|
|
61
|
+
* not a control above it. Copy copies the one shown.
|
|
62
|
+
*/
|
|
63
|
+
readonly variants: readonly CodeVariant[]
|
|
64
|
+
/** The variant shown. Leave it out and the block keeps its own choice, starting at the first. */
|
|
65
|
+
readonly value?: string
|
|
66
|
+
readonly onValueChange?: (value: string) => void
|
|
67
|
+
readonly code?: never
|
|
68
|
+
readonly language?: never
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type CodeBlockProps = OneCode | VariantCode
|
|
72
|
+
|
|
73
|
+
type Kind = 'comment' | 'string' | 'keyword' | 'number' | 'key'
|
|
74
|
+
interface Token { readonly kind?: Kind | undefined; readonly text: string }
|
|
75
|
+
|
|
76
|
+
/* A quote: how it opens, whether it may run over lines, and how it says its own quote inside. */
|
|
77
|
+
interface Quote { readonly open: string; readonly lines?: boolean; readonly escape?: 'backslash' | 'double' }
|
|
78
|
+
|
|
79
|
+
interface Grammar {
|
|
80
|
+
readonly line?: string
|
|
81
|
+
/** `#` starts a comment only at the start of a word, so `$#` and `a#b` are not one (shell, YAML). */
|
|
82
|
+
readonly wordComment?: boolean
|
|
83
|
+
/** A quote opens only where a value can start, so an apostrophe inside a word or a sentence is text. */
|
|
84
|
+
readonly quoteAt?: (code: string, at: number) => boolean
|
|
85
|
+
readonly block?: readonly [string, string]
|
|
86
|
+
readonly quotes: readonly Quote[]
|
|
87
|
+
readonly keywords: ReadonlySet<string>
|
|
88
|
+
readonly keys?: 'yaml' | 'json'
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const words = (list: string) => new Set(list.split(' '))
|
|
92
|
+
const C_COMMENTS = { line: '//', block: ['/*', '*/'] } as const
|
|
93
|
+
const B: Quote['escape'] = 'backslash'
|
|
94
|
+
const JS = 'async await break case catch class const continue debugger default delete do else export extends false ' +
|
|
95
|
+
'finally for function if import in instanceof let new null of return static super switch this throw true try ' +
|
|
96
|
+
'typeof undefined var void while with yield'
|
|
97
|
+
|
|
98
|
+
const GRAMMARS: Partial<Record<CodeLanguage, Grammar>> = {
|
|
99
|
+
python: {
|
|
100
|
+
line: '#',
|
|
101
|
+
quotes: [{ open: '"""', lines: true, escape: B }, { open: "'''", lines: true, escape: B }, { open: '"', escape: B }, { open: "'", escape: B }],
|
|
102
|
+
keywords: words('False None True and as assert async await break class continue def del elif else except ' +
|
|
103
|
+
'finally for from global if import in is lambda nonlocal not or pass raise return try while with yield'),
|
|
104
|
+
},
|
|
105
|
+
javascript: {
|
|
106
|
+
...C_COMMENTS,
|
|
107
|
+
quotes: [{ open: '`', lines: true, escape: B }, { open: '"', escape: B }, { open: "'", escape: B }],
|
|
108
|
+
keywords: words(JS),
|
|
109
|
+
},
|
|
110
|
+
typescript: {
|
|
111
|
+
...C_COMMENTS,
|
|
112
|
+
quotes: [{ open: '`', lines: true, escape: B }, { open: '"', escape: B }, { open: "'", escape: B }],
|
|
113
|
+
keywords: words(`${JS} abstract as declare enum implements interface keyof namespace private protected public readonly satisfies`),
|
|
114
|
+
},
|
|
115
|
+
kotlin: {
|
|
116
|
+
...C_COMMENTS,
|
|
117
|
+
quotes: [{ open: '"""', lines: true }, { open: '"', escape: B }, { open: "'", escape: B }],
|
|
118
|
+
keywords: words('abstract as break by class companion const constructor continue data do else enum false for fun ' +
|
|
119
|
+
'if import in init interface internal is lateinit null object open override package private protected public ' +
|
|
120
|
+
'return sealed super suspend this throw true try typealias val var when while'),
|
|
121
|
+
},
|
|
122
|
+
shell: {
|
|
123
|
+
line: '#',
|
|
124
|
+
wordComment: true,
|
|
125
|
+
quotes: [{ open: '"', lines: true, escape: B }, { open: "'", lines: true }],
|
|
126
|
+
// `Don't panic` in a heredoc or an unquoted echo is not the start of a string.
|
|
127
|
+
quoteAt: (code, at) => !/[\w]/.test(code[at - 1] ?? ''),
|
|
128
|
+
keywords: words('case do done elif else esac export fi for function if in local readonly return select then until while'),
|
|
129
|
+
},
|
|
130
|
+
yaml: {
|
|
131
|
+
line: '#',
|
|
132
|
+
wordComment: true,
|
|
133
|
+
quotes: [{ open: '"', lines: true, escape: B }, { open: "'", lines: true, escape: 'double' }],
|
|
134
|
+
keywords: words('true false null'),
|
|
135
|
+
quoteAt: (code, at) => yamlValueStarts(code, at),
|
|
136
|
+
keys: 'yaml',
|
|
137
|
+
},
|
|
138
|
+
json: {
|
|
139
|
+
quotes: [{ open: '"', escape: B }],
|
|
140
|
+
keywords: words('true false null'),
|
|
141
|
+
keys: 'json',
|
|
142
|
+
},
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const NUMBER = /0[xX][\da-fA-F_]+|\d[\d_]*(?:\.\d[\d_]*)?(?:[eE][+-]?\d+)?[fFLn]?/y
|
|
146
|
+
const WORD = /[A-Za-z_$][\w$]*/y
|
|
147
|
+
const JSON_COLON = /[ \t]*:/y
|
|
148
|
+
/* Indentation and list dashes, a key — plain or quoted — and its colon, which ends at a space or the line. */
|
|
149
|
+
const YAML_KEY = /([ \t]*(?:-[ \t]+)*)([^\s#'"{}[\],:-](?:[^:\n#]*[^\s:#])?|"(?:[^"\\\n]|\\.)*"|'(?:[^'\n]|'')*')([ \t]*:)(?=[ \t]|$)/my
|
|
150
|
+
/* What a number or keyword cannot touch, so `v1`, `1.2.3`, `obj.if` and `--if-exists` stay text. */
|
|
151
|
+
const JOINED = /[\w$.-]/
|
|
152
|
+
|
|
153
|
+
function sticky(pattern: RegExp, code: string, at: number): RegExpExecArray | null {
|
|
154
|
+
pattern.lastIndex = at
|
|
155
|
+
return pattern.exec(code)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Where a quote that opens at `at` closes: past its closing quote, or at the line or text's end. */
|
|
159
|
+
function closeOf(code: string, at: number, quote: Quote): number {
|
|
160
|
+
let i = at + quote.open.length
|
|
161
|
+
while (i < code.length) {
|
|
162
|
+
if (quote.escape === 'backslash' && code[i] === '\\') i += 2
|
|
163
|
+
else if (code.startsWith(quote.open, i)) {
|
|
164
|
+
if (quote.escape === 'double' && code[i + 1] === quote.open) i += 2
|
|
165
|
+
else {
|
|
166
|
+
// A raw string may end in its own quote: `"""a""""` closes at the last of the run.
|
|
167
|
+
let end = i + quote.open.length
|
|
168
|
+
while (quote.open.length === 3 && code[end] === quote.open[0]) end++
|
|
169
|
+
return end
|
|
170
|
+
}
|
|
171
|
+
} else if (code[i] === '\n' && !quote.lines) return i
|
|
172
|
+
else i++
|
|
173
|
+
}
|
|
174
|
+
return code.length
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/* YAML opens a quote only where a value starts: `title: Don't stop` is a plain sentence. */
|
|
178
|
+
function yamlValueStarts(code: string, at: number): boolean {
|
|
179
|
+
let i = at - 1
|
|
180
|
+
while (code[i] === ' ' || code[i] === '\t') i--
|
|
181
|
+
return i < 0 || ':-[{,\n'.includes(code[i] ?? '')
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function tokenize(code: string, grammar: Grammar): Token[] {
|
|
185
|
+
const out: Token[] = []
|
|
186
|
+
const push = (text = '', kind?: Kind) => {
|
|
187
|
+
if (!text) return
|
|
188
|
+
const last = out[out.length - 1]
|
|
189
|
+
if (!kind && last && !last.kind) out[out.length - 1] = { text: last.text + text }
|
|
190
|
+
else out.push({ kind, text })
|
|
191
|
+
}
|
|
192
|
+
let i = 0
|
|
193
|
+
while (i < code.length) {
|
|
194
|
+
const before = code[i - 1]
|
|
195
|
+
const lineStart = i === 0 || before === '\n'
|
|
196
|
+
if (lineStart && grammar.keys === 'yaml') {
|
|
197
|
+
const key = sticky(YAML_KEY, code, i)
|
|
198
|
+
if (key) {
|
|
199
|
+
push(key[1])
|
|
200
|
+
push(key[2], 'key')
|
|
201
|
+
push(key[3])
|
|
202
|
+
i += key[0].length
|
|
203
|
+
continue
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (grammar.block && code.startsWith(grammar.block[0], i)) {
|
|
207
|
+
const end = code.indexOf(grammar.block[1], i + 2)
|
|
208
|
+
// Never closed: most likely a `/*` inside a regular expression, so it colours one line, not the file.
|
|
209
|
+
const open = code.indexOf('\n', i)
|
|
210
|
+
const stop = end >= 0 ? end + grammar.block[1].length : open < 0 ? code.length : open
|
|
211
|
+
push(code.slice(i, stop), 'comment')
|
|
212
|
+
i = stop
|
|
213
|
+
continue
|
|
214
|
+
}
|
|
215
|
+
if (grammar.line && code.startsWith(grammar.line, i) && (!grammar.wordComment || lineStart || /\s/.test(before ?? ''))) {
|
|
216
|
+
const end = code.indexOf('\n', i)
|
|
217
|
+
const stop = end < 0 ? code.length : end
|
|
218
|
+
push(code.slice(i, stop), 'comment')
|
|
219
|
+
i = stop
|
|
220
|
+
continue
|
|
221
|
+
}
|
|
222
|
+
const quote = grammar.quotes.find(q => code.startsWith(q.open, i))
|
|
223
|
+
if (quote && (!grammar.quoteAt || grammar.quoteAt(code, i))) {
|
|
224
|
+
const stop = closeOf(code, i, quote)
|
|
225
|
+
const isKey = grammar.keys === 'json' && sticky(JSON_COLON, code, stop) !== null
|
|
226
|
+
push(code.slice(i, stop), isKey ? 'key' : 'string')
|
|
227
|
+
i = stop
|
|
228
|
+
continue
|
|
229
|
+
}
|
|
230
|
+
const loose = before === undefined || !JOINED.test(before)
|
|
231
|
+
// A minus is a sign where nothing it could join sits before it: `-1`, but not the 10 of `2026-10`.
|
|
232
|
+
const signed = before === '-' && !JOINED.test(code[i - 2] ?? ' ')
|
|
233
|
+
const number = loose || signed ? sticky(NUMBER, code, i) : null
|
|
234
|
+
if (number && !JOINED.test(code[i + number[0].length] ?? '')) {
|
|
235
|
+
push(number[0], 'number')
|
|
236
|
+
i += number[0].length
|
|
237
|
+
continue
|
|
238
|
+
}
|
|
239
|
+
const word = sticky(WORD, code, i)
|
|
240
|
+
if (word) {
|
|
241
|
+
const keyword = loose && grammar.keywords.has(word[0]) && code[i + word[0].length] !== '-'
|
|
242
|
+
push(word[0], keyword ? 'keyword' : undefined)
|
|
243
|
+
i += word[0].length
|
|
244
|
+
continue
|
|
245
|
+
}
|
|
246
|
+
push(code[i])
|
|
247
|
+
i++
|
|
248
|
+
}
|
|
249
|
+
return out
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function highlighted(code: string, language: CodeLanguage): ReactNode {
|
|
253
|
+
const grammar = GRAMMARS[language]
|
|
254
|
+
if (!grammar) return code
|
|
255
|
+
return tokenize(code, grammar).map((token, n) =>
|
|
256
|
+
token.kind ? <span key={n} className={`desk-code-${token.kind}`}>{token.text}</span> : token.text,
|
|
257
|
+
)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const NAMES: Record<CodeLanguage, string> = {
|
|
261
|
+
python: 'Python', typescript: 'TypeScript', javascript: 'JavaScript', kotlin: 'Kotlin', yaml: 'YAML',
|
|
262
|
+
shell: 'Shell', json: 'JSON', text: '',
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** How long "Copied" stays before the button says Copy again. */
|
|
266
|
+
const COPIED_MS = 2000
|
|
267
|
+
|
|
268
|
+
type Copying = 'idle' | 'copied' | 'select'
|
|
269
|
+
|
|
270
|
+
/** A framed block of code to copy, highlighted for its language, with a Copy button in its corner. */
|
|
271
|
+
export function CodeBlock(props: CodeBlockProps) {
|
|
272
|
+
const { label, copy = true, highlight, className, variants, value, onValueChange } = props
|
|
273
|
+
const [own, setOwn] = useState(variants?.[0]?.value)
|
|
274
|
+
const variant = variants && (variants.find(v => v.value === (value ?? own)) ?? variants[0])
|
|
275
|
+
const choose = (next: string) => {
|
|
276
|
+
if (value === undefined) setOwn(next)
|
|
277
|
+
onValueChange?.(next)
|
|
278
|
+
}
|
|
279
|
+
const code = (variants ? variant?.code : props.code) ?? ''
|
|
280
|
+
const language: CodeLanguage = (variants ? variant?.language : props.language) ?? 'text'
|
|
281
|
+
const body = useRef<HTMLElement>(null)
|
|
282
|
+
const [copying, setCopying] = useState<Copying>('idle')
|
|
283
|
+
const timer = useRef<ReturnType<typeof setTimeout>>(undefined)
|
|
284
|
+
const alive = useRef(true)
|
|
285
|
+
const copied = useRef(code)
|
|
286
|
+
const shown = code.endsWith('\n') ? code.slice(0, -1) : code
|
|
287
|
+
const content = useMemo(() => (highlight ?? highlighted)(shown, language), [shown, language, highlight])
|
|
288
|
+
|
|
289
|
+
useEffect(() => {
|
|
290
|
+
alive.current = true
|
|
291
|
+
return () => {
|
|
292
|
+
alive.current = false
|
|
293
|
+
clearTimeout(timer.current)
|
|
294
|
+
}
|
|
295
|
+
}, [])
|
|
296
|
+
// Copied or selected says something about this code; new code has been neither. Put back while
|
|
297
|
+
// rendering, so not one frame says Copied over code nobody copied.
|
|
298
|
+
if (copied.current !== code) {
|
|
299
|
+
copied.current = code
|
|
300
|
+
clearTimeout(timer.current)
|
|
301
|
+
if (copying !== 'idle') setCopying('idle')
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const settle = (next: Copying) => {
|
|
305
|
+
// A copy that finishes after the block has gone has nothing left to say it in.
|
|
306
|
+
if (!alive.current) return
|
|
307
|
+
clearTimeout(timer.current)
|
|
308
|
+
setCopying(next)
|
|
309
|
+
if (next === 'copied') timer.current = setTimeout(() => setCopying('idle'), COPIED_MS)
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/*
|
|
313
|
+
* THE CLIPBOARD IS NOT ALWAYS THERE: a page over plain http has none, and a browser may refuse
|
|
314
|
+
* it. Then the code is selected and the older copy command tried; where that fails too, the
|
|
315
|
+
* selection stays and the button says which keys copy it, so the person is one keystroke away.
|
|
316
|
+
* The selection is the code as drawn: without the trailing newline, and with whatever a host's
|
|
317
|
+
* `highlight` adds to it.
|
|
318
|
+
*/
|
|
319
|
+
const onCopy = async () => {
|
|
320
|
+
try {
|
|
321
|
+
await navigator.clipboard.writeText(code)
|
|
322
|
+
settle('copied')
|
|
323
|
+
} catch {
|
|
324
|
+
const selection = window.getSelection()
|
|
325
|
+
if (body.current && selection) selection.selectAllChildren(body.current)
|
|
326
|
+
let copied = false
|
|
327
|
+
try {
|
|
328
|
+
copied = document.execCommand('copy')
|
|
329
|
+
} catch {
|
|
330
|
+
// Unsupported: the selection is the way.
|
|
331
|
+
}
|
|
332
|
+
settle(copied ? 'copied' : 'select')
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const keys = formatShortcut('mod+c')
|
|
337
|
+
const name = variant
|
|
338
|
+
? label ? `${label}, ${variant.label}` : `${variant.label} code`
|
|
339
|
+
: label ?? (NAMES[language] ? `${NAMES[language]} code` : 'Code')
|
|
340
|
+
const actions = copy && (
|
|
341
|
+
<div className="desk-code-actions">
|
|
342
|
+
<Button intent="quiet" size="small" onClick={onCopy}>
|
|
343
|
+
{copying === 'copied' ? 'Copied' : copying === 'select' ? `Press ${keys}` : 'Copy'}
|
|
344
|
+
</Button>
|
|
345
|
+
<span className="desk-code-status" role="status">
|
|
346
|
+
{copying === 'copied' ? 'Copied' : copying === 'select' ? `Selected. Press ${keys} to copy.` : ''}
|
|
347
|
+
</span>
|
|
348
|
+
</div>
|
|
349
|
+
)
|
|
350
|
+
return (
|
|
351
|
+
<div
|
|
352
|
+
className={['desk-code', className].filter(Boolean).join(' ')}
|
|
353
|
+
data-language={language}
|
|
354
|
+
{...(variants ? { 'data-variants': '' } : {})}
|
|
355
|
+
>
|
|
356
|
+
{variants && (
|
|
357
|
+
// The tabs are the frame's top edge, as on a phone: one card, not a control above a box.
|
|
358
|
+
<div className="desk-code-head">
|
|
359
|
+
<SegmentedControl
|
|
360
|
+
label={label ?? 'Code'}
|
|
361
|
+
size="small"
|
|
362
|
+
options={variants.map(v => ({ value: v.value, label: v.label }))}
|
|
363
|
+
value={variant?.value ?? ''}
|
|
364
|
+
onChange={choose}
|
|
365
|
+
/>
|
|
366
|
+
{actions}
|
|
367
|
+
</div>
|
|
368
|
+
)}
|
|
369
|
+
<div className="desk-code-main">
|
|
370
|
+
{label && !variants && <div className="desk-code-label" aria-hidden="true">{label}</div>}
|
|
371
|
+
<pre className="desk-code-body" role="region" aria-label={name} tabIndex={0}>
|
|
372
|
+
<code ref={body}>{content}</code>
|
|
373
|
+
</pre>
|
|
374
|
+
</div>
|
|
375
|
+
{!variants && actions}
|
|
376
|
+
</div>
|
|
377
|
+
)
|
|
378
|
+
}
|
package/src/react/index.ts
CHANGED
|
@@ -52,6 +52,8 @@ export { PageDots, pageWindow } from './pageDots.js'
|
|
|
52
52
|
export type { PageDot, PageDotsProps } from './pageDots.js'
|
|
53
53
|
export type { LedColor, LedProps } from './led.js'
|
|
54
54
|
export { Page, Pane, PaneHeader, Section, Toolbar } from './pane.js'
|
|
55
|
+
export { Split } from './split.js'
|
|
56
|
+
export type { SplitProps } from './split.js'
|
|
55
57
|
export type { PageProps, PaneHeaderProps, PaneProps, SectionProps, ToolbarProps } from './pane.js'
|
|
56
58
|
export { InfoTip } from './infoTip.js'
|
|
57
59
|
export type { InfoTipProps } from './infoTip.js'
|
|
@@ -62,6 +64,8 @@ export type { ChecklistProps, Task, TaskProgress, Tasks, TaskState } from './tas
|
|
|
62
64
|
export { PopUpButton } from './popup.js'
|
|
63
65
|
export { Chip, Disclosure } from './disclosure.js'
|
|
64
66
|
export type { ChipProps, DisclosureProps } from './disclosure.js'
|
|
67
|
+
export { CodeBlock } from './codeBlock.js'
|
|
68
|
+
export type { CodeBlockProps, CodeLanguage, CodeVariant } from './codeBlock.js'
|
|
65
69
|
export type { PopUpButtonProps, PopUpOption } from './popup.js'
|
|
66
70
|
export { Tabs } from './tabs.js'
|
|
67
71
|
export type { Tab, TabsProps } from './tabs.js'
|