Skip to content

Commit

Permalink
Merge branch 'fix-cut-format' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Feb 3, 2021
2 parents 232b6f4 + bf721e6 commit aa5bb44
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
3 changes: 2 additions & 1 deletion modules/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ColorStyle } from '../formats/color';
import { DirectionAttribute, DirectionStyle } from '../formats/direction';
import { FontStyle } from '../formats/font';
import { SizeStyle } from '../formats/size';
import { deleteRange } from './keyboard';

const debug = logger('quill:clipboard');

Expand Down Expand Up @@ -135,7 +136,7 @@ class Clipboard extends Module {
e.clipboardData.setData('text/plain', text);
e.clipboardData.setData('text/html', html);
if (isCut) {
this.quill.deleteText(range, Quill.sources.USER);
deleteRange({ range, quill: this.quill });
}
}

Expand Down
30 changes: 17 additions & 13 deletions modules/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,7 @@ class Keyboard extends Module {
}

handleDeleteRange(range) {
const lines = this.quill.getLines(range);
let formats = {};
if (lines.length > 1) {
const firstFormats = lines[0].formats();
const lastFormats = lines[lines.length - 1].formats();
formats = AttributeMap.diff(lastFormats, firstFormats) || {};
}
this.quill.deleteText(range, Quill.sources.USER);
if (Object.keys(formats).length > 0) {
this.quill.formatLine(range.index, 1, formats, Quill.sources.USER);
}
this.quill.setSelection(range.index, Quill.sources.SILENT);
deleteRange({ range, quill: this.quill });
this.quill.focus();
}

Expand Down Expand Up @@ -709,6 +698,21 @@ function normalize(binding) {
return binding;
}

function deleteRange({ quill, range }) {
const lines = quill.getLines(range);
let formats = {};
if (lines.length > 1) {
const firstFormats = lines[0].formats();
const lastFormats = lines[lines.length - 1].formats();
formats = AttributeMap.diff(lastFormats, firstFormats) || {};
}
quill.deleteText(range, Quill.sources.USER);
if (Object.keys(formats).length > 0) {
quill.formatLine(range.index, 1, formats, Quill.sources.USER);
}
quill.setSelection(range.index, Quill.sources.SILENT);
}

function tableSide(table, row, cell, offset) {
if (row.prev == null && row.next == null) {
if (cell.prev == null && cell.next == null) {
Expand All @@ -725,4 +729,4 @@ function tableSide(table, row, cell, offset) {
return null;
}

export { Keyboard as default, SHORTKEY, normalize };
export { Keyboard as default, SHORTKEY, normalize, deleteRange };
27 changes: 27 additions & 0 deletions test/unit/modules/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ describe('Clipboard', function() {
});
});

describe('cut', () => {
beforeEach(function() {
this.clipboardData = {};
this.clipboardEvent = {
clipboardData: {
setData: (type, data) => {
this.clipboardData[type] = data;
},
},
preventDefault: () => {},
};
});

it('keeps formats of first line', function(done) {
this.quill.clipboard.onCaptureCopy(this.clipboardEvent, true);
setTimeout(() => {
expect(this.quill.root).toEqualHTML('<h1>01<em>7</em>8</h1>');
expect(this.quill.getSelection()).toEqual(new Range(2));
expect(this.clipboardData['text/plain']).toEqual('23\n56');
expect(this.clipboardData['text/html']).toEqual(
'<h1>23</h1><p>5<em>6</em></p>',
);
done();
}, 2);
});
});

it('dangerouslyPasteHTML(html)', function() {
this.quill.clipboard.dangerouslyPasteHTML('<i>ab</i><b>cd</b>');
expect(this.quill.root).toEqualHTML(
Expand Down

0 comments on commit aa5bb44

Please sign in to comment.