mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-15 05:37:32 +00:00
fix(editor): make std stable in affine-link and affine-reference (#10207)
This commit is contained in:
@@ -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({
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user