gigantic ass reorg. let me know if this breaks.

This commit is contained in:
Chris Wanstrath
2011-08-27 03:48:00 -07:00
parent 6708f52280
commit 409a77c485
250 changed files with 221 additions and 180 deletions

152
src/atomicity.coffee Normal file
View File

@@ -0,0 +1,152 @@
# nice!
{Chrome, File, Process, Dir} = require 'osx'
ace = require 'ace/ace'
canon = require 'pilot/canon'
$ = require 'jquery'
{CoffeeScript} = require 'coffee-script'
Chrome.addPane 'main', '<div id="editor"></div>'
editor = ace.edit "editor"
editor.setTheme require "ace/theme/twilight"
JavaScriptMode = require("ace/mode/javascript").Mode
CoffeeMode = require("ace/mode/coffee").Mode
HTMLMode = require("ace/mode/html").Mode
editor.getSession().setMode new JavaScriptMode
editor.getSession().setUseSoftTabs true
editor.getSession().setTabSize 2
editor.focus()
# fuuuuu, ui bug
setTimeout ->
editor.resize()
, 50
if css = File.read "~/.atomicity/twilight.css"
head = $('head')[0]
style = document.createElement 'style'
rules = document.createTextNode css
style.type = 'text/css'
style.appendChild rules
head.appendChild style
_.map Dir.list("~/.atomicity/"), (path) ->
if /\.js$/.test path
$.getScript path
else if /\.coffee/.test path
eval CoffeeScript.compile File.read path
filename = null
editor.getSession().on 'change', ->
Chrome.setDirty true
save = ->
File.write filename, editor.getSession().getValue()
setMode()
Chrome.setDirty false
open = ->
if /png|jpe?g|gif/i.test filename
Chrome.openURL filename
else
Chrome.title _.last filename.split('/')
editor.getSession().setValue File.read filename
setMode()
Chrome.setDirty false
setMode = ->
if /\.js$/.test filename
editor.getSession().setMode new JavaScriptMode
else if /\.coffee$/.test filename
editor.getSession().setMode new CoffeeMode
else if /\.html/.test filename
editor.getSession().setMode new HTMLMode
saveAs = ->
if file = Chrome.savePanel()
filename = file
Chrome.title _.last filename.split('/')
save()
bindKey = (name, shortcut, callback) ->
canon.addCommand
name: name
exec: callback
bindKey:
win: null
mac: shortcut
sender: 'editor'
bindKey 'open', 'Command-O', (env, args, request) ->
if file = Chrome.openPanel()
filename = file
open()
bindKey 'openURL', 'Command-Shift-O', (env, args, request) ->
if url = prompt "Enter URL:"
Chrome.openURL url
bindKey 'saveAs', 'Command-Shift-S', (env, args, request) ->
saveAs()
bindKey 'save', 'Command-S', (env, args, request) ->
if filename then save() else saveAs()
bindKey 'new', 'Command-N', (env, args, request) ->
Chrome.createWindow()
bindKey 'copy', 'Command-C', (env, args, request) ->
text = editor.getSession().doc.getTextRange editor.getSelectionRange()
Chrome.writeToPasteboard text
bindKey 'cut', 'Command-X', (env, args, request) ->
text = editor.getSession().doc.getTextRange editor.getSelectionRange()
Chrome.writeToPasteboard text
editor.session.remove editor.getSelectionRange()
bindKey 'eval', 'Command-R', (env, args, request) ->
eval env.editor.getSession().getValue()
# textmate
bindKey 'togglecomment', 'Command-/', (env) ->
env.editor.toggleCommentLines()
bindKey 'tmoutdent', 'Command-[', (env) ->
env.editor.blockOutdent()
bindKey 'tmindent', 'Command-]', (env) ->
env.editor.indent()
# emacs > you
bindKey 'moveforward', 'Alt-F', (env) ->
env.editor.navigateWordRight()
bindKey 'moveback', 'Alt-B', (env) ->
env.editor.navigateWordLeft()
bindKey 'deleteword', 'Alt-D', (env) ->
env.editor.removeWordRight()
bindKey 'selectwordright', 'Alt-B', (env) ->
env.editor.navigateWordLeft()
bindKey 'home', 'Alt-Shift-,', (env) ->
env.editor.navigateFileStart()
bindKey 'end', 'Alt-Shift-.', (env) ->
env.editor.navigateFileEnd()
bindKey 'fullscreen', 'Command-Shift-Return', (env) ->
Chrome.toggleFullscreen()
# HAX
# this should go in coffee.coffee or something
bindKey 'consolelog', 'Ctrl-L', (env) ->
env.editor.insert 'console.log ""'
env.editor.navigateLeft()
bindKey 'toggleProjectDrawer', 'Command-Ctrl-N', (env) ->
Project.toggle()

