@emberbox/ui 0.5.0 → 0.6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emberbox/ui",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Shared look and building blocks of the Emberbox tools, built on Nuxt UI.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -11,6 +11,7 @@
11
11
  },
12
12
  "files": [
13
13
  "src",
14
+ "!src/**/*.test.ts",
14
15
  "dist",
15
16
  "logos"
16
17
  ],
@@ -28,6 +29,7 @@
28
29
  }
29
30
  },
30
31
  "scripts": {
32
+ "test": "vitest run",
31
33
  "build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.vite.json && node scripts/build-types.mjs",
32
34
  "typecheck": "vue-tsc -b",
33
35
  "icons": "node scripts/check-icons.mjs",
@@ -55,10 +57,13 @@
55
57
  "devDependencies": {
56
58
  "@emberbox/lint": "^0.2.0",
57
59
  "@types/node": "^24.0.0",
60
+ "@vitejs/plugin-vue": "^6.0.11",
58
61
  "@vue/tsconfig": "^0.9.1",
59
62
  "eslint": "^10.15.0",
63
+ "happy-dom": "^21.0.4",
60
64
  "typescript": "~6.0.3",
61
65
  "vite": "^8.3.8",
66
+ "vitest": "^5.1.1",
62
67
  "vue": "^3.5.46",
63
68
  "vue-tsc": "^3.3.14"
64
69
  },
@@ -0,0 +1,41 @@
1
+ <script setup lang="ts">
2
+ import {h, type VNode} from 'vue'
3
+ import type {SvgElement} from '../symbols/tactical'
4
+
5
+ /**
6
+ * A tactical symbol drawn into the page as SVG nodes, so the theme's signal red applies to it.
7
+ * The tree comes from `unitSymbolTree` or `markSymbolTree`.
8
+ */
9
+ const props = defineProps<{
10
+ /** The symbol to draw. */
11
+ symbol: SvgElement
12
+ /** Width in pixels; the height follows the symbol's aspect ratio. */
13
+ width?: number
14
+ }>()
15
+
16
+ /** Builds the Vue nodes of one element and everything below it. */
17
+ function draw(node: SvgElement): VNode {
18
+ return h(node.tag, node.attributes, node.text ?? node.children?.map(draw))
19
+ }
20
+
21
+ /** The symbol as Vue nodes. */
22
+ function Drawing(): VNode {
23
+ return draw(props.symbol)
24
+ }
25
+ </script>
26
+
27
+ <template>
28
+ <span class="tactical-symbol" :style="{width: `${width ?? 48}px`}"><Drawing/></span>
29
+ </template>
30
+
31
+ <style scoped>
32
+ .tactical-symbol {
33
+ display: inline-block;
34
+ line-height: 0;
35
+ }
36
+
37
+ .tactical-symbol :deep(svg) {
38
+ width: 100%;
39
+ height: auto;
40
+ }
41
+ </style>
package/src/index.ts CHANGED
@@ -26,6 +26,12 @@ export {default as ComboboxField} from './components/ComboboxField.vue'
26
26
  export {default as YesNoField} from './components/YesNoField.vue'
27
27
  export {default as DateTimeField} from './components/DateTimeField.vue'
28
28
  export {default as CollapsibleSection} from './components/CollapsibleSection.vue'
29
+ export {default as TacticalSymbol} from './components/TacticalSymbol.vue'
30
+ export {
31
+ INCIDENT_MARK_KINDS, MARK_KINDS, SYMBOL_HEIGHT, SYMBOL_WIDTH, WATER_SUPPLY_MARK_KINDS, escapeSvg, markSymbol,
32
+ markSymbolTree, unitSymbol, unitSymbolTree,
33
+ type MarkKind, type Organisation, type SvgElement, type SymbolShape, type SymbolSpec, type UnitSize,
34
+ } from './symbols/tactical'
29
35
 
30
36
  /**
31
37
  * Vue plugin that installs Nuxt UI in the Emberbox style. Icons come only from the bundled set; a
@@ -0,0 +1,222 @@
1
+ /**
2
+ * Tactical symbols after DIN 14034-6 and the recommendations of the BBK for DV 102. The drawings
3
+ * follow the system of the recommendations (basic form, size mark, writing) without copying their
4
+ * artwork.
5
+ *
6
+ * A symbol is built as a tree of SVG elements: `TacticalSymbol` renders the tree as Vue nodes in
7
+ * the page, and `renderSvg` writes it as an escaped string for map icons. Every symbol is drawn on
8
+ * the same 120 × 80 canvas: the size mark above, the basic form in the middle, wheels or keel
9
+ * below. The fire service draws red with white writing; medical services draw red on white. Red
10
+ * comes from the theme's signal palette, so a symbol has to be part of the page (not loaded as an
11
+ * image) for it to apply.
12
+ */
13
+
14
+ /** The organisation a unit belongs to, which sets its colours. */
15
+ export type Organisation = 'fire' | 'medical'
16
+
17
+ /** The basic form of a unit's symbol. */
18
+ export type SymbolShape = 'vehicle' | 'container' | 'trailer' | 'boat' | 'unit'
19
+
20
+ /** The size of a unit, shown by dots or a bar above its symbol. */
21
+ export type UnitSize = 'squad' | 'section' | 'group' | 'platoon'
22
+
23
+ /** Marks of places and events of an incident. */
24
+ export const INCIDENT_MARK_KINDS = ['incident', 'fire', 'hazard', 'staging', 'command-post', 'water', 'casualty-collection'] as const
25
+
26
+ /** Marks of the water supply along a relay line. */
27
+ export const WATER_SUPPLY_MARK_KINDS = ['hydrant-underground', 'hydrant-pillar', 'open-water', 'water-tank', 'dividing-breeching'] as const
28
+
29
+ /** Every kind of mark that is not a unit. */
30
+ export const MARK_KINDS = [...INCIDENT_MARK_KINDS, ...WATER_SUPPLY_MARK_KINDS] as const
31
+
32
+ /** A kind of mark. */
33
+ export type MarkKind = typeof MARK_KINDS[number]
34
+
35
+ /** What a unit's symbol is drawn from. */
36
+ export interface SymbolSpec {
37
+ shape: SymbolShape
38
+ organisation: Organisation
39
+ text: string
40
+ size?: UnitSize
41
+ }
42
+
43
+ /** One SVG element of a symbol, with its attributes and either child elements or text. */
44
+ export interface SvgElement {
45
+ tag: string
46
+ attributes: Record<string, string | number>
47
+ children?: SvgElement[]
48
+ text?: string
49
+ }
50
+
51
+ /** The width of every symbol's canvas. */
52
+ export const SYMBOL_WIDTH = 120
53
+ /** The height of every symbol's canvas. */
54
+ export const SYMBOL_HEIGHT = 80
55
+
56
+ const RED = 'var(--color-signal-500)'
57
+ const WHITE = 'white'
58
+ const STROKE = 3
59
+ const MAX_TEXT_WIDTH = 86
60
+
61
+ interface Colours {
62
+ fill: string
63
+ line: string
64
+ text: string
65
+ }
66
+
67
+ type Attributes = SvgElement['attributes']
68
+
69
+ /** An SVG element without children. */
70
+ function element(tag: string, attributes: Attributes): SvgElement {
71
+ return {tag, attributes}
72
+ }
73
+
74
+ /** The attributes of an outlined form. */
75
+ function outlined(fill: string, line: string): Attributes {
76
+ return {fill, stroke: line, 'stroke-width': STROKE}
77
+ }
78
+
79
+ /** The attributes of a plain line. */
80
+ function stroked(line: string): Attributes {
81
+ return {fill: 'none', stroke: line, 'stroke-width': STROKE}
82
+ }
83
+
84
+ /** The colours of an organisation. */
85
+ function colours(organisation: Organisation): Colours {
86
+ return organisation === 'medical'
87
+ ? {fill: WHITE, line: RED, text: RED}
88
+ : {fill: RED, line: RED, text: WHITE}
89
+ }
90
+
91
+ /** Escapes text for use inside SVG or HTML markup. */
92
+ export function escapeSvg(text: string): string {
93
+ return text.replace(/[&<>"']/g, character => `&#${character.charCodeAt(0)};`)
94
+ }
95
+
96
+ /**
97
+ * Writes a symbol tree as SVG markup, escaping every attribute value and text. Tag and attribute
98
+ * names are written as they are, so the tree must be built by code, as the trees of this module are.
99
+ */
100
+ export function renderSvg(node: SvgElement): string {
101
+ const attributes = Object.entries(node.attributes)
102
+ .map(([name, value]) => ` ${name}="${escapeSvg(String(value))}"`)
103
+ .join('')
104
+ const content = node.text !== undefined
105
+ ? escapeSvg(node.text)
106
+ : (node.children ?? []).map(renderSvg).join('')
107
+ return content ? `<${node.tag}${attributes}>${content}</${node.tag}>` : `<${node.tag}${attributes}/>`
108
+ }
109
+
110
+ /** The writing inside a symbol, squeezed to fit when it is long. */
111
+ function label(text: string, x: number, y: number, colour: string): SvgElement {
112
+ const size = text.length > 8 ? 17 : 22
113
+ const squeeze: Attributes = text.length * size * 0.5 > MAX_TEXT_WIDTH
114
+ ? {textLength: MAX_TEXT_WIDTH, lengthAdjust: 'spacingAndGlyphs'}
115
+ : {}
116
+ return {
117
+ tag: 'text',
118
+ attributes: {
119
+ x, y, fill: colour, 'font-size': size, 'font-weight': 700,
120
+ 'font-family': 'Barlow Condensed, Barlow, sans-serif', 'text-anchor': 'middle',
121
+ 'dominant-baseline': 'central', ...squeeze,
122
+ },
123
+ text,
124
+ }
125
+ }
126
+
127
+ /** The dots or bar above a unit that tell its size. */
128
+ function sizeMark(size: UnitSize | undefined, colour: string): SvgElement[] {
129
+ if (!size) return []
130
+ if (size === 'platoon') return [element('line', {x1: 60, y1: 1, x2: 60, y2: 11, ...stroked(colour)})]
131
+ const count = {squad: 1, section: 2, group: 3}[size]
132
+ return Array.from({length: count}, (_, index) =>
133
+ element('circle', {cx: 60 + (index - (count - 1) / 2) * 10, cy: 6, r: 3.5, fill: colour}))
134
+ }
135
+
136
+ /** The basic form of a unit's symbol, with the writing inside. */
137
+ function body(shape: SymbolShape, colour: Colours, text: string): SvgElement[] {
138
+ const outline = outlined(colour.fill, colour.line)
139
+ const wheel = (x: number) => element('circle', {cx: x, cy: 70, r: 6, ...outlined(WHITE, colour.line)})
140
+ switch (shape) {
141
+ case 'vehicle':
142
+ return [element('rect', {x: 10, y: 16, width: 100, height: 44, ...outline}), wheel(34), wheel(86), label(text, 60, 38, colour.text)]
143
+ case 'container':
144
+ return [
145
+ element('rect', {x: 14, y: 16, width: 92, height: 44, ...outline}),
146
+ element('path', {d: 'M8 66 H112 M14 60 L8 66 M106 60 L112 66', ...stroked(colour.line)}),
147
+ label(text, 60, 38, colour.text),
148
+ ]
149
+ case 'trailer':
150
+ return [
151
+ element('rect', {x: 24, y: 16, width: 86, height: 44, ...outline}),
152
+ element('path', {d: 'M24 50 H6', ...stroked(colour.line)}),
153
+ wheel(67),
154
+ label(text, 67, 38, colour.text),
155
+ ]
156
+ case 'boat':
157
+ return [element('path', {d: 'M10 16 H110 L100 56 Q60 72 20 56 Z', ...outline}), label(text, 60, 36, colour.text)]
158
+ case 'unit':
159
+ return [element('rect', {x: 10, y: 16, width: 100, height: 52, ...outline}), label(text, 60, 42, colour.text)]
160
+ }
161
+ }
162
+
163
+ /** The symbol of a unit: a vehicle, container, trailer, boat or squad. */
164
+ export function unitSymbolTree(spec: SymbolSpec): SvgElement {
165
+ const colour = colours(spec.organisation)
166
+ return canvas([...sizeMark(spec.size, colour.line), ...body(spec.shape, colour, spec.text)])
167
+ }
168
+
169
+ const OUTLINE = outlined(WHITE, RED)
170
+ const TRIANGLE = element('path', {d: 'M60 6 L108 74 H12 Z', ...OUTLINE})
171
+ const RING = element('circle', {cx: 60, cy: 40, r: 30, ...OUTLINE})
172
+
173
+ /** A circle with writing, the form most water supply marks share. */
174
+ function waterPoint(text: string): SvgElement[] {
175
+ return [RING, label(text, 60, 40, RED)]
176
+ }
177
+
178
+ /** The drawing of every kind of mark. */
179
+ const MARK_DRAWINGS: Record<MarkKind, () => SvgElement[]> = {
180
+ 'incident': () => [TRIANGLE],
181
+ 'fire': () => [TRIANGLE, element('path', {d: 'M60 30 C50 44 46 52 52 62 C54 54 58 52 60 48 C62 54 66 56 68 62 C74 52 70 42 60 30 Z', fill: RED})],
182
+ 'hazard': () => [TRIANGLE, label('!', 60, 52, RED)],
183
+ 'staging': () => [element('rect', {x: 10, y: 14, width: 100, height: 52, rx: 26, ...OUTLINE}), label('BR', 60, 40, RED)],
184
+ 'command-post': () => [element('path', {d: 'M30 70 V8 M30 10 H96 L84 24 L96 38 H30', ...OUTLINE}), label('EL', 58, 24, RED)],
185
+ 'water': () => waterPoint('W'),
186
+ 'casualty-collection': () => [RING, element('path', {d: 'M60 22 V58 M42 40 H78', fill: 'none', stroke: RED, 'stroke-width': 8})],
187
+ 'hydrant-underground': () => waterPoint('UH'),
188
+ 'hydrant-pillar': () => waterPoint('ÜH'),
189
+ 'open-water': () => [RING, element('path', {d: 'M38 36 Q49 28 60 36 T82 36 M38 48 Q49 40 60 48 T82 48', ...stroked(RED)})],
190
+ 'water-tank': () => [element('path', {d: 'M24 22 V62 H96 V22', ...OUTLINE}), label('LWB', 60, 44, RED)],
191
+ 'dividing-breeching': () => [
192
+ element('path', {d: 'M60 14 V40 M60 40 L32 68 M60 40 V68 M60 40 L88 68', ...stroked(RED)}),
193
+ element('circle', {cx: 60, cy: 40, r: 7, fill: RED}),
194
+ ],
195
+ }
196
+
197
+ /** The symbol of a mark: a place or event of an incident, or a point of the water supply. */
198
+ export function markSymbolTree(kind: MarkKind): SvgElement {
199
+ return canvas(MARK_DRAWINGS[kind]())
200
+ }
201
+
202
+ /** A unit's symbol as SVG markup, for map icons. */
203
+ export function unitSymbol(spec: SymbolSpec): string {
204
+ return renderSvg(unitSymbolTree(spec))
205
+ }
206
+
207
+ /** A mark's symbol as SVG markup, for map icons. */
208
+ export function markSymbol(kind: MarkKind): string {
209
+ return renderSvg(markSymbolTree(kind))
210
+ }
211
+
212
+ /** The SVG element of the common canvas around a symbol's content. */
213
+ function canvas(children: SvgElement[]): SvgElement {
214
+ return {
215
+ tag: 'svg',
216
+ attributes: {
217
+ xmlns: 'http://www.w3.org/2000/svg', viewBox: `0 0 ${SYMBOL_WIDTH} ${SYMBOL_HEIGHT}`,
218
+ width: SYMBOL_WIDTH, height: SYMBOL_HEIGHT,
219
+ },
220
+ children,
221
+ }
222
+ }