Some checks failed
Build & Test / Analyze (javascript, affine) (push) Failing after 4s
Build & Test / Analyze (typescript, affine) (push) Failing after 3s
Build & Test / Rust test filter (push) Failing after 4s
Build & Test / Check yarn binary (push) Failing after 3s
Build & Test / E2E BlockSuite Test (3) (push) Failing after 3s
Build & Test / Analyze (javascript, blocksuite) (push) Failing after 3s
Build & Test / Analyze (typescript, blocksuite) (push) Failing after 3s
Build & Test / Mobile native build filter (push) Failing after 3s
Build & Test / E2E BlockSuite Test (1) (push) Failing after 4s
Build & Test / E2E BlockSuite Test (2) (push) Failing after 4s
Build & Test / E2E Mobile Test (1) (push) Failing after 3s
Build & Test / Unit Test (1) (push) Has been skipped
Build & Test / Unit Test (2) (push) Has been skipped
Build & Test / Unit Test (3) (push) Has been skipped
Build & Test / Unit Test (4) (push) Has been skipped
Build & Test / Unit Test (5) (push) Has been skipped
Build & Test / Native Unit Test (push) Has been skipped
Build & Test / E2E BlockSuite Test (4) (push) Failing after 3s
Build & Test / E2E BlockSuite Test (5) (push) Failing after 3s
Build & Test / E2E BlockSuite Cross Browser Test (push) Failing after 3s
Build & Test / E2E Mobile Test (2) (push) Failing after 3s
Build & Test / Build AFFiNE native (x86_64-unknown-linux-gnu) (push) Failing after 3s
Build & Test / Build Server native (push) Failing after 3s
Build & Test / Check Git Status (push) Has been skipped
Build & Test / Server Test (0, 4) (push) Has been skipped
Build & Test / Server Test (1, 4) (push) Has been skipped
Build & Test / Server Test (2, 4) (push) Has been skipped
Build & Test / Server Test (3, 4) (push) Has been skipped
Build & Test / Server Test with Elasticsearch (push) Has been skipped
Build & Test / Build @affine/electron renderer (push) Failing after 3s
Build & Test / Copilot test filter (push) Failing after 4s
Build & Test / Lint Rust (push) Has been skipped
Build & Test / Server Test with Embedded Search (push) Has been skipped
Build & Test / Server Test with Manticore Search (push) Has been skipped
Build & Test / Server E2E Test (push) Has been skipped
Build & Test / Cloud Desktop E2E Test (push) Has been skipped
Build & Test / Cloud E2E Test 1/2 (push) Has been skipped
Build & Test / Cloud E2E Test 2/2 (push) Has been skipped
Build & Test / Build Android app (push) Has been skipped
Build & Test / Test Android app (push) Has been skipped
Build & Test / Run native tests (push) Has been skipped
Build & Test / Server Copilot Api Test (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (1, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (2, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (3, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (4, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (5, 5) (push) Has been skipped
Release / Canary Gate (push) Has been skipped
Build & Test / Build AFFiNE native (aarch64-apple-darwin) (push) Has been cancelled
Build & Test / Lint (push) Has been cancelled
Build & Test / Typecheck (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-apple-darwin) (push) Has been cancelled
Build & Test / Desktop Test (ubuntu-latest, linux, x64, x86_64-unknown-linux-gnu, true) (push) Has been cancelled
Build & Test / Desktop Test (windows-latest, windows, x64, x86_64-pc-windows-msvc, true) (push) Has been cancelled
Build & Test / E2E Test (1) (push) Has been cancelled
Build & Test / E2E Test (2) (push) Has been cancelled
Build & Test / E2E Test (3) (push) Has been cancelled
Build & Test / E2E Test (4) (push) Has been cancelled
Build & Test / E2E Test (5) (push) Has been cancelled
Build & Test / Build AFFiNE native (aarch64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, arm64, aarch64-apple-darwin, true) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, x64, x86_64-apple-darwin, false) (push) Has been cancelled
Build & Test / 3, 2, 1 Launch (push) Has been cancelled
Build & Test / Build iOS app (push) Has been cancelled
Copilot Test Automatically / Setup Test (push) Failing after 1s
Release / Prepare (push) Failing after 2s
Release / Release Cloud (push) Has been skipped
Release / Release Desktop (push) Has been skipped
Release / Release Docker Image (push) Has been skipped
Release / Release Mobile (push) Has been skipped
325 lines
8.5 KiB
TypeScript
325 lines
8.5 KiB
TypeScript
import { DefaultTheme, NoteDisplayMode } from '@blocksuite/affine-model';
|
|
import type { ServiceProvider } from '@blocksuite/global/di';
|
|
import {
|
|
type AssetsManager,
|
|
ASTWalker,
|
|
BaseAdapter,
|
|
type BlockSnapshot,
|
|
BlockSnapshotSchema,
|
|
type DocSnapshot,
|
|
type ExtensionType,
|
|
type FromBlockSnapshotPayload,
|
|
type FromBlockSnapshotResult,
|
|
type FromDocSnapshotPayload,
|
|
type FromDocSnapshotResult,
|
|
type FromSliceSnapshotPayload,
|
|
type FromSliceSnapshotResult,
|
|
nanoid,
|
|
type SliceSnapshot,
|
|
type ToBlockSnapshotPayload,
|
|
type ToDocSnapshotPayload,
|
|
type Transformer,
|
|
} from '@blocksuite/store';
|
|
|
|
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;
|
|
assets?: AssetsManager;
|
|
workspaceId: string;
|
|
pageId: string;
|
|
};
|
|
|
|
export class PlainTextAdapter extends BaseAdapter<PlainText> {
|
|
deltaConverter: PlainTextDeltaConverter;
|
|
|
|
readonly blockMatchers: BlockPlainTextAdapterMatcher[];
|
|
|
|
constructor(job: Transformer, provider: ServiceProvider) {
|
|
super(job, provider);
|
|
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,
|
|
[]
|
|
);
|
|
}
|
|
|
|
private async _traverseSnapshot(
|
|
snapshot: BlockSnapshot
|
|
): Promise<{ plaintext: string }> {
|
|
const textBuffer: TextBuffer = {
|
|
content: '',
|
|
};
|
|
const walker = new ASTWalker<BlockSnapshot, TextBuffer>();
|
|
walker.setONodeTypeGuard(
|
|
(node): node is BlockSnapshot =>
|
|
BlockSnapshotSchema.safeParse(node).success
|
|
);
|
|
walker.setEnter(async (o, context) => {
|
|
for (const matcher of this.blockMatchers) {
|
|
if (matcher.fromMatch(o)) {
|
|
const adapterContext: AdapterContext<BlockSnapshot, TextBuffer> = {
|
|
walker,
|
|
walkerContext: context,
|
|
configs: this.configs,
|
|
job: this.job,
|
|
deltaConverter: this.deltaConverter,
|
|
provider: this.provider,
|
|
textBuffer,
|
|
};
|
|
await matcher.fromBlockSnapshot.enter?.(o, adapterContext);
|
|
}
|
|
}
|
|
});
|
|
walker.setLeave(async (o, context) => {
|
|
for (const matcher of this.blockMatchers) {
|
|
if (matcher.fromMatch(o)) {
|
|
const adapterContext: AdapterContext<BlockSnapshot, TextBuffer> = {
|
|
walker,
|
|
walkerContext: context,
|
|
configs: this.configs,
|
|
job: this.job,
|
|
deltaConverter: this.deltaConverter,
|
|
provider: this.provider,
|
|
textBuffer,
|
|
};
|
|
await matcher.fromBlockSnapshot.leave?.(o, adapterContext);
|
|
}
|
|
}
|
|
});
|
|
await walker.walkONode(snapshot);
|
|
return {
|
|
plaintext: textBuffer.content,
|
|
};
|
|
}
|
|
|
|
async fromBlockSnapshot({
|
|
snapshot,
|
|
}: FromBlockSnapshotPayload): Promise<FromBlockSnapshotResult<PlainText>> {
|
|
const { plaintext } = await this._traverseSnapshot(snapshot);
|
|
return {
|
|
file: plaintext,
|
|
assetsIds: [],
|
|
};
|
|
}
|
|
|
|
async fromDocSnapshot({
|
|
snapshot,
|
|
assets,
|
|
}: FromDocSnapshotPayload): Promise<FromDocSnapshotResult<PlainText>> {
|
|
let buffer = '';
|
|
if (snapshot.meta.title) {
|
|
buffer += `${snapshot.meta.title}\n\n`;
|
|
}
|
|
const { file, assetsIds } = await this.fromBlockSnapshot({
|
|
snapshot: snapshot.blocks,
|
|
assets,
|
|
});
|
|
buffer += file;
|
|
return {
|
|
file: buffer,
|
|
assetsIds,
|
|
};
|
|
}
|
|
|
|
async fromSliceSnapshot({
|
|
snapshot,
|
|
}: FromSliceSnapshotPayload): Promise<FromSliceSnapshotResult<PlainText>> {
|
|
let buffer = '';
|
|
const sliceAssetsIds: string[] = [];
|
|
for (const contentSlice of snapshot.content) {
|
|
const { plaintext } = await this._traverseSnapshot(contentSlice);
|
|
buffer += plaintext;
|
|
}
|
|
const plaintext =
|
|
buffer.match(/\n/g)?.length === 1 ? buffer.trimEnd() : buffer;
|
|
return {
|
|
file: plaintext,
|
|
assetsIds: sliceAssetsIds,
|
|
};
|
|
}
|
|
|
|
toBlockSnapshot(payload: ToBlockSnapshotPayload<PlainText>): BlockSnapshot {
|
|
payload.file = payload.file.replaceAll('\r', '');
|
|
return {
|
|
type: 'block',
|
|
id: nanoid(),
|
|
flavour: 'affine:note',
|
|
props: {
|
|
xywh: '[0,0,800,95]',
|
|
background: DefaultTheme.noteBackgrounColor,
|
|
index: 'a0',
|
|
hidden: false,
|
|
displayMode: NoteDisplayMode.DocAndEdgeless,
|
|
},
|
|
children: payload.file.split('\n').map((line): BlockSnapshot => {
|
|
return {
|
|
type: 'block',
|
|
id: nanoid(),
|
|
flavour: 'affine:paragraph',
|
|
props: {
|
|
type: 'text',
|
|
text: {
|
|
'$blocksuite:internal:text$': true,
|
|
delta: [
|
|
{
|
|
insert: line,
|
|
},
|
|
],
|
|
},
|
|
},
|
|
children: [],
|
|
};
|
|
}),
|
|
};
|
|
}
|
|
|
|
toDocSnapshot(payload: ToDocSnapshotPayload<PlainText>): DocSnapshot {
|
|
payload.file = payload.file.replaceAll('\r', '');
|
|
return {
|
|
type: 'page',
|
|
meta: {
|
|
id: nanoid(),
|
|
title: 'Untitled',
|
|
createDate: Date.now(),
|
|
tags: [],
|
|
},
|
|
blocks: {
|
|
type: 'block',
|
|
id: nanoid(),
|
|
flavour: 'affine:page',
|
|
props: {
|
|
title: {
|
|
'$blocksuite:internal:text$': true,
|
|
delta: [
|
|
{
|
|
insert: 'Untitled',
|
|
},
|
|
],
|
|
},
|
|
},
|
|
children: [
|
|
{
|
|
type: 'block',
|
|
id: nanoid(),
|
|
flavour: 'affine:surface',
|
|
props: {
|
|
elements: {},
|
|
},
|
|
children: [],
|
|
},
|
|
{
|
|
type: 'block',
|
|
id: nanoid(),
|
|
flavour: 'affine:note',
|
|
props: {
|
|
xywh: '[0,0,800,95]',
|
|
background: DefaultTheme.noteBackgrounColor,
|
|
index: 'a0',
|
|
hidden: false,
|
|
displayMode: NoteDisplayMode.DocAndEdgeless,
|
|
},
|
|
children: payload.file.split('\n').map((line): BlockSnapshot => {
|
|
return {
|
|
type: 'block',
|
|
id: nanoid(),
|
|
flavour: 'affine:paragraph',
|
|
props: {
|
|
type: 'text',
|
|
text: {
|
|
'$blocksuite:internal:text$': true,
|
|
delta: [
|
|
{
|
|
insert: line,
|
|
},
|
|
],
|
|
},
|
|
},
|
|
children: [],
|
|
};
|
|
}),
|
|
},
|
|
],
|
|
},
|
|
};
|
|
}
|
|
|
|
toSliceSnapshot(
|
|
payload: PlainTextToSliceSnapshotPayload
|
|
): SliceSnapshot | null {
|
|
if (payload.file.trim().length === 0) {
|
|
return null;
|
|
}
|
|
payload.file = payload.file.replaceAll('\r', '');
|
|
const contentSlice = {
|
|
type: 'block',
|
|
id: nanoid(),
|
|
flavour: 'affine:note',
|
|
props: {
|
|
xywh: '[0,0,800,95]',
|
|
background: DefaultTheme.noteBackgrounColor,
|
|
index: 'a0',
|
|
hidden: false,
|
|
displayMode: NoteDisplayMode.DocAndEdgeless,
|
|
},
|
|
children: payload.file.split('\n').map((line): BlockSnapshot => {
|
|
return {
|
|
type: 'block',
|
|
id: nanoid(),
|
|
flavour: 'affine:paragraph',
|
|
props: {
|
|
type: 'text',
|
|
text: {
|
|
'$blocksuite:internal:text$': true,
|
|
delta: [
|
|
{
|
|
insert: line,
|
|
},
|
|
],
|
|
},
|
|
},
|
|
children: [],
|
|
};
|
|
}),
|
|
} as BlockSnapshot;
|
|
return {
|
|
type: 'slice',
|
|
content: [contentSlice],
|
|
workspaceId: payload.workspaceId,
|
|
pageId: payload.pageId,
|
|
};
|
|
}
|
|
}
|
|
|
|
export const PlainTextAdapterFactoryIdentifier =
|
|
AdapterFactoryIdentifier('PlainText');
|
|
|
|
export const PlainTextAdapterFactoryExtension: ExtensionType = {
|
|
setup: di => {
|
|
di.addImpl(PlainTextAdapterFactoryIdentifier, provider => ({
|
|
get: (job: Transformer) => new PlainTextAdapter(job, provider),
|
|
}));
|
|
},
|
|
};
|