-
-
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.
feat(blocks): support embed linked doc and synced doc html block adap…
…ter (#8907) [BS-2063](https://linear.app/affine-design/issue/BS-2063/补齐-embed-linked-and-synced-doc-html-block-adapter)
- Loading branch information
1 parent
aaf7756
commit 0c26cad
Showing
16 changed files
with
719 additions
and
44 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,14 @@ | ||
import type { ReferenceParams } from '@blocksuite/affine-model'; | ||
|
||
import { toURLSearchParams } from '@blocksuite/affine-shared/adapters'; | ||
|
||
export function generateDocUrl( | ||
docBaseUrl: string, | ||
pageId: string, | ||
params: ReferenceParams | ||
) { | ||
const search = toURLSearchParams(params); | ||
const query = search?.size ? `?${search.toString()}` : ''; | ||
const url = docBaseUrl ? `${docBaseUrl}/${pageId}${query}` : ''; | ||
return url; | ||
} |
64 changes: 64 additions & 0 deletions
64
packages/affine/block-embed/src/embed-linked-doc-block/adapters/html.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,64 @@ | ||
import { EmbedLinkedDocBlockSchema } from '@blocksuite/affine-model'; | ||
import { | ||
BlockHtmlAdapterExtension, | ||
type BlockHtmlAdapterMatcher, | ||
} from '@blocksuite/affine-shared/adapters'; | ||
|
||
import { generateDocUrl } from '../../common/adapters/utils.js'; | ||
|
||
export const embedLinkedDocBlockHtmlAdapterMatcher: BlockHtmlAdapterMatcher = { | ||
flavour: EmbedLinkedDocBlockSchema.model.flavour, | ||
toMatch: () => false, | ||
fromMatch: o => o.node.flavour === EmbedLinkedDocBlockSchema.model.flavour, | ||
toBlockSnapshot: {}, | ||
fromBlockSnapshot: { | ||
enter: (o, context) => { | ||
const { configs, walkerContext } = context; | ||
// Parse as link | ||
if (!o.node.props.pageId) { | ||
return; | ||
} | ||
const title = configs.get('title:' + o.node.props.pageId) ?? 'untitled'; | ||
const url = generateDocUrl( | ||
configs.get('docLinkBaseUrl') ?? '', | ||
String(o.node.props.pageId), | ||
o.node.props.params ?? Object.create(null) | ||
); | ||
|
||
walkerContext | ||
.openNode( | ||
{ | ||
type: 'element', | ||
tagName: 'div', | ||
properties: { | ||
className: ['affine-paragraph-block-container'], | ||
}, | ||
children: [], | ||
}, | ||
'children' | ||
) | ||
.openNode( | ||
{ | ||
type: 'element', | ||
tagName: 'a', | ||
properties: { | ||
href: url, | ||
}, | ||
children: [ | ||
{ | ||
type: 'text', | ||
value: title, | ||
}, | ||
], | ||
}, | ||
'children' | ||
) | ||
.closeNode() | ||
.closeNode(); | ||
}, | ||
}, | ||
}; | ||
|
||
export const EmbedLinkedDocHtmlAdapterExtension = BlockHtmlAdapterExtension( | ||
embedLinkedDocBlockHtmlAdapterMatcher | ||
); |
1 change: 1 addition & 0 deletions
1
packages/affine/block-embed/src/embed-linked-doc-block/adapters/index.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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './html.js'; | ||
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
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
88 changes: 88 additions & 0 deletions
88
packages/affine/block-embed/src/embed-synced-doc-block/adapters/html.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,88 @@ | ||
import { EmbedSyncedDocBlockSchema } from '@blocksuite/affine-model'; | ||
import { | ||
BlockHtmlAdapterExtension, | ||
type BlockHtmlAdapterMatcher, | ||
} from '@blocksuite/affine-shared/adapters'; | ||
|
||
export const embedSyncedDocBlockHtmlAdapterMatcher: BlockHtmlAdapterMatcher = { | ||
flavour: EmbedSyncedDocBlockSchema.model.flavour, | ||
toMatch: () => false, | ||
fromMatch: o => o.node.flavour === EmbedSyncedDocBlockSchema.model.flavour, | ||
toBlockSnapshot: {}, | ||
fromBlockSnapshot: { | ||
enter: async (o, context) => { | ||
const { configs, walker, walkerContext, job } = context; | ||
const type = configs.get('embedSyncedDocExportType'); | ||
|
||
// this context is used for nested sync block | ||
if ( | ||
walkerContext.getGlobalContext('embed-synced-doc-counter') === undefined | ||
) { | ||
walkerContext.setGlobalContext('embed-synced-doc-counter', 0); | ||
} | ||
let counter = walkerContext.getGlobalContext( | ||
'embed-synced-doc-counter' | ||
) as number; | ||
walkerContext.setGlobalContext('embed-synced-doc-counter', ++counter); | ||
|
||
if (type === 'content') { | ||
const syncedDocId = o.node.props.pageId as string; | ||
const syncedDoc = job.collection.getDoc(syncedDocId); | ||
walkerContext.setGlobalContext('hast:html-root-doc', false); | ||
if (!syncedDoc) return; | ||
|
||
if (counter === 1) { | ||
const syncedSnapshot = await job.docToSnapshot(syncedDoc); | ||
if (syncedSnapshot) { | ||
await walker.walkONode(syncedSnapshot.blocks); | ||
} | ||
} else { | ||
walkerContext | ||
.openNode( | ||
{ | ||
type: 'element', | ||
tagName: 'div', | ||
properties: { | ||
className: ['affine-paragraph-block-container'], | ||
}, | ||
children: [], | ||
}, | ||
'children' | ||
) | ||
.openNode( | ||
{ | ||
type: 'element', | ||
tagName: 'p', | ||
properties: {}, | ||
children: [ | ||
{ type: 'text', value: syncedDoc.meta?.title ?? '' }, | ||
], | ||
}, | ||
'children' | ||
) | ||
.closeNode() | ||
.closeNode(); | ||
} | ||
} | ||
}, | ||
leave: (_, context) => { | ||
const { walkerContext } = context; | ||
const counter = walkerContext.getGlobalContext( | ||
'embed-synced-doc-counter' | ||
) as number; | ||
const currentCounter = counter - 1; | ||
walkerContext.setGlobalContext( | ||
'embed-synced-doc-counter', | ||
currentCounter | ||
); | ||
// When leave the last embed synced doc block, we need to set the html root doc context to true | ||
walkerContext.setGlobalContext( | ||
'hast:html-root-doc', | ||
currentCounter === 0 | ||
); | ||
}, | ||
}, | ||
}; | ||
|
||
export const EmbedSyncedDocBlockHtmlAdapterExtension = | ||
BlockHtmlAdapterExtension(embedSyncedDocBlockHtmlAdapterMatcher); |
1 change: 1 addition & 0 deletions
1
packages/affine/block-embed/src/embed-synced-doc-block/adapters/index.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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './html.js'; | ||
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
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.