tortoise-orm-studio 1.0.0__py3-none-any.whl
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.
- tortoise_orm_studio/__init__.py +11 -0
- tortoise_orm_studio/adapters/__init__.py +1 -0
- tortoise_orm_studio/adapters/aiohttp_adapter.py +31 -0
- tortoise_orm_studio/adapters/asgi.py +89 -0
- tortoise_orm_studio/adapters/base.py +25 -0
- tortoise_orm_studio/adapters/django_adapter.py +84 -0
- tortoise_orm_studio/adapters/quart_adapter.py +32 -0
- tortoise_orm_studio/adapters/registry.py +39 -0
- tortoise_orm_studio/adapters/sanic_adapter.py +39 -0
- tortoise_orm_studio/auth.py +34 -0
- tortoise_orm_studio/cli.py +41 -0
- tortoise_orm_studio/core/__init__.py +1 -0
- tortoise_orm_studio/core/aerich.py +86 -0
- tortoise_orm_studio/core/ai.py +87 -0
- tortoise_orm_studio/core/audit.py +69 -0
- tortoise_orm_studio/core/config.py +58 -0
- tortoise_orm_studio/core/filters.py +94 -0
- tortoise_orm_studio/core/handler.py +249 -0
- tortoise_orm_studio/core/http.py +55 -0
- tortoise_orm_studio/core/i18n.py +18 -0
- tortoise_orm_studio/core/introspect.py +197 -0
- tortoise_orm_studio/core/manifest.py +128 -0
- tortoise_orm_studio/core/pagination.py +59 -0
- tortoise_orm_studio/core/permissions.py +46 -0
- tortoise_orm_studio/core/plugins.py +214 -0
- tortoise_orm_studio/core/ratelimit.py +32 -0
- tortoise_orm_studio/core/scoping.py +23 -0
- tortoise_orm_studio/core/serializers.py +61 -0
- tortoise_orm_studio/core/service.py +594 -0
- tortoise_orm_studio/plugins.py +8 -0
- tortoise_orm_studio/static/ALPINE_LICENSE.txt +11 -0
- tortoise_orm_studio/static/_types.d.ts +706 -0
- tortoise_orm_studio/static/a11y.js +115 -0
- tortoise_orm_studio/static/ai.js +66 -0
- tortoise_orm_studio/static/alpine.min.js +23 -0
- tortoise_orm_studio/static/app.css +514 -0
- tortoise_orm_studio/static/app.js +1443 -0
- tortoise_orm_studio/static/audit.js +171 -0
- tortoise_orm_studio/static/copy-as.js +118 -0
- tortoise_orm_studio/static/er-layout.js +227 -0
- tortoise_orm_studio/static/er.js +296 -0
- tortoise_orm_studio/static/filters.js +114 -0
- tortoise_orm_studio/static/i18n.en.js +190 -0
- tortoise_orm_studio/static/i18n.js +140 -0
- tortoise_orm_studio/static/i18n.zh-CN.js +189 -0
- tortoise_orm_studio/static/index.html +515 -0
- tortoise_orm_studio/static/inline-edit.js +107 -0
- tortoise_orm_studio/static/json-view.js +161 -0
- tortoise_orm_studio/static/migrations.js +139 -0
- tortoise_orm_studio/static/palette.js +141 -0
- tortoise_orm_studio/static/pending.js +240 -0
- tortoise_orm_studio/static/pickers.js +230 -0
- tortoise_orm_studio/static/playground.js +105 -0
- tortoise_orm_studio/static/query-history.js +84 -0
- tortoise_orm_studio/static/shortcuts.js +145 -0
- tortoise_orm_studio/static/state-hash.js +112 -0
- tortoise_orm_studio/static/tabs.js +368 -0
- tortoise_orm_studio/static/theme-init.js +33 -0
- tortoise_orm_studio/static/theme.js +106 -0
- tortoise_orm_studio/static/toasts.js +76 -0
- tortoise_orm_studio/static/views.js +70 -0
- tortoise_orm_studio/static/widgets.js +345 -0
- tortoise_orm_studio/studio.py +51 -0
- tortoise_orm_studio-1.0.0.dist-info/METADATA +177 -0
- tortoise_orm_studio-1.0.0.dist-info/RECORD +68 -0
- tortoise_orm_studio-1.0.0.dist-info/WHEEL +4 -0
- tortoise_orm_studio-1.0.0.dist-info/entry_points.txt +2 -0
- tortoise_orm_studio-1.0.0.dist-info/licenses/LICENSE +13 -0
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Audit log panel renderer.
|
|
4
|
+
*
|
|
5
|
+
* Exposes window.StudioAudit.render(info, opts).
|
|
6
|
+
* Rows are expandable — click to reveal before/after JSON.
|
|
7
|
+
* Pagination controls call opts.onPageChange(page) when clicked.
|
|
8
|
+
*
|
|
9
|
+
* Types: AuditInfo, AuditAPI from _types.d.ts.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
(function (/** @type {Window & { StudioAudit?: AuditAPI }} */ global) {
|
|
13
|
+
"use strict";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @template {keyof HTMLElementTagNameMap} K
|
|
17
|
+
* @param {K} tag
|
|
18
|
+
* @param {Record<string, any>} [props]
|
|
19
|
+
* @param {Array<Node|string>} [children]
|
|
20
|
+
* @returns {HTMLElementTagNameMap[K]}
|
|
21
|
+
*/
|
|
22
|
+
function create(tag, props, children) {
|
|
23
|
+
/** @type {any} */
|
|
24
|
+
const el = document.createElement(tag);
|
|
25
|
+
for (const k of Object.keys(props || {})) {
|
|
26
|
+
const v = props[k];
|
|
27
|
+
if (v === undefined) continue;
|
|
28
|
+
if (k === "class") el.className = v;
|
|
29
|
+
else if (k.startsWith("on") && typeof v === "function") el.addEventListener(k.slice(2), v);
|
|
30
|
+
else if (k in el) el[k] = v;
|
|
31
|
+
else el.setAttribute(k, String(v));
|
|
32
|
+
}
|
|
33
|
+
for (const c of children || []) {
|
|
34
|
+
el.appendChild(typeof c === "string" ? document.createTextNode(c) : c);
|
|
35
|
+
}
|
|
36
|
+
return el;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** @param {unknown} v @returns {string} */
|
|
40
|
+
function short(v) {
|
|
41
|
+
if (v === null || v === undefined) return "—";
|
|
42
|
+
const s = typeof v === "object" ? JSON.stringify(v) : String(v);
|
|
43
|
+
return s.length > 40 ? s.slice(0, 37) + "…" : s;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {unknown} v
|
|
48
|
+
* @returns {HTMLElement}
|
|
49
|
+
*/
|
|
50
|
+
function jsonBlock(v) {
|
|
51
|
+
const text = v === null || v === undefined ? "(null)" : JSON.stringify(v, null, 2);
|
|
52
|
+
return create("pre", { class: "audit-json" }, [text]);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @param {AuditInfo | null} info
|
|
57
|
+
* @param {{ onPageChange?: (page: number) => void }} [opts]
|
|
58
|
+
* @returns {HTMLElement}
|
|
59
|
+
*/
|
|
60
|
+
function render(info, opts) {
|
|
61
|
+
const root = create("div", { class: "audit-panel" });
|
|
62
|
+
|
|
63
|
+
if (!info) {
|
|
64
|
+
root.appendChild(create("p", { class: "mut" }, ["Loading…"]));
|
|
65
|
+
return root;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (!info.available) {
|
|
69
|
+
root.appendChild(create("p", { class: "mut" }, [
|
|
70
|
+
"Audit log is not enabled for this instance.",
|
|
71
|
+
]));
|
|
72
|
+
root.appendChild(create("p", { class: "mut audit-hint" }, [
|
|
73
|
+
"Register the model: add \"tortoise_orm_studio.core.audit\" to your Tortoise apps, then pass audit=hook to Studio().",
|
|
74
|
+
]));
|
|
75
|
+
return root;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (!info.items.length) {
|
|
79
|
+
root.appendChild(create("p", { class: "mut" }, ["No audit entries match the current filters."]));
|
|
80
|
+
return root;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const table = create("table", { class: "audit-table" });
|
|
84
|
+
const thead = create("thead");
|
|
85
|
+
const headRow = create("tr");
|
|
86
|
+
for (const h of ["at", "action", "model", "pk", "actor", ""]) {
|
|
87
|
+
headRow.appendChild(create("th", {}, [h]));
|
|
88
|
+
}
|
|
89
|
+
thead.appendChild(headRow);
|
|
90
|
+
table.appendChild(thead);
|
|
91
|
+
|
|
92
|
+
const tbody = create("tbody");
|
|
93
|
+
for (const entry of info.items) {
|
|
94
|
+
const tr = create("tr", { class: "audit-row" });
|
|
95
|
+
tr.setAttribute("data-id", String(entry.id));
|
|
96
|
+
|
|
97
|
+
tr.appendChild(create("td", { class: "audit-at" }, [entry.at || ""]));
|
|
98
|
+
tr.appendChild(create("td", {}, [create("span", {
|
|
99
|
+
class: "audit-action audit-action-" + entry.action,
|
|
100
|
+
}, [entry.action])]));
|
|
101
|
+
tr.appendChild(create("td", {}, [entry.model]));
|
|
102
|
+
tr.appendChild(create("td", { class: "audit-pk" }, [entry.pk === null ? "—" : entry.pk]));
|
|
103
|
+
tr.appendChild(create("td", {}, [entry.actor === null ? "—" : entry.actor]));
|
|
104
|
+
|
|
105
|
+
const expandBtn = create("button", {
|
|
106
|
+
type: "button", class: "audit-expand", "aria-label": "Show details",
|
|
107
|
+
}, ["▸"]);
|
|
108
|
+
const cell6 = create("td", { class: "audit-expand-cell" }, [expandBtn]);
|
|
109
|
+
tr.appendChild(cell6);
|
|
110
|
+
|
|
111
|
+
const detailRow = create("tr", { class: "audit-detail" });
|
|
112
|
+
detailRow.style.display = "none";
|
|
113
|
+
const detailCell = create("td", { colSpan: 6 });
|
|
114
|
+
const details = create("div", { class: "audit-details" });
|
|
115
|
+
if (entry.before !== null && entry.before !== undefined) {
|
|
116
|
+
details.appendChild(create("div", { class: "audit-col" }, [
|
|
117
|
+
create("h5", {}, ["Before"]), jsonBlock(entry.before),
|
|
118
|
+
]));
|
|
119
|
+
}
|
|
120
|
+
if (entry.after !== null && entry.after !== undefined) {
|
|
121
|
+
details.appendChild(create("div", { class: "audit-col" }, [
|
|
122
|
+
create("h5", {}, ["After"]), jsonBlock(entry.after),
|
|
123
|
+
]));
|
|
124
|
+
}
|
|
125
|
+
if (!details.children.length) {
|
|
126
|
+
details.appendChild(create("p", { class: "mut" }, ["No before/after payload."]));
|
|
127
|
+
}
|
|
128
|
+
detailCell.appendChild(details);
|
|
129
|
+
detailRow.appendChild(detailCell);
|
|
130
|
+
|
|
131
|
+
let expanded = false;
|
|
132
|
+
expandBtn.addEventListener("click", function () {
|
|
133
|
+
expanded = !expanded;
|
|
134
|
+
detailRow.style.display = expanded ? "" : "none";
|
|
135
|
+
expandBtn.textContent = expanded ? "▾" : "▸";
|
|
136
|
+
expandBtn.setAttribute("aria-label", expanded ? "Hide details" : "Show details");
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
tbody.appendChild(tr);
|
|
140
|
+
tbody.appendChild(detailRow);
|
|
141
|
+
}
|
|
142
|
+
table.appendChild(tbody);
|
|
143
|
+
root.appendChild(table);
|
|
144
|
+
|
|
145
|
+
// Pagination
|
|
146
|
+
const pages = Math.max(1, Math.ceil(info.total / info.limit));
|
|
147
|
+
const footer = create("div", { class: "audit-footer" });
|
|
148
|
+
footer.appendChild(create("span", { class: "mut" }, [
|
|
149
|
+
`${info.total} entries · page ${info.page} / ${pages}`,
|
|
150
|
+
]));
|
|
151
|
+
if (opts && opts.onPageChange) {
|
|
152
|
+
const cb = opts.onPageChange;
|
|
153
|
+
const prev = create("button", {
|
|
154
|
+
type: "button", disabled: info.page <= 1,
|
|
155
|
+
}, ["Prev"]);
|
|
156
|
+
prev.addEventListener("click", function () { cb(info.page - 1); });
|
|
157
|
+
footer.appendChild(prev);
|
|
158
|
+
|
|
159
|
+
const next = create("button", {
|
|
160
|
+
type: "button", disabled: info.page >= pages,
|
|
161
|
+
}, ["Next"]);
|
|
162
|
+
next.addEventListener("click", function () { cb(info.page + 1); });
|
|
163
|
+
footer.appendChild(next);
|
|
164
|
+
}
|
|
165
|
+
root.appendChild(footer);
|
|
166
|
+
|
|
167
|
+
return root;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
global.StudioAudit = { render: render };
|
|
171
|
+
})(window);
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Pure "Copy as" formatters for v0.5.0 (UI program D).
|
|
4
|
+
*
|
|
5
|
+
* Given a row and its model description, produce clipboard text in three
|
|
6
|
+
* shapes: JSON, a Tortoise ``create()`` snippet and a CSV line. No DOM, no
|
|
7
|
+
* network — the formatters are plain functions testable with `node --test`.
|
|
8
|
+
* ``write()`` is the only browser-facing helper (clipboard / legacy fallback).
|
|
9
|
+
*
|
|
10
|
+
* Types: CopyAsAPI in _types.d.ts.
|
|
11
|
+
*/
|
|
12
|
+
(function (/** @type {any} */ global) {
|
|
13
|
+
"use strict";
|
|
14
|
+
|
|
15
|
+
/** @param {any} row @returns {string} */
|
|
16
|
+
function json(row) {
|
|
17
|
+
return JSON.stringify(row, null, 2);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Render a Python literal for a value.
|
|
22
|
+
* @param {any} v
|
|
23
|
+
* @returns {string}
|
|
24
|
+
*/
|
|
25
|
+
function pyLiteral(v) {
|
|
26
|
+
if (v === null || v === undefined) return "None";
|
|
27
|
+
if (v === true) return "True";
|
|
28
|
+
if (v === false) return "False";
|
|
29
|
+
if (typeof v === "number") return String(v);
|
|
30
|
+
if (typeof v === "string") return JSON.stringify(v);
|
|
31
|
+
// objects / arrays: JSON is a valid Python literal for these shapes.
|
|
32
|
+
return JSON.stringify(v);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param {any} desc model description
|
|
37
|
+
* @param {any} row
|
|
38
|
+
* @returns {string}
|
|
39
|
+
*/
|
|
40
|
+
function createSnippet(desc, row) {
|
|
41
|
+
var name = (desc && (desc.name || desc.key)) || "Model";
|
|
42
|
+
if (name.indexOf(".") >= 0) name = name.split(".").pop();
|
|
43
|
+
/** @type {string[]} */
|
|
44
|
+
var parts = [];
|
|
45
|
+
var fields = (desc && desc.fields) || [];
|
|
46
|
+
if (fields.length) {
|
|
47
|
+
for (var i = 0; i < fields.length; i++) {
|
|
48
|
+
var f = fields[i];
|
|
49
|
+
if (f.pk || f.readonly) continue;
|
|
50
|
+
if (!Object.prototype.hasOwnProperty.call(row, f.name)) continue;
|
|
51
|
+
parts.push(f.name + "=" + pyLiteral(row[f.name]));
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
// No field metadata (e.g. a related row): include every row key.
|
|
55
|
+
for (var key of Object.keys(row)) {
|
|
56
|
+
if (key === "_pk") continue;
|
|
57
|
+
parts.push(key + "=" + pyLiteral(row[key]));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return "await " + name + ".create(" + parts.join(", ") + ")";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* CSV-escape one value (quote when it contains comma/quote/newline).
|
|
65
|
+
* @param {any} v
|
|
66
|
+
* @returns {string}
|
|
67
|
+
*/
|
|
68
|
+
function csvCell(v) {
|
|
69
|
+
var s = v === null || v === undefined ? "" : String(v);
|
|
70
|
+
if (/[",\n\r]/.test(s)) return '"' + s.replace(/"/g, '""') + '"';
|
|
71
|
+
return s;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* One CSV line for a row over the given columns.
|
|
76
|
+
* @param {any} row
|
|
77
|
+
* @param {string[]} cols
|
|
78
|
+
* @returns {string}
|
|
79
|
+
*/
|
|
80
|
+
function csvLine(row, cols) {
|
|
81
|
+
return cols.map(function (/** @type {string} */ c) {
|
|
82
|
+
return csvCell(row[c]);
|
|
83
|
+
}).join(",");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Clipboard write with a legacy fallback. Returns a promise<boolean>.
|
|
88
|
+
* @param {string} text
|
|
89
|
+
* @returns {Promise<boolean>}
|
|
90
|
+
*/
|
|
91
|
+
function write(text) {
|
|
92
|
+
try {
|
|
93
|
+
var nav = global.navigator;
|
|
94
|
+
if (nav && nav.clipboard && typeof nav.clipboard.writeText === "function") {
|
|
95
|
+
return nav.clipboard.writeText(text).then(
|
|
96
|
+
function () { return true; }, function () { return false; }
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
} catch (e) { /* fall through */ }
|
|
100
|
+
try {
|
|
101
|
+
var ta = global.document && global.document.createElement("textarea");
|
|
102
|
+
if (!ta) return Promise.resolve(false);
|
|
103
|
+
ta.value = text;
|
|
104
|
+
ta.style.position = "fixed";
|
|
105
|
+
ta.style.opacity = "0";
|
|
106
|
+
global.document.body.appendChild(ta);
|
|
107
|
+
ta.select();
|
|
108
|
+
var ok = !!(global.document.execCommand && global.document.execCommand("copy"));
|
|
109
|
+
global.document.body.removeChild(ta);
|
|
110
|
+
return Promise.resolve(ok);
|
|
111
|
+
} catch (e) { return Promise.resolve(false); }
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
global.StudioCopyAs = {
|
|
115
|
+
json: json, createSnippet: createSnippet, csvLine: csvLine,
|
|
116
|
+
csvCell: csvCell, pyLiteral: pyLiteral, write: write,
|
|
117
|
+
};
|
|
118
|
+
})(typeof window !== "undefined" ? window : globalThis);
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Pure layout for the in-app ER diagram. No DOM, no SVG, no network — just
|
|
4
|
+
* deterministic geometry that can be tested with `node --test`.
|
|
5
|
+
*
|
|
6
|
+
* Input is `ModelDesc[]` from GET /api/schema. Output is boxes (with exact
|
|
7
|
+
* coordinates) and edges (with a rendered SVG path). The DOM renderer in
|
|
8
|
+
* er.js only turns these numbers into elements.
|
|
9
|
+
*
|
|
10
|
+
* Types: ModelDesc, Field, Relation from _types.d.ts.
|
|
11
|
+
*/
|
|
12
|
+
(function (/** @type {any} */ global) {
|
|
13
|
+
"use strict";
|
|
14
|
+
|
|
15
|
+
/** Geometry constants. Kept here so layout tests can assert exact numbers. */
|
|
16
|
+
var BOX_W = 220;
|
|
17
|
+
var HEADER_H = 30;
|
|
18
|
+
var FIELD_H = 18;
|
|
19
|
+
var FOOTER_H = 8;
|
|
20
|
+
var GAP_X = 72;
|
|
21
|
+
var GAP_Y = 64;
|
|
22
|
+
var COLUMNS = 3;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Height of a model box: one header + one row per field + a little padding.
|
|
26
|
+
* @param {ModelDesc} model
|
|
27
|
+
* @returns {number}
|
|
28
|
+
*/
|
|
29
|
+
function boxHeight(model) {
|
|
30
|
+
return HEADER_H + model.fields.length * FIELD_H + FOOTER_H;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Lay models out on a deterministic grid, in key order. Boxes never
|
|
35
|
+
* overlap: each row is as tall as its tallest box, and rows are separated
|
|
36
|
+
* by GAP_Y. Same input -> same output, so snapshots are stable.
|
|
37
|
+
* @param {ModelDesc[]} models
|
|
38
|
+
* @returns {Array<{ key: string, name: string, app: string, x: number, y: number,
|
|
39
|
+
* width: number, height: number, fields: Field[] }>}
|
|
40
|
+
*/
|
|
41
|
+
function layout(models) {
|
|
42
|
+
var sorted = models.slice().sort(function (a, b) {
|
|
43
|
+
return a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
|
|
44
|
+
});
|
|
45
|
+
var boxes = [];
|
|
46
|
+
var rowTop = 0;
|
|
47
|
+
|
|
48
|
+
for (var start = 0; start < sorted.length; start += COLUMNS) {
|
|
49
|
+
var row = sorted.slice(start, start + COLUMNS);
|
|
50
|
+
var tallest = 0;
|
|
51
|
+
for (var c = 0; c < row.length; c++) {
|
|
52
|
+
var m = row[c];
|
|
53
|
+
var h = boxHeight(m);
|
|
54
|
+
if (h > tallest) tallest = h;
|
|
55
|
+
boxes.push({
|
|
56
|
+
key: m.key,
|
|
57
|
+
name: m.name,
|
|
58
|
+
app: m.app,
|
|
59
|
+
x: c * (BOX_W + GAP_X),
|
|
60
|
+
y: rowTop,
|
|
61
|
+
width: BOX_W,
|
|
62
|
+
height: h,
|
|
63
|
+
fields: m.fields,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
rowTop += tallest + GAP_Y;
|
|
67
|
+
}
|
|
68
|
+
return boxes;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Where a relationship line meets a box. We attach to the facing sides
|
|
73
|
+
* (right of the left box, left of the right box) unless one box is clearly
|
|
74
|
+
* above the other, in which case we use top/bottom.
|
|
75
|
+
* @param {{x:number,y:number,width:number,height:number}} a
|
|
76
|
+
* @param {{x:number,y:number,width:number,height:number}} b
|
|
77
|
+
* @returns {{ from: [number,number], to: [number,number] }}
|
|
78
|
+
*/
|
|
79
|
+
function anchors(a, b) {
|
|
80
|
+
var ac = [a.x + a.width / 2, a.y + a.height / 2];
|
|
81
|
+
var bc = [b.x + b.width / 2, b.y + b.height / 2];
|
|
82
|
+
var dx = bc[0] - ac[0];
|
|
83
|
+
var dy = bc[1] - ac[1];
|
|
84
|
+
if (Math.abs(dx) >= Math.abs(dy)) {
|
|
85
|
+
return dx >= 0
|
|
86
|
+
? { from: [a.x + a.width, ac[1]], to: [b.x, bc[1]] }
|
|
87
|
+
: { from: [a.x, ac[1]], to: [b.x + b.width, bc[1]] };
|
|
88
|
+
}
|
|
89
|
+
return dy >= 0
|
|
90
|
+
? { from: [ac[0], a.y + a.height], to: [bc[0], b.y] }
|
|
91
|
+
: { from: [ac[0], a.y], to: [bc[0], b.y + b.height] };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Crow's-foot decoration at one end of a line. `one` draws a single bar,
|
|
96
|
+
* `many` draws the three-pronged foot. Points are absolute coordinates.
|
|
97
|
+
* @param {"one"|"many"} cardinality
|
|
98
|
+
* @param {[number,number]} at
|
|
99
|
+
* @param {[number,number]} towards
|
|
100
|
+
* @returns {Array<Array<[number,number]>>} polylines, each an array of [x,y] points
|
|
101
|
+
*/
|
|
102
|
+
function foot(cardinality, at, towards) {
|
|
103
|
+
if (cardinality === "one") {
|
|
104
|
+
var ux = towards[0] - at[0];
|
|
105
|
+
var uy = towards[1] - at[1];
|
|
106
|
+
var len = Math.sqrt(ux * ux + uy * uy) || 1;
|
|
107
|
+
var nx = -uy / len * 6;
|
|
108
|
+
var ny = ux / len * 6;
|
|
109
|
+
return [[[at[0] + nx, at[1] + ny], [at[0] - nx, at[1] - ny]]];
|
|
110
|
+
}
|
|
111
|
+
// Three prongs: centre and two diagonals, all extending `12`px inward.
|
|
112
|
+
var vx = towards[0] - at[0];
|
|
113
|
+
var vy = towards[1] - at[1];
|
|
114
|
+
var m = Math.sqrt(vx * vx + vy * vy) || 1;
|
|
115
|
+
var fx = vx / m;
|
|
116
|
+
var fy = vy / m;
|
|
117
|
+
var px = -fy;
|
|
118
|
+
var py = fx;
|
|
119
|
+
return [
|
|
120
|
+
[[at[0] + fx * 12, at[1] + fy * 12], at],
|
|
121
|
+
[[at[0] + fx * 12 + px * 6, at[1] + fy * 12 + py * 6], at],
|
|
122
|
+
[[at[0] + fx * 12 - px * 6, at[1] + fy * 12 - py * 6], at],
|
|
123
|
+
];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Derive edges from the models' relations. M2M is deduplicated by pair so
|
|
128
|
+
* the same relation is not drawn twice (once from each end).
|
|
129
|
+
* @param {ModelDesc[]} models
|
|
130
|
+
* @returns {Array<{ from: string, to: string, type: string, name: string }>}
|
|
131
|
+
*/
|
|
132
|
+
function edges(models) {
|
|
133
|
+
/** @type {Array<{ from: string, to: string, type: string, name: string }>} */
|
|
134
|
+
var out = [];
|
|
135
|
+
var seen = Object.create(null);
|
|
136
|
+
var byKey = Object.create(null);
|
|
137
|
+
models.forEach(function (m) { byKey[m.key] = m; });
|
|
138
|
+
|
|
139
|
+
models.forEach(function (m) {
|
|
140
|
+
(m.relations || []).forEach(function (r) {
|
|
141
|
+
if (!byKey[r.model]) return; // model not in schema
|
|
142
|
+
var from, to;
|
|
143
|
+
if (r.type === "m2m") {
|
|
144
|
+
var pair = [m.key, r.model].sort().join("\u0000");
|
|
145
|
+
if (seen[pair]) return;
|
|
146
|
+
seen[pair] = true;
|
|
147
|
+
from = m.key; to = r.model;
|
|
148
|
+
} else if (r.type === "fk" || r.type === "o2o") {
|
|
149
|
+
// Mermaid draws the related model on the "one" side.
|
|
150
|
+
from = r.model; to = m.key;
|
|
151
|
+
} else {
|
|
152
|
+
from = r.model; to = m.key; // backward_fk / backward_o2o
|
|
153
|
+
}
|
|
154
|
+
out.push({ from: from, to: to, type: r.type, name: r.name });
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Cardinality of each end of a relationship, in Mermaid's notation.
|
|
162
|
+
* @param {string} type
|
|
163
|
+
* @returns {{ from: "one"|"many", to: "one"|"many" }}
|
|
164
|
+
*/
|
|
165
|
+
function cardinality(type) {
|
|
166
|
+
if (type === "m2m") return { from: "many", to: "many" };
|
|
167
|
+
if (type === "o2o" || type === "backward_o2o") return { from: "one", to: "one" };
|
|
168
|
+
return { from: "one", to: "many" };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Build the SVG path data for one relationship: one connector plus the
|
|
173
|
+
* crow's-foot bars at both ends. Returned as plain strings of `d` data so
|
|
174
|
+
* the DOM layer never has to do geometry.
|
|
175
|
+
* @param {{from:string,to:string,type:string,name:string}} edge
|
|
176
|
+
* @param {Array<{key:string,x:number,y:number,width:number,height:number}>} boxes
|
|
177
|
+
* @returns {{ paths: string[], mid: [number,number], type: string }}
|
|
178
|
+
*/
|
|
179
|
+
function edgePaths(edge, boxes) {
|
|
180
|
+
var a = boxes.find(function (b) { return b.key === edge.from; });
|
|
181
|
+
var b = boxes.find(function (x) { return x.key === edge.to; });
|
|
182
|
+
if (!a || !b) return { paths: [], mid: [0, 0], type: edge.type };
|
|
183
|
+
|
|
184
|
+
var pts = anchors(a, b);
|
|
185
|
+
var p = pts.from;
|
|
186
|
+
var q = pts.to;
|
|
187
|
+
var paths = ["M" + p[0] + "," + p[1] + " L" + q[0] + "," + q[1]];
|
|
188
|
+
var card = cardinality(edge.type);
|
|
189
|
+
foot(card.from, p, q).forEach(function (line) {
|
|
190
|
+
paths.push("M" + line[0][0] + "," + line[0][1] + " L" + line[1][0] + "," + line[1][1]);
|
|
191
|
+
});
|
|
192
|
+
foot(card.to, q, p).forEach(function (line) {
|
|
193
|
+
paths.push("M" + line[0][0] + "," + line[0][1] + " L" + line[1][0] + "," + line[1][1]);
|
|
194
|
+
});
|
|
195
|
+
return {
|
|
196
|
+
paths: paths,
|
|
197
|
+
mid: [(p[0] + q[0]) / 2, (p[1] + q[1]) / 2],
|
|
198
|
+
type: edge.type,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Overall SVG viewBox for a set of boxes, with a small margin.
|
|
204
|
+
* @param {Array<{x:number,y:number,width:number,height:number}>} boxes
|
|
205
|
+
* @returns {{ x: number, y: number, width: number, height: number }}
|
|
206
|
+
*/
|
|
207
|
+
function viewBox(boxes) {
|
|
208
|
+
if (!boxes.length) return { x: 0, y: 0, width: 400, height: 300 };
|
|
209
|
+
var minX = Math.min.apply(null, boxes.map(function (b) { return b.x; }));
|
|
210
|
+
var minY = Math.min.apply(null, boxes.map(function (b) { return b.y; }));
|
|
211
|
+
var maxX = Math.max.apply(null, boxes.map(function (b) { return b.x + b.width; }));
|
|
212
|
+
var maxY = Math.max.apply(null, boxes.map(function (b) { return b.y + b.height; }));
|
|
213
|
+
var pad = 24;
|
|
214
|
+
return {
|
|
215
|
+
x: minX - pad, y: minY - pad,
|
|
216
|
+
width: Math.max(1, maxX - minX + 2 * pad),
|
|
217
|
+
height: Math.max(1, maxY - minY + 2 * pad),
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
global.StudioErLayout = {
|
|
222
|
+
BOX_W: BOX_W, HEADER_H: HEADER_H, FIELD_H: FIELD_H, FOOTER_H: FOOTER_H, GAP_X: GAP_X,
|
|
223
|
+
GAP_Y: GAP_Y, COLUMNS: COLUMNS,
|
|
224
|
+
boxHeight: boxHeight, layout: layout, anchors: anchors, foot: foot,
|
|
225
|
+
edges: edges, cardinality: cardinality, edgePaths: edgePaths, viewBox: viewBox,
|
|
226
|
+
};
|
|
227
|
+
})(typeof window !== "undefined" ? window : globalThis);
|