Skip to content

Commit ce925cf

Browse files
authored
Add multiline module (#7)
* update packages * add multiline module * Fix markup generation * update version * update package.lock * fix package.lock * update dependency * fix tests * refactor
1 parent 29ae77f commit ce925cf

File tree

8 files changed

+1812
-20376
lines changed

8 files changed

+1812
-20376
lines changed

blots/multilineBreak.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { EmbedBlot } from 'parchment';
2+
3+
class MultilineBreak extends EmbedBlot {
4+
static value() {
5+
return '\n';
6+
}
7+
8+
length() {
9+
return 1;
10+
}
11+
12+
value() {
13+
return '\n';
14+
}
15+
16+
optimize() {
17+
if (!this.prev && !this.next) {
18+
this.remove();
19+
}
20+
}
21+
}
22+
23+
MultilineBreak.blotName = 'multilineBreak';
24+
MultilineBreak.tagName = 'BR';
25+
26+
export default MultilineBreak;

core/quill.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,7 @@ class Quill {
109109
source,
110110
);
111111
});
112-
const contents = this.clipboard.convert({
113-
html: `${html}<p><br></p>`,
114-
text: '\n',
115-
});
116-
this.setContents(contents);
112+
this.setContents(this.getInitialContent(html));
117113
this.history.clear();
118114
if (this.options.placeholder) {
119115
this.root.setAttribute('data-placeholder', this.options.placeholder);
@@ -124,6 +120,13 @@ class Quill {
124120
this.allowReadOnlyEdits = false;
125121
}
126122

123+
getInitialContent(html) {
124+
return this.clipboard.convert({
125+
html: `${html}<p><br></p>`,
126+
text: '\n',
127+
});
128+
}
129+
127130
addContainer(container, refNode = null) {
128131
if (typeof container === 'string') {
129132
const className = container;

modules/multiline.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import Delta from 'quill-delta';
2+
import Quill from '../core/quill';
3+
import MultilineBreak from '../blots/multilineBreak';
4+
import Module from '../core/module';
5+
6+
function breakMatcher(node) {
7+
if (!node.nextSibling && !node.previousSibling) {
8+
return new Delta().insert('\n');
9+
}
10+
return new Delta().insert({ multilineBreak: '' });
11+
}
12+
13+
class Multiline extends Module {
14+
constructor(quill, options) {
15+
const path = 'blots/multilineBreak';
16+
super(quill, options);
17+
18+
Quill.register({ [path]: MultilineBreak }, true);
19+
20+
quill.keyboard.addBinding(
21+
{
22+
key: 'enter',
23+
shiftKey: true,
24+
},
25+
this.enterHandler.bind(this),
26+
);
27+
quill.keyboard.bindings.enter.unshift(quill.keyboard.bindings.enter.pop());
28+
quill.clipboard.addMatcher('BR', breakMatcher);
29+
}
30+
31+
enterHandler(range) {
32+
const currentLeaf = this.quill.getLeaf(range.index)[0];
33+
const nextLeaf = this.quill.getLeaf(range.index + 1)[0];
34+
35+
this.quill.insertEmbed(range.index, 'multilineBreak', true, 'user');
36+
37+
if (nextLeaf === null || currentLeaf.parent !== nextLeaf.parent) {
38+
this.quill.insertEmbed(range.index, 'multilineBreak', true, 'user');
39+
}
40+
41+
this.quill.setSelection(range.index + 1, Quill.sources.SILENT);
42+
}
43+
}
44+
45+
export default Multiline;

0 commit comments

Comments
 (0)