soma-kit 0.1.14 → 0.1.15
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 +3 -3
- package/packs/finance/reconcile-desk/compile.ts +1 -1
- package/packs/finance/reconcile-desk/shell.ts +1 -1
- package/spec/generated/kit-app.schema.json +4 -2
- package/spec/generated/kit-page.schema.json +72 -2
- package/spec/kit-grammar.json +163 -57
- package/spec/kit-grammar.schema.json +0 -24
- package/src/app/app-config.ts +1 -1
- package/src/app/component-gate.ts +2 -2
- package/src/app/compose-apps.ts +5 -2
- package/src/app/define-kit-actions.ts +82 -10
- package/src/app/define-kit-app.ts +6 -5
- package/src/app/define-kit-view.ts +1 -1
- package/src/app/index.ts +7 -7
- package/src/app/kit-view-context.ts +11 -2
- package/src/app/theme-endpoint.ts +1 -1
- package/src/app/theme-runtime.ts +7 -2
- package/src/base/alert.ts +23 -20
- package/src/base/anchored-layer.ts +1 -1
- package/src/base/button.ts +30 -15
- package/src/base/cascader.ts +8 -4
- package/src/base/check-core.ts +9 -6
- package/src/base/color-picker.ts +10 -6
- package/src/base/command-palette.ts +10 -4
- package/src/base/context-menu.ts +5 -2
- package/src/base/date-range.ts +9 -6
- package/src/base/date.ts +10 -6
- package/src/base/delta.ts +4 -2
- package/src/base/divider.ts +15 -66
- package/src/base/fab.ts +11 -6
- package/src/base/gap-value.ts +1 -1
- package/src/base/hint-tips.ts +1 -1
- package/src/base/index.ts +15 -4
- package/src/base/inline-error.ts +84 -0
- package/src/base/input.ts +54 -18
- package/src/base/kbd.ts +4 -3
- package/src/base/link.ts +1 -1
- package/src/base/listbox-core.ts +70 -22
- package/src/base/mentions.ts +6 -3
- package/src/base/menu-button.ts +63 -27
- package/src/base/menu-core.ts +9 -2
- package/src/base/motion/hover-3d.ts +4 -1
- package/src/base/motion/hover-gallery.ts +4 -1
- package/src/base/otp.ts +7 -3
- package/src/base/overlay-runtime.ts +3 -2
- package/src/base/overlay-shell.ts +141 -0
- package/src/base/popover.ts +2 -2
- package/src/base/popup.ts +47 -41
- package/src/base/qty-stepper.ts +14 -9
- package/src/base/reason-tip.ts +2 -2
- package/src/{frames → base}/resize-handle.ts +1 -1
- package/src/base/segmented.ts +35 -36
- package/src/base/select.ts +1 -0
- package/src/base/shared.ts +43 -7
- package/src/base/status-tone.ts +1 -1
- package/src/base/tag-input.ts +13 -8
- package/src/base/tag.ts +53 -59
- package/src/base/text.ts +37 -13
- package/src/base/textarea.ts +1 -1
- package/src/base/time-picker.ts +9 -5
- package/src/base/tree-select.ts +12 -9
- package/src/base/upload.ts +21 -13
- package/src/biz/approval-desk/compile.ts +28 -1
- package/src/biz/approval-desk/shell.ts +2 -2
- package/src/biz/auth-panel/compile.ts +1 -1
- package/src/biz/auth-panel/shell.ts +2 -2
- package/src/biz/category-crud/compile.ts +17 -4
- package/src/biz/category-crud/shell.ts +7 -5
- package/src/biz/checkout-page/compile.ts +1 -1
- package/src/biz/checkout-page/shell.ts +7 -6
- package/src/biz/compose.ts +3 -3
- package/src/biz/define.ts +1 -1
- package/src/biz/import-wizard/compile.ts +1 -1
- package/src/biz/import-wizard/shell.ts +6 -4
- package/src/biz/index.ts +4 -3
- package/src/biz/landing-page/compile.ts +1 -1
- package/src/biz/landing-page/shell.ts +5 -4
- package/src/biz/link/shared.ts +8 -1
- package/src/biz/master-sub/compile.ts +162 -19
- package/src/biz/master-sub/detail-head.ts +7 -6
- package/src/biz/master-sub/index.ts +3 -3
- package/src/biz/master-sub/shell.ts +90 -53
- package/src/biz/multi-board/compile.ts +3 -3
- package/src/biz/multi-board/index.ts +24 -9
- package/src/biz/multi-board/prose-fold.ts +2 -2
- package/src/biz/multi-board/shell.ts +17 -13
- package/src/biz/names.ts +1 -0
- package/src/biz/notification-center/compile.ts +9 -2
- package/src/biz/notification-center/shell.ts +9 -2
- package/src/biz/page-templates/domain-templates.ts +6 -2
- package/src/biz/page-templates/result-page/compile.ts +1 -1
- package/src/biz/page-templates/result-page/shell.ts +4 -3
- package/src/biz/page-templates/shared.ts +1 -1
- package/src/biz/page-templates/sign-in-page/compile.ts +1 -1
- package/src/biz/page-templates/sign-in-page/shell.ts +4 -4
- package/src/biz/record-detail/compile.ts +5 -2
- package/src/biz/record-detail/shell.ts +4 -4
- package/src/biz/settings-center/compile.ts +1 -1
- package/src/biz/settings-center/shell.ts +8 -5
- package/src/biz/shared/action-host.ts +12 -5
- package/src/biz/shared/compile-helpers.ts +4 -4
- package/src/biz/shared/coordinator.ts +104 -21
- package/src/biz/shared/overlay-compile.ts +48 -18
- package/src/biz/shared/overlay-shell.ts +63 -112
- package/src/biz/single-form/compile.ts +2 -2
- package/src/biz/single-form/shell.ts +8 -4
- package/src/biz/standard-crud/compile.ts +11 -4
- package/src/biz/standard-crud/shell.ts +26 -9
- package/src/biz/standard-crud/summary.ts +3 -1
- package/src/biz/step-form/compile.ts +1 -1
- package/src/biz/step-form/hub.ts +4 -0
- package/src/biz/step-form/shell.ts +6 -4
- package/src/biz/types.ts +11 -4
- package/src/cli/app-layer-check.ts +375 -0
- package/src/cli/kit-cli.ts +35 -8
- package/src/cli/kit-init.ts +149 -0
- package/src/compile/access-gate-attach.ts +1 -1
- package/src/compile/action-host-attach.ts +1 -1
- package/src/compile/compile-page.ts +29 -234
- package/src/compile/component-candidates.ts +2 -1
- package/src/compile/hints.ts +5 -58
- package/src/compile/index.ts +2 -2
- package/src/compile/layout-lint/index.ts +4 -1
- package/src/compile/layout-lint/rules-composition.ts +43 -13
- package/src/compile/page-composition.ts +33 -31
- package/src/compile/redirect.ts +1 -1
- package/src/compile/stages.ts +169 -0
- package/src/compile/styles.ts +70 -32
- package/src/compile/types.ts +2 -2
- package/src/data/amount-breakdown.ts +4 -2
- package/src/data/anchor-nav.ts +4 -1
- package/src/data/approval-flow.ts +9 -4
- package/src/data/assignment-picker.ts +28 -18
- package/src/data/async-result-panel.ts +6 -2
- package/src/data/breadcrumbs.ts +13 -9
- package/src/data/calendar.ts +4 -3
- package/src/data/carousel.ts +11 -5
- package/src/data/chart.ts +153 -23
- package/src/data/checklist.ts +5 -4
- package/src/data/code-view.ts +220 -26
- package/src/data/comment-thread.ts +7 -2
- package/src/data/confirm-dialog.ts +56 -13
- package/src/data/diff.ts +4 -1
- package/src/data/dock.ts +5 -1
- package/src/data/embed.ts +1 -1
- package/src/data/engine-ports.ts +126 -0
- package/src/data/field-mapper.ts +12 -6
- package/src/data/flow-editor.ts +105 -29
- package/src/data/footer.ts +6 -2
- package/src/data/gantt.ts +11 -8
- package/src/data/hero.ts +6 -2
- package/src/data/image-viewer.ts +7 -3
- package/src/data/index.ts +12 -5
- package/src/data/item-list.ts +6 -5
- package/src/data/json-view.ts +844 -0
- package/src/data/kanban.ts +11 -8
- package/src/data/markdown-view.ts +487 -0
- package/src/data/matrix.ts +23 -14
- package/src/data/media-grid.ts +5 -2
- package/src/data/mega-menu.ts +4 -3
- package/src/data/menu-dom.ts +9 -8
- package/src/data/menu.ts +15 -9
- package/src/data/mockup.ts +9 -5
- package/src/data/modal-dialog.ts +39 -88
- package/src/data/navbar.ts +11 -7
- package/src/data/notice-list.ts +1 -1
- package/src/data/notify-renderer.ts +21 -10
- package/src/data/object-form.ts +129 -48
- package/src/data/pager.ts +12 -8
- package/src/data/panel-head.ts +1 -1
- package/src/data/permission-matrix.ts +15 -8
- package/src/data/permission-tree.ts +24 -11
- package/src/data/pricing-table.ts +8 -3
- package/src/data/reaction-bar.ts +6 -3
- package/src/data/record-card.ts +13 -9
- package/src/data/record-picker.ts +8 -4
- package/src/data/redirect.ts +1 -1
- package/src/data/request-console.ts +20 -7
- package/src/data/resident.ts +0 -12
- package/src/data/result-panel.ts +2 -2
- package/src/data/rich-text.ts +58 -96
- package/src/data/row-editor.ts +10 -5
- package/src/data/rule-builder.ts +14 -8
- package/src/data/shape-util.ts +14 -3
- package/src/data/stat-group.ts +63 -22
- package/src/data/static-lazy.ts +3 -0
- package/src/data/static-registry.ts +8 -2
- package/src/data/steps.ts +6 -2
- package/src/data/survey.ts +9 -3
- package/src/data/system-switcher.ts +15 -6
- package/src/data/text-diff.ts +663 -0
- package/src/data/timeline.ts +1 -1
- package/src/data/tour-guide.ts +6 -3
- package/src/data/transfer.ts +19 -10
- package/src/data/tree-table.ts +77 -12
- package/src/data/tree.ts +5 -2
- package/src/data/virtual-rows.ts +17 -7
- package/src/data/wrap.ts +2 -2
- package/src/frames/index.ts +3 -2
- package/src/frames/inherited-shell.ts +178 -0
- package/src/frames/list-summary.ts +59 -36
- package/src/frames/page-bar.ts +4 -2
- package/src/frames/resident-components.ts +20 -0
- package/src/frames/shared.ts +18 -14
- package/src/frames/split-frame/shell.ts +17 -11
- package/src/frames/split-frame/wiring.ts +2 -2
- package/src/frames/tabs-frame/shell.ts +12 -6
- package/src/frames/types.ts +3 -2
- package/src/frames/workspace-frame/extras.ts +19 -5
- package/src/frames/workspace-frame/shell.ts +2 -1
- package/src/frames/workspace-frame/state.ts +8 -2
- package/src/frames/workspace-frame/view.ts +125 -76
- package/src/generated/kit-grammar-runtime.ts +61 -39
- package/src/i18n/kit-text.ts +114 -2
- package/src/i18n/resolve-text-refs.ts +1 -1
- package/src/object-renderer/action-group.ts +31 -1
- package/src/object-renderer/action-more.ts +28 -12
- package/src/object-renderer/arrange.ts +37 -7
- package/src/object-renderer/collection-parts.ts +38 -7
- package/src/object-renderer/collection-table.ts +68 -23
- package/src/object-renderer/collection.ts +63 -30
- package/src/object-renderer/compile-data.ts +1 -1
- package/src/object-renderer/defaults.ts +8 -0
- package/src/object-renderer/emit-events.ts +1 -1
- package/src/object-renderer/emit-object.ts +3 -0
- package/src/object-renderer/emit-static.ts +140 -37
- package/src/object-renderer/field-value.ts +12 -239
- package/src/object-renderer/filter-toolbar.ts +103 -0
- package/src/{compile → object-renderer}/fragment.ts +2 -2
- package/src/object-renderer/index.ts +1 -1
- package/src/object-renderer/layout-facts.ts +37 -5
- package/src/object-renderer/overlay-decide.ts +82 -0
- package/src/object-renderer/render-object.ts +1 -1
- package/src/object-renderer/table-actions.ts +70 -23
- package/src/object-renderer/table-tools.ts +12 -5
- package/src/object-renderer/types.ts +9 -15
- package/src/recipes/app-blocks.ts +66 -0
- package/src/recipes/code-view.ts +56 -0
- package/src/recipes/compact-group.ts +31 -0
- package/src/recipes/container.ts +25 -15
- package/src/recipes/control.ts +21 -3
- package/src/recipes/desc-list.ts +14 -0
- package/src/recipes/divider.ts +53 -0
- package/src/recipes/emphasis.ts +12 -8
- package/src/recipes/frame-bleed.ts +3 -3
- package/src/recipes/index.ts +11 -1
- package/src/recipes/inherited-shell.ts +37 -0
- package/src/recipes/json-view.ts +49 -0
- package/src/recipes/markdown.ts +86 -0
- package/src/recipes/overlay.ts +112 -26
- package/src/recipes/page-width.ts +20 -0
- package/src/recipes/placeholder.ts +1 -1
- package/src/recipes/stat-state.ts +1 -1
- package/src/recipes/state.ts +10 -8
- package/src/recipes/status.ts +13 -12
- package/src/recipes/strip.ts +72 -0
- package/src/recipes/surface.ts +10 -6
- package/src/recipes/table-gap.ts +1 -1
- package/src/recipes/table-native-edit.ts +4 -4
- package/src/recipes/table-row.ts +11 -8
- package/src/recipes/tabs.ts +108 -31
- package/src/recipes/text-diff.ts +90 -0
- package/src/recipes/title-row.ts +4 -2
- package/src/recipes/typography.ts +2 -2
- package/src/shared/diagnostics.ts +16 -0
- package/src/shared/envelope.ts +1 -1
- package/src/shared/field-value-format.ts +246 -0
- package/src/shared/focus-ring.ts +8 -0
- package/src/shared/hints.ts +56 -0
- package/src/{base → shared}/icons.ts +12 -0
- package/src/shared/kit-error.ts +1 -1
- package/src/{object-renderer → shared}/merge-class.ts +3 -2
- package/src/shared/path-guard.ts +1 -1
- package/src/shared/primitives-types.ts +8 -0
- package/src/shared/theme-runtime-shared.ts +19 -0
- package/src/theme/kit-theme-text.ts +1 -1
- package/src/theme/kit-theme.css +38 -14
- package/src/theme/kit-theme.tpl +38 -14
- package/src/theme/nine-dim-structure.ts +66 -41
- package/src/theme/surface-layers.ts +11 -3
- package/src/theme/theme-tokens.ts +5 -1
- package/src/app/envelope.ts +0 -1
- package/src/app/notify-renderer.ts +0 -1
- package/src/app/paged-list-adapter.ts +0 -1
- package/src/app/theme-runtime-shared.ts +0 -1
- package/src/compile/kit-error.ts +0 -1
- /package/src/{base → shared}/class-candidates.ts +0 -0
package/src/data/kanban.ts
CHANGED
|
@@ -30,31 +30,35 @@ import { kitKey, kitText } from '../i18n/kit-text';
|
|
|
30
30
|
import { createKeyboardWiring } from 'soma-core/keyboard';
|
|
31
31
|
import { SOMA_SIZE_UNITS } from 'soma-foundation/theme';
|
|
32
32
|
import { setKitFace } from '../recipes';
|
|
33
|
+
import { kitShellRecipe } from '../recipes/container';
|
|
34
|
+
import { kitSurfaceRecipe } from '../recipes/surface';
|
|
33
35
|
import { kitEmphasisButtonRecipe } from '../recipes/emphasis';
|
|
34
|
-
import { mergeClass } from '../
|
|
36
|
+
import { mergeClass } from '../shared/merge-class';
|
|
35
37
|
import { provideCtxRows } from './context-menu';
|
|
36
38
|
|
|
37
39
|
// ───────── 样式常量:Tailwind 类串与候选类清单 ─────────
|
|
38
40
|
|
|
39
41
|
const ROOT_CLASS = '@container flex flex-col gap-2';
|
|
40
42
|
const BOARD_CLASS = 'flex items-start gap-3 overflow-x-auto pb-2';
|
|
41
|
-
|
|
43
|
+
// 泳道 = 凹槽面的壳配方,卡片 = 壳配方(容器边 + 容器阴影令牌,KT-B2);移动菜单 = 浮起表面(KT-B4);提示条浅底无描边
|
|
44
|
+
const LANE_CLASS = mergeClass('flex w-72 shrink-0 flex-col gap-2 p-2', kitShellRecipe({ tone: 'sunken', shadow: false }));
|
|
42
45
|
const LANE_HEAD_CLASS = 'flex items-center justify-between gap-2 px-1 text-sm';
|
|
43
46
|
const LANE_TITLE_CLASS = 'font-medium text-fg';
|
|
44
47
|
const COUNT_CLASS = 'text-fg-muted';
|
|
45
48
|
const COUNT_OVER_CLASS = 'font-medium text-danger-on-soft';
|
|
46
49
|
const LANE_BODY_CLASS = 'flex min-h-16 flex-col gap-2 rounded-md';
|
|
47
50
|
const DROP_CLASS = 'ring-2 ring-inset ring-ring';
|
|
48
|
-
const
|
|
51
|
+
const CARD_EDGE = kitShellRecipe({ tone: 'base' });
|
|
52
|
+
const CARD_CLASS = `flex flex-col gap-1 ${CARD_EDGE} p-3 text-sm text-fg transition-colors state-layer focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-inset focus-visible:ring-ring`;
|
|
49
53
|
const CARD_DRAG_CLASS = 'cursor-grab active:cursor-grabbing';
|
|
50
54
|
const CARD_TITLE_CLASS = 'font-medium text-fg';
|
|
51
55
|
const FACT_CLASS = 'flex items-baseline justify-between gap-2 text-xs text-fg-muted';
|
|
52
56
|
const FACT_VALUE_CLASS = 'truncate text-fg';
|
|
53
57
|
const EMPTY_CLASS = 'px-2 py-3 text-center text-sm text-fg-muted';
|
|
54
58
|
const MOVE_BTN_CLASS = mergeClass(kitEmphasisButtonRecipe('button.toolbar'), 'self-start');
|
|
55
|
-
const MENU_CLASS = 'flex flex-col gap-1
|
|
59
|
+
const MENU_CLASS = mergeClass('flex flex-col gap-1 p-1', kitSurfaceRecipe('raised'));
|
|
56
60
|
const MENU_ITEM_CLASS = 'flex h-7 items-center rounded-md px-2 text-start text-xs text-fg hover:bg-state-layer-hover active:bg-state-layer-press focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:cursor-not-allowed disabled:text-fg-subtle';
|
|
57
|
-
const NOTICE_CLASS = 'rounded-md
|
|
61
|
+
const NOTICE_CLASS = 'rounded-md bg-warning-soft px-3 py-1.5 text-xs text-warning-on-soft';
|
|
58
62
|
const HIDDEN_CLASS = 'hidden';
|
|
59
63
|
const LIVE_CLASS = 'sr-only';
|
|
60
64
|
|
|
@@ -225,8 +229,6 @@ export function createKanban(): SomaComponentDefinition {
|
|
|
225
229
|
const port = (api.services as { data?: SomaDataPort } | undefined)?.data;
|
|
226
230
|
return port && typeof port === 'object' ? port : undefined;
|
|
227
231
|
};
|
|
228
|
-
const notifyPort = (): { toast?: (o: { level: string; message: string }) => void } | undefined =>
|
|
229
|
-
(api.services as { notify?: { toast?: (o: { level: string; message: string }) => void } } | undefined)?.notify;
|
|
230
232
|
|
|
231
233
|
const config = (): KitKanbanConfig => {
|
|
232
234
|
const p = P();
|
|
@@ -277,7 +279,7 @@ export function createKanban(): SomaComponentDefinition {
|
|
|
277
279
|
}
|
|
278
280
|
showNotice(text);
|
|
279
281
|
announce(text);
|
|
280
|
-
|
|
282
|
+
// 错误不用轻提示(KT-K-feedback-3,§6.1 G-17):保存失败已由看板自己的 notice + announce 就地呈现,不再重复发轻提示
|
|
281
283
|
paint();
|
|
282
284
|
// 连着移了好几次、中间结果不可知:以服务端为准重取
|
|
283
285
|
if (several) {
|
|
@@ -469,6 +471,7 @@ export function createKanban(): SomaComponentDefinition {
|
|
|
469
471
|
const limit = cfg.wip[lane.value];
|
|
470
472
|
const over = limit !== undefined && list.length > limit;
|
|
471
473
|
const col = el('section', LANE_CLASS);
|
|
474
|
+
col.setAttribute('data-kit-container', 'card');
|
|
472
475
|
col.setAttribute('aria-label', lane.label);
|
|
473
476
|
col.setAttribute('data-kit-lane-box', lane.value);
|
|
474
477
|
const head = el('div', LANE_HEAD_CLASS);
|
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
// MarkdownView:Markdown 展示(layer: data,写死形状,accept: single;内容展示批次 ④,规格 knowledge/Kit主题规则与算法.md §10.18)。
|
|
2
|
+
//
|
|
3
|
+
// 数据:Markdown 原文字符串,或 { source | markdown | content | text },或字符串数组(按块拼接、中间分割线);`path` 取 data 里的点路径。
|
|
4
|
+
// 原文只经 core 端口 engines.markdown 解析成块树,这里按节点类型 createElement / textContent 出 DOM(不用 innerHTML、不渲染原始 HTML)。
|
|
5
|
+
// 自动决定:标题层级(heading_offset 叠加)、目录(二级以上标题够多且正文够长才出)、顶层成段正文首行缩进两字(裁决 #4:
|
|
6
|
+
// 列表 / 引用 / 表格 / 脚注里的段落不缩进;只有一段的短内容不缩进)、代码块(与 CodeView 同一画法:语言推断、行号、着色、复制)、
|
|
7
|
+
// 表格横向滚动壳、图片懒加载与失败替换、外链新窗口 + 图标 + 读屏提示、任务列表只读勾选。
|
|
8
|
+
// 流式(stream: true,AI 逐字输出):原文不断增长,按帧节流整段重解析,只替换从第一个变化的块起之后的 DOM,前面已输出的文字保持可选中。
|
|
9
|
+
// 降级:空 → 空态;引擎不可用 / 过长 / 解析失败 → 纯文本分段 + 说明;原始 HTML 与不安全地址各有计数提示。
|
|
10
|
+
|
|
11
|
+
import type { SomaComponentDefinition } from 'soma-core';
|
|
12
|
+
import { KIT_CONTENT_LIMITS } from 'soma-foundation/kit-limits';
|
|
13
|
+
import { bool, createKitController, joinClass, str } from '../base/shared';
|
|
14
|
+
import { kitIcon } from '../shared/icons';
|
|
15
|
+
import { kitText } from '../i18n/kit-text';
|
|
16
|
+
import { KIT_PART_ATTRIBUTE, KIT_PART_WELL } from '../recipes';
|
|
17
|
+
import { KIT_CODE_LANG_CLASS, KIT_CODE_NOTE_CLASS, KIT_CODE_VIEW_RECIPE_CLASS_CANDIDATES } from '../recipes/code-view';
|
|
18
|
+
import {
|
|
19
|
+
KIT_MARKDOWN_RECIPE_CLASS_CANDIDATES, KIT_MD_BODY_CLASS, KIT_MD_CODE_CLASS, KIT_MD_DEL_CLASS, KIT_MD_FNREF_CLASS, KIT_MD_FOOTNOTES_CLASS,
|
|
20
|
+
KIT_MD_HR_CLASS, KIT_MD_HTML_CLASS, KIT_MD_IMG_CLASS, KIT_MD_LINK_CLASS, KIT_MD_LIST_CLASS, KIT_MD_LIST_STYLE, KIT_MD_LI_CLASS,
|
|
21
|
+
KIT_MD_P_CLASS, KIT_MD_P_INDENT_CLASS, KIT_MD_P_TIGHT_CLASS, KIT_MD_QUOTE_CLASS, KIT_MD_TABLE_CLASS, KIT_MD_TABLE_WRAP_CLASS,
|
|
22
|
+
KIT_MD_TASK_CLASS, KIT_MD_TD_CLASS, KIT_MD_TH_CLASS, kitMdHeadingClass,
|
|
23
|
+
KIT_MD_CODE_HEAD_CLASS, KIT_MD_CODE_WELL_CLASS, KIT_MD_EXT_ICON_CLASS, KIT_MD_IMG_FAIL_CLASS, KIT_MD_PLAIN_P_CLASS, KIT_MD_ROOT_CLASS,
|
|
24
|
+
KIT_MD_STRONG_CLASS, KIT_MD_TASK_BOX_CLASS, KIT_MD_TOC_CLASS, KIT_MD_TOC_ITEM_CLASS, KIT_MD_TOC_TITLE_CLASS,
|
|
25
|
+
} from '../recipes/markdown';
|
|
26
|
+
import { classTokens, emptyNode, mk } from './shape-util';
|
|
27
|
+
import { codeWrapDefault, paintCodeLines } from './code-view';
|
|
28
|
+
import {
|
|
29
|
+
kitEnginePort, type KitHighlightEnginePort, type KitMarkdownEnginePort, type KitMdBlock, type KitMdInline, type KitMdMeta, type KitMdResult,
|
|
30
|
+
} from './engine-ports';
|
|
31
|
+
import { KIT_STATE_PANEL_CLASS_CANDIDATES } from '../base/state-panel';
|
|
32
|
+
import { TEXT_ACTION_CLASS_TOKENS, textActionButton } from './text-action-button';
|
|
33
|
+
|
|
34
|
+
const ML = KIT_CONTENT_LIMITS.markdownView;
|
|
35
|
+
|
|
36
|
+
// ── 纯函数 ────────────────────────────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
/** 数据 → Markdown 原文:字符串原样;{ source | markdown | content | text };字符串数组以分割线拼接;数字 / 布尔转字符串;其余空串 */
|
|
39
|
+
export function markdownSourceOf(raw: unknown): string {
|
|
40
|
+
if (raw === null || raw === undefined) return '';
|
|
41
|
+
if (typeof raw === 'string') return raw;
|
|
42
|
+
if (typeof raw === 'number' || typeof raw === 'boolean') return String(raw);
|
|
43
|
+
if (Array.isArray(raw)) return raw.map(markdownSourceOf).filter((s) => s.trim() !== '').join('\n\n---\n\n');
|
|
44
|
+
if (typeof raw === 'object') {
|
|
45
|
+
const o = raw as Record<string, unknown>;
|
|
46
|
+
for (const k of ['source', 'markdown', 'content', 'text']) if (typeof o[k] === 'string') return o[k] as string;
|
|
47
|
+
}
|
|
48
|
+
return '';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** 目录:二级及以上(叠加偏移前的 depth ≤ 3)标题数 ≥ tocMinHeadings 且正文字数 ≥ tocMinChars */
|
|
52
|
+
export function markdownTocOf(meta: KitMdMeta, mode: string): KitMdMeta['outline'] {
|
|
53
|
+
if (mode === 'off') return [];
|
|
54
|
+
const list = meta.outline.filter((o) => o.depth <= 3);
|
|
55
|
+
return list.length >= ML.tocMinHeadings && meta.counts.chars >= ML.tocMinChars ? list : [];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 首行缩进(裁决 #4):只有顶层段落;全文只有一个顶层段落且短于 indentMinChars 字 → 不缩进 */
|
|
59
|
+
export function markdownIndentTopLevel(blocks: readonly KitMdBlock[]): boolean {
|
|
60
|
+
const paras = blocks.filter((b) => b.t === 'paragraph');
|
|
61
|
+
if (paras.length === 0) return false;
|
|
62
|
+
if (paras.length === 1 && blocks.length === 1) {
|
|
63
|
+
const p = paras[0]!;
|
|
64
|
+
return p.t === 'paragraph' && Array.from(inlinePlain(p.inline)).length >= ML.indentMinChars;
|
|
65
|
+
}
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function inlinePlain(nodes: readonly KitMdInline[]): string {
|
|
70
|
+
let s = '';
|
|
71
|
+
for (const n of nodes) {
|
|
72
|
+
if (n.t === 'text' || n.t === 'code') s += n.v;
|
|
73
|
+
else if (n.t === 'image') s += n.alt;
|
|
74
|
+
else if (n.t === 'strong' || n.t === 'em' || n.t === 'del' || n.t === 'link') s += inlinePlain(n.c);
|
|
75
|
+
}
|
|
76
|
+
return s;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** 只含图片(与空白)的段落:不缩进 */
|
|
80
|
+
const imageOnly = (nodes: readonly KitMdInline[]): boolean =>
|
|
81
|
+
nodes.length > 0 && nodes.every((n) => n.t === 'image' || (n.t === 'text' && n.v.trim() === '') || n.t === 'br');
|
|
82
|
+
|
|
83
|
+
// ── 块树渲染(MarkdownView 与 RichTextEditor 的 Markdown 来源共用) ─────────────────
|
|
84
|
+
|
|
85
|
+
// 类串全部来自配方 recipes/markdown.ts(G101:组件文件不手写圆角 / 字阶)
|
|
86
|
+
const CODE_WELL_CLASS = KIT_MD_CODE_WELL_CLASS;
|
|
87
|
+
const CODE_HEAD_CLASS = KIT_MD_CODE_HEAD_CLASS;
|
|
88
|
+
const TASK_BOX_CLASS = KIT_MD_TASK_BOX_CLASS;
|
|
89
|
+
const EXT_ICON_CLASS = KIT_MD_EXT_ICON_CLASS;
|
|
90
|
+
const IMG_FAIL_CLASS = KIT_MD_IMG_FAIL_CLASS;
|
|
91
|
+
const ROOT_CLASS = KIT_MD_ROOT_CLASS;
|
|
92
|
+
const TOC_CLASS = KIT_MD_TOC_CLASS;
|
|
93
|
+
const TOC_TITLE_CLASS = KIT_MD_TOC_TITLE_CLASS;
|
|
94
|
+
const TOC_ITEM_CLASS = KIT_MD_TOC_ITEM_CLASS;
|
|
95
|
+
const NOTE_CLASS = KIT_CODE_NOTE_CLASS;
|
|
96
|
+
const PLAIN_P_CLASS = KIT_MD_PLAIN_P_CLASS;
|
|
97
|
+
|
|
98
|
+
export const KIT_MARKDOWN_VIEW_CLASS_CANDIDATES: readonly string[] = classTokens(
|
|
99
|
+
NOTE_CLASS, KIT_CODE_LANG_CLASS, KIT_CODE_VIEW_RECIPE_CLASS_CANDIDATES, KIT_MARKDOWN_RECIPE_CLASS_CANDIDATES, KIT_STATE_PANEL_CLASS_CANDIDATES,
|
|
100
|
+
TEXT_ACTION_CLASS_TOKENS, 'sr-only', 'min-w-0', 'text-start', 'text-end', 'text-center',
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
export interface KitMdRenderContext {
|
|
104
|
+
/** 组件根(取页面语言的文案) */
|
|
105
|
+
readonly root: HTMLElement;
|
|
106
|
+
/** 标题层级偏移(0–5) */
|
|
107
|
+
readonly headingOffset: number;
|
|
108
|
+
/** 本实例的 id 前缀(同页多个 Markdown 不撞 id) */
|
|
109
|
+
readonly idPrefix: string;
|
|
110
|
+
/** 顶层段落是否首行缩进 */
|
|
111
|
+
readonly indent: boolean;
|
|
112
|
+
readonly highlight: KitHighlightEnginePort | null;
|
|
113
|
+
/** 代码块是否出复制钮 */
|
|
114
|
+
readonly copyCode: boolean;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function inlineInto(host: HTMLElement, nodes: readonly KitMdInline[], ctx: KitMdRenderContext): void {
|
|
118
|
+
for (const n of nodes) {
|
|
119
|
+
switch (n.t) {
|
|
120
|
+
case 'text': host.appendChild(document.createTextNode(n.v)); break;
|
|
121
|
+
case 'br': host.appendChild(document.createElement('br')); break;
|
|
122
|
+
case 'code': host.appendChild(mk('code', KIT_MD_CODE_CLASS, n.v)); break;
|
|
123
|
+
case 'strong': { const el = mk('strong', KIT_MD_STRONG_CLASS); inlineInto(el, n.c, ctx); host.appendChild(el); break; }
|
|
124
|
+
case 'em': { const el = mk('em', ''); inlineInto(el, n.c, ctx); host.appendChild(el); break; }
|
|
125
|
+
case 'del': { const el = mk('del', KIT_MD_DEL_CLASS); inlineInto(el, n.c, ctx); host.appendChild(el); break; }
|
|
126
|
+
case 'link': {
|
|
127
|
+
const a = mk('a', KIT_MD_LINK_CLASS);
|
|
128
|
+
a.href = n.href.startsWith('#md-') ? `#${ctx.idPrefix}${n.href.slice(1)}` : n.href;
|
|
129
|
+
if (n.title) a.title = n.title;
|
|
130
|
+
inlineInto(a, n.c, ctx);
|
|
131
|
+
if (n.external) {
|
|
132
|
+
a.target = '_blank';
|
|
133
|
+
a.rel = 'noopener noreferrer nofollow';
|
|
134
|
+
const icon = kitIcon('external-link', 'xs', EXT_ICON_CLASS);
|
|
135
|
+
icon.setAttribute('aria-hidden', 'true');
|
|
136
|
+
a.appendChild(icon);
|
|
137
|
+
a.appendChild(mk('span', 'sr-only', kitText('soma.kit.md_external', undefined, ctx.root)));
|
|
138
|
+
}
|
|
139
|
+
host.appendChild(a);
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
case 'image': {
|
|
143
|
+
const img = mk('img', KIT_MD_IMG_CLASS);
|
|
144
|
+
img.src = n.src;
|
|
145
|
+
img.alt = n.alt;
|
|
146
|
+
if (n.title) img.title = n.title;
|
|
147
|
+
img.setAttribute('loading', 'lazy');
|
|
148
|
+
img.setAttribute('decoding', 'async');
|
|
149
|
+
img.setAttribute('referrerpolicy', 'no-referrer');
|
|
150
|
+
img.addEventListener('error', () => {
|
|
151
|
+
img.replaceWith(mk('span', IMG_FAIL_CLASS, kitText('soma.kit.md_image_failed', { alt: n.alt !== '' ? n.alt : n.src }, ctx.root)));
|
|
152
|
+
}, { once: true });
|
|
153
|
+
host.appendChild(img);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
case 'fnref': {
|
|
157
|
+
const sup = mk('sup', KIT_MD_FNREF_CLASS);
|
|
158
|
+
const a = mk('a', KIT_MD_LINK_CLASS, `[${n.n}]`);
|
|
159
|
+
a.href = `#${ctx.idPrefix}${n.id}`;
|
|
160
|
+
a.setAttribute('aria-label', kitText('soma.kit.md_fnref', { n: n.n }, ctx.root));
|
|
161
|
+
sup.appendChild(a);
|
|
162
|
+
host.appendChild(sup);
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
default: break;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** 一个块 → DOM(level = 0 表示顶层,用于首行缩进判定) */
|
|
171
|
+
export function renderMdBlock(b: KitMdBlock, ctx: KitMdRenderContext, level: number): HTMLElement {
|
|
172
|
+
switch (b.t) {
|
|
173
|
+
case 'heading': {
|
|
174
|
+
const depth = Math.min(6, b.depth + ctx.headingOffset);
|
|
175
|
+
const h = mk(`h${depth}` as 'h1', kitMdHeadingClass(depth));
|
|
176
|
+
h.id = `${ctx.idPrefix}${b.id}`;
|
|
177
|
+
inlineInto(h, b.inline, ctx);
|
|
178
|
+
return h;
|
|
179
|
+
}
|
|
180
|
+
case 'paragraph': {
|
|
181
|
+
const cls = b.tight ? KIT_MD_P_TIGHT_CLASS : level === 0 && ctx.indent && !imageOnly(b.inline) ? KIT_MD_P_INDENT_CLASS : KIT_MD_P_CLASS;
|
|
182
|
+
const p = mk('p', cls);
|
|
183
|
+
inlineInto(p, b.inline, ctx);
|
|
184
|
+
return p;
|
|
185
|
+
}
|
|
186
|
+
case 'list': {
|
|
187
|
+
const tasky = b.items.some((it) => it.task !== null);
|
|
188
|
+
const list = mk(b.ordered ? 'ol' : 'ul', `${KIT_MD_LIST_CLASS} ${tasky ? '' : KIT_MD_LIST_STYLE[b.ordered ? 'ol' : 'ul']}`.trim());
|
|
189
|
+
if (b.ordered && b.start !== undefined) (list as HTMLOListElement).start = b.start;
|
|
190
|
+
for (const it of b.items) {
|
|
191
|
+
const li = mk('li', it.task !== null ? KIT_MD_TASK_CLASS : KIT_MD_LI_CLASS);
|
|
192
|
+
if (it.task !== null) {
|
|
193
|
+
const box = mk('span', TASK_BOX_CLASS);
|
|
194
|
+
box.setAttribute('role', 'checkbox');
|
|
195
|
+
box.setAttribute('aria-checked', it.task ? 'true' : 'false');
|
|
196
|
+
box.setAttribute('aria-disabled', 'true');
|
|
197
|
+
box.setAttribute('aria-label', kitText(it.task ? 'soma.kit.md_task_done' : 'soma.kit.md_task_todo', undefined, ctx.root));
|
|
198
|
+
if (it.task) box.appendChild(kitIcon('check', 'xs'));
|
|
199
|
+
li.appendChild(box);
|
|
200
|
+
const body = mk('div', 'min-w-0');
|
|
201
|
+
for (const c of it.blocks) body.appendChild(renderMdBlock(c, ctx, level + 1));
|
|
202
|
+
li.appendChild(body);
|
|
203
|
+
} else {
|
|
204
|
+
for (const c of it.blocks) li.appendChild(renderMdBlock(c, ctx, level + 1));
|
|
205
|
+
}
|
|
206
|
+
list.appendChild(li);
|
|
207
|
+
}
|
|
208
|
+
return list;
|
|
209
|
+
}
|
|
210
|
+
case 'quote': {
|
|
211
|
+
const q = mk('blockquote', KIT_MD_QUOTE_CLASS);
|
|
212
|
+
for (const c of b.blocks) q.appendChild(renderMdBlock(c, ctx, level + 1));
|
|
213
|
+
return q;
|
|
214
|
+
}
|
|
215
|
+
case 'code': {
|
|
216
|
+
const well = mk('div', CODE_WELL_CLASS);
|
|
217
|
+
well.setAttribute(KIT_PART_ATTRIBUTE, KIT_PART_WELL);
|
|
218
|
+
well.setAttribute('data-kit-md-code', '');
|
|
219
|
+
const hl = ctx.highlight;
|
|
220
|
+
const language = hl === null ? 'text' : hl.resolveLanguage({ fence: b.lang, sample: b.text.length <= KIT_CONTENT_LIMITS.codeView.sniffMaxChars ? b.text : '' });
|
|
221
|
+
const label = language !== 'text' && hl !== null ? hl.languageLabel(language) : b.lang;
|
|
222
|
+
if (label !== '' || ctx.copyCode) {
|
|
223
|
+
const head = mk('div', CODE_HEAD_CLASS);
|
|
224
|
+
head.appendChild(mk('span', KIT_CODE_LANG_CLASS, label));
|
|
225
|
+
if (ctx.copyCode) head.appendChild(textActionButton('md-copy-code', kitText('soma.kit.copy', undefined, ctx.root), 'primary'));
|
|
226
|
+
well.appendChild(head);
|
|
227
|
+
}
|
|
228
|
+
well.setAttribute('role', 'region');
|
|
229
|
+
well.setAttribute('data-kit-container', 'plain');
|
|
230
|
+
well.setAttribute('aria-label', kitText('soma.kit.code_region', { name: label !== '' ? label : kitText('soma.kit.code_plain', undefined, ctx.root) }, ctx.root));
|
|
231
|
+
paintCodeLines(well, b.text, { language: b.closed ? language : 'text', wrap: codeWrapDefault(language, b.text.split('\n')), port: b.closed ? hl : null, root: ctx.root });
|
|
232
|
+
return well;
|
|
233
|
+
}
|
|
234
|
+
case 'table': {
|
|
235
|
+
const wrap = mk('div', KIT_MD_TABLE_WRAP_CLASS);
|
|
236
|
+
wrap.tabIndex = 0;
|
|
237
|
+
wrap.setAttribute('role', 'region');
|
|
238
|
+
wrap.setAttribute('data-kit-container', 'plain');
|
|
239
|
+
wrap.setAttribute('aria-label', kitText('soma.kit.md_table', undefined, ctx.root));
|
|
240
|
+
const table = mk('table', KIT_MD_TABLE_CLASS);
|
|
241
|
+
const alignClass = (i: number): string => (b.align[i] === 'right' ? ' text-end' : b.align[i] === 'center' ? ' text-center' : ' text-start');
|
|
242
|
+
const thead = mk('thead', '');
|
|
243
|
+
const hr = mk('tr', '');
|
|
244
|
+
b.head.forEach((cell, i) => { const th = mk('th', `${KIT_MD_TH_CLASS}${alignClass(i)}`); th.scope = 'col'; inlineInto(th, cell, ctx); hr.appendChild(th); });
|
|
245
|
+
thead.appendChild(hr);
|
|
246
|
+
table.appendChild(thead);
|
|
247
|
+
const tbody = mk('tbody', '');
|
|
248
|
+
for (const row of b.rows) {
|
|
249
|
+
const tr = mk('tr', '');
|
|
250
|
+
row.forEach((cell, i) => { const td = mk('td', `${KIT_MD_TD_CLASS}${alignClass(i)}`); inlineInto(td, cell, ctx); tr.appendChild(td); });
|
|
251
|
+
tbody.appendChild(tr);
|
|
252
|
+
}
|
|
253
|
+
table.appendChild(tbody);
|
|
254
|
+
wrap.appendChild(table);
|
|
255
|
+
return wrap;
|
|
256
|
+
}
|
|
257
|
+
case 'hr': return mk('hr', KIT_MD_HR_CLASS);
|
|
258
|
+
case 'html': return mk('pre', KIT_MD_HTML_CLASS, b.text);
|
|
259
|
+
case 'footnotes': {
|
|
260
|
+
const sec = mk('section', KIT_MD_FOOTNOTES_CLASS);
|
|
261
|
+
sec.setAttribute('data-kit-container', 'plain');
|
|
262
|
+
sec.setAttribute('aria-label', kitText('soma.kit.md_footnotes', undefined, ctx.root));
|
|
263
|
+
const ol = mk('ol', `${KIT_MD_LIST_CLASS} ${KIT_MD_LIST_STYLE.ol}`);
|
|
264
|
+
for (const f of b.items) {
|
|
265
|
+
const li = mk('li', KIT_MD_LI_CLASS);
|
|
266
|
+
li.id = `${ctx.idPrefix}${f.id}`;
|
|
267
|
+
for (const c of f.blocks) li.appendChild(renderMdBlock(c, ctx, level + 1));
|
|
268
|
+
ol.appendChild(li);
|
|
269
|
+
}
|
|
270
|
+
sec.appendChild(ol);
|
|
271
|
+
return sec;
|
|
272
|
+
}
|
|
273
|
+
default: return mk('div', '');
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** 纯文本降级:按空行分段 */
|
|
278
|
+
export function renderMdPlain(host: HTMLElement, text: string): void {
|
|
279
|
+
for (const para of text.split(/\n{2,}/u)) if (para.trim() !== '') host.appendChild(mk('p', PLAIN_P_CLASS, para));
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** 块尾提示:原始 HTML、不安全地址、截断 */
|
|
283
|
+
export function mdNotes(meta: KitMdMeta, root: HTMLElement): HTMLElement[] {
|
|
284
|
+
const out: HTMLElement[] = [];
|
|
285
|
+
if (meta.dropped.html > 0) out.push(mk('p', NOTE_CLASS, kitText('soma.kit.md_html_dropped', { n: meta.dropped.html }, root)));
|
|
286
|
+
if (meta.dropped.unsafeUrls > 0) out.push(mk('p', NOTE_CLASS, kitText('soma.kit.md_unsafe_urls', { n: meta.dropped.unsafeUrls }, root)));
|
|
287
|
+
if (meta.truncated) out.push(mk('p', NOTE_CLASS, kitText('soma.kit.md_truncated', undefined, root)));
|
|
288
|
+
return out;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// ── 组件 ──────────────────────────────────────────────────────────────
|
|
292
|
+
|
|
293
|
+
interface ViewState {
|
|
294
|
+
source: string | null;
|
|
295
|
+
result: KitMdResult | null;
|
|
296
|
+
seq: number;
|
|
297
|
+
/** 流式:已画块的指纹与节点 */
|
|
298
|
+
keys: string[];
|
|
299
|
+
nodes: HTMLElement[];
|
|
300
|
+
timer: ReturnType<typeof setTimeout> | null;
|
|
301
|
+
lastParse: number;
|
|
302
|
+
indent: boolean;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
let instanceSeq = 0;
|
|
306
|
+
|
|
307
|
+
export function createMarkdownView(): SomaComponentDefinition {
|
|
308
|
+
const states = new WeakMap<HTMLElement, ViewState>();
|
|
309
|
+
const prefixes = new WeakMap<HTMLElement, string>();
|
|
310
|
+
const stateOf = (root: HTMLElement): ViewState => {
|
|
311
|
+
let st = states.get(root);
|
|
312
|
+
if (st === undefined) { st = { source: null, result: null, seq: 0, keys: [], nodes: [], timer: null, lastParse: 0, indent: false }; states.set(root, st); }
|
|
313
|
+
return st;
|
|
314
|
+
};
|
|
315
|
+
const prefixOf = (root: HTMLElement): string => {
|
|
316
|
+
let p = prefixes.get(root);
|
|
317
|
+
if (p === undefined) { instanceSeq += 1; p = `kmd${instanceSeq}-`; prefixes.set(root, p); }
|
|
318
|
+
return p;
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
return {
|
|
322
|
+
contract: {
|
|
323
|
+
name: 'MarkdownView',
|
|
324
|
+
layer: 'data',
|
|
325
|
+
props: {
|
|
326
|
+
data: { type: 'any', default: '', desc: 'Markdown 原文字符串,或 { source | markdown | content | text },或字符串数组(按块拼接);经 core 端口 engines.markdown 解析' },
|
|
327
|
+
path: { type: 'string', default: '', desc: '只看 data 里的一个点路径(如 detail.description)' },
|
|
328
|
+
heading_offset: { type: 'number', default: 0, desc: '标题层级偏移(0–5):嵌在详情里时 # 从更低的层级开始,组件不另改层级' },
|
|
329
|
+
toc: { type: 'enum', values: ['auto', 'off'], default: 'auto', desc: '目录:auto = 二级以上标题够多且正文够长才出(阈值 kit-limits#content.markdownView)' },
|
|
330
|
+
stream: { type: 'boolean', default: false, desc: '流式(AI 逐字输出):原文不断增长,按帧节流重解析,只替换变化的块' },
|
|
331
|
+
copy_code: { type: 'boolean', default: true, desc: '代码块出「复制」钮' },
|
|
332
|
+
empty_text: { type: 'string', default: '', desc: '没有内容时的文案;缺省取内置文案' },
|
|
333
|
+
aria_label: { type: 'string', default: '', desc: '正文区的无障碍名称' },
|
|
334
|
+
class: { type: 'string', default: '', desc: '外部传入的类,追加在根节点类串末尾' },
|
|
335
|
+
},
|
|
336
|
+
emits: {
|
|
337
|
+
copy: { payload: { ok: { type: 'boolean' } }, desc: '代码块复制后抛;不带文本' },
|
|
338
|
+
},
|
|
339
|
+
},
|
|
340
|
+
services: ['engines'],
|
|
341
|
+
templateHtml: `<article data-kit-container="plain" data-soma-id="root" class="${ROOT_CLASS}"></article>`,
|
|
342
|
+
controllerFactory: createKitController((api) => ({
|
|
343
|
+
attach(root) {
|
|
344
|
+
const onClick = (event: Event): void => {
|
|
345
|
+
const btn = (event.target as HTMLElement | null)?.closest<HTMLElement>('[data-kit-action="md-copy-code"]');
|
|
346
|
+
if (!btn || !root.contains(btn)) return;
|
|
347
|
+
const well = btn.closest<HTMLElement>('[data-kit-md-code]');
|
|
348
|
+
const text = Array.from(well?.querySelectorAll<HTMLElement>('[data-kit-code-lines] > div') ?? [])
|
|
349
|
+
.map((row) => (row.lastElementChild?.textContent ?? '').replace(/^ $/u, '')).join('\n');
|
|
350
|
+
void (async () => {
|
|
351
|
+
let ok = false;
|
|
352
|
+
try { if (typeof navigator !== 'undefined' && navigator.clipboard) { await navigator.clipboard.writeText(text); ok = true; } } catch { ok = false; }
|
|
353
|
+
if (ok) {
|
|
354
|
+
btn.textContent = kitText('soma.kit.copied', undefined, root);
|
|
355
|
+
setTimeout(() => { btn.textContent = kitText('soma.kit.copy', undefined, root); }, KIT_CONTENT_LIMITS.codeView.copyFeedbackMs);
|
|
356
|
+
}
|
|
357
|
+
api.emit('copy', { ok });
|
|
358
|
+
})();
|
|
359
|
+
};
|
|
360
|
+
root.addEventListener('click', onClick);
|
|
361
|
+
return () => {
|
|
362
|
+
root.removeEventListener('click', onClick);
|
|
363
|
+
const st = states.get(root);
|
|
364
|
+
if (st?.timer) clearTimeout(st.timer);
|
|
365
|
+
states.delete(root);
|
|
366
|
+
};
|
|
367
|
+
},
|
|
368
|
+
render(root, p, renderApi) {
|
|
369
|
+
const st = stateOf(root);
|
|
370
|
+
const source = markdownSourceOf(pathOf(p.data, str(p.path)));
|
|
371
|
+
const stream = bool(p.stream);
|
|
372
|
+
if (source === st.source && st.result !== null) { paint(root, p, renderApi.services, st, false); return; }
|
|
373
|
+
st.source = source;
|
|
374
|
+
if (source.trim() === '') { st.result = null; st.seq += 1; paintEmpty(root, p); return; }
|
|
375
|
+
const port = kitEnginePort<KitMarkdownEnginePort>(renderApi.services, 'markdown');
|
|
376
|
+
if (port === null) { st.result = { ok: false, reason: 'engine-load-failed', plainText: source }; paint(root, p, renderApi.services, st, false); return; }
|
|
377
|
+
const run = (): void => {
|
|
378
|
+
st.timer = null;
|
|
379
|
+
st.lastParse = Date.now();
|
|
380
|
+
const mine = (st.seq += 1);
|
|
381
|
+
const text = st.source ?? '';
|
|
382
|
+
void port.parse(text).then((res) => {
|
|
383
|
+
if (st.seq !== mine) return;
|
|
384
|
+
st.result = res;
|
|
385
|
+
paint(root, api.props(), renderApi.services, st, stream);
|
|
386
|
+
}, () => {
|
|
387
|
+
if (st.seq !== mine) return;
|
|
388
|
+
st.result = { ok: false, reason: 'parse-error', plainText: text };
|
|
389
|
+
paint(root, api.props(), renderApi.services, st, false);
|
|
390
|
+
});
|
|
391
|
+
};
|
|
392
|
+
// 流式:距上次解析不足节流间隔就推迟到间隔末(最后一次一定会解析)
|
|
393
|
+
if (stream) {
|
|
394
|
+
if (st.timer !== null) return;
|
|
395
|
+
const wait = Math.max(0, ML.streamThrottleMs - (Date.now() - st.lastParse));
|
|
396
|
+
st.timer = setTimeout(run, wait);
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
run();
|
|
400
|
+
},
|
|
401
|
+
})),
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
function pathOf(data: unknown, path: string): unknown {
|
|
405
|
+
if (path.trim() === '') return data;
|
|
406
|
+
let cur: unknown = data;
|
|
407
|
+
for (const part of path.trim().split('.')) {
|
|
408
|
+
if (cur === null || typeof cur !== 'object') return undefined;
|
|
409
|
+
cur = (cur as Record<string, unknown>)[part];
|
|
410
|
+
}
|
|
411
|
+
return cur;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function paintEmpty(root: HTMLElement, p: Record<string, unknown>): void {
|
|
415
|
+
const st = stateOf(root);
|
|
416
|
+
st.keys = [];
|
|
417
|
+
st.nodes = [];
|
|
418
|
+
root.className = joinClass(ROOT_CLASS, p.class);
|
|
419
|
+
root.textContent = '';
|
|
420
|
+
root.appendChild(emptyNode(str(p.empty_text, kitText('soma.kit.empty_content', undefined, root))));
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function paint(root: HTMLElement, p: Record<string, unknown>, services: unknown, st: ViewState, incremental: boolean): void {
|
|
424
|
+
const res = st.result;
|
|
425
|
+
root.className = joinClass(ROOT_CLASS, p.class);
|
|
426
|
+
if (res === null) { paintEmpty(root, p); return; }
|
|
427
|
+
root.setAttribute('aria-busy', bool(p.stream) ? 'true' : 'false');
|
|
428
|
+
if (!res.ok) {
|
|
429
|
+
st.keys = [];
|
|
430
|
+
st.nodes = [];
|
|
431
|
+
root.textContent = '';
|
|
432
|
+
const body = mk('div', KIT_MD_BODY_CLASS);
|
|
433
|
+
renderMdPlain(body, res.plainText);
|
|
434
|
+
root.appendChild(body);
|
|
435
|
+
const key = res.reason === 'too-long' ? 'soma.kit.md_too_long' : res.reason === 'engine-load-failed' ? 'soma.kit.md_engine_failed' : 'soma.kit.md_parse_failed';
|
|
436
|
+
root.appendChild(mk('p', NOTE_CLASS, kitText(key, undefined, root)));
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
const ctx: KitMdRenderContext = {
|
|
440
|
+
root, headingOffset: Math.max(0, Math.min(5, Math.floor(Number(p.heading_offset) || 0))), idPrefix: prefixOf(root),
|
|
441
|
+
indent: markdownIndentTopLevel(res.blocks), highlight: kitEnginePort<KitHighlightEnginePort>(services, 'highlight'),
|
|
442
|
+
copyCode: p.copy_code === undefined ? true : bool(p.copy_code),
|
|
443
|
+
};
|
|
444
|
+
const keys = res.blocks.map((b) => JSON.stringify(b));
|
|
445
|
+
let body = root.querySelector<HTMLElement>(':scope > [data-kit-md-body]');
|
|
446
|
+
// 流式增量:正文容器还在就从第一个变化的块起替换;否则整段重画
|
|
447
|
+
if (!incremental || body === null || st.nodes.length === 0 || st.nodes.some((n) => !body!.contains(n))) {
|
|
448
|
+
root.textContent = '';
|
|
449
|
+
const toc = markdownTocOf(res.meta, str(p.toc, 'auto'));
|
|
450
|
+
if (toc.length > 0) root.appendChild(tocNode(toc, ctx));
|
|
451
|
+
body = mk('div', KIT_MD_BODY_CLASS);
|
|
452
|
+
body.setAttribute('data-kit-md-body', '');
|
|
453
|
+
const label = str(p.aria_label);
|
|
454
|
+
if (label !== '') { body.setAttribute('role', 'region'); body.setAttribute('data-kit-container', 'plain'); body.setAttribute('aria-label', label); }
|
|
455
|
+
root.appendChild(body);
|
|
456
|
+
st.keys = [];
|
|
457
|
+
st.nodes = [];
|
|
458
|
+
}
|
|
459
|
+
let from = 0;
|
|
460
|
+
// 缩进判定随全文变化(单段 → 多段),变了就从头替换
|
|
461
|
+
if (ctx.indent === st.indent) while (from < keys.length && from < st.keys.length && keys[from] === st.keys[from]) from += 1;
|
|
462
|
+
st.indent = ctx.indent;
|
|
463
|
+
for (const n of st.nodes.slice(from)) n.remove();
|
|
464
|
+
const nodes = st.nodes.slice(0, from);
|
|
465
|
+
for (let i = from; i < res.blocks.length; i += 1) {
|
|
466
|
+
const el = renderMdBlock(res.blocks[i]!, ctx, 0);
|
|
467
|
+
body.appendChild(el);
|
|
468
|
+
nodes.push(el);
|
|
469
|
+
}
|
|
470
|
+
st.keys = keys;
|
|
471
|
+
st.nodes = nodes;
|
|
472
|
+
for (const old of Array.from(root.querySelectorAll(':scope > [data-kit-md-note]'))) old.remove();
|
|
473
|
+
for (const note of mdNotes(res.meta, root)) { note.setAttribute('data-kit-md-note', ''); root.appendChild(note); }
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function tocNode(toc: KitMdMeta['outline'], ctx: KitMdRenderContext): HTMLElement {
|
|
477
|
+
const nav = mk('nav', TOC_CLASS);
|
|
478
|
+
nav.setAttribute('aria-label', kitText('soma.kit.md_toc', undefined, ctx.root));
|
|
479
|
+
nav.appendChild(mk('span', TOC_TITLE_CLASS, kitText('soma.kit.md_toc', undefined, ctx.root)));
|
|
480
|
+
for (const o of toc) {
|
|
481
|
+
const a = mk('a', `${KIT_MD_LINK_CLASS} ${TOC_ITEM_CLASS[o.depth] ?? 'ps-4'}`, o.text);
|
|
482
|
+
a.href = `#${ctx.idPrefix}${o.id}`;
|
|
483
|
+
nav.appendChild(a);
|
|
484
|
+
}
|
|
485
|
+
return nav;
|
|
486
|
+
}
|
|
487
|
+
}
|
package/src/data/matrix.ts
CHANGED
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
MATRIX_LEVEL_PCT, matrixCellView, parseMatrixModel,
|
|
21
21
|
type MatrixCellView, type MatrixModel, type MatrixRow, type MatrixSpec, type MatrixTone,
|
|
22
22
|
} from 'soma-foundation/kit-viz';
|
|
23
|
-
import { classCandidates } from '../
|
|
23
|
+
import { classCandidates } from '../shared/class-candidates';
|
|
24
24
|
import { createKitController, joinClass, bool, str } from '../base/shared';
|
|
25
25
|
import {
|
|
26
26
|
KIT_READ_STATE_EMIT_DEFS, KIT_READ_STATE_PROP_DEFS, KIT_STATE_PANEL_CLASS_CANDIDATES, paintKitStatePanel,
|
|
@@ -29,7 +29,9 @@ import { kitText } from '../i18n/kit-text';
|
|
|
29
29
|
import { kitFocusRingRecipe } from '../recipes/state';
|
|
30
30
|
import { KIT_TAG_UNKNOWN_OUTLINE } from '../recipes/status';
|
|
31
31
|
import { kitTypographyRecipe } from '../recipes/typography';
|
|
32
|
-
import { mergeClass } from '../
|
|
32
|
+
import { mergeClass } from '../shared/merge-class';
|
|
33
|
+
import { kitDividerRecipe } from '../recipes/divider';
|
|
34
|
+
import { kitShellRecipe } from '../recipes/container';
|
|
33
35
|
import { KIT_GRAMMAR_RUNTIME as grammarJson } from '../generated/kit-grammar-runtime';
|
|
34
36
|
|
|
35
37
|
/** 闭集 / 占位 / 字形读语法真源 kit-grammar.json#dataLimits.matrix;数字限值(行列封顶、强度档不透明度)读 design kit-limits 经 foundation */
|
|
@@ -39,32 +41,36 @@ export const KIT_MATRIX_MARKS: readonly string[] = MATRIX_SPEC.marks;
|
|
|
39
41
|
|
|
40
42
|
// ───────── 类串与候选 ─────────
|
|
41
43
|
|
|
42
|
-
const ROOT_CLASS = 'relative flex w-full flex-col gap-2';
|
|
43
|
-
/** 滚动外壳:纵向最大高度档(表头吸顶要有有界的滚动容器);none =
|
|
44
|
+
const ROOT_CLASS = 'relative flex w-full flex-col gap-3 pb-2';
|
|
45
|
+
/** 滚动外壳:纵向最大高度档(表头吸顶要有有界的滚动容器);none = 不设上限;isolate 限制吸顶/吸边层级不盖住下方页脚按钮 */
|
|
44
46
|
const SCROLL_CLASS: Readonly<Record<string, string>> = Object.freeze({ none: '', sm: 'max-h-64', md: 'max-h-96', lg: 'max-h-128' });
|
|
45
|
-
const SCROLLER_CLASS = 'relative w-full overflow-auto';
|
|
47
|
+
const SCROLLER_CLASS = 'relative isolate w-full overflow-auto pb-1';
|
|
46
48
|
const TABLE_CLASS = mergeClass('w-full', kitTypographyRecipe('text'));
|
|
47
49
|
const HEAD_CELL_CLASS = mergeClass('whitespace-nowrap px-1 py-1 text-center', kitTypographyRecipe('tableHead'));
|
|
48
|
-
const GROUP_HEAD_CLASS = mergeClass('whitespace-nowrap px-1 py-1 text-center', kitTypographyRecipe('tableGroup'));
|
|
50
|
+
const GROUP_HEAD_CLASS = mergeClass('whitespace-nowrap px-1.5 py-1 text-center', kitTypographyRecipe('tableGroup'));
|
|
51
|
+
const GROUP_HEAD_SINGLE_CLASS = 'min-w-20 px-2';
|
|
49
52
|
const NOTE_CLASS = 'block font-normal text-fg-subtle';
|
|
50
53
|
const ROW_HEAD_CLASS = mergeClass('whitespace-nowrap px-2 py-0.5 text-start', kitTypographyRecipe('text'));
|
|
51
54
|
const ROW_HEAD_BTN_CLASS = mergeClass(mergeClass('max-w-full truncate rounded-sm text-start', kitTypographyRecipe('text')), kitFocusRingRecipe('item'));
|
|
52
55
|
const ROW_GROUP_CLASS = mergeClass('px-2 pb-1 pt-3 text-start', kitTypographyRecipe('tableGroup'));
|
|
53
56
|
const SUMMARY_CLASS = mergeClass(kitTypographyRecipe('text'), 'whitespace-nowrap px-2 py-0.5 text-end tabular-nums text-fg-muted');
|
|
54
57
|
const CELL_TD_CLASS = 'p-0.5 align-middle';
|
|
55
|
-
const CELL_BTN_CLASS = mergeClass(mergeClass('flex h-8 w-full min-w-
|
|
58
|
+
const CELL_BTN_CLASS = mergeClass(mergeClass('flex h-8 w-full min-w-16 items-center justify-center gap-1 rounded-md px-1', kitTypographyRecipe('deltaSm')), kitFocusRingRecipe('item'));
|
|
56
59
|
const GLYPH_CLASS = 'shrink-0';
|
|
57
60
|
const STICKY_HEAD = 'sticky top-0 z-10 bg-surface';
|
|
58
61
|
const STICKY_FIRST = 'sticky start-0 z-10 bg-surface';
|
|
59
62
|
const STICKY_CORNER = 'sticky start-0 top-0 z-20 bg-surface';
|
|
60
|
-
/**
|
|
61
|
-
|
|
62
|
-
const
|
|
63
|
+
/** 汇总列固定在右侧可见:横向滚动只发生在中间的数据列,窄宽下汇总也不被裁,并补清晰左侧分隔边框 */
|
|
64
|
+
// 汇总列朝数据列的一条线 = 分隔线出口(发丝宽、分隔灰,KT-B1 / KT-B5);空态框 = 凹槽面的壳配方(KT-B2)
|
|
65
|
+
const SUM_LINE = kitDividerRecipe('start');
|
|
66
|
+
const EMPTY_EDGE = kitShellRecipe({ tone: 'sunken', shadow: false });
|
|
67
|
+
const STICKY_LAST = `sticky end-0 z-10 ${SUM_LINE} bg-surface`;
|
|
68
|
+
const STICKY_HEAD_LAST = `sticky end-0 top-0 z-20 ${SUM_LINE} bg-surface`;
|
|
63
69
|
const FIRST_COL_MIN = 'min-w-36';
|
|
64
70
|
const LEGEND_CLASS = mergeClass('m-0 flex list-none flex-wrap items-center gap-x-4 gap-y-1 p-0', kitTypographyRecipe('caption'));
|
|
65
71
|
const LEGEND_ITEM_CLASS = 'inline-flex items-center gap-1.5';
|
|
66
72
|
const LEGEND_SWATCH_CLASS = 'size-3 shrink-0 rounded-sm';
|
|
67
|
-
const EMPTY_INNER_CLASS = mergeClass(
|
|
73
|
+
const EMPTY_INNER_CLASS = mergeClass(`flex w-full items-center justify-center ${EMPTY_EDGE} px-3 py-6`, kitTypographyRecipe('textMuted'));
|
|
68
74
|
|
|
69
75
|
/** 语气 -> 浅底 + 同色相文字(level 0);颜色之外必有字形 / 文本,不靠颜色单独表达 */
|
|
70
76
|
const TONE_SOFT: Readonly<Record<MatrixTone, string>> = Object.freeze({
|
|
@@ -88,7 +94,7 @@ const MISSING_CLASS = 'bg-transparent text-fg-subtle';
|
|
|
88
94
|
|
|
89
95
|
/** 本组件自带的全部类(供编译器并入候选类) */
|
|
90
96
|
export const KIT_MATRIX_CLASS_CANDIDATES: readonly string[] = classCandidates(
|
|
91
|
-
ROOT_CLASS, Object.values(SCROLL_CLASS), SCROLLER_CLASS, TABLE_CLASS, HEAD_CELL_CLASS, GROUP_HEAD_CLASS, NOTE_CLASS, ROW_HEAD_CLASS,
|
|
97
|
+
ROOT_CLASS, Object.values(SCROLL_CLASS), SCROLLER_CLASS, TABLE_CLASS, HEAD_CELL_CLASS, GROUP_HEAD_CLASS, GROUP_HEAD_SINGLE_CLASS, NOTE_CLASS, ROW_HEAD_CLASS,
|
|
92
98
|
ROW_HEAD_BTN_CLASS, ROW_GROUP_CLASS, SUMMARY_CLASS, CELL_TD_CLASS, CELL_BTN_CLASS, GLYPH_CLASS,
|
|
93
99
|
STICKY_HEAD, STICKY_FIRST, STICKY_CORNER, STICKY_LAST, STICKY_HEAD_LAST, FIRST_COL_MIN, LEGEND_CLASS, LEGEND_ITEM_CLASS, LEGEND_SWATCH_CLASS, EMPTY_INNER_CLASS,
|
|
94
100
|
Object.values(TONE_SOFT), LEVEL_CLASSES, MISSING_CLASS, KIT_TAG_UNKNOWN_OUTLINE,
|
|
@@ -251,6 +257,7 @@ export function createMatrix(): SomaComponentDefinition {
|
|
|
251
257
|
const heightKey = str(p.scroll_height, 'md');
|
|
252
258
|
const scroller = el('div', joinClass(SCROLLER_CLASS, SCROLL_CLASS[heightKey] ?? SCROLL_CLASS.md));
|
|
253
259
|
scroller.setAttribute('role', 'region');
|
|
260
|
+
scroller.setAttribute('data-kit-container', 'card');
|
|
254
261
|
const label = str(p.aria_label) !== '' ? str(p.aria_label) : str(p.title);
|
|
255
262
|
if (label !== '') scroller.setAttribute('aria-label', label);
|
|
256
263
|
// 滚动外壳可聚焦(键盘用户需要能滚动),单元格 roving 之外多一个停靠点是 WCAG 对可滚动区域的要求
|
|
@@ -273,9 +280,11 @@ export function createMatrix(): SomaComponentDefinition {
|
|
|
273
280
|
const g = model.cols[i]!.group;
|
|
274
281
|
let j = i;
|
|
275
282
|
while (j < model.cols.length && model.cols[j]!.group === g) j += 1;
|
|
276
|
-
const
|
|
277
|
-
th
|
|
283
|
+
const spanCount = j - i;
|
|
284
|
+
const th = el('th', joinClass(headCls(), spanCount === 1 ? joinClass(GROUP_HEAD_CLASS, GROUP_HEAD_SINGLE_CLASS) : GROUP_HEAD_CLASS), g);
|
|
285
|
+
th.colSpan = spanCount;
|
|
278
286
|
th.setAttribute('scope', 'colgroup');
|
|
287
|
+
if (g !== '') th.title = g;
|
|
279
288
|
tr.appendChild(th);
|
|
280
289
|
i = j;
|
|
281
290
|
}
|