mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-21 00:07:01 +08:00
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:
@@ -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, {
|
||||
|
||||
@@ -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: [
|
||||
|
||||
@@ -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, {
|
||||
|
||||
11
blocksuite/affine/shared/src/adapters/middlewares/code.ts
Normal file
11
blocksuite/affine/shared/src/adapters/middlewares/code.ts
Normal 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));
|
||||
};
|
||||
};
|
||||
@@ -1,2 +1,3 @@
|
||||
export * from './code';
|
||||
export * from './copy';
|
||||
export * from './paste';
|
||||
|
||||
@@ -1980,6 +1980,7 @@ describe('html to snapshot', () => {
|
||||
flavour: 'affine:code',
|
||||
props: {
|
||||
language: 'python',
|
||||
wrap: false,
|
||||
text: {
|
||||
'$blocksuite:internal:text$': true,
|
||||
delta: [
|
||||
|
||||
@@ -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: [
|
||||
|
||||
@@ -51,6 +51,7 @@ describe('notion html to snapshot', () => {
|
||||
flavour: 'affine:code',
|
||||
props: {
|
||||
language: 'Plain Text',
|
||||
wrap: false,
|
||||
text: {
|
||||
'$blocksuite:internal:text$': true,
|
||||
delta: [
|
||||
|
||||
@@ -102,6 +102,7 @@ export {
|
||||
AttachmentAdapter,
|
||||
AttachmentAdapterFactoryExtension,
|
||||
AttachmentAdapterFactoryIdentifier,
|
||||
codeBlockWrapMiddleware,
|
||||
FetchUtils,
|
||||
HtmlAdapter,
|
||||
HtmlAdapterFactoryExtension,
|
||||
|
||||
Reference in New Issue
Block a user