refactor(editor): extract html adapter to shared (#9319)

This commit is contained in:
Saul-Mirone
2024-12-26 04:05:10 +00:00
parent 37e44e0341
commit fad0237d94
24 changed files with 77 additions and 62 deletions

View File

@@ -29,6 +29,9 @@
"lodash.clonedeep": "^4.5.0", "lodash.clonedeep": "^4.5.0",
"lodash.mergewith": "^4.6.2", "lodash.mergewith": "^4.6.2",
"minimatch": "^10.0.1", "minimatch": "^10.0.1",
"rehype-parse": "^9.0.0",
"rehype-stringify": "^10.0.0",
"unified": "^11.0.5",
"zod": "^3.23.8" "zod": "^3.23.8"
}, },
"exports": { "exports": {

View File

@@ -2,16 +2,6 @@ import {
DEFAULT_NOTE_BACKGROUND_COLOR, DEFAULT_NOTE_BACKGROUND_COLOR,
NoteDisplayMode, NoteDisplayMode,
} from '@blocksuite/affine-model'; } from '@blocksuite/affine-model';
import {
type AdapterContext,
type BlockHtmlAdapterMatcher,
BlockHtmlAdapterMatcherIdentifier,
HastUtils,
type HtmlAST,
HtmlASTToDeltaMatcherIdentifier,
HtmlDeltaConverter,
InlineDeltaToHtmlAdapterMatcherIdentifier,
} from '@blocksuite/affine-shared/adapters';
import type { ExtensionType } from '@blocksuite/block-std'; import type { ExtensionType } from '@blocksuite/block-std';
import type { ServiceProvider } from '@blocksuite/global/di'; import type { ServiceProvider } from '@blocksuite/global/di';
import { import {
@@ -38,7 +28,21 @@ import rehypeParse from 'rehype-parse';
import rehypeStringify from 'rehype-stringify'; import rehypeStringify from 'rehype-stringify';
import { unified } from 'unified'; import { unified } from 'unified';
import { AdapterFactoryIdentifier } from '../type.js'; import {
type AdapterContext,
AdapterFactoryIdentifier,
type HtmlAST,
} from '../types';
import { HastUtils } from '../utils/hast';
import {
type BlockHtmlAdapterMatcher,
BlockHtmlAdapterMatcherIdentifier,
} from './block-adapter';
import {
HtmlASTToDeltaMatcherIdentifier,
HtmlDeltaConverter,
InlineDeltaToHtmlAdapterMatcherIdentifier,
} from './delta-converter';
export type Html = string; export type Html = string;

View File

@@ -1,3 +1,3 @@
export * from './block-adapter.js'; export * from './block-adapter.js';
export * from './delta-converter.js'; export * from './delta-converter.js';
export * from './type.js'; export * from './html.js';

View File

@@ -1 +0,0 @@
export type Html = string;

View File

@@ -3,6 +3,9 @@ export {
type BlockHtmlAdapterMatcher, type BlockHtmlAdapterMatcher,
BlockHtmlAdapterMatcherIdentifier, BlockHtmlAdapterMatcherIdentifier,
type Html, type Html,
HtmlAdapter,
HtmlAdapterFactoryExtension,
HtmlAdapterFactoryIdentifier,
HtmlASTToDeltaExtension, HtmlASTToDeltaExtension,
type HtmlASTToDeltaMatcher, type HtmlASTToDeltaMatcher,
HtmlASTToDeltaMatcherIdentifier, HtmlASTToDeltaMatcherIdentifier,
@@ -10,7 +13,7 @@ export {
InlineDeltaToHtmlAdapterExtension, InlineDeltaToHtmlAdapterExtension,
type InlineDeltaToHtmlAdapterMatcher, type InlineDeltaToHtmlAdapterMatcher,
InlineDeltaToHtmlAdapterMatcherIdentifier, InlineDeltaToHtmlAdapterMatcherIdentifier,
} from './html/index.js'; } from './html';
export { export {
BlockMarkdownAdapterExtension, BlockMarkdownAdapterExtension,
type BlockMarkdownAdapterMatcher, type BlockMarkdownAdapterMatcher,
@@ -23,7 +26,7 @@ export {
type MarkdownASTToDeltaMatcher, type MarkdownASTToDeltaMatcher,
MarkdownASTToDeltaMatcherIdentifier, MarkdownASTToDeltaMatcherIdentifier,
MarkdownDeltaConverter, MarkdownDeltaConverter,
} from './markdown/index.js'; } from './markdown';
export { export {
BlockNotionHtmlAdapterExtension, BlockNotionHtmlAdapterExtension,
type BlockNotionHtmlAdapterMatcher, type BlockNotionHtmlAdapterMatcher,
@@ -34,7 +37,7 @@ export {
type NotionHtmlASTToDeltaMatcher, type NotionHtmlASTToDeltaMatcher,
NotionHtmlASTToDeltaMatcherIdentifier, NotionHtmlASTToDeltaMatcherIdentifier,
NotionHtmlDeltaConverter, NotionHtmlDeltaConverter,
} from './notion-html/index.js'; } from './notion-html';
export { export {
BlockPlainTextAdapterExtension, BlockPlainTextAdapterExtension,
type BlockPlainTextAdapterMatcher, type BlockPlainTextAdapterMatcher,
@@ -43,14 +46,16 @@ export {
InlineDeltaToPlainTextAdapterMatcherIdentifier, InlineDeltaToPlainTextAdapterMatcherIdentifier,
type PlainText, type PlainText,
PlainTextDeltaConverter, PlainTextDeltaConverter,
} from './plain-text/index.js'; } from './plain-text';
export { export {
type AdapterContext, type AdapterContext,
type AdapterFactory,
AdapterFactoryIdentifier,
type BlockAdapterMatcher, type BlockAdapterMatcher,
DeltaASTConverter, DeltaASTConverter,
type HtmlAST, type HtmlAST,
type InlineHtmlAST, type InlineHtmlAST,
isBlockSnapshotNode, isBlockSnapshotNode,
type TextBuffer, type TextBuffer,
} from './types/index.js'; } from './types';
export * from './utils/index.js'; export * from './utils';

View File

@@ -1,8 +1,10 @@
import { createIdentifier } from '@blocksuite/global/di';
import type { BaseTextAttributes, DeltaInsert } from '@blocksuite/inline'; import type { BaseTextAttributes, DeltaInsert } from '@blocksuite/inline';
import { import {
type AssetsManager, type AssetsManager,
type ASTWalker, type ASTWalker,
type ASTWalkerContext, type ASTWalkerContext,
type BaseAdapter,
type BlockSnapshot, type BlockSnapshot,
BlockSnapshotSchema, BlockSnapshotSchema,
type Job, type Job,
@@ -168,3 +170,11 @@ export type ASTToDeltaMatcher<AST> = {
} }
) => DeltaInsert<AffineTextAttributes>[]; ) => DeltaInsert<AffineTextAttributes>[];
}; };
export type AdapterFactory = {
// TODO(@chen): Make it return the specific adapter type
get: (job: Job) => BaseAdapter;
};
export const AdapterFactoryIdentifier =
createIdentifier<AdapterFactory>('AdapterFactory');

View File

@@ -2,6 +2,7 @@ import {
DEFAULT_NOTE_BACKGROUND_COLOR, DEFAULT_NOTE_BACKGROUND_COLOR,
NoteDisplayMode, NoteDisplayMode,
} from '@blocksuite/affine-model'; } from '@blocksuite/affine-model';
import { HtmlAdapter } from '@blocksuite/affine-shared/adapters';
import { Container } from '@blocksuite/global/di'; import { Container } from '@blocksuite/global/di';
import type { import type {
BlockSnapshot, BlockSnapshot,
@@ -14,7 +15,6 @@ import { describe, expect, test } from 'vitest';
import { defaultBlockHtmlAdapterMatchers } from '../../_common/adapters/html/block-matcher.js'; import { defaultBlockHtmlAdapterMatchers } from '../../_common/adapters/html/block-matcher.js';
import { htmlInlineToDeltaMatchers } from '../../_common/adapters/html/delta-converter/html-inline.js'; import { htmlInlineToDeltaMatchers } from '../../_common/adapters/html/delta-converter/html-inline.js';
import { inlineDeltaToHtmlAdapterMatchers } from '../../_common/adapters/html/delta-converter/inline-delta.js'; import { inlineDeltaToHtmlAdapterMatchers } from '../../_common/adapters/html/delta-converter/inline-delta.js';
import { HtmlAdapter } from '../../_common/adapters/html/html.js';
import { nanoidReplacement } from '../../_common/test-utils/test-utils.js'; import { nanoidReplacement } from '../../_common/test-utils/test-utils.js';
import { embedSyncedDocMiddleware } from '../../_common/transformers/middlewares.js'; import { embedSyncedDocMiddleware } from '../../_common/transformers/middlewares.js';
import { createJob } from '../utils/create-job.js'; import { createJob } from '../utils/create-job.js';

View File

@@ -1,3 +1,4 @@
import { AdapterFactoryIdentifier } from '@blocksuite/affine-shared/adapters';
import type { ExtensionType } from '@blocksuite/block-std'; import type { ExtensionType } from '@blocksuite/block-std';
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions'; import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { sha } from '@blocksuite/global/utils'; import { sha } from '@blocksuite/global/utils';
@@ -19,8 +20,6 @@ import {
type ToDocSnapshotPayload, type ToDocSnapshotPayload,
} from '@blocksuite/store'; } from '@blocksuite/store';
import { AdapterFactoryIdentifier } from './type.js';
export type Attachment = File[]; export type Attachment = File[];
type AttachmentToSliceSnapshotPayload = { type AttachmentToSliceSnapshotPayload = {

View File

@@ -1,9 +1,9 @@
import { HtmlAdapterFactoryExtension } from '@blocksuite/affine-shared/adapters';
import type { ExtensionType } from '@blocksuite/block-std'; import type { ExtensionType } from '@blocksuite/block-std';
import { AttachmentAdapterFactoryExtension } from './attachment.js'; import { AttachmentAdapterFactoryExtension } from './attachment.js';
import { htmlInlineToDeltaMatchers } from './html/delta-converter/html-inline.js'; import { htmlInlineToDeltaMatchers } from './html/delta-converter/html-inline.js';
import { inlineDeltaToHtmlAdapterMatchers } from './html/delta-converter/inline-delta.js'; import { inlineDeltaToHtmlAdapterMatchers } from './html/delta-converter/inline-delta.js';
import { HtmlAdapterFactoryExtension } from './html/html.js';
import { ImageAdapterFactoryExtension } from './image.js'; import { ImageAdapterFactoryExtension } from './image.js';
import { MarkdownAdapterFactoryExtension } from './markdown/markdown.js'; import { MarkdownAdapterFactoryExtension } from './markdown/markdown.js';
import { MixTextAdapterFactoryExtension } from './mix-text.js'; import { MixTextAdapterFactoryExtension } from './mix-text.js';

View File

@@ -1,6 +1 @@
export { defaultBlockHtmlAdapterMatchers } from './block-matcher.js'; export { defaultBlockHtmlAdapterMatchers } from './block-matcher.js';
export {
HtmlAdapter,
HtmlAdapterFactoryExtension,
HtmlAdapterFactoryIdentifier,
} from './html.js';

View File

@@ -1,3 +1,4 @@
import { AdapterFactoryIdentifier } from '@blocksuite/affine-shared/adapters';
import type { ExtensionType } from '@blocksuite/block-std'; import type { ExtensionType } from '@blocksuite/block-std';
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions'; import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { sha } from '@blocksuite/global/utils'; import { sha } from '@blocksuite/global/utils';
@@ -19,8 +20,6 @@ import {
type ToDocSnapshotPayload, type ToDocSnapshotPayload,
} from '@blocksuite/store'; } from '@blocksuite/store';
import { AdapterFactoryIdentifier } from './type.js';
export type Image = File[]; export type Image = File[];
type ImageToSliceSnapshotPayload = { type ImageToSliceSnapshotPayload = {

View File

@@ -1,6 +1,5 @@
export * from './attachment.js'; export * from './attachment.js';
export * from './extension.js'; export * from './extension.js';
export * from './html/html.js';
export * from './image.js'; export * from './image.js';
export * from './markdown/index.js'; export * from './markdown/index.js';
export * from './mix-text.js'; export * from './mix-text.js';

View File

@@ -4,6 +4,7 @@ import {
} from '@blocksuite/affine-model'; } from '@blocksuite/affine-model';
import { import {
type AdapterContext, type AdapterContext,
AdapterFactoryIdentifier,
type BlockMarkdownAdapterMatcher, type BlockMarkdownAdapterMatcher,
BlockMarkdownAdapterMatcherIdentifier, BlockMarkdownAdapterMatcherIdentifier,
type Markdown, type Markdown,
@@ -36,7 +37,6 @@ import remarkParse from 'remark-parse';
import remarkStringify from 'remark-stringify'; import remarkStringify from 'remark-stringify';
import { unified } from 'unified'; import { unified } from 'unified';
import { AdapterFactoryIdentifier } from '../type.js';
import { defaultBlockMarkdownAdapterMatchers } from './block-matcher.js'; import { defaultBlockMarkdownAdapterMatchers } from './block-matcher.js';
import { inlineDeltaToMarkdownAdapterMatchers } from './delta-converter/inline-delta.js'; import { inlineDeltaToMarkdownAdapterMatchers } from './delta-converter/inline-delta.js';
import { markdownInlineToDeltaMatchers } from './delta-converter/markdown-inline.js'; import { markdownInlineToDeltaMatchers } from './delta-converter/markdown-inline.js';

View File

@@ -2,6 +2,7 @@ import {
DEFAULT_NOTE_BACKGROUND_COLOR, DEFAULT_NOTE_BACKGROUND_COLOR,
NoteDisplayMode, NoteDisplayMode,
} from '@blocksuite/affine-model'; } from '@blocksuite/affine-model';
import { AdapterFactoryIdentifier } from '@blocksuite/affine-shared/adapters';
import type { ExtensionType } from '@blocksuite/block-std'; import type { ExtensionType } from '@blocksuite/block-std';
import type { DeltaInsert } from '@blocksuite/inline'; import type { DeltaInsert } from '@blocksuite/inline';
import { import {
@@ -25,7 +26,6 @@ import {
} from '@blocksuite/store'; } from '@blocksuite/store';
import { MarkdownAdapter } from './markdown/index.js'; import { MarkdownAdapter } from './markdown/index.js';
import { AdapterFactoryIdentifier } from './type.js';
export type MixText = string; export type MixText = string;

View File

@@ -4,6 +4,7 @@ import {
} from '@blocksuite/affine-model'; } from '@blocksuite/affine-model';
import { import {
type AdapterContext, type AdapterContext,
AdapterFactoryIdentifier,
type BlockNotionHtmlAdapterMatcher, type BlockNotionHtmlAdapterMatcher,
BlockNotionHtmlAdapterMatcherIdentifier, BlockNotionHtmlAdapterMatcherIdentifier,
HastUtils, HastUtils,
@@ -34,8 +35,6 @@ import {
import rehypeParse from 'rehype-parse'; import rehypeParse from 'rehype-parse';
import { unified } from 'unified'; import { unified } from 'unified';
import { AdapterFactoryIdentifier } from '../type.js';
type NotionHtmlToSliceSnapshotPayload = { type NotionHtmlToSliceSnapshotPayload = {
file: NotionHtml; file: NotionHtml;
assets?: AssetsManager; assets?: AssetsManager;

View File

@@ -1,4 +1,5 @@
import { DEFAULT_NOTE_BACKGROUND_COLOR } from '@blocksuite/affine-model'; import { DEFAULT_NOTE_BACKGROUND_COLOR } from '@blocksuite/affine-model';
import { AdapterFactoryIdentifier } from '@blocksuite/affine-shared/adapters';
import type { AffineTextAttributes } from '@blocksuite/affine-shared/types'; import type { AffineTextAttributes } from '@blocksuite/affine-shared/types';
import type { ExtensionType } from '@blocksuite/block-std'; import type { ExtensionType } from '@blocksuite/block-std';
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions'; import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
@@ -16,8 +17,6 @@ import {
type SliceSnapshot, type SliceSnapshot,
} from '@blocksuite/store'; } from '@blocksuite/store';
import { AdapterFactoryIdentifier } from './type.js';
type NotionEditingStyle = { type NotionEditingStyle = {
0: string; 0: string;
}; };

View File

@@ -4,6 +4,7 @@ import {
} from '@blocksuite/affine-model'; } from '@blocksuite/affine-model';
import { import {
type AdapterContext, type AdapterContext,
AdapterFactoryIdentifier,
type BlockPlainTextAdapterMatcher, type BlockPlainTextAdapterMatcher,
BlockPlainTextAdapterMatcherIdentifier, BlockPlainTextAdapterMatcherIdentifier,
type PlainText, type PlainText,
@@ -31,7 +32,6 @@ import {
type ToDocSnapshotPayload, type ToDocSnapshotPayload,
} from '@blocksuite/store'; } from '@blocksuite/store';
import { AdapterFactoryIdentifier } from '../type.js';
import { defaultBlockPlainTextAdapterMatchers } from './block-matcher.js'; import { defaultBlockPlainTextAdapterMatchers } from './block-matcher.js';
import { inlineDeltaToPlainTextAdapterMatchers } from './delta-converter/inline-delta.js'; import { inlineDeltaToPlainTextAdapterMatchers } from './delta-converter/inline-delta.js';

View File

@@ -1,10 +0,0 @@
import { createIdentifier } from '@blocksuite/global/di';
import type { BaseAdapter, Job } from '@blocksuite/store';
export type AdapterFactory = {
// TODO(@chen): Make it return the specific adapter type
get: (job: Job) => BaseAdapter;
};
export const AdapterFactoryIdentifier =
createIdentifier<AdapterFactory>('AdapterFactory');

View File

@@ -7,12 +7,8 @@ import {
import { EmbedOptionProvider } from '@blocksuite/affine-shared/services'; import { EmbedOptionProvider } from '@blocksuite/affine-shared/services';
import type { EditorHost } from '@blocksuite/block-std'; import type { EditorHost } from '@blocksuite/block-std';
import { ShadowlessElement } from '@blocksuite/block-std'; import { ShadowlessElement } from '@blocksuite/block-std';
import { import { GfxControllerIdentifier } from '@blocksuite/block-std/gfx';
assertExists, import { Bound, Vec, WithDisposable } from '@blocksuite/global/utils';
Bound,
Vec,
WithDisposable,
} from '@blocksuite/global/utils';
import type { BlockModel } from '@blocksuite/store'; import type { BlockModel } from '@blocksuite/store';
import { html } from 'lit'; import { html } from 'lit';
import { property, query, state } from 'lit/decorators.js'; import { property, query, state } from 'lit/decorators.js';
@@ -70,10 +66,19 @@ export class EmbedCardCreateModal extends WithDisposable(ShadowlessElement) {
const edgelessRoot = getRootByEditorHost( const edgelessRoot = getRootByEditorHost(
this.host this.host
) as EdgelessRootBlockComponent | null; ) as EdgelessRootBlockComponent | null;
assertExists(edgelessRoot); if (!edgelessRoot) {
return;
}
const surface = edgelessRoot.surface; const gfx = this.host.std.get(GfxControllerIdentifier);
const center = Vec.toVec(surface.renderer.viewport.center);
const viewport = gfx.viewport;
const surfaceModel = gfx.surface;
if (!surfaceModel) {
return;
}
const center = Vec.toVec(viewport.center);
edgelessRoot.service.addBlock( edgelessRoot.service.addBlock(
flavour, flavour,
{ {
@@ -85,10 +90,10 @@ export class EmbedCardCreateModal extends WithDisposable(ShadowlessElement) {
).serialize(), ).serialize(),
style: targetStyle, style: targetStyle,
}, },
surface.model surfaceModel
); );
edgelessRoot.gfx.tool.setTool('default'); gfx.tool.setTool('default');
} }
this.onConfirm(); this.onConfirm();
this.remove(); this.remove();

View File

@@ -1,3 +1,4 @@
import { HtmlAdapter } from '@blocksuite/affine-shared/adapters';
import { Container } from '@blocksuite/global/di'; import { Container } from '@blocksuite/global/di';
import { sha } from '@blocksuite/global/utils'; import { sha } from '@blocksuite/global/utils';
import type { Doc, DocCollection } from '@blocksuite/store'; import type { Doc, DocCollection } from '@blocksuite/store';
@@ -6,7 +7,6 @@ import { extMimeMap, Job } from '@blocksuite/store';
import { defaultBlockHtmlAdapterMatchers } from '../adapters/html/block-matcher.js'; import { defaultBlockHtmlAdapterMatchers } from '../adapters/html/block-matcher.js';
import { htmlInlineToDeltaMatchers } from '../adapters/html/delta-converter/html-inline.js'; import { htmlInlineToDeltaMatchers } from '../adapters/html/delta-converter/html-inline.js';
import { inlineDeltaToHtmlAdapterMatchers } from '../adapters/html/delta-converter/inline-delta.js'; import { inlineDeltaToHtmlAdapterMatchers } from '../adapters/html/delta-converter/inline-delta.js';
import { HtmlAdapter } from '../adapters/html/html.js';
import { import {
defaultImageProxyMiddleware, defaultImageProxyMiddleware,
docLinkBaseURLMiddleware, docLinkBaseURLMiddleware,

View File

@@ -1,3 +1,4 @@
import { HtmlAdapter } from '@blocksuite/affine-shared/adapters';
import { import {
type BlockComponent, type BlockComponent,
Clipboard, Clipboard,
@@ -5,7 +6,7 @@ import {
} from '@blocksuite/block-std'; } from '@blocksuite/block-std';
import { assertExists, DisposableGroup } from '@blocksuite/global/utils'; import { assertExists, DisposableGroup } from '@blocksuite/global/utils';
import { HtmlAdapter, PlainTextAdapter } from '../../_common/adapters/index.js'; import { PlainTextAdapter } from '../../_common/adapters/index.js';
import { pasteMiddleware } from '../../root-block/clipboard/middlewares/index.js'; import { pasteMiddleware } from '../../root-block/clipboard/middlewares/index.js';
export class CodeClipboardController { export class CodeClipboardController {

View File

@@ -99,6 +99,11 @@ export {
Tooltip, Tooltip,
} from '@blocksuite/affine-components/toolbar'; } from '@blocksuite/affine-components/toolbar';
export * from '@blocksuite/affine-model'; export * from '@blocksuite/affine-model';
export {
HtmlAdapter,
HtmlAdapterFactoryExtension,
HtmlAdapterFactoryIdentifier,
} from '@blocksuite/affine-shared/adapters';
export * from '@blocksuite/affine-shared/services'; export * from '@blocksuite/affine-shared/services';
export { scrollbarStyle } from '@blocksuite/affine-shared/styles'; export { scrollbarStyle } from '@blocksuite/affine-shared/styles';
export { export {

View File

@@ -1,10 +1,10 @@
import { HtmlAdapter } from '@blocksuite/affine-shared/adapters';
import type { BlockComponent, UIEventHandler } from '@blocksuite/block-std'; import type { BlockComponent, UIEventHandler } from '@blocksuite/block-std';
import { DisposableGroup } from '@blocksuite/global/utils'; import { DisposableGroup } from '@blocksuite/global/utils';
import type { BlockSnapshot, Doc } from '@blocksuite/store'; import type { BlockSnapshot, Doc } from '@blocksuite/store';
import { import {
AttachmentAdapter, AttachmentAdapter,
HtmlAdapter,
ImageAdapter, ImageAdapter,
MixTextAdapter, MixTextAdapter,
NotionTextAdapter, NotionTextAdapter,

View File

@@ -3472,6 +3472,9 @@ __metadata:
lodash.clonedeep: "npm:^4.5.0" lodash.clonedeep: "npm:^4.5.0"
lodash.mergewith: "npm:^4.6.2" lodash.mergewith: "npm:^4.6.2"
minimatch: "npm:^10.0.1" minimatch: "npm:^10.0.1"
rehype-parse: "npm:^9.0.0"
rehype-stringify: "npm:^10.0.0"
unified: "npm:^11.0.5"
zod: "npm:^3.23.8" zod: "npm:^3.23.8"
languageName: unknown languageName: unknown
linkType: soft linkType: soft