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

feat: linked doc supports aliases #8806

Draft
wants to merge 1 commit into
base: fundon/11_27_add_generate_doc_url_service
Choose a base branch
from
Draft
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 @@ -198,9 +198,7 @@ export class EmbedLinkedDocBlockComponent extends EmbedBlockComponent<EmbedLinke
}

get docTitle() {
return this.linkedDoc?.meta?.title.length
? this.linkedDoc.meta.title
: 'Untitled';
return this.model.title || this.linkedDoc?.meta?.title || 'Untitled';
}

get editorMode() {
Expand Down Expand Up @@ -373,13 +371,15 @@ export class EmbedLinkedDocBlockComponent extends EmbedBlockComponent<EmbedLinke
? BlockLinkIcon
: LinkedDocIcon;

const title = this.docTitle;

const titleText = isError
? linkedDoc?.meta?.title || 'Untitled'
? title
: isLoading
? 'Loading...'
: isDeleted
? `Deleted doc`
: linkedDoc?.meta?.title || 'Untitled';
: title;

const showDefaultNoteContent = isError || isLoading || isDeleted || isEmpty;
const defaultNoteContent = isError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ import {
} from '@blocksuite/block-std';
import { GfxControllerIdentifier } from '@blocksuite/block-std/gfx';
import { assertExists, Bound, getCommonBound } from '@blocksuite/global/utils';
import { BlockViewType, DocCollection, type Query } from '@blocksuite/store';
import {
BlockViewType,
DocCollection,
type GetDocOptions,
type Query,
} from '@blocksuite/store';
import { html, type PropertyValues } from 'lit';
import { query, state } from 'lit/decorators.js';
import { choose } from 'lit/directives/choose.js';
Expand Down Expand Up @@ -327,9 +332,7 @@ export class EmbedSyncedDocBlockComponent extends EmbedBlockComponent<EmbedSynce
}

get docTitle() {
return this.syncedDoc?.meta?.title.length
? this.syncedDoc.meta.title
: 'Untitled';
return this.model.title || this.syncedDoc?.meta?.title || 'Untitled';
}

get docUpdatedAt() {
Expand All @@ -353,14 +356,9 @@ export class EmbedSyncedDocBlockComponent extends EmbedBlockComponent<EmbedSynce
}

get syncedDoc() {
return this.isPageMode
? this.std.collection.getDoc(this.model.pageId, {
readonly: true,
query: this._pageFilter,
})
: this.std.collection.getDoc(this.model.pageId, {
readonly: true,
});
const options: GetDocOptions = { readonly: true };
if (!this.isPageMode) options.query = this._pageFilter;
return this.std.collection.getDoc(this.model.pageId, options);
}

