mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-18 10:36:22 +08:00
refactor(editor): extensionalize plain text adapter (#9330)
This commit is contained in:
@@ -2,17 +2,20 @@ import {
|
||||
EdgelessSurfaceBlockMarkdownAdapterExtension,
|
||||
SurfaceBlockMarkdownAdapterExtension,
|
||||
} from './markdown/markdown.js';
|
||||
import { elementToPlainTextAdapterMatchers } from './plain-text/element-adapter/elements/index.js';
|
||||
import {
|
||||
EdgelessSurfaceBlockPlainTextAdapterExtension,
|
||||
SurfaceBlockPlainTextAdapterExtension,
|
||||
} from './plain-text/plain-text.js';
|
||||
|
||||
export const SurfaceBlockAdapterExtensions = [
|
||||
...elementToPlainTextAdapterMatchers,
|
||||
SurfaceBlockPlainTextAdapterExtension,
|
||||
SurfaceBlockMarkdownAdapterExtension,
|
||||
];
|
||||
|
||||
export const EdgelessSurfaceBlockAdapterExtensions = [
|
||||
...elementToPlainTextAdapterMatchers,
|
||||
EdgelessSurfaceBlockPlainTextAdapterExtension,
|
||||
EdgelessSurfaceBlockMarkdownAdapterExtension,
|
||||
];
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
export {
|
||||
EdgelessSurfaceBlockAdapterExtensions,
|
||||
SurfaceBlockAdapterExtensions,
|
||||
} from './extension.js';
|
||||
+4
-4
@@ -1,11 +1,11 @@
|
||||
import type { ElementModelToPlainTextAdapterMatcher } from '../type.js';
|
||||
import { ElementToPlainTextAdapterExtension } from '../type.js';
|
||||
|
||||
export const brushToPlainTextAdapterMatcher: ElementModelToPlainTextAdapterMatcher =
|
||||
{
|
||||
export const brushToPlainTextAdapterMatcher =
|
||||
ElementToPlainTextAdapterExtension({
|
||||
name: 'brush',
|
||||
match: elementModel => elementModel.type === 'brush',
|
||||
toAST: () => {
|
||||
const content = `Brush Stroke`;
|
||||
return { content };
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
+4
-4
@@ -1,8 +1,8 @@
|
||||
import { getConnectorText } from '../../../utils/text.js';
|
||||
import type { ElementModelToPlainTextAdapterMatcher } from '../type.js';
|
||||
import { ElementToPlainTextAdapterExtension } from '../type.js';
|
||||
|
||||
export const connectorToPlainTextAdapterMatcher: ElementModelToPlainTextAdapterMatcher =
|
||||
{
|
||||
export const connectorToPlainTextAdapterMatcher =
|
||||
ElementToPlainTextAdapterExtension({
|
||||
name: 'connector',
|
||||
match: elementModel => elementModel.type === 'connector',
|
||||
toAST: elementModel => {
|
||||
@@ -10,4 +10,4 @@ export const connectorToPlainTextAdapterMatcher: ElementModelToPlainTextAdapterM
|
||||
const content = `Connector, with text label "${text}"`;
|
||||
return { content };
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
+4
-4
@@ -1,8 +1,8 @@
|
||||
import { getGroupTitle } from '../../../utils/text.js';
|
||||
import type { ElementModelToPlainTextAdapterMatcher } from '../type.js';
|
||||
import { ElementToPlainTextAdapterExtension } from '../type.js';
|
||||
|
||||
export const groupToPlainTextAdapterMatcher: ElementModelToPlainTextAdapterMatcher =
|
||||
{
|
||||
export const groupToPlainTextAdapterMatcher =
|
||||
ElementToPlainTextAdapterExtension({
|
||||
name: 'group',
|
||||
match: elementModel => elementModel.type === 'group',
|
||||
toAST: elementModel => {
|
||||
@@ -10,4 +10,4 @@ export const groupToPlainTextAdapterMatcher: ElementModelToPlainTextAdapterMatch
|
||||
const content = `Group, with title "${title}"`;
|
||||
return { content };
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ import { mindmapToPlainTextAdapterMatcher } from './mindmap.js';
|
||||
import { shapeToPlainTextAdapterMatcher } from './shape.js';
|
||||
import { textToPlainTextAdapterMatcher } from './text.js';
|
||||
|
||||
export const elementModelToPlainTextAdapterMatchers = [
|
||||
export const elementToPlainTextAdapterMatchers = [
|
||||
groupToPlainTextAdapterMatcher,
|
||||
shapeToPlainTextAdapterMatcher,
|
||||
connectorToPlainTextAdapterMatcher,
|
||||
|
||||
+4
-4
@@ -1,12 +1,12 @@
|
||||
import { getMindMapTreeText } from '../../../utils/text.js';
|
||||
import type { ElementModelToPlainTextAdapterMatcher } from '../type.js';
|
||||
import { ElementToPlainTextAdapterExtension } from '../type.js';
|
||||
|
||||
export const mindmapToPlainTextAdapterMatcher: ElementModelToPlainTextAdapterMatcher =
|
||||
{
|
||||
export const mindmapToPlainTextAdapterMatcher =
|
||||
ElementToPlainTextAdapterExtension({
|
||||
name: 'mindmap',
|
||||
match: elementModel => elementModel.type === 'mindmap',
|
||||
toAST: (elementModel, context) => {
|
||||
const mindMapContent = getMindMapTreeText(elementModel, context.elements);
|
||||
return { content: mindMapContent };
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
+4
-4
@@ -1,9 +1,9 @@
|
||||
import type { MindMapTreeNode } from '../../../types/mindmap.js';
|
||||
import { getShapeText, getShapeType } from '../../../utils/text.js';
|
||||
import type { ElementModelToPlainTextAdapterMatcher } from '../type.js';
|
||||
import { ElementToPlainTextAdapterExtension } from '../type.js';
|
||||
|
||||
export const shapeToPlainTextAdapterMatcher: ElementModelToPlainTextAdapterMatcher =
|
||||
{
|
||||
export const shapeToPlainTextAdapterMatcher =
|
||||
ElementToPlainTextAdapterExtension({
|
||||
name: 'shape',
|
||||
match: elementModel => elementModel.type === 'shape',
|
||||
toAST: (elementModel, context) => {
|
||||
@@ -31,4 +31,4 @@ export const shapeToPlainTextAdapterMatcher: ElementModelToPlainTextAdapterMatch
|
||||
content = `${shapeType}, with text label "${text}"`;
|
||||
return { content };
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
+4
-3
@@ -1,7 +1,7 @@
|
||||
import { getTextElementText } from '../../../utils/text.js';
|
||||
import type { ElementModelToPlainTextAdapterMatcher } from '../type.js';
|
||||
import { ElementToPlainTextAdapterExtension } from '../type.js';
|
||||
|
||||
export const textToPlainTextAdapterMatcher: ElementModelToPlainTextAdapterMatcher =
|
||||
export const textToPlainTextAdapterMatcher = ElementToPlainTextAdapterExtension(
|
||||
{
|
||||
name: 'text',
|
||||
match: elementModel => elementModel.type === 'text',
|
||||
@@ -9,4 +9,5 @@ export const textToPlainTextAdapterMatcher: ElementModelToPlainTextAdapterMatche
|
||||
const content = getTextElementText(elementModel);
|
||||
return { content };
|
||||
},
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
@@ -4,15 +4,14 @@ import {
|
||||
ElementModelAdapter,
|
||||
type ElementModelAdapterContext,
|
||||
} from '../../type.js';
|
||||
import { elementModelToPlainTextAdapterMatchers } from './elements/index.js';
|
||||
import type { ElementModelToPlainTextAdapterMatcher } from './type.js';
|
||||
import type { ElementToPlainTextAdapterMatcher } from './type.js';
|
||||
|
||||
export class PlainTextElementModelAdapter extends ElementModelAdapter<
|
||||
string,
|
||||
TextBuffer
|
||||
> {
|
||||
constructor(
|
||||
readonly elementModelMatchers: ElementModelToPlainTextAdapterMatcher[] = elementModelToPlainTextAdapterMatchers
|
||||
readonly elementModelMatchers: ElementToPlainTextAdapterMatcher[]
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,29 @@
|
||||
import type { TextBuffer } from '@blocksuite/affine-shared/adapters';
|
||||
import type { ExtensionType } from '@blocksuite/block-std';
|
||||
import {
|
||||
createIdentifier,
|
||||
type ServiceIdentifier,
|
||||
} from '@blocksuite/global/di';
|
||||
|
||||
import type { ElementModelMatcher } from '../../type.js';
|
||||
|
||||
export type ElementModelToPlainTextAdapterMatcher =
|
||||
ElementModelMatcher<TextBuffer>;
|
||||
export type ElementToPlainTextAdapterMatcher = ElementModelMatcher<TextBuffer>;
|
||||
|
||||
export const ElementToPlainTextAdapterMatcherIdentifier =
|
||||
createIdentifier<ElementToPlainTextAdapterMatcher>(
|
||||
'elementToPlainTextAdapterMatcher'
|
||||
);
|
||||
|
||||
export function ElementToPlainTextAdapterExtension(
|
||||
matcher: ElementToPlainTextAdapterMatcher
|
||||
): ExtensionType & {
|
||||
identifier: ServiceIdentifier<ElementToPlainTextAdapterMatcher>;
|
||||
} {
|
||||
const identifier = ElementToPlainTextAdapterMatcherIdentifier(matcher.name);
|
||||
return {
|
||||
setup: di => {
|
||||
di.addImpl(identifier, () => matcher);
|
||||
},
|
||||
identifier,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
|
||||
import { getMindMapNodeMap } from '../utils/mindmap.js';
|
||||
import { PlainTextElementModelAdapter } from './element-adapter/index.js';
|
||||
import { ElementToPlainTextAdapterMatcherIdentifier } from './element-adapter/type.js';
|
||||
|
||||
export const surfaceBlockPlainTextAdapterMatcher: BlockPlainTextAdapterMatcher =
|
||||
{
|
||||
@@ -30,8 +31,18 @@ export const edgelessSurfaceBlockPlainTextAdapterMatcher: BlockPlainTextAdapterM
|
||||
toBlockSnapshot: {},
|
||||
fromBlockSnapshot: {
|
||||
enter: (o, context) => {
|
||||
const { walkerContext } = context;
|
||||
const plainTextElementModelAdapter = new PlainTextElementModelAdapter();
|
||||
const { walkerContext, provider } = context;
|
||||
if (!provider) {
|
||||
context.walkerContext.skipAllChildren();
|
||||
return;
|
||||
}
|
||||
|
||||
const elementModelMatchers = Array.from(
|
||||
provider.getAll(ElementToPlainTextAdapterMatcherIdentifier).values()
|
||||
);
|
||||
const plainTextElementModelAdapter = new PlainTextElementModelAdapter(
|
||||
elementModelMatchers
|
||||
);
|
||||
if ('elements' in o.node.props) {
|
||||
const elements = o.node.props.elements as Record<
|
||||
string,
|
||||
|
||||
@@ -44,6 +44,10 @@ import {
|
||||
isSameFontFamily,
|
||||
wrapFontFamily,
|
||||
} from './utils/font.js';
|
||||
export {
|
||||
EdgelessSurfaceBlockAdapterExtensions,
|
||||
SurfaceBlockAdapterExtensions,
|
||||
} from './adapters/index.js';
|
||||
export type { SurfaceContext } from './surface-block.js';
|
||||
export { SurfaceBlockComponent } from './surface-block.js';
|
||||
export { SurfaceBlockModel, SurfaceBlockSchema } from './surface-model.js';
|
||||
|
||||
@@ -42,9 +42,13 @@ export {
|
||||
BlockPlainTextAdapterExtension,
|
||||
type BlockPlainTextAdapterMatcher,
|
||||
BlockPlainTextAdapterMatcherIdentifier,
|
||||
InlineDeltaToPlainTextAdapterExtension,
|
||||
type InlineDeltaToPlainTextAdapterMatcher,
|
||||
InlineDeltaToPlainTextAdapterMatcherIdentifier,
|
||||
type PlainText,
|
||||
PlainTextAdapter,
|
||||
PlainTextAdapterFactoryExtension,
|
||||
PlainTextAdapterFactoryIdentifier,
|
||||
PlainTextDeltaConverter,
|
||||
} from './plain-text';
|
||||
export {
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import { createIdentifier } from '@blocksuite/global/di';
|
||||
import type { ExtensionType } from '@blocksuite/block-std';
|
||||
import {
|
||||
createIdentifier,
|
||||
type ServiceIdentifier,
|
||||
} from '@blocksuite/global/di';
|
||||
import type { DeltaInsert } from '@blocksuite/inline';
|
||||
|
||||
import type { AffineTextAttributes } from '../../types/index.js';
|
||||
@@ -17,6 +21,22 @@ export const InlineDeltaToPlainTextAdapterMatcherIdentifier =
|
||||
'InlineDeltaToPlainTextAdapterMatcher'
|
||||
);
|
||||
|
||||
export function InlineDeltaToPlainTextAdapterExtension(
|
||||
matcher: InlineDeltaToPlainTextAdapterMatcher
|
||||
): ExtensionType & {
|
||||
identifier: ServiceIdentifier<InlineDeltaToPlainTextAdapterMatcher>;
|
||||
} {
|
||||
const identifier = InlineDeltaToPlainTextAdapterMatcherIdentifier(
|
||||
matcher.name
|
||||
);
|
||||
return {
|
||||
setup: di => {
|
||||
di.addImpl(identifier, () => matcher);
|
||||
},
|
||||
identifier,
|
||||
};
|
||||
}
|
||||
|
||||
export type PlainTextASTToDeltaMatcher = ASTToDeltaMatcher<string>;
|
||||
|
||||
export class PlainTextDeltaConverter extends DeltaASTConverter<
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export * from './block-adapter.js';
|
||||
export * from './delta-converter.js';
|
||||
export * from './type.js';
|
||||
export * from './plain-text.js';
|
||||
|
||||
+29
-19
@@ -2,16 +2,8 @@ import {
|
||||
DEFAULT_NOTE_BACKGROUND_COLOR,
|
||||
NoteDisplayMode,
|
||||
} from '@blocksuite/affine-model';
|
||||
import {
|
||||
type AdapterContext,
|
||||
AdapterFactoryIdentifier,
|
||||
type BlockPlainTextAdapterMatcher,
|
||||
BlockPlainTextAdapterMatcherIdentifier,
|
||||
type PlainText,
|
||||
PlainTextDeltaConverter,
|
||||
type TextBuffer,
|
||||
} from '@blocksuite/affine-shared/adapters';
|
||||
import type { ExtensionType } from '@blocksuite/block-std';
|
||||
import type { ServiceProvider } from '@blocksuite/global/di';
|
||||
import {
|
||||
type AssetsManager,
|
||||
ASTWalker,
|
||||
@@ -32,8 +24,21 @@ import {
|
||||
type ToDocSnapshotPayload,
|
||||
} from '@blocksuite/store';
|
||||
|
||||
import { defaultBlockPlainTextAdapterMatchers } from './block-matcher.js';
|
||||
import { inlineDeltaToPlainTextAdapterMatchers } from './delta-converter/inline-delta.js';
|
||||
import {
|
||||
type AdapterContext,
|
||||
AdapterFactoryIdentifier,
|
||||
type TextBuffer,
|
||||
} from '../types';
|
||||
import {
|
||||
type BlockPlainTextAdapterMatcher,
|
||||
BlockPlainTextAdapterMatcherIdentifier,
|
||||
} from './block-adapter';
|
||||
import {
|
||||
InlineDeltaToPlainTextAdapterMatcherIdentifier,
|
||||
PlainTextDeltaConverter,
|
||||
} from './delta-converter';
|
||||
|
||||
export type PlainText = string;
|
||||
|
||||
type PlainTextToSliceSnapshotPayload = {
|
||||
file: PlainText;
|
||||
@@ -46,11 +51,20 @@ type PlainTextToSliceSnapshotPayload = {
|
||||
export class PlainTextAdapter extends BaseAdapter<PlainText> {
|
||||
deltaConverter: PlainTextDeltaConverter;
|
||||
|
||||
readonly blockMatchers: BlockPlainTextAdapterMatcher[];
|
||||
|
||||
constructor(
|
||||
job: Job,
|
||||
readonly blockMatchers: BlockPlainTextAdapterMatcher[] = defaultBlockPlainTextAdapterMatchers
|
||||
readonly provider: ServiceProvider
|
||||
) {
|
||||
super(job);
|
||||
const blockMatchers = Array.from(
|
||||
provider.getAll(BlockPlainTextAdapterMatcherIdentifier).values()
|
||||
);
|
||||
const inlineDeltaToPlainTextAdapterMatchers = Array.from(
|
||||
provider.getAll(InlineDeltaToPlainTextAdapterMatcherIdentifier).values()
|
||||
);
|
||||
this.blockMatchers = blockMatchers;
|
||||
this.deltaConverter = new PlainTextDeltaConverter(
|
||||
job.adapterConfigs,
|
||||
inlineDeltaToPlainTextAdapterMatchers,
|
||||
@@ -78,6 +92,7 @@ export class PlainTextAdapter extends BaseAdapter<PlainText> {
|
||||
configs: this.configs,
|
||||
job: this.job,
|
||||
deltaConverter: this.deltaConverter,
|
||||
provider: this.provider,
|
||||
textBuffer,
|
||||
};
|
||||
await matcher.fromBlockSnapshot.enter?.(o, adapterContext);
|
||||
@@ -93,6 +108,7 @@ export class PlainTextAdapter extends BaseAdapter<PlainText> {
|
||||
configs: this.configs,
|
||||
job: this.job,
|
||||
deltaConverter: this.deltaConverter,
|
||||
provider: this.provider,
|
||||
textBuffer,
|
||||
};
|
||||
await matcher.fromBlockSnapshot.leave?.(o, adapterContext);
|
||||
@@ -309,13 +325,7 @@ export const PlainTextAdapterFactoryIdentifier =
|
||||
export const PlainTextAdapterFactoryExtension: ExtensionType = {
|
||||
setup: di => {
|
||||
di.addImpl(PlainTextAdapterFactoryIdentifier, provider => ({
|
||||
get: (job: Job) =>
|
||||
new PlainTextAdapter(
|
||||
job,
|
||||
Array.from(
|
||||
provider.getAll(BlockPlainTextAdapterMatcherIdentifier).values()
|
||||
)
|
||||
),
|
||||
get: (job: Job) => new PlainTextAdapter(job, provider),
|
||||
}));
|
||||
},
|
||||
};
|
||||
@@ -1 +0,0 @@
|
||||
export type PlainText = string;
|
||||
@@ -1,4 +1,4 @@
|
||||
import { createIdentifier } from '@blocksuite/global/di';
|
||||
import { createIdentifier, type ServiceProvider } from '@blocksuite/global/di';
|
||||
import type { BaseTextAttributes, DeltaInsert } from '@blocksuite/inline';
|
||||
import {
|
||||
type AssetsManager,
|
||||
@@ -38,6 +38,7 @@ export type AdapterContext<
|
||||
job: Job;
|
||||
deltaConverter: TConverter;
|
||||
textBuffer: TextBuffer;
|
||||
provider?: ServiceProvider;
|
||||
assets?: AssetsManager;
|
||||
pageMap?: Map<string, string>;
|
||||
updateAssetIds?: (assetsId: string) => void;
|
||||
|
||||
@@ -2,6 +2,8 @@ import {
|
||||
DEFAULT_NOTE_BACKGROUND_COLOR,
|
||||
NoteDisplayMode,
|
||||
} from '@blocksuite/affine-model';
|
||||
import { PlainTextAdapter } from '@blocksuite/affine-shared/adapters';
|
||||
import { Container } from '@blocksuite/global/di';
|
||||
import type {
|
||||
BlockSnapshot,
|
||||
DocSnapshot,
|
||||
@@ -9,10 +11,20 @@ import type {
|
||||
} from '@blocksuite/store';
|
||||
import { describe, expect, test } from 'vitest';
|
||||
|
||||
import { PlainTextAdapter } from '../../_common/adapters/plain-text/plain-text.js';
|
||||
import { defaultBlockPlainTextAdapterMatchers } from '../../_common/adapters/plain-text/block-matcher.js';
|
||||
import { inlineDeltaToPlainTextAdapterMatchers } from '../../_common/adapters/plain-text/delta-converter/inline-delta.js';
|
||||
import { embedSyncedDocMiddleware } from '../../_common/transformers/middlewares.js';
|
||||
import { createJob } from '../utils/create-job.js';
|
||||
|
||||
const container = new Container();
|
||||
[
|
||||
...defaultBlockPlainTextAdapterMatchers,
|
||||
...inlineDeltaToPlainTextAdapterMatchers,
|
||||
].forEach(ext => {
|
||||
ext.setup(container);
|
||||
});
|
||||
const provider = container.provider();
|
||||
|
||||
describe('snapshot to plain text', () => {
|
||||
test('paragraph', async () => {
|
||||
const blockSnapshot: BlockSnapshot = {
|
||||
@@ -157,7 +169,7 @@ describe('snapshot to plain text', () => {
|
||||
};
|
||||
|
||||
const plainText = 'aaabbbccc\nddd\neee\nfff\nggg\n';
|
||||
const plainTextAdapter = new PlainTextAdapter(createJob());
|
||||
const plainTextAdapter = new PlainTextAdapter(createJob(), provider);
|
||||
const target = await plainTextAdapter.fromBlockSnapshot({
|
||||
snapshot: blockSnapshot,
|
||||
});
|
||||
@@ -316,7 +328,7 @@ describe('snapshot to plain text', () => {
|
||||
|
||||
const plainText = 'aaa\nbbb\nccc\nddd\neee\n';
|
||||
|
||||
const plainTextAdapter = new PlainTextAdapter(createJob());
|
||||
const plainTextAdapter = new PlainTextAdapter(createJob(), provider);
|
||||
const target = await plainTextAdapter.fromBlockSnapshot({
|
||||
snapshot: blockSnapshot,
|
||||
});
|
||||
@@ -386,7 +398,7 @@ describe('snapshot to plain text', () => {
|
||||
};
|
||||
|
||||
const plainText = 'aaa\n---\n';
|
||||
const plainTextAdapter = new PlainTextAdapter(createJob());
|
||||
const plainTextAdapter = new PlainTextAdapter(createJob(), provider);
|
||||
const target = await plainTextAdapter.fromBlockSnapshot({
|
||||
snapshot: blockSnapshot,
|
||||
});
|
||||
@@ -449,7 +461,7 @@ describe('snapshot to plain text', () => {
|
||||
};
|
||||
|
||||
const plainText = 'import this\n';
|
||||
const plainTextAdapter = new PlainTextAdapter(createJob());
|
||||
const plainTextAdapter = new PlainTextAdapter(createJob(), provider);
|
||||
const target = await plainTextAdapter.fromBlockSnapshot({
|
||||
snapshot: blockSnapshot,
|
||||
});
|
||||
@@ -568,7 +580,10 @@ describe('snapshot to plain text', () => {
|
||||
adapterConfigs.set('title:deadbeef', 'test');
|
||||
adapterConfigs.set('docLinkBaseUrl', 'https://example.com');
|
||||
};
|
||||
const plainTextAdapter = new PlainTextAdapter(createJob([middleware]));
|
||||
const plainTextAdapter = new PlainTextAdapter(
|
||||
createJob([middleware]),
|
||||
provider
|
||||
);
|
||||
|
||||
const plainText =
|
||||
'aaa: https://affine.pro/\ntest: https://example.com/deadbeef?mode=page&blockIds=abc%2C123&elementIds=def%2C456&databaseId=deadbeef&databaseRowId=123\nE=mc^2\n';
|
||||
@@ -670,7 +685,7 @@ describe('snapshot to plain text', () => {
|
||||
],
|
||||
};
|
||||
|
||||
const plainTextAdapter = new PlainTextAdapter(createJob());
|
||||
const plainTextAdapter = new PlainTextAdapter(createJob(), provider);
|
||||
const target = await plainTextAdapter.fromBlockSnapshot({
|
||||
snapshot: blockSnapshot,
|
||||
});
|
||||
@@ -750,7 +765,10 @@ describe('snapshot to plain text', () => {
|
||||
};
|
||||
const plainText =
|
||||
'test: https://example.com/4T5ObMgEIMII-4Bexyta1?mode=page&blockIds=abc%2C123&elementIds=def%2C456&databaseId=deadbeef&databaseRowId=123\n';
|
||||
const plainTextAdapter = new PlainTextAdapter(createJob([middleware]));
|
||||
const plainTextAdapter = new PlainTextAdapter(
|
||||
createJob([middleware]),
|
||||
provider
|
||||
);
|
||||
const target = await plainTextAdapter.fromBlockSnapshot({
|
||||
snapshot: blockSnapShot,
|
||||
});
|
||||
@@ -1146,7 +1164,7 @@ describe('snapshot to plain text', () => {
|
||||
await job.snapshotToDoc(syncedDocSnapshot);
|
||||
await job.snapshotToDoc(docSnapShot);
|
||||
|
||||
const mdAdapter = new PlainTextAdapter(job);
|
||||
const mdAdapter = new PlainTextAdapter(job, provider);
|
||||
const target = await mdAdapter.fromDocSnapshot({
|
||||
snapshot: docSnapShot,
|
||||
});
|
||||
@@ -1180,7 +1198,7 @@ describe('snapshot to plain text', () => {
|
||||
};
|
||||
|
||||
const plainText = 'LaTex, with value: E=mc^2\n';
|
||||
const plainTextAdapter = new PlainTextAdapter(createJob());
|
||||
const plainTextAdapter = new PlainTextAdapter(createJob(), provider);
|
||||
const target = await plainTextAdapter.fromBlockSnapshot({
|
||||
snapshot: blockSnapshot,
|
||||
});
|
||||
@@ -1405,7 +1423,7 @@ describe('snapshot to plain text', () => {
|
||||
| Task 1 | TODO | 2023-12-15 | 1 | 65 | test1,test2 | test2: https://google.com | https://google.com | true |
|
||||
| Task 2 | In Progress | 2023-12-20 | | | | test1 | | |
|
||||
`;
|
||||
const plainTextAdapter = new PlainTextAdapter(createJob());
|
||||
const plainTextAdapter = new PlainTextAdapter(createJob(), provider);
|
||||
const target = await plainTextAdapter.fromBlockSnapshot({
|
||||
snapshot: blockSnapshot,
|
||||
});
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { HtmlAdapterFactoryExtension } from '@blocksuite/affine-shared/adapters';
|
||||
import {
|
||||
HtmlAdapterFactoryExtension,
|
||||
PlainTextAdapterFactoryExtension,
|
||||
} from '@blocksuite/affine-shared/adapters';
|
||||
import type { ExtensionType } from '@blocksuite/block-std';
|
||||
|
||||
import { AttachmentAdapterFactoryExtension } from './attachment.js';
|
||||
@@ -10,12 +13,13 @@ import { MixTextAdapterFactoryExtension } from './mix-text.js';
|
||||
import { notionHtmlInlineToDeltaMatchers } from './notion-html/delta-converter/html-inline.js';
|
||||
import { NotionHtmlAdapterFactoryExtension } from './notion-html/notion-html.js';
|
||||
import { NotionTextAdapterFactoryExtension } from './notion-text.js';
|
||||
import { PlainTextAdapterFactoryExtension } from './plain-text/plain-text.js';
|
||||
import { inlineDeltaToPlainTextAdapterMatchers } from './plain-text/delta-converter/inline-delta.js';
|
||||
|
||||
export const AdapterFactoryExtensions: ExtensionType[] = [
|
||||
...htmlInlineToDeltaMatchers,
|
||||
...inlineDeltaToHtmlAdapterMatchers,
|
||||
...notionHtmlInlineToDeltaMatchers,
|
||||
...inlineDeltaToPlainTextAdapterMatchers,
|
||||
AttachmentAdapterFactoryExtension,
|
||||
ImageAdapterFactoryExtension,
|
||||
MarkdownAdapterFactoryExtension,
|
||||
|
||||
@@ -5,4 +5,3 @@ export * from './markdown/index.js';
|
||||
export * from './mix-text.js';
|
||||
export * from './notion-html/index.js';
|
||||
export * from './notion-text.js';
|
||||
export * from './plain-text/plain-text.js';
|
||||
|
||||
@@ -1,34 +1,33 @@
|
||||
import { bookmarkBlockPlainTextAdapterMatcher } from '@blocksuite/affine-block-bookmark';
|
||||
import { BookmarkBlockPlainTextAdapterExtension } from '@blocksuite/affine-block-bookmark';
|
||||
import {
|
||||
embedFigmaBlockPlainTextAdapterMatcher,
|
||||
embedGithubBlockPlainTextAdapterMatcher,
|
||||
embedLinkedDocBlockPlainTextAdapterMatcher,
|
||||
embedLoomBlockPlainTextAdapterMatcher,
|
||||
embedSyncedDocBlockPlainTextAdapterMatcher,
|
||||
embedYoutubeBlockPlainTextAdapterMatcher,
|
||||
EmbedFigmaBlockPlainTextAdapterExtension,
|
||||
EmbedGithubBlockPlainTextAdapterExtension,
|
||||
EmbedLinkedDocBlockPlainTextAdapterExtension,
|
||||
EmbedLoomBlockPlainTextAdapterExtension,
|
||||
EmbedSyncedDocBlockPlainTextAdapterExtension,
|
||||
EmbedYoutubeBlockPlainTextAdapterExtension,
|
||||
} from '@blocksuite/affine-block-embed';
|
||||
import { latexBlockPlainTextAdapterMatcher } from '@blocksuite/affine-block-latex';
|
||||
import { listBlockPlainTextAdapterMatcher } from '@blocksuite/affine-block-list';
|
||||
import { paragraphBlockPlainTextAdapterMatcher } from '@blocksuite/affine-block-paragraph';
|
||||
import type { BlockPlainTextAdapterMatcher } from '@blocksuite/affine-shared/adapters';
|
||||
import { LatexBlockPlainTextAdapterExtension } from '@blocksuite/affine-block-latex';
|
||||
import { ListBlockPlainTextAdapterExtension } from '@blocksuite/affine-block-list';
|
||||
import { ParagraphBlockPlainTextAdapterExtension } from '@blocksuite/affine-block-paragraph';
|
||||
import type { ExtensionType } from '@blocksuite/block-std';
|
||||
|
||||
import { codeBlockPlainTextAdapterMatcher } from '../../../code-block/adapters/plain-text.js';
|
||||
import { databaseBlockPlainTextAdapterMatcher } from '../../../database-block/adapters/plain-text.js';
|
||||
import { dividerBlockPlainTextAdapterMatcher } from '../../../divider-block/adapters/plain-text.js';
|
||||
import { CodeBlockPlainTextAdapterExtension } from '../../../code-block/adapters/plain-text.js';
|
||||
import { DatabaseBlockPlainTextAdapterExtension } from '../../../database-block/adapters/plain-text.js';
|
||||
import { DividerBlockPlainTextAdapterExtension } from '../../../divider-block/adapters/plain-text.js';
|
||||
|
||||
export const defaultBlockPlainTextAdapterMatchers: BlockPlainTextAdapterMatcher[] =
|
||||
[
|
||||
paragraphBlockPlainTextAdapterMatcher,
|
||||
listBlockPlainTextAdapterMatcher,
|
||||
dividerBlockPlainTextAdapterMatcher,
|
||||
codeBlockPlainTextAdapterMatcher,
|
||||
bookmarkBlockPlainTextAdapterMatcher,
|
||||
embedFigmaBlockPlainTextAdapterMatcher,
|
||||
embedGithubBlockPlainTextAdapterMatcher,
|
||||
embedLoomBlockPlainTextAdapterMatcher,
|
||||
embedYoutubeBlockPlainTextAdapterMatcher,
|
||||
embedLinkedDocBlockPlainTextAdapterMatcher,
|
||||
embedSyncedDocBlockPlainTextAdapterMatcher,
|
||||
latexBlockPlainTextAdapterMatcher,
|
||||
databaseBlockPlainTextAdapterMatcher,
|
||||
];
|
||||
export const defaultBlockPlainTextAdapterMatchers: ExtensionType[] = [
|
||||
ParagraphBlockPlainTextAdapterExtension,
|
||||
ListBlockPlainTextAdapterExtension,
|
||||
DividerBlockPlainTextAdapterExtension,
|
||||
CodeBlockPlainTextAdapterExtension,
|
||||
BookmarkBlockPlainTextAdapterExtension,
|
||||
EmbedFigmaBlockPlainTextAdapterExtension,
|
||||
EmbedGithubBlockPlainTextAdapterExtension,
|
||||
EmbedLoomBlockPlainTextAdapterExtension,
|
||||
EmbedYoutubeBlockPlainTextAdapterExtension,
|
||||
EmbedLinkedDocBlockPlainTextAdapterExtension,
|
||||
EmbedSyncedDocBlockPlainTextAdapterExtension,
|
||||
LatexBlockPlainTextAdapterExtension,
|
||||
DatabaseBlockPlainTextAdapterExtension,
|
||||
];
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { generateDocUrl } from '@blocksuite/affine-block-embed';
|
||||
import type {
|
||||
InlineDeltaToPlainTextAdapterMatcher,
|
||||
TextBuffer,
|
||||
import {
|
||||
InlineDeltaToPlainTextAdapterExtension,
|
||||
type TextBuffer,
|
||||
} from '@blocksuite/affine-shared/adapters';
|
||||
import type { ExtensionType } from '@blocksuite/block-std';
|
||||
|
||||
export const referenceDeltaMarkdownAdapterMatch: InlineDeltaToPlainTextAdapterMatcher =
|
||||
{
|
||||
export const referenceDeltaMarkdownAdapterMatch =
|
||||
InlineDeltaToPlainTextAdapterExtension({
|
||||
name: 'reference',
|
||||
match: delta => !!delta.attributes?.reference,
|
||||
toAST: (delta, context) => {
|
||||
@@ -30,10 +31,10 @@ export const referenceDeltaMarkdownAdapterMatch: InlineDeltaToPlainTextAdapterMa
|
||||
content,
|
||||
};
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
export const linkDeltaMarkdownAdapterMatch: InlineDeltaToPlainTextAdapterMatcher =
|
||||
{
|
||||
export const linkDeltaMarkdownAdapterMatch =
|
||||
InlineDeltaToPlainTextAdapterExtension({
|
||||
name: 'link',
|
||||
match: delta => !!delta.attributes?.link,
|
||||
toAST: delta => {
|
||||
@@ -51,10 +52,10 @@ export const linkDeltaMarkdownAdapterMatch: InlineDeltaToPlainTextAdapterMatcher
|
||||
content,
|
||||
};
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
export const latexDeltaMarkdownAdapterMatch: InlineDeltaToPlainTextAdapterMatcher =
|
||||
{
|
||||
export const latexDeltaMarkdownAdapterMatch =
|
||||
InlineDeltaToPlainTextAdapterExtension({
|
||||
name: 'inlineLatex',
|
||||
match: delta => !!delta.attributes?.latex,
|
||||
toAST: delta => {
|
||||
@@ -68,11 +69,10 @@ export const latexDeltaMarkdownAdapterMatch: InlineDeltaToPlainTextAdapterMatche
|
||||
content: delta.attributes?.latex,
|
||||
};
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
export const inlineDeltaToPlainTextAdapterMatchers: InlineDeltaToPlainTextAdapterMatcher[] =
|
||||
[
|
||||
referenceDeltaMarkdownAdapterMatch,
|
||||
linkDeltaMarkdownAdapterMatch,
|
||||
latexDeltaMarkdownAdapterMatch,
|
||||
];
|
||||
export const inlineDeltaToPlainTextAdapterMatchers: ExtensionType[] = [
|
||||
referenceDeltaMarkdownAdapterMatch,
|
||||
linkDeltaMarkdownAdapterMatch,
|
||||
latexDeltaMarkdownAdapterMatch,
|
||||
];
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { HtmlAdapter } from '@blocksuite/affine-shared/adapters';
|
||||
import {
|
||||
HtmlAdapter,
|
||||
PlainTextAdapter,
|
||||
} from '@blocksuite/affine-shared/adapters';
|
||||
import {
|
||||
type BlockComponent,
|
||||
Clipboard,
|
||||
@@ -6,7 +9,6 @@ import {
|
||||
} from '@blocksuite/block-std';
|
||||
import { assertExists, DisposableGroup } from '@blocksuite/global/utils';
|
||||
|
||||
import { PlainTextAdapter } from '../../_common/adapters/index.js';
|
||||
import { pasteMiddleware } from '../../root-block/clipboard/middlewares/index.js';
|
||||
|
||||
export class CodeClipboardController {
|
||||
|
||||
@@ -103,6 +103,9 @@ export {
|
||||
HtmlAdapter,
|
||||
HtmlAdapterFactoryExtension,
|
||||
HtmlAdapterFactoryIdentifier,
|
||||
PlainTextAdapter,
|
||||
PlainTextAdapterFactoryExtension,
|
||||
PlainTextAdapterFactoryIdentifier,
|
||||
} from '@blocksuite/affine-shared/adapters';
|
||||
export * from '@blocksuite/affine-shared/services';
|
||||
export { scrollbarStyle } from '@blocksuite/affine-shared/styles';
|
||||
|
||||
@@ -86,7 +86,9 @@ export async function getContentFromSlice(
|
||||
}
|
||||
processTextInSnapshot(snapshot, host);
|
||||
const adapter =
|
||||
type === 'markdown' ? new MarkdownAdapter(job) : new PlainTextAdapter(job);
|
||||
type === 'markdown'
|
||||
? new MarkdownAdapter(job)
|
||||
: new PlainTextAdapter(job, host.std.provider);
|
||||
const content = await adapter.fromSliceSnapshot({
|
||||
snapshot,
|
||||
assets: job.assetsManager,
|
||||
@@ -104,7 +106,7 @@ export async function getPlainTextFromSlice(host: EditorHost, slice: Slice) {
|
||||
return '';
|
||||
}
|
||||
processTextInSnapshot(snapshot, host);
|
||||
const plainTextAdapter = new PlainTextAdapter(job);
|
||||
const plainTextAdapter = new PlainTextAdapter(job, host.std.provider);
|
||||
const plainText = await plainTextAdapter.fromSliceSnapshot({
|
||||
snapshot,
|
||||
assets: job.assetsManager,
|
||||
|
||||
Reference in New Issue
Block a user