43
src/bootstrap.coffee Normal file
View File

@@ -0,0 +1,43 @@
# This file is the first thing loaded on startup.
console.log = (thing) -> OSX.NSLog thing.toString()
modules = {}
paths = ['src', 'vendor']
this.require = (file) ->
# hack for stupid requirejs
if file.indexOf('ace/requirejs/text!') > -1
file = file.replace 'ace/requirejs/text!', ''
text = true
return modules[file] if modules[file]
code = null
paths.forEach (path) ->
return code if code
root = OSX.NSBundle.mainBundle.resourcePath + "/#{path}"
fullpath = if text then "#{root}/#{file}" else "#{root}/#{file}.js"
code = OSX.NSString.stringWithContentsOfFile fullpath
if text
modules[file] = code.toString()
return modules[file]
exports = {}
module = exports: exports
src = "function define(cb){cb.call(this,require,exports)};"
src += """(function(exports, define, module){
#{code}
}).call(exports, exports, define, module);
"""
eval src
modules[file] = module.exports or exports
modules[file]
this.require.paths = paths
this.require.nameToUrl = (path) -> "#{path}.js"
this._ = require 'underscore'

26
src/document.coffee Normal file
View File

@@ -0,0 +1,26 @@
# Fires these events:
# - opened
# - saved
# - created
class Document
path: null
text: null
listeners: []
constructor: (@path, @text) ->
name: ->
_.last @path.split '/' if @path
save: ->
trigger 'saved'
open = ->
trigger 'opened'
on: (message, listener) ->
@listeners.push listener
trigger: (message, args...) ->
_.each @listeners, (listener) ->
listener.call args...

168
src/osx.coffee Normal file
View File

