mirror of
https://github.com/atom/atom.git
synced 2026-01-23 22:08:08 -05:00
Merge pull request #844 from atom/custom-resource-loader
Add custom resource loader, fixes #197
This commit is contained in:
@@ -214,6 +214,19 @@ your grammar supports:
|
||||
]
|
||||
```
|
||||
|
||||
## Bundle External Resources
|
||||
|
||||
It's common to ship external resources like images and fonts in the package, to
|
||||
make it easy to reference the resources in HTML or CSS, you can use the `atom`
|
||||
protocol URLs to load resources in the package.
|
||||
|
||||
The URLs should be in the format of
|
||||
`atom://package-name/relative-path-to-package-of-resource`, for example, the
|
||||
`atom://image-view/images/transparent-background.gif` would be equivablent to
|
||||
`~/.atom/packages/image-view/images/transparent-background.gif`.
|
||||
|
||||
You can also use the `atom` protocol URLs in themes.
|
||||
|
||||
## Writing Tests
|
||||
|
||||
Your package **should** have tests, and if they're placed in the _spec_ directory,
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"bugs": {
|
||||
"url": "https://github.com/atom/atom/issues"
|
||||
},
|
||||
"atomShellVersion": "0.4.5",
|
||||
"atomShellVersion": "0.4.7",
|
||||
"dependencies": {
|
||||
"async": "0.2.6",
|
||||
"coffee-script": "1.6.2",
|
||||
|
||||
15
spec/atom-protocol-handler-spec.coffee
Normal file
15
spec/atom-protocol-handler-spec.coffee
Normal file
@@ -0,0 +1,15 @@
|
||||
$ = require 'jquery'
|
||||
|
||||
describe '"atom" protocol URL', ->
|
||||
it 'sends the file relative in the package as response', ->
|
||||
called = false
|
||||
callback = -> called = true
|
||||
$.ajax
|
||||
url: 'atom://async/package.json'
|
||||
success: callback
|
||||
# In old versions of jQuery, ajax calls to custom protocol would always
|
||||
# be treated as error eventhough the browser thinks it's a success
|
||||
# request.
|
||||
error: callback
|
||||
|
||||
waitsFor 'request to be done', -> called is true
|
||||
@@ -1,5 +1,6 @@
|
||||
AtomWindow = require 'atom-window'
|
||||
ApplicationMenu = require 'application-menu'
|
||||
AtomProtocolHandler = require 'atom-protocol-handler'
|
||||
BrowserWindow = require 'browser-window'
|
||||
Menu = require 'menu'
|
||||
autoUpdater = require 'auto-updater'
|
||||
@@ -45,6 +46,7 @@ class AtomApplication
|
||||
|
||||
windows: null
|
||||
applicationMenu: null
|
||||
atomProtocolHandler: null
|
||||
resourcePath: null
|
||||
version: null
|
||||
|
||||
@@ -56,6 +58,7 @@ class AtomApplication
|
||||
@windows = []
|
||||
|
||||
@applicationMenu = new ApplicationMenu(@version, devMode)
|
||||
@atomProtocolHandler = new AtomProtocolHandler(@resourcePath)
|
||||
|
||||
@listenForArgumentsFromNewProcess()
|
||||
@setupJavaScriptArguments()
|
||||
|
||||
27
src/atom-protocol-handler.coffee
Normal file
27
src/atom-protocol-handler.coffee
Normal file
@@ -0,0 +1,27 @@
|
||||
app = require 'app'
|
||||
fs = require 'fs'
|
||||
path = require 'path'
|
||||
protocol = require 'protocol'
|
||||
|
||||
# Private: Handles requests with 'atom' protocol.
|
||||
#
|
||||
# It's created by {AtomApplication} upon instantiation, and is used to create a
|
||||
# custom resource loader by adding the 'atom' custom protocol.
|
||||
module.exports =
|
||||
class AtomProtocolHandler
|
||||
constructor: (@resourcePath) ->
|
||||
@loadPaths = [
|
||||
path.join(@resourcePath, 'node_modules')
|
||||
path.join(app.getHomeDir(), '.atom', 'packages')
|
||||
]
|
||||
|
||||
@registerAtomProtocol()
|
||||
|
||||
# Private: Creates the 'atom' custom protocol handler.
|
||||
registerAtomProtocol: ->
|
||||
protocol.registerProtocol 'atom', (request) =>
|
||||
relativePath = path.normalize(request.url.substr(7))
|
||||
for loadPath in @loadPaths
|
||||
filePath = path.join(loadPath, relativePath)
|
||||
break if fs.statSyncNoException(filePath)?
|
||||
return new protocol.RequestFileJob(filePath)
|
||||
Reference in New Issue
Block a user