|  | 
|  | 1 | +import { | 
|  | 2 | + toVNodes, | 
|  | 3 | + camelize, | 
|  | 4 | + hyphenate, | 
|  | 5 | + callHooks, | 
|  | 6 | + getInitialProps, | 
|  | 7 | + createCustomEvent, | 
|  | 8 | + convertAttributeValue | 
|  | 9 | +} from './utils.js' | 
|  | 10 | + | 
|  | 11 | +export default function wrap (Vue, Component) { | 
|  | 12 | + const options = typeof Component === 'function' | 
|  | 13 | + ? Component.options | 
|  | 14 | + : Component | 
|  | 15 | + | 
|  | 16 | + // inject hook to proxy $emit to native DOM events | 
|  | 17 | + options.beforeCreate = [].concat(options.beforeCreate || []) | 
|  | 18 | + options.beforeCreate.unshift(function () { | 
|  | 19 | + const emit = this.$emit | 
|  | 20 | + this.$emit = (name, ...args) => { | 
|  | 21 | + this.$root.$options.customElement.dispatchEvent(createCustomEvent(name, args)) | 
|  | 22 | + return emit.call(this, name, ...args) | 
|  | 23 | + } | 
|  | 24 | + }) | 
|  | 25 | + | 
|  | 26 | + // extract props info | 
|  | 27 | + const propsList = Array.isArray(options.props) | 
|  | 28 | + ? options.props | 
|  | 29 | + : Object.keys(options.props || {}) | 
|  | 30 | + const hyphenatedPropsList = propsList.map(hyphenate) | 
|  | 31 | + const camelizedPropsList = propsList.map(camelize) | 
|  | 32 | + const originalPropsAsObject = Array.isArray(options.props) ? {} : options.props || {} | 
|  | 33 | + const camelizedPropsMap = camelizedPropsList.reduce((map, key, i) => { | 
|  | 34 | + map[key] = originalPropsAsObject[propsList[i]] | 
|  | 35 | + return map | 
|  | 36 | + }, {}) | 
|  | 37 | + | 
|  | 38 | + class CustomElement extends HTMLElement { | 
|  | 39 | + static get observedAttributes () { | 
|  | 40 | + return hyphenatedPropsList | 
|  | 41 | + } | 
|  | 42 | + | 
|  | 43 | + constructor () { | 
|  | 44 | + super() | 
|  | 45 | + const el = this | 
|  | 46 | + this._wrapper = new Vue({ | 
|  | 47 | + name: 'shadow-root', | 
|  | 48 | + customElement: this, | 
|  | 49 | + data () { | 
|  | 50 | + return { | 
|  | 51 | + props: getInitialProps(camelizedPropsList), | 
|  | 52 | + slotChildren: Object.freeze(toVNodes( | 
|  | 53 | + this.$createElement, | 
|  | 54 | + el.childNodes | 
|  | 55 | + )) | 
|  | 56 | + } | 
|  | 57 | + }, | 
|  | 58 | + render (h) { | 
|  | 59 | + return h(Component, { | 
|  | 60 | + ref: 'inner', | 
|  | 61 | + props: this.props | 
|  | 62 | + }, this.slotChildren) | 
|  | 63 | + } | 
|  | 64 | + }) | 
|  | 65 | + | 
|  | 66 | + // in Chrome, this.childNodes will be empty when connectedCallback | 
|  | 67 | + // is fired, so it's necessary to use a mutationObserver | 
|  | 68 | + const observer = new MutationObserver(() => { | 
|  | 69 | + this._wrapper.slotChildren = Object.freeze(toVNodes( | 
|  | 70 | + this._wrapper.$createElement, | 
|  | 71 | + this.childNodes | 
|  | 72 | + )) | 
|  | 73 | + }) | 
|  | 74 | + observer.observe(this, { | 
|  | 75 | + childList: true, | 
|  | 76 | + subtree: true, | 
|  | 77 | + characterData: true, | 
|  | 78 | + attributes: true | 
|  | 79 | + }) | 
|  | 80 | + } | 
|  | 81 | + | 
|  | 82 | + get vueComponent () { | 
|  | 83 | + return this._wrapper.$refs.inner | 
|  | 84 | + } | 
|  | 85 | + | 
|  | 86 | + connectedCallback () { | 
|  | 87 | + if (!this._wrapper._isMounted) { | 
|  | 88 | + this._shadowRoot = this.attachShadow({ mode: 'open' }) | 
|  | 89 | + this._wrapper.$options.shadowRoot = this._shadowRoot | 
|  | 90 | + this._wrapper.$mount() | 
|  | 91 | + // sync default props values to wrapper | 
|  | 92 | + for (const key of camelizedPropsList) { | 
|  | 93 | + this._wrapper.props[key] = this.vueComponent[key] | 
|  | 94 | + } | 
|  | 95 | + this._shadowRoot.appendChild(this._wrapper.$el) | 
|  | 96 | + } else { | 
|  | 97 | + callHooks(this.vueComponent, 'activated') | 
|  | 98 | + } | 
|  | 99 | + } | 
|  | 100 | + | 
|  | 101 | + disconnectedCallback () { | 
|  | 102 | + callHooks(this.vueComponent, 'deactivated') | 
|  | 103 | + } | 
|  | 104 | + | 
|  | 105 | + // watch attribute change and sync | 
|  | 106 | + attributeChangedCallback (attrName, oldVal, newVal) { | 
|  | 107 | + const camelized = camelize(attrName) | 
|  | 108 | + this._wrapper.props[camelized] = convertAttributeValue( | 
|  | 109 | + newVal, | 
|  | 110 | + attrName, | 
|  | 111 | + camelizedPropsMap[camelized] | 
|  | 112 | + ) | 
|  | 113 | + } | 
|  | 114 | + } | 
|  | 115 | + | 
|  | 116 | + // proxy props as Element properties | 
|  | 117 | + camelizedPropsList.forEach(key => { | 
|  | 118 | + Object.defineProperty(CustomElement.prototype, key, { | 
|  | 119 | + get () { | 
|  | 120 | + return this._wrapper.props[key] | 
|  | 121 | + }, | 
|  | 122 | + set (newVal) { | 
|  | 123 | + this._wrapper.props[key] = newVal | 
|  | 124 | + }, | 
|  | 125 | + enumerable: false, | 
|  | 126 | + configurable: true | 
|  | 127 | + }) | 
|  | 128 | + }) | 
|  | 129 | + | 
|  | 130 | + return CustomElement | 
|  | 131 | +} | 
0 commit comments