(
component,
selector: string | HTMLElement,
option?: { shadowDom?: boolean; use?: (vue: App) => void },
)
| 2 | import VueDOMPurifyHTML from 'vue-dompurify-html'; |
| 3 | |
| 4 | export function createApp( |
| 5 | component, |
| 6 | selector: string | HTMLElement, |
| 7 | option?: { shadowDom?: boolean; use?: (vue: App) => void }, |
| 8 | ) { |
| 9 | const app = vueCreateApp(component); |
| 10 | app.use(VueDOMPurifyHTML, { |
| 11 | default: { ADD_ATTR: ['target'] }, |
| 12 | namedConfigurations: { |
| 13 | noMedia: { FORBID_TAGS: ['img', 'svg', 'picture', 'video', 'audio'] }, |
| 14 | }, |
| 15 | }); |
| 16 | |
| 17 | if (option && option.use) { |
| 18 | option.use(app); |
| 19 | } |
| 20 | |
| 21 | let rootElement = typeof selector === 'string' ? document.querySelector(selector) : selector; |
| 22 | |
| 23 | if (!rootElement) throw new Error("Can't find root element"); |
| 24 | |
| 25 | if (option && option.shadowDom) { |
| 26 | let shadowRoot: ShadowRoot; |
| 27 | if (rootElement.shadowRoot) { |
| 28 | shadowRoot = rootElement.shadowRoot; |
| 29 | shadowRoot.innerHTML = ''; |
| 30 | } else { |
| 31 | shadowRoot = rootElement.attachShadow({ mode: 'open' }); |
| 32 | } |
| 33 | |
| 34 | rootElement = shadowRoot.appendChild(document.createElement('div')); |
| 35 | } |
| 36 | |
| 37 | const loadedStyles: { [key: string]: boolean } = {}; |
| 38 | |
| 39 | app.mixin({ |
| 40 | beforeCreate() { |
| 41 | if (this.$options.styles && !loadedStyles[this.$options.__file]) { |
| 42 | rootElement!.appendChild( |
| 43 | createStyleTag(this.$options.styles.join('\n'), this.$options.__file), |
| 44 | ); |
| 45 | loadedStyles[this.$options.__file] = true; |
| 46 | } |
| 47 | }, |
| 48 | methods: { |
| 49 | lang: api.storage.lang, |
| 50 | getOption: value => api.settings.get(value), |
| 51 | }, |
| 52 | }); |
| 53 | |
| 54 | app.directive('visible', (el, binding) => { |
| 55 | el.style.visibility = binding.value ? 'visible' : 'hidden'; |
| 56 | }); |
| 57 | |
| 58 | const root = app.mount(rootElement); |
| 59 | |
| 60 | return root; |
| 61 | } |
no test coverage detected