Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve open link on link popup #8462

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,6 @@ export class AffineLink extends ShadowlessElement {
// The link has been identified.
private _identified: boolean = false;

private _onClick = (e: MouseEvent) => {
if (!this._identified) {
this._identified = true;
this._identify();
}

const referenceInfo = this._referenceInfo;
if (!referenceInfo) return;

const refNodeSlotsProvider = this.std?.getOptional(RefNodeSlotsProvider);
if (!refNodeSlotsProvider) return;

e.preventDefault();

refNodeSlotsProvider.docLinkClicked.emit(referenceInfo);
};

// see https://github.com/toeverything/AFFiNE/issues/1540
private _onMouseUp = () => {
const anchorElement = this.querySelector('a');
Expand Down Expand Up @@ -87,13 +70,34 @@ export class AffineLink extends ShadowlessElement {
this.inlineEditor,
'view',
this.selfInlineRange,
abortController
abortController,
(e?: MouseEvent) => {
this.openLink(e);
abortController.abort();
}
),
};
},
{ enterDelay: 500 }
);

openLink = (e?: MouseEvent) => {
if (!this._identified) {
this._identified = true;
this._identify();
}

const referenceInfo = this._referenceInfo;
if (!referenceInfo) return;

const refNodeSlotsProvider = this.std?.getOptional(RefNodeSlotsProvider);
if (!refNodeSlotsProvider) return;

e?.preventDefault();

refNodeSlotsProvider.docLinkClicked.emit(referenceInfo);
};

// Workaround for links not working in contenteditable div
// see also https://stackoverflow.com/questions/12059211/how-to-make-clickable-anchor-in-contenteditable-div
//
Expand Down Expand Up @@ -156,7 +160,7 @@ export class AffineLink extends ShadowlessElement {
rel="noopener noreferrer"
target="_blank"
style=${styleMap(style)}
@click=${this._onClick}
@click=${this.openLink}
@mouseup=${this._onMouseUp}
><v-text .str=${this.delta.insert}></v-text
></a>`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ export class LinkPopup extends WithDisposable(LitElement) {
private _embedOptions: EmbedOptions | null = null;

private _openLink = () => {
if (this.openLink) {
this.openLink();
return;
}

let link = this.currentLink;
if (!link) return;
if (!link.match(/^[a-zA-Z]+:\/\//)) {
Expand Down Expand Up @@ -161,6 +166,7 @@ export class LinkPopup extends WithDisposable(LitElement) {
href=${this.currentLink}
rel="noopener noreferrer"
target="_blank"
@click=${(e: MouseEvent) => this.openLink?.(e)}
>
<span>${getHostName(this.currentLink)}</span>
</a>
Expand Down Expand Up @@ -619,6 +625,9 @@ export class LinkPopup extends WithDisposable(LitElement) {
@query('.mock-selection-container')
accessor mockSelectionContainer!: HTMLDivElement;

@property({ attribute: false })
accessor openLink: ((e?: MouseEvent) => void) | null = null;

@query('.affine-link-popover-container')
accessor popupContainer!: HTMLDivElement;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ export function toggleLinkPopup(
inlineEditor: AffineInlineEditor,
type: LinkPopup['type'],
targetInlineRange: InlineRange,
abortController: AbortController
abortController: AbortController,
openLink: ((e?: MouseEvent) => void) | null = null
): LinkPopup {
const popup = new LinkPopup();
popup.inlineEditor = inlineEditor;
popup.type = type;
popup.targetInlineRange = targetInlineRange;
popup.openLink = openLink;
popup.abortController = abortController;

document.body.append(popup);
Expand Down
Loading