fix(core): apply model ui (#13084)

This commit is contained in:
德布劳外 · 贾贵
2025-07-09 11:55:17 +08:00
committed by GitHub
parent ee878e8f27
commit 3cc33bd40f
4 changed files with 41 additions and 41 deletions

View File

@@ -167,7 +167,7 @@ You should specify the following arguments before the others: [doc_id], [origin_
}, },
]); ]);
return { result }; return { result, content };
} catch { } catch {
return 'Failed to apply edit to the doc'; return 'Failed to apply edit to the doc';
} }

View File

@@ -11,7 +11,7 @@ import {
PenIcon as EditIcon, PenIcon as EditIcon,
PenIcon, PenIcon,
} from '@blocksuite/icons/lit'; } from '@blocksuite/icons/lit';
import { css, html, nothing, type PropertyValues } from 'lit'; import { css, html, nothing } from 'lit';
import { property, state } from 'lit/decorators.js'; import { property, state } from 'lit/decorators.js';
import type { BlockDiffService } from '../../services/block-diff'; import type { BlockDiffService } from '../../services/block-diff';
@@ -37,6 +37,7 @@ interface DocEditToolResult {
result: result:
| { | {
result: string; result: string;
content: string;
} }
| ToolError | ToolError
| null; | null;
@@ -197,9 +198,6 @@ export class DocEditTool extends WithDisposable(ShadowlessElement) {
@state() @state()
accessor isCollapsed = false; accessor isCollapsed = false;
@state()
accessor originalMarkdown: string | undefined;
private async _handleApply(markdown: string) { private async _handleApply(markdown: string) {
if (!this.host) { if (!this.host) {
return; return;
@@ -259,11 +257,8 @@ export class DocEditTool extends WithDisposable(ShadowlessElement) {
return this.renderRichText(removeMarkdownComments(text)); return this.renderRichText(removeMarkdownComments(text));
} }
renderBlockDiffs(originalMarkdown: string, changedMarkdown: string) { renderBlockDiffs(diffs: ReturnType<typeof diffMarkdown>) {
const { patches, oldBlocks } = diffMarkdown( const { patches, oldBlocks } = diffs;
originalMarkdown,
changedMarkdown
);
const oldBlockMap = new Map(oldBlocks.map(b => [b.id, b])); const oldBlockMap = new Map(oldBlocks.map(b => [b.id, b]));
@@ -311,15 +306,18 @@ export class DocEditTool extends WithDisposable(ShadowlessElement) {
} }
renderToolResult() { renderToolResult() {
if (this.data.type !== 'tool-result' || !this.originalMarkdown) { if (this.data.type !== 'tool-result') {
return nothing; return nothing;
} }
const result = this.data.result; const result = this.data.result;
if (result && 'result' in result) { if (result && 'result' in result && 'content' in result) {
const changedMarkdown = result.result; const { result: changedMarkdown, content } = result;
const { instructions, doc_id: docId } = this.data.args; const { instructions, doc_id: docId } = this.data.args;
const diffs = diffMarkdown(content, changedMarkdown);
return html` return html`
<div class="doc-edit-tool-result-wrapper"> <div class="doc-edit-tool-result-wrapper">
<div class="doc-edit-tool-result-title">${instructions}</div> <div class="doc-edit-tool-result-title">${instructions}</div>
@@ -351,7 +349,7 @@ export class DocEditTool extends WithDisposable(ShadowlessElement) {
</div> </div>
<div class="doc-edit-tool-result-card-content"> <div class="doc-edit-tool-result-card-content">
<div class="doc-edit-tool-result-card-content-title"> <div class="doc-edit-tool-result-card-content-title">
${this.renderBlockDiffs(this.originalMarkdown, changedMarkdown)} ${this.renderBlockDiffs(diffs)}
</div> </div>
</div> </div>
<div class="doc-edit-tool-result-card-footer"> <div class="doc-edit-tool-result-card-footer">
@@ -387,21 +385,6 @@ export class DocEditTool extends WithDisposable(ShadowlessElement) {
`; `;
} }
protected override firstUpdated(_changedProperties: PropertyValues): void {
super.firstUpdated(_changedProperties);
if (!this.host) {
return;
}
this.blockDiffService
?.getMarkdownFromDoc(this.host.store)
.then(markdown => {
this.originalMarkdown = markdown;
})
.catch(() => {
// TODO: handle error
});
}
protected override render() { protected override render() {
const { data } = this; const { data } = this;

View File

@@ -31,8 +31,22 @@ export class AffineBlockDiffWidgetForBlock extends WidgetComponent {
margin: 0; margin: 0;
background: transparent; background: transparent;
} }
.ai-block-diff,
.ai-block-diff.delete,
.ai-block-diff.update,
.ai-block-diff.insert {
pointer-events: auto;
}
.diff-options {
visibility: hidden;
}
.ai-block-diff:hover .diff-options,
.ai-block-diff.delete:hover .diff-options,
.ai-block-diff.update:hover .diff-options,
.ai-block-diff.insert:hover .diff-options {
visibility: visible;
}
`; `;
private _setDeletedStyle(blockId: string) { private _setDeletedStyle(blockId: string) {
const deleted = document.querySelector<HTMLDivElement>( const deleted = document.querySelector<HTMLDivElement>(
`[data-block-id="${blockId}"]` `[data-block-id="${blockId}"]`
@@ -59,12 +73,11 @@ export class AffineBlockDiffWidgetForBlock extends WidgetComponent {
} }
this._setDeletedStyle(blockId); this._setDeletedStyle(blockId);
const diffId = `delete-${blockId}`;
return html`<div return html`<div class="ai-block-diff delete" data-diff-id=${diffId}>
class="ai-block-diff delete"
data-diff-id=${`delete-${blockId}`}
>
<ai-block-diff-options <ai-block-diff-options
class="diff-options"
.onAccept=${() => .onAccept=${() =>
this.diffService.accept( this.diffService.accept(
{ type: 'delete', payload: { id: blockId } }, { type: 'delete', payload: { id: blockId } },
@@ -79,12 +92,10 @@ export class AffineBlockDiffWidgetForBlock extends WidgetComponent {
private _renderInsert(from: string, blocks: Block[]) { private _renderInsert(from: string, blocks: Block[]) {
return blocks return blocks
.map((block, offset) => { .map((block, offset) => {
const diffId = `insert-${block.id}-${offset}`;
return this.diffService.isRejected('insert', `${from}:${offset}`) return this.diffService.isRejected('insert', `${from}:${offset}`)
? null ? null
: html`<div : html`<div class="ai-block-diff insert" data-diff-id=${diffId}>
class="ai-block-diff insert"
data-diff-id=${`insert-${block.id}-${offset}`}
>
<chat-content-rich-text <chat-content-rich-text
.text=${block.content} .text=${block.content}
.host=${this.host} .host=${this.host}
@@ -92,6 +103,7 @@ export class AffineBlockDiffWidgetForBlock extends WidgetComponent {
.extensions=${this.userExtensions} .extensions=${this.userExtensions}
></chat-content-rich-text> ></chat-content-rich-text>
<ai-block-diff-options <ai-block-diff-options
class="diff-options"
.onAccept=${() => .onAccept=${() =>
this.diffService.accept( this.diffService.accept(
{ {
@@ -115,9 +127,9 @@ export class AffineBlockDiffWidgetForBlock extends WidgetComponent {
if (this.diffService.isRejected('update', blockId)) { if (this.diffService.isRejected('update', blockId)) {
return nothing; return nothing;
} }
const diffId = `update-${blockId}`;
return html` return html`
<div class="ai-block-diff update" data-diff-id=${`update-${blockId}`}> <div class="ai-block-diff update" data-diff-id=${diffId}>
<chat-content-rich-text <chat-content-rich-text
.text=${content} .text=${content}
.host=${this.host} .host=${this.host}
@@ -125,6 +137,7 @@ export class AffineBlockDiffWidgetForBlock extends WidgetComponent {
.extensions=${this.userExtensions} .extensions=${this.userExtensions}
></chat-content-rich-text> ></chat-content-rich-text>
<ai-block-diff-options <ai-block-diff-options
class="diff-options"
.onAccept=${() => .onAccept=${() =>
this.diffService.accept( this.diffService.accept(
{ {

View File

@@ -18,7 +18,11 @@ export const AFFINE_BLOCK_DIFF_WIDGET_FOR_PAGE =
export class AffineBlockDiffWidgetForPage extends WidgetComponent { export class AffineBlockDiffWidgetForPage extends WidgetComponent {
static override styles = css` static override styles = css`
.ai-block-diff-scroller-container { .ai-block-diff-scroller-container {
margin: auto; position: fixed;
left: 50%;
transform: translateX(-50%);
bottom: 180px;
margin: 0;
display: flex; display: flex;
gap: 4px; gap: 4px;
justify-content: center; justify-content: center;