1
- /**
2
- * Google Drive plugin Author: dave Date : 16/11/2014
1
+ /*
2
+ TinyMCE plugin for Google Drive.
3
+
4
+ Uses the front-end Google API Client (gapi) and Google sign-in (gsi) libraries,
5
+ via scripts imported in `header.jsp`.
3
6
*/
4
7
5
- var googleDriveEditor ;
6
- var googleDriveUrl ;
8
+ let accessToken ;
7
9
8
- function authorizeGoogleDriveLink ( ) {
9
- if ( typeof window . gapi === 'object' ) {
10
- window . gapi . auth . authorize ( {
11
- 'client_id' : gdClientId ,
12
- 'scope' : gdScope ,
13
- 'immediate' : false
14
- } , handleGoogleDriveAuthLinkResult ) ;
10
+ async function insertFromGoogleDrive ( ) {
11
+ if ( ! gdClientId || ! gdScope ) {
12
+ apprise ( "Missing required system properties for google drive "
13
+ + "integration. Please speak to your rspace administrator" ) ;
15
14
} else {
16
- apprise ( 'Unable to contact Google Drive. Please try again later or ensure that your network connection is active and reload the page.' ) ;
15
+ await gapi . load ( 'client:picker' ) ;
16
+
17
+ const client = await google . accounts . oauth2 . initTokenClient ( {
18
+ client_id : gdClientId ,
19
+ scope : gdScope ,
20
+ callback : onTokenResponse ,
21
+ error_callback : onTokenResponseError ,
22
+ } ) ;
23
+
24
+ if ( ! accessToken ) {
25
+ // show consent screen for user login and authorisation
26
+ client . requestAccessToken ( { prompt : 'consent' } ) ;
27
+ } else {
28
+ // skip consent screen when user has already consented and has a token
29
+ client . requestAccessToken ( { prompt : '' } ) ;
30
+ }
17
31
}
18
32
}
19
33
20
- function handleGoogleDriveAuthLinkResult ( authResult ) {
21
- if ( authResult && ! authResult . error ) {
22
- gdOauthToken = authResult . access_token ;
23
- createTinyMCEGoogleDrivePicker ( googleDrivePickerLinkCallback ) ;
24
- }
34
+ function onTokenResponse ( response ) {
35
+ accessToken = response . access_token ;
36
+ showPicker ( ) ;
25
37
}
26
38
27
- function createTinyMCEGoogleDrivePicker ( callback ) {
28
- if ( gdPickerApiLoaded && gdOauthToken ) {
29
- var picker = new google . picker . PickerBuilder ( )
30
- . addView ( google . picker . ViewId . DOCS )
31
- . setOAuthToken ( gdOauthToken )
32
- . setDeveloperKey ( gdDeveloperKey )
33
- . setCallback ( callback )
34
- . build ( ) ;
35
- picker . setVisible ( true ) ;
36
- }
39
+ function onTokenResponseError ( response ) {
40
+ apprise ( 'Unable to contact Google Drive. Please try again later or '
41
+ + 'ensure that your network connection is active and reload the page.' ) ;
37
42
}
38
43
39
- function googleDrivePickerLinkCallback ( data ) {
40
- var googleDriveLinkUrl = 'nothing' ;
41
- if ( data [ google . picker . Response . ACTION ] == google . picker . Action . PICKED ) {
42
- var doc = data [ google . picker . Response . DOCUMENTS ] [ 0 ] ;
43
- googleDriveLinkUrl = doc [ google . picker . Document . EMBEDDABLE_URL ] || doc [ google . picker . Document . URL ] ;
44
+ function showPicker ( ) {
45
+ picker = new google . picker . PickerBuilder ( )
46
+ . setDeveloperKey ( gdDeveloperKey )
47
+ . setAppId ( gdClientId )
48
+ . setOAuthToken ( accessToken )
49
+ . addView ( google . picker . ViewId . DOCS )
50
+ . setCallback ( pickerCallback )
51
+ . build ( ) ;
52
+ picker . setVisible ( true ) ;
53
+ }
44
54
45
- var name = doc [ google . picker . Document . NAME ] ;
55
+ /*
56
+ When a Google Drive file is picked, grab some metadata and embed a link in the
57
+ rspace document.
58
+ */
59
+ function pickerCallback ( data ) {
60
+ if ( data . action === google . picker . Action . PICKED ) {
61
+ const doc = data [ google . picker . Response . DOCUMENTS ] [ 0 ] ;
62
+ googleDriveLinkUrl = doc [ google . picker . Document . EMBEDDABLE_URL ] || doc [ google . picker . Document . URL ] ;
46
63
47
- var extension = RS . getFileExtension ( name ) ;
48
- var iconPath = RS . getIconPathForExtension ( extension ) ;
49
- var docId = doc . id ;
64
+ const name = doc [ google . picker . Document . NAME ] ;
65
+ const extension = RS . getFileExtension ( name ) ;
66
+ const iconPath = RS . getIconPathForExtension ( extension ) ;
67
+ const docId = doc . id ;
50
68
51
- var json = {
69
+ const json = {
52
70
id : 'googledrive-' + docId ,
53
71
fileStore : 'Google Drive' ,
54
72
recordURL : googleDriveLinkUrl ,
55
73
name : name ,
56
74
iconPath : iconPath ,
57
75
badgeIconPath : '/images/icons/drive_icon.png'
58
76
} ;
59
-
60
77
RS . insertTemplateIntoTinyMCE ( 'insertedExternalDocumentTemplate' , json ) ;
61
78
}
62
79
}
63
80
64
81
tinymce . PluginManager . add ( 'googledrive' , function ( editor , url ) {
65
-
66
- googleDriveEditor = editor ;
67
- googleDriveUrl = url ;
68
-
69
- // Add command to open the comment dialog.htm
70
82
editor . addCommand ( 'cmdGoogleDrive' , function ( ) {
71
- authorizeGoogleDriveLink ( ) ;
83
+ insertFromGoogleDrive ( ) ;
72
84
} ) ;
73
85
74
- // Add a button that opens a window
86
+ // adds editor button
75
87
editor . ui . registry . addButton ( 'googledrive' , {
76
88
tooltip : 'Insert from Google Drive' ,
77
89
icon : 'google_drive' ,
@@ -80,7 +92,7 @@ tinymce.PluginManager.add('googledrive', function (editor, url) {
80
92
}
81
93
} ) ;
82
94
83
- // Adds a menu item to the insert menu
95
+ // adds From Google Drive menu item
84
96
editor . ui . registry . addMenuItem ( 'optGoogleDrive' , {
85
97
text : 'From Google Drive' ,
86
98
icon : 'google_drive' ,
@@ -89,6 +101,7 @@ tinymce.PluginManager.add('googledrive', function (editor, url) {
89
101
}
90
102
} ) ;
91
103
104
+ // adds menu item to Insert menu
92
105
if ( ! window . insertActions ) window . insertActions = new Map ( ) ;
93
106
window . insertActions . set ( "optGoogleDrive" , {
94
107
text : 'From Google Drive' ,
0 commit comments