@aevantec/litevue 0.5.6 → 0.5.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -6
- package/dist/litevue-devtools.iife.js +1 -1
- package/dist/litevue-devtools.mjs +1 -1
- package/dist/litevue-devtools.umd.js +1 -1
- package/dist/litevue-plugins.dev.iife.js +898 -0
- package/dist/litevue-plugins.dev.mjs +880 -0
- package/dist/litevue-plugins.iife.js +1 -1
- package/dist/litevue-plugins.mjs +1 -1
- package/dist/litevue-plugins.umd.js +1 -1
- package/dist/litevue.dev.iife.js +2193 -0
- package/dist/litevue.dev.mjs +2182 -0
- package/dist/litevue.iife.js +1 -1
- package/dist/litevue.mjs +1 -1
- package/dist/litevue.umd.js +1 -1
- package/dist/plugins/collapse.iife.js +1 -1
- package/dist/plugins/collapse.mjs +1 -1
- package/dist/plugins/collapse.umd.js +1 -1
- package/dist/plugins/media.dev.iife.js +249 -0
- package/dist/plugins/media.dev.mjs +243 -0
- package/dist/plugins/morph.dev.iife.js +217 -0
- package/dist/plugins/morph.dev.mjs +212 -0
- package/dist/plugins/persist.dev.iife.js +126 -0
- package/dist/plugins/persist.dev.mjs +120 -0
- package/dist/types/app.d.mts +7 -0
- package/dist/types/app.d.ts +7 -0
- package/dist/types/directives/bind.d.mts +1 -1
- package/dist/types/directives/bind.d.ts +1 -1
- package/dist/types/errors.d.mts +36 -0
- package/dist/types/errors.d.ts +36 -0
- package/dist/types/eval.d.mts +8 -2
- package/dist/types/eval.d.ts +8 -2
- package/dist/types/index.d.mts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/lazy.d.mts +15 -0
- package/dist/types/lazy.d.ts +15 -0
- package/dist/types/plugins/morph/index.d.mts +2 -1
- package/dist/types/plugins/morph/index.d.ts +2 -1
- package/package.json +10 -2
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
//#region src/plugins/morph/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* A `data-` attribute rather than `v-preserve`: an unrecognised `v-` attribute
|
|
4
|
+
* logs an unknown-directive error, and registering one would have the walker
|
|
5
|
+
* strip it, leaving nothing for the next morph. Matches `data-morph-skip`.
|
|
6
|
+
*/
|
|
7
|
+
var preserved = (node, opts) => node.nodeType === 1 && (node.hasAttribute("data-morph-preserve") || !!opts.preserve?.(node));
|
|
8
|
+
var dirRE = /^(?:v-|:|@)/;
|
|
9
|
+
var bindRE = /^(?::|v-bind:)/;
|
|
10
|
+
/**
|
|
11
|
+
* Attributes tried in order when no `key` option is given. All three express
|
|
12
|
+
* identity, so none repeat among siblings the way `name` does on a radio group
|
|
13
|
+
* — hence its absence. The attribute name is part of the key, so `id="5"` and
|
|
14
|
+
* `data-id="5"` stay distinct.
|
|
15
|
+
*/
|
|
16
|
+
var KEY_ATTRS = [
|
|
17
|
+
"id",
|
|
18
|
+
"data-key",
|
|
19
|
+
"data-id"
|
|
20
|
+
];
|
|
21
|
+
var defaultKey = (el) => {
|
|
22
|
+
for (const attr of KEY_ATTRS) {
|
|
23
|
+
const value = el.getAttribute(attr);
|
|
24
|
+
if (value != null) return attr + "=" + value;
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* The nearest stashed context, walking up from a live node. `v-scope` elements
|
|
30
|
+
* record theirs during walk, so inserted markup binds with the scope it landed
|
|
31
|
+
* in rather than the root.
|
|
32
|
+
*/
|
|
33
|
+
var nearestCtx = (node) => {
|
|
34
|
+
for (let el = node; el; el = el.parentElement) if (el.__ctx) return el.__ctx;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Attributes the client owns, read from the *incoming* markup — it still
|
|
38
|
+
* carries the directives the live DOM had stripped. `:class` there means the
|
|
39
|
+
* live `class` is bound and must survive the server's static value.
|
|
40
|
+
*/
|
|
41
|
+
var boundAttrs = (to) => {
|
|
42
|
+
const owned = /* @__PURE__ */ new Set();
|
|
43
|
+
for (const { name } of to.attributes) if (bindRE.test(name)) owned.add(name.replace(bindRE, "").split(".")[0]);
|
|
44
|
+
else if (name === "v-model") {
|
|
45
|
+
owned.add("value");
|
|
46
|
+
owned.add("checked");
|
|
47
|
+
}
|
|
48
|
+
return owned;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* `v-for` and `v-if` render into Blocks: the live DOM holds anchors plus the
|
|
52
|
+
* clones the data produced, while the server still sends one template. Matched
|
|
53
|
+
* positionally, the rest would be deleted as "no longer sent".
|
|
54
|
+
*/
|
|
55
|
+
var isBlockRoot = (el) => el.hasAttribute("v-for") || el.hasAttribute("v-if") || el.hasAttribute("v-else") || el.hasAttribute("v-else-if");
|
|
56
|
+
/** True when the client, not the server, decides this element's contents. */
|
|
57
|
+
var ownsChildren = (to) => {
|
|
58
|
+
if (to.hasAttribute("v-text") || to.hasAttribute("v-html") || to.hasAttribute("v-pre") || to.hasAttribute("v-once") || isBlockRoot(to)) return true;
|
|
59
|
+
for (let c = to.firstElementChild; c; c = c.nextElementSibling) if (isBlockRoot(c)) return true;
|
|
60
|
+
return false;
|
|
61
|
+
};
|
|
62
|
+
var patchAttrs = (from, to) => {
|
|
63
|
+
const owned = boundAttrs(to);
|
|
64
|
+
for (const { name, value } of [...to.attributes]) {
|
|
65
|
+
if (dirRE.test(name) || owned.has(name)) continue;
|
|
66
|
+
if (from.getAttribute(name) !== value) from.setAttribute(name, value);
|
|
67
|
+
}
|
|
68
|
+
for (const { name } of [...from.attributes]) {
|
|
69
|
+
if (owned.has(name) || to.hasAttribute(name)) continue;
|
|
70
|
+
from.removeAttribute(name);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Incoming text still holding `{{ }}` is rendered by a text directive on the
|
|
75
|
+
* live side, so the live value wins.
|
|
76
|
+
*/
|
|
77
|
+
var isInterpolation = (data, ctx) => data.includes(ctx ? ctx.delimiters[0] : "{{");
|
|
78
|
+
var patchNode = (from, to, ctx, opts) => {
|
|
79
|
+
if (from.nodeType !== to.nodeType) return replaceNode(from, to, ctx, opts);
|
|
80
|
+
if (from.nodeType === 3 || from.nodeType === 8) {
|
|
81
|
+
const data = to.data;
|
|
82
|
+
if (isInterpolation(data, ctx)) return;
|
|
83
|
+
if (from.data !== data) from.data = data;
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (from.nodeType !== 1) return;
|
|
87
|
+
const a = from;
|
|
88
|
+
const b = to;
|
|
89
|
+
if (a.tagName !== b.tagName) return replaceNode(from, to, ctx, opts);
|
|
90
|
+
if (opts.skip?.(a, b) || a.hasAttribute("data-morph-skip")) return;
|
|
91
|
+
patchAttrs(a, b);
|
|
92
|
+
if (!ownsChildren(b)) patchChildren(a, b, a.__ctx || ctx, opts);
|
|
93
|
+
};
|
|
94
|
+
var replaceNode = (from, to, ctx, opts) => {
|
|
95
|
+
if (preserved(from, opts)) return;
|
|
96
|
+
const fresh = to.cloneNode(true);
|
|
97
|
+
if (opts.beforeNodeAdded?.(fresh) === false) return;
|
|
98
|
+
ctx?.dispose?.(from);
|
|
99
|
+
from.parentNode.replaceChild(fresh, from);
|
|
100
|
+
opts.afterNodeRemoved?.(from);
|
|
101
|
+
mountNew(fresh, ctx);
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* Bind never-walked markup with the scope it was inserted into. The walker
|
|
105
|
+
* comes off the context, not an import, so this plugin doesn't pull a second
|
|
106
|
+
* copy of the core into the plugins bundle.
|
|
107
|
+
*/
|
|
108
|
+
var mountNew = (node, ctx) => {
|
|
109
|
+
const target = ctx || nearestCtx(node.parentNode);
|
|
110
|
+
target?.walk?.(node, target);
|
|
111
|
+
};
|
|
112
|
+
var patchChildren = (from, to, ctx, opts) => {
|
|
113
|
+
const key = opts.key || defaultKey;
|
|
114
|
+
const keyed = /* @__PURE__ */ new Map();
|
|
115
|
+
for (let n = from.firstChild; n; n = n.nextSibling) if (n.nodeType === 1) {
|
|
116
|
+
const k = key(n);
|
|
117
|
+
if (k == null) continue;
|
|
118
|
+
if (keyed.has(k)) console.warn(`[morph] duplicate key ${JSON.stringify(k)} among siblings; only the last is reusable. Keys must be unique within a parent.`);
|
|
119
|
+
keyed.set(k, n);
|
|
120
|
+
}
|
|
121
|
+
const keyOf = (n) => n && n.nodeType === 1 ? key(n) : null;
|
|
122
|
+
let oldChild = from.firstChild;
|
|
123
|
+
let newChild = to.firstChild;
|
|
124
|
+
const insert = (node) => {
|
|
125
|
+
const fresh = node.cloneNode(true);
|
|
126
|
+
if (opts.beforeNodeAdded?.(fresh) === false) return;
|
|
127
|
+
from.insertBefore(fresh, oldChild);
|
|
128
|
+
mountNew(fresh, ctx);
|
|
129
|
+
};
|
|
130
|
+
while (newChild) {
|
|
131
|
+
const nextNew = newChild.nextSibling;
|
|
132
|
+
const wantKey = keyOf(newChild);
|
|
133
|
+
if (wantKey != null) {
|
|
134
|
+
const match = keyed.get(wantKey);
|
|
135
|
+
if (match) {
|
|
136
|
+
if (match === oldChild) oldChild = oldChild.nextSibling;
|
|
137
|
+
else from.insertBefore(match, oldChild);
|
|
138
|
+
keyed.delete(wantKey);
|
|
139
|
+
patchNode(match, newChild, ctx, opts);
|
|
140
|
+
} else insert(newChild);
|
|
141
|
+
} else if (oldChild && keyOf(oldChild) == null) {
|
|
142
|
+
const nextOld = oldChild.nextSibling;
|
|
143
|
+
patchNode(oldChild, newChild, ctx, opts);
|
|
144
|
+
oldChild = nextOld;
|
|
145
|
+
} else insert(newChild);
|
|
146
|
+
newChild = nextNew;
|
|
147
|
+
}
|
|
148
|
+
while (oldChild) {
|
|
149
|
+
const next = oldChild.nextSibling;
|
|
150
|
+
if (!preserved(oldChild, opts)) {
|
|
151
|
+
ctx?.dispose?.(oldChild);
|
|
152
|
+
from.removeChild(oldChild);
|
|
153
|
+
opts.afterNodeRemoved?.(oldChild);
|
|
154
|
+
}
|
|
155
|
+
oldChild = next;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* morph(from, to) — patch `from` in place so it matches `to`, which may be an
|
|
160
|
+
* element or an HTML string for the *outer* element.
|
|
161
|
+
*/
|
|
162
|
+
var morph = (from, to, opts = {}) => {
|
|
163
|
+
let target;
|
|
164
|
+
if (typeof to === "string") {
|
|
165
|
+
const holder = document.createElement("template");
|
|
166
|
+
holder.innerHTML = to.trim();
|
|
167
|
+
const content = holder.content;
|
|
168
|
+
target = content.childElementCount === 1 ? content.firstElementChild : (() => {
|
|
169
|
+
const wrapper = from.cloneNode(false);
|
|
170
|
+
wrapper.appendChild(content);
|
|
171
|
+
return wrapper;
|
|
172
|
+
})();
|
|
173
|
+
} else target = to;
|
|
174
|
+
if (target) {
|
|
175
|
+
const restore = captureFocus();
|
|
176
|
+
patchNode(from, target, nearestCtx(from), opts);
|
|
177
|
+
restore();
|
|
178
|
+
}
|
|
179
|
+
return from;
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* `insertBefore` blurs the node it moves, so a reorder drops focus even though
|
|
183
|
+
* the element survived. Re-focus afterwards and restore the caret. Only
|
|
184
|
+
* observable in a real browser — jsdom does not blur on move.
|
|
185
|
+
*/
|
|
186
|
+
var captureFocus = () => {
|
|
187
|
+
const active = document.activeElement;
|
|
188
|
+
if (!active || active === document.body) return () => {};
|
|
189
|
+
const field = active;
|
|
190
|
+
let start = null;
|
|
191
|
+
let end = null;
|
|
192
|
+
try {
|
|
193
|
+
start = field.selectionStart;
|
|
194
|
+
end = field.selectionEnd;
|
|
195
|
+
} catch {}
|
|
196
|
+
return () => {
|
|
197
|
+
if (active === document.activeElement || !active.isConnected) return;
|
|
198
|
+
active.focus();
|
|
199
|
+
if (start !== null) try {
|
|
200
|
+
field.setSelectionRange(start, end);
|
|
201
|
+
} catch {}
|
|
202
|
+
};
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* Registers `$morph`, so a template can update a region from a fetch response
|
|
206
|
+
* without dropping into JavaScript.
|
|
207
|
+
*/
|
|
208
|
+
var morphPlugin = (app) => {
|
|
209
|
+
app.scope.$morph = morph;
|
|
210
|
+
};
|
|
211
|
+
//#endregion
|
|
212
|
+
export { morph, morphPlugin };
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
var LiteVuePersist = (function(exports, _aevantec_litevue) {
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
//#region src/plugins/persist/index.ts
|
|
4
|
+
var PREFIX = "litevue:";
|
|
5
|
+
/** Custom storages registered by name, usable as a directive modifier. */
|
|
6
|
+
var registry = Object.create(null);
|
|
7
|
+
var defaultStorage = "local";
|
|
8
|
+
var builtin = (name) => {
|
|
9
|
+
try {
|
|
10
|
+
if (name === "local" || name === "localStorage") return localStorage;
|
|
11
|
+
if (name === "session" || name === "sessionStorage") return sessionStorage;
|
|
12
|
+
} catch {}
|
|
13
|
+
};
|
|
14
|
+
var resolve = (storage) => {
|
|
15
|
+
const target = storage ?? defaultStorage;
|
|
16
|
+
if (typeof target !== "string") return target;
|
|
17
|
+
return registry[target] ?? builtin(target);
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Register a custom storage under a name, selectable anywhere — including as a
|
|
21
|
+
* directive modifier. Any object with getItem/setItem works.
|
|
22
|
+
*
|
|
23
|
+
* registerStorage('memory', myMemoryStorage);
|
|
24
|
+
* persistStore('scratch', { storage: 'memory' });
|
|
25
|
+
* <div v-persist.memory="scratch">…</div>
|
|
26
|
+
*/
|
|
27
|
+
var registerStorage = (name, storage) => {
|
|
28
|
+
registry[name] = storage;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Switch the storage used when none is specified. Accepts a registered name
|
|
32
|
+
* ('local', 'session', or your own) or a storage object.
|
|
33
|
+
*
|
|
34
|
+
* setDefaultStorage('session'); // everything defaults to sessionStorage
|
|
35
|
+
*/
|
|
36
|
+
var setDefaultStorage = (storage) => {
|
|
37
|
+
defaultStorage = storage;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Which own properties of `target` can round-trip through storage. Methods are
|
|
41
|
+
* behavior, not state; getter-only properties are derived and would throw on
|
|
42
|
+
* restore. An explicit list is filtered too, so naming a getter is a no-op.
|
|
43
|
+
*/
|
|
44
|
+
var persistable = (target, keys) => (keys ?? Object.keys(target)).filter((k) => {
|
|
45
|
+
if (k[0] === "$") return false;
|
|
46
|
+
const desc = Object.getOwnPropertyDescriptor(target, k);
|
|
47
|
+
if (desc && desc.get && !desc.set) return false;
|
|
48
|
+
const raw = desc && desc.value;
|
|
49
|
+
if (raw && raw.__v_isRef === true && raw.__v_isReadonly === true) return false;
|
|
50
|
+
return typeof target[k] !== "function";
|
|
51
|
+
});
|
|
52
|
+
/** Restore saved values, then keep storage in sync with every change. */
|
|
53
|
+
var sync = (target, key, keys, storage, run) => {
|
|
54
|
+
if (!storage) return run(() => {});
|
|
55
|
+
const fields = persistable(target, keys);
|
|
56
|
+
try {
|
|
57
|
+
const saved = JSON.parse(storage.getItem(key) || "null");
|
|
58
|
+
if (saved) {
|
|
59
|
+
for (const k of fields) if (k in saved) target[k] = saved[k];
|
|
60
|
+
}
|
|
61
|
+
} catch {}
|
|
62
|
+
return run(() => {
|
|
63
|
+
const snapshot = {};
|
|
64
|
+
for (const k of fields) snapshot[k] = target[k];
|
|
65
|
+
try {
|
|
66
|
+
storage.setItem(key, JSON.stringify(snapshot));
|
|
67
|
+
} catch {}
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* v-persist="storage-key" — syncs the element's scope to storage: restored on
|
|
72
|
+
* mount, written back on every change, nested values included.
|
|
73
|
+
*
|
|
74
|
+
* The attribute value is the storage key verbatim, not an expression, and
|
|
75
|
+
* falls back to the element id. An argument narrows which properties are
|
|
76
|
+
* stored; a modifier picks the storage (`local` default, `session`, or any
|
|
77
|
+
* name given to registerStorage()).
|
|
78
|
+
*
|
|
79
|
+
* v-persist:draft="composer" — only `draft`
|
|
80
|
+
* v-persist:draft,recipient="composer" — only those two
|
|
81
|
+
* v-persist:draft.session="composer" — and into sessionStorage
|
|
82
|
+
*/
|
|
83
|
+
var persist = (app) => {
|
|
84
|
+
app.directive("persist", ({ ctx, el, exp, arg, modifiers, effect }) => {
|
|
85
|
+
const key = PREFIX + (exp || el.id);
|
|
86
|
+
if (key === PREFIX) console.error(`v-persist needs a key: v-persist="my-key".`);
|
|
87
|
+
let storage;
|
|
88
|
+
for (const name in modifiers || {}) {
|
|
89
|
+
if (resolve(name)) {
|
|
90
|
+
storage = name;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
console.error(`v-persist: unknown storage ".${name}" — use .local, .session, or a name passed to registerStorage().`);
|
|
94
|
+
}
|
|
95
|
+
const keys = arg ? arg.split(",").map((k) => k.trim()).filter(Boolean) : void 0;
|
|
96
|
+
sync(ctx.scope, key, keys, resolve(storage), effect);
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* persistStore('cart') — the JS-side counterpart for global stores, which have
|
|
101
|
+
* no element to hang a directive on. Restores immediately, saves on change,
|
|
102
|
+
* and returns a stop function.
|
|
103
|
+
*
|
|
104
|
+
* persistStore('cart') // the whole store
|
|
105
|
+
* persistStore('cart', { keys: ['items'] }) // only these properties
|
|
106
|
+
* persistStore('cart', { key: 'v2:cart' }) // custom storage key
|
|
107
|
+
* persistStore('draft', { storage: 'session' }) // a different storage
|
|
108
|
+
* persistStore('scratch', { storage: myStorage }) // any getItem/setItem
|
|
109
|
+
*/
|
|
110
|
+
var persistStore = (name, options = {}) => {
|
|
111
|
+
const target = (0, _aevantec_litevue.store)(name);
|
|
112
|
+
if (!target) {
|
|
113
|
+
console.error(`persistStore("${name}") — no such store. Register it with store("${name}", { … }) first.`);
|
|
114
|
+
return () => {};
|
|
115
|
+
}
|
|
116
|
+
const storage = resolve(options.storage);
|
|
117
|
+
if (!storage) console.error(`persistStore("${name}") — storage "${String(options.storage)}" is not available.`);
|
|
118
|
+
return sync(target, PREFIX + (options.key ?? name), options.keys, storage, _aevantec_litevue.watchEffect);
|
|
119
|
+
};
|
|
120
|
+
//#endregion
|
|
121
|
+
exports.persist = persist;
|
|
122
|
+
exports.persistStore = persistStore;
|
|
123
|
+
exports.registerStorage = registerStorage;
|
|
124
|
+
exports.setDefaultStorage = setDefaultStorage;
|
|
125
|
+
return exports;
|
|
126
|
+
})({}, LiteVue);
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { store, watchEffect } from "@aevantec/litevue";
|
|
2
|
+
//#region src/plugins/persist/index.ts
|
|
3
|
+
var PREFIX = "litevue:";
|
|
4
|
+
/** Custom storages registered by name, usable as a directive modifier. */
|
|
5
|
+
var registry = Object.create(null);
|
|
6
|
+
var defaultStorage = "local";
|
|
7
|
+
var builtin = (name) => {
|
|
8
|
+
try {
|
|
9
|
+
if (name === "local" || name === "localStorage") return localStorage;
|
|
10
|
+
if (name === "session" || name === "sessionStorage") return sessionStorage;
|
|
11
|
+
} catch {}
|
|
12
|
+
};
|
|
13
|
+
var resolve = (storage) => {
|
|
14
|
+
const target = storage ?? defaultStorage;
|
|
15
|
+
if (typeof target !== "string") return target;
|
|
16
|
+
return registry[target] ?? builtin(target);
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Register a custom storage under a name, selectable anywhere — including as a
|
|
20
|
+
* directive modifier. Any object with getItem/setItem works.
|
|
21
|
+
*
|
|
22
|
+
* registerStorage('memory', myMemoryStorage);
|
|
23
|
+
* persistStore('scratch', { storage: 'memory' });
|
|
24
|
+
* <div v-persist.memory="scratch">…</div>
|
|
25
|
+
*/
|
|
26
|
+
var registerStorage = (name, storage) => {
|
|
27
|
+
registry[name] = storage;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Switch the storage used when none is specified. Accepts a registered name
|
|
31
|
+
* ('local', 'session', or your own) or a storage object.
|
|
32
|
+
*
|
|
33
|
+
* setDefaultStorage('session'); // everything defaults to sessionStorage
|
|
34
|
+
*/
|
|
35
|
+
var setDefaultStorage = (storage) => {
|
|
36
|
+
defaultStorage = storage;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Which own properties of `target` can round-trip through storage. Methods are
|
|
40
|
+
* behavior, not state; getter-only properties are derived and would throw on
|
|
41
|
+
* restore. An explicit list is filtered too, so naming a getter is a no-op.
|
|
42
|
+
*/
|
|
43
|
+
var persistable = (target, keys) => (keys ?? Object.keys(target)).filter((k) => {
|
|
44
|
+
if (k[0] === "$") return false;
|
|
45
|
+
const desc = Object.getOwnPropertyDescriptor(target, k);
|
|
46
|
+
if (desc && desc.get && !desc.set) return false;
|
|
47
|
+
const raw = desc && desc.value;
|
|
48
|
+
if (raw && raw.__v_isRef === true && raw.__v_isReadonly === true) return false;
|
|
49
|
+
return typeof target[k] !== "function";
|
|
50
|
+
});
|
|
51
|
+
/** Restore saved values, then keep storage in sync with every change. */
|
|
52
|
+
var sync = (target, key, keys, storage, run) => {
|
|
53
|
+
if (!storage) return run(() => {});
|
|
54
|
+
const fields = persistable(target, keys);
|
|
55
|
+
try {
|
|
56
|
+
const saved = JSON.parse(storage.getItem(key) || "null");
|
|
57
|
+
if (saved) {
|
|
58
|
+
for (const k of fields) if (k in saved) target[k] = saved[k];
|
|
59
|
+
}
|
|
60
|
+
} catch {}
|
|
61
|
+
return run(() => {
|
|
62
|
+
const snapshot = {};
|
|
63
|
+
for (const k of fields) snapshot[k] = target[k];
|
|
64
|
+
try {
|
|
65
|
+
storage.setItem(key, JSON.stringify(snapshot));
|
|
66
|
+
} catch {}
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* v-persist="storage-key" — syncs the element's scope to storage: restored on
|
|
71
|
+
* mount, written back on every change, nested values included.
|
|
72
|
+
*
|
|
73
|
+
* The attribute value is the storage key verbatim, not an expression, and
|
|
74
|
+
* falls back to the element id. An argument narrows which properties are
|
|
75
|
+
* stored; a modifier picks the storage (`local` default, `session`, or any
|
|
76
|
+
* name given to registerStorage()).
|
|
77
|
+
*
|
|
78
|
+
* v-persist:draft="composer" — only `draft`
|
|
79
|
+
* v-persist:draft,recipient="composer" — only those two
|
|
80
|
+
* v-persist:draft.session="composer" — and into sessionStorage
|
|
81
|
+
*/
|
|
82
|
+
var persist = (app) => {
|
|
83
|
+
app.directive("persist", ({ ctx, el, exp, arg, modifiers, effect }) => {
|
|
84
|
+
const key = PREFIX + (exp || el.id);
|
|
85
|
+
if (key === PREFIX) console.error(`v-persist needs a key: v-persist="my-key".`);
|
|
86
|
+
let storage;
|
|
87
|
+
for (const name in modifiers || {}) {
|
|
88
|
+
if (resolve(name)) {
|
|
89
|
+
storage = name;
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
console.error(`v-persist: unknown storage ".${name}" — use .local, .session, or a name passed to registerStorage().`);
|
|
93
|
+
}
|
|
94
|
+
const keys = arg ? arg.split(",").map((k) => k.trim()).filter(Boolean) : void 0;
|
|
95
|
+
sync(ctx.scope, key, keys, resolve(storage), effect);
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* persistStore('cart') — the JS-side counterpart for global stores, which have
|
|
100
|
+
* no element to hang a directive on. Restores immediately, saves on change,
|
|
101
|
+
* and returns a stop function.
|
|
102
|
+
*
|
|
103
|
+
* persistStore('cart') // the whole store
|
|
104
|
+
* persistStore('cart', { keys: ['items'] }) // only these properties
|
|
105
|
+
* persistStore('cart', { key: 'v2:cart' }) // custom storage key
|
|
106
|
+
* persistStore('draft', { storage: 'session' }) // a different storage
|
|
107
|
+
* persistStore('scratch', { storage: myStorage }) // any getItem/setItem
|
|
108
|
+
*/
|
|
109
|
+
var persistStore = (name, options = {}) => {
|
|
110
|
+
const target = store(name);
|
|
111
|
+
if (!target) {
|
|
112
|
+
console.error(`persistStore("${name}") — no such store. Register it with store("${name}", { … }) first.`);
|
|
113
|
+
return () => {};
|
|
114
|
+
}
|
|
115
|
+
const storage = resolve(options.storage);
|
|
116
|
+
if (!storage) console.error(`persistStore("${name}") — storage "${String(options.storage)}" is not available.`);
|
|
117
|
+
return sync(target, PREFIX + (options.key ?? name), options.keys, storage, watchEffect);
|
|
118
|
+
};
|
|
119
|
+
//#endregion
|
|
120
|
+
export { persist, persistStore, registerStorage, setDefaultStorage };
|
package/dist/types/app.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Directive } from './directives/index.mjs';
|
|
2
|
+
import { type ErrorHandler } from './errors.mjs';
|
|
2
3
|
export interface App {
|
|
3
4
|
/**
|
|
4
5
|
* The reactive root scope. Plugins may attach helpers here (conventionally
|
|
@@ -18,6 +19,12 @@ export interface App {
|
|
|
18
19
|
* plugin twice is a no-op.
|
|
19
20
|
*/
|
|
20
21
|
use<Options>(plugin: Plugin<Options>, options?: Options): App;
|
|
22
|
+
/**
|
|
23
|
+
* Register a handler for every runtime error LiteVue catches. Runs in
|
|
24
|
+
* production too — this is where an app forwards errors to monitoring.
|
|
25
|
+
* Returns an unregister function; a full `unmount()` also releases it.
|
|
26
|
+
*/
|
|
27
|
+
onError(handler: ErrorHandler): () => void;
|
|
21
28
|
mount(el?: string | Element | null): App | void;
|
|
22
29
|
/**
|
|
23
30
|
* Tear down every mounted root, or — given an element or selector — only the
|
package/dist/types/app.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Directive } from './directives';
|
|
2
|
+
import { type ErrorHandler } from './errors';
|
|
2
3
|
export interface App {
|
|
3
4
|
/**
|
|
4
5
|
* The reactive root scope. Plugins may attach helpers here (conventionally
|
|
@@ -18,6 +19,12 @@ export interface App {
|
|
|
18
19
|
* plugin twice is a no-op.
|
|
19
20
|
*/
|
|
20
21
|
use<Options>(plugin: Plugin<Options>, options?: Options): App;
|
|
22
|
+
/**
|
|
23
|
+
* Register a handler for every runtime error LiteVue catches. Runs in
|
|
24
|
+
* production too — this is where an app forwards errors to monitoring.
|
|
25
|
+
* Returns an unregister function; a full `unmount()` also releases it.
|
|
26
|
+
*/
|
|
27
|
+
onError(handler: ErrorHandler): () => void;
|
|
21
28
|
mount(el?: string | Element | null): App | void;
|
|
22
29
|
/**
|
|
23
30
|
* Tear down every mounted root, or — given an element or selector — only the
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One route for every runtime error. The diagnostic prose is DEV-only; the
|
|
3
|
+
* routing and `onError` handlers ship, since that is where monitoring hooks in.
|
|
4
|
+
*/
|
|
5
|
+
export type ErrorPhase =
|
|
6
|
+
/** evaluating a directive's expression, or a text interpolation */
|
|
7
|
+
'expression'
|
|
8
|
+
/** inside a v-on handler, after the event fired */
|
|
9
|
+
| 'handler'
|
|
10
|
+
/** a directive's own setup threw */
|
|
11
|
+
| 'directive'
|
|
12
|
+
/** the expression could not be compiled into a function at all */
|
|
13
|
+
| 'compile'
|
|
14
|
+
/** re-running after a change: a `$watch` callback, `watchEffect`, a plugin effect */
|
|
15
|
+
| 'effect'
|
|
16
|
+
/** a plugin's teardown, during a full `app.unmount()` */
|
|
17
|
+
| 'teardown';
|
|
18
|
+
export interface ErrorInfo {
|
|
19
|
+
phase: ErrorPhase;
|
|
20
|
+
/** the expression text, as written in the attribute */
|
|
21
|
+
expression?: string;
|
|
22
|
+
/** the attribute or construct it came from, e.g. `v-on:click` or `{{ }}` */
|
|
23
|
+
source?: string;
|
|
24
|
+
/** the element the directive sits on */
|
|
25
|
+
el?: Node;
|
|
26
|
+
/** the scope the expression was evaluated against */
|
|
27
|
+
scope?: Record<string, any>;
|
|
28
|
+
}
|
|
29
|
+
export type ErrorHandler = (err: unknown, info: ErrorInfo) => void;
|
|
30
|
+
/** A short, recognisable rendering of the element — not its whole subtree. */
|
|
31
|
+
export declare const describeEl: (el?: Node) => string | undefined;
|
|
32
|
+
/** Registered by `app.onError()`. Shared across apps, like the store registry. */
|
|
33
|
+
export declare const addErrorHandler: (fn: ErrorHandler) => () => void;
|
|
34
|
+
/** Test seam — drops every registered handler. */
|
|
35
|
+
export declare const clearErrorHandlers: () => undefined;
|
|
36
|
+
export declare const handleError: (err: unknown, info: ErrorInfo) => void;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One route for every runtime error. The diagnostic prose is DEV-only; the
|
|
3
|
+
* routing and `onError` handlers ship, since that is where monitoring hooks in.
|
|
4
|
+
*/
|
|
5
|
+
export type ErrorPhase =
|
|
6
|
+
/** evaluating a directive's expression, or a text interpolation */
|
|
7
|
+
'expression'
|
|
8
|
+
/** inside a v-on handler, after the event fired */
|
|
9
|
+
| 'handler'
|
|
10
|
+
/** a directive's own setup threw */
|
|
11
|
+
| 'directive'
|
|
12
|
+
/** the expression could not be compiled into a function at all */
|
|
13
|
+
| 'compile'
|
|
14
|
+
/** re-running after a change: a `$watch` callback, `watchEffect`, a plugin effect */
|
|
15
|
+
| 'effect'
|
|
16
|
+
/** a plugin's teardown, during a full `app.unmount()` */
|
|
17
|
+
| 'teardown';
|
|
18
|
+
export interface ErrorInfo {
|
|
19
|
+
phase: ErrorPhase;
|
|
20
|
+
/** the expression text, as written in the attribute */
|
|
21
|
+
expression?: string;
|
|
22
|
+
/** the attribute or construct it came from, e.g. `v-on:click` or `{{ }}` */
|
|
23
|
+
source?: string;
|
|
24
|
+
/** the element the directive sits on */
|
|
25
|
+
el?: Node;
|
|
26
|
+
/** the scope the expression was evaluated against */
|
|
27
|
+
scope?: Record<string, any>;
|
|
28
|
+
}
|
|
29
|
+
export type ErrorHandler = (err: unknown, info: ErrorInfo) => void;
|
|
30
|
+
/** A short, recognisable rendering of the element — not its whole subtree. */
|
|
31
|
+
export declare const describeEl: (el?: Node) => string | undefined;
|
|
32
|
+
/** Registered by `app.onError()`. Shared across apps, like the store registry. */
|
|
33
|
+
export declare const addErrorHandler: (fn: ErrorHandler) => () => void;
|
|
34
|
+
/** Test seam — drops every registered handler. */
|
|
35
|
+
export declare const clearErrorHandlers: () => undefined;
|
|
36
|
+
export declare const handleError: (err: unknown, info: ErrorInfo) => void;
|
package/dist/types/eval.d.mts
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { type ErrorInfo } from './errors.mjs';
|
|
2
|
+
/** Where the expression came from, so a failure can name its directive and element. */
|
|
3
|
+
export type EvalMeta = Pick<ErrorInfo, 'source' | 'el'> & {
|
|
4
|
+
/** Shown instead of the compiled text: `{{ total }}`, not `$s( total )`. */
|
|
5
|
+
expression?: string;
|
|
6
|
+
};
|
|
7
|
+
export declare const evaluate: (scope: any, exp: string, el?: Node, meta?: EvalMeta) => any;
|
|
8
|
+
export declare const execute: (scope: any, exp: string, el?: Node, meta?: EvalMeta) => any;
|
package/dist/types/eval.d.ts
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { type ErrorInfo } from './errors';
|
|
2
|
+
/** Where the expression came from, so a failure can name its directive and element. */
|
|
3
|
+
export type EvalMeta = Pick<ErrorInfo, 'source' | 'el'> & {
|
|
4
|
+
/** Shown instead of the compiled text: `{{ total }}`, not `$s( total )`. */
|
|
5
|
+
expression?: string;
|
|
6
|
+
};
|
|
7
|
+
export declare const evaluate: (scope: any, exp: string, el?: Node, meta?: EvalMeta) => any;
|
|
8
|
+
export declare const execute: (scope: any, exp: string, el?: Node, meta?: EvalMeta) => any;
|
package/dist/types/index.d.mts
CHANGED
|
@@ -5,5 +5,6 @@ export { nextTick } from './scheduler.mjs';
|
|
|
5
5
|
export { watchEffect } from './effect.mjs';
|
|
6
6
|
export { computed, reactive } from '@vue/reactivity';
|
|
7
7
|
export type { ComputedRef, WritableComputedRef } from '@vue/reactivity';
|
|
8
|
+
export type { ErrorHandler, ErrorInfo, ErrorPhase } from './errors.mjs';
|
|
8
9
|
export { devtools, disableDevtools } from './devtools.mjs';
|
|
9
10
|
export type { LiteVueDevtools, DevtoolsEvent } from './devtools.mjs';
|
package/dist/types/index.d.ts
CHANGED
|
@@ -5,5 +5,6 @@ export { nextTick } from './scheduler';
|
|
|
5
5
|
export { watchEffect } from './effect';
|
|
6
6
|
export { computed, reactive } from '@vue/reactivity';
|
|
7
7
|
export type { ComputedRef, WritableComputedRef } from '@vue/reactivity';
|
|
8
|
+
export type { ErrorHandler, ErrorInfo, ErrorPhase } from './errors';
|
|
8
9
|
export { devtools, disableDevtools } from './devtools';
|
|
9
10
|
export type { LiteVueDevtools, DevtoolsEvent } from './devtools';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Context } from './context.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* `v-scope.visible` / `.idle` / `.interaction`: defer a region's walk, so
|
|
4
|
+
* nothing inside is bound until the trigger. The author still wrote
|
|
5
|
+
* `v-scope`; only the moment of initialisation moves.
|
|
6
|
+
*/
|
|
7
|
+
export type LazyMode = 'visible' | 'idle' | 'interaction';
|
|
8
|
+
export declare const LAZY_MODES: LazyMode[];
|
|
9
|
+
/** Selector matching every scope root, deferred or not. */
|
|
10
|
+
export declare const SCOPE_SELECTOR: string;
|
|
11
|
+
/**
|
|
12
|
+
* Returns true if `el` was deferred. The walk must not descend into it:
|
|
13
|
+
* walking consumes directives, so it could not be walked again later.
|
|
14
|
+
*/
|
|
15
|
+
export declare const deferScope: (el: Element, ctx: Context, walk: (node: Node, ctx: Context, once: boolean) => unknown, once: boolean) => boolean;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Context } from './context';
|
|
2
|
+
/**
|
|
3
|
+
* `v-scope.visible` / `.idle` / `.interaction`: defer a region's walk, so
|
|
4
|
+
* nothing inside is bound until the trigger. The author still wrote
|
|
5
|
+
* `v-scope`; only the moment of initialisation moves.
|
|
6
|
+
*/
|
|
7
|
+
export type LazyMode = 'visible' | 'idle' | 'interaction';
|
|
8
|
+
export declare const LAZY_MODES: LazyMode[];
|
|
9
|
+
/** Selector matching every scope root, deferred or not. */
|
|
10
|
+
export declare const SCOPE_SELECTOR: string;
|
|
11
|
+
/**
|
|
12
|
+
* Returns true if `el` was deferred. The walk must not descend into it:
|
|
13
|
+
* walking consumes directives, so it could not be walked again later.
|
|
14
|
+
*/
|
|
15
|
+
export declare const deferScope: (el: Element, ctx: Context, walk: (node: Node, ctx: Context, once: boolean) => unknown, once: boolean) => boolean;
|
|
@@ -9,7 +9,8 @@ import type { Plugin } from '../../app.mjs';
|
|
|
9
9
|
*/
|
|
10
10
|
export interface MorphOptions {
|
|
11
11
|
/**
|
|
12
|
-
* Identity used to match children across the update. Defaults to `id
|
|
12
|
+
* Identity used to match children across the update. Defaults to `id`,
|
|
13
|
+
* then `data-key`, then `data-id`.
|
|
13
14
|
* Elements that match keep their DOM node (and therefore their scope);
|
|
14
15
|
* unkeyed children fall back to matching by position.
|
|
15
16
|
*/
|