@zooui/nuxt 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 +26 -0
- package/README.md +475 -2
- package/assets/css/base.css +57 -0
- package/components/Card.vue +20 -0
- package/components/CodeBlock.vue +63 -0
- package/components/DateField.vue +312 -0
- package/components/Drawer.vue +22 -0
- package/components/EcosystemMark.vue +39 -0
- package/components/Identifier.vue +8 -0
- package/components/Kicker.vue +8 -0
- package/components/Modal.vue +22 -0
- package/components/NumberInput.vue +370 -0
- package/components/QuotaMeter.vue +32 -0
- package/components/RepeatingList.vue +256 -0
- package/components/Slideover.vue +22 -0
- package/components/Slots.vue +27 -0
- package/components/Status.vue +24 -0
- package/components/ThemeToggle.vue +30 -0
- package/components/TokenReveal.vue +62 -0
- package/components/ecosystems.ts +38 -0
- package/components/edge.ts +63 -0
- package/components/number-calc.ts +118 -0
- package/components/number-format.ts +357 -0
- package/components/number-input.ts +110 -0
- package/components/recipes.ts +141 -0
- package/composables/useToday.ts +19 -0
- package/composables/useZooTheme.ts +25 -0
- package/nuxt.config.ts +93 -0
- package/package.json +57 -3
- package/stylesheet-guard.d.ts +12 -0
- package/stylesheet-guard.js +103 -0
- package/testing/find-conflicts.d.ts +2 -0
- package/testing/find-conflicts.js +103 -0
- package/utils/date-entry.ts +245 -0
- package/utils/select-none.ts +23 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* USlideover with Zoo UI's colored edge: side drawer — a top edge, as for every overlay. Only `color` is added
|
|
4
|
+
* — every other prop, `v-model`, event and slot passes straight through to Nuxt UI's USlideover, so its
|
|
5
|
+
* behavior and polish are untouched. No `color`, no edge: a plain USlideover.
|
|
6
|
+
*/
|
|
7
|
+
import { edge, useSectionAccent, withEdge, withSectionAccent, type EdgeColor } from './edge'
|
|
8
|
+
|
|
9
|
+
defineOptions({ inheritAttrs: false })
|
|
10
|
+
const props = defineProps<{ color?: EdgeColor; ui?: Record<string, any> }>()
|
|
11
|
+
const ui = computed(() => withEdge(props.ui, 'content', edge('top', props.color)))
|
|
12
|
+
// `accent`: the overlay portals to <body>, so it takes the name of the section it was opened from
|
|
13
|
+
const section = useSectionAccent()
|
|
14
|
+
</script>
|
|
15
|
+
|
|
16
|
+
<template>
|
|
17
|
+
<USlideover v-bind="$attrs" :ui="ui" :content="withSectionAccent($attrs.content, color, section)">
|
|
18
|
+
<template v-for="(_, name) in $slots" #[name]="scope">
|
|
19
|
+
<slot :name="name" v-bind="scope ?? {}" />
|
|
20
|
+
</template>
|
|
21
|
+
</USlideover>
|
|
22
|
+
</template>
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* One chip per slot of capacity, each NAMED: "slot 1 · free", "slot 2 · build 4812". An open slot is a solid
|
|
4
|
+
* primary badge (the accent, with ground-colored text in dark); a held one is an outline neutral badge saying what
|
|
5
|
+
* holds it. Blank colored bars read as empty tags, so a chip always carries its text. Nuxt UI's UBadge as it ships
|
|
6
|
+
* (the Forest theme sets badges in its mono face); the list is labeled for assistive tech by `label`.
|
|
7
|
+
*/
|
|
8
|
+
defineProps<{
|
|
9
|
+
/** In display order. `label` is the chip's text; `available` marks an open slot; `title` is an optional tooltip. */
|
|
10
|
+
slots: { label: string; available: boolean; title?: string }[]
|
|
11
|
+
/** What the group is, for assistive tech: "Linux VMs: 1 of 4 free". */
|
|
12
|
+
label?: string
|
|
13
|
+
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl'
|
|
14
|
+
}>()
|
|
15
|
+
</script>
|
|
16
|
+
|
|
17
|
+
<template>
|
|
18
|
+
<ul class="flex flex-wrap gap-1.5 m-0 p-0 list-none" :aria-label="label">
|
|
19
|
+
<li v-for="(slot, i) in slots" :key="i" class="flex">
|
|
20
|
+
<UBadge
|
|
21
|
+
:label="slot.label" :title="slot.title" :size="size"
|
|
22
|
+
:color="slot.available ? 'primary' : 'neutral'" :variant="slot.available ? 'solid' : 'outline'"
|
|
23
|
+
:data-available="slot.available ? 'true' : 'false'"
|
|
24
|
+
/>
|
|
25
|
+
</li>
|
|
26
|
+
</ul>
|
|
27
|
+
</template>
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { status, cx } from "./recipes";
|
|
3
|
+
|
|
4
|
+
const LABELS = {
|
|
5
|
+
healthy: "Healthy", warn: "Near limit", suspended: "Suspended",
|
|
6
|
+
unreachable: "Unreachable", quiet: "Quiet",
|
|
7
|
+
} as const;
|
|
8
|
+
|
|
9
|
+
const props = withDefaults(defineProps<{
|
|
10
|
+
state?: keyof typeof LABELS;
|
|
11
|
+
pill?: boolean;
|
|
12
|
+
/** A soft glow around the healthy dot. Other states stay unlit. */
|
|
13
|
+
glow?: boolean;
|
|
14
|
+
}>(), { state: "healthy", pill: false, glow: false });
|
|
15
|
+
</script>
|
|
16
|
+
|
|
17
|
+
<template>
|
|
18
|
+
<!-- Shape carries the state; color only reinforces it, so this survives
|
|
19
|
+
grayscale. Always render the glyph child. -->
|
|
20
|
+
<span :class="cx(status.base, status.state[state], pill ? cx(status.pill, status.size.pill) : status.size.plain)">
|
|
21
|
+
<span :class="cx(status.glyph, status.shape[state], glow && state === 'healthy' && status.glow)" aria-hidden="true" />
|
|
22
|
+
<slot>{{ LABELS[props.state] }}</slot>
|
|
23
|
+
</span>
|
|
24
|
+
</template>
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* Light / dark / system as one segmented control: Nuxt UI buttons in a field group, the chosen one pressed
|
|
4
|
+
* (aria-pressed) and solid. The preference is only known on the client, so the control renders there and a
|
|
5
|
+
* disabled copy stands in on the server rather than a guess.
|
|
6
|
+
*/
|
|
7
|
+
import { ZOO_THEMES } from '../composables/useZooTheme'
|
|
8
|
+
|
|
9
|
+
const { theme, setTheme } = useZooTheme()
|
|
10
|
+
|
|
11
|
+
const ICONS = { light: 'i-lucide-sun', dark: 'i-lucide-moon', system: 'i-lucide-monitor' } as const
|
|
12
|
+
</script>
|
|
13
|
+
|
|
14
|
+
<template>
|
|
15
|
+
<ClientOnly>
|
|
16
|
+
<UFieldGroup role="group" aria-label="Theme">
|
|
17
|
+
<UButton
|
|
18
|
+
v-for="t in ZOO_THEMES" :key="t"
|
|
19
|
+
:icon="ICONS[t]" :label="t" size="xs" color="neutral"
|
|
20
|
+
:variant="theme === t ? 'solid' : 'outline'" :aria-pressed="theme === t"
|
|
21
|
+
@click="setTheme(t)"
|
|
22
|
+
/>
|
|
23
|
+
</UFieldGroup>
|
|
24
|
+
<template #fallback>
|
|
25
|
+
<UFieldGroup role="group" aria-label="Theme">
|
|
26
|
+
<UButton v-for="t in ZOO_THEMES" :key="t" :icon="ICONS[t]" :label="t" size="xs" color="neutral" variant="outline" disabled />
|
|
27
|
+
</UFieldGroup>
|
|
28
|
+
</template>
|
|
29
|
+
</ClientOnly>
|
|
30
|
+
</template>
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* The shown-once moment: a secret (an API token, say) displayed once after it is created. Zoo UI's code surface holds
|
|
4
|
+
* the token — its copy button, the token marked and selectable in one click — under a `[ token · name · shown once ]`
|
|
5
|
+
* kicker, with Zoo UI's banner (UAlert outline, warning) saying it will not be shown again. With `require-ack`, a
|
|
6
|
+
* button acknowledges it has been saved: the secret then blurs and goes inert, out of reach of focus and readers.
|
|
7
|
+
*/
|
|
8
|
+
import { tokenReveal, cx } from "./recipes";
|
|
9
|
+
|
|
10
|
+
withDefaults(defineProps<{
|
|
11
|
+
/** the secret, shown in full */
|
|
12
|
+
token: string;
|
|
13
|
+
/** the token's name, in the kicker */
|
|
14
|
+
name?: string;
|
|
15
|
+
/** show the acknowledgment button; the parent usually moves on when it emits `ack` */
|
|
16
|
+
requireAck?: boolean;
|
|
17
|
+
ackLabel?: string;
|
|
18
|
+
/** the banner's title; the default slot replaces its description */
|
|
19
|
+
title?: string;
|
|
20
|
+
}>(), { name: "default", requireAck: false, ackLabel: "I have saved it", title: "You won't see this token again" });
|
|
21
|
+
|
|
22
|
+
defineSlots<{
|
|
23
|
+
/** the warning's description: what the token grants and what to do if it is lost */
|
|
24
|
+
default?: () => unknown;
|
|
25
|
+
/** more actions beside the acknowledgment, e.g. a "Done" link */
|
|
26
|
+
actions?: () => unknown;
|
|
27
|
+
}>();
|
|
28
|
+
|
|
29
|
+
const emit = defineEmits<{
|
|
30
|
+
/** the token reached the clipboard (never on a failed copy) */
|
|
31
|
+
copy: [token: string];
|
|
32
|
+
/** the acknowledgment button was pressed; emitted once */
|
|
33
|
+
ack: [];
|
|
34
|
+
}>();
|
|
35
|
+
|
|
36
|
+
const labelId = useId();
|
|
37
|
+
const acknowledged = ref(false);
|
|
38
|
+
|
|
39
|
+
function ack() {
|
|
40
|
+
if (acknowledged.value) return;
|
|
41
|
+
acknowledged.value = true;
|
|
42
|
+
emit("ack");
|
|
43
|
+
}
|
|
44
|
+
</script>
|
|
45
|
+
|
|
46
|
+
<template>
|
|
47
|
+
<section :class="tokenReveal.root" :aria-labelledby="labelId">
|
|
48
|
+
<ZKicker :id="labelId">token · {{ name }} · shown once</ZKicker>
|
|
49
|
+
<div :class="cx(tokenReveal.secret, acknowledged && tokenReveal.hidden)" :inert="acknowledged" data-slot="secret">
|
|
50
|
+
<ZCodeBlock variant="block" :code="token" :token="token" @copy="emit('copy', $event)" />
|
|
51
|
+
</div>
|
|
52
|
+
<UAlert color="warning" variant="outline" icon="i-lucide-triangle-alert" :title="title">
|
|
53
|
+
<template #description>
|
|
54
|
+
<slot>Copy it now and keep it somewhere safe. It is not stored in a recoverable form: if you lose it, revoke it and create another.</slot>
|
|
55
|
+
</template>
|
|
56
|
+
</UAlert>
|
|
57
|
+
<div v-if="requireAck || $slots.actions" :class="tokenReveal.actions">
|
|
58
|
+
<UButton v-if="requireAck" color="primary" :label="ackLabel" :icon="acknowledged ? 'i-lucide-check' : undefined" @click="ack" />
|
|
59
|
+
<slot name="actions" />
|
|
60
|
+
</div>
|
|
61
|
+
</section>
|
|
62
|
+
</template>
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ecosystems ZEcosystemMark knows by name — package managers as two-letter monogram tiles. Each has its own
|
|
3
|
+
* corner SHAPE so they read apart at 20px without brand color. A product with ecosystems beyond these passes
|
|
4
|
+
* `label`, `monogram` and `shape` to the component (any of them also overrides an entry here); an unknown ecosystem
|
|
5
|
+
* with none of them is still drawn, from its own name, on the plain `rounded` tile.
|
|
6
|
+
*/
|
|
7
|
+
export const ECOSYSTEM_SHAPES = ['square', 'circle', 'leaf', 'arch', 'lens', 'drop', 'rounded'] as const
|
|
8
|
+
export type EcosystemShape = (typeof ECOSYSTEM_SHAPES)[number]
|
|
9
|
+
|
|
10
|
+
export interface Ecosystem {
|
|
11
|
+
/** what the tile reads as — the accessible name, and the visible label with `with-label` */
|
|
12
|
+
label: string
|
|
13
|
+
/** two letters, set in the mono face */
|
|
14
|
+
monogram: string
|
|
15
|
+
shape: EcosystemShape
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const ECOSYSTEMS = {
|
|
19
|
+
npm: { label: 'npm', monogram: 'npm', shape: 'square' },
|
|
20
|
+
nuget: { label: 'NuGet', monogram: 'nu', shape: 'circle' },
|
|
21
|
+
winget: { label: 'winget', monogram: 'wg', shape: 'leaf' },
|
|
22
|
+
chocolatey: { label: 'Chocolatey', monogram: 'ch', shape: 'arch' },
|
|
23
|
+
scoop: { label: 'Scoop', monogram: 'sc', shape: 'lens' },
|
|
24
|
+
homebrew: { label: 'Homebrew', monogram: 'br', shape: 'drop' },
|
|
25
|
+
} as const satisfies Record<string, Ecosystem>
|
|
26
|
+
|
|
27
|
+
export type KnownEcosystem = keyof typeof ECOSYSTEMS
|
|
28
|
+
|
|
29
|
+
/** The entry for `id`, with any overrides applied; an unknown id falls back to its own name. */
|
|
30
|
+
export function ecosystem(id: string, overrides: Partial<Ecosystem> = {}): Ecosystem {
|
|
31
|
+
const known: Ecosystem | undefined = (ECOSYSTEMS as Record<string, Ecosystem>)[id]
|
|
32
|
+
const base = known ?? { label: id, monogram: id.slice(0, 2), shape: 'rounded' }
|
|
33
|
+
return {
|
|
34
|
+
label: overrides.label ?? base.label,
|
|
35
|
+
monogram: overrides.monogram ?? base.monogram,
|
|
36
|
+
shape: overrides.shape ?? base.shape,
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zoo UI's colored edge — the one piece of Zoo UI styling the base adds to Nuxt UI's surfaces. A 3px stripe in the
|
|
3
|
+
* surface's color: on the TOP for overlays (dialog, slideover, drawer — Zoo UI's danger dialog), on the LEFT for
|
|
4
|
+
* inline surfaces (card, toast — Zoo UI's toast and callout). Everything else about the surface is Nuxt UI's.
|
|
5
|
+
*
|
|
6
|
+
* The class strings are written out in full so Tailwind's scan finds them. `border-<color>` sets every side's
|
|
7
|
+
* color, but only the edge side has width, so only the edge shows; Nuxt UI's semantic colors switch to their
|
|
8
|
+
* 400 shade in dark mode on their own.
|
|
9
|
+
*
|
|
10
|
+
* `accent` is the section's accent (`data-accent`, base.css): the `--accent` role, which carries its own dark value.
|
|
11
|
+
* Outside any `data-accent` container it falls back to the primary. Overlays portal to <body>, outside the section,
|
|
12
|
+
* so their wrappers carry the section's name onto the overlay's content (useSectionAccent).
|
|
13
|
+
*/
|
|
14
|
+
import { getCurrentInstance, onMounted, onUpdated, ref } from 'vue'
|
|
15
|
+
|
|
16
|
+
export const EDGE_COLORS = ['primary', 'secondary', 'success', 'info', 'warning', 'error', 'neutral', 'accent'] as const
|
|
17
|
+
export type EdgeColor = (typeof EDGE_COLORS)[number]
|
|
18
|
+
|
|
19
|
+
const edgeColor: Record<EdgeColor, string> = {
|
|
20
|
+
primary: 'border-primary',
|
|
21
|
+
secondary: 'border-secondary',
|
|
22
|
+
success: 'border-success',
|
|
23
|
+
info: 'border-info',
|
|
24
|
+
warning: 'border-warning',
|
|
25
|
+
error: 'border-error',
|
|
26
|
+
neutral: 'border-inverted',
|
|
27
|
+
accent: 'border-[color:var(--accent,var(--ui-primary))]',
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The edge classes for a surface, or '' for no color (a plain Nuxt UI surface). */
|
|
31
|
+
export const edge = (side: 'top' | 'left', color?: EdgeColor) =>
|
|
32
|
+
color ? `${side === 'top' ? 'border-t-3' : 'border-l-3'} ${edgeColor[color]}` : ''
|
|
33
|
+
|
|
34
|
+
/** Prepend the edge to one slot of a Nuxt UI `ui` object, keeping whatever the caller passed for it. */
|
|
35
|
+
export function withEdge<T extends Record<string, unknown>>(ui: T | undefined, slot: string, classes: string): T {
|
|
36
|
+
if (!classes) return (ui ?? {}) as T
|
|
37
|
+
const existing = ui?.[slot]
|
|
38
|
+
return { ...(ui ?? {}), [slot]: existing ? `${classes} ${existing}` : classes } as T
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The `data-accent` name of the section a component sits in, read from the DOM where it renders — for an overlay
|
|
43
|
+
* wrapper to put on its content, which Nuxt UI portals to <body> and so out of the section's reach. Read after mount
|
|
44
|
+
* and after every update (the wrapper re-renders when its `open` or other attributes change), so it is current
|
|
45
|
+
* whenever the overlay opens; undefined on the server and outside any section.
|
|
46
|
+
*/
|
|
47
|
+
export function useSectionAccent() {
|
|
48
|
+
const instance = getCurrentInstance()
|
|
49
|
+
const name = ref<string>()
|
|
50
|
+
const read = () => {
|
|
51
|
+
const el = instance?.proxy?.$el as Node | null | undefined
|
|
52
|
+
name.value = (el?.parentElement?.closest('[data-accent]') as HTMLElement | null)?.dataset.accent
|
|
53
|
+
}
|
|
54
|
+
onMounted(read)
|
|
55
|
+
onUpdated(read)
|
|
56
|
+
return name
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** An overlay's `content` props with the section's accent added, for `color="accent"`; the caller's kept otherwise. */
|
|
60
|
+
export function withSectionAccent(content: unknown, color: EdgeColor | undefined, accent: string | undefined) {
|
|
61
|
+
const props = content as Record<string, any> | undefined
|
|
62
|
+
return color === 'accent' && accent ? { ...(props ?? {}), 'data-accent': accent } : props
|
|
63
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ZNumberInput's `calc` mode: a currency field with a calculator in it, apart from the component (tests:
|
|
3
|
+
* scripts/number-calc.test.mjs). `+ - * /` with the usual precedence, read by a small recursive-descent parser (never
|
|
4
|
+
* `eval`) and worked in decimal.js, so 0.1 + 0.2 is 0.3. Each number in the expression is grouped as it is typed, for
|
|
5
|
+
* the field's locale, with the caret kept on the same character.
|
|
6
|
+
*/
|
|
7
|
+
import { D, deletion, grouped, isDecimal, isGroup, isMinus, splice, type Dec, type Edit, type Field, type Places, type Selection, type Symbols } from './number-format.ts'
|
|
8
|
+
|
|
9
|
+
const OPERATOR = /([+\-*/−])/
|
|
10
|
+
const isOperator = (c: string) => c.length === 1 && OPERATOR.test(c)
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The value of an expression, or `null` when it is incomplete ("678+"), not one ("abc"), or divides by zero. Spaces
|
|
14
|
+
* and the locale's group separators are ignored ("1,000+500" is 1500 in en-US, "1.000+500" in de); a minus straight
|
|
15
|
+
* before a number makes it negative ("-100+200", "5*-2").
|
|
16
|
+
*/
|
|
17
|
+
export function evaluate(input: string, s: Symbols): Dec | null {
|
|
18
|
+
let expr = ''
|
|
19
|
+
for (const c of input) {
|
|
20
|
+
if (/\s/.test(c) || isGroup(c, s)) continue
|
|
21
|
+
expr += isDecimal(c, s) ? '.' : isMinus(c) ? '-' : c
|
|
22
|
+
}
|
|
23
|
+
let pos = 0
|
|
24
|
+
|
|
25
|
+
// number → -? [digits and one decimal point]
|
|
26
|
+
const number = (): Dec | null => {
|
|
27
|
+
let text = ''
|
|
28
|
+
if (expr[pos] === '-') text += expr[pos++]
|
|
29
|
+
while (pos < expr.length && /[\d.]/.test(expr[pos]!)) text += expr[pos++]
|
|
30
|
+
if (!/\d/.test(text)) return null
|
|
31
|
+
try {
|
|
32
|
+
return new D(text)
|
|
33
|
+
} catch {
|
|
34
|
+
return null
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
// term → number ((* or /) number)*
|
|
38
|
+
const term = (): Dec | null => {
|
|
39
|
+
let result = number()
|
|
40
|
+
while (result !== null && (expr[pos] === '*' || expr[pos] === '/')) {
|
|
41
|
+
const op = expr[pos++]
|
|
42
|
+
const right = number()
|
|
43
|
+
if (right === null || (op === '/' && right.isZero())) return null
|
|
44
|
+
result = op === '*' ? result.times(right) : result.div(right)
|
|
45
|
+
}
|
|
46
|
+
return result
|
|
47
|
+
}
|
|
48
|
+
// expression → term ((+ or -) term)*
|
|
49
|
+
const expression = (): Dec | null => {
|
|
50
|
+
let result = term()
|
|
51
|
+
while (result !== null && (expr[pos] === '+' || expr[pos] === '-')) {
|
|
52
|
+
const op = expr[pos++]
|
|
53
|
+
const right = term()
|
|
54
|
+
if (right === null) return null
|
|
55
|
+
result = op === '+' ? result.plus(right) : result.minus(right)
|
|
56
|
+
}
|
|
57
|
+
return result
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (!expr) return null
|
|
61
|
+
const result = expression()
|
|
62
|
+
return pos === expr.length && result !== null && result.isFinite() ? result : null
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Ten places, for a calc field with no maximum of its own: 1/3*3 is 1 rather than forty nines. */
|
|
66
|
+
export const CALC_PLACES = 10
|
|
67
|
+
|
|
68
|
+
/** The places a calc field's result is rounded to, once, on commit: the field's maximum, else ten. */
|
|
69
|
+
export const calcPlaces = (places: Places): Places => (places.max === undefined ? { min: places.min, max: CALC_PLACES } : places)
|
|
70
|
+
|
|
71
|
+
/** Whether there is an operator past the first character (a leading minus is a sign, not a sum). */
|
|
72
|
+
export const containsOperator = (text: string) => text.length > 1 && OPERATOR.test(text.slice(1))
|
|
73
|
+
|
|
74
|
+
/** One number of an expression grouped: "12345.6" → "12,345.6", spaces around it kept. Anything it can't read is
|
|
75
|
+
* left as typed. */
|
|
76
|
+
function segment(part: string, s: Symbols): string {
|
|
77
|
+
const bare = [...part].filter((c) => !isGroup(c, s)).join('')
|
|
78
|
+
const [, before, core, after] = /^(\s*)(.*?)(\s*)$/s.exec(bare)!
|
|
79
|
+
if (!/\d/.test(core!)) return part
|
|
80
|
+
const at = [...core!].findIndex((c) => isDecimal(c, s))
|
|
81
|
+
const int = at < 0 ? core! : core!.slice(0, at)
|
|
82
|
+
return before + (/^\d+$/.test(int) ? grouped(int, s).text : int) + (at < 0 ? '' : core!.slice(at)) + after
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The same caret in a regrouped text: after as many characters other than group separators. */
|
|
86
|
+
function keepCaret(before: string, after: string, caret: number, s: Symbols): number {
|
|
87
|
+
let count = 0
|
|
88
|
+
for (let i = 0; i < Math.min(caret, before.length); i++) if (!isGroup(before[i]!, s)) count++
|
|
89
|
+
let pos = 0
|
|
90
|
+
for (let seen = 0; pos < after.length && seen < count; pos++) if (!isGroup(after[pos]!, s)) seen++
|
|
91
|
+
return pos
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** An expression as the field shows it while typing: each number grouped, the caret on the same character. */
|
|
95
|
+
export function reformatExpression(raw: string, caret: number, s: Symbols): Field {
|
|
96
|
+
const text = raw.split(OPERATOR).map((part, i) => (i % 2 ? part : segment(part, s))).join('')
|
|
97
|
+
return { text, caret: keepCaret(raw, text, caret, s) }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The expression after one edit. The rule: an operator typed over a selection is added at the end rather than
|
|
102
|
+
* replacing it (so "1,200" selected on focus, then "+50", is 1,250), except a minus over the whole text, which starts
|
|
103
|
+
* a negative number. `null`: the edit is refused and nothing changes, the selection included.
|
|
104
|
+
*/
|
|
105
|
+
export function editExpression(sel: Selection, e: Edit, s: Symbols): Field | null {
|
|
106
|
+
const { text, start, end } = sel
|
|
107
|
+
if ('delete' in e) {
|
|
108
|
+
const range = deletion(sel, e.delete, s)
|
|
109
|
+
return range ? reformatExpression(splice(text, range[0], range[1]), range[0], s) : { text, caret: start }
|
|
110
|
+
}
|
|
111
|
+
// what an expression can hold: a pasted "$1,200" arrives as 1,200, and a line break as a space
|
|
112
|
+
const put = [...e.insert.replace(/\s/g, ' ')].filter((c) => /[\d ]/.test(c) || isOperator(c) || isDecimal(c, s) || isGroup(c, s)).join('')
|
|
113
|
+
if (!put) return null
|
|
114
|
+
if (start !== end && isOperator(put) && !(isMinus(put) && start === 0 && end === text.length)) {
|
|
115
|
+
return reformatExpression(text + put, text.length + 1, s)
|
|
116
|
+
}
|
|
117
|
+
return reformatExpression(splice(text, start, end, put), start + put.length, s)
|
|
118
|
+
}
|