mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-07 09:21:24 +08:00
feat(editor): unify block props api (#10888)
Closes: [BS-2707](https://linear.app/affine-design/issue/BS-2707/统一使用props获取和更新block-prop)
This commit is contained in:
@@ -28,7 +28,7 @@ import { checkAttachmentBlob, downloadAttachmentBlob } from './utils';
|
||||
|
||||
@Peekable({
|
||||
enableOn: ({ model }: AttachmentBlockComponent) => {
|
||||
return model.type.endsWith('pdf');
|
||||
return model.props.type.endsWith('pdf');
|
||||
},
|
||||
})
|
||||
export class AttachmentBlockComponent extends CaptionedBlockComponent<AttachmentBlockModel> {
|
||||
@@ -104,7 +104,7 @@ export class AttachmentBlockComponent extends CaptionedBlockComponent<Attachment
|
||||
|
||||
this.contentEditable = 'false';
|
||||
|
||||
if (!this.model.style) {
|
||||
if (!this.model.props.style) {
|
||||
this.doc.withoutTransact(() => {
|
||||
this.doc.updateBlock(this.model, {
|
||||
style: AttachmentBlockStyles[1],
|
||||
@@ -170,7 +170,7 @@ export class AttachmentBlockComponent extends CaptionedBlockComponent<Attachment
|
||||
}
|
||||
|
||||
override renderBlock() {
|
||||
const { name, size, style } = this.model;
|
||||
const { name, size, style } = this.model.props;
|
||||
const cardStyle = style ?? AttachmentBlockStyles[1];
|
||||
|
||||
const theme = this.std.get(ThemeProvider).theme;
|
||||
|
||||
@@ -42,7 +42,7 @@ export class AttachmentEdgelessBlockComponent extends toGfxBlockComponent(
|
||||
}
|
||||
|
||||
override renderGfxBlock() {
|
||||
const { style$ } = this.model;
|
||||
const { style$ } = this.model.props;
|
||||
const cardStyle = style$.value ?? AttachmentBlockStyles[1];
|
||||
const width = EMBED_CARD_WIDTH[cardStyle];
|
||||
const height = EMBED_CARD_HEIGHT[cardStyle];
|
||||
|
||||
@@ -19,7 +19,7 @@ export const RenameModal = ({
|
||||
const inputRef = createRef<HTMLInputElement>();
|
||||
// Fix auto focus
|
||||
setTimeout(() => inputRef.value?.focus());
|
||||
const originalName = model.name;
|
||||
const originalName = model.props.name;
|
||||
const nameWithoutExtension = originalName.slice(
|
||||
0,
|
||||
originalName.lastIndexOf('.')
|
||||
|
||||
@@ -100,7 +100,7 @@ const createAttachmentViewDropdownMenuWith = <T extends SelectionConstructor>(
|
||||
const actions = this.actions.map(action => ({ ...action }));
|
||||
const viewType$ = computed(() => {
|
||||
const [cardAction, embedAction] = actions;
|
||||
const embed = model.embed$.value ?? false;
|
||||
const embed = model.props.embed$.value ?? false;
|
||||
|
||||
cardAction.disabled = !embed;
|
||||
embedAction.disabled = embed && embedProvider.embedded(model);
|
||||
|
||||
@@ -113,11 +113,11 @@ export class AttachmentEmbedService extends Extension {
|
||||
blobUrl?: string,
|
||||
maxFileSize = this._maxFileSize
|
||||
) {
|
||||
if (!model.embed || !blobUrl) return;
|
||||
if (!model.props.embed || !blobUrl) return;
|
||||
|
||||
const config = this.values.find(config => config.check(model, maxFileSize));
|
||||
if (!config || !config.template) {
|
||||
console.error('No embed view template found!', model, model.type);
|
||||
console.error('No embed view template found!', model, model.props.type);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -130,7 +130,7 @@ const embedConfig: AttachmentEmbedConfig[] = [
|
||||
name: 'image',
|
||||
check: model =>
|
||||
model.doc.schema.flavourSchemaMap.has('affine:image') &&
|
||||
model.type.startsWith('image/'),
|
||||
model.props.type.startsWith('image/'),
|
||||
async action(model, std) {
|
||||
const component = std.view.getBlock(model.id);
|
||||
if (!component) return;
|
||||
@@ -141,7 +141,7 @@ const embedConfig: AttachmentEmbedConfig[] = [
|
||||
{
|
||||
name: 'pdf',
|
||||
check: (model, maxFileSize) =>
|
||||
model.type === 'application/pdf' && model.size <= maxFileSize,
|
||||
model.props.type === 'application/pdf' && model.props.size <= maxFileSize,
|
||||
template: (_, blobUrl) => {
|
||||
// More options: https://tinytip.co/tips/html-pdf-params/
|
||||
// https://chromium.googlesource.com/chromium/src/+/refs/tags/121.0.6153.1/chrome/browser/resources/pdf/open_pdf_params_parser.ts
|
||||
@@ -162,7 +162,7 @@ const embedConfig: AttachmentEmbedConfig[] = [
|
||||
{
|
||||
name: 'video',
|
||||
check: (model, maxFileSize) =>
|
||||
model.type.startsWith('video/') && model.size <= maxFileSize,
|
||||
model.props.type.startsWith('video/') && model.props.size <= maxFileSize,
|
||||
template: (_, blobUrl) =>
|
||||
html`<video
|
||||
style="max-height: max-content;"
|
||||
@@ -175,7 +175,7 @@ const embedConfig: AttachmentEmbedConfig[] = [
|
||||
{
|
||||
name: 'audio',
|
||||
check: (model, maxFileSize) =>
|
||||
model.type.startsWith('audio/') && model.size <= maxFileSize,
|
||||
model.props.type.startsWith('audio/') && model.props.size <= maxFileSize,
|
||||
template: (_, blobUrl) =>
|
||||
html`<audio controls src=${blobUrl} style="margin: 4px;"></audio>`,
|
||||
},
|
||||
@@ -190,13 +190,15 @@ export async function turnIntoImageBlock(model: AttachmentBlockModel) {
|
||||
return;
|
||||
}
|
||||
|
||||
const sourceId = model.sourceId;
|
||||
const sourceId = model.props.sourceId;
|
||||
if (!sourceId) return;
|
||||
|
||||
const { saveAttachmentData, getImageData } = withTempBlobData();
|
||||
saveAttachmentData(sourceId, { name: model.name });
|
||||
saveAttachmentData(sourceId, { name: model.props.name });
|
||||
|
||||
let imageSize = model.sourceId ? getImageData(model.sourceId) : undefined;
|
||||
let imageSize = model.props.sourceId
|
||||
? getImageData(model.props.sourceId)
|
||||
: undefined;
|
||||
|
||||
const bounds = model.xywh
|
||||
? Bound.fromXYWH(model.deserializedXYWH)
|
||||
@@ -223,8 +225,8 @@ export async function turnIntoImageBlock(model: AttachmentBlockModel) {
|
||||
|
||||
const imageProp: Partial<ImageBlockProps> = {
|
||||
sourceId,
|
||||
caption: model.caption,
|
||||
size: model.size,
|
||||
caption: model.props.caption,
|
||||
size: model.props.size,
|
||||
...imageSize,
|
||||
...others,
|
||||
};
|
||||
|
||||
@@ -98,7 +98,7 @@ export async function uploadAttachmentBlob(
|
||||
}
|
||||
|
||||
export async function getAttachmentBlob(model: AttachmentBlockModel) {
|
||||
const sourceId = model.sourceId;
|
||||
const sourceId = model.props.sourceId;
|
||||
if (!sourceId) {
|
||||
return null;
|
||||
}
|
||||
@@ -107,7 +107,7 @@ export async function getAttachmentBlob(model: AttachmentBlockModel) {
|
||||
let blob = await doc.blobSync.get(sourceId);
|
||||
|
||||
if (blob) {
|
||||
blob = new Blob([blob], { type: model.type });
|
||||
blob = new Blob([blob], { type: model.props.type });
|
||||
}
|
||||
|
||||
return blob;
|
||||
@@ -115,7 +115,8 @@ export async function getAttachmentBlob(model: AttachmentBlockModel) {
|
||||
|
||||
export async function checkAttachmentBlob(block: AttachmentBlockComponent) {
|
||||
const model = block.model;
|
||||
const { id, sourceId } = model;
|
||||
const { id } = model;
|
||||
const { sourceId } = model.props;
|
||||
|
||||
if (isAttachmentUploading(id)) {
|
||||
block.loading = true;
|
||||
@@ -174,7 +175,7 @@ export function downloadAttachmentBlob(block: AttachmentBlockComponent) {
|
||||
return;
|
||||
}
|
||||
|
||||
const name = model.name;
|
||||
const name = model.props.name;
|
||||
const shortName = name.length < 20 ? name : name.slice(0, 20) + '...';
|
||||
|
||||
if (error || !blobUrl) {
|
||||
|
||||
Reference in New Issue
Block a user