From 59e7229ea40de0321665a06057a7ff470dd3cb54 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 13 Sep 2013 10:41:54 +0800 Subject: [PATCH 1/6] Update to atom-shell v0.4.7. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 30ce8cba7..39df27962 100644 --- a/package.json +++ b/package.json @@ -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", From 656793108c869b6029217b4b05e75190ded4bac6 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 13 Sep 2013 16:24:24 +0800 Subject: [PATCH 2/6] Add the "atom://" custom protocol handler. --- src/atom-application.coffee | 3 +++ src/atom-protocol-handler.coffee | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 src/atom-protocol-handler.coffee diff --git a/src/atom-application.coffee b/src/atom-application.coffee index 54f2fa92b..507baccfc 100644 --- a/src/atom-application.coffee +++ b/src/atom-application.coffee @@ -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() diff --git a/src/atom-protocol-handler.coffee b/src/atom-protocol-handler.coffee new file mode 100644 index 000000000..e8fde85f1 --- /dev/null +++ b/src/atom-protocol-handler.coffee @@ -0,0 +1,18 @@ +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) -> + @registerAtomProtocol() + + # Private: Creates the 'atom' custom protocol handler. + registerAtomProtocol: -> + protocol.registerProtocol 'atom', (request) => + relativePath = path.normalize(request.url.substr(7)) + filePath = path.join(@resourcePath, 'node_modules', relativePath) + return new protocol.RequestFileJob(filePath) From 1b4be18dd721dfba793210dfca44a328f91916c5 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 13 Sep 2013 17:05:00 +0800 Subject: [PATCH 3/6] Add ~/.atom/packages to the resource load path. --- src/atom-protocol-handler.coffee | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/atom-protocol-handler.coffee b/src/atom-protocol-handler.coffee index e8fde85f1..d95bd30f1 100644 --- a/src/atom-protocol-handler.coffee +++ b/src/atom-protocol-handler.coffee @@ -1,3 +1,5 @@ +app = require 'app' +fs = require 'fs' path = require 'path' protocol = require 'protocol' @@ -8,11 +10,18 @@ protocol = require '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)) - filePath = path.join(@resourcePath, 'node_modules', relativePath) + for loadPath in @loadPaths + filePath = path.join(loadPath, relativePath) + break if fs.statSyncNoException(filePath)? return new protocol.RequestFileJob(filePath) From 0a8d9bdad55f63fe5d1c0e413bd3ba6c46d704c3 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sat, 14 Sep 2013 14:34:00 +0800 Subject: [PATCH 4/6] doc: Add usage on the 'atom' protocol URL. --- docs/creating-a-package.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/creating-a-package.md b/docs/creating-a-package.md index 75d47f5aa..167a73008 100644 --- a/docs/creating-a-package.md +++ b/docs/creating-a-package.md @@ -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, From c682a964faa10d29a5ff5b30c8ba8733763d7a2f Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sat, 14 Sep 2013 15:00:19 +0800 Subject: [PATCH 5/6] Add spec for the 'atom' protocol URLs. --- spec/atom-protocol-handler-spec.coffee | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 spec/atom-protocol-handler-spec.coffee diff --git a/spec/atom-protocol-handler-spec.coffee b/spec/atom-protocol-handler-spec.coffee new file mode 100644 index 000000000..75df979bb --- /dev/null +++ b/spec/atom-protocol-handler-spec.coffee @@ -0,0 +1,17 @@ +$ = require 'jquery' +fs = require 'fs' +path = require 'path' + +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 From 61f9cf059a4b5ab92b2a6ada8d2683cc31b57010 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 17 Sep 2013 00:05:09 +0800 Subject: [PATCH 6/6] :lipstick: Remove unused requires. --- spec/atom-protocol-handler-spec.coffee | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/spec/atom-protocol-handler-spec.coffee b/spec/atom-protocol-handler-spec.coffee index 75df979bb..218cfb810 100644 --- a/spec/atom-protocol-handler-spec.coffee +++ b/spec/atom-protocol-handler-spec.coffee @@ -1,6 +1,4 @@ $ = require 'jquery' -fs = require 'fs' -path = require 'path' describe '"atom" protocol URL', -> it 'sends the file relative in the package as response', -> @@ -11,7 +9,7 @@ describe '"atom" protocol URL', -> 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 + # request. error: callback waitsFor 'request to be done', -> called is true