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