Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

fix(editor): make std stable in affine-link and affine-reference (#10207)

fundon34472fdfa964
diff --git a/blocksuite/affine/components/src/rich-text/inline/presets/affine-inline-specs.ts b/blocksuite/affine/components/src/rich-text/inline/presets/affine-inline-specs.ts
index fe957b0638..e94a9d1184 100644
--- a/blocksuite/affine/components/src/rich-text/inline/presets/affine-inline-specs.ts
+++ b/blocksuite/affine/components/src/rich-text/inline/presets/affine-inline-specs.ts
@@ -149,6 +149,7 @@ export const ReferenceInlineSpecExtension = InlineSpecExtension(
},
renderer: ({ delta, selected }) => {
return html`<affine-reference
+ .std=${std}
.delta=${delta}
.selected=${selected}
.config=${configProvider}
@@ -159,15 +160,18 @@ export const ReferenceInlineSpecExtension = InlineSpecExtension(
}
);
-export const LinkInlineSpecExtension = InlineSpecExtension({
- name: 'link',
- schema: z.string().optional().nullable().catch(undefined),
- match: delta => {
- return !!delta.attributes?.link;
- },
- renderer: ({ delta }) => {
- return html`<affine-link .delta=${delta}></affine-link>`;
- },
+export const LinkInlineSpecExtension = InlineSpecExtension('link', provider => {
+ const std = provider.get(StdIdentifier);
+ return {
+ name: 'link',
+ schema: z.string().optional().nullable().catch(undefined),
+ match: delta => {
+ return !!delta.attributes?.link;
+ },
+ renderer: ({ delta }) => {
+ return html`<affine-link .std=${std} .delta=${delta}></affine-link>`;
+ },
+ };
});
export const LatexEditorUnitSpecExtension = InlineSpecExtension({
diff --git a/blocksuite/affine/components/src/rich-text/inline/presets/nodes/link-node/affine-link.ts b/blocksuite/affine/components/src/rich-text/inline/presets/nodes/link-node/affine-link.ts
index aa0e6306b7..76e8af258d 100644
--- a/blocksuite/affine/components/src/rich-text/inline/presets/nodes/link-node/affine-link.ts
+++ b/blocksuite/affine/components/src/rich-text/inline/presets/nodes/link-node/affine-link.ts
@@ -1,7 +1,7 @@
import type { ReferenceInfo } from '@blocksuite/affine-model';
import { ParseDocUrlProvider } from '@blocksuite/affine-shared/services';
import type { AffineTextAttributes } from '@blocksuite/affine-shared/types';
-import type { BlockComponent } from '@blocksuite/block-std';
+import type { BlockComponent, BlockStdScope } from '@blocksuite/block-std';
import {
BLOCK_ID_ATTR,
BlockSelection,
@@ -55,8 +55,8 @@ export class AffineLink extends ShadowlessElement {
const referenceInfo = this._referenceInfo;
if (!referenceInfo) return;
- const refNodeSlotsProvider = this.std?.getOptional(RefNodeSlotsProvider);
- if (!refNodeSlotsProvider || !this.std) return;
+ const refNodeSlotsProvider = this.std.getOptional(RefNodeSlotsProvider);
+ if (!refNodeSlotsProvider) return;
e?.preventDefault();
@@ -76,7 +76,7 @@ export class AffineLink extends ShadowlessElement {
return null;
}
- const selection = this.std?.selection;
+ const selection = this.std.selection;
const textSelection = selection?.find(TextSelection);
if (!!textSelection && !textSelection.isCollapsed()) {
return null;
@@ -134,19 +134,12 @@ export class AffineLink extends ShadowlessElement {
return selfInlineRange;
}
- get std() {
- const std = this.block?.std;
- return std;
- }
-
// Identify if url is an internal link
private _identify() {
const link = this.link;
if (!link) return;
- const result = this.std
- ?.getOptional(ParseDocUrlProvider)
- ?.parseDocUrl(link);
+ const result = this.std.getOptional(ParseDocUrlProvider)?.parseDocUrl(link);
if (!result) return;
const { docId: pageId, ...params } = result;
@@ -193,4 +186,7 @@ export class AffineLink extends ShadowlessElement {
accessor delta: DeltaInsert<AffineTextAttributes> = {
insert: ZERO_WIDTH_SPACE,
};
+
+ @property({ attribute: false })
+ accessor std!: BlockStdScope;
}
diff --git a/blocksuite/affine/components/src/rich-text/inline/presets/nodes/reference-node/reference-node.ts b/blocksuite/affine/components/src/rich-text/inline/presets/nodes/reference-node/reference-node.ts
index ddf452b715..5aad9b60c0 100644
--- a/blocksuite/affine/components/src/rich-text/inline/presets/nodes/reference-node/reference-node.ts
+++ b/blocksuite/affine/components/src/rich-text/inline/presets/nodes/reference-node/reference-node.ts
@@ -13,10 +13,10 @@ import {
BLOCK_ID_ATTR,
type BlockComponent,
BlockSelection,
+ type BlockStdScope,
ShadowlessElement,
TextSelection,
} from '@blocksuite/block-std';
-import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { WithDisposable } from '@blocksuite/global/utils';
import { LinkedPageIcon } from '@blocksuite/icons/lit';
import {
@@ -106,7 +106,7 @@ export class AffineReference extends WithDisposable(ShadowlessElement) {
return null;
}
- const selection = this.std?.selection;
+ const selection = this.std.selection;
if (!selection) {
return null;
}
@@ -137,16 +137,16 @@ export class AffineReference extends WithDisposable(ShadowlessElement) {
get _icon() {
const { pageId, params, title } = this.referenceInfo;
- return this.block?.std
- ?.get(DocDisplayMetaProvider)
+ return this.std
+ .get(DocDisplayMetaProvider)
.icon(pageId, { params, title, referenced: true }).value;
}
get _title() {
const { pageId, params, title } = this.referenceInfo;
return (
- this.block?.std
- ?.get(DocDisplayMetaProvider)
+ this.std
+ .get(DocDisplayMetaProvider)
.title(pageId, { params, title, referenced: true }).value || title
);
}
@@ -187,17 +187,6 @@ export class AffineReference extends WithDisposable(ShadowlessElement) {
return selfInlineRange;
}
- get std() {
- const std = this.block?.std;
- if (!std) {
- throw new BlockSuiteError(
- ErrorCode.ValueNotExists,
- 'std not found in reference node'
- );
- }
- return std;
- }
-
private _onClick() {
if (!this.config.interactable) return;
this.std.getOptional(RefNodeSlotsProvider)?.docLinkClicked.emit({
@@ -321,4 +310,7 @@ export class AffineReference extends WithDisposable(ShadowlessElement) {
@property({ type: Boolean })
accessor selected = false;
+
+ @property({ attribute: false })
+ accessor std!: BlockStdScope;
}