Archived
1
0

Added a remove formatting feature to the tools menu.

This commit is contained in:
Dan Helfman 2009-03-16 16:35:04 -07:00
parent bbd0328858
commit 205adeb904
3 changed files with 76 additions and 0 deletions

2
NEWS
View File

@ -1,4 +1,6 @@
1.6.9: ?
* Added a remove formatting feature to the tools menu. This allows you to
strip out all formatting from the currently selected text.
* Fixed a bug in which the filename of an exported HTML file was just
"export" instead of being based on the notebook name.

View File

@ -1014,6 +1014,59 @@ Editor.prototype.cleanup_html = function ( key_code ) {
}
}
Editor.prototype.unformat_selection = function () {
if ( !this.iframe || !this.document )
return false;
// if the selection is collapsed, then there is nothing to unformat
if ( this.iframe.contentWindow && this.iframe.contentWindow.getSelection ) { // browsers such as Firefox
var selection = this.iframe.contentWindow.getSelection();
var range = selection.getRangeAt( 0 );
if ( range.collapsed )
return false;
} else if ( this.document.selection ) { // browsers such as IE
var range = this.document.selection.createRange();
var collapsed_range = range.duplicate();
collapsed_range.collapse();
if ( range.isEqual( collapsed_range ) )
return false;
}
// first, remove all standard formatting (<b>, <i>, etc.)
this.exec_command( "removeformat" );
// then, remove all other "formatting" that removeformat doesn't touch (<h1>, <h2>, etc.)
var nodes =
getElementsByTagAndClassName( "h1", null, this.document ).concat(
getElementsByTagAndClassName( "h2", null, this.document ) ).concat(
getElementsByTagAndClassName( "h3", null, this.document ) ).concat(
getElementsByTagAndClassName( "h4", null, this.document ) ).concat(
getElementsByTagAndClassName( "h5", null, this.document ) ).concat(
getElementsByTagAndClassName( "h6", null, this.document ) );
for ( var i in nodes ) {
var node = nodes[ i ];
if ( !node ) continue;
// before proceeding, make sure the node is within the selected text
if ( selection && selection.containsNode ) { // browsers such as Firefox
if ( !selection.containsNode( node, true ) )
continue;
} else if ( this.document.selection ) {
var node_range = this.document.selection.createRange();
node_range.moveToElementText( node );
if ( !range.inRange( node_range ) )
continue;
}
var replacement = withDocument( this.document, function () { return createDOM( "span" ); } );
swapDOM( node, replacement );
appendChildNodes( replacement, node.childNodes );
}
return true;
}
Editor.prototype.mouse_released = function ( event ) {
this.link_started = null;
var self = this;

View File

@ -3226,6 +3226,17 @@ function Tools_pulldown( wiki, notebook_id, invoker, editor ) {
);
appendChildNodes( this.div, createDOM( "div", {}, this.startup_checkbox, this.startup_label ) );
this.unformat_checkbox = createDOM( "input", { "type": "checkbox", "class": "pulldown_checkbox invisible" } );
this.unformat_link = createDOM( "a",
{
"href": "#",
"class": "pulldown_link",
"title": "Remove formatting from the currently selected text."
},
"remove formatting"
);
appendChildNodes( this.div, createDOM( "div", {}, this.unformat_checkbox, this.unformat_link ) );
if ( !editor.empty() ) {
this.print_checkbox = createDOM( "input", { "type": "checkbox", "class": "pulldown_checkbox invisible" } );
this.print_link = createDOM( "a",
@ -3244,6 +3255,7 @@ function Tools_pulldown( wiki, notebook_id, invoker, editor ) {
var self = this;
connect( this.startup_checkbox, "onclick", function ( event ) { self.startup_clicked( event ); } );
connect( this.unformat_link, "onclick", function ( event ) { self.unformat_clicked( event ); } );
if ( this.print_link )
connect( this.print_link, "onclick", function ( event ) { self.print_clicked( event ); } );
@ -3261,6 +3273,15 @@ Tools_pulldown.prototype.startup_clicked = function ( event ) {
this.wiki.save_editor( this.editor );
}
Tools_pulldown.prototype.unformat_clicked = function ( event ) {
event.stop();
if ( !this.editor.unformat_selection() )
this.wiki.display_message( "To remove the formatting from some text in this note, first highlight the text that you'd like to be unformatted." );
this.shutdown();
}
Tools_pulldown.prototype.print_clicked = function ( event ) {
this.wiki.editor_focused( null, true );
}