Skip to content

Commit 6afa1a9

Browse files
author
dazhi
committed
fix(minimax-pet): 0.7.0 移动端表格对齐与流式回复滚动位置
- renderMarkdown 替换节点时保留 scrollTop:流式回复每轮都会变,之前会把阅读位置拉回顶部。 - dataset.mdSource 改存短键(长度 + FNV-1a),不再把整段回复复制进属性。 - 表格对齐读 remark-gfm 记在 table 节点上的列对齐,之前读的是 cell.align,永远取不到。 - 补测试:列对齐、滚动位置、短键;DEVELOPMENT 与注释同步说明。
1 parent 30c23b2 commit 6afa1a9

6 files changed

Lines changed: 80 additions & 22 deletions

File tree

‎plugins/minimax-ai/minimax-pet/miniapp/client/compact-pet-view.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ export function createCompactPetView({
143143
else if (!detail) text = '正在读取任务…';
144144
else text = (detail.status === 'error' && textOf(detail.error)) || textOf(detail.lastAssistantText) || '该任务暂时没有可读取的最后回复。';
145145
// Status copy stays plain text; an actual reply is markdown. renderMarkdown keeps
146-
// the DOM when the text is unchanged, so a background refresh cannot reset the
147-
// reader's scroll position.
146+
// the DOM when the text is unchanged and restores the scroll offset when a
147+
// streaming reply replaces it.
148148
if (needsAnswer || !available || taskSelection.detailLoading && !detail) {
149149
if (detailReply.dataset.mdSource !== text) { resetMarkdown(detailReply); setText(detailReply, text); }
150150
} else {

‎plugins/minimax-ai/minimax-pet/miniapp/client/markdown.mjs‎

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ const HEADING_LEVEL_BASE = 2;
1414
const parses = new Map();
1515
const flats = new Map();
1616

17+
// A reply can be 200k characters; keep a short identity for the DOM marker instead
18+
// of copying the whole text into an attribute.
19+
function sourceKey(text) {
20+
let hash = 0x811c9dc5;
21+
for (let index = 0; index < text.length; index++) {
22+
hash ^= text.charCodeAt(index);
23+
hash = Math.imul(hash, 0x01000193);
24+
}
25+
return `${text.length}:${(hash >>> 0).toString(36)}`;
26+
}
27+
1728
function remember(map, key, value) {
1829
if (map.size >= MAX_CACHE) map.delete(map.keys().next().value);
1930
map.set(key, value);
@@ -172,16 +183,17 @@ function blockNode(doc, node) {
172183
wrap.setAttribute('aria-label', '任务回复中的表格');
173184
const table = doc.createElement('table');
174185
const rows = Array.isArray(node.children) ? node.children : [];
186+
const align = Array.isArray(node.align) ? node.align : [];
175187
const header = rows.find((row) => row.type === 'tableRow');
176188
if (header) {
177189
const thead = doc.createElement('thead');
178-
thead.append(tableRow(doc, header, 'th'));
190+
thead.append(tableRow(doc, header, 'th', align));
179191
table.append(thead);
180192
}
181193
const body = rows.filter((row) => row.type === 'tableRow' && row !== header);
182194
if (body.length) {
183195
const tbody = doc.createElement('tbody');
184-
for (const row of body) tbody.append(tableRow(doc, row, 'td'));
196+
for (const row of body) tbody.append(tableRow(doc, row, 'td', align));
185197
table.append(tbody);
186198
}
187199
wrap.append(table);
@@ -218,13 +230,15 @@ function textParagraph(doc, text) {
218230
return p;
219231
}
220232

221-
function tableRow(doc, row, cellTag) {
233+
function tableRow(doc, row, cellTag, align = []) {
222234
const tr = doc.createElement('tr');
223-
for (const cell of Array.isArray(row.children) ? row.children : []) {
224-
if (cell.type !== 'tableCell') continue;
235+
const cells = Array.isArray(row.children) ? row.children.filter((cell) => cell.type === 'tableCell') : [];
236+
for (const [index, cell] of cells.entries()) {
225237
const td = doc.createElement(cellTag);
226238
if (cellTag === 'th') td.scope = 'col';
227-
if (typeof cell.align === 'string') td.dataset.align = cell.align;
239+
// remark-gfm records alignment once per column on the table, not on each cell.
240+
const column = align[index];
241+
if (column === 'left' || column === 'center' || column === 'right') td.dataset.align = column;
228242
appendInline(doc, td, cell.children);
229243
tr.append(td);
230244
}
@@ -251,19 +265,26 @@ function appendBlocks(doc, parent, children, { tight = false } = {}) {
251265
// Public API
252266
// ---------------------------------------------------------------------------
253267

254-
/** Renders markdown into `target`, replacing its content only when the text changed. */
268+
/**
269+
* Renders markdown into `target`, replacing its content only when the text changed.
270+
* A reply that is still streaming changes on every poll, so the reader's scroll
271+
* position is captured and restored around the replace.
272+
*/
255273
export function renderMarkdown(target, text) {
256274
const source = typeof text === 'string' ? text : '';
257-
if (target.dataset.mdSource === source) return false;
275+
const key = sourceKey(source);
276+
if (target.dataset.mdSource === key) return false;
258277
const doc = target.ownerDocument;
259278
const fragment = doc.createDocumentFragment();
260279
for (const child of tree(source).children) {
261280
if (!child || child.type === 'footnoteDefinition') continue;
262281
const node = blockNode(doc, child);
263282
if (node) fragment.append(node);
264283
}
284+
const keepTop = target.scrollTop;
265285
target.replaceChildren(fragment);
266-
target.dataset.mdSource = source;
286+
if (keepTop > 0) target.scrollTop = keepTop;
287+
target.dataset.mdSource = key;
267288
target.classList.add('md');
268289
return true;
269290
}

‎sources/minimax-ai/minimax-pet/DEVELOPMENT.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ npm run check:package
2121
- Wide task views show list and detail together; narrow views provide a return action with focus/scroll restoration. Reading a reply must survive background polls without scroll reset.
2222
- The desktop pet uses compact five-line notification cards and keeps the pet visible while reading. Completed status and answer-needed state must agree across notification, list and detail.
2323
- One global observation plus three detailed observations share the Host budget. Cancellation does not free a slot until its subscription settles. Global reconnect also waits for the previous observation.
24+
- A reply that is still streaming changes on every activity poll: `renderMarkdown` keeps the DOM when the text is unchanged and restores `scrollTop` when it replaces it, so reading is not interrupted.
2425
- A task's latest reply renders through `markdown.mjs`: the vendored parser produces an mdast tree and the module builds DOM nodes from it. Never assign parsed HTML to `innerHTML`; raw HTML in a reply is inert text, links are not opened, and images are not fetched. Notification cards and reminder previews use the flat-text output, which must stay one line per logical line.
2526
- **打开对话** / **去回答** calls `host.navigation.open` synchronously inside the click (the Host checks user activation), never retries automatically, and drops a late result once another task is selected. Without the API the button stays hidden.
2627
- The page follows `host.ui.subscribe` themes through `data-theme`; without it the CSS falls back to `prefers-color-scheme`.

‎sources/minimax-ai/minimax-pet/dev/markdown.test.mjs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,18 @@ domTest('a task list keeps its checked state without form controls', (t) => {
150150
assert.equal(target.querySelector('input, checkbox'), null);
151151
assert.deepEqual([...target.querySelectorAll('.md-task')].map((node) => node.dataset.checked), ['false', 'true']);
152152
});
153+
154+
domTest('table alignment comes from the column spec and a streaming re-render keeps the scroll', (t) => {
155+
const dom = new JSDOM('<!doctype html><div id="reply"></div>');
156+
t.after(() => dom.window.close());
157+
const target = dom.window.document.getElementById('reply');
158+
renderMarkdown(target, '| 名称 | 数量 | 备注 |\n| :--- | ---: | :---: |\n| 新增 | 1204 | 含游客 |');
159+
const cells = [...target.querySelectorAll('.md-table tbody td')];
160+
assert.deepEqual(cells.map((cell) => cell.dataset.align), ['left', 'right', 'center']);
161+
assert.deepEqual([...target.querySelectorAll('.md-table th')].map((cell) => cell.dataset.align), ['left', 'right', 'center']);
162+
// A longer reply replaces the nodes; the reader's scroll offset survives.
163+
target.scrollTop = 90;
164+
assert.equal(renderMarkdown(target, '| 名称 | 数量 | 备注 |\n| :--- | ---: | :---: |\n| 新增 | 1204 | 含游客 |\n| 留存 | 41% | 次周 |'), true);
165+
assert.equal(target.scrollTop, 90);
166+
assert.equal(target.dataset.mdSource.length < 30, true, 'the marker is a short key, not the reply');
167+
});

‎sources/minimax-ai/minimax-pet/miniapp/client/compact-pet-view.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ export function createCompactPetView({
143143
else if (!detail) text = '正在读取任务…';
144144
else text = (detail.status === 'error' && textOf(detail.error)) || textOf(detail.lastAssistantText) || '该任务暂时没有可读取的最后回复。';
145145
// Status copy stays plain text; an actual reply is markdown. renderMarkdown keeps
146-
// the DOM when the text is unchanged, so a background refresh cannot reset the
147-
// reader's scroll position.
146+
// the DOM when the text is unchanged and restores the scroll offset when a
147+
// streaming reply replaces it.
148148
if (needsAnswer || !available || taskSelection.detailLoading && !detail) {
149149
if (detailReply.dataset.mdSource !== text) { resetMarkdown(detailReply); setText(detailReply, text); }
150150
} else {

‎sources/minimax-ai/minimax-pet/miniapp/client/markdown.mjs‎

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ const HEADING_LEVEL_BASE = 2;
1414
const parses = new Map();
1515
const flats = new Map();
1616

17+
// A reply can be 200k characters; keep a short identity for the DOM marker instead
18+
// of copying the whole text into an attribute.
19+
function sourceKey(text) {
20+
let hash = 0x811c9dc5;
21+
for (let index = 0; index < text.length; index++) {
22+
hash ^= text.charCodeAt(index);
23+
hash = Math.imul(hash, 0x01000193);
24+
}
25+
return `${text.length}:${(hash >>> 0).toString(36)}`;
26+
}
27+
1728
function remember(map, key, value) {
1829
if (map.size >= MAX_CACHE) map.delete(map.keys().next().value);
1930
map.set(key, value);
@@ -172,16 +183,17 @@ function blockNode(doc, node) {
172183
wrap.setAttribute('aria-label', '任务回复中的表格');
173184
const table = doc.createElement('table');
174185
const rows = Array.isArray(node.children) ? node.children : [];
186+
const align = Array.isArray(node.align) ? node.align : [];
175187
const header = rows.find((row) => row.type === 'tableRow');
176188
if (header) {
177189
const thead = doc.createElement('thead');
178-
thead.append(tableRow(doc, header, 'th'));
190+
thead.append(tableRow(doc, header, 'th', align));
179191
table.append(thead);
180192
}
181193
const body = rows.filter((row) => row.type === 'tableRow' && row !== header);
182194
if (body.length) {
183195
const tbody = doc.createElement('tbody');
184-
for (const row of body) tbody.append(tableRow(doc, row, 'td'));
196+
for (const row of body) tbody.append(tableRow(doc, row, 'td', align));
185197
table.append(tbody);
186198
}
187199
wrap.append(table);
@@ -218,13 +230,15 @@ function textParagraph(doc, text) {
218230
return p;
219231
}
220232

221-
function tableRow(doc, row, cellTag) {
233+
function tableRow(doc, row, cellTag, align = []) {
222234
const tr = doc.createElement('tr');
223-
for (const cell of Array.isArray(row.children) ? row.children : []) {
224-
if (cell.type !== 'tableCell') continue;
235+
const cells = Array.isArray(row.children) ? row.children.filter((cell) => cell.type === 'tableCell') : [];
236+
for (const [index, cell] of cells.entries()) {
225237
const td = doc.createElement(cellTag);
226238
if (cellTag === 'th') td.scope = 'col';
227-
if (typeof cell.align === 'string') td.dataset.align = cell.align;
239+
// remark-gfm records alignment once per column on the table, not on each cell.
240+
const column = align[index];
241+
if (column === 'left' || column === 'center' || column === 'right') td.dataset.align = column;
228242
appendInline(doc, td, cell.children);
229243
tr.append(td);
230244
}
@@ -251,19 +265,26 @@ function appendBlocks(doc, parent, children, { tight = false } = {}) {
251265
// Public API
252266
// ---------------------------------------------------------------------------
253267

254-
/** Renders markdown into `target`, replacing its content only when the text changed. */
268+
/**
269+
* Renders markdown into `target`, replacing its content only when the text changed.
270+
* A reply that is still streaming changes on every poll, so the reader's scroll
271+
* position is captured and restored around the replace.
272+
*/
255273
export function renderMarkdown(target, text) {
256274
const source = typeof text === 'string' ? text : '';
257-
if (target.dataset.mdSource === source) return false;
275+
const key = sourceKey(source);
276+
if (target.dataset.mdSource === key) return false;
258277
const doc = target.ownerDocument;
259278
const fragment = doc.createDocumentFragment();
260279
for (const child of tree(source).children) {
261280
if (!child || child.type === 'footnoteDefinition') continue;
262281
const node = blockNode(doc, child);
263282
if (node) fragment.append(node);
264283
}
284+
const keepTop = target.scrollTop;
265285
target.replaceChildren(fragment);
266-
target.dataset.mdSource = source;
286+
if (keepTop > 0) target.scrollTop = keepTop;
287+
target.dataset.mdSource = key;
267288
target.classList.add('md');
268289
return true;
269290
}

0 commit comments

Comments
 (0)