Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Assist.js: Add a shared FrameService to assistjs extension (#32784)
* Create the skeleton of the new extension for assist.js: amp-google-assistant-assistjs. It consists of one shared service and multiple custom elements. * Remove unneeded regenerator- runtime dependency in package.json * - Add/modify copyright statement and header comment for each file - Add appropriate validator rules for the new extension - Add more detailed description about what this extension does * Add a shared service(AssistjsConfigService) and a new config element. Also includes some minor refactoring. The service provides config to other components and the element take prespecified config json. * Fix styling and remove commented code in AssistjsFrameService. * Improve config service: - Ensure config json retrieval - Move widget iframe url creation into one single place. - Add assertion to ensure there's only one config element on the page Minor fixes: - Replace await usage with then to avoid extra dependencies. - Update validator rules for the invisible config element * Fix prettify errors. * Unit test fix and some minor fixes. * Fix lint errors. * Update extensions/amp-google-assistant-assistjs/0.1/assistjs-frame-service.js Co-authored-by: Justin Ridgewell <justin@ridgewell.name> * Minor fixes. * Fix errors caused by not having this.win in a service class(non-AMP.BaseElement). Also add a config service mock to the unit test. * ampdoc.getWin() is forbidden. Replace it with "window" directly. * - Append iframe to the document after frame src is retrieved - Parse config in config element to save a DOM query - Delete assistjs-frame-service.js. Add it in next PR with real usage. * Fix FrameService reference errors and lint errors. * Add a shared FrameService for all custom elements to handle requests from underlying iframes. Actual implementation of two endpoints: OpenMic and SendTextQuery is empty now and will be implemented in upcoming PRs. * Use ampdoc.whenready instead to fix the loading error. Add sandbox attributes to all iframes. * Update extensions/amp-google-assistant-assistjs/0.1/assistjs-frame-service.js Co-authored-by: Justin Ridgewell <justin@ridgewell.name> * Update extensions/amp-google-assistant-assistjs/0.1/assistjs-frame-service.js Co-authored-by: Justin Ridgewell <justin@ridgewell.name> Co-authored-by: Justin Ridgewell <justin@ridgewell.name>
- Loading branch information
1 parent
9597423
commit 476d99c
Showing
6 changed files
with
109 additions
and
0 deletions.
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
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
71 changes: 71 additions & 0 deletions
71
extensions/amp-google-assistant-assistjs/0.1/assistjs-frame-service.js
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,71 @@ | ||
/** | ||
* Copyright 2021 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @fileoverview The shared service used by all custom elements to talk to Assistant services. It loads an iframe that exports | ||
* endpoints to handle requests from custom elements. | ||
*/ | ||
|
||
import {Services} from '../../../src/services'; | ||
import {addAttributesToElement} from '../../../src/dom'; | ||
import {toggle} from '../../../src/style'; | ||
|
||
export class AssistjsFrameService { | ||
/** | ||
* @param {!../../../src/service/ampdoc-impl.AmpDoc} ampdoc | ||
*/ | ||
constructor(ampdoc) { | ||
/** @private @const {!../../../src/service/ampdoc-impl.AmpDoc} */ | ||
this.ampDoc_ = ampdoc; | ||
|
||
/** @private {?AssistjsConfigService} */ | ||
this.configService_ = null; | ||
|
||
/** Create Assistant iframe and append it to the main AMP document. */ | ||
this.createAssistantIframe_(); | ||
} | ||
|
||
/** @private */ | ||
createAssistantIframe_() { | ||
this.ampDoc_.whenFirstVisible().then(() => { | ||
this.configService_ = Services.assistjsConfigServiceForDoc(this.ampDoc_); | ||
const iframe = this.ampDoc_.win.document.createElement('iframe'); | ||
this.configService_.getWidgetIframeUrl('frame').then((iframeUrl) => { | ||
addAttributesToElement(iframe, { | ||
src: iframeUrl, | ||
allow: 'microphone', | ||
sandbox: 'allow-scripts', | ||
}); | ||
toggle(iframe, false); | ||
document.body.appendChild(iframe); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Activates Assistant microphone on 3P page. | ||
*/ | ||
openMic() { | ||
// TODO: add implementation once the channels for iframes are implemented. | ||
} | ||
|
||
/** | ||
* Sends text query to Assistant server. | ||
*/ | ||
sendTextQuery() { | ||
// TODO: add implementation once the channels for iframes are implemented. | ||
} | ||
} |
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