mirror of
https://github.com/fastmail/Squire.git
synced 2025-01-21 14:02:28 -05:00
Added Squire#removePreformatted().
This commit is contained in:
parent
5d5d75e62f
commit
857a3c2a78
4 changed files with 184 additions and 2 deletions
|
@ -1629,6 +1629,15 @@ if ( !isMac ) {
|
|||
};
|
||||
}
|
||||
|
||||
keyHandlers[ ctrlKey + 'shift-m' ] = function ( self, event, range ) {
|
||||
event.preventDefault();
|
||||
if ( self.hasFormat( 'pre', null, range ) ) {
|
||||
self.removePreformatted();
|
||||
} else {
|
||||
self.makePreformatted();
|
||||
}
|
||||
};
|
||||
|
||||
keyHandlers[ ctrlKey + 'b' ] = mapKeyToFormat( 'B' );
|
||||
keyHandlers[ ctrlKey + 'i' ] = mapKeyToFormat( 'I' );
|
||||
keyHandlers[ ctrlKey + 'u' ] = mapKeyToFormat( 'U' );
|
||||
|
@ -3528,6 +3537,86 @@ var decreaseListLevel = function ( frag ) {
|
|||
return frag;
|
||||
};
|
||||
|
||||
var makePreformatted = function ( frag ) {
|
||||
var walker = getBlockWalker( frag ),
|
||||
lines = [],
|
||||
node;
|
||||
while ( node = walker.nextNode() ) {
|
||||
// Strip down to text only
|
||||
lines.push( node.textContent );
|
||||
}
|
||||
node = this._doc.createTextNode( lines.join( '\n' ) || '\n' );
|
||||
return this.createElement( 'PRE',
|
||||
this._config.tagAttributes.pre, [
|
||||
node
|
||||
] );
|
||||
};
|
||||
|
||||
var removePreformatted = function ( frag ) {
|
||||
var range = this._doc.createRange();
|
||||
var startRangeMarker = frag.getElementById( startSelectionId );
|
||||
var endRangeMarker = frag.getElementById( endSelectionId );
|
||||
if (!startRangeMarker || !endRangeMarker) { return frag; }
|
||||
|
||||
range.setStartBefore( startRangeMarker );
|
||||
range.setEndAfter( endRangeMarker );
|
||||
|
||||
if (!range || range.collapsed) {
|
||||
return frag; // Maybe remove whole element instead? Single line?
|
||||
} else {
|
||||
var preElems = frag.querySelectorAll('pre');
|
||||
if ( preElems.length === 0 ) {
|
||||
return frag;
|
||||
} else {
|
||||
var firstPre = preElems[0],
|
||||
lastPre = preElems[preElems.length - 1],
|
||||
startContainer = range.startContainer,
|
||||
startOffset = range.startOffset,
|
||||
endContainer = range.endContainer,
|
||||
endOffset = range.endOffset,
|
||||
splitElems = [];
|
||||
if ( getNearest( endContainer, 'PRE' ) === lastPre &&
|
||||
endOffset < endContainer.childNodes.length - 1 ) {
|
||||
splitElems.push( split( endContainer, endOffset, lastPre.parentNode ) );
|
||||
}
|
||||
if ( getNearest( startContainer, 'PRE' ) === firstPre &&
|
||||
startOffset > 0 ) {
|
||||
split( startContainer, startOffset, firstPre.parentNode );
|
||||
splitElems.push( startContainer );
|
||||
}
|
||||
preElems = frag.querySelectorAll('pre');
|
||||
range.setStartBefore( startRangeMarker );
|
||||
range.setEndAfter( endRangeMarker );
|
||||
var self = this,
|
||||
node, elemsInRange;
|
||||
for ( var i = 0; i < preElems.length; i += 1 ) {
|
||||
node = preElems[i];
|
||||
if ( isNodeContainedInRange( range, node, true ) ) {
|
||||
var replacement = this._doc.createDocumentFragment(),
|
||||
childNode;
|
||||
while ( childNode = node.childNodes[0] ) {
|
||||
if ( childNode.nodeType === TEXT_NODE ) {
|
||||
// replace all text nodes with HTMLified version of text content (1 DIV per line)
|
||||
var nodeLines = childNode.nodeValue.split( '\n' );
|
||||
/*jshint loopfunc: true*/
|
||||
nodeLines.forEach( function (line) {
|
||||
var div = self.createDefaultBlock( [ self._doc.createTextNode ( line ) ] );
|
||||
replacement.appendChild( div );
|
||||
});
|
||||
/*jshint loopfunc: false*/
|
||||
node.removeChild( childNode );
|
||||
} else {
|
||||
replacement.appendChild( childNode );
|
||||
}
|
||||
}
|
||||
replaceWith( node, replacement );
|
||||
}
|
||||
}
|
||||
return frag;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
proto._ensureBottomLine = function () {
|
||||
var body = this._body,
|
||||
last = body.lastElementChild;
|
||||
|
@ -4058,6 +4147,8 @@ proto.makeUnorderedList = command( 'modifyBlocks', makeUnorderedList );
|
|||
proto.makeOrderedList = command( 'modifyBlocks', makeOrderedList );
|
||||
proto.removeList = command( 'modifyBlocks', removeList );
|
||||
|
||||
proto.makePreformatted = command( 'modifyBlocks', makePreformatted );
|
||||
proto.removePreformatted = command( 'modifyBlocks', removePreformatted );
|
||||
proto.increaseListLevel = command( 'modifyBlocks', increaseListLevel );
|
||||
proto.decreaseListLevel = command( 'modifyBlocks', decreaseListLevel );
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1338,6 +1338,86 @@ var decreaseListLevel = function ( frag ) {
|
|||
return frag;
|
||||
};
|
||||
|
||||
var makePreformatted = function ( frag ) {
|
||||
var walker = getBlockWalker( frag ),
|
||||
lines = [],
|
||||
node;
|
||||
while ( node = walker.nextNode() ) {
|
||||
// Strip down to text only
|
||||
lines.push( node.textContent );
|
||||
}
|
||||
node = this._doc.createTextNode( lines.join( '\n' ) || '\n' );
|
||||
return this.createElement( 'PRE',
|
||||
this._config.tagAttributes.pre, [
|
||||
node
|
||||
] );
|
||||
};
|
||||
|
||||
var removePreformatted = function ( frag ) {
|
||||
var range = this._doc.createRange();
|
||||
var startRangeMarker = frag.getElementById( startSelectionId );
|
||||
var endRangeMarker = frag.getElementById( endSelectionId );
|
||||
if (!startRangeMarker || !endRangeMarker) { return frag; }
|
||||
|
||||
range.setStartBefore( startRangeMarker );
|
||||
range.setEndAfter( endRangeMarker );
|
||||
|
||||
if (!range || range.collapsed) {
|
||||
return frag; // Maybe remove whole element instead? Single line?
|
||||
} else {
|
||||
var preElems = frag.querySelectorAll('pre');
|
||||
if ( preElems.length === 0 ) {
|
||||
return frag;
|
||||
} else {
|
||||
var firstPre = preElems[0],
|
||||
lastPre = preElems[preElems.length - 1],
|
||||
startContainer = range.startContainer,
|
||||
startOffset = range.startOffset,
|
||||
endContainer = range.endContainer,
|
||||
endOffset = range.endOffset,
|
||||
splitElems = [];
|
||||
if ( getNearest( endContainer, 'PRE' ) === lastPre &&
|
||||
endOffset < endContainer.childNodes.length - 1 ) {
|
||||
splitElems.push( split( endContainer, endOffset, lastPre.parentNode ) );
|
||||
}
|
||||
if ( getNearest( startContainer, 'PRE' ) === firstPre &&
|
||||
startOffset > 0 ) {
|
||||
split( startContainer, startOffset, firstPre.parentNode );
|
||||
splitElems.push( startContainer );
|
||||
}
|
||||
preElems = frag.querySelectorAll('pre');
|
||||
range.setStartBefore( startRangeMarker );
|
||||
range.setEndAfter( endRangeMarker );
|
||||
var self = this,
|
||||
node, elemsInRange;
|
||||
for ( var i = 0; i < preElems.length; i += 1 ) {
|
||||
node = preElems[i];
|
||||
if ( isNodeContainedInRange( range, node, true ) ) {
|
||||
var replacement = this._doc.createDocumentFragment(),
|
||||
childNode;
|
||||
while ( childNode = node.childNodes[0] ) {
|
||||
if ( childNode.nodeType === TEXT_NODE ) {
|
||||
// replace all text nodes with HTMLified version of text content (1 DIV per line)
|
||||
var nodeLines = childNode.nodeValue.split( '\n' );
|
||||
/*jshint loopfunc: true*/
|
||||
nodeLines.forEach( function (line) {
|
||||
var div = self.createDefaultBlock( [ self._doc.createTextNode ( line ) ] );
|
||||
replacement.appendChild( div );
|
||||
});
|
||||
/*jshint loopfunc: false*/
|
||||
node.removeChild( childNode );
|
||||
} else {
|
||||
replacement.appendChild( childNode );
|
||||
}
|
||||
}
|
||||
replaceWith( node, replacement );
|
||||
}
|
||||
}
|
||||
return frag;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
proto._ensureBottomLine = function () {
|
||||
var body = this._body,
|
||||
last = body.lastElementChild;
|
||||
|
@ -1868,5 +1948,7 @@ proto.makeUnorderedList = command( 'modifyBlocks', makeUnorderedList );
|
|||
proto.makeOrderedList = command( 'modifyBlocks', makeOrderedList );
|
||||
proto.removeList = command( 'modifyBlocks', removeList );
|
||||
|
||||
proto.makePreformatted = command( 'modifyBlocks', makePreformatted );
|
||||
proto.removePreformatted = command( 'modifyBlocks', removePreformatted );
|
||||
proto.increaseListLevel = command( 'modifyBlocks', increaseListLevel );
|
||||
proto.decreaseListLevel = command( 'modifyBlocks', decreaseListLevel );
|
||||
|
|
|
@ -478,6 +478,15 @@ if ( !isMac ) {
|
|||
};
|
||||
}
|
||||
|
||||
keyHandlers[ ctrlKey + 'shift-m' ] = function ( self, event, range ) {
|
||||
event.preventDefault();
|
||||
if ( self.hasFormat( 'pre', null, range ) ) {
|
||||
self.removePreformatted();
|
||||
} else {
|
||||
self.makePreformatted();
|
||||
}
|
||||
};
|
||||
|
||||
keyHandlers[ ctrlKey + 'b' ] = mapKeyToFormat( 'B' );
|
||||
keyHandlers[ ctrlKey + 'i' ] = mapKeyToFormat( 'I' );
|
||||
keyHandlers[ ctrlKey + 'u' ] = mapKeyToFormat( 'U' );
|
||||
|
|
Loading…
Add table
Reference in a new issue