forked from sxswdemo/github-book
-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for importing from Google Docs #75
Open
izak
wants to merge
14
commits into
master
Choose a base branch
from
izak-gdocs-refactor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ad07bef
stub
bnwest 7f66b09
first attempt to get gdoc importer into the Add drop down
bnwest 746a83f
stype google doc import drop down item
bnwest e36682a
namespace-ified google's js API
bnwest 03e7b11
call gdoc2html service and get back html
bnwest b465bfd
got _loadComplex() plumbing to work. imported html is diplayed in th…
bnwest 35f80dd
futz with this
bnwest d4dce81
made some but not of phil's suggested changes
bnwest cd905e2
reworked the promise nest using jquery 1.8 then()s.
bnwest d1ab8d6
moving console messages out of the promise nest, which now looks much…
bnwest 4c1a434
clean up code
philschatz 2a613ef
Fix breakage when context model is null.
izak 9905470
Distinguish between pickerbuilder and picker correctly.
izak 40eb482
Merge branch 'master' into izak-gdocs-refactor
izak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
define [ | ||
'underscore' | ||
'jquery' | ||
'backbone' | ||
'cs!models/content/module' | ||
'cs!gh-book/xhtml-file' | ||
'gh-book/googlejsapi' | ||
], (_, $, Backbone, ModuleModel, XhtmlModel) -> | ||
|
||
# Picker Reference: https://developers.google.com/picker/docs/reference | ||
|
||
GDOC_TO_HTML_URL = 'http://testing.oerpub.org/gdoc2html' # eventually `http://remix.oerpub.org/gdoc2html` | ||
gdocsURL = (id) -> "https://docs.google.com/document/d/#{id}/export?format=html&confirm=no_antivirus" | ||
|
||
# Opens a new Modal Dialog allowing the user to pick a Google Doc to import | ||
newPicker = () -> | ||
promise = $.Deferred() | ||
|
||
google.load 'picker', '1', | ||
callback: () => | ||
# Create a new Doc Picker Modal popup and re-ify the promise when | ||
# 1. a document is selected | ||
# 2. the dialog is canceled/closed | ||
builder = new google.picker.PickerBuilder() | ||
builder.addView(google.picker.ViewId.DOCUMENTS) | ||
builder.setCallback (data) -> | ||
switch data.action | ||
when google.picker.Action.PICKED then promise.resolve(data) | ||
when google.picker.Action.CANCEL then promise.reject('USER_CANCELLED') | ||
else | ||
promise.progress(data) | ||
picker = builder.build() | ||
picker.setVisible(true) | ||
return picker | ||
|
||
return promise.promise() | ||
|
||
# Retreive the HTML of the 1st Google Doc selected in the Picker | ||
getGoogleDocHtml = (data) -> | ||
resourceId = data.docs[0].id | ||
htmlUrl = gdocsURL(resourceId) | ||
promise = $.get(htmlUrl) | ||
return promise | ||
|
||
# Clean up HTML retrieved from Google to be used in the Editor. | ||
# Makes an AJAX call to a service that converts the HTML | ||
transformGoogleDocHtml = (html) -> | ||
promise = $.ajax | ||
url: GDOC_TO_HTML_URL | ||
type: 'POST' | ||
dataType: 'json' | ||
async: true | ||
data: | ||
html: html | ||
textbook_html: 0 | ||
copy_images: 0 | ||
|
||
return promise | ||
|
||
return class GoogleDocXhtmlModel extends XhtmlModel | ||
|
||
title: 'Google Document Import' | ||
|
||
# **NOTE:** The mediaType is inherited from XhtmlModel because a successful import will | ||
# 'appear' as a XHTML document. | ||
# This mediaType is used in the OPF manifest | ||
|
||
# In order to add this type to the Add dropdown for a Book (OPF File) | ||
# this model must have a unique mediaType (not `application/xhtml+xml`) | ||
# This is used to register with `media-types` and is in the | ||
# list of types `opf-file` accepts as a child (so it shows up in the filtered dropdown) | ||
uniqueMediaType: 'application/vnd.org.cnx.gdoc-import' | ||
|
||
# Saves the fetched and converted Document into this model for saving | ||
_injectHtml: (html) -> @set('body', html) # html is '<body>...</body>' | ||
|
||
# Pop up the Picker dialog when this Model is added to a book | ||
_loadComplex: (fetchPromise) -> | ||
# **NOTE:** `fetchPromise` is not used because this type can only be created as a new object | ||
# (the fetchPromise is already resolved) | ||
|
||
promise = newPicker() # 1. Open the picker dialog | ||
.then (data) => | ||
return getGoogleDocHtml(data) # 2. Get the HTML from Google | ||
.then (html) => | ||
return transformGoogleDocHtml(html) # 3. Send the HTML to the transform service | ||
.then (json) => | ||
@_injectHtml(json.html) # 4. Inject the cleaned HTML into the Model | ||
|
||
promise.fail => | ||
console.warn('BUG: Import failed (maybe the user canceled it) and there is no cleanup code') | ||
|
||
return promise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! I did the exact same thing locally, just forgot to commit it ; ) No wonder it worked for me.