diff --git a/usability/search-replace/icon.png b/usability/search-replace/icon.png new file mode 100644 index 0000000..ebe66aa Binary files /dev/null and b/usability/search-replace/icon.png differ diff --git a/usability/search-replace/main.css b/usability/search-replace/main.css new file mode 100644 index 0000000..b285266 --- /dev/null +++ b/usability/search-replace/main.css @@ -0,0 +1,26 @@ + + +#searchbar-wrapper { + position: absolute; + background-color: #ffffff; + border-style: solid; + border-radius: 5px; + border-color: #aaaaaa; + padding-left: 10px; + width:430px; + top: 100px; + right: 5px; + float: right; + opacity: 1; + z-index: 100; + font-size: 12px; + font-weight: bold; +} + +.searchbar_buttons { + height: 30px; +} + +.searchbar_input { + width: 200px; +} \ No newline at end of file diff --git a/usability/search-replace/main.js b/usability/search-replace/main.js new file mode 100755 index 0000000..7f665e0 --- /dev/null +++ b/usability/search-replace/main.js @@ -0,0 +1,217 @@ +// Simple search&replace extension based on a codemirror addon. +// Adds a search box to the notebook toolbar and selects search word if found. + +define([ + 'base/js/namespace', + 'jquery', + 'require', + 'codemirror/addon/search/search', + 'codemirror/addon/search/searchcursor' +], function(IPython, $, require) { + "use strict"; + + /** + * Search for a string within the complete notebook, starting at current cell or + * CodeMirror selection + * @param hotkey + * @returns {boolean} + */ + var search = function(hotkey) { + /* execute search operation only after pressing return key or button click */ + if (hotkey != 0 && hotkey != 13) { + return false; + } + + var cell = IPython.notebook.get_selected_cell(); + if (cell.rendered == true && cell.cell_type == "markdown" ) cell.unrender(); + var findString = $('#searchbar_search_text').val(); + var cur = cell.code_mirror.getCursor(); + + var case_sensitive = !$('#searchbar_case_sensitive').hasClass('active'); + var find = cell.code_mirror.getSearchCursor(findString,cur, case_sensitive); + if (find.find() == true) { + cell.code_mirror.setSelection(find.pos.from,find.pos.to); + cell.code_mirror.focus(); + } else { + var ncells = IPython.notebook.ncells(); + if ( IPython.notebook.get_selected_index()+1 == ncells) { + cell.code_mirror.setCursor({line:0, ch:0}); + cell.code_mirror.focus(); + } else { + IPython.notebook.select_next(); + IPython.notebook.edit_mode(); + cell.code_mirror.setCursor({line:0, ch:0}); + return search(hotkey); + } + } + }; + + /** + * Replace a string within the complete notebook, starting at current cell or + * CodeMirror selection + * @param hotkey + * @returns {boolean} + */ + var replace = function(hotkey) { + /* execute replace operation only after pressing return key or button click */ + if (hotkey != 0 && hotkey != 13) { + return false; + } + + var cell = IPython.notebook.get_selected_cell(); + if (cell.rendered == true && cell.cell_type == "markdown" ) cell.unrender(); + + var findString = $('#searchbar_search_text').val(); + var replaceString = $('#searchbar_replace_text').val(); + var cur = cell.code_mirror.getCursor(); + + var case_sensitive = !$('#searchbar_case_sensitive').hasClass('active'); + var find = cell.code_mirror.getSearchCursor(findString,cur, case_sensitive); + if (find.find() == true) { + cell.code_mirror.setSelection(find.pos.from,find.pos.to); + cell.code_mirror.replaceSelection(replaceString); + cell.code_mirror.focus(); + } else { + var ncells = IPython.notebook.ncells(); + if ( IPython.notebook.get_selected_index()+1 == ncells) { + cell.code_mirror.setCursor({line:0, ch:0}); + cell.code_mirror.focus(); + } else { + IPython.notebook.select_next(); + IPython.notebook.edit_mode(); + cell.code_mirror.setCursor({line:0, ch:0}); + return replace(hotkey); + } + } + }; + + /** + * Create floating toolbar + * + */ + var create_searchbar_div = function () { + var btn = '
'; + + var searchbar_wrapper = $('