diff --git a/.gitignore b/.gitignore index e12f9cc..66d2135 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ node_modules .DS_Store -dist dist-demo dist-ssr *.local diff --git a/dist/favicon.ico b/dist/favicon.ico new file mode 100644 index 0000000..df36fcf Binary files /dev/null and b/dist/favicon.ico differ diff --git a/dist/vue-tribute.es.js b/dist/vue-tribute.es.js new file mode 100644 index 0000000..f2e41c6 --- /dev/null +++ b/dist/vue-tribute.es.js @@ -0,0 +1,79 @@ +var __defProp = Object.defineProperty; +var __getOwnPropSymbols = Object.getOwnPropertySymbols; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __propIsEnum = Object.prototype.propertyIsEnumerable; +var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; +var __spreadValues = (a, b) => { + for (var prop in b || (b = {})) + if (__hasOwnProp.call(b, prop)) + __defNormalProp(a, prop, b[prop]); + if (__getOwnPropSymbols) + for (var prop of __getOwnPropSymbols(b)) { + if (__propIsEnum.call(b, prop)) + __defNormalProp(a, prop, b[prop]); + } + return a; +}; +import { defineComponent, ref, onMounted, onBeforeUnmount, watch, nextTick, h, unref } from "vue"; +import Tribute from "tributejs"; +const VueTribute = defineComponent({ + name: "vue-tribute", + props: { + options: { + type: Object, + required: true + } + }, + setup(props, context) { + if (typeof Tribute === "undefined") { + throw new Error("[vue-tribute] cannot locate tributejs."); + } + const root = ref(); + const el = ref(); + const attachTribute = (el2, options = props.options) => { + if (!el2.value) + return; + let tribute = new Tribute(unref(options)); + tribute.attach(el2.value); + el2.value.tributeInstance = tribute; + }; + onMounted(() => { + var _a; + el.value = (_a = root.value) == null ? void 0 : _a.childNodes[0]; + if (!el) { + throw new Error("[vue-tribute] cannot find a suitable element to attach to."); + } + attachTribute(el); + el.value.addEventListener("tribute-replaced", (e) => { + var _a2; + (_a2 = e.target) == null ? void 0 : _a2.dispatchEvent(new Event("input", { bubbles: true })); + }); + }); + const detachTribute = (el2) => { + var _a; + if (!((_a = el2.value) == null ? void 0 : _a.tributeInstance)) + return; + el2.value.tributeInstance.detach(el2.value); + el2.value.tributeInstance = void 0; + delete el2.value.dataset.tribute; + }; + onBeforeUnmount(() => { + detachTribute(el); + }); + watch(() => props.options, async (newOptions) => { + var _a; + if ((_a = el.value) == null ? void 0 : _a.tributeInstance) { + await nextTick(); + detachTribute(el); + await nextTick(); + attachTribute(el, __spreadValues({}, newOptions)); + } + }, { deep: true }); + return () => h("div", { class: "v-tribute", ref: root }, [context.slots.default ? context.slots.default()[0] : null].filter(Boolean)); + } +}); +const install = (app) => { + app.component(VueTribute.name, VueTribute); +}; +VueTribute.install = install; +export { VueTribute, VueTribute as default }; diff --git a/dist/vue-tribute.umd.js b/dist/vue-tribute.umd.js new file mode 100644 index 0000000..b3e8462 --- /dev/null +++ b/dist/vue-tribute.umd.js @@ -0,0 +1 @@ +var w=Object.defineProperty;var p=Object.getOwnPropertySymbols;var y=Object.prototype.hasOwnProperty,j=Object.prototype.propertyIsEnumerable;var v=(t,e,u)=>e in t?w(t,e,{enumerable:!0,configurable:!0,writable:!0,value:u}):t[e]=u,h=(t,e)=>{for(var u in e||(e={}))y.call(e,u)&&v(t,u,e[u]);if(p)for(var u of p(e))j.call(e,u)&&v(t,u,e[u]);return t};(function(t,e){typeof exports=="object"&&typeof module!="undefined"?e(exports,require("vue"),require("tributejs")):typeof define=="function"&&define.amd?define(["exports","vue","tributejs"],e):(t=typeof globalThis!="undefined"?globalThis:t||self,e(t["vue-tribute"]={},t.Vue,t.Tribute))})(this,function(t,e,u){"use strict";function T(i){return i&&typeof i=="object"&&"default"in i?i:{default:i}}var d=T(u);const o=e.defineComponent({name:"vue-tribute",props:{options:{type:Object,required:!0}},setup(i,f){if(typeof d.default=="undefined")throw new Error("[vue-tribute] cannot locate tributejs.");const l=e.ref(),a=e.ref(),c=(n,r=i.options)=>{if(!n.value)return;let s=new d.default(e.unref(r));s.attach(n.value),n.value.tributeInstance=s};e.onMounted(()=>{var n;if(a.value=(n=l.value)==null?void 0:n.childNodes[0],!a)throw new Error("[vue-tribute] cannot find a suitable element to attach to.");c(a),a.value.addEventListener("tribute-replaced",r=>{var s;(s=r.target)==null||s.dispatchEvent(new Event("input",{bubbles:!0}))})});const b=n=>{var r;!((r=n.value)==null?void 0:r.tributeInstance)||(n.value.tributeInstance.detach(n.value),n.value.tributeInstance=void 0,delete n.value.dataset.tribute)};return e.onBeforeUnmount(()=>{b(a)}),e.watch(()=>i.options,async n=>{var r;((r=a.value)==null?void 0:r.tributeInstance)&&(await e.nextTick(),b(a),await e.nextTick(),c(a,h({},n)))},{deep:!0}),()=>e.h("div",{class:"v-tribute",ref:l},[f.slots.default?f.slots.default()[0]:null].filter(Boolean))}}),m=i=>{i.component(o.name,o)};o.install=m,t.VueTribute=o,t.default=o,Object.defineProperty(t,"__esModule",{value:!0}),t[Symbol.toStringTag]="Module"}); diff --git a/package.json b/package.json index 7d94321..4a5e8ac 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "scripts": { "dev": "vite", "start": "vite", - "build": "vue-tsc --noEmit && vite build", + "build": "vue-tsc --noEmit --skipLibCheck && vite build", "build:demo": "vite build --mode demo", "preview": "vite preview", "serve": "vite preview", diff --git a/tsconfig.json b/tsconfig.json index 336c32e..028d6f0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,5 +13,6 @@ "lib": ["esnext", "dom"], "types": ["node", "vitest/globals"] }, - "include": ["vite.config.ts", "env.d.ts", "lib/**/*.ts", "demo/**/*.ts", "demo/**/*.vue"] + "include": ["vite.config.ts", "env.d.ts", "lib/**/*.ts", "demo/**/*.ts", "demo/**/*.vue"], + "exclude": ["node_modules"] }