diff --git a/NEWS b/NEWS index e1c16ac..9162caf 100644 --- a/NEWS +++ b/NEWS @@ -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. diff --git a/static/js/Editor.js b/static/js/Editor.js index 29dfae5..37a45a6 100644 --- a/static/js/Editor.js +++ b/static/js/Editor.js @@ -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 (, , etc.) + this.exec_command( "removeformat" ); + + // then, remove all other "formatting" that removeformat doesn't touch (

,

, 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; diff --git a/static/js/Wiki.js b/static/js/Wiki.js index 0b08995..5351be0 100644 --- a/static/js/Wiki.js +++ b/static/js/Wiki.js @@ -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 ); }