-
-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate plain text adapter to extension (#8831)
[BS-1855](https://linear.app/affine-design/issue/BS-1855/migrate-plain-text-adapter) - [x] Migrate PlainTextAdapter to extension - [x] Handle inline delta: reference, link, inline code - [x] Add unit test
- Loading branch information
1 parent
441c677
commit 6e0bc08
Showing
23 changed files
with
1,032 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './markdown.js'; | ||
export * from './plain-text.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { DeltaInsert } from '@blocksuite/inline'; | ||
|
||
import { ListBlockSchema } from '@blocksuite/affine-model'; | ||
import { | ||
BlockPlainTextAdapterExtension, | ||
type BlockPlainTextAdapterMatcher, | ||
} from '@blocksuite/affine-shared/adapters'; | ||
|
||
export const listBlockPlainTextAdapterMatcher: BlockPlainTextAdapterMatcher = { | ||
flavour: ListBlockSchema.model.flavour, | ||
toMatch: () => false, | ||
fromMatch: o => o.node.flavour === ListBlockSchema.model.flavour, | ||
toBlockSnapshot: {}, | ||
fromBlockSnapshot: { | ||
enter: (o, context) => { | ||
const text = (o.node.props.text ?? { delta: [] }) as { | ||
delta: DeltaInsert[]; | ||
}; | ||
const { deltaConverter } = context; | ||
const buffer = deltaConverter.deltaToAST(text.delta).join(''); | ||
context.textBuffer.content += buffer; | ||
context.textBuffer.content += '\n'; | ||
}, | ||
}, | ||
}; | ||
|
||
export const ListBlockPlainTextAdapterExtension = | ||
BlockPlainTextAdapterExtension(listBlockPlainTextAdapterMatcher); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from './adapters/markdown.js'; | ||
export * from './adapters/index.js'; | ||
export * from './list-block.js'; | ||
export * from './list-service.js'; | ||
export * from './list-spec.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './markdown.js'; | ||
export * from './plain-text.js'; |
29 changes: 29 additions & 0 deletions
29
packages/affine/block-paragraph/src/adapters/plain-text.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { DeltaInsert } from '@blocksuite/inline'; | ||
|
||
import { ParagraphBlockSchema } from '@blocksuite/affine-model'; | ||
import { | ||
BlockPlainTextAdapterExtension, | ||
type BlockPlainTextAdapterMatcher, | ||
} from '@blocksuite/affine-shared/adapters'; | ||
|
||
export const paragraphBlockPlainTextAdapterMatcher: BlockPlainTextAdapterMatcher = | ||
{ | ||
flavour: ParagraphBlockSchema.model.flavour, | ||
toMatch: () => false, | ||
fromMatch: o => o.node.flavour === ParagraphBlockSchema.model.flavour, | ||
toBlockSnapshot: {}, | ||
fromBlockSnapshot: { | ||
enter: (o, context) => { | ||
const text = (o.node.props.text ?? { delta: [] }) as { | ||
delta: DeltaInsert[]; | ||
}; | ||
const { deltaConverter } = context; | ||
const buffer = deltaConverter.deltaToAST(text.delta).join(''); | ||
context.textBuffer.content += buffer; | ||
context.textBuffer.content += '\n'; | ||
}, | ||
}, | ||
}; | ||
|
||
export const ParagraphBlockPlainTextAdapterExtension = | ||
BlockPlainTextAdapterExtension(paragraphBlockPlainTextAdapterMatcher); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from './adapters/markdown.js'; | ||
export * from './adapters/index.js'; | ||
export * from './paragraph-block.js'; | ||
export * from './paragraph-service.js'; | ||
export * from './paragraph-spec.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/affine/shared/src/adapters/plain-text/block-adapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { ExtensionType } from '@blocksuite/block-std'; | ||
|
||
import { | ||
createIdentifier, | ||
type ServiceIdentifier, | ||
} from '@blocksuite/global/di'; | ||
|
||
import type { BlockAdapterMatcher } from '../type.js'; | ||
|
||
export type BlockPlainTextAdapterMatcher = BlockAdapterMatcher; | ||
|
||
export const BlockPlainTextAdapterMatcherIdentifier = | ||
createIdentifier<BlockPlainTextAdapterMatcher>( | ||
'BlockPlainTextAdapterMatcher' | ||
); | ||
|
||
export function BlockPlainTextAdapterExtension( | ||
matcher: BlockPlainTextAdapterMatcher | ||
): ExtensionType & { | ||
identifier: ServiceIdentifier<BlockPlainTextAdapterMatcher>; | ||
} { | ||
const identifier = BlockPlainTextAdapterMatcherIdentifier(matcher.flavour); | ||
return { | ||
setup: di => { | ||
di.addImpl(identifier, () => matcher); | ||
}, | ||
identifier, | ||
}; | ||
} |
63 changes: 63 additions & 0 deletions
63
packages/affine/shared/src/adapters/plain-text/delta-converter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type { DeltaInsert } from '@blocksuite/inline'; | ||
|
||
import { createIdentifier } from '@blocksuite/global/di'; | ||
|
||
import type { AffineTextAttributes } from '../../types/index.js'; | ||
|
||
import { | ||
type ASTToDeltaMatcher, | ||
DeltaASTConverter, | ||
type InlineDeltaMatcher, | ||
type TextBuffer, | ||
} from '../type.js'; | ||
|
||
export type InlineDeltaToPlainTextAdapterMatcher = | ||
InlineDeltaMatcher<TextBuffer>; | ||
|
||
export const InlineDeltaToPlainTextAdapterMatcherIdentifier = | ||
createIdentifier<InlineDeltaToPlainTextAdapterMatcher>( | ||
'InlineDeltaToPlainTextAdapterMatcher' | ||
); | ||
|
||
export type PlainTextASTToDeltaMatcher = ASTToDeltaMatcher<string>; | ||
|
||
export class PlainTextDeltaConverter extends DeltaASTConverter< | ||
AffineTextAttributes, | ||
string | ||
> { | ||
constructor( | ||
readonly configs: Map<string, string>, | ||
readonly inlineDeltaMatchers: InlineDeltaToPlainTextAdapterMatcher[], | ||
readonly plainTextASTToDeltaMatchers: PlainTextASTToDeltaMatcher[] | ||
) { | ||
super(); | ||
} | ||
|
||
astToDelta(ast: string) { | ||
const context = { | ||
configs: this.configs, | ||
toDelta: (ast: string) => this.astToDelta(ast), | ||
}; | ||
for (const matcher of this.plainTextASTToDeltaMatchers) { | ||
if (matcher.match(ast)) { | ||
return matcher.toDelta(ast, context); | ||
} | ||
} | ||
return []; | ||
} | ||
|
||
deltaToAST(deltas: DeltaInsert<AffineTextAttributes>[]): string[] { | ||
return deltas.map(delta => { | ||
const context = { | ||
configs: this.configs, | ||
current: { content: delta.insert }, | ||
}; | ||
for (const matcher of this.inlineDeltaMatchers) { | ||
if (matcher.match(delta)) { | ||
context.current = matcher.toAST(delta, context); | ||
} | ||
} | ||
return context.current.content; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './block-adapter.js'; | ||
export * from './delta-converter.js'; | ||
export * from './type.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type PlainText = string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.