0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-03 13:16:31 -05:00

Enter press at end of PRE will break line.

This commit is contained in:
Gert K. Sønderby 2015-11-25 15:39:29 +01:00 committed by Gert Sønderby
parent 8a8285560e
commit 29fffdccd0
5 changed files with 23 additions and 13 deletions

View file

@ -1327,10 +1327,14 @@ var keyHandlers = {
}
if ( /^PRE|CODE|SAMP$/.test( block.nodeName ) ) {
// Inside a preformatted block, insert a linebreak, and done.
if ( !getNodeAfter( range.endContainer, range.endOffset ).nodeValue[ range.endOffset ] ) {
insertNodeInRange( range, self._doc.createTextNode( '\n\n' ) );
} else {
insertNodeInRange( range, self._doc.createTextNode( '\n' ) );
range.collapse( false );
}
// Inside a preformatted block, insert a linebreak, and done.
block.normalize();
range.collapse( false );
self.setSelection( range );
self._updatePath( range, true );
return;
@ -3545,7 +3549,7 @@ var getTextFromHTMLFragment = function ( self, frag ) {
// Strip down to text only
lines.push( node.textContent );
}
return self._doc.createTextNode( lines.join( '\n' ) || '\n' );
return self._doc.createTextNode( lines.join( '\n' ) || '' );
};
var makePreformatted = function ( frag ) {
@ -3553,7 +3557,8 @@ var makePreformatted = function ( frag ) {
this._config.tagAttributes.pre, [
this.createElement( 'INPUT', { id: startSelectionId, type: 'hidden' } ),
getTextFromHTMLFragment( this, frag ),
this.createElement( 'INPUT', { id: endSelectionId, type: 'hidden' } )
this.createElement( 'INPUT', { id: endSelectionId, type: 'hidden' } ),
this._doc.createTextNode( '\n' )
] );
};

File diff suppressed because one or more lines are too long

View file

@ -1346,7 +1346,7 @@ var getTextFromHTMLFragment = function ( self, frag ) {
// Strip down to text only
lines.push( node.textContent );
}
return self._doc.createTextNode( lines.join( '\n' ) || '\n' );
return self._doc.createTextNode( lines.join( '\n' ) || '' );
};
var makePreformatted = function ( frag ) {
@ -1354,7 +1354,8 @@ var makePreformatted = function ( frag ) {
this._config.tagAttributes.pre, [
this.createElement( 'INPUT', { id: startSelectionId, type: 'hidden' } ),
getTextFromHTMLFragment( this, frag ),
this.createElement( 'INPUT', { id: endSelectionId, type: 'hidden' } )
this.createElement( 'INPUT', { id: endSelectionId, type: 'hidden' } ),
this._doc.createTextNode( '\n' )
] );
};

View file

@ -176,10 +176,14 @@ var keyHandlers = {
}
if ( /^PRE|CODE|SAMP$/.test( block.nodeName ) ) {
// Inside a preformatted block, insert a linebreak, and done.
if ( !getNodeAfter( range.endContainer, range.endOffset ).nodeValue[ range.endOffset ] ) {
insertNodeInRange( range, self._doc.createTextNode( '\n\n' ) );
} else {
insertNodeInRange( range, self._doc.createTextNode( '\n' ) );
range.collapse( false );
}
// Inside a preformatted block, insert a linebreak, and done.
block.normalize();
range.collapse( false );
self.setSelection( range );
self._updatePath( range, true );
return;

View file

@ -204,7 +204,7 @@ describe('Squire RTE', function () {
expect(editor, 'to contain HTML', startHTML);
editor.moveCursorToStart();
editor.makePreformatted();
expect(editor, 'to contain HTML', '<pre>one two three four five</pre>');
expect(editor, 'to contain HTML', '<pre>one two three four five\n</pre>');
});
it('adds an empty PRE element', function () {
@ -222,7 +222,7 @@ describe('Squire RTE', function () {
expect(editor, 'to contain HTML', startHTML);
selectAll(editor);
editor.makePreformatted();
expect(editor, 'to contain HTML', '<pre>one two\nthree four\nfive</pre>');
expect(editor, 'to contain HTML', '<pre>one two\nthree four\nfive\n</pre>');
});
it('expands existing PRE tags to encompass selection', function () {
@ -231,7 +231,7 @@ describe('Squire RTE', function () {
expect(editor, 'to contain HTML', startHTML);
selectAll(editor);
editor.makePreformatted();
expect(editor, 'to contain HTML', '<pre>abc\n\none two three four five\n</pre>');
expect(editor, 'to contain HTML', '<pre>abc\n\none two three four five\n\n</pre>');
});
});