UNPKG

5.14 kBJavaScriptView Raw
1function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
2
3function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
5/***************************************************************************************
6 * (c) 2017 Adobe. All rights reserved.
7 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License. You may obtain a copy
9 * of the License at http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software distributed under
12 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
13 * OF ANY KIND, either express or implied. See the License for the specific language
14 * governing permissions and limitations under the License.
15 ****************************************************************************************/
16import Penpal from 'penpal';
17import Logger from './utils/logger';
18import addStylesToPage from './utils/addStylesToPage';
19const STYLES = "\n html, body {\n background-color: transparent !important;\n }\n";
20addStylesToPage(STYLES);
21const logger = new Logger('ExtensionBridge:Child');
22let extensionViewMethods = {};
23let connectionPromise;
24
25const getExtensionViewMethod = methodName => {
26 const method = extensionViewMethods[methodName];
27
28 if (method) {
29 return method.bind(extensionViewMethods);
30 } else {
31 throw new Error("Unable to call ".concat(methodName, " on the extension. The extension must register a ").concat(methodName, " function using extensionBridge.register()."));
32 }
33};
34
35const init = function init() {
36 getExtensionViewMethod('init')(...arguments);
37};
38
39const validate = function validate() {
40 return Promise.resolve(getExtensionViewMethod('validate')(...arguments)).then(result => {
41 if (typeof result !== 'boolean') {
42 throw new Error("The extension attempted to return a non-boolean value from validate: ".concat(result));
43 }
44
45 return result;
46 });
47};
48
49const getSettings = function getSettings() {
50 return Promise.resolve(getExtensionViewMethod('getSettings')(...arguments)).then(result => {
51 if (typeof result !== 'object') {
52 throw new Error('The extension attempted to return a non-object value from getSettings: ' + result);
53 }
54
55 return result;
56 });
57};
58
59const wrapOpenSharedViewMethod = (methodName, sharedViewName) => function () {
60 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
61 args[_key] = arguments[_key];
62 }
63
64 let callback;
65
66 if (typeof args[0] === 'function') {
67 callback = args.shift(); // Deprecated April 10, 2018. 30+ extensions were using a callback at the time.
68
69 console.warn('Passing a callback to extensionBridge.' + methodName + '() has been deprecated. ' + 'The method now returns a promise that should be used instead.');
70 }
71
72 return connectionPromise.then(parent => {
73 if (parent[methodName]) {
74 return parent[methodName](...args);
75 } else {
76 throw new Error("An error occurred while opening ".concat(sharedViewName, ". The shared view is unavailable."));
77 }
78 }).then(result => {
79 if (callback) {
80 callback(result);
81 }
82
83 return result;
84 });
85};
86
87connectionPromise = Penpal.connectToParent({
88 methods: {
89 init,
90 validate,
91 getSettings
92 }
93}).promise;
94const extensionBridge = {
95 openCodeEditor: wrapOpenSharedViewMethod('openCodeEditor', 'code editor'),
96 openDataElementSelector: wrapOpenSharedViewMethod('openDataElementSelector', 'data element selector'),
97 openRegexTester: wrapOpenSharedViewMethod('openRegexTester', 'regex tester'),
98
99 register(methods) {
100 extensionViewMethods = _objectSpread({}, methods);
101 connectionPromise.then(parent => parent.extensionRegistered());
102 logger.log('Methods registered by extension.');
103 },
104
105 setDebug(value) {
106 Penpal.debug = value;
107 Logger.enabled = value;
108 }
109
110};
111window.addEventListener('focus', () => {
112 connectionPromise.then(parent => parent.markAsDirty());
113});
114
115const executeQueuedCall = call => {
116 // Not all of the extension bridge methods return promises. Rather than do a switch where we
117 // only handle promises coming from certain methods here, we'll just always convert the return
118 // value to a promise and them consistently.
119 Promise.resolve(extensionBridge[call.methodName](...call.args)).then(call.resolve, call.reject);
120};
121
122const callQueue = window.extensionBridge._callQueue;
123
124while (callQueue.length) {
125 executeQueuedCall(callQueue.shift());
126}
127
128callQueue.push = executeQueuedCall;
\No newline at end of file