fix(core): wrap code in ai chat (#10108)

[BS-2540](https://linear.app/affine-design/issue/BS-2540/ai-chat-中-code-block-需要默认换行)
This commit is contained in:
donteatfriedrice
2025-02-12 09:43:52 +00:00
parent e350ba4a9c
commit 30612de1ad
12 changed files with 43 additions and 15 deletions

View File

@@ -2,6 +2,7 @@ import { CodeBlockSchema } from '@blocksuite/affine-model';
import {
BlockHtmlAdapterExtension,
type BlockHtmlAdapterMatcher,
CODE_BLOCK_WRAP_KEY,
HastUtils,
} from '@blocksuite/affine-shared/adapters';
import type { DeltaInsert } from '@blocksuite/inline';
@@ -37,7 +38,8 @@ export const codeBlockHtmlAdapterMatcher: BlockHtmlAdapterMatcher = {
? codeLang.replace('code-', '')
: undefined;
const { walkerContext, deltaConverter } = context;
const { walkerContext, deltaConverter, configs } = context;
const wrap = configs.get(CODE_BLOCK_WRAP_KEY) === 'true';
walkerContext
.openNode(
{
@@ -46,6 +48,7 @@ export const codeBlockHtmlAdapterMatcher: BlockHtmlAdapterMatcher = {
flavour: 'affine:code',
props: {
language: codeLang ?? 'Plain Text',
wrap,
text: {
'$blocksuite:internal:text$': true,
delta: deltaConverter.astToDelta(codeText, {

View File

@@ -2,6 +2,7 @@ import { CodeBlockSchema } from '@blocksuite/affine-model';
import {
BlockMarkdownAdapterExtension,
type BlockMarkdownAdapterMatcher,
CODE_BLOCK_WRAP_KEY,
type MarkdownAST,
} from '@blocksuite/affine-shared/adapters';
import type { DeltaInsert } from '@blocksuite/inline';
@@ -19,7 +20,8 @@ export const codeBlockMarkdownAdapterMatcher: BlockMarkdownAdapterMatcher = {
if (!isCodeNode(o.node)) {
return;
}
const { walkerContext } = context;
const { walkerContext, configs } = context;
const wrap = configs.get(CODE_BLOCK_WRAP_KEY) === 'true';
walkerContext
.openNode(
{
@@ -28,6 +30,7 @@ export const codeBlockMarkdownAdapterMatcher: BlockMarkdownAdapterMatcher = {
flavour: 'affine:code',
props: {
language: o.node.lang ?? 'Plain Text',
wrap,
text: {
'$blocksuite:internal:text$': true,
delta: [

View File

@@ -2,6 +2,7 @@ import { CodeBlockSchema } from '@blocksuite/affine-model';
import {
BlockNotionHtmlAdapterExtension,
type BlockNotionHtmlAdapterMatcher,
CODE_BLOCK_WRAP_KEY,
HastUtils,
} from '@blocksuite/affine-shared/adapters';
import { nanoid } from '@blocksuite/store';
@@ -20,7 +21,8 @@ export const codeBlockNotionHtmlAdapterMatcher: BlockNotionHtmlAdapterMatcher =
if (!code) {
return;
}
const { walkerContext, deltaConverter } = context;
const { walkerContext, deltaConverter, configs } = context;
const wrap = configs.get(CODE_BLOCK_WRAP_KEY) === 'true';
const codeText =
code.children.length === 1 && code.children[0].type === 'text'
? code.children[0]
@@ -33,6 +35,7 @@ export const codeBlockNotionHtmlAdapterMatcher: BlockNotionHtmlAdapterMatcher =
flavour: CodeBlockSchema.model.flavour,
props: {
language: 'Plain Text',
wrap,
text: {
'$blocksuite:internal:text$': true,
delta: deltaConverter.astToDelta(codeText, {

View File

@@ -0,0 +1,11 @@
import type { TransformerMiddleware } from '@blocksuite/store';
export const CODE_BLOCK_WRAP_KEY = 'codeBlockWrap';
export const codeBlockWrapMiddleware = (
wrap: boolean
): TransformerMiddleware => {
return ({ adapterConfigs }) => {
adapterConfigs.set(CODE_BLOCK_WRAP_KEY, String(wrap));
};
};

View File

@@ -1,2 +1,3 @@
export * from './code';
export * from './copy';
export * from './paste';

View File

@@ -1980,6 +1980,7 @@ describe('html to snapshot', () => {
flavour: 'affine:code',
props: {
language: 'python',
wrap: false,
text: {
'$blocksuite:internal:text$': true,
delta: [

View File

@@ -2481,6 +2481,7 @@ describe('markdown to snapshot', () => {
flavour: 'affine:code',
props: {
language: 'python',
wrap: false,
text: {
'$blocksuite:internal:text$': true,
delta: [
@@ -2526,6 +2527,7 @@ describe('markdown to snapshot', () => {
flavour: 'affine:code',
props: {
language: 'python',
wrap: false,
text: {
'$blocksuite:internal:text$': true,
delta: [
@@ -2577,6 +2579,7 @@ describe('markdown to snapshot', () => {
flavour: 'affine:code',
props: {
language: 'python',
wrap: false,
text: {
'$blocksuite:internal:text$': true,
delta: [
@@ -2628,6 +2631,7 @@ describe('markdown to snapshot', () => {
flavour: 'affine:code',
props: {
language: 'python',
wrap: false,
text: {
'$blocksuite:internal:text$': true,
delta: [

View File

@@ -51,6 +51,7 @@ describe('notion html to snapshot', () => {
flavour: 'affine:code',
props: {
language: 'Plain Text',
wrap: false,
text: {
'$blocksuite:internal:text$': true,
delta: [

View File

@@ -102,6 +102,7 @@ export {
AttachmentAdapter,
AttachmentAdapterFactoryExtension,
AttachmentAdapterFactoryIdentifier,
codeBlockWrapMiddleware,
FetchUtils,
HtmlAdapter,
HtmlAdapterFactoryExtension,