private _checkCycle() {
Expand Down
3 changes: 3 additions & 0 deletions packages/affine/components/src/rich-text/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { LatexEditorMenu } from './inline/presets/nodes/latex-node/latex-editor-
import { LatexEditorUnit } from './inline/presets/nodes/latex-node/latex-editor-unit.js';
import { AffineLatexNode } from './inline/presets/nodes/latex-node/latex-node.js';
import { LinkPopup } from './inline/presets/nodes/link-node/link-popup/link-popup.js';
import { ReferenceAliasPopup } from './inline/presets/nodes/reference-node/reference-alias-popup.js';
import { ReferencePopup } from './inline/presets/nodes/reference-node/reference-popup.js';
import { RichText } from './rich-text.js';

Expand All @@ -34,6 +35,7 @@ export function effects() {
customElements.define('link-popup', LinkPopup);
customElements.define('affine-link', AffineLink);
customElements.define('reference-popup', ReferencePopup);
customElements.define('reference-alias-popup', ReferenceAliasPopup);
customElements.define('affine-reference', AffineReference);
}

Expand All @@ -45,6 +47,7 @@ declare global {
'affine-text': AffineText;
'rich-text': RichText;
'reference-popup': ReferencePopup;
'reference-alias-popup': ReferenceAliasPopup;
'latex-editor-unit': LatexEditorUnit;
'latex-editor-menu': LatexEditorMenu;
'link-popup': LinkPopup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getHostName,
isValidUrl,
normalizeUrl,
stopPropagation,
} from '@blocksuite/affine-shared/utils';
import { BLOCK_ID_ATTR, type BlockComponent } from '@blocksuite/block-std';
import { WithDisposable } from '@blocksuite/global/utils';
Expand Down Expand Up @@ -174,7 +175,7 @@ export class LinkPopup extends WithDisposable(LitElement) {
<editor-icon-button
aria-label="Copy"
data-testid="copy-link"
.tooltip=${'Click to copy link'}
.tooltip=${'Copy link'}
@click=${this._copyUrl}
>
${CopyIcon}
Expand Down Expand Up @@ -528,15 +529,9 @@ export class LinkPopup extends WithDisposable(LitElement) {
protected override firstUpdated() {
if (!this.linkInput) return;

this._disposables.addFromEvent(this.linkInput, 'copy', e => {
e.stopPropagation();
});
this._disposables.addFromEvent(this.linkInput, 'cut', e => {
e.stopPropagation();
});
this._disposables.addFromEvent(this.linkInput, 'paste', e => {
e.stopPropagation();
});
this._disposables.addFromEvent(this.linkInput, 'copy', stopPropagation);
this._disposables.addFromEvent(this.linkInput, 'cut', stopPropagation);
this._disposables.addFromEvent(this.linkInput, 'paste', stopPropagation);
}

override render() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
import type { ReferenceInfo } from '@blocksuite/affine-model';
import type { DeltaInsert, InlineRange } from '@blocksuite/inline';

import { FONT_XS, PANEL_BASE } from '@blocksuite/affine-shared/styles';
import { ShadowlessElement } from '@blocksuite/block-std';
import { assertExists, WithDisposable } from '@blocksuite/global/utils';
import { computePosition, inline, offset, shift } from '@floating-ui/dom';
import { css, html } from 'lit';
import { property, query } from 'lit/decorators.js';

import type { EditorIconButton } from '../../../../../toolbar/index.js';
import type { AffineTextAttributes } from '../../../../extension/type.js';
import type { AffineInlineEditor } from '../../affine-inline-specs.js';

import { ConfirmIcon } from '../../../../../icons/text.js';

export class ReferenceAliasPopup extends WithDisposable(ShadowlessElement) {
static override styles = css`
:host {
box-sizing: border-box;
}

.overlay-mask {
position: fixed;
z-index: var(--affine-z-index-popover);
top: 0;
left: 0;
width: 100vw;
height: 100vh;
}

.alias-popup-form {
${PANEL_BASE};
position: absolute;
display: flex;
width: 320px;
gap: 8px 12px;
padding: 12px;
box-sizing: content-box;
justify-items: center;
align-items: center;
animation: affine-popover-fade-in 0.2s ease;
z-index: var(--affine-z-index-popover);
}

@keyframes affine-popover-fade-in {
from {
opacity: 0;
transform: translateY(-3px);
}
to {
opacity: 1;
transform: translateY(0);
}
}

.row {
width: 100%;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.row > .col {
display: flex;
align-items: center;
}
.row > .col.form-item {
display: grid;
gap: 8px;
grid-template-columns: 26px auto;
grid-template-rows: repeat(1, 1fr);
grid-template-areas: 'label input';
user-select: none;
box-sizing: border-box;
}

.form-item {
width: 280px;
padding: 4px 10px;
display: grid;
gap: 8px;
grid-template-columns: 26px auto;
grid-template-rows: repeat(1, 1fr);
grid-template-areas: 'label input';
user-select: none;
box-sizing: border-box;

border: 1px solid var(--affine-border-color);
box-sizing: border-box;

outline: none;
border-radius: 4px;
background: transparent;
}
.form-item:focus-within {
border-color: var(--affine-blue-700);
box-shadow: var(--affine-active-shadow);
}

label {
grid-area: label;
box-sizing: border-box;
color: var(--affine-icon-color);
${FONT_XS};
font-weight: 400;
}

input {
grid-area: input;
color: inherit;
padding: 0;
border: none;
background: transparent;
color: var(--affine-text-primary-color);
${FONT_XS};
}
input::placeholder {
color: var(--affine-placeholder-color);
}
input:focus {
outline: none;
}
input:focus ~ label,
input:active ~ label {
color: var(--affine-primary-color);
}
`;

private _onConfirm = () => {
const title = this.titleInput.value.trim();
if (!title) return;

const text = ' ';
this.inlineEditor.insertText(this.inlineRange, text, {
reference: {
...this.referenceInfo,
type: 'LinkedPage',
title,
},
});
this.inlineEditor.setInlineRange({
index: this.inlineRange.index + text.length,
length: 0,
});
this.remove();
};

private _onKeydown(e: KeyboardEvent) {
e.stopPropagation();
if (!e.isComposing) {
if (e.key === 'Escape') {
e.preventDefault();
this.remove();
return;
}
if (e.key === 'Enter') {
e.preventDefault();
this._onConfirm();
}
}
}

private _updateConfirmBtn() {
const value = this.titleInput.value.trim();
const disabled = !value;
this.confirmBtn.disabled = disabled;
this.confirmBtn.active = !disabled;
this.confirmBtn.requestUpdate();
}

override firstUpdated() {
this.disposables.addFromEvent(this.overlayMask, 'click', e => {
e.stopPropagation();
this.remove();
});
this.disposables.addFromEvent(this, 'keydown', this._onKeydown);

this.titleInput.value = this.referenceInfo.title ?? this.docTitle;
this.titleInput.select();
this._updateConfirmBtn();
}

override render() {
return html`
<div class="overlay-root">
<div class="overlay-mask"></div>
<div class="alias-popup-form">
<div class="row">
<div class="col form-item">
<input
id="alias-title"
type="text"
placeholder="Enter title"
@input=${this._updateConfirmBtn}
/>
<label for="alias-title">Title</label>
</div>
<div class="col">
<editor-icon-button
class="confirm"
.iconSize=${'24px'}
.disabled=${true}
@click=${this._onConfirm}
>
${ConfirmIcon}
</editor-icon-button>
</div>
</div>
</div>
</div>
`;
}

override updated() {
const range = this.inlineEditor.toDomRange(this.inlineRange);
assertExists(range);

const visualElement = {
getBoundingClientRect: () => range.getBoundingClientRect(),
getClientRects: () => range.getClientRects(),
};
computePosition(visualElement, this.popupContainer, {
middleware: [
offset(10),
inline(),
shift({
padding: 6,
}),
],
})
.then(({ x, y }) => {
const popupContainer = this.popupContainer;
if (!popupContainer) return;
popupContainer.style.left = `${x}px`;
popupContainer.style.top = `${y}px`;
})
.catch(console.error);
}

@query('.alias-popup-form editor-icon-button.confirm')
accessor confirmBtn!: EditorIconButton;

@property({ type: Object })
accessor delta!: DeltaInsert<AffineTextAttributes>;

@property({ attribute: false })
accessor docTitle!: string;

@property({ attribute: false })
accessor inlineEditor!: AffineInlineEditor;

@property({ attribute: false })
accessor inlineRange!: InlineRange;

@query('.overlay-mask')
accessor overlayMask!: HTMLDivElement;

@query('.alias-popup-form')
accessor popupContainer!: HTMLDivElement;

@property({ type: Object })
accessor referenceInfo!: ReferenceInfo;

@query('.alias-popup-form input')
accessor titleInput!: HTMLInputElement;
}
Loading
Loading