From 7ab473b0ce0f17289077211d09f50aa96e619bbb Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Mon, 7 Nov 2011 14:51:21 -0800 Subject: [PATCH] Browser and Editor are panes. I hate the pane HTML code now. --- extensions/tabs/tabs-pane.coffee | 16 +++++++---- extensions/tabs/tabs.coffee | 4 +++ index.html | 4 +-- src/browser.coffee | 33 ++++++++++++++-------- src/editor.coffee | 47 ++++++++++++++------------------ src/pane.coffee | 35 ++++++++++++++---------- src/window.coffee | 9 +++--- 7 files changed, 83 insertions(+), 65 deletions(-) diff --git a/extensions/tabs/tabs-pane.coffee b/extensions/tabs/tabs-pane.coffee index a4e8d3b13..1f17df8bf 100644 --- a/extensions/tabs/tabs-pane.coffee +++ b/extensions/tabs/tabs-pane.coffee @@ -2,6 +2,7 @@ $ = require 'jquery' _ = require 'underscore' Pane = require 'pane' +Browser = require 'browser' module.exports = class TabsPane extends Pane @@ -30,19 +31,24 @@ class TabsPane extends Pane switchToTab: (tab) -> tab = $("#tabs ul li").get(tab - 1) if _.isNumber tab return if tab.length is 0 - return if $(tab).is "#tabs ul .active" + return if $(tab).is ".active" path = $(tab).data 'path' $("#tabs ul .active").removeClass("active") $(tab).addClass 'active' - window.editor.focusBuffer path + window.open path addTab: (path) -> existing = $("#tabs [data-path='#{path}']") - if existing.length - return @switchToTab existing + return @switchToTab existing if existing.length + + name = if not path + "untitled" + else if Browser.isPathUrl path + path.match(/(\w+:\/\/)([^\/]+)?/)[2] + else + _.last path.split '/' - name = if path then _.last path.split '/' else "untitled" $("#tabs ul .active").removeClass() $("#tabs ul").append """
  • #{name}
  • diff --git a/extensions/tabs/tabs.coffee b/extensions/tabs/tabs.coffee index 17a6a3dc0..1c76c712b 100644 --- a/extensions/tabs/tabs.coffee +++ b/extensions/tabs/tabs.coffee @@ -27,6 +27,10 @@ class Tabs extends Extension path = e.details @pane.removeTab path + Event.on 'browser:focus', (e) => + path = e.details + @pane.addTab path + startup: -> @pane.show() for path, buffer of window.editor.buffers diff --git a/index.html b/index.html index 79344bf25..2914eb1a3 100644 --- a/index.html +++ b/index.html @@ -61,9 +61,7 @@
    -
    -
    -
    +
    diff --git a/src/browser.coffee b/src/browser.coffee index 77dad6804..f801710d0 100644 --- a/src/browser.coffee +++ b/src/browser.coffee @@ -1,19 +1,28 @@ $ = require 'jquery' Event = require 'event' +Pane = require 'pane' module.exports = -class Browser - constructor: (@path) -> - $('.main.pane').append @html().hide() +class Browser extends Pane + buffers: {} - Event.on "editor:bufferFocus", (e) => - @hide() if e.details isnt @path + html: $ "
    " - on: -> - html: -> - $ "" - show: -> - $(".main iframe[src='#{@path}']").show() - hide: -> - $(".main iframe[src='#{@path}']").hide() \ No newline at end of file + position: 'main' + + @isPathUrl: (path) -> + /^https?:\/\//.test path + + constructor: -> + Event.on "window:open", (e) => + path = e.details + return unless @constructor.isPathUrl path + + @buffers[path] ?= $ "" + + @html.html @buffers[path] + + @show() + + Event.trigger "browser:focus", path diff --git a/src/editor.coffee b/src/editor.coffee index e61979627..7f14c6a65 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -7,25 +7,31 @@ Event = require 'event' KeyBinder = require 'key-binder' Native = require 'native' Storage = require 'storage' -Browser = require 'browser' Pane = require 'pane' {EditSession} = require 'ace/edit_session' {UndoManager} = require 'ace/undomanager' module.exports = -class Editor +class Editor extends Pane activePath: null buffers: {} openPathsKey: "editor.openPaths.#{window.path}" + focusedPathKey: "editor.focusedPath.#{window.path}" - constructor: (path) -> + html: $ "
    " + + position: "main" + + constructor: -> KeyBinder.register "editor", @ - @ace = ace.edit "ace-editor" + @show() + + @ace = ace.edit 'ace-editor' # This stuff should all be grabbed from the .atomicity dir @ace.setTheme require "ace/theme/twilight" @@ -41,8 +47,6 @@ class Editor Event.on 'window:close', (e) => @removeBuffer e.details Event.on 'editor:bufferFocus', (e) => @resize() - @addBuffer path if path - # Resize editor when panes are added/removed el = document.body el.addEventListener 'DOMNodeInsertedIntoDocument', => @resize() @@ -89,22 +93,17 @@ class Editor buffer = @buffers[path] if not buffer - if fs.isFile path - code = if path then fs.read path else '' - buffer = new EditSession code - buffer.setUndoManager new UndoManager - buffer.setUseSoftTabs useSoftTabs = @usesSoftTabs code - buffer.setTabSize if useSoftTabs then @guessTabSize code else 8 - mode = @modeForPath path - buffer.setMode new mode if mode - else if /^https?:\/\//.test path - buffer = new Browser path - else - throw "#{@constructor.name}: I don't know what to do with `#{path}`" + code = if path then fs.read path else '' + buffer = new EditSession code + buffer.setUndoManager new UndoManager + buffer.setUseSoftTabs useSoftTabs = @usesSoftTabs code + buffer.setTabSize if useSoftTabs then @guessTabSize code else 8 + + mode = @modeForPath path + buffer.setMode new mode if mode @buffers[path] = buffer - openPaths = Storage.get @openPathsKey, [] unless path in openPaths openPaths.push path @@ -154,17 +153,13 @@ class Editor @ace.setSession new EditSession '' focusBuffer: (path) -> - return if not path or @activePath == path + return if not path + @show() @activePath = path buffer = @buffers[path] or @addBuffer path - if buffer.constructor is EditSession - $('#ace-editor').show() - @ace.setSession buffer - else - $('#ace-editor').hide() - buffer.show() + @ace.setSession buffer Storage.set @focusedPathKey, path Event.trigger "editor:bufferFocus", path diff --git a/src/pane.coffee b/src/pane.coffee index ce9388cb0..a7659b2c5 100644 --- a/src/pane.coffee +++ b/src/pane.coffee @@ -6,40 +6,45 @@ class Pane html: null - showing: false - add: -> verticalDiv = $('#app-vertical') horizontalDiv = $('#app-horizontal') - el = $ "
    " - el.addClass "pane " + @position - el.append @html + @pane = $ "
    " + @pane.addClass "pane " + @position + @pane.append @html switch @position when 'main' - $('.main').replaceWith el + # ICK: There can be multiple 'main' panes, but only one can be active + # at at time. + $('#main-container').children().css 'display', 'none !important' + $('#main-container').append @pane when 'top' - verticalDiv.prepend el + verticalDiv.prepend @pane when 'left' - horizontalDiv.prepend el + horizontalDiv.prepend @pane when 'bottom' - verticalDiv.append el + verticalDiv.append @pane when 'right' - horizontalDiv.append el + horizontalDiv.append @pane else throw "I DON'T KNOW HOW TO DEAL WITH #{@position}" + showing: -> + @pane and not @pane.css('display').match /none/ + show: -> - @add this - @showing = true + if @position == 'main' + $('#main-container').children().css 'display', 'none !important' + + if not @pane then @add() else @pane.css 'display', '-webkit-box !important' hide: -> - @html.parent().detach() - @showing = false + @pane.css 'display', 'none !important' toggle: -> - if @showing + if @showing() @hide() else @show() diff --git a/src/window.coffee b/src/window.coffee index 5a2cde64b..078c380c9 100644 --- a/src/window.coffee +++ b/src/window.coffee @@ -1,3 +1,4 @@ +Browser = require 'browser' Editor = require 'editor' Extension = require 'extension' Event = require 'event' @@ -13,6 +14,8 @@ _ = require 'underscore' windowAdditions = editor: null + browser: null + extensions: {} appRoot: OSX.NSBundle.mainBundle.resourcePath @@ -23,10 +26,8 @@ windowAdditions = @path = atomController.path @setTitle _.last @path.split '/' - @editor = if fs.isFile @path - new Editor @path - else - new Editor + @editor = new Editor + @browser = new Browser @loadExtensions() @loadKeyBindings()