Skip to content

Commit

Permalink
fix(database): support for pasting linked doc into title cell (#8703)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzj3720 authored Nov 7, 2024
1 parent 9bad42b commit e37be7f
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions packages/blocks/src/database-block/properties/title/text.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Text } from '@blocksuite/store';
import type { DeltaInsert } from '@blocksuite/inline';
import type { BlockSnapshot, Text } from '@blocksuite/store';

import {
DefaultInlineManagerExtension,
Expand All @@ -19,6 +20,7 @@ import { html } from 'lit/static-html.js';

import type { DatabaseBlockComponent } from '../../database-block.js';

import { ClipboardAdapter } from '../../../root-block/clipboard/adapter.js';
import { HostContextKey } from '../../context/host-context.js';

const styles = css`
Expand Down Expand Up @@ -193,11 +195,29 @@ export class HeaderAreaTextCellEditing extends BaseTextCell {

private _onPaste = (e: ClipboardEvent) => {
const inlineEditor = this.inlineEditor;
assertExists(inlineEditor);

const inlineRange = inlineEditor.getInlineRange();
if (!inlineRange) return;

if (e.clipboardData) {
try {
const getDeltas = (snapshot: BlockSnapshot): DeltaInsert[] => {
// @ts-ignore
const text = snapshot.props?.text?.delta;
return text
? [...text, ...(snapshot.children?.flatMap(getDeltas) ?? [])]
: snapshot.children?.flatMap(getDeltas);
};
const snapshot = this.std?.clipboard?.readFromClipboard(
e.clipboardData
)[ClipboardAdapter.MIME];
const deltas = (
JSON.parse(snapshot).snapshot.content as BlockSnapshot[]
).flatMap(getDeltas);
deltas.forEach(delta => this.insertDelta(delta));
return;
} catch (_e) {
//
}
}
const text = e.clipboardData
?.getData('text/plain')
?.replace(/\r?\n|\r/g, '\n');
Expand Down Expand Up @@ -242,6 +262,19 @@ export class HeaderAreaTextCellEditing extends BaseTextCell {
}
};

insertDelta = (delta: DeltaInsert) => {
const inlineEditor = this.inlineEditor;
const range = inlineEditor.getInlineRange();
if (!range || !delta.insert) {
return;
}
inlineEditor.insertText(range, delta.insert, delta.attributes);
inlineEditor.setInlineRange({
index: range.index + delta.insert.length,
length: 0,
});
};

private get std() {
const host = this.view.contextGet(HostContextKey);
return host?.std;
Expand Down

0 comments on commit e37be7f

Please sign in to comment.