Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quantum-kittens
GitHub Repository: quantum-kittens/platypus
Path: blob/main/frontend/vue/wc-wrapper/platypusWrapper.ts
3367 views
1
import { createApp, h, Component, CreateAppFunction, App } from 'vue'
2
3
import { Step, StepComponent } from '@mathigon/studio'
4
import { HTMLBaseView } from '@mathigon/boost'
5
import * as i18nPlugin from '../../plugins/i18nStrings'
6
import * as segmentPlugin from '../../plugins/segmentAnalytics'
7
import wrapper from './index'
8
9
declare module '@vue/runtime-core' {
10
interface ComponentCustomProperties {
11
$step: Step;
12
$trackClickEvent: (cta: string, location: string) => void;
13
$trackPage: (title: string) => void;
14
$translate: (str: string, args?: string[]) => string
15
}
16
}
17
18
interface ICustomElement extends HTMLElement {
19
_wrapper: App;
20
}
21
22
const createAppWithPlugins: CreateAppFunction<Element> = (rootComponent: Component, rootProps?: Record<string, unknown> | null) => {
23
return createApp(rootComponent, rootProps)
24
.use(segmentPlugin)
25
.use(i18nPlugin)
26
}
27
28
class VueWidgetView extends HTMLBaseView<ICustomElement> implements StepComponent {
29
setup ($step: Step /*, goal: string, initialData?: UserData | undefined */) {
30
const app = this._el._wrapper
31
app.config.globalProperties.$step = $step
32
}
33
}
34
35
function connectedCallback (): void {
36
// @ts-ignore "this" is the CustomElement from wrapper function.
37
const self: ICustomElement = this
38
39
// eslint-disable-next-line no-new
40
new VueWidgetView(self)
41
}
42
43
export default function wrap (component: Component) {
44
return wrapper(component, createAppWithPlugins, h, { connectedCallback })
45
}
46
47