Skip to content

Commit

Permalink
fix: no need to save aliases on embed synced doc model (#8884)
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Dec 9, 2024
1 parent 9996ae0 commit 4d98a86
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cloneReferenceInfoWithoutAliases } from '@blocksuite/affine-components/rich-text';
import {
EMBED_CARD_HEIGHT,
EMBED_CARD_WIDTH,
Expand Down Expand Up @@ -35,7 +36,11 @@ export class EmbedEdgelessLinkedDocBlockComponent extends toEdgelessEmbedBlock(
// @ts-expect-error TODO: fix after edgeless refactor
const newId = edgelessService.addBlock(
'affine:embed-synced-doc',
{ xywh: bound.serialize(), caption, ...this.referenceInfo },
{
xywh: bound.serialize(),
caption,
...cloneReferenceInfoWithoutAliases(this.referenceInfo),
},
// @ts-expect-error TODO: fix after edgeless refactor
edgelessService.surface
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { BlockLinkIcon } from '@blocksuite/affine-components/icons';
import { isPeekable, Peekable } from '@blocksuite/affine-components/peek';
import {
cloneReferenceInfo,
cloneReferenceInfoWithoutAliases,
REFERENCE_NODE,
referenceToNode,
RefNodeSlotsProvider,
Expand Down Expand Up @@ -141,7 +142,7 @@ export class EmbedLinkedDocBlockComponent extends EmbedBlockComponent<EmbedLinke

doc.addBlock(
'affine:embed-synced-doc',
{ caption, ...this.referenceInfo },
{ caption, ...cloneReferenceInfoWithoutAliases(this.referenceInfo) },
parent,
index
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RefNodeSlotsProvider,
} from '@blocksuite/affine-components/rich-text';
import {
type AliasInfo,
type DocMode,
type EmbedSyncedDocModel,
NoteDisplayMode,
Expand Down Expand Up @@ -254,7 +255,7 @@ export class EmbedSyncedDocBlockComponent extends EmbedBlockComponent<EmbedSynce
width: '100%',
});

convertToCard = () => {
convertToCard = (aliasInfo?: AliasInfo) => {
const { doc, caption } = this.model;

const parent = doc.getParent(this.model);
Expand All @@ -263,7 +264,7 @@ export class EmbedSyncedDocBlockComponent extends EmbedBlockComponent<EmbedSynce

doc.addBlock(
'affine:embed-linked-doc',
{ caption, ...this.referenceInfo },
{ caption, ...this.referenceInfo, ...aliasInfo },
parent,
index
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ export * from './reference-node/reference-config.js';

export { AffineReference } from './reference-node/reference-node.js';
export type { RefNodeSlots } from './reference-node/types.js';
export { cloneReferenceInfo, referenceToNode } from './reference-node/utils.js';
export {
cloneReferenceInfo,
cloneReferenceInfoWithoutAliases,
referenceToNode,
} from './reference-node/utils.js';
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
import { RefNodeSlotsProvider } from '../../../../extension/index.js';
import { ReferenceAliasPopup } from './reference-alias-popup.js';
import { styles } from './styles.js';
import { cloneReferenceInfoWithoutAliases } from './utils.js';

export class ReferencePopup extends WithDisposable(LitElement) {
static override styles = styles;
Expand Down Expand Up @@ -123,8 +124,14 @@ export class ReferencePopup extends WithDisposable(LitElement) {

const index = parent.children.indexOf(block.model);
const referenceInfo = this.referenceInfo;
const hasTitleAlias = Boolean(referenceInfo.title);

doc.addBlock('affine:embed-synced-doc', referenceInfo, parent, index + 1);
doc.addBlock(
'affine:embed-synced-doc',
cloneReferenceInfoWithoutAliases(referenceInfo),
parent,
index + 1
);

const totalTextLength = this.inlineEditor.yTextLength;
const inlineTextLength = this.targetInlineRange.length;
Expand All @@ -134,7 +141,9 @@ export class ReferencePopup extends WithDisposable(LitElement) {
this.inlineEditor.insertText(this.targetInlineRange, this.docTitle);
}

if (referenceInfo.title) notifyLinkedDocSwitchedToEmbed(std);
if (hasTitleAlias) {
notifyLinkedDocSwitchedToEmbed(std);
}

this.abortController.abort();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,16 @@ export function referenceToNode({ params }: ReferenceInfo) {
if (databaseId || databaseRowId) return true;
return false;
}

/**
* Clones reference info without the aliases.
* In `EmbedSyncedDocModel`, the aliases are not needed at the moment.
*/
export function cloneReferenceInfoWithoutAliases({
pageId,
params,
}: ReferenceInfo) {
const info: ReferenceInfo = { pageId };
if (params) info.params = cloneDeep(params);
return info;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { AliasInfo } from '@blocksuite/affine-model';
import type { BlockComponent, EditorHost } from '@blocksuite/block-std';
import type { BlockProps } from '@blocksuite/store';

import {
EmbedLinkedDocBlockComponent,
Expand Down Expand Up @@ -178,11 +177,9 @@ export class EmbedCardEditModal extends SignalWatcher(

const description = this.description$.value.trim();

const props: Partial<BlockProps> = { title };
const props: AliasInfo = { title };
if (description) props.description = description;

this.model.doc.updateBlock(this.model, props);

const blockComponent = this._blockComponent;

if (
Expand All @@ -191,10 +188,11 @@ export class EmbedCardEditModal extends SignalWatcher(
) {
const std = blockComponent.std;

blockComponent.convertToCard();
blockComponent.convertToCard(props);

notifyLinkedDocSwitchedToCard(std);
} else {
this.model.doc.updateBlock(this.model, props);
blockComponent?.requestUpdate();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,9 @@ export class EdgelessChangeEmbedCardButton extends WithDisposable(LitElement) {

if (doc) {
const title = doc.meta?.title;
const description = getDocContentWithMaxLength(doc);
const description = isEmbedLinkedDocBlock(model)
? getDocContentWithMaxLength(doc)
: undefined;
return { title, description };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ export class EmbedCardToolbar extends WidgetComponent<

if (doc) {
const title = doc.meta?.title;
const description = getDocContentWithMaxLength(doc);
const description = isEmbedLinkedDocBlock(model)
? getDocContentWithMaxLength(doc)
: undefined;
return { title, description };
}

Expand Down Expand Up @@ -331,8 +333,9 @@ export class EmbedCardToolbar extends WidgetComponent<

this.focusBlock.convertToEmbed();

if (referenceInfo.title || referenceInfo.description)
if (referenceInfo.title || referenceInfo.description) {
notifyLinkedDocSwitchedToEmbed(this.std);
}

return;
}
Expand Down

0 comments on commit 4d98a86

Please sign in to comment.