mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
fix(editor): paste to code block should delete selected text (#11546)
Close [BS-3064](https://linear.app/affine-design/issue/BS-3064/fix-bug-pasting-in-code-block-does-not-replace-text)
This commit is contained in:
@@ -124,6 +124,13 @@ class PasteTr {
|
||||
|
||||
private readonly _mergeCode = () => {
|
||||
const deltas: DeltaOperation[] = [{ retain: this.pointState.point.index }];
|
||||
// if there is text selection, delete the text selected
|
||||
if (this.pointState.text.length - this.pointState.point.index > 0) {
|
||||
deltas.push({
|
||||
delete: this.pointState.text.length - this.pointState.point.index,
|
||||
});
|
||||
}
|
||||
// paste the text from the snapshot to code block
|
||||
this.snapshot.content.forEach((blockSnapshot, i) => {
|
||||
if (blockSnapshot.props.text) {
|
||||
const text = this._textFromSnapshot(blockSnapshot);
|
||||
@@ -133,6 +140,11 @@ class PasteTr {
|
||||
deltas.push(...text.delta);
|
||||
}
|
||||
});
|
||||
// paste the text after the text selection from the snapshot to code block
|
||||
const { toDelta } = this._getDeltas();
|
||||
if (toDelta.length > 0) {
|
||||
deltas.push(...toDelta);
|
||||
}
|
||||
this.pointState.text.applyDelta(deltas);
|
||||
this.snapshot.content = [];
|
||||
};
|
||||
@@ -490,13 +502,13 @@ class PasteTr {
|
||||
}
|
||||
|
||||
merge() {
|
||||
if (this.pointState.model.flavour === 'affine:code') {
|
||||
this._mergeCode();
|
||||
if (this.firstSnapshot === this.lastSnapshot) {
|
||||
this._mergeSingle();
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.firstSnapshot === this.lastSnapshot) {
|
||||
this._mergeSingle();
|
||||
if (this.pointState.model.flavour === 'affine:code') {
|
||||
this._mergeCode();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user