-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (46 loc) · 1.93 KB
/
index.js
File metadata and controls
56 lines (46 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import Widget from "./Widget.vue";
import { createApp, h } from "vue";
import {_createI18n, _createPinia} from "../plugins";
import { createHead } from '@unhead/vue/client'
let expose
function _buildApp(props) {
const app = createApp({ render: () => expose = h(Widget, props) })
.use(_createPinia())
.use(_createI18n());
app.use(createHead());
return app;
}
class ChatfaqWidget {
constructor(attrs) {
if (typeof attrs.element == "string")
attrs.element = document.querySelector(attrs.element)
this.element = attrs.element;
const props = { ...this.element.dataset, ...attrs }
this.app = _buildApp(props);
}
mount() {
this.app.mount(this.element)
}
}
// couldn't implement this: https://rimdev.io/vue-3-custom-elements cause shadow dom problems (Rollup does not include 'styles' inside the .ce.vue element)
// a possible solution is to use Vite istead of Rollup as such: https://maximomussini.com/posts/vue-custom-elements
// for the moment we just implemented: https://github.com/vuejs/vue-web-component-wrapper/issues/93#issuecomment-909136116
class ChatfaqWidgetCustomElement extends HTMLElement {
static get observedAttributes() {
return ['data-split-screen-iframe']; // Add any other attributes you want to observe
}
connectedCallback() {
this.app = _buildApp(this.dataset);
this.app.mount(this)
}
attributeChangedCallback(name, oldValue, newValue) {
if (!this.app) return; // Guard if app isn't mounted yet
// Convert the attribute name to a prop name (remove 'data-' prefix and convert to camelCase)
const propName = name.replace('data-', '')
.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
// Update the prop in the Vue app
window.testApp = this.app;
expose.component.props[propName] = newValue;
}
}
export { ChatfaqWidgetCustomElement, ChatfaqWidget };