soma-core 1.4.0 → 1.4.2
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 +1 -1
- package/core/build/blocks/block-compile.ts +31 -2
- package/core/build/blocks/block-container.ts +2 -11
- package/core/build/compiler/page-declaration-compiler.ts +30 -2
- package/core/build/compiler/soma-precompile.js +2053 -23
- package/core/build/component/component-graph.ts +208 -0
- package/core/build/component/package-loader.ts +32 -0
- package/core/build/eject/component-eject.ts +473 -0
- package/core/build/eject/component-upgrade.ts +359 -0
- package/core/build/layout/constraint-solver.ts +235 -0
- package/core/build/layout/css-emitter.ts +116 -0
- package/core/build/layout/page-layout.ts +85 -0
- package/core/build/pack/pack-init.ts +270 -0
- package/core/client/base/SomaHttpClient.ts +34 -1
- package/core/client/base/SomaPageRootBase.ts +61 -0
- package/core/client/component/ai-catalog.ts +121 -4
- package/core/client/component/component-defaults.ts +100 -0
- package/core/client/component/component-definition.types.ts +39 -0
- package/core/client/component/component-manager.ts +79 -4
- package/core/client/component/component-pack.ts +126 -2
- package/core/client/component/keyboard-matrix.ts +144 -0
- package/core/client/component/pack-styles.ts +59 -1
- package/core/client/component/style-features.ts +131 -0
- package/core/client/component/theme-component-tokens.ts +83 -0
- package/core/client/component/verify-pack.ts +120 -1
- package/core/client/container/SomaContainerAdapter.ts +6 -2
- package/core/client/data/data-object-resolver.ts +407 -4
- package/core/client/data/reaction-engine.ts +176 -0
- package/core/client/data/window-engine.ts +477 -0
- package/core/client/engine/event-engine.ts +98 -0
- package/core/client/engine/soma-bootstrap.ts +265 -5
- package/core/client/engine/step-runner.ts +134 -0
- package/core/client/infra/http/SomaHttpService.ts +2 -2
- package/core/client/parser/blocks/page-parser.ts +15 -1
- package/core/client/parser/blocks/use-parser.ts +5 -12
- package/core/client/parser/data-config-parser.ts +316 -8
- package/core/client/parser/duration.ts +16 -0
- package/core/client/parser/layout-parser.ts +157 -0
- package/core/client/parser/page-state-parser.ts +166 -0
- package/core/client/parser/pipeline-parser.ts +76 -1
- package/core/client/parser/reaction-parser.ts +459 -0
- package/core/client/parser/reaction-sugar.ts +47 -0
- package/core/client/parser/step-parser.ts +359 -0
- package/core/client/parser/ui-config-parser.ts +59 -0
- package/core/client/parser/window-parser.ts +294 -0
- package/core/client/theme/theme-engine.ts +102 -2
- package/core/index.ts +184 -0
- package/core/shared/block-errors.ts +11 -0
- package/core/shared/calc/coerce.ts +29 -0
- package/core/shared/calc/errors.ts +43 -0
- package/core/shared/calc/evaluator.ts +101 -0
- package/core/shared/calc/index.ts +12 -0
- package/core/shared/calc/lexer.ts +194 -0
- package/core/shared/calc/parser.ts +204 -0
- package/core/shared/calc/typer.ts +184 -0
- package/core/shared/component/derive-sample.ts +105 -0
- package/core/shared/component/field-lines.ts +473 -0
- package/core/shared/path-algebra.ts +169 -0
- package/core/shared/predicates/index.ts +527 -0
- package/core/shared/types/component-contract.types.ts +151 -9
- package/core/shared/types/data-config.types.ts +120 -1
- package/core/shared/types/domain.types.ts +13 -0
- package/core/shared/types/event.types.ts +22 -2
- package/core/shared/types/layout-constraint.types.ts +73 -0
- package/core/shared/types/page-state.types.ts +60 -0
- package/core/shared/types/reaction.types.ts +144 -0
- package/core/shared/types/step.types.ts +91 -0
- package/core/shared/types/ui-config.types.ts +18 -0
- package/core/shared/types/window.types.ts +177 -0
- package/dist/cli/soma.js +13 -1
- package/dist/core/index.cjs +5529 -631
- package/dist/types/build/blocks/block-container.d.ts +1 -6
- package/dist/types/build/compiler/page-declaration-compiler.d.ts +2 -0
- package/dist/types/build/component/component-graph.d.ts +85 -0
- package/dist/types/build/eject/component-eject.d.ts +121 -0
- package/dist/types/build/eject/component-upgrade.d.ts +43 -0
- package/dist/types/build/layout/constraint-solver.d.ts +36 -0
- package/dist/types/build/layout/css-emitter.d.ts +18 -0
- package/dist/types/build/layout/page-layout.d.ts +24 -0
- package/dist/types/build/pack/pack-init.d.ts +8 -0
- package/dist/types/client/base/SomaHttpClient.d.ts +8 -0
- package/dist/types/client/base/SomaPageRootBase.d.ts +28 -0
- package/dist/types/client/component/ai-catalog.d.ts +33 -1
- package/dist/types/client/component/component-defaults.d.ts +15 -0
- package/dist/types/client/component/component-definition.types.d.ts +38 -0
- package/dist/types/client/component/component-manager.d.ts +36 -0
- package/dist/types/client/component/component-pack.d.ts +32 -2
- package/dist/types/client/component/keyboard-matrix.d.ts +24 -0
- package/dist/types/client/component/pack-styles.d.ts +21 -0
- package/dist/types/client/component/style-features.d.ts +26 -0
- package/dist/types/client/component/theme-component-tokens.d.ts +14 -0
- package/dist/types/client/component/verify-pack.d.ts +10 -0
- package/dist/types/client/data/data-object-resolver.d.ts +106 -1
- package/dist/types/client/data/reaction-engine.d.ts +21 -0
- package/dist/types/client/data/window-engine.d.ts +27 -0
- package/dist/types/client/engine/event-engine.d.ts +15 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +70 -0
- package/dist/types/client/engine/step-runner.d.ts +24 -0
- package/dist/types/client/parser/blocks/use-parser.d.ts +2 -2
- package/dist/types/client/parser/duration.d.ts +1 -0
- package/dist/types/client/parser/layout-parser.d.ts +9 -0
- package/dist/types/client/parser/page-state-parser.d.ts +32 -0
- package/dist/types/client/parser/reaction-parser.d.ts +16 -0
- package/dist/types/client/parser/reaction-sugar.d.ts +13 -0
- package/dist/types/client/parser/step-parser.d.ts +23 -0
- package/dist/types/client/parser/ui-config-parser.d.ts +5 -0
- package/dist/types/client/parser/window-parser.d.ts +25 -0
- package/dist/types/client/theme/theme-engine.d.ts +26 -0
- package/dist/types/index.d.ts +35 -2
- package/dist/types/shared/block-errors.d.ts +5 -0
- package/dist/types/shared/calc/coerce.d.ts +4 -0
- package/dist/types/shared/calc/errors.d.ts +14 -0
- package/dist/types/shared/calc/evaluator.d.ts +2 -0
- package/dist/types/shared/calc/index.d.ts +6 -0
- package/dist/types/shared/calc/lexer.d.ts +20 -0
- package/dist/types/shared/calc/parser.d.ts +49 -0
- package/dist/types/shared/calc/typer.d.ts +16 -0
- package/dist/types/shared/component/derive-sample.d.ts +23 -0
- package/dist/types/shared/component/field-lines.d.ts +19 -0
- package/dist/types/shared/path-algebra.d.ts +56 -0
- package/dist/types/shared/predicates/index.d.ts +79 -0
- package/dist/types/shared/types/component-contract.types.d.ts +134 -6
- package/dist/types/shared/types/data-config.types.d.ts +115 -1
- package/dist/types/shared/types/domain.types.d.ts +12 -0
- package/dist/types/shared/types/event.types.d.ts +8 -2
- package/dist/types/shared/types/layout-constraint.types.d.ts +52 -0
- package/dist/types/shared/types/page-state.types.d.ts +49 -0
- package/dist/types/shared/types/reaction.types.d.ts +115 -0
- package/dist/types/shared/types/step.types.d.ts +76 -0
- package/dist/types/shared/types/ui-config.types.d.ts +18 -0
- package/dist/types/shared/types/window.types.d.ts +171 -0
- package/package.json +3 -2
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
// `soma component upgrade`(REQ-010 F-45)——把上游的改动合进宿主已经改过的产物。
|
|
2
|
+
//
|
|
3
|
+
// ── 为什么这件事必须做 ────────────────────────────────────────────────
|
|
4
|
+
// shadcn 的模式是"复制源码进你的仓库",代价是**从此与上游断链**:上游修了一个
|
|
5
|
+
// 无障碍缺陷,你的副本永远修不到,除非有人记得去看 changelog。它的 registry 里
|
|
6
|
+
// 甚至没有版本号与哈希,所以连"我这份是从哪一版复制的"都答不出来。
|
|
7
|
+
//
|
|
8
|
+
// SOMA 的 eject 产物带 provenance(来源 / 版本 / sha256)并在 `.soma-eject-base/`
|
|
9
|
+
// 里留了一份**基线**(eject 那一刻的上游原文)。有了基线,升级就是一次标准的三方合并:
|
|
10
|
+
//
|
|
11
|
+
// 基线(共同祖先) ── 上游新版
|
|
12
|
+
// └──────────── 宿主当前版
|
|
13
|
+
//
|
|
14
|
+
// 上游改 A 行、宿主改 B 行 → 自动合并;两边改同一行 → 写冲突标记并**不更新基线**
|
|
15
|
+
// (基线一旦提前更新,那次冲突就再也没有共同祖先可依了)。
|
|
16
|
+
//
|
|
17
|
+
// 这是 shadcn 结构上做不到的:它没有基线,只能做两方比较,而两方比较分不清
|
|
18
|
+
// "这一行是上游改的"还是"这一行是我改的"。
|
|
19
|
+
|
|
20
|
+
import { createHash } from 'node:crypto';
|
|
21
|
+
import fs from 'node:fs';
|
|
22
|
+
import path from 'node:path';
|
|
23
|
+
import {
|
|
24
|
+
BASE_DIR,
|
|
25
|
+
ComponentEjectError,
|
|
26
|
+
readProvenance,
|
|
27
|
+
renderUnifiedDiff,
|
|
28
|
+
rewriteImportsForHost,
|
|
29
|
+
splitProvenanceHeader,
|
|
30
|
+
type ComponentProvenance,
|
|
31
|
+
} from './component-eject';
|
|
32
|
+
|
|
33
|
+
export interface ComponentUpgradeOptions {
|
|
34
|
+
/** 宿主包里的产物文件 */
|
|
35
|
+
file: string;
|
|
36
|
+
/** soma-ui 源根 */
|
|
37
|
+
uiRoot: string;
|
|
38
|
+
/** 只看不写 */
|
|
39
|
+
dryRun?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface ComponentUpgradeResult {
|
|
43
|
+
componentName: string;
|
|
44
|
+
/** 上游没动过:什么都不用做 */
|
|
45
|
+
upToDate: boolean;
|
|
46
|
+
/** 合并后的内容(dryRun 时也给) */
|
|
47
|
+
merged?: string;
|
|
48
|
+
/** 冲突块数;> 0 时基线**不**更新 */
|
|
49
|
+
conflicts: number;
|
|
50
|
+
/** 自动合进来的上游改动行数 */
|
|
51
|
+
applied: number;
|
|
52
|
+
ejectedFrom: ComponentProvenance;
|
|
53
|
+
currentVersion: string;
|
|
54
|
+
/** 供人复核的统一 diff(基线 → 上游新版) */
|
|
55
|
+
upstreamDiff?: string;
|
|
56
|
+
written: string[];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 三方合并的一块:要么是三边一致的公共段,要么是一处分歧 */
|
|
60
|
+
interface MergeChunk {
|
|
61
|
+
kind: 'common' | 'ours' | 'theirs' | 'conflict';
|
|
62
|
+
base: string[];
|
|
63
|
+
ours: string[];
|
|
64
|
+
theirs: string[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function upgradeEjectedComponent(options: ComponentUpgradeOptions): ComponentUpgradeResult {
|
|
68
|
+
const { file, uiRoot } = options;
|
|
69
|
+
if (!fs.existsSync(file)) throw new ComponentEjectError(`找不到文件 ${file}`);
|
|
70
|
+
|
|
71
|
+
const oursRaw = fs.readFileSync(file, 'utf8');
|
|
72
|
+
const provenance = readProvenance(oursRaw);
|
|
73
|
+
if (!provenance) {
|
|
74
|
+
throw new ComponentEjectError(
|
|
75
|
+
`${file} 里没有 provenance 头,它不是 \`soma component eject\` 的产物——无从知道该从哪一版升起`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// 三方要摆在**同一坐标系**里,否则合并出来的差异一半不是任何人改的:
|
|
80
|
+
// · 产物带 provenance 头,基线与上游不带 → 剥掉头再合,最后原样贴回;
|
|
81
|
+
// · 产物的 import 是改写过的公开子路径,基线与上游是内部相对路径
|
|
82
|
+
// → 用 eject 的同一张表把基线与上游也改写一遍。
|
|
83
|
+
// 少任何一步,上游一次普通的 import 调整都会变成一处凭空的冲突。
|
|
84
|
+
const { header, body: ours } = splitProvenanceHeader(oursRaw);
|
|
85
|
+
|
|
86
|
+
const upstreamPath = path.join(uiRoot, provenance.source);
|
|
87
|
+
if (!fs.existsSync(upstreamPath)) {
|
|
88
|
+
throw new ComponentEjectError(
|
|
89
|
+
`上游文件 ${provenance.source} 已不存在——该组件可能已改名或被删除。`
|
|
90
|
+
+ '\n 这种情况不能自动升级:改名意味着契约可能也变了,需要人来判断如何迁移',
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
const theirsUpstream = fs.readFileSync(upstreamPath, 'utf8');
|
|
94
|
+
const theirs = rewriteImportsForHost(theirsUpstream);
|
|
95
|
+
const componentName = path.basename(file).replace(/\.ts$/, '');
|
|
96
|
+
const currentVersion = readVersion(uiRoot);
|
|
97
|
+
|
|
98
|
+
const basePath = path.join(path.dirname(file), BASE_DIR, path.basename(file));
|
|
99
|
+
if (!fs.existsSync(basePath)) {
|
|
100
|
+
throw new ComponentEjectError(
|
|
101
|
+
`找不到基线 ${basePath}——没有共同祖先就做不了三方合并。`
|
|
102
|
+
+ '\n 基线是 eject 那一刻的上游原文;若它被误删,只能重新 eject 一次再手工搬回你的改动',
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
const baseUpstream = fs.readFileSync(basePath, 'utf8');
|
|
106
|
+
const base = rewriteImportsForHost(baseUpstream);
|
|
107
|
+
// 上游动没动,比的是**原文**:改写是我们自己加的一层,不该影响这个判断
|
|
108
|
+
if (sha256Like(baseUpstream) === sha256Like(theirsUpstream)) {
|
|
109
|
+
return {
|
|
110
|
+
componentName,
|
|
111
|
+
upToDate: true,
|
|
112
|
+
conflicts: 0,
|
|
113
|
+
applied: 0,
|
|
114
|
+
ejectedFrom: provenance,
|
|
115
|
+
currentVersion,
|
|
116
|
+
written: [],
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const chunks = threeWayMerge(splitLines(base), splitLines(ours), splitLines(theirs));
|
|
121
|
+
const conflicts = chunks.filter((chunk) => chunk.kind === 'conflict').length;
|
|
122
|
+
const applied = chunks
|
|
123
|
+
.filter((chunk) => chunk.kind === 'theirs')
|
|
124
|
+
.reduce((sum, chunk) => sum + chunk.theirs.length, 0);
|
|
125
|
+
|
|
126
|
+
// 合并结果贴回 provenance 头(版本已升到当前上游版)
|
|
127
|
+
const merged = `${renderMergedHeader(header, currentVersion, theirsUpstream)}\n${renderMerged(chunks)}`;
|
|
128
|
+
// diff 给人看的是**上游原文**的变化,不是改写后的形态——
|
|
129
|
+
// 作者要对照的是 soma-ui 的 changelog,那边写的是原文
|
|
130
|
+
const upstreamDiff = renderUnifiedDiff(
|
|
131
|
+
provenance.source,
|
|
132
|
+
`(soma-ui@${provenance.version})`,
|
|
133
|
+
`(soma-ui@${currentVersion})`,
|
|
134
|
+
baseUpstream,
|
|
135
|
+
theirsUpstream,
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
const written: string[] = [];
|
|
139
|
+
if (options.dryRun !== true) {
|
|
140
|
+
fs.writeFileSync(file, merged, 'utf8');
|
|
141
|
+
written.push(file);
|
|
142
|
+
// 有冲突就**不**更新基线:基线一旦提前推进,这次没解完的冲突下一次升级时
|
|
143
|
+
// 就再也没有共同祖先可依了——那一刻起这个文件就永久脱离了升级链路。
|
|
144
|
+
if (conflicts === 0) {
|
|
145
|
+
// 基线存**原文**(与 eject 一致):它是"上游那一刻的样子",
|
|
146
|
+
// 存改写后的形态会让下一次的 diff 对不上 soma-ui 的 changelog
|
|
147
|
+
fs.writeFileSync(basePath, theirsUpstream, 'utf8');
|
|
148
|
+
written.push(basePath);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
componentName,
|
|
154
|
+
upToDate: false,
|
|
155
|
+
merged,
|
|
156
|
+
conflicts,
|
|
157
|
+
applied,
|
|
158
|
+
ejectedFrom: provenance,
|
|
159
|
+
currentVersion,
|
|
160
|
+
upstreamDiff,
|
|
161
|
+
written,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 三方合并。
|
|
167
|
+
*
|
|
168
|
+
* 做法:先分别求 base↔ours 与 base↔theirs 的 LCS 对齐,再沿 base 的行号同步扫两边。
|
|
169
|
+
* 同一段 base 上只有一边改 → 取那一边;两边都改且内容不同 → 冲突。
|
|
170
|
+
*
|
|
171
|
+
* 刻意不引 diff3 库:算法本身二十行,而引一个依赖要为它的供应链负责。
|
|
172
|
+
*/
|
|
173
|
+
export function threeWayMerge(
|
|
174
|
+
base: string[],
|
|
175
|
+
ours: string[],
|
|
176
|
+
theirs: string[],
|
|
177
|
+
): MergeChunk[] {
|
|
178
|
+
const ourMap = alignToBase(base, ours);
|
|
179
|
+
const theirMap = alignToBase(base, theirs);
|
|
180
|
+
|
|
181
|
+
const chunks: MergeChunk[] = [];
|
|
182
|
+
let baseIndex = 0;
|
|
183
|
+
let ourIndex = 0;
|
|
184
|
+
let theirIndex = 0;
|
|
185
|
+
|
|
186
|
+
const push = (chunk: MergeChunk): void => {
|
|
187
|
+
const last = chunks[chunks.length - 1];
|
|
188
|
+
// 相邻同类块合并:一行一块的话,冲突标记会把一段连续的分歧切成几十段
|
|
189
|
+
if (last && last.kind === chunk.kind && chunk.kind !== 'conflict') {
|
|
190
|
+
last.base.push(...chunk.base);
|
|
191
|
+
last.ours.push(...chunk.ours);
|
|
192
|
+
last.theirs.push(...chunk.theirs);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
chunks.push(chunk);
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
while (baseIndex < base.length) {
|
|
199
|
+
const ourAt = ourMap[baseIndex];
|
|
200
|
+
const theirAt = theirMap[baseIndex];
|
|
201
|
+
|
|
202
|
+
// 这一行在两边都还在,且它前面各自没有插入 → 公共行
|
|
203
|
+
if (ourAt !== -1 && theirAt !== -1) {
|
|
204
|
+
const ourInserted = ours.slice(ourIndex, ourAt);
|
|
205
|
+
const theirInserted = theirs.slice(theirIndex, theirAt);
|
|
206
|
+
if (ourInserted.length > 0 || theirInserted.length > 0) {
|
|
207
|
+
push(classify([], ourInserted, theirInserted));
|
|
208
|
+
}
|
|
209
|
+
push({ kind: 'common', base: [base[baseIndex]], ours: [base[baseIndex]], theirs: [base[baseIndex]] });
|
|
210
|
+
ourIndex = ourAt + 1;
|
|
211
|
+
theirIndex = theirAt + 1;
|
|
212
|
+
baseIndex += 1;
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// 这一行至少在一边被删/改:把连续的这一段收起来一起判
|
|
217
|
+
const runStart = baseIndex;
|
|
218
|
+
while (baseIndex < base.length && (ourMap[baseIndex] === -1 || theirMap[baseIndex] === -1)) {
|
|
219
|
+
baseIndex += 1;
|
|
220
|
+
}
|
|
221
|
+
const nextOur = baseIndex < base.length ? ourMap[baseIndex] : ours.length;
|
|
222
|
+
const nextTheir = baseIndex < base.length ? theirMap[baseIndex] : theirs.length;
|
|
223
|
+
push(classify(
|
|
224
|
+
base.slice(runStart, baseIndex),
|
|
225
|
+
ours.slice(ourIndex, nextOur),
|
|
226
|
+
theirs.slice(theirIndex, nextTheir),
|
|
227
|
+
));
|
|
228
|
+
ourIndex = nextOur;
|
|
229
|
+
theirIndex = nextTheir;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// 尾部残留
|
|
233
|
+
const tailOur = ours.slice(ourIndex);
|
|
234
|
+
const tailTheir = theirs.slice(theirIndex);
|
|
235
|
+
if (tailOur.length > 0 || tailTheir.length > 0) {
|
|
236
|
+
push(classify([], tailOur, tailTheir));
|
|
237
|
+
}
|
|
238
|
+
return chunks;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** 一段分歧的归类:谁动了、动的是不是同一件事 */
|
|
242
|
+
function classify(baseRun: string[], ourRun: string[], theirRun: string[]): MergeChunk {
|
|
243
|
+
const ourChanged = !sameLines(baseRun, ourRun);
|
|
244
|
+
const theirChanged = !sameLines(baseRun, theirRun);
|
|
245
|
+
if (!ourChanged && !theirChanged) {
|
|
246
|
+
return { kind: 'common', base: baseRun, ours: ourRun, theirs: theirRun };
|
|
247
|
+
}
|
|
248
|
+
if (ourChanged && !theirChanged) return { kind: 'ours', base: baseRun, ours: ourRun, theirs: theirRun };
|
|
249
|
+
if (!ourChanged && theirChanged) return { kind: 'theirs', base: baseRun, ours: ourRun, theirs: theirRun };
|
|
250
|
+
// 两边都改了:内容一样就不算冲突(两边各自做了同一个修改,这很常见)
|
|
251
|
+
if (sameLines(ourRun, theirRun)) {
|
|
252
|
+
return { kind: 'common', base: baseRun, ours: ourRun, theirs: theirRun };
|
|
253
|
+
}
|
|
254
|
+
return { kind: 'conflict', base: baseRun, ours: ourRun, theirs: theirRun };
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* 渲染合并结果。
|
|
259
|
+
*
|
|
260
|
+
* 冲突标记用 git 的形状(`<<<<<<<` / `=======` / `>>>>>>>`)——不是因为好看,
|
|
261
|
+
* 是因为每一个编辑器的冲突解决界面都认它。自创一套标记等于让宿主只能手工比对。
|
|
262
|
+
*/
|
|
263
|
+
function renderMerged(chunks: MergeChunk[]): string {
|
|
264
|
+
const out: string[] = [];
|
|
265
|
+
for (const chunk of chunks) {
|
|
266
|
+
if (chunk.kind === 'conflict') {
|
|
267
|
+
out.push('<<<<<<< 你的改动');
|
|
268
|
+
out.push(...chunk.ours);
|
|
269
|
+
out.push('=======');
|
|
270
|
+
out.push(...chunk.theirs);
|
|
271
|
+
out.push('>>>>>>> soma-ui(上游)');
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
out.push(...(chunk.kind === 'theirs' ? chunk.theirs : chunk.ours));
|
|
275
|
+
}
|
|
276
|
+
return out.join('\n');
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* 头里的版本与哈希要跟着升。
|
|
281
|
+
*
|
|
282
|
+
* 不升的话,下一次 `diff` / `upgrade` 会拿**旧版本号与旧哈希**去比——
|
|
283
|
+
* 表现是刚升完的组件立刻又报"上游有改动",而且报的是上一次已经合过的那批。
|
|
284
|
+
*/
|
|
285
|
+
function renderMergedHeader(header: string, toVersion: string, upstreamSource: string): string {
|
|
286
|
+
return header
|
|
287
|
+
.replace(/(@soma-ejected-from\s+[\w-]+)@\S+/, `$1@${toVersion}`)
|
|
288
|
+
.replace(/(@soma-ejected-sha256\s+)[0-9a-f]{64}/, `$1${sha256Hex(upstreamSource)}`);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* provenance 头里的哈希是 sha256(`component-eject` 写的那份)。
|
|
293
|
+
* 升级时必须用同一种算法重算,否则下一次 `diff` 会把"哈希对不上"当成上游改动。
|
|
294
|
+
*/
|
|
295
|
+
function sha256Hex(text: string): string {
|
|
296
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
297
|
+
return createHash('sha256').update(text, 'utf8').digest('hex');
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* 把 `other` 的行对齐到 `base` 的行号上:
|
|
302
|
+
* 返回长度等于 base 的数组,第 i 位是这一行在 other 里的下标,没有则 -1。
|
|
303
|
+
*/
|
|
304
|
+
function alignToBase(base: string[], other: string[]): number[] {
|
|
305
|
+
const rows = base.length;
|
|
306
|
+
const cols = other.length;
|
|
307
|
+
// LCS 表。组件文件是几百行量级,O(n·m) 完全够用;
|
|
308
|
+
// 真到几万行的时候该做的是拆文件,不是换算法。
|
|
309
|
+
const table: number[][] = Array.from({ length: rows + 1 }, () => new Array<number>(cols + 1).fill(0));
|
|
310
|
+
for (let i = rows - 1; i >= 0; i -= 1) {
|
|
311
|
+
for (let j = cols - 1; j >= 0; j -= 1) {
|
|
312
|
+
table[i][j] = base[i] === other[j]
|
|
313
|
+
? table[i + 1][j + 1] + 1
|
|
314
|
+
: Math.max(table[i + 1][j], table[i][j + 1]);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
const map = new Array<number>(rows).fill(-1);
|
|
318
|
+
let i = 0;
|
|
319
|
+
let j = 0;
|
|
320
|
+
while (i < rows && j < cols) {
|
|
321
|
+
if (base[i] === other[j]) {
|
|
322
|
+
map[i] = j;
|
|
323
|
+
i += 1;
|
|
324
|
+
j += 1;
|
|
325
|
+
} else if (table[i + 1][j] >= table[i][j + 1]) {
|
|
326
|
+
i += 1;
|
|
327
|
+
} else {
|
|
328
|
+
j += 1;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return map;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function splitLines(text: string): string[] {
|
|
335
|
+
return text.split('\n');
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function sameLines(a: string[], b: string[]): boolean {
|
|
339
|
+
return a.length === b.length && a.every((line, index) => line === b[index]);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** 内容指纹:这里只需要"一样还是不一样",不需要密码学强度 */
|
|
343
|
+
function sha256Like(text: string): string {
|
|
344
|
+
let hash = 0x811c9dc5;
|
|
345
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
346
|
+
hash ^= text.charCodeAt(index);
|
|
347
|
+
hash = (hash + (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24)) >>> 0;
|
|
348
|
+
}
|
|
349
|
+
return hash.toString(16);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function readVersion(uiRoot: string): string {
|
|
353
|
+
try {
|
|
354
|
+
const raw = fs.readFileSync(path.join(uiRoot, '..', 'package.json'), 'utf8');
|
|
355
|
+
return String((JSON.parse(raw) as { version?: string }).version ?? '0.0.0');
|
|
356
|
+
} catch {
|
|
357
|
+
return '0.0.0';
|
|
358
|
+
}
|
|
359
|
+
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
// 构建期约束求解(REQ-010 S3 / CL-1)。
|
|
2
|
+
//
|
|
3
|
+
// ── 为什么自己写而不是引 kiwi ────────────────────────────────────────
|
|
4
|
+
// 设计稿里写的是「依赖 @lume/kiwi 作 devDependency」。真去实现时问题的形状清楚了:
|
|
5
|
+
// 这是**单轴**分配问题——一行里若干子项分一条已知长度的线段,每项有 min/max 与
|
|
6
|
+
// 一个四档优先级。Cassowary 求解的是任意线性约束的一般情形(二维、双向依赖、
|
|
7
|
+
// 循环约束),而这里一个都用不上。
|
|
8
|
+
//
|
|
9
|
+
// 拿一般求解器解一个特殊问题的代价不是性能,是**可解释性**:单纯形法给出的"不可满足"
|
|
10
|
+
// 只能报出一组内部约束编号,翻译回作者写的那行 YAML 要另做一层映射,而那层映射本身
|
|
11
|
+
// 就是这里想避免的复杂度。逐档按优先级分配是确定性的,冲突时能直接指着两条声明说
|
|
12
|
+
// 「这两条打架」——CL-1 要求的正是这个。
|
|
13
|
+
//
|
|
14
|
+
// 换句话说:不引 kiwi 不是为了省一个依赖,是因为**自己写的错误信息更有用**。
|
|
15
|
+
// 如果哪天词汇扩到二维(行列同时约束、跨行对齐),这个判断要重新做。
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
SOMA_LAYOUT_PRIORITIES,
|
|
19
|
+
type SomaLayoutConstraint,
|
|
20
|
+
type SomaLayoutNodeConstraint,
|
|
21
|
+
type SomaLayoutPlan,
|
|
22
|
+
type SomaLayoutSolution,
|
|
23
|
+
} from '../../shared/types/layout-constraint.types';
|
|
24
|
+
|
|
25
|
+
/** 容器断点八档(rem)。与 soma-ui 的 `responsive-constants` 同源同值 */
|
|
26
|
+
export const CONTAINER_STEPS: ReadonlyArray<readonly [string, number]> = [
|
|
27
|
+
['cqXs', 20], ['cqSm', 24], ['cqMd', 28], ['cqLg', 36],
|
|
28
|
+
['cqXl', 40], ['cq2xl', 48], ['cq3xl', 56], ['cq4xl', 64],
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
export class SomaLayoutConstraintError extends Error {
|
|
32
|
+
/** 打架的两条声明(CL-1 要求错误信息含冲突约束对) */
|
|
33
|
+
readonly conflict: readonly [string, string];
|
|
34
|
+
|
|
35
|
+
constructor(message: string, conflict: readonly [string, string]) {
|
|
36
|
+
super(`[SOMA Layout] ${message}`);
|
|
37
|
+
this.name = 'SomaLayoutConstraintError';
|
|
38
|
+
this.conflict = conflict;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 长度字面量 → rem。
|
|
44
|
+
*
|
|
45
|
+
* 三种单位:`rem` 直取;`%` 与 `cqi` 都是"容器宽的百分比"(`cqi` 就是容器内联轴的 1%),
|
|
46
|
+
* 所以都按当前档的容器宽折算。折算而不是留到 CSS 里去算,是因为求解要在数上做——
|
|
47
|
+
* 留着符号的话,「min 有没有被违反」就成了一个运行期才知道答案的问题。
|
|
48
|
+
*/
|
|
49
|
+
export function toRem(literal: string, containerRem: number, where: string): number {
|
|
50
|
+
const text = String(literal).trim();
|
|
51
|
+
const match = /^(-?\d+(?:\.\d+)?)(rem|%|cqi)$/.exec(text);
|
|
52
|
+
if (!match) {
|
|
53
|
+
throw new SomaLayoutConstraintError(
|
|
54
|
+
`${where} 的长度 "${text}" 非法——只认 rem / % / cqi(单位必填,不猜)`,
|
|
55
|
+
[where, text],
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
const value = Number(match[1]);
|
|
59
|
+
if (value < 0) {
|
|
60
|
+
throw new SomaLayoutConstraintError(`${where} 的长度不能是负数(收到 ${text})`, [where, text]);
|
|
61
|
+
}
|
|
62
|
+
return match[2] === 'rem' ? value : (value / 100) * containerRem;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 声明校验(CL-3):优先级四档、min ≤ max、fixed 必须给 width */
|
|
66
|
+
export function validateConstraint(constraint: SomaLayoutConstraint, path: string): void {
|
|
67
|
+
const names = Object.keys(constraint.nodes ?? {});
|
|
68
|
+
if (names.length === 0) {
|
|
69
|
+
throw new SomaLayoutConstraintError(`${path} 至少要有一个子项`, [path, 'nodes']);
|
|
70
|
+
}
|
|
71
|
+
for (const name of names) {
|
|
72
|
+
const node = constraint.nodes[name];
|
|
73
|
+
const at = `${path}.${name}`;
|
|
74
|
+
if (node.priority !== undefined && !(SOMA_LAYOUT_PRIORITIES as readonly number[]).includes(node.priority)) {
|
|
75
|
+
throw new SomaLayoutConstraintError(
|
|
76
|
+
`${at}.priority 只能是 ${SOMA_LAYOUT_PRIORITIES.join(' / ')}(收到 ${node.priority})`
|
|
77
|
+
+ '——优先级只在相对次序上有意义,开放成任意整数只会让人写出自己也说不清差别的值',
|
|
78
|
+
[at, String(node.priority)],
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
if (node.sizing === 'fixed' && node.width === undefined) {
|
|
82
|
+
throw new SomaLayoutConstraintError(
|
|
83
|
+
`${at} 是 fixed 却没给 width——"固定"固定到多少必须说出来`,
|
|
84
|
+
[at, 'sizing: fixed'],
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
if (node.sizing === 'hug' && (node.min !== undefined || node.max !== undefined)) {
|
|
88
|
+
// hug 的宽度由内容决定,再给上下界就是两个来源在争同一个数
|
|
89
|
+
throw new SomaLayoutConstraintError(
|
|
90
|
+
`${at} 是 hug,不能同时给 min / max——hug 的宽度由内容决定,`
|
|
91
|
+
+ '再加上下界等于让两个来源争同一个数。要夹在区间里请改用 fill 或 fixed',
|
|
92
|
+
[at, 'sizing: hug'],
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
// min ≤ max 用 rem 直比即可:混单位(16rem vs 50%)要等到具体档才能比,
|
|
96
|
+
// 那一层的检查在 solveStep 里做(它拿得到容器宽)
|
|
97
|
+
if (node.min !== undefined && node.max !== undefined) {
|
|
98
|
+
const minRem = /rem$/.test(node.min) ? Number.parseFloat(node.min) : NaN;
|
|
99
|
+
const maxRem = /rem$/.test(node.max) ? Number.parseFloat(node.max) : NaN;
|
|
100
|
+
if (Number.isFinite(minRem) && Number.isFinite(maxRem) && minRem > maxRem) {
|
|
101
|
+
throw new SomaLayoutConstraintError(
|
|
102
|
+
`${at} 的 min (${node.min}) 大于 max (${node.max})——这条约束无解`,
|
|
103
|
+
[`${at}.min`, `${at}.max`],
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 单档求解。
|
|
112
|
+
*
|
|
113
|
+
* 算法(确定性,不迭代):
|
|
114
|
+
* 1. 每项先拿它的**基线**:fixed 取 width,fill 取 min(fill 的诉求是"多余的给我",
|
|
115
|
+
* 所以它从下界起步),hug 取 min 或 0;
|
|
116
|
+
* 2. 基线总和超过可用长度 → **按优先级从低到高压缩**,压到各自 min 为止;
|
|
117
|
+
* 3. 仍有富余 → 按优先级从低到高**分给 fill 项**(低优先级 = Content Hugging = 更愿意被拉大),
|
|
118
|
+
* 受各自 max 限制;
|
|
119
|
+
* 4. 压到全部 min 仍放不下 → 该档无解,交给 `wrap` 决定是换行还是报错。
|
|
120
|
+
*
|
|
121
|
+
* 第 2、3 步的优先级方向是相反的,这不是笔误:压缩时先动"不介意被压"的(低优先级),
|
|
122
|
+
* 拉伸时也先给"愿意被拉大"的(低优先级)。Auto Layout 的两个名字(Hugging /
|
|
123
|
+
* Compression Resistance)说的就是同一个数的两面。
|
|
124
|
+
*/
|
|
125
|
+
export function solveStep(
|
|
126
|
+
constraint: SomaLayoutConstraint,
|
|
127
|
+
containerRem: number,
|
|
128
|
+
breakpoint: string,
|
|
129
|
+
path: string,
|
|
130
|
+
): SomaLayoutSolution {
|
|
131
|
+
const names = Object.keys(constraint.nodes);
|
|
132
|
+
const gap = constraint.gap === undefined ? 0 : toRem(constraint.gap, containerRem, `${path}.gap`);
|
|
133
|
+
const available = containerRem - gap * Math.max(0, names.length - 1);
|
|
134
|
+
|
|
135
|
+
const at = (name: string): string => `${path}.${name}`;
|
|
136
|
+
const bounds = names.map((name) => {
|
|
137
|
+
const node: SomaLayoutNodeConstraint = constraint.nodes[name];
|
|
138
|
+
const min = node.min === undefined ? 0 : toRem(node.min, containerRem, `${at(name)}.min`);
|
|
139
|
+
const max = node.max === undefined
|
|
140
|
+
? Number.POSITIVE_INFINITY
|
|
141
|
+
: toRem(node.max, containerRem, `${at(name)}.max`);
|
|
142
|
+
if (min > max) {
|
|
143
|
+
throw new SomaLayoutConstraintError(
|
|
144
|
+
`${at(name)} 在 ${breakpoint}(容器 ${containerRem}rem)档上 min (${min}rem) 大于 max (${max}rem)`
|
|
145
|
+
+ '——混用 rem 与 % 时,两者的大小关系会随档变化,这一档翻过来了',
|
|
146
|
+
[`${at(name)}.min`, `${at(name)}.max`],
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
const base = node.sizing === 'fixed'
|
|
150
|
+
? toRem(node.width!, containerRem, `${at(name)}.width`)
|
|
151
|
+
: min;
|
|
152
|
+
return {
|
|
153
|
+
name,
|
|
154
|
+
node,
|
|
155
|
+
min,
|
|
156
|
+
max,
|
|
157
|
+
priority: node.priority ?? 500,
|
|
158
|
+
width: Math.min(Math.max(base, min), max),
|
|
159
|
+
};
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
let total = bounds.reduce((sum, item) => sum + item.width, 0);
|
|
163
|
+
|
|
164
|
+
// ── 步 2:压缩(低优先级先让) ────────────────────────────────────
|
|
165
|
+
if (total > available) {
|
|
166
|
+
for (const priority of SOMA_LAYOUT_PRIORITIES) {
|
|
167
|
+
if (total <= available) break;
|
|
168
|
+
// required(1000)不参与压缩:它就是"不可违反"的意思
|
|
169
|
+
if (priority === 1000) break;
|
|
170
|
+
const group = bounds.filter((item) => item.priority === priority && item.width > item.min);
|
|
171
|
+
const slack = group.reduce((sum, item) => sum + (item.width - item.min), 0);
|
|
172
|
+
if (slack <= 0) continue;
|
|
173
|
+
const need = Math.min(total - available, slack);
|
|
174
|
+
for (const item of group) {
|
|
175
|
+
const share = ((item.width - item.min) / slack) * need;
|
|
176
|
+
item.width -= share;
|
|
177
|
+
}
|
|
178
|
+
total -= need;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const wrapped = total > available + 1e-9;
|
|
183
|
+
if (wrapped && (constraint.wrap ?? 'none') === 'none') {
|
|
184
|
+
// 无解且不许换行:这才是真正的冲突。指着两条最挤的声明说话——
|
|
185
|
+
// 只说"放不下"的话,作者面对一行五个子项无从下手。
|
|
186
|
+
const sorted = [...bounds].sort((a, b) => b.min - a.min);
|
|
187
|
+
throw new SomaLayoutConstraintError(
|
|
188
|
+
`${path} 在 ${breakpoint}(容器 ${containerRem}rem)档上放不下:`
|
|
189
|
+
+ `各项 min 之和 ${total.toFixed(2)}rem 已超过可用 ${available.toFixed(2)}rem。`
|
|
190
|
+
+ `最挤的两项是 "${sorted[0]?.name}"(min ${sorted[0]?.min}rem) 与 `
|
|
191
|
+
+ `"${sorted[1]?.name}"(min ${sorted[1]?.min ?? 0}rem)。`
|
|
192
|
+
+ '把其中一个的 min 调小,或声明 wrap: below 让它们在窄档竖排。',
|
|
193
|
+
[at(sorted[0]?.name ?? ''), at(sorted[1]?.name ?? '')],
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// ── 步 3:分配富余(低优先级先拿) ────────────────────────────────
|
|
198
|
+
if (!wrapped && total < available) {
|
|
199
|
+
for (const priority of SOMA_LAYOUT_PRIORITIES) {
|
|
200
|
+
const rest = available - total;
|
|
201
|
+
if (rest <= 1e-9) break;
|
|
202
|
+
const group = bounds.filter(
|
|
203
|
+
(item) => item.priority === priority && item.node.sizing === 'fill' && item.width < item.max,
|
|
204
|
+
);
|
|
205
|
+
if (group.length === 0) continue;
|
|
206
|
+
const room = group.reduce((sum, item) => sum + (item.max - item.width), 0);
|
|
207
|
+
const give = Math.min(rest, room);
|
|
208
|
+
for (const item of group) {
|
|
209
|
+
const share = room === Number.POSITIVE_INFINITY
|
|
210
|
+
? give / group.length
|
|
211
|
+
: ((item.max - item.width) / room) * give;
|
|
212
|
+
item.width += share;
|
|
213
|
+
}
|
|
214
|
+
total += give;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const widths: Record<string, number> = {};
|
|
219
|
+
for (const item of bounds) widths[item.name] = round(item.width);
|
|
220
|
+
return { breakpoint, containerRem, widths, wrapped };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** 八档各解一次(CL-1) */
|
|
224
|
+
export function solveLayout(constraint: SomaLayoutConstraint, path: string): SomaLayoutPlan {
|
|
225
|
+
validateConstraint(constraint, path);
|
|
226
|
+
const solutions = CONTAINER_STEPS.map(
|
|
227
|
+
([name, rem]) => solveStep(constraint, rem, name, path),
|
|
228
|
+
);
|
|
229
|
+
return { constraint, solutions };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** 四位小数:CSS 里再精确没有意义,而浮点尾巴会让产物 diff 每次都不一样 */
|
|
233
|
+
function round(value: number): number {
|
|
234
|
+
return Math.round(value * 10_000) / 10_000;
|
|
235
|
+
}
|