|
1 | 1 | import type { AppearPreset, ComponentDefinition, ElementNode, GraphNode, InstanceNode, NodeTree, OwnerRef, ProjectState } from '@shared/types'; |
2 | 2 | import { repeatProp, STYLE_SCOPES, isAppearPreset, isBinding, slotFallback } from '@shared/types'; |
3 | 3 | import { pageParents } from '@shared/page-tree'; |
| 4 | +import { CODE_TEXT_ATTR, CODE_TEXT_HOOK } from '@shared/code-text'; |
4 | 5 |
|
5 | 6 | export class EngineError extends Error { |
6 | 7 | constructor( |
@@ -134,26 +135,150 @@ export function enclosingRepeatProp(def: ComponentDefinition, nodeId: string): s |
134 | 135 | /** |
135 | 136 | * The links a list repeats whose visible text and destination are two fields of the same item (a contact method's |
136 | 137 | * value and link): `{ text, link }` field names. Read from the definition's own tree, so a project's copy with |
137 | | - * renamed fields pairs the same way; a code component has no tree to read and pairs nothing. A destination shared |
138 | | - * by two different texts belongs to neither, so it is left out. |
| 138 | + * renamed fields pairs the same way, or from a code component's markup (codeItemLinks). A destination shared by two |
| 139 | + * different texts belongs to neither, so it is left out. |
139 | 140 | */ |
140 | 141 | export function itemLinkPairs(def: ComponentDefinition, listProp: string): Array<{ text: string; link: string }> { |
141 | 142 | const schema = Object.hasOwn(def.properties, listProp) ? def.properties[listProp] : undefined; |
142 | | - if (!def.tree || schema?.type !== 'list') return []; |
| 143 | + if (schema?.type !== 'list') return []; |
| 144 | + const shown: Array<{ text: string; link: string }> = []; |
| 145 | + if (def.tree) { |
| 146 | + for (const n of Object.values(def.tree.nodes)) { |
| 147 | + if (n.kind !== 'link' || !isBinding(n.label) || !isBinding(n.link) || !('$item' in n.label) || !('$item' in n.link)) continue; |
| 148 | + if (enclosingRepeatProp(def, n.id) === listProp) shown.push({ text: n.label.$item, link: n.link.$item }); |
| 149 | + } |
| 150 | + } else if (def.kind === 'code') shown.push(...codeItemLinks(def.source ?? '', listProp)); |
143 | 151 | const found = new Map<string, { text: string; link: string }>(); |
144 | | - for (const n of Object.values(def.tree.nodes)) { |
145 | | - if (n.kind !== 'link' || !isBinding(n.label) || !isBinding(n.link) || !('$item' in n.label) || !('$item' in n.link)) continue; |
146 | | - const text = n.label.$item; |
147 | | - const link = n.link.$item; |
| 152 | + for (const { text, link } of shown) { |
148 | 153 | if (!Object.hasOwn(schema.fields, text) || !Object.hasOwn(schema.fields, link)) continue; |
149 | 154 | if (schema.fields[text].type !== 'string' || schema.fields[link].type !== 'link') continue; |
150 | | - if (enclosingRepeatProp(def, n.id) !== listProp) continue; |
151 | 155 | found.set(`${text}\u0000${link}`, { text, link }); |
152 | 156 | } |
153 | 157 | const pairs = [...found.values()]; |
154 | 158 | return pairs.filter((p) => pairs.filter((q) => q.link === p.link).length === 1); |
155 | 159 | } |
156 | 160 |
|
| 161 | +/** `name.field` alone: an item's field, as a link's destination or its only content. */ |
| 162 | +const ITEM_FIELD = /^\s*([A-Za-z_$][\w$]*)\s*\.\s*([A-Za-z_$][\w$]*)\s*$/; |
| 163 | +/** The text hook's call for an item's field (src/shared/code-text.ts): its list, field and the item it names. */ |
| 164 | +const ITEM_TEXT_HOOK = new RegExp(`^\\s*${CODE_TEXT_HOOK}\\s*\\?\\.\\s*\\(\\s*(['"])([A-Za-z][\\w]*)\\.([A-Za-z][\\w]*)\\1\\s*,\\s*([A-Za-z_$][\\w$]*)\\s*\\.`); |
| 165 | +/** The SoLinks of a code source, read once per source. */ |
| 166 | +const codeLinkCache = new Map<string, CodeItemLink[]>(); |
| 167 | +type CodeItemLink = { list: string; text: string; link: string }; |
| 168 | + |
| 169 | +/** |
| 170 | + * The links a code component draws for each item of a list, read from the marker that already tells the canvas which |
| 171 | + * field a link shows (src/shared/code-text.ts): |
| 172 | + * `<SoLink data-so-text={so_text?.('items.value', item.id)} link={item.link}>{item.value}</SoLink>` reads as |
| 173 | + * `{ text: 'value', link: 'link' }` of `items`. The marker names the list, so no reading of the blocks around it is |
| 174 | + * needed; a link whose marker, destination and content are not all the same item's, or whose tag spreads attributes |
| 175 | + * that could replace the link, pairs nothing. |
| 176 | + */ |
| 177 | +function codeItemLinks(source: string, listProp: string): Array<{ text: string; link: string }> { |
| 178 | + let links = codeLinkCache.get(source); |
| 179 | + if (!links) { |
| 180 | + links = readCodeItemLinks(source); |
| 181 | + if (codeLinkCache.size >= 64) codeLinkCache.delete(codeLinkCache.keys().next().value!); |
| 182 | + codeLinkCache.set(source, links); |
| 183 | + } |
| 184 | + return links.filter((l) => l.list === listProp).map(({ text, link }) => ({ text, link })); |
| 185 | +} |
| 186 | + |
| 187 | +/** |
| 188 | + * Every marked `<SoLink …>{item.field}</SoLink>` of a source, in one forward pass that steps over comments, scripts and |
| 189 | + * styles, which draw no links. |
| 190 | + */ |
| 191 | +function readCodeItemLinks(source: string): CodeItemLink[] { |
| 192 | + const out: CodeItemLink[] = []; |
| 193 | + const next = /<SoLink|<!--|<(script|style)\b/g; |
| 194 | + for (let m = next.exec(source); m; m = next.exec(source)) { |
| 195 | + const at = m.index; |
| 196 | + if (m[0] === '<!--' || m[1]) { |
| 197 | + const close = m[1] ? source.indexOf(`</${m[1]}>`, at) : source.indexOf('-->', at + 4); |
| 198 | + if (close < 0) break; |
| 199 | + next.lastIndex = close + 1; |
| 200 | + continue; |
| 201 | + } |
| 202 | + const tag = readTag(source, at + '<SoLink'.length); |
| 203 | + next.lastIndex = Math.max(tag.end, at + 1); |
| 204 | + if (!tag.attrs) continue; |
| 205 | + const close = source.indexOf('</SoLink>', tag.end); |
| 206 | + // No link closes after this one, so none of the rest has content to read. |
| 207 | + if (close < 0) break; |
| 208 | + // Content is one short expression: a longer run is not read. |
| 209 | + const content = close - tag.end > 400 ? null : /^\s*\{([^{}]*)\}\s*$/.exec(source.slice(tag.end, close)); |
| 210 | + const shown = content && ITEM_FIELD.exec(content[1]); |
| 211 | + const marker = tag.attrs.get(CODE_TEXT_ATTR); |
| 212 | + const hook = marker && ITEM_TEXT_HOOK.exec(marker); |
| 213 | + const to = tag.attrs.get('link'); |
| 214 | + const link = to && ITEM_FIELD.exec(to); |
| 215 | + // An href or a click handler of its own decides where the link goes instead. |
| 216 | + if (!shown || !hook || !link || [...tag.attrs.keys()].some((k) => k === 'href' || /^on(:|[a-z])/i.test(k))) continue; |
| 217 | + const [, item, text] = shown; |
| 218 | + // An item made in the markup (`{@const item = …}`) is not the stored one, whatever its ID. |
| 219 | + if (new RegExp(`\\{@const\\s+${item.replace(/\$/g, '\\$')}\\b`).test(source)) continue; |
| 220 | + if (hook[4] === item && link[1] === item && hook[3] === text) out.push({ list: hook[2], text, link: link[2] }); |
| 221 | + } |
| 222 | + return out; |
| 223 | +} |
| 224 | + |
| 225 | +/** |
| 226 | + * The attributes of a tag from just after its name, with the expressions in braces as written (strings in them |
| 227 | + * skipped, braces counted), and where it ends. No attributes when it spreads any, or cannot be read to its `>`. |
| 228 | + */ |
| 229 | +function readTag(src: string, from: number): { attrs: Map<string, string> | null; end: number } { |
| 230 | + const attrs = new Map<string, string>(); |
| 231 | + let i = from; |
| 232 | + const expression = (): string | null => { |
| 233 | + const start = i; |
| 234 | + let depth = 0; |
| 235 | + for (; i < src.length; i++) { |
| 236 | + const c = src[i]; |
| 237 | + if (c === '"' || c === "'" || c === '`') { |
| 238 | + const q = src.indexOf(c, i + 1); |
| 239 | + // Unclosed to the end: nothing after it is read again. |
| 240 | + if (q < 0) return ((i = src.length), null); |
| 241 | + i = q; |
| 242 | + } else if (c === '{') depth++; |
| 243 | + else if (c === '}' && --depth === 0) return src.slice(start + 1, i++); |
| 244 | + } |
| 245 | + return null; |
| 246 | + }; |
| 247 | + if (!/\s|>|\//.test(src[i] ?? '')) return { attrs: null, end: i }; |
| 248 | + while (i < src.length) { |
| 249 | + while (/\s/.test(src[i] ?? '')) i++; |
| 250 | + if (src[i] === '>') return { attrs, end: i + 1 }; |
| 251 | + if (src.startsWith('/>', i)) return { attrs: null, end: i + 2 }; |
| 252 | + // A spread, or the `{link}` shorthand: either way what the link opens is not read here. |
| 253 | + if (src[i] === '{') return { attrs: null, end: i + 1 }; |
| 254 | + const name = /^[^\s=/>"'{}<]+/.exec(src.slice(i, i + 200)); |
| 255 | + if (!name) return { attrs: null, end: i }; |
| 256 | + i += name[0].length; |
| 257 | + let value = ''; |
| 258 | + while (/\s/.test(src[i] ?? '')) i++; |
| 259 | + if (src[i] === '=') { |
| 260 | + i++; |
| 261 | + while (/\s/.test(src[i] ?? '')) i++; |
| 262 | + if (src[i] === '{') { |
| 263 | + const v = expression(); |
| 264 | + if (v === null) return { attrs: null, end: i }; |
| 265 | + value = v; |
| 266 | + } else if (src[i] === '"' || src[i] === "'") { |
| 267 | + const q = src.indexOf(src[i], i + 1); |
| 268 | + if (q < 0) return { attrs: null, end: src.length }; |
| 269 | + // Text around expressions, whose quotes can sit inside them, is not read: the tag pairs nothing. |
| 270 | + if (src.slice(i, q).includes('{')) return { attrs: null, end: q + 1 }; |
| 271 | + value = ''; |
| 272 | + i = q + 1; |
| 273 | + } else return { attrs: null, end: i }; |
| 274 | + } |
| 275 | + // An attribute named twice is not one value. |
| 276 | + if (attrs.has(name[0])) return { attrs: null, end: i }; |
| 277 | + attrs.set(name[0], value); |
| 278 | + } |
| 279 | + return { attrs: null, end: i }; |
| 280 | +} |
| 281 | + |
157 | 282 | /** |
158 | 283 | * Whether a code component places the override classes it is given (so style changes, hiding included, show). |
159 | 284 | * A theme's declares the controls its root takes; a project's own shows it in its script by taking `class` |
|
0 commit comments