@@ -0,0 +1,168 @@
# This is the CoffeeScript API that wraps all of Cocoa.
$ = require 'jquery'
# Handles the UI chrome
Chrome =
addPane: (position, html) ->
verticalDiv = $('#app-vertical')
horizontalDiv = $('#app-horizontal')
el = document.createElement("div")
el.setAttribute('class', "pane " + position)
el.innerHTML = html
switch position
when 'top', 'main'
verticalDiv.prepend(el)
when 'left'
horizontalDiv.prepend(el)
when 'bottom'
verticalDiv.append(el)
when 'right'
horizontalDiv.append(el)
else
NSLog("I DON'T KNOW HOW TO DEAL WITH #{position}")
# path - Optional. The String path to the file to base it on.
createWindow: (path) ->
c = OSX.AtomWindowController.alloc.initWithWindowNibName "AtomWindow"
c.window
c.window.makeKeyAndOrderFront null
# Set the active window's dirty status.
setDirty: (bool) ->
Chrome.activeWindow().setDocumentEdited bool
# Returns a boolean
dirty: ->
Chrome.activeWindow().isDocumentEdited()
# Returns the active NSWindow object
activeWindow: ->
OSX.NSApplication.sharedApplication.keyWindow
# Returns null or a file path.
openPanel: ->
panel = OSX.NSOpenPanel.openPanel
panel.setCanChooseDirectories(true)
if panel.runModal isnt OSX.NSFileHandlingPanelOKButton
return null
panel.filenames.lastObject
# Returns null or a file path.
savePanel: ->
panel = OSX.NSSavePanel.savePanel
if panel.runModal isnt OSX.NSFileHandlingPanelOKButton
return null
panel.filenames.lastObject
writeToPasteboard: (text) ->
pb = OSX.NSPasteboard.generalPasteboard
pb.declareTypes_owner [OSX.NSStringPboardType], null
pb.setString_forType text, OSX.NSStringPboardType
openURL: (url) ->
window.location = url
Chrome.title _.last url.replace(/\/$/,'').split '/'
title: (text) ->
WindowController.window.title = text
toggleFullscreen: ->
if Chrome.fullscreen?
Chrome.leaveFullscreen()
else
Chrome.enterFullscreen()
leaveFullscreen: ->
Chrome.fullscreen = false
OSX.NSMenu.setMenuBarVisible not OSX.NSMenu.menuBarVisible
window = WindowController.window
enterFullscreen: ->
Chrome.fullscreen = true
OSX.NSMenu.setMenuBarVisible not OSX.NSMenu.menuBarVisible
window = WindowController.window
fullscreenWindow = OSX.NSWindow.alloc.
initWithContentRect_styleMask_backing_defer_screen(
window.contentRectForFrameRect(window.frame),
OSX.NSBorderlessWindowMask,
OSX.NSBackingStoreBuffered,
true,
window.screen)
contentView = window.contentView
window.setContentView OSX.NSView.alloc.init
fullscreenWindow.setHidesOnDeactivate true
fullscreenWindow.setLevel OSX.NSFloatingWindowLevel
fullscreenWindow.setContentView contentView
fullscreenWindow.setTitle window.title
fullscreenWindow.makeFirstResponder null
fullscreenWindow.makeKeyAndOrderFront null
frame = fullscreenWindow.frameRectForContentRect(fullscreenWindow.screen.frame)
fullscreenWindow.setFrame_display_animate frame, true, true
# Handles the file system
File =
read: (path) ->
OSX.NSString.stringWithContentsOfFile File.expand path
write: (path, contents) ->
str = OSX.NSString.stringWithString contents
str.writeToFile_atomically File.expand(path), true
expand: (path) ->
if /~/.test path
OSX.NSString.stringWithString(path).stringByExpandingTildeInPath
else
path
isFile: (path) ->
isDir = new outArgument
exists = OSX.NSFileManager.defaultManager.fileExistsAtPath_isDirectory(path, isDir)
exists and not isDir.valueOf()
Dir =
list: (path) ->
path = File.expand path
_.map OSX.NSFileManager.defaultManager.subpathsAtPath(path), (entry) ->
"#{path}/#{entry}"
isDir: (path) ->
isDir = new outArgument
exists = OSX.NSFileManager.defaultManager.fileExistsAtPath_isDirectory(path, isDir)
exists and isDir.valueOf()
Process =
cwd: (path) ->
if dir?
OSX.NSFileManager.defaultManager.changeCurrentDirectoryPath(path)
else
OSX.NSFileManager.defaultManager.currentDirectoryPath()
env: ->
OSX.NSProcess.processInfo.environment()
# Need to rename and move stuff like this
Project =
toggle: ->
frameset = top.document.getElementsByTagName("frameset")[0]
if @showing
frameset.removeChild(frameset.firstChild)
frameset.setAttribute('cols', '*')
else
frame = document.createElement("frame")
frame.src = 'project.html'
frameset.insertBefore(frame, frameset.firstChild)
frameset.setAttribute('cols', '25%, *')
@showing = not @showing
exports ?= this
exports.Chrome = Chrome
exports.File = File
exports.Dir = Dir
exports.Project = Project

13
src/project.coffee Normal file
View File

@@ -0,0 +1,13 @@
$ = require 'jquery'
$ ->
dir = OSX.NSFileManager.defaultManager.currentDirectoryPath
$('#cwd').text(dir)
files = Dir.list(dir)
files = _.map files, (file) ->
listItems = _.map files, (file) ->
file = file.replace(dir, "")
"<li>#{file}</li>"
$('#files').append(listItems.join('\n'))

31
src/tabs.coffee Normal file
View File

@@ -0,0 +1,31 @@
$ = require 'jquery'
{Chrome, File, Dir, Process} = require 'osx'
exports.show = ->
root = OSX.NSBundle.mainBundle.resourcePath + '/HTML/'
tabs = OSX.NSString.stringWithContentsOfFile "#{root}/tabs.html"
edit = OSX.NSString.stringWithContentsOfFile "#{root}/editor.html"
Chrome.addPane 'main', tabs.replace '<%= editor %>', edit
# events
$('#tabs ul li:not(.add) a').live 'click', ->
$('#tabs ul .active').removeClass()
$(this).parents('li').addClass 'active'
idx = $('#tabs ul a').index this
$('.content iframe').hide().eq(idx).show().focus()
false
$('#tabs .add a').click ->
$('#tabs ul .active').removeClass()
$('#tabs ul .add').before '<li><a href="#">untitled</a></li>'
$('.content iframe').hide()
$('.content').append '<iframe src="editor.html" width="100%" height="100%"></iframe>'
$('#tabs ul .add').prev().addClass 'active'
false