2022-05-04 07:25:50 +03:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-06-05 00:07:06 +03:00
|
|
|
// @ts-check
|
|
|
|
|
2022-05-04 07:25:50 +03:00
|
|
|
// This file is injected into the registry as text, no dependencies are allowed.
|
|
|
|
|
2023-03-18 04:08:28 +03:00
|
|
|
import { detach as __pwDetach, insert as __pwInsert, noop as __pwNoop } from 'svelte/internal';
|
2022-08-29 19:11:51 +03:00
|
|
|
|
2023-04-20 01:32:33 +03:00
|
|
|
/** @typedef {import('../playwright-ct-core/types/component').Component} Component */
|
2023-04-26 04:08:34 +03:00
|
|
|
/** @typedef {import('../playwright-ct-core/types/component').ObjectComponent} ObjectComponent */
|
2022-06-05 00:07:06 +03:00
|
|
|
/** @typedef {any} FrameworkComponent */
|
2022-07-28 01:12:36 +03:00
|
|
|
/** @typedef {import('svelte').SvelteComponent} SvelteComponent */
|
2022-06-05 00:07:06 +03:00
|
|
|
|
2023-04-26 04:08:34 +03:00
|
|
|
/** @type {Map<string, () => Promise<FrameworkComponent>>} */
|
|
|
|
const __pwLoaderRegistry = new Map();
|
2022-06-05 00:07:06 +03:00
|
|
|
/** @type {Map<string, FrameworkComponent>} */
|
2023-03-18 04:08:28 +03:00
|
|
|
const __pwRegistry = new Map();
|
2022-05-04 07:25:50 +03:00
|
|
|
|
2022-06-05 00:07:06 +03:00
|
|
|
/**
|
2023-04-26 04:08:34 +03:00
|
|
|
* @param {{[key: string]: () => Promise<FrameworkComponent>}} components
|
2022-06-05 00:07:06 +03:00
|
|
|
*/
|
2023-03-18 04:08:28 +03:00
|
|
|
export function pwRegister(components) {
|
2022-05-04 07:25:50 +03:00
|
|
|
for (const [name, value] of Object.entries(components))
|
2023-04-26 04:08:34 +03:00
|
|
|
__pwLoaderRegistry.set(name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-27 00:57:49 +03:00
|
|
|
* @param {any} component
|
|
|
|
* @returns {component is ObjectComponent}
|
2023-04-26 04:08:34 +03:00
|
|
|
*/
|
|
|
|
function isComponent(component) {
|
|
|
|
return !(typeof component !== 'object' || Array.isArray(component));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-27 00:57:49 +03:00
|
|
|
* @param {ObjectComponent} component
|
2023-04-26 04:08:34 +03:00
|
|
|
*/
|
|
|
|
async function __pwResolveComponent(component) {
|
|
|
|
if (!isComponent(component))
|
2023-08-20 02:26:06 +03:00
|
|
|
return;
|
2023-04-26 04:08:34 +03:00
|
|
|
|
|
|
|
let componentFactory = __pwLoaderRegistry.get(component.type);
|
|
|
|
if (!componentFactory) {
|
|
|
|
// Lookup by shorthand.
|
|
|
|
for (const [name, value] of __pwLoaderRegistry) {
|
|
|
|
if (component.type.endsWith(`_${name}_svelte`)) {
|
|
|
|
componentFactory = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!componentFactory)
|
|
|
|
throw new Error(`Unregistered component: ${component.type}. Following components are registered: ${[...__pwRegistry.keys()]}`);
|
|
|
|
|
2023-08-20 02:26:06 +03:00
|
|
|
if (componentFactory)
|
2023-10-27 00:57:49 +03:00
|
|
|
__pwRegistry.set(component.type, await componentFactory());
|
2022-05-04 07:25:50 +03:00
|
|
|
}
|
|
|
|
|
2022-08-29 19:11:51 +03:00
|
|
|
/**
|
|
|
|
* TODO: remove this function when the following issue is fixed:
|
|
|
|
* https://github.com/sveltejs/svelte/issues/2588
|
|
|
|
*/
|
2023-03-18 04:08:28 +03:00
|
|
|
function __pwCreateSlots(slots) {
|
2022-08-29 19:11:51 +03:00
|
|
|
const svelteSlots = {};
|
|
|
|
|
|
|
|
for (const slotName in slots) {
|
|
|
|
const template = document
|
2023-08-20 02:26:06 +03:00
|
|
|
.createRange()
|
|
|
|
.createContextualFragment(slots[slotName]);
|
2022-08-29 19:11:51 +03:00
|
|
|
svelteSlots[slotName] = [createSlotFn(template)];
|
|
|
|
}
|
|
|
|
|
|
|
|
function createSlotFn(element) {
|
|
|
|
return function() {
|
|
|
|
return {
|
2023-03-18 04:08:28 +03:00
|
|
|
c: __pwNoop,
|
2022-08-29 19:11:51 +03:00
|
|
|
m: function mount(target, anchor) {
|
2023-03-18 04:08:28 +03:00
|
|
|
__pwInsert(target, element, anchor);
|
2022-08-29 19:11:51 +03:00
|
|
|
},
|
|
|
|
d: function destroy(detaching) {
|
2023-03-18 04:08:28 +03:00
|
|
|
if (detaching) __pwDetach(element);
|
2022-08-29 19:11:51 +03:00
|
|
|
},
|
2023-03-18 04:08:28 +03:00
|
|
|
l: __pwNoop,
|
2022-08-29 19:11:51 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return svelteSlots;
|
|
|
|
}
|
|
|
|
|
2023-03-18 04:08:28 +03:00
|
|
|
const __pwSvelteComponentKey = Symbol('svelteComponent');
|
2022-10-10 22:45:48 +03:00
|
|
|
|
2022-07-12 19:37:33 +03:00
|
|
|
window.playwrightMount = async (component, rootElement, hooksConfig) => {
|
2022-06-05 00:07:06 +03:00
|
|
|
if (component.kind !== 'object')
|
|
|
|
throw new Error('JSX mount notation is not supported');
|
|
|
|
|
2023-10-27 00:57:49 +03:00
|
|
|
await __pwResolveComponent(component);
|
|
|
|
const componentCtor = __pwRegistry.get(component.type);
|
|
|
|
|
2023-08-20 02:26:06 +03:00
|
|
|
class App extends componentCtor {
|
|
|
|
constructor(options = {}) {
|
|
|
|
super({
|
|
|
|
target: rootElement,
|
|
|
|
props: {
|
|
|
|
...component.options?.props,
|
|
|
|
$$slots: __pwCreateSlots(component.options?.slots),
|
|
|
|
$$scope: {},
|
|
|
|
},
|
|
|
|
...options
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-07-12 19:37:33 +03:00
|
|
|
|
2023-08-20 02:26:06 +03:00
|
|
|
let svelteComponent;
|
2023-02-09 19:58:15 +03:00
|
|
|
for (const hook of window.__pw_hooks_before_mount || [])
|
2023-08-20 02:26:06 +03:00
|
|
|
svelteComponent = await hook({ hooksConfig, App });
|
|
|
|
|
|
|
|
if (!svelteComponent)
|
|
|
|
svelteComponent = new App();
|
|
|
|
|
2023-03-18 04:08:28 +03:00
|
|
|
rootElement[__pwSvelteComponentKey] = svelteComponent;
|
2022-07-12 19:37:33 +03:00
|
|
|
|
2022-05-04 07:25:50 +03:00
|
|
|
for (const [key, listener] of Object.entries(component.options?.on || {}))
|
2022-07-28 01:12:36 +03:00
|
|
|
svelteComponent.$on(key, event => listener(event.detail));
|
2022-10-10 22:45:48 +03:00
|
|
|
|
2023-02-09 19:58:15 +03:00
|
|
|
for (const hook of window.__pw_hooks_after_mount || [])
|
2022-10-10 22:45:48 +03:00
|
|
|
await hook({ hooksConfig, svelteComponent });
|
2022-07-28 01:12:36 +03:00
|
|
|
};
|
|
|
|
|
2022-08-04 20:43:43 +03:00
|
|
|
window.playwrightUnmount = async rootElement => {
|
2023-03-18 04:08:28 +03:00
|
|
|
const svelteComponent = /** @type {SvelteComponent} */ (rootElement[__pwSvelteComponentKey]);
|
2022-07-28 01:12:36 +03:00
|
|
|
if (!svelteComponent)
|
|
|
|
throw new Error('Component was not mounted');
|
|
|
|
svelteComponent.$destroy();
|
2022-05-04 07:25:50 +03:00
|
|
|
};
|
2022-07-28 01:12:36 +03:00
|
|
|
|
2022-10-11 05:56:33 +03:00
|
|
|
window.playwrightUpdate = async (rootElement, component) => {
|
2023-10-27 00:57:49 +03:00
|
|
|
if (component.kind !== 'object')
|
|
|
|
throw new Error('JSX mount notation is not supported');
|
|
|
|
|
2023-04-26 04:08:34 +03:00
|
|
|
await __pwResolveComponent(component);
|
2023-03-18 04:08:28 +03:00
|
|
|
const svelteComponent = /** @type {SvelteComponent} */ (rootElement[__pwSvelteComponentKey]);
|
2022-10-10 22:45:48 +03:00
|
|
|
if (!svelteComponent)
|
|
|
|
throw new Error('Component was not mounted');
|
|
|
|
|
|
|
|
for (const [key, listener] of Object.entries(component.options?.on || {}))
|
|
|
|
svelteComponent.$on(key, event => listener(event.detail));
|
|
|
|
|
|
|
|
if (component.options?.props)
|
|
|
|
svelteComponent.$set(component.options.props);
|
|
|
|
};
|