From fd3d5edb66c2f750e893fed7a4beeaa8dcfcb3c6 Mon Sep 17 00:00:00 2001 From: Brian Granger Date: Thu, 26 Jan 2012 17:07:00 -0800 Subject: [PATCH] Finishing first draft of RST and heading cells. --- .../html/notebook/static/js/menubar.js | 18 ++++++ .../html/notebook/static/js/notebook.js | 61 +++++++++++++++++++ .../html/notebook/static/js/textcell.js | 23 +++++-- .../html/notebook/templates/notebook.html | 12 +++- 4 files changed, 107 insertions(+), 7 deletions(-) diff --git a/IPython/frontend/html/notebook/static/js/menubar.js b/IPython/frontend/html/notebook/static/js/menubar.js index cdeb1c504..ae5893d08 100644 --- a/IPython/frontend/html/notebook/static/js/menubar.js +++ b/IPython/frontend/html/notebook/static/js/menubar.js @@ -132,6 +132,24 @@ var IPython = (function (IPython) { this.element.find('#to_rst').click(function () { IPython.notebook.to_rst(); }); + this.element.find('#to_heading1').click(function () { + IPython.notebook.to_heading(undefined, 1); + }); + this.element.find('#to_heading2').click(function () { + IPython.notebook.to_heading(undefined, 2); + }); + this.element.find('#to_heading3').click(function () { + IPython.notebook.to_heading(undefined, 3); + }); + this.element.find('#to_heading4').click(function () { + IPython.notebook.to_heading(undefined, 4); + }); + this.element.find('#to_heading5').click(function () { + IPython.notebook.to_heading(undefined, 5); + }); + this.element.find('#to_heading6').click(function () { + IPython.notebook.to_heading(undefined, 6); + }); this.element.find('#toggle_output').click(function () { IPython.notebook.toggle_output(); }); diff --git a/IPython/frontend/html/notebook/static/js/notebook.js b/IPython/frontend/html/notebook/static/js/notebook.js index 9e0137da6..60277a5bf 100644 --- a/IPython/frontend/html/notebook/static/js/notebook.js +++ b/IPython/frontend/html/notebook/static/js/notebook.js @@ -140,6 +140,36 @@ var IPython = (function (IPython) { that.to_rst(); that.control_key_active = false; return false; + } else if (event.which === 49 && that.control_key_active) { + // To Heading 1 = 1 + that.to_heading(undefined, 1); + that.control_key_active = false; + return false; + } else if (event.which === 50 && that.control_key_active) { + // To Heading 2 = 2 + that.to_heading(undefined, 2); + that.control_key_active = false; + return false; + } else if (event.which === 51 && that.control_key_active) { + // To Heading 3 = 3 + that.to_heading(undefined, 3); + that.control_key_active = false; + return false; + } else if (event.which === 52 && that.control_key_active) { + // To Heading 4 = 4 + that.to_heading(undefined, 4); + that.control_key_active = false; + return false; + } else if (event.which === 53 && that.control_key_active) { + // To Heading 5 = 5 + that.to_heading(undefined, 5); + that.control_key_active = false; + return false; + } else if (event.which === 54 && that.control_key_active) { + // To Heading 6 = 6 + that.to_heading(undefined, 6); + that.control_key_active = false; + return false; } else if (event.which === 84 && that.control_key_active) { // Toggle output = t that.toggle_output(); @@ -483,6 +513,8 @@ var IPython = (function (IPython) { cell = new IPython.HTMLCell(this); } else if (type === 'rst') { cell = new IPython.RSTCell(this); + } else if (type === 'heading') { + cell = new IPython.HeadingCell(this); }; if (cell !== null) { if (this.ncells() === 0) { @@ -515,6 +547,8 @@ var IPython = (function (IPython) { cell = new IPython.HTMLCell(this); } else if (type === 'rst') { cell = new IPython.RSTCell(this); + } else if (type === 'heading') { + cell = new IPython.HeadingCell(this); }; if (cell !== null) { if (this.ncells() === 0) { @@ -615,6 +649,33 @@ var IPython = (function (IPython) { }; }; + + Notebook.prototype.to_heading = function (index, level) { + level = level || 1; + var i = this.index_or_selected(index); + if (this.is_valid_cell_index(i)) { + var source_element = this.get_cell_element(i); + var source_cell = source_element.data("cell"); + var target_cell = null; + if (source_cell instanceof IPython.HeadingCell) { + source_cell.set_level(level); + } else { + target_cell = this.insert_cell_below('heading',i); + var text = source_cell.get_text(); + if (text === source_cell.placeholder) { + text = ''; + }; + // The edit must come before the set_text. + target_cell.set_level(level); + target_cell.edit(); + target_cell.set_text(text); + source_element.remove(); + this.dirty = true; + }; + }; + }; + + // Cut/Copy/Paste Notebook.prototype.enable_paste = function () { diff --git a/IPython/frontend/html/notebook/static/js/textcell.js b/IPython/frontend/html/notebook/static/js/textcell.js index 570be4e3a..984328f20 100644 --- a/IPython/frontend/html/notebook/static/js/textcell.js +++ b/IPython/frontend/html/notebook/static/js/textcell.js @@ -299,17 +299,31 @@ var IPython = (function (IPython) { HeadingCell.prototype = new TextCell(); + HeadingCell.prototype.set_level = function (level) { + this.level = level; + if (this.rendered) { + this.rendered = false; + this.render(); + }; + }; + + + HeadingCell.prototype.get_level = function () { + return this.level; + }; + + HeadingCell.prototype.set_rendered = function (text) { var r = this.element.find("div.text_cell_render"); r.empty(); - r.append($('

').html(text)); - } + r.append($('').html(text)); + }; HeadingCell.prototype.get_rendered = function () { var r = this.element.find("div.text_cell_render"); return r.children().first().html(); - } + }; HeadingCell.prototype.render = function () { @@ -321,13 +335,14 @@ var IPython = (function (IPython) { this.element.find('div.text_cell_input').hide(); this.element.find("div.text_cell_render").show(); this.rendered = true; - } + }; }; IPython.TextCell = TextCell; IPython.HTMLCell = HTMLCell; IPython.MarkdownCell = MarkdownCell; IPython.RSTCell = RSTCell; + IPython.HeadingCell = HeadingCell; return IPython; diff --git a/IPython/frontend/html/notebook/templates/notebook.html b/IPython/frontend/html/notebook/templates/notebook.html index ab215f054..b9e25eedc 100644 --- a/IPython/frontend/html/notebook/templates/notebook.html +++ b/IPython/frontend/html/notebook/templates/notebook.html @@ -117,9 +117,15 @@
  • Run in Place
  • Run All

  • -
  • Code Cell
  • -
  • Markdown Cell
  • -
  • RST Cell
  • +
  • Code
  • +
  • Markdown
  • +
  • RST
  • +
  • Heading 1
  • +
  • Heading 2
  • +
  • Heading 3
  • +
  • Heading 4
  • +
  • Heading 5
  • +
  • Heading 6

  • Toggle Output
  • Clear All Output