hyper-scope-rails 1.0.0.alpha.1
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.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.md +104 -0
- data/app/assets/javascripts/hyper-scope/extensions/turbo-drive.esm.js +4 -0
- data/app/assets/javascripts/hyper-scope/extensions/turbo-drive.esm.js.map +1 -0
- data/app/assets/javascripts/hyper-scope/extensions/turbo-drive.iife.js +4 -0
- data/app/assets/javascripts/hyper-scope/extensions/turbo-drive.iife.js.map +1 -0
- data/app/assets/javascripts/hyper-scope/extensions/turbo-frame.esm.js +4 -0
- data/app/assets/javascripts/hyper-scope/extensions/turbo-frame.esm.js.map +1 -0
- data/app/assets/javascripts/hyper-scope/extensions/turbo-frame.iife.js +4 -0
- data/app/assets/javascripts/hyper-scope/extensions/turbo-frame.iife.js.map +1 -0
- data/app/assets/javascripts/hyper-scope/extensions/validate.esm.js +4 -0
- data/app/assets/javascripts/hyper-scope/extensions/validate.esm.js.map +1 -0
- data/app/assets/javascripts/hyper-scope/extensions/validate.iife.js +4 -0
- data/app/assets/javascripts/hyper-scope/extensions/validate.iife.js.map +1 -0
- data/app/assets/javascripts/hyper-scope/global.esm.js +4 -0
- data/app/assets/javascripts/hyper-scope/global.esm.js.map +1 -0
- data/app/assets/javascripts/hyper-scope.esm.js +7 -0
- data/app/assets/javascripts/hyper-scope.esm.js.map +1 -0
- data/app/assets/javascripts/hyper-scope.iife.js +7 -0
- data/app/assets/javascripts/hyper-scope.iife.js.map +1 -0
- data/config/importmap.rb +16 -0
- data/jsbundle/extensions/turbo-drive.esm.js +25 -0
- data/jsbundle/extensions/turbo-frame.esm.js +43 -0
- data/jsbundle/extensions/validate.esm.js +182 -0
- data/jsbundle/hyper-scope.esm.js +4354 -0
- data/lib/hyper-scope-rails.rb +4 -0
- data/lib/hyper_scope/rails/engine.rb +59 -0
- data/lib/hyper_scope/rails/helper.rb +76 -0
- data/lib/hyper_scope/rails/version.rb +9 -0
- data/lib/hyper_scope/rails.rb +22 -0
- metadata +88 -0
data/config/importmap.rb
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Pins added to apps that use importmap-rails — the names match the package's export paths.
|
|
4
|
+
# import { plugin, start } from "hyper-scope"
|
|
5
|
+
# import { turboDrive } from "hyper-scope/extensions/turbo-drive"
|
|
6
|
+
# import { ready } from "hyper-scope/global"
|
|
7
|
+
#
|
|
8
|
+
# With config.hyper_scope.importmap = :global (the app bundles hyper-scope itself), "hyper-scope" points at the
|
|
9
|
+
# bridge too, so inline modules reach the bundled runtime instead of loading a second one.
|
|
10
|
+
bridge = ::Rails.application.config.hyper_scope.importmap.to_s == "global"
|
|
11
|
+
|
|
12
|
+
pin "hyper-scope", to: bridge ? "hyper-scope/global.esm.js" : "hyper-scope.esm.js"
|
|
13
|
+
pin "hyper-scope/global", to: "hyper-scope/global.esm.js"
|
|
14
|
+
pin "hyper-scope/extensions/turbo-drive", to: "hyper-scope/extensions/turbo-drive.esm.js"
|
|
15
|
+
pin "hyper-scope/extensions/turbo-frame", to: "hyper-scope/extensions/turbo-frame.esm.js"
|
|
16
|
+
pin "hyper-scope/extensions/validate", to: "hyper-scope/extensions/validate.esm.js"
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/extensions/turbo-drive.ts
|
|
2
|
+
var PERMANENT = "[data-turbo-permanent]";
|
|
3
|
+
var PERMANENT_WITH_ID = "[id][data-turbo-permanent]";
|
|
4
|
+
var PLACEHOLDER = 'meta[name="turbo-permanent-placeholder"]';
|
|
5
|
+
var byId = (root, id, selector) => [...root.querySelectorAll(selector)].find((el) => (el.id || el.getAttribute("content")) === id);
|
|
6
|
+
var transplant = (dead, fresh) => {
|
|
7
|
+
for (const spot of [...fresh.querySelectorAll(PERMANENT_WITH_ID)]) {
|
|
8
|
+
const kept = byId(dead, spot.id, PLACEHOLDER) || byId(dead, spot.id, PERMANENT_WITH_ID);
|
|
9
|
+
if (kept)
|
|
10
|
+
spot.replaceWith(kept);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
var turboDrive = ({ detach, snapshot, onRevive, onStop }) => {
|
|
14
|
+
detach(PERMANENT);
|
|
15
|
+
onRevive(transplant);
|
|
16
|
+
const onBeforeCache = () => {
|
|
17
|
+
const release = snapshot();
|
|
18
|
+
setTimeout(() => setTimeout(release));
|
|
19
|
+
};
|
|
20
|
+
document.addEventListener("turbo:before-cache", onBeforeCache);
|
|
21
|
+
onStop(() => document.removeEventListener("turbo:before-cache", onBeforeCache));
|
|
22
|
+
};
|
|
23
|
+
export {
|
|
24
|
+
turboDrive
|
|
25
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// src/extensions/turbo-frame.ts
|
|
2
|
+
var sameURL = (a, b) => {
|
|
3
|
+
try {
|
|
4
|
+
return new URL(a, document.baseURI).href === new URL(b, document.baseURI).href;
|
|
5
|
+
} catch {
|
|
6
|
+
return a === b;
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
var frameOf = (root) => {
|
|
10
|
+
const el = root.closest("turbo-frame");
|
|
11
|
+
const missing = (what) => console.warn(`hyper-scope: $tf.${what}() — 감싼 <turbo-frame> 이 없습니다.`);
|
|
12
|
+
const reload = () => {
|
|
13
|
+
if (!el)
|
|
14
|
+
return missing("reload");
|
|
15
|
+
if (typeof el.reload === "function") {
|
|
16
|
+
el.reload();
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const src = el.getAttribute("src");
|
|
20
|
+
if (src != null) {
|
|
21
|
+
el.removeAttribute("src");
|
|
22
|
+
el.setAttribute("src", src);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
return Object.freeze({
|
|
26
|
+
el,
|
|
27
|
+
reload,
|
|
28
|
+
visit(url) {
|
|
29
|
+
if (!el)
|
|
30
|
+
return missing("visit");
|
|
31
|
+
const src = el.getAttribute("src");
|
|
32
|
+
if (src && sameURL(src, url))
|
|
33
|
+
reload();
|
|
34
|
+
else
|
|
35
|
+
el.setAttribute("src", url);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
var turboFrame = ({ defineScoped }) => defineScoped("$tf", (host) => frameOf(host.el));
|
|
40
|
+
export {
|
|
41
|
+
turboFrame,
|
|
42
|
+
frameOf
|
|
43
|
+
};
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// src/extensions/validate.ts
|
|
2
|
+
var empty = (v) => v === undefined || v === null || v === false || typeof v === "string" && v.trim() === "" || Array.isArray(v) && v.length === 0;
|
|
3
|
+
var size = (v) => Array.isArray(v) ? v.length : String(v).length;
|
|
4
|
+
var withParams = (params, fn) => Object.assign(fn, { $params: params });
|
|
5
|
+
var filled = (test) => (v) => empty(v) || test(v);
|
|
6
|
+
var EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
7
|
+
var rules = {
|
|
8
|
+
required: (v) => !empty(v),
|
|
9
|
+
email: filled((v) => EMAIL.test(String(v))),
|
|
10
|
+
numeric: filled((v) => /^-?\d+(\.\d+)?$/.test(String(v))),
|
|
11
|
+
integer: filled((v) => /^-?\d+$/.test(String(v))),
|
|
12
|
+
minLength: (min) => withParams({ min }, filled((v) => size(v) >= min)),
|
|
13
|
+
maxLength: (max) => withParams({ max }, filled((v) => size(v) <= max)),
|
|
14
|
+
minValue: (min) => withParams({ min }, filled((v) => Number(v) >= min)),
|
|
15
|
+
maxValue: (max) => withParams({ max }, filled((v) => Number(v) <= max)),
|
|
16
|
+
between: (min, max) => withParams({ min, max }, filled((v) => Number(v) >= min && Number(v) <= max)),
|
|
17
|
+
pattern: (re) => {
|
|
18
|
+
const exp = typeof re === "string" ? new RegExp(re) : re;
|
|
19
|
+
return withParams({ pattern: String(exp) }, filled((v) => exp.test(String(v))));
|
|
20
|
+
},
|
|
21
|
+
sameAs: (other) => withParams({ other }, (v) => v === other)
|
|
22
|
+
};
|
|
23
|
+
var defaultMessages = {
|
|
24
|
+
required: "Value is required",
|
|
25
|
+
email: "Value is not a valid email address",
|
|
26
|
+
numeric: "Value must be numeric",
|
|
27
|
+
integer: "Value must be an integer",
|
|
28
|
+
minLength: (p) => `Value must be at least ${p.min} characters long`,
|
|
29
|
+
maxLength: (p) => `Value must be at most ${p.max} characters long`,
|
|
30
|
+
minValue: (p) => `Value must be at least ${p.min}`,
|
|
31
|
+
maxValue: (p) => `Value must be at most ${p.max}`,
|
|
32
|
+
between: (p) => `Value must be between ${p.min} and ${p.max}`,
|
|
33
|
+
pattern: "Value has an invalid format",
|
|
34
|
+
sameAs: "Value must match"
|
|
35
|
+
};
|
|
36
|
+
var literal = (raw) => {
|
|
37
|
+
const t = raw.trim();
|
|
38
|
+
if (t !== "" && !isNaN(Number(t)))
|
|
39
|
+
return Number(t);
|
|
40
|
+
return t;
|
|
41
|
+
};
|
|
42
|
+
var createValidate = (options = {}) => ({ defineScoped, define, directive, reactive, computed }) => {
|
|
43
|
+
const messages = { ...defaultMessages, ...options.messages };
|
|
44
|
+
const table = { ...rules, ...options.rules };
|
|
45
|
+
const registries = new WeakMap;
|
|
46
|
+
const messageOf = (name, params) => {
|
|
47
|
+
const m = messages[name];
|
|
48
|
+
return typeof m === "function" ? m(params) : m || `Value is invalid (${name})`;
|
|
49
|
+
};
|
|
50
|
+
const registryOf = (host) => {
|
|
51
|
+
let reg = registries.get(host);
|
|
52
|
+
if (reg)
|
|
53
|
+
return reg;
|
|
54
|
+
const fields = reactive(new Set);
|
|
55
|
+
const all = () => [...fields].map((f) => f.state);
|
|
56
|
+
const root = reactive({});
|
|
57
|
+
const fixed = {
|
|
58
|
+
$invalid: { get: () => all().some((s) => s.$invalid) },
|
|
59
|
+
$dirty: { get: () => all().length > 0 && all().every((s) => s.$dirty) },
|
|
60
|
+
$anyDirty: { get: () => all().some((s) => s.$dirty) },
|
|
61
|
+
$error: { get: () => all().some((s) => s.$error) },
|
|
62
|
+
$errors: { get: () => all().flatMap((s) => s.$errors) },
|
|
63
|
+
$silentErrors: { get: () => all().flatMap((s) => s.$silentErrors) },
|
|
64
|
+
$touch: { value: () => all().forEach((s) => s.$touch()) },
|
|
65
|
+
$reset: { value: () => all().forEach((s) => s.$reset()) },
|
|
66
|
+
$validate: { value: () => (all().forEach((s) => s.$touch()), !all().some((s) => s.$invalid)) }
|
|
67
|
+
};
|
|
68
|
+
const view = new Proxy(root, {
|
|
69
|
+
get: (t, key, r) => typeof key === "string" && (key in fixed) ? fixed[key].get ? fixed[key].get() : fixed[key].value : Reflect.get(t, key, r),
|
|
70
|
+
has: (t, key) => typeof key === "string" && (key in fixed) || Reflect.has(t, key)
|
|
71
|
+
});
|
|
72
|
+
registries.set(host, reg = { root: view, fields });
|
|
73
|
+
return reg;
|
|
74
|
+
};
|
|
75
|
+
const place = (root, path, state) => {
|
|
76
|
+
const keys = path.split(".");
|
|
77
|
+
let at = root;
|
|
78
|
+
for (const key of keys.slice(0, -1))
|
|
79
|
+
at = at[key] ||= {};
|
|
80
|
+
const last = keys[keys.length - 1];
|
|
81
|
+
at[last] = state;
|
|
82
|
+
return () => {
|
|
83
|
+
if (at[last] === state)
|
|
84
|
+
delete at[last];
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
const shorthand = /^[\w$]+(:[^\s{}()]+)?(\s+[\w$]+(:[^\s{}()]+)?)*$/;
|
|
88
|
+
const parseShorthand = (exp, get) => {
|
|
89
|
+
const out = {};
|
|
90
|
+
for (const token of exp.trim().split(/\s+/)) {
|
|
91
|
+
const [name, raw] = token.split(/:(.*)/s);
|
|
92
|
+
const rule = table[name];
|
|
93
|
+
if (!rule) {
|
|
94
|
+
console.error(`hyper-scope: h-rules — 모르는 규칙입니다: ${name}`);
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (raw === undefined)
|
|
98
|
+
out[name] = rule;
|
|
99
|
+
else
|
|
100
|
+
out[name] = name === "sameAs" ? rule(get(raw)) : rule(...raw.split(",").map(literal));
|
|
101
|
+
}
|
|
102
|
+
return out;
|
|
103
|
+
};
|
|
104
|
+
define("$rules", table);
|
|
105
|
+
defineScoped("$v", (host) => registryOf(host.el).root);
|
|
106
|
+
directive("rules", ({ el, exp, arg, modifiers, get }) => {
|
|
107
|
+
const input = el;
|
|
108
|
+
const modelAttr = [...input.attributes].find((a) => a.name === "h-model" || a.name.startsWith("h-model."));
|
|
109
|
+
const path = arg || modelAttr?.value.trim() || input.getAttribute("name") || "";
|
|
110
|
+
if (!path) {
|
|
111
|
+
console.error("hyper-scope: h-rules — 필드 이름을 알 수 없습니다. h-model 과 함께 쓰거나 h-rules:이름 으로 적습니다.", el);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const valueExp = modelAttr?.value.trim() || path;
|
|
115
|
+
const hostEl = get("$host").el;
|
|
116
|
+
const reg = registryOf(hostEl);
|
|
117
|
+
const isShort = shorthand.test(exp.trim());
|
|
118
|
+
const failures = computed(() => {
|
|
119
|
+
const value = get(valueExp);
|
|
120
|
+
const set = (isShort ? parseShorthand(exp, get) : get()) || {};
|
|
121
|
+
const out = [];
|
|
122
|
+
for (const [name, rule] of Object.entries(set)) {
|
|
123
|
+
if (typeof rule !== "function")
|
|
124
|
+
continue;
|
|
125
|
+
const result = rule(value);
|
|
126
|
+
if (result === true)
|
|
127
|
+
continue;
|
|
128
|
+
const params = rule.$params || {};
|
|
129
|
+
out.push({
|
|
130
|
+
$property: path,
|
|
131
|
+
$validator: name,
|
|
132
|
+
$message: typeof result === "string" ? result : messageOf(name, params),
|
|
133
|
+
$params: params
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
return out;
|
|
137
|
+
});
|
|
138
|
+
const state = reactive({
|
|
139
|
+
$dirty: false,
|
|
140
|
+
get $invalid() {
|
|
141
|
+
return failures.value.length > 0;
|
|
142
|
+
},
|
|
143
|
+
get $error() {
|
|
144
|
+
return this.$dirty && failures.value.length > 0;
|
|
145
|
+
},
|
|
146
|
+
get $errors() {
|
|
147
|
+
return this.$dirty ? failures.value : [];
|
|
148
|
+
},
|
|
149
|
+
get $silentErrors() {
|
|
150
|
+
return failures.value;
|
|
151
|
+
},
|
|
152
|
+
get $model() {
|
|
153
|
+
return get(valueExp);
|
|
154
|
+
},
|
|
155
|
+
$touch() {
|
|
156
|
+
this.$dirty = true;
|
|
157
|
+
},
|
|
158
|
+
$reset() {
|
|
159
|
+
this.$dirty = false;
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
const field = { path, state };
|
|
163
|
+
reg.fields.add(field);
|
|
164
|
+
const unplace = place(reg.root, path, state);
|
|
165
|
+
const touch = () => state.$touch();
|
|
166
|
+
const events = modifiers?.input ? ["input", "change", "blur"] : ["change", "blur"];
|
|
167
|
+
for (const type of events)
|
|
168
|
+
input.addEventListener(type, touch);
|
|
169
|
+
return () => {
|
|
170
|
+
for (const type of events)
|
|
171
|
+
input.removeEventListener(type, touch);
|
|
172
|
+
reg.fields.delete(field);
|
|
173
|
+
unplace();
|
|
174
|
+
};
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
var validate = createValidate();
|
|
178
|
+
export {
|
|
179
|
+
validate,
|
|
180
|
+
rules,
|
|
181
|
+
createValidate
|
|
182
|
+
};
|