@getcortexlabs/create-mavis 0.1.0-rc.13 → 0.1.0-rc.14
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 +26 -5
- package/package.json +4 -2
- package/src/adapters.mjs +7 -0
- package/src/add-macos.mjs +386 -0
- package/src/capabilities.mjs +78 -57
- package/src/cli.mjs +86 -4
- package/src/doctor.mjs +100 -18
- package/src/macos-capabilities.generated.mjs +4190 -0
- package/src/macos.mjs +147 -29
- package/src/migrate-directory.mjs +121 -0
- package/src/portability.mjs +108 -5
- package/src/scaffold.mjs +21 -2
- package/src/ui.mjs +21 -12
- package/templates/directory/host/directory-storage.ts +78 -0
- package/templates/directory/macos/DirectoryStorage.swift +68 -0
- package/templates/directory/mobile/src/app.ts +11 -8
- package/templates/directory/nuxt/app/plugins/mavis.ts +11 -8
- package/templates/directory/shared/Edit.vue +8 -5
- package/templates/directory/shared/directory-store.ts +42 -46
- package/templates/directory/vite/src/main.ts +11 -8
- package/templates/macos/examples/tasks/shared/TaskDetail.lite.tsx +2 -2
- package/templates/macos/examples/tasks/shared/Tasks.lite.tsx +2 -2
- package/templates/macos/examples/tasks/shared/types.ts +1 -0
- package/templates/macos/mobile/src/App.vue +4 -2
- package/templates/macos/native/Package.swift +3 -1
- package/templates/macos/native/Sources/MavisDesktop/App.swift +26 -11
- package/templates/macos/native/Sources/MavisDesktop/Fonts/LICENSE +45 -0
- package/templates/macos/native/Sources/MavisDesktop/Fonts/lucide.ttf +0 -0
- package/templates/macos/scripts/generate.mjs +3 -26
- package/templates/macos/scripts/macos-manifest.mjs +33 -0
- package/templates/macos/scripts/macos.mjs +20 -18
- package/templates/macos/web/index.html +1 -1
- package/templates/macos/web/src/main.js +176 -3
- package/templates/macos/web/src/mavis.js +2 -0
- package/templates/macos/web/src/style.css +2 -1
- package/templates/macos/web/vite.config.mjs +12 -1
- package/templates/migrations/directory-rc13.json +36 -0
- package/templates/mobile/src/mavis.ts +2 -82
- package/templates/mobile/webpack.config.js +4 -0
- package/templates/nuxt/app/mavis.ts +2 -84
- package/templates/vite/src/mavis.ts +2 -84
- package/templates/macos/scripts/portable/emit-swift.mjs +0 -1287
- package/templates/macos/scripts/portable/emit-vue.mjs +0 -615
- package/templates/macos/scripts/portable/index.mjs +0 -95
- package/templates/macos/scripts/portable/model.mjs +0 -1441
- package/templates/macos/scripts/portable/program.mjs +0 -163
|
@@ -1,615 +0,0 @@
|
|
|
1
|
-
// Vue output: the model printed back as JavaScript, so the Web runs the source's own semantics.
|
|
2
|
-
|
|
3
|
-
const tsType = (type) => {
|
|
4
|
-
switch (type.k) {
|
|
5
|
-
case 'string':
|
|
6
|
-
return 'string'
|
|
7
|
-
case 'number':
|
|
8
|
-
return 'number'
|
|
9
|
-
case 'boolean':
|
|
10
|
-
return 'boolean'
|
|
11
|
-
case 'record':
|
|
12
|
-
return type.name
|
|
13
|
-
case 'array':
|
|
14
|
-
return `${type.of.k === 'optional' || type.of.k === 'function' ? `(${tsType(type.of)})` : tsType(type.of)}[]`
|
|
15
|
-
case 'optional':
|
|
16
|
-
return `${tsType(type.of)} | undefined`
|
|
17
|
-
case 'function':
|
|
18
|
-
return `(${type.params.map((param, i) => `arg${i}: ${tsType(param)}`).join(', ')}) => void`
|
|
19
|
-
default:
|
|
20
|
-
return 'void'
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const jsString = (text) =>
|
|
25
|
-
`'${text
|
|
26
|
-
.replace(/\\/g, '\\\\')
|
|
27
|
-
.replace(/'/g, "\\'")
|
|
28
|
-
.replace(/\n/g, '\\n')
|
|
29
|
-
.replace(/\r/g, '\\r')
|
|
30
|
-
.replace(/\u2028/g, '\\u2028')
|
|
31
|
-
.replace(/\u2029/g, '\\u2029')}'`
|
|
32
|
-
|
|
33
|
-
const template = (text) => text.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\$\{/g, '\\${')
|
|
34
|
-
|
|
35
|
-
export function js(e) {
|
|
36
|
-
switch (e.e) {
|
|
37
|
-
case 'lit':
|
|
38
|
-
return typeof e.value === 'string'
|
|
39
|
-
? jsString(e.value)
|
|
40
|
-
: Object.is(e.value, -0)
|
|
41
|
-
? '-0'
|
|
42
|
-
: String(e.value)
|
|
43
|
-
case 'undefined':
|
|
44
|
-
return 'undefined'
|
|
45
|
-
case 'template':
|
|
46
|
-
return `\`${e.parts.map((part) => (typeof part === 'string' ? template(part) : `\${${js(part)}}`)).join('')}\``
|
|
47
|
-
case 'state':
|
|
48
|
-
case 'getter':
|
|
49
|
-
return `state.${e.name}`
|
|
50
|
-
case 'prop':
|
|
51
|
-
return `props.${e.name}`
|
|
52
|
-
case 'local':
|
|
53
|
-
return e.name
|
|
54
|
-
case 'length':
|
|
55
|
-
return `${wrap(e.object)}.length`
|
|
56
|
-
case 'field':
|
|
57
|
-
return `${wrap(e.object)}${e.optional ? '?.' : '.'}${e.name}`
|
|
58
|
-
case 'call':
|
|
59
|
-
return `${wrap(e.object)}.${e.method}(${e.args.map(js).join(', ')})`
|
|
60
|
-
case 'math':
|
|
61
|
-
return `Math.${e.fn}(${e.args.map(js).join(', ')})`
|
|
62
|
-
case 'arrow':
|
|
63
|
-
return `(${e.params.join(', ')}) => ${e.body.e === 'record' ? `(${js(e.body)})` : js(e.body)}`
|
|
64
|
-
case 'index':
|
|
65
|
-
return `${wrap(e.object)}[${js(e.index)}]`
|
|
66
|
-
case 'unary':
|
|
67
|
-
return `${e.op}${wrap(e.arg)}`
|
|
68
|
-
case 'binary':
|
|
69
|
-
return `(${js(e.left)} ${e.op === 'concat' ? '+' : e.op} ${js(e.right)})`
|
|
70
|
-
case 'logical':
|
|
71
|
-
return `(${js(e.left)} ${e.op} ${js(e.right)})`
|
|
72
|
-
case 'cond':
|
|
73
|
-
return `(${js(e.test)} ? ${js(e.then)} : ${js(e.else)})`
|
|
74
|
-
case 'record':
|
|
75
|
-
return `{ ${[...(e.spread ? [`...${js(e.spread)}`] : []), ...e.fields.map((field) => `${field.name}: ${js(field.value)}`)].join(', ')} }`
|
|
76
|
-
case 'array':
|
|
77
|
-
return `[${e.items.map(js).join(', ')}]`
|
|
78
|
-
}
|
|
79
|
-
throw new Error(`Unknown expression ${e.e}`)
|
|
80
|
-
}
|
|
81
|
-
const wrap = (e) =>
|
|
82
|
-
['state', 'getter', 'prop', 'local', 'field', 'call', 'length', 'lit', 'array', 'math'].includes(
|
|
83
|
-
e.e,
|
|
84
|
-
) && !(e.e === 'lit' && typeof e.value === 'number' && e.value < 0)
|
|
85
|
-
? js(e)
|
|
86
|
-
: `(${js(e)})`
|
|
87
|
-
|
|
88
|
-
const path = (target) => ['state', target.name, ...target.fields].join('.')
|
|
89
|
-
|
|
90
|
-
export function statements(list, indent) {
|
|
91
|
-
const pad = ' '.repeat(indent)
|
|
92
|
-
return list
|
|
93
|
-
.map((s) => {
|
|
94
|
-
switch (s.s) {
|
|
95
|
-
case 'assign':
|
|
96
|
-
return `${pad}${path(s.target)} ${s.op} ${js(s.value)}`
|
|
97
|
-
case 'const':
|
|
98
|
-
return `${pad}const ${s.name} = ${js(s.value)}`
|
|
99
|
-
case 'return':
|
|
100
|
-
return `${pad}return`
|
|
101
|
-
case 'guard':
|
|
102
|
-
return `${pad}if (${s.name} === undefined) return`
|
|
103
|
-
case 'iflet': {
|
|
104
|
-
const otherwise = s.else.length
|
|
105
|
-
? ` else {\n${statements(s.else, indent + 2)}\n${pad}}`
|
|
106
|
-
: ''
|
|
107
|
-
return `${pad}if (${s.name} !== undefined) {\n${statements(s.then, indent + 2)}\n${pad}}${otherwise}`
|
|
108
|
-
}
|
|
109
|
-
case 'action':
|
|
110
|
-
return `${pad}state.${s.name}(${s.args.map(js).join(', ')})`
|
|
111
|
-
case 'focus':
|
|
112
|
-
case 'blur':
|
|
113
|
-
return `${pad}${s.ref}.value?.${s.s}()`
|
|
114
|
-
case 'callback':
|
|
115
|
-
return `${pad}props.${s.name}(${s.args.map(js).join(', ')})`
|
|
116
|
-
case 'push':
|
|
117
|
-
case 'unshift':
|
|
118
|
-
return `${pad}${path(s.target)}.${s.s}(${js(s.value)})`
|
|
119
|
-
case 'splice':
|
|
120
|
-
return `${pad}${path(s.target)}.splice(${[s.start, s.deleteCount, ...s.items].map(js).join(', ')})`
|
|
121
|
-
case 'if': {
|
|
122
|
-
const otherwise =
|
|
123
|
-
s.else.length === 1 && s.else[0].s === 'if'
|
|
124
|
-
? ` else ${statements(s.else, indent).trimStart()}`
|
|
125
|
-
: s.else.length
|
|
126
|
-
? ` else {\n${statements(s.else, indent + 2)}\n${pad}}`
|
|
127
|
-
: ''
|
|
128
|
-
return `${pad}if (${js(s.test)}) {\n${statements(s.then, indent + 2)}\n${pad}}${otherwise}`
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
throw new Error(`Unknown statement ${s.s}`)
|
|
132
|
-
})
|
|
133
|
-
.join('\n')
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const attribute = (text) => text.replace(/&/g, '&').replace(/"/g, '"')
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Generated JavaScript as it goes into a template. The Vue compiler decodes character
|
|
140
|
-
* references such as & there, and a double quote ends an attribute value, so both are
|
|
141
|
-
* written as JavaScript escapes inside string and template literals. Operators stay as
|
|
142
|
-
* written: vue-tsc reads the expression from the file, where && is not JavaScript.
|
|
143
|
-
*/
|
|
144
|
-
function embed(code, quoted) {
|
|
145
|
-
let out = ''
|
|
146
|
-
const open = []
|
|
147
|
-
for (let index = 0; index < code.length; index++) {
|
|
148
|
-
const char = code[index]
|
|
149
|
-
const top = open.at(-1)
|
|
150
|
-
if (top === "'" || top === '`') {
|
|
151
|
-
if (char === '\\') out += char + code[++index]
|
|
152
|
-
else if (char === top) {
|
|
153
|
-
open.pop()
|
|
154
|
-
out += char
|
|
155
|
-
} else if (top === '`' && char === '$' && code[index + 1] === '{') {
|
|
156
|
-
open.push('{')
|
|
157
|
-
out += '${'
|
|
158
|
-
index++
|
|
159
|
-
} else if (char === '&') out += '\\x26'
|
|
160
|
-
else if (char === '"' && quoted) out += '\\x22'
|
|
161
|
-
else out += char
|
|
162
|
-
continue
|
|
163
|
-
}
|
|
164
|
-
if (char === "'" || char === '`') open.push(char)
|
|
165
|
-
else if (char === '{' && top === '{') open.push('{')
|
|
166
|
-
else if (char === '}' && top === '{') open.pop()
|
|
167
|
-
else if (char === '"' && quoted) throw new Error('Unexpected double quote in generated code.')
|
|
168
|
-
out += char
|
|
169
|
-
}
|
|
170
|
-
return out
|
|
171
|
-
}
|
|
172
|
-
const expression = (code) => embed(code, true)
|
|
173
|
-
|
|
174
|
-
function handlerJs(handler) {
|
|
175
|
-
const params = handler.params
|
|
176
|
-
.filter((param) => !param.unused)
|
|
177
|
-
.map((param) => param.name)
|
|
178
|
-
.join(', ')
|
|
179
|
-
if (handler.body.length === 1 && !['if', 'const', 'return'].includes(handler.body[0].s))
|
|
180
|
-
return `(${params}) => ${statements(handler.body, 0)}`
|
|
181
|
-
return `(${params}) => {\n${statements(handler.body, 2)}\n}`
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
function textContent(parts) {
|
|
185
|
-
const simple =
|
|
186
|
-
parts.every((part) => typeof part === 'string') && !/[<>&{}]|^\s|\s$|\s\s/.test(parts.join(''))
|
|
187
|
-
if (simple) return parts.join('')
|
|
188
|
-
const literal =
|
|
189
|
-
parts.length === 1 && typeof parts[0] !== 'string'
|
|
190
|
-
? js(parts[0])
|
|
191
|
-
: `\`${parts.map((part) => (typeof part === 'string' ? template(part) : `\${${js(part)}}`)).join('')}\``
|
|
192
|
-
return `{{ ${embed(literal, false).replace(/}}/g, '} }')} }}`
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// Source property names that differ from the Mavis Web component's.
|
|
196
|
-
const renamed = {
|
|
197
|
-
MInput: { value: 'modelValue', onChange: 'update:modelValue', onEscape: 'keydown.esc' },
|
|
198
|
-
MCheckbox: { checked: 'modelValue', onChange: 'update:modelValue' },
|
|
199
|
-
MSelect: { value: 'modelValue', onChange: 'update:modelValue' },
|
|
200
|
-
MColumn: { label: 'accessibilityLabel' },
|
|
201
|
-
MRow: { label: 'accessibilityLabel' },
|
|
202
|
-
MCard: { label: 'accessibilityLabel' },
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// One emitVue call at a time: the host flavor and the list regions of the component being printed.
|
|
206
|
-
let target = 'web'
|
|
207
|
-
let regions = new Map()
|
|
208
|
-
|
|
209
|
-
// Hardware keys and DOM focus repair only exist in the Web flavor.
|
|
210
|
-
const webOnly = (name) => name === 'onKeyDown' || name === 'onEscape'
|
|
211
|
-
|
|
212
|
-
const walk = (node, visit) => {
|
|
213
|
-
visit(node)
|
|
214
|
-
children(node).forEach((child) => walk(child, visit))
|
|
215
|
-
}
|
|
216
|
-
const nodesWhere = (view, test) => {
|
|
217
|
-
const found = []
|
|
218
|
-
walk(view, (node) => test(node) && found.push(node))
|
|
219
|
-
return found
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
function props(node) {
|
|
223
|
-
const names = renamed[node.tag] ?? {}
|
|
224
|
-
const entries = Object.entries(node.props).filter(([name]) => target === 'web' || !webOnly(name))
|
|
225
|
-
const attributes = entries.map(([name, value]) => {
|
|
226
|
-
if (name === 'onKeyDown')
|
|
227
|
-
return `tabindex="0" @keydown="${expression(`(event) => mavisKey(event, ${handlerJs(value)})`)}"`
|
|
228
|
-
// The card paints with a theme color; shortcuts are registered in the script.
|
|
229
|
-
if (node.tag === 'MCard' && name === 'tone')
|
|
230
|
-
return `:backgroundColor="${expression('lit' in value ? `mavisTheme.colors.${value.lit}` : `mavisTheme.colors[${js(value.expr)} as keyof typeof mavisTheme.colors]`)}"`
|
|
231
|
-
if (node.tag === 'MCard' && name === 'onClick')
|
|
232
|
-
return `interactive @click="${expression(handlerJs(value))}"`
|
|
233
|
-
if (name === 'shortcut') return ''
|
|
234
|
-
// MSelect reports string or number choices; these options are strings.
|
|
235
|
-
if (node.tag === 'MSelect' && name === 'onChange' && value.params.some((p) => !p.unused))
|
|
236
|
-
return `@update:modelValue="${expression(`(choice) => (${handlerJs(value)})(String(choice))`)}"`
|
|
237
|
-
const target = names[name] ?? name
|
|
238
|
-
if ('ref' in value) return `ref="${value.ref}"`
|
|
239
|
-
if ('body' in value)
|
|
240
|
-
return `@${target.startsWith('on') ? target[2].toLowerCase() + target.slice(3) : target}="${expression(handlerJs(value))}"`
|
|
241
|
-
if ('lit' in value) return `${target}="${attribute(value.lit)}"`
|
|
242
|
-
// MSelect takes null for no choice; the source writes undefined.
|
|
243
|
-
if (node.tag === 'MSelect' && name === 'value')
|
|
244
|
-
return `:${target}="${expression(`${js(value.expr)} ?? null`)}"`
|
|
245
|
-
return `:${target}="${expression(js(value.expr))}"`
|
|
246
|
-
})
|
|
247
|
-
if (target === 'web' && (node.tag === 'MColumn' || node.tag === 'MRow') && node.props.label)
|
|
248
|
-
attributes.unshift('role="group"')
|
|
249
|
-
if (regions.has(node)) attributes.push(`:motionRegion="${regions.get(node).name}"`)
|
|
250
|
-
return attributes
|
|
251
|
-
.filter(Boolean)
|
|
252
|
-
.map((text) => ` ${text}`)
|
|
253
|
-
.join('')
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
function render(nodes, indent) {
|
|
257
|
-
const pad = ' '.repeat(indent)
|
|
258
|
-
return nodes
|
|
259
|
-
.map((node) => {
|
|
260
|
-
switch (node.v) {
|
|
261
|
-
case 'text':
|
|
262
|
-
return `${pad}<MText${props(node)}>${textContent(node.parts)}</MText>`
|
|
263
|
-
case 'element': {
|
|
264
|
-
const keep = listParent(node) ? ' v-mavis-keep-focus' : ''
|
|
265
|
-
return node.children.length
|
|
266
|
-
? `${pad}<${node.tag}${props(node)}${keep}>\n${render(node.children, indent + 2)}\n${pad}</${node.tag}>`
|
|
267
|
-
: `${pad}<${node.tag}${props(node)} />`
|
|
268
|
-
}
|
|
269
|
-
case 'component':
|
|
270
|
-
return `${pad}<${node.name}${node.props
|
|
271
|
-
.map(({ name, value }) =>
|
|
272
|
-
value.handler
|
|
273
|
-
? ` :${name}="${expression(handlerJs(value.handler))}"`
|
|
274
|
-
: ` :${name}="${expression(js(value.expr))}"`,
|
|
275
|
-
)
|
|
276
|
-
.join('')} />`
|
|
277
|
-
case 'show': {
|
|
278
|
-
if (node.animate) {
|
|
279
|
-
// MReveal keeps leaving content until its exit finishes, with the column's spacing.
|
|
280
|
-
const gap = node.gap ? ` gap="${node.gap}"` : ''
|
|
281
|
-
const reveal = (when, body) =>
|
|
282
|
-
`${pad}<MReveal :show="${expression(when)}"${gap}>\n${render(body, indent + 2)}\n${pad}</MReveal>`
|
|
283
|
-
const shown = js(node.when)
|
|
284
|
-
return [
|
|
285
|
-
reveal(shown, node.then),
|
|
286
|
-
...(node.else.length ? [reveal(`!${wrap(node.when)}`, node.else)] : []),
|
|
287
|
-
].join('\n')
|
|
288
|
-
}
|
|
289
|
-
const then = `${pad}<template v-if="${expression(js(node.when))}">\n${render(node.then, indent + 2)}\n${pad}</template>`
|
|
290
|
-
return node.else.length
|
|
291
|
-
? `${then}\n${pad}<template v-else>\n${render(node.else, indent + 2)}\n${pad}</template>`
|
|
292
|
-
: then
|
|
293
|
-
}
|
|
294
|
-
case 'for': {
|
|
295
|
-
const alias = node.index ? `(${node.item}, ${node.index})` : node.item
|
|
296
|
-
const key = node.key ? `${node.item}.${node.key}` : node.item
|
|
297
|
-
return `${pad}<template v-for="${alias} in ${expression(js(node.each))}" :key="${key}">\n${render([node.body], indent + 2)}\n${pad}</template>`
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
throw new Error(`Unknown view ${node.v}`)
|
|
301
|
-
})
|
|
302
|
-
.join('\n')
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
const runtimeProp = (type) => {
|
|
306
|
-
const base = type.k === 'optional' ? type.of : type
|
|
307
|
-
const constructor = {
|
|
308
|
-
string: 'String',
|
|
309
|
-
number: 'Number',
|
|
310
|
-
boolean: 'Boolean',
|
|
311
|
-
record: 'Object',
|
|
312
|
-
array: 'Array',
|
|
313
|
-
function: 'Function',
|
|
314
|
-
}[base.k]
|
|
315
|
-
const cast = ['record', 'array', 'function'].includes(base.k)
|
|
316
|
-
? ` as PropType<${tsType(base)}>`
|
|
317
|
-
: ''
|
|
318
|
-
return `{ type: ${constructor}${cast}, required: ${type.k !== 'optional'} }`
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
const animatedFor = (node) =>
|
|
322
|
-
children(node)
|
|
323
|
-
.map((child) =>
|
|
324
|
-
child.v === 'for' && child.animate ? child : child.v === 'show' ? animatedFor(child) : null,
|
|
325
|
-
)
|
|
326
|
-
.find(Boolean)
|
|
327
|
-
|
|
328
|
-
/** target 'web' prints for Mavis Web in a browser, 'native' for Mavis on NativeScript. */
|
|
329
|
-
export function emitVue(component, header, { target: flavor = 'web' } = {}) {
|
|
330
|
-
target = flavor
|
|
331
|
-
regions = new Map()
|
|
332
|
-
const visit = (node) => {
|
|
333
|
-
if (node.animatesList) {
|
|
334
|
-
const list = animatedFor(node)
|
|
335
|
-
regions.set(node, { name: `mavisRows${regions.size + 1}`, list })
|
|
336
|
-
}
|
|
337
|
-
children(node).forEach(visit)
|
|
338
|
-
}
|
|
339
|
-
visit(component.view)
|
|
340
|
-
const records = new Set()
|
|
341
|
-
const collect = (type) => {
|
|
342
|
-
if (type.k === 'record') records.add(type.name)
|
|
343
|
-
if (type.of) collect(type.of)
|
|
344
|
-
if (type.params) type.params.forEach(collect)
|
|
345
|
-
}
|
|
346
|
-
component.props.forEach((prop) => collect(prop.type))
|
|
347
|
-
component.state.forEach((value) => collect(value.type))
|
|
348
|
-
component.getters.forEach((getter) => collect(getter.type))
|
|
349
|
-
component.actions.forEach((action) => action.params.forEach((param) => collect(param.type)))
|
|
350
|
-
const needsPropType = component.props.some((prop) =>
|
|
351
|
-
['record', 'array', 'function'].includes(
|
|
352
|
-
(prop.type.k === 'optional' ? prop.type.of : prop.type).k,
|
|
353
|
-
),
|
|
354
|
-
)
|
|
355
|
-
const lines = [`<!-- ${header} -->`, '<script setup lang="ts">']
|
|
356
|
-
const vueImports = [
|
|
357
|
-
...(component.state.length || component.getters.length || component.actions.length
|
|
358
|
-
? ['reactive']
|
|
359
|
-
: []),
|
|
360
|
-
...(component.refs.length ? ['ref'] : []),
|
|
361
|
-
...(needsPropType ? ['type PropType'] : []),
|
|
362
|
-
]
|
|
363
|
-
if (vueImports.length) lines.push(`import { ${vueImports.join(', ')} } from 'vue'`)
|
|
364
|
-
for (const name of [...component.imports.keys()].sort())
|
|
365
|
-
lines.push(`import ${name} from './${name}.vue'`)
|
|
366
|
-
if (records.size) lines.push(`import type { ${[...records].sort().join(', ')} } from './types'`)
|
|
367
|
-
const shortcuts =
|
|
368
|
-
flavor === 'web'
|
|
369
|
-
? nodesWhere(component.view, (node) => node.tag === 'MButton' && node.props.shortcut)
|
|
370
|
-
: []
|
|
371
|
-
const themed = nodesWhere(
|
|
372
|
-
component.view,
|
|
373
|
-
(node) => node.tag === 'MCard' && node.props.tone,
|
|
374
|
-
).length
|
|
375
|
-
const helpers = [
|
|
376
|
-
...(flavor === 'web' && usesKeys(component.view) ? ['mavisKey'] : []),
|
|
377
|
-
...(shortcuts.length ? ['useMavisShortcuts'] : []),
|
|
378
|
-
...(hasFor(component.view) ? ['vMavisKeepFocus'] : []),
|
|
379
|
-
]
|
|
380
|
-
if (helpers.length) lines.push(`import { ${helpers.join(', ')} } from './runtime'`)
|
|
381
|
-
// Keyed list layout runs through the renderer's Jolt adapter on both hosts.
|
|
382
|
-
const core = [
|
|
383
|
-
...(regions.size ? ['useComponentLayout'] : []),
|
|
384
|
-
...(themed ? ['useMavisTheme'] : []),
|
|
385
|
-
]
|
|
386
|
-
if (core.length) lines.push(`import { ${core.join(', ')} } from '@getcortexlabs/mavis-core/vue'`)
|
|
387
|
-
lines.push('')
|
|
388
|
-
for (const reference of component.refs)
|
|
389
|
-
lines.push(`const ${reference.name} = ref<{ focus(): void; blur(): void }>()`)
|
|
390
|
-
if (component.props.length) {
|
|
391
|
-
lines.push('const props = defineProps({')
|
|
392
|
-
for (const prop of component.props) lines.push(` ${prop.name}: ${runtimeProp(prop.type)},`)
|
|
393
|
-
lines.push('})')
|
|
394
|
-
}
|
|
395
|
-
const members = [
|
|
396
|
-
...component.state.map(
|
|
397
|
-
(value) =>
|
|
398
|
-
` ${value.name}: ${js(value.init)}${['record', 'array', 'optional'].includes(value.type.k) || value.init.e === 'lit' ? ` as ${tsType(value.type)}` : ''},`,
|
|
399
|
-
),
|
|
400
|
-
...component.getters.map((getter) =>
|
|
401
|
-
[
|
|
402
|
-
` get ${getter.name}(): ${tsType(getter.type)} {`,
|
|
403
|
-
...getter.locals.map((local) => ` const ${local.name} = ${js(local.value)}`),
|
|
404
|
-
` return ${js(getter.expr)}`,
|
|
405
|
-
' },',
|
|
406
|
-
].join('\n'),
|
|
407
|
-
),
|
|
408
|
-
...component.actions.map((action) =>
|
|
409
|
-
[
|
|
410
|
-
` ${action.name}(${action.params.map((param) => `${param.name}: ${tsType(param.type)}`).join(', ')}): void {`,
|
|
411
|
-
statements(action.body, 4),
|
|
412
|
-
' },',
|
|
413
|
-
]
|
|
414
|
-
.filter(Boolean)
|
|
415
|
-
.join('\n'),
|
|
416
|
-
),
|
|
417
|
-
]
|
|
418
|
-
if (members.length) lines.push('const state = reactive({', ...members, '})')
|
|
419
|
-
if (themed) lines.push('const { theme: mavisTheme } = useMavisTheme()')
|
|
420
|
-
if (shortcuts.length)
|
|
421
|
-
lines.push(
|
|
422
|
-
'useMavisShortcuts([',
|
|
423
|
-
...shortcuts.map(
|
|
424
|
-
({ props: button }) =>
|
|
425
|
-
` { key: ${jsString(button.shortcut.lit.slice(4))}, ${button.disabled ? `disabled: () => ${js(button.disabled.expr)}, ` : ''}run: ${handlerJs(button.onClick)} },`,
|
|
426
|
-
),
|
|
427
|
-
'])',
|
|
428
|
-
)
|
|
429
|
-
for (const { name, list } of regions.values())
|
|
430
|
-
lines.push(
|
|
431
|
-
`const ${name} = useComponentLayout(() => ${wrap(list.each)}.map((${list.item}) => ${list.key ? `${list.item}.${list.key}` : list.item}))`,
|
|
432
|
-
)
|
|
433
|
-
lines.push('</script>', '', '<template>', render([component.view], 2), '</template>', '')
|
|
434
|
-
return lines.join('\n')
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
const children = (node) => [
|
|
438
|
-
...(node.children ?? []),
|
|
439
|
-
...(node.then ?? []),
|
|
440
|
-
...(node.else ?? []),
|
|
441
|
-
...(node.body ? [node.body] : []),
|
|
442
|
-
]
|
|
443
|
-
export const usesKeys = (node) => Boolean(node.props?.onKeyDown) || children(node).some(usesKeys)
|
|
444
|
-
const hasFor = (node) => node.v === 'for' || children(node).some(hasFor)
|
|
445
|
-
// A For whose rows are this element's DOM children, through Show and For but no other element.
|
|
446
|
-
const listParent = (node) =>
|
|
447
|
-
children(node).some((child) => child.v === 'for' || (child.v === 'show' && listParent(child)))
|
|
448
|
-
const usesShortcuts = (node) =>
|
|
449
|
-
Boolean(node.tag === 'MButton' && node.props.shortcut) || children(node).some(usesShortcuts)
|
|
450
|
-
export const usesRuntime = (node, flavor = 'web') =>
|
|
451
|
-
(flavor === 'web' && (usesKeys(node) || usesShortcuts(node))) || hasFor(node)
|
|
452
|
-
|
|
453
|
-
/** NativeScript's runtime: the same focus repair, on NativeScript views. */
|
|
454
|
-
export function emitVueNativeRuntime(header) {
|
|
455
|
-
return `// ${header}
|
|
456
|
-
import type { Directive } from 'vue'
|
|
457
|
-
import { TextField, TextView, isIOS, type View } from '@nativescript/core'
|
|
458
|
-
|
|
459
|
-
type Editor = TextField | TextView
|
|
460
|
-
type Child = { nativeView?: View }
|
|
461
|
-
type Parent = Child & {
|
|
462
|
-
insertBefore(child: Child, anchor: unknown): void
|
|
463
|
-
appendChild(child: Child): void
|
|
464
|
-
}
|
|
465
|
-
type Selection = { ios?: unknown; start?: number; end?: number }
|
|
466
|
-
|
|
467
|
-
function focusedEditor(root: View | undefined): Editor | undefined {
|
|
468
|
-
let found: Editor | undefined
|
|
469
|
-
const visit = (view: View) => {
|
|
470
|
-
if (found) return
|
|
471
|
-
if ((view instanceof TextField || view instanceof TextView) && hasFocus(view)) found = view
|
|
472
|
-
view.eachChildView((child) => {
|
|
473
|
-
visit(child)
|
|
474
|
-
return !found
|
|
475
|
-
})
|
|
476
|
-
}
|
|
477
|
-
if (root) visit(root)
|
|
478
|
-
return found
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
const hasFocus = (view: View) =>
|
|
482
|
-
isIOS ? Boolean(view.ios?.isFirstResponder) : Boolean(view.android?.hasFocus())
|
|
483
|
-
|
|
484
|
-
function selection(editor: Editor): Selection {
|
|
485
|
-
if (isIOS) return { ios: editor.ios?.selectedTextRange }
|
|
486
|
-
const native = editor.android as { getSelectionStart(): number; getSelectionEnd(): number }
|
|
487
|
-
return { start: native.getSelectionStart(), end: native.getSelectionEnd() }
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
function restore(editor: Editor, saved: Selection) {
|
|
491
|
-
editor.focus()
|
|
492
|
-
if (isIOS) {
|
|
493
|
-
if (saved.ios) editor.ios.selectedTextRange = saved.ios
|
|
494
|
-
} else if (saved.start !== undefined && saved.end !== undefined)
|
|
495
|
-
(editor.android as { setSelection(start: number, end: number): void }).setSelection(
|
|
496
|
-
saved.start,
|
|
497
|
-
saved.end,
|
|
498
|
-
)
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
/**
|
|
502
|
-
* On the element whose children are a keyed list. NativeScript-Vue moves a row by removing
|
|
503
|
-
* and adding its view, which ends editing: when the row holds the focused field, focus and
|
|
504
|
-
* the text selection go back to it.
|
|
505
|
-
*/
|
|
506
|
-
export const vMavisKeepFocus: Directive<Parent> = {
|
|
507
|
-
mounted(element) {
|
|
508
|
-
const keep = (child: Child, move: () => void) => {
|
|
509
|
-
const editor = focusedEditor(child.nativeView)
|
|
510
|
-
const saved = editor && selection(editor)
|
|
511
|
-
move()
|
|
512
|
-
if (editor && saved && !hasFocus(editor)) restore(editor, saved)
|
|
513
|
-
}
|
|
514
|
-
const insertBefore = element.insertBefore.bind(element)
|
|
515
|
-
const appendChild = element.appendChild.bind(element)
|
|
516
|
-
element.insertBefore = (child, anchor) => keep(child, () => insertBefore(child, anchor))
|
|
517
|
-
element.appendChild = (child) => keep(child, () => appendChild(child))
|
|
518
|
-
},
|
|
519
|
-
beforeUnmount(element) {
|
|
520
|
-
Reflect.deleteProperty(element, 'insertBefore')
|
|
521
|
-
Reflect.deleteProperty(element, 'appendChild')
|
|
522
|
-
},
|
|
523
|
-
}
|
|
524
|
-
`
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
/** Shared by the generated components that handle keys on a stack. */
|
|
528
|
-
export function emitVueRuntime(header) {
|
|
529
|
-
return `// ${header}
|
|
530
|
-
import { onBeforeUnmount, onMounted, type Directive } from 'vue'
|
|
531
|
-
|
|
532
|
-
export type MavisKey = 'ArrowUp' | 'ArrowDown' | 'ArrowLeft' | 'ArrowRight'
|
|
533
|
-
|
|
534
|
-
const keys = new Set<string>(['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'])
|
|
535
|
-
|
|
536
|
-
/** Keys pressed while a stack or one of its rows has focus. Text editing keeps its own keys. */
|
|
537
|
-
export function mavisKey(event: KeyboardEvent, handle: (key: MavisKey) => void) {
|
|
538
|
-
if (event.defaultPrevented || event.isComposing || event.altKey || event.ctrlKey || event.metaKey) return
|
|
539
|
-
if (!keys.has(event.key)) return
|
|
540
|
-
const target = event.target as Element | null
|
|
541
|
-
if (target?.closest('input, textarea, select, [contenteditable]')) return
|
|
542
|
-
event.preventDefault()
|
|
543
|
-
handle(event.key as MavisKey)
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
type Shortcut = { key: string; disabled?: () => boolean; run: () => void }
|
|
547
|
-
|
|
548
|
-
/** Control or Command with a key, anywhere on the page, for the buttons that declare one. */
|
|
549
|
-
export function useMavisShortcuts(shortcuts: Shortcut[]) {
|
|
550
|
-
const listen = (event: KeyboardEvent) => {
|
|
551
|
-
if (event.defaultPrevented || event.repeat || event.altKey || event.shiftKey) return
|
|
552
|
-
if (!event.metaKey && !event.ctrlKey) return
|
|
553
|
-
const key = event.key.length === 1 ? event.key.toLowerCase() : event.key
|
|
554
|
-
const match = shortcuts.find((shortcut) => shortcut.key === key)
|
|
555
|
-
if (!match) return
|
|
556
|
-
event.preventDefault()
|
|
557
|
-
if (!match.disabled?.()) match.run()
|
|
558
|
-
}
|
|
559
|
-
// Before the focused field sees the key, as a menu shortcut would be.
|
|
560
|
-
onMounted(() => document.addEventListener('keydown', listen, true))
|
|
561
|
-
onBeforeUnmount(() => document.removeEventListener('keydown', listen, true))
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
const editable = (element: Element): element is HTMLInputElement | HTMLTextAreaElement =>
|
|
565
|
-
element instanceof HTMLTextAreaElement ||
|
|
566
|
-
(element instanceof HTMLInputElement && element.selectionStart !== null)
|
|
567
|
-
|
|
568
|
-
type Mover = Element & { moveBefore?: (node: Node, child: Node | null) => void }
|
|
569
|
-
|
|
570
|
-
/**
|
|
571
|
-
* On the element whose children are a keyed list. Vue reorders rows with insertBefore,
|
|
572
|
-
* and browsers drop focus from a moved element: an edited row would lose its caret.
|
|
573
|
-
* Moves that carry the focused element use moveBefore where the browser has it, or put
|
|
574
|
-
* focus and the text selection back right away.
|
|
575
|
-
*/
|
|
576
|
-
export const vMavisKeepFocus: Directive<HTMLElement> = {
|
|
577
|
-
mounted(element) {
|
|
578
|
-
const insert = element.insertBefore
|
|
579
|
-
element.insertBefore = function <T extends Node>(this: Mover, node: T, child: Node | null): T {
|
|
580
|
-
const active = document.activeElement
|
|
581
|
-
if (!node.isConnected || !(active instanceof HTMLElement) || !node.contains(active))
|
|
582
|
-
return insert.call(this, node, child) as T
|
|
583
|
-
if (typeof this.moveBefore === 'function') {
|
|
584
|
-
try {
|
|
585
|
-
this.moveBefore(node, child)
|
|
586
|
-
return node
|
|
587
|
-
} catch {}
|
|
588
|
-
}
|
|
589
|
-
const selection = editable(active)
|
|
590
|
-
? ([active.selectionStart, active.selectionEnd, active.selectionDirection] as const)
|
|
591
|
-
: undefined
|
|
592
|
-
insert.call(this, node, child)
|
|
593
|
-
active.focus({ preventScroll: true })
|
|
594
|
-
if (selection && editable(active) && selection[0] !== null && selection[1] !== null)
|
|
595
|
-
active.setSelectionRange(selection[0], selection[1], selection[2] ?? undefined)
|
|
596
|
-
return node
|
|
597
|
-
}
|
|
598
|
-
},
|
|
599
|
-
beforeUnmount(element) {
|
|
600
|
-
Reflect.deleteProperty(element, 'insertBefore')
|
|
601
|
-
},
|
|
602
|
-
}
|
|
603
|
-
`
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
export function emitVueTypes(records, header) {
|
|
607
|
-
return [
|
|
608
|
-
`// ${header}`,
|
|
609
|
-
...records.map(
|
|
610
|
-
(record) =>
|
|
611
|
-
`export type ${record.name} = {\n${record.fields.map((field) => ` ${field.name}: ${tsType(field.type)}`).join('\n')}\n}`,
|
|
612
|
-
),
|
|
613
|
-
'',
|
|
614
|
-
].join('\n')
|
|
615
|
-
}
|