mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-24 05:07:06 +08:00
chore(editor): reorg packages (#10702)
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
import { DefaultInlineManagerExtension } from '@blocksuite/affine-rich-text';
|
||||
import type { EditorHost } from '@blocksuite/block-std';
|
||||
import { ShadowlessElement } from '@blocksuite/block-std';
|
||||
import type { DetailSlotProps } from '@blocksuite/data-view';
|
||||
import type {
|
||||
KanbanSingleView,
|
||||
TableSingleView,
|
||||
} from '@blocksuite/data-view/view-presets';
|
||||
import { WithDisposable } from '@blocksuite/global/lit';
|
||||
import { css, html } from 'lit';
|
||||
import { property } from 'lit/decorators.js';
|
||||
|
||||
export class BlockRenderer
|
||||
extends WithDisposable(ShadowlessElement)
|
||||
implements DetailSlotProps
|
||||
{
|
||||
static override styles = css`
|
||||
database-datasource-block-renderer {
|
||||
padding-top: 36px;
|
||||
padding-bottom: 16px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
margin-bottom: 12px;
|
||||
border-bottom: 1px solid var(--affine-border-color);
|
||||
font-size: var(--affine-font-base);
|
||||
line-height: var(--affine-line-height);
|
||||
}
|
||||
|
||||
database-datasource-block-renderer .tips-placeholder {
|
||||
display: none;
|
||||
}
|
||||
|
||||
database-datasource-block-renderer rich-text {
|
||||
font-size: 15px;
|
||||
line-height: 24px;
|
||||
}
|
||||
|
||||
database-datasource-block-renderer.empty rich-text::before {
|
||||
content: 'Untitled';
|
||||
position: absolute;
|
||||
color: var(--affine-text-disable-color);
|
||||
font-size: 15px;
|
||||
line-height: 24px;
|
||||
user-select: none;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.database-block-detail-header-icon {
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
padding: 2px;
|
||||
border-radius: 4px;
|
||||
background-color: var(--affine-background-secondary-color);
|
||||
}
|
||||
|
||||
.database-block-detail-header-icon svg {
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
}
|
||||
`;
|
||||
|
||||
get attributeRenderer() {
|
||||
return this.inlineManager.getRenderer();
|
||||
}
|
||||
|
||||
get attributesSchema() {
|
||||
return this.inlineManager.getSchema();
|
||||
}
|
||||
|
||||
get inlineManager() {
|
||||
return this.host.std.get(DefaultInlineManagerExtension.identifier);
|
||||
}
|
||||
|
||||
get model() {
|
||||
return this.host?.doc.getBlock(this.rowId)?.model;
|
||||
}
|
||||
|
||||
override connectedCallback() {
|
||||
super.connectedCallback();
|
||||
if (this.model && this.model.text) {
|
||||
const cb = () => {
|
||||
if (this.model?.text?.length == 0) {
|
||||
this.classList.add('empty');
|
||||
} else {
|
||||
this.classList.remove('empty');
|
||||
}
|
||||
};
|
||||
this.model.text.yText.observe(cb);
|
||||
this.disposables.add(() => {
|
||||
this.model?.text?.yText.unobserve(cb);
|
||||
});
|
||||
}
|
||||
this._disposables.addFromEvent(
|
||||
this,
|
||||
'keydown',
|
||||
e => {
|
||||
if (e.key === 'Enter' && !e.shiftKey && !e.isComposing) {
|
||||
e.stopPropagation();
|
||||
e.preventDefault();
|
||||
return;
|
||||
}
|
||||
if (
|
||||
e.key === 'Backspace' &&
|
||||
!e.shiftKey &&
|
||||
!e.metaKey &&
|
||||
this.model?.text?.length === 0
|
||||
) {
|
||||
e.stopPropagation();
|
||||
e.preventDefault();
|
||||
return;
|
||||
}
|
||||
},
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
protected override render(): unknown {
|
||||
const model = this.model;
|
||||
if (!model) {
|
||||
return;
|
||||
}
|
||||
return html`
|
||||
${this.renderIcon()}
|
||||
<rich-text
|
||||
.yText=${model.text}
|
||||
.attributesSchema=${this.attributesSchema}
|
||||
.attributeRenderer=${this.attributeRenderer}
|
||||
.embedChecker=${this.inlineManager.embedChecker}
|
||||
.markdownMatches=${this.inlineManager.markdownMatches}
|
||||
class="inline-editor"
|
||||
></rich-text>
|
||||
`;
|
||||
}
|
||||
|
||||
renderIcon() {
|
||||
const iconColumn = this.view.mainProperties$.value.iconColumn;
|
||||
if (!iconColumn) {
|
||||
return;
|
||||
}
|
||||
return html` <div class="database-block-detail-header-icon">
|
||||
${this.view.cellValueGet(this.rowId, iconColumn)}
|
||||
</div>`;
|
||||
}
|
||||
|
||||
@property({ attribute: false })
|
||||
accessor host!: EditorHost;
|
||||
|
||||
@property({ attribute: false })
|
||||
accessor openDoc!: (docId: string) => void;
|
||||
|
||||
@property({ attribute: false })
|
||||
accessor rowId!: string;
|
||||
|
||||
@property({ attribute: false })
|
||||
accessor view!: TableSingleView | KanbanSingleView;
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
import {
|
||||
CodeBlockModel,
|
||||
type DatabaseBlockModel,
|
||||
ListBlockModel,
|
||||
ParagraphBlockModel,
|
||||
type RootBlockModel,
|
||||
} from '@blocksuite/affine-model';
|
||||
import { REFERENCE_NODE } from '@blocksuite/affine-shared/consts';
|
||||
import { TelemetryProvider } from '@blocksuite/affine-shared/services';
|
||||
import type { AffineTextAttributes } from '@blocksuite/affine-shared/types';
|
||||
import { createDefaultDoc, matchModels } from '@blocksuite/affine-shared/utils';
|
||||
import { type EditorHost, ShadowlessElement } from '@blocksuite/block-std';
|
||||
import type { DetailSlotProps, SingleView } from '@blocksuite/data-view';
|
||||
import { SignalWatcher, WithDisposable } from '@blocksuite/global/lit';
|
||||
import type { BaseTextAttributes } from '@blocksuite/inline';
|
||||
import { computed } from '@preact/signals-core';
|
||||
import { css, html } from 'lit';
|
||||
import { property } from 'lit/decorators.js';
|
||||
|
||||
import { isPureText } from '../utils/title-doc.js';
|
||||
|
||||
export class NoteRenderer
|
||||
extends SignalWatcher(WithDisposable(ShadowlessElement))
|
||||
implements DetailSlotProps
|
||||
{
|
||||
static override styles = css`
|
||||
database-datasource-note-renderer {
|
||||
width: 100%;
|
||||
--affine-editor-side-padding: 0;
|
||||
flex: 1;
|
||||
}
|
||||
`;
|
||||
|
||||
@property({ attribute: false })
|
||||
accessor rowId!: string;
|
||||
|
||||
rowText$ = computed(() => {
|
||||
return this.databaseBlock.doc.getBlock(this.rowId)?.model?.text;
|
||||
});
|
||||
|
||||
allowCreateDoc$ = computed(() => {
|
||||
return isPureText(this.rowText$.value);
|
||||
});
|
||||
|
||||
get databaseBlock(): DatabaseBlockModel {
|
||||
return this.model;
|
||||
}
|
||||
|
||||
addNote() {
|
||||
const collection = this.host?.std.workspace;
|
||||
if (!collection) {
|
||||
return;
|
||||
}
|
||||
const note = createDefaultDoc(collection);
|
||||
if (note) {
|
||||
this.openDoc(note.id);
|
||||
const rowContent = this.rowText$.value?.toString();
|
||||
this.rowText$.value?.replace(
|
||||
0,
|
||||
this.rowText$.value.length,
|
||||
REFERENCE_NODE,
|
||||
{
|
||||
reference: {
|
||||
type: 'LinkedPage',
|
||||
pageId: note.id,
|
||||
},
|
||||
} satisfies AffineTextAttributes as BaseTextAttributes
|
||||
);
|
||||
collection.meta.setDocMeta(note.id, { title: rowContent });
|
||||
if (note.root) {
|
||||
(note.root as RootBlockModel).title.insert(rowContent ?? '', 0);
|
||||
note.root.children
|
||||
.find(child => child.flavour === 'affine:note')
|
||||
?.children.find(block =>
|
||||
matchModels(block, [
|
||||
ParagraphBlockModel,
|
||||
ListBlockModel,
|
||||
CodeBlockModel,
|
||||
])
|
||||
);
|
||||
}
|
||||
// Track when a linked doc is created in database title column
|
||||
this.host.std.getOptional(TelemetryProvider)?.track('LinkedDocCreated', {
|
||||
segment: 'database',
|
||||
module: 'center peek in database',
|
||||
type: 'turn into',
|
||||
parentFlavour: 'affine:database',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
protected override render(): unknown {
|
||||
return html`
|
||||
<div
|
||||
style="height: 1px;max-width: var(--affine-editor-width);background-color: var(--affine-border-color);margin: auto;margin-bottom: 16px"
|
||||
></div>
|
||||
${this.renderNote()}
|
||||
`;
|
||||
}
|
||||
|
||||
renderNote() {
|
||||
if (this.allowCreateDoc$.value) {
|
||||
return html` <div>
|
||||
<div
|
||||
@click="${this.addNote}"
|
||||
style="max-width: var(--affine-editor-width);margin: auto;cursor: pointer;color: var(--affine-text-disable-color)"
|
||||
>
|
||||
Click to create a linked doc in center peek.
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@property({ attribute: false })
|
||||
accessor host!: EditorHost;
|
||||
|
||||
@property({ attribute: false })
|
||||
accessor model!: DatabaseBlockModel;
|
||||
|
||||
@property({ attribute: false })
|
||||
accessor openDoc!: (docId: string) => void;
|
||||
|
||||
@property({ attribute: false })
|
||||
accessor view!: SingleView;
|
||||
}
|
||||
Reference in New Issue
Block a user