@plitzi/sdk-schema 0.37.8 → 0.37.10
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/CHANGELOG.md +825 -0
- package/dist/SchemaReducer.d.ts +34 -4
- package/dist/SchemaReducer.mjs +143 -96
- package/dist/helpers/FlatMap.d.ts +9 -3
- package/dist/helpers/FlatMap.mjs +11 -10
- package/dist/helpers/components.d.ts +68 -0
- package/dist/helpers/components.mjs +130 -0
- package/dist/helpers/elementId.d.ts +4 -4
- package/dist/helpers/elementId.mjs +2 -0
- package/dist/helpers/fitSnippet.d.ts +32 -0
- package/dist/helpers/fitSnippet.mjs +71 -0
- package/dist/helpers/schemaValidator.d.ts +3 -0
- package/dist/helpers/schemaValidator.mjs +289 -176
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +4 -3
- package/package.json +12 -4
package/dist/SchemaReducer.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Element, PageFolder, ReducerActionOrigin, Schema, SchemaVariable, DropPosition,
|
|
1
|
+
import { Element, PageFolder, ReducerActionOrigin, Schema, SnippetStyle, SchemaFlag, SchemaVariable, DropPosition, SpaceComponent, SpaceComponentDeclaration } from '@plitzi/sdk-shared';
|
|
2
2
|
export declare const SchemaActions: {
|
|
3
3
|
readonly SCHEMA_UPDATE: "SCHEMA_UPDATE";
|
|
4
4
|
readonly SCHEMA_ADD_PAGE: "SCHEMA_ADD_PAGE";
|
|
@@ -11,6 +11,8 @@ export declare const SchemaActions: {
|
|
|
11
11
|
readonly SCHEMA_ADD_VARIABLE: "SCHEMA_ADD_VARIABLE";
|
|
12
12
|
readonly SCHEMA_UPDATE_VARIABLE: "SCHEMA_UPDATE_VARIABLE";
|
|
13
13
|
readonly SCHEMA_REMOVE_VARIABLE: "SCHEMA_REMOVE_VARIABLE";
|
|
14
|
+
readonly SCHEMA_SET_FLAG: "SCHEMA_SET_FLAG";
|
|
15
|
+
readonly SCHEMA_REMOVE_FLAG: "SCHEMA_REMOVE_FLAG";
|
|
14
16
|
readonly SCHEMA_ADD_ELEMENT: "SCHEMA_ADD_ELEMENT";
|
|
15
17
|
readonly SCHEMA_REMOVE_ELEMENT: "SCHEMA_REMOVE_ELEMENT";
|
|
16
18
|
readonly SCHEMA_MOVE_ELEMENT: "SCHEMA_MOVE_ELEMENT";
|
|
@@ -18,8 +20,12 @@ export declare const SchemaActions: {
|
|
|
18
20
|
readonly SCHEMA_UPDATE_ELEMENT: "SCHEMA_UPDATE_ELEMENT";
|
|
19
21
|
readonly SCHEMA_RENAME_ELEMENT: "SCHEMA_RENAME_ELEMENT";
|
|
20
22
|
readonly SCHEMA_UPDATE_ELEMENTS: "SCHEMA_UPDATE_ELEMENTS";
|
|
21
|
-
readonly
|
|
23
|
+
readonly SCHEMA_ADD_SNIPPET: "SCHEMA_ADD_SNIPPET";
|
|
22
24
|
readonly SCHEMA_UPDATE_SETTINGS: "SCHEMA_UPDATE_SETTINGS";
|
|
25
|
+
readonly SCHEMA_ADD_COMPONENT: "SCHEMA_ADD_COMPONENT";
|
|
26
|
+
readonly SCHEMA_UPDATE_COMPONENT: "SCHEMA_UPDATE_COMPONENT";
|
|
27
|
+
readonly SCHEMA_REMOVE_COMPONENT: "SCHEMA_REMOVE_COMPONENT";
|
|
28
|
+
readonly SCHEMA_DETACH_INSTANCE: "SCHEMA_DETACH_INSTANCE";
|
|
23
29
|
};
|
|
24
30
|
export type SchemaReducerActionsBase = ReducerActionOrigin;
|
|
25
31
|
export type SchemaReducerActions = SchemaReducerActionsBase & ({
|
|
@@ -56,13 +62,20 @@ export type SchemaReducerActions = SchemaReducerActionsBase & ({
|
|
|
56
62
|
type: 'SCHEMA_REMOVE_VARIABLE';
|
|
57
63
|
name: string;
|
|
58
64
|
} | {
|
|
59
|
-
type: '
|
|
65
|
+
type: 'SCHEMA_SET_FLAG';
|
|
66
|
+
name: string;
|
|
67
|
+
flag: SchemaFlag;
|
|
68
|
+
} | {
|
|
69
|
+
type: 'SCHEMA_REMOVE_FLAG';
|
|
70
|
+
name: string;
|
|
71
|
+
} | {
|
|
72
|
+
type: 'SCHEMA_ADD_ELEMENT' | 'SCHEMA_ADD_SNIPPET';
|
|
60
73
|
to: string;
|
|
61
74
|
data: Element;
|
|
62
75
|
dropPosition: DropPosition;
|
|
63
76
|
initialItems: Record<string, Element>;
|
|
64
77
|
variables?: SchemaVariable[];
|
|
65
|
-
style?:
|
|
78
|
+
style?: SnippetStyle;
|
|
66
79
|
} | {
|
|
67
80
|
type: 'SCHEMA_REMOVE_ELEMENT';
|
|
68
81
|
elementId: string;
|
|
@@ -92,6 +105,23 @@ export type SchemaReducerActions = SchemaReducerActionsBase & ({
|
|
|
92
105
|
type: 'SCHEMA_UPDATE_SETTINGS';
|
|
93
106
|
path: string;
|
|
94
107
|
value: string | number | boolean;
|
|
108
|
+
} | {
|
|
109
|
+
type: 'SCHEMA_ADD_COMPONENT';
|
|
110
|
+
component: SpaceComponent;
|
|
111
|
+
from?: {
|
|
112
|
+
elementId: Element['id'];
|
|
113
|
+
instanceId: Element['id'];
|
|
114
|
+
};
|
|
115
|
+
} | {
|
|
116
|
+
type: 'SCHEMA_UPDATE_COMPONENT';
|
|
117
|
+
componentId: SpaceComponent['id'];
|
|
118
|
+
declaration: SpaceComponentDeclaration;
|
|
119
|
+
} | {
|
|
120
|
+
type: 'SCHEMA_REMOVE_COMPONENT';
|
|
121
|
+
componentId: SpaceComponent['id'];
|
|
122
|
+
} | {
|
|
123
|
+
type: 'SCHEMA_DETACH_INSTANCE';
|
|
124
|
+
instanceId: Element['id'];
|
|
95
125
|
});
|
|
96
126
|
declare const SchemaReducer: (state: Schema, action: SchemaReducerActions) => Schema;
|
|
97
127
|
export default SchemaReducer;
|
package/dist/SchemaReducer.mjs
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import n from "./helpers/
|
|
3
|
-
import { get as
|
|
4
|
-
import { produce as
|
|
1
|
+
import e from "./helpers/FlatMap.mjs";
|
|
2
|
+
import { addComponent as t, detachInstance as n, flatMapOf as r, removeComponent as i, renameElement as a, updateComponent as o } from "./helpers/components.mjs";
|
|
3
|
+
import { get as s, has as c, set as l } from "@plitzi/plitzi-ui/helpers";
|
|
4
|
+
import { produce as u } from "immer";
|
|
5
5
|
//#region src/SchemaReducer.ts
|
|
6
|
-
var
|
|
6
|
+
var d = (e, t) => {
|
|
7
7
|
if (t.length === 0) return;
|
|
8
8
|
let n = t.filter((t) => e.variables && !e.variables.find((e) => e.name === t.name));
|
|
9
|
-
|
|
10
|
-
},
|
|
9
|
+
l(e, "variables", [...e.variables ?? [], ...n]);
|
|
10
|
+
}, f = {
|
|
11
11
|
SCHEMA_UPDATE: "SCHEMA_UPDATE",
|
|
12
12
|
SCHEMA_ADD_PAGE: "SCHEMA_ADD_PAGE",
|
|
13
13
|
SCHEMA_HOME_PAGE: "SCHEMA_HOME_PAGE",
|
|
@@ -19,6 +19,8 @@ var s = (e, t) => {
|
|
|
19
19
|
SCHEMA_ADD_VARIABLE: "SCHEMA_ADD_VARIABLE",
|
|
20
20
|
SCHEMA_UPDATE_VARIABLE: "SCHEMA_UPDATE_VARIABLE",
|
|
21
21
|
SCHEMA_REMOVE_VARIABLE: "SCHEMA_REMOVE_VARIABLE",
|
|
22
|
+
SCHEMA_SET_FLAG: "SCHEMA_SET_FLAG",
|
|
23
|
+
SCHEMA_REMOVE_FLAG: "SCHEMA_REMOVE_FLAG",
|
|
22
24
|
SCHEMA_ADD_ELEMENT: "SCHEMA_ADD_ELEMENT",
|
|
23
25
|
SCHEMA_REMOVE_ELEMENT: "SCHEMA_REMOVE_ELEMENT",
|
|
24
26
|
SCHEMA_MOVE_ELEMENT: "SCHEMA_MOVE_ELEMENT",
|
|
@@ -26,145 +28,190 @@ var s = (e, t) => {
|
|
|
26
28
|
SCHEMA_UPDATE_ELEMENT: "SCHEMA_UPDATE_ELEMENT",
|
|
27
29
|
SCHEMA_RENAME_ELEMENT: "SCHEMA_RENAME_ELEMENT",
|
|
28
30
|
SCHEMA_UPDATE_ELEMENTS: "SCHEMA_UPDATE_ELEMENTS",
|
|
29
|
-
|
|
30
|
-
SCHEMA_UPDATE_SETTINGS: "SCHEMA_UPDATE_SETTINGS"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
SCHEMA_ADD_SNIPPET: "SCHEMA_ADD_SNIPPET",
|
|
32
|
+
SCHEMA_UPDATE_SETTINGS: "SCHEMA_UPDATE_SETTINGS",
|
|
33
|
+
SCHEMA_ADD_COMPONENT: "SCHEMA_ADD_COMPONENT",
|
|
34
|
+
SCHEMA_UPDATE_COMPONENT: "SCHEMA_UPDATE_COMPONENT",
|
|
35
|
+
SCHEMA_REMOVE_COMPONENT: "SCHEMA_REMOVE_COMPONENT",
|
|
36
|
+
SCHEMA_DETACH_INSTANCE: "SCHEMA_DETACH_INSTANCE"
|
|
37
|
+
}, p = (p, m) => {
|
|
38
|
+
switch (m.type) {
|
|
39
|
+
case f.SCHEMA_UPDATE: return {
|
|
40
|
+
...p,
|
|
41
|
+
...m.schema
|
|
36
42
|
};
|
|
37
|
-
case
|
|
38
|
-
let { page: e } =
|
|
39
|
-
return
|
|
40
|
-
|
|
43
|
+
case f.SCHEMA_ADD_PAGE: {
|
|
44
|
+
let { page: e } = m;
|
|
45
|
+
return u(p, (t) => {
|
|
46
|
+
l(t, "flat", {
|
|
41
47
|
...t.flat,
|
|
42
48
|
[e.id]: e
|
|
43
49
|
}), t.pages.push(e.id);
|
|
44
50
|
});
|
|
45
51
|
}
|
|
46
|
-
case
|
|
47
|
-
let { flat: e, pages: t } =
|
|
52
|
+
case f.SCHEMA_HOME_PAGE: {
|
|
53
|
+
let { flat: e, pages: t } = p, { pageId: n } = m, r;
|
|
48
54
|
return t.forEach((t) => {
|
|
49
|
-
if (
|
|
55
|
+
if (r) return;
|
|
50
56
|
let n = e[t];
|
|
51
|
-
|
|
52
|
-
}),
|
|
53
|
-
|
|
54
|
-
}) :
|
|
55
|
-
|
|
57
|
+
s(n, "attributes.default", !1) && (r = n);
|
|
58
|
+
}), r ? u(p, (e) => {
|
|
59
|
+
l(e.flat, `${n}.attributes.default`, !0), l(e.flat, `${r.id}.attributes.default`, !1);
|
|
60
|
+
}) : u(p, (e) => {
|
|
61
|
+
l(e.flat, `${n}.attributes.default`, !0);
|
|
56
62
|
});
|
|
57
63
|
}
|
|
58
|
-
case
|
|
59
|
-
let { page: e } =
|
|
60
|
-
return
|
|
61
|
-
|
|
64
|
+
case f.SCHEMA_UPDATE_PAGE: {
|
|
65
|
+
let { page: e } = m;
|
|
66
|
+
return u(p, (t) => {
|
|
67
|
+
l(t.flat, e.id, e);
|
|
62
68
|
});
|
|
63
69
|
}
|
|
64
|
-
case
|
|
65
|
-
let { pageId:
|
|
66
|
-
return
|
|
67
|
-
|
|
70
|
+
case f.SCHEMA_REMOVE_PAGE: {
|
|
71
|
+
let { pageId: t } = m;
|
|
72
|
+
return u(p, (n) => {
|
|
73
|
+
e.removeElement(n.flat, t, !0), n.pages = n.pages.filter((e) => e !== t);
|
|
68
74
|
});
|
|
69
75
|
}
|
|
70
|
-
case
|
|
71
|
-
let { pageFolder: e } =
|
|
72
|
-
return
|
|
76
|
+
case f.SCHEMA_ADD_PAGE_FOLDER: {
|
|
77
|
+
let { pageFolder: e } = m;
|
|
78
|
+
return u(p, (t) => {
|
|
73
79
|
t.pageFolders.push(e);
|
|
74
80
|
});
|
|
75
81
|
}
|
|
76
|
-
case
|
|
77
|
-
let { pageFolder: e } =
|
|
78
|
-
return
|
|
82
|
+
case f.SCHEMA_UPDATE_PAGE_FOLDER: {
|
|
83
|
+
let { pageFolder: e } = m;
|
|
84
|
+
return u(p, (t) => {
|
|
79
85
|
let n = t.pageFolders.findIndex((t) => t.id === e.id);
|
|
80
86
|
n !== -1 && (t.pageFolders[n] = e);
|
|
81
87
|
});
|
|
82
88
|
}
|
|
83
|
-
case
|
|
84
|
-
let { pageFolderId: e } =
|
|
85
|
-
return
|
|
89
|
+
case f.SCHEMA_REMOVE_PAGE_FOLDER: {
|
|
90
|
+
let { pageFolderId: e } = m;
|
|
91
|
+
return u(p, (t) => {
|
|
86
92
|
t.pageFolders = t.pageFolders.filter((t) => t.id !== e);
|
|
87
93
|
});
|
|
88
94
|
}
|
|
89
|
-
case
|
|
90
|
-
let { variable: e } =
|
|
91
|
-
return
|
|
95
|
+
case f.SCHEMA_ADD_VARIABLE: {
|
|
96
|
+
let { variable: e } = m;
|
|
97
|
+
return u(p, (t) => {
|
|
92
98
|
t.variables ||= [], t.variables.push(e);
|
|
93
99
|
});
|
|
94
100
|
}
|
|
95
|
-
case
|
|
96
|
-
let { variable: e } =
|
|
97
|
-
return
|
|
101
|
+
case f.SCHEMA_UPDATE_VARIABLE: {
|
|
102
|
+
let { variable: e } = m;
|
|
103
|
+
return p.variables ? u(p, (t) => {
|
|
98
104
|
let n = t.variables.findIndex((t) => t.name === e.name);
|
|
99
105
|
t.variables[n] && (t.variables[n] = e);
|
|
100
|
-
}) :
|
|
106
|
+
}) : p;
|
|
101
107
|
}
|
|
102
|
-
case
|
|
103
|
-
let { name: e } =
|
|
104
|
-
return
|
|
108
|
+
case f.SCHEMA_REMOVE_VARIABLE: {
|
|
109
|
+
let { name: e } = m;
|
|
110
|
+
return p.variables ? u(p, (t) => {
|
|
105
111
|
t.variables = t.variables.filter((t) => t.name !== e);
|
|
106
|
-
}) :
|
|
112
|
+
}) : p;
|
|
107
113
|
}
|
|
108
|
-
case
|
|
109
|
-
let {
|
|
110
|
-
return
|
|
111
|
-
n.
|
|
114
|
+
case f.SCHEMA_SET_FLAG: {
|
|
115
|
+
let { name: e, flag: t } = m;
|
|
116
|
+
return u(p, (n) => {
|
|
117
|
+
n.flags = {
|
|
118
|
+
...n.flags,
|
|
119
|
+
[e]: t
|
|
120
|
+
};
|
|
112
121
|
});
|
|
113
122
|
}
|
|
114
|
-
case
|
|
115
|
-
let {
|
|
116
|
-
return
|
|
117
|
-
let
|
|
118
|
-
|
|
119
|
-
...c
|
|
120
|
-
}), u = t(o.flat), f = e(l, (e) => u.has(e))[i.id] ?? i.id, { [f]: p, ...m } = l;
|
|
121
|
-
n.addElement(o.flat, p, r, a, m), s(o, d);
|
|
123
|
+
case f.SCHEMA_REMOVE_FLAG: {
|
|
124
|
+
let { name: e } = m;
|
|
125
|
+
return !p.flags || !Object.hasOwn(p.flags, e) ? p : u(p, (t) => {
|
|
126
|
+
let { [e]: n, ...r } = t.flags ?? {};
|
|
127
|
+
t.flags = r;
|
|
122
128
|
});
|
|
123
129
|
}
|
|
124
|
-
case
|
|
125
|
-
let {
|
|
126
|
-
return
|
|
127
|
-
|
|
130
|
+
case f.SCHEMA_ADD_ELEMENT: {
|
|
131
|
+
let { to: e, data: t, dropPosition: n, initialItems: i, variables: a = [] } = m;
|
|
132
|
+
return u(p, (o) => {
|
|
133
|
+
r(o, e)?.addElement(t, e, n, i), d(o, a);
|
|
128
134
|
});
|
|
129
135
|
}
|
|
130
|
-
case
|
|
131
|
-
let {
|
|
132
|
-
return
|
|
133
|
-
|
|
136
|
+
case f.SCHEMA_ADD_SNIPPET: {
|
|
137
|
+
let { to: e, data: t, dropPosition: n, initialItems: i, variables: a = [] } = m;
|
|
138
|
+
return u(p, (o) => {
|
|
139
|
+
let { [t.id]: s, ...c } = structuredClone({
|
|
140
|
+
...i,
|
|
141
|
+
[t.id]: t
|
|
142
|
+
});
|
|
143
|
+
r(o, e)?.addElement(s, e, n, c) && d(o, a);
|
|
134
144
|
});
|
|
135
145
|
}
|
|
136
|
-
case
|
|
137
|
-
let {
|
|
138
|
-
return
|
|
139
|
-
|
|
146
|
+
case f.SCHEMA_REMOVE_ELEMENT: {
|
|
147
|
+
let { elementId: e } = m;
|
|
148
|
+
return u(p, (t) => {
|
|
149
|
+
r(t, e)?.removeElement(e);
|
|
140
150
|
});
|
|
141
151
|
}
|
|
142
|
-
case
|
|
143
|
-
let {
|
|
144
|
-
return
|
|
145
|
-
|
|
152
|
+
case f.SCHEMA_MOVE_ELEMENT: {
|
|
153
|
+
let { from: e, to: t, elementId: n, dropPosition: i } = m;
|
|
154
|
+
return u(p, (a) => {
|
|
155
|
+
let o = r(a, n);
|
|
156
|
+
o && Object.hasOwn(o.flat, t) && o.moveElement(e, t, n, i);
|
|
146
157
|
});
|
|
147
158
|
}
|
|
148
|
-
case
|
|
149
|
-
let {
|
|
150
|
-
return
|
|
151
|
-
|
|
159
|
+
case f.SCHEMA_CLONE_ELEMENT: {
|
|
160
|
+
let { to: e, data: t, dropPosition: n, initialItems: i } = m;
|
|
161
|
+
return u(p, (a) => {
|
|
162
|
+
r(a, e)?.addElement(t, e, n, i);
|
|
152
163
|
});
|
|
153
164
|
}
|
|
154
|
-
case
|
|
155
|
-
let {
|
|
156
|
-
return
|
|
157
|
-
e.
|
|
165
|
+
case f.SCHEMA_UPDATE_ELEMENT: {
|
|
166
|
+
let { element: e } = m;
|
|
167
|
+
return u(p, (t) => {
|
|
168
|
+
r(t, e.id)?.updateElement(e);
|
|
158
169
|
});
|
|
159
170
|
}
|
|
160
|
-
case
|
|
161
|
-
let {
|
|
162
|
-
return
|
|
163
|
-
|
|
171
|
+
case f.SCHEMA_RENAME_ELEMENT: {
|
|
172
|
+
let { elementId: e, id: t } = m;
|
|
173
|
+
return u(p, (n) => {
|
|
174
|
+
a(n, e, t);
|
|
164
175
|
});
|
|
165
176
|
}
|
|
166
|
-
|
|
177
|
+
case f.SCHEMA_UPDATE_ELEMENTS: {
|
|
178
|
+
let { elements: e } = m;
|
|
179
|
+
return u(p, (t) => {
|
|
180
|
+
e.forEach((e) => r(t, e.id)?.updateElement(e));
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
case f.SCHEMA_UPDATE_SETTINGS: {
|
|
184
|
+
let { path: e, value: t } = m;
|
|
185
|
+
return u(p, (n) => {
|
|
186
|
+
e && c(p.settings, e) ? l(n.settings, e, t) : e || l(n, "settings", t);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
case f.SCHEMA_ADD_COMPONENT: {
|
|
190
|
+
let { component: e, from: n } = m;
|
|
191
|
+
return u(p, (r) => {
|
|
192
|
+
t(r, structuredClone(e), n);
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
case f.SCHEMA_UPDATE_COMPONENT: {
|
|
196
|
+
let { componentId: e, declaration: t } = m;
|
|
197
|
+
return u(p, (n) => {
|
|
198
|
+
o(n, e, t);
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
case f.SCHEMA_REMOVE_COMPONENT: {
|
|
202
|
+
let { componentId: e } = m;
|
|
203
|
+
return u(p, (t) => {
|
|
204
|
+
i(t, e);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
case f.SCHEMA_DETACH_INSTANCE: {
|
|
208
|
+
let { instanceId: e } = m;
|
|
209
|
+
return u(p, (t) => {
|
|
210
|
+
n(t, e);
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
default: return p;
|
|
167
214
|
}
|
|
168
215
|
};
|
|
169
216
|
//#endregion
|
|
170
|
-
export {
|
|
217
|
+
export { f as SchemaActions, p as default };
|
|
@@ -9,9 +9,14 @@ export type FlatMapProps = {
|
|
|
9
9
|
variables?: Schema['variables'];
|
|
10
10
|
/**
|
|
11
11
|
* The document's page list. Given, a page rename rewrites it too — a page renamed without it is a page the space
|
|
12
|
-
* no longer lists. Left out for a map that holds no pages (a
|
|
12
|
+
* no longer lists. Left out for a map that holds no pages (a component's tree, a snippet being cut).
|
|
13
13
|
*/
|
|
14
14
|
pages?: Schema['pages'];
|
|
15
|
+
/**
|
|
16
|
+
* The ids the rest of the document holds, for a map over one of its trees: ids are one namespace across the space's
|
|
17
|
+
* `flat` and every component's, so a name free in this tree may still be taken in another.
|
|
18
|
+
*/
|
|
19
|
+
takenElsewhere?: (id: Element['id']) => boolean;
|
|
15
20
|
/**
|
|
16
21
|
* How an id is minted for an element nobody named. Defaults to the random minter, which is what a live document
|
|
17
22
|
* wants: the builder and the MCP write concurrently, and a counter has two writers pick the same name. An
|
|
@@ -24,6 +29,7 @@ declare class FlatMap {
|
|
|
24
29
|
variables: Schema['variables'];
|
|
25
30
|
pages: Schema['pages'];
|
|
26
31
|
mintId: MintElementId;
|
|
32
|
+
takenElsewhere: (id: Element['id']) => boolean;
|
|
27
33
|
constructor(props?: FlatMapProps);
|
|
28
34
|
/** A free id for a new element of `type`, minted through this map's minter and unique against what it holds. */
|
|
29
35
|
nextId: (type: string, alsoTaken?: (candidate: string) => boolean) => string;
|
|
@@ -79,7 +85,7 @@ declare class FlatMap {
|
|
|
79
85
|
removeVariables: (variables: string[]) => boolean;
|
|
80
86
|
removeVariable: (variable: string) => boolean;
|
|
81
87
|
isValidElement: (element?: Partial<Element>) => boolean;
|
|
82
|
-
|
|
88
|
+
flatAsSnippet: (style: Style, elementId: Element["id"], excludeRoot?: boolean) => {
|
|
83
89
|
elements: {
|
|
84
90
|
acum: Record<Element["id"], Element>;
|
|
85
91
|
item?: Element;
|
|
@@ -102,7 +108,7 @@ declare class FlatMap {
|
|
|
102
108
|
static addVariable: (schemaVariables: Schema["variables"], variable: SchemaVariable) => boolean;
|
|
103
109
|
static updateVariable: (schemaVariables: Schema["variables"], variable: SchemaVariable) => boolean;
|
|
104
110
|
static removeVariable: (schemaVariables: Schema["variables"], variable: string) => boolean;
|
|
105
|
-
static
|
|
111
|
+
static flatAsSnippet: (schema: Schema, style: Style, elementId: Element["id"], excludeRoot?: boolean) => {
|
|
106
112
|
elements: {
|
|
107
113
|
acum: Record<Element["id"], Element>;
|
|
108
114
|
item?: Element;
|
package/dist/helpers/FlatMap.mjs
CHANGED
|
@@ -9,21 +9,22 @@ var d = class {
|
|
|
9
9
|
variables;
|
|
10
10
|
pages;
|
|
11
11
|
mintId;
|
|
12
|
+
takenElsewhere;
|
|
12
13
|
constructor(e = {}) {
|
|
13
|
-
let { flat: t, variables: r, pages: i, mintId: a } = e;
|
|
14
|
+
let { flat: t, variables: r, pages: i, mintId: a, takenElsewhere: o } = e;
|
|
14
15
|
if (!t) throw Error("Flat is required");
|
|
15
|
-
this.flat = t, this.variables = r ?? [], this.pages = i ?? [], this.mintId = a ?? n;
|
|
16
|
+
this.flat = t, this.variables = r ?? [], this.pages = i ?? [], this.mintId = a ?? n, this.takenElsewhere = o ?? (() => !1);
|
|
16
17
|
}
|
|
17
18
|
nextId = (e, t = () => !1) => {
|
|
18
19
|
let n = i(this.flat);
|
|
19
|
-
return this.mintId(e, (e) => n.has(e) || t(e));
|
|
20
|
+
return this.mintId(e, (e) => n.has(e) || this.takenElsewhere(e) || t(e));
|
|
20
21
|
};
|
|
21
22
|
addElement = (e, n, r = "inside", i = {}) => {
|
|
22
23
|
let a = e.id ? e : {
|
|
23
24
|
...e,
|
|
24
25
|
id: this.nextId(e.definition.type)
|
|
25
|
-
};
|
|
26
|
-
if (!this.isValidElement(a) || !t(this.flat,
|
|
26
|
+
}, o = [a, ...Object.values(i)];
|
|
27
|
+
if (!this.isValidElement(a) || !t(this.flat, o) || o.some((e) => this.takenElsewhere(e.id))) return !1;
|
|
27
28
|
if (r !== "custom") {
|
|
28
29
|
let e = this.placement(n, r, [a.id]);
|
|
29
30
|
if (!e) return !1;
|
|
@@ -47,7 +48,7 @@ var d = class {
|
|
|
47
48
|
});
|
|
48
49
|
};
|
|
49
50
|
updateElement = (e) => !e || !this.flat[e.id] ? !1 : (this.flat[e.id] = e, !0);
|
|
50
|
-
renameElement = (t, n) => this.flat[t] ? t === n ? [] :
|
|
51
|
+
renameElement = (t, n) => this.flat[t] ? t === n ? [] : e(this.flat, n) || this.takenElsewhere(n) ? !1 : r(this.flat, { [t]: n }, this.pages) : !1;
|
|
51
52
|
moveElement = (e, t, n, r = "inside") => {
|
|
52
53
|
let i = this.flat[e], a = this.flat[n];
|
|
53
54
|
if (!i || !a || n === t || s(this.flat, t).includes(n)) return !1;
|
|
@@ -65,7 +66,7 @@ var d = class {
|
|
|
65
66
|
if (!u) return c;
|
|
66
67
|
let d = [e, ...o(this.flat, e)], f = i(this.flat);
|
|
67
68
|
for (let e of d) {
|
|
68
|
-
let t = a(e, (e) => f.has(e));
|
|
69
|
+
let t = a(e, (e) => f.has(e) || this.takenElsewhere(e));
|
|
69
70
|
f.add(t), l[e] = t;
|
|
70
71
|
}
|
|
71
72
|
let p = {};
|
|
@@ -110,7 +111,7 @@ var d = class {
|
|
|
110
111
|
let { type: i, label: a, styleSelectors: o, rootId: s } = r;
|
|
111
112
|
return !(!i || a === void 0 || typeof o != "object" || s === void 0);
|
|
112
113
|
};
|
|
113
|
-
|
|
114
|
+
flatAsSnippet = (e, t, n = !1) => {
|
|
114
115
|
let r = {
|
|
115
116
|
...l,
|
|
116
117
|
platform: {
|
|
@@ -187,12 +188,12 @@ var d = class {
|
|
|
187
188
|
static addVariable = (e, t) => this.getInstance({ variables: e }).addVariable(t);
|
|
188
189
|
static updateVariable = (e, t) => this.getInstance({ variables: e }).updateVariable(t);
|
|
189
190
|
static removeVariable = (e, t) => this.getInstance({ variables: e }).removeVariable(t);
|
|
190
|
-
static
|
|
191
|
+
static flatAsSnippet = (e, t, n, r = !1) => {
|
|
191
192
|
let { flat: i, variables: a } = e;
|
|
192
193
|
return this.getInstance({
|
|
193
194
|
flat: i,
|
|
194
195
|
variables: a
|
|
195
|
-
}).
|
|
196
|
+
}).flatAsSnippet(t, n, r);
|
|
196
197
|
};
|
|
197
198
|
};
|
|
198
199
|
//#endregion
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { default as FlatMap } from './FlatMap';
|
|
2
|
+
import { MintElementId } from './elementId';
|
|
3
|
+
import { Element, Schema, SpaceComponent, SpaceComponentDeclaration } from '@plitzi/sdk-shared';
|
|
4
|
+
/** The parts of a schema that hold elements: the pages' tree and every component's. */
|
|
5
|
+
export type ElementDocument = Pick<Schema, 'flat' | 'pages' | 'components'>;
|
|
6
|
+
/** One tree of a document: the space's own `flat`, or the one of the component named. */
|
|
7
|
+
export type ElementTree = {
|
|
8
|
+
flat: Schema['flat'];
|
|
9
|
+
componentId?: SpaceComponent['id'];
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Attributes an instance already answers to, so a prop may not be called one of them: a prop IS an attribute of the
|
|
13
|
+
* instance, and a prop named `referenceId` would be the instance pointing at another component.
|
|
14
|
+
*/
|
|
15
|
+
export declare const RESERVED_PROP_NAMES: ReadonlySet<string>;
|
|
16
|
+
/** Why `name` cannot be a prop, phrased for a person, or null when it can. */
|
|
17
|
+
export declare const propNameProblem: (name: string) => string | null;
|
|
18
|
+
export declare const treesOf: (schema: Pick<Schema, "flat" | "components">) => ElementTree[];
|
|
19
|
+
/** The tree `id` lives in. Ids are one namespace across the document, so there is at most one. */
|
|
20
|
+
export declare const treeOf: (schema: Pick<Schema, "flat" | "components">, id: Element["id"]) => ElementTree | undefined;
|
|
21
|
+
/** Every element id the document holds, in any tree. */
|
|
22
|
+
export declare const documentIds: (schema: Pick<Schema, "flat" | "components">) => Set<Element["id"]>;
|
|
23
|
+
/**
|
|
24
|
+
* A `FlatMap` over the tree that holds `id`, held to the ids every other tree holds.
|
|
25
|
+
*
|
|
26
|
+
* What lets an element edit carry nothing but the element: its id already says which tree it is in, so the writers
|
|
27
|
+
* of a space — the builder's reducer and the server's model — resolve the tree here and edit it as they always have.
|
|
28
|
+
*/
|
|
29
|
+
export declare const flatMapOf: (schema: ElementDocument, id: Element["id"], mintId?: MintElementId) => FlatMap | undefined;
|
|
30
|
+
/** The component declared under `componentId`, if any: a key read off a document cannot be trusted to be there. */
|
|
31
|
+
export declare const componentNamed: (schema: Pick<Schema, "components">, componentId: SpaceComponent["id"]) => SpaceComponent | undefined;
|
|
32
|
+
/** Whether `element` is an instance — of `componentId`, when one is named. */
|
|
33
|
+
export declare const isInstance: (element: Element, componentId?: SpaceComponent["id"]) => boolean;
|
|
34
|
+
/** Every instance of a component, wherever it is placed: on a page, in a layout or inside another component. */
|
|
35
|
+
export declare const instancesOf: (schema: Pick<Schema, "flat" | "components">, componentId: SpaceComponent["id"]) => Element[];
|
|
36
|
+
/**
|
|
37
|
+
* Renames an element wherever it is, carrying every reference to it — in any tree.
|
|
38
|
+
*
|
|
39
|
+
* The element's own tree is rewritten by `FlatMap`, as a rename always was; the others are rewritten too, because a
|
|
40
|
+
* page can name an element of a component as little as a component can name a page, and the declarations name their
|
|
41
|
+
* roots and slots. Returns the ids of everything it touched, under their new names.
|
|
42
|
+
*/
|
|
43
|
+
export declare const renameElement: (schema: ElementDocument, from: Element["id"], to: Element["id"]) => Element["id"][] | false;
|
|
44
|
+
/**
|
|
45
|
+
* Declares a component.
|
|
46
|
+
*
|
|
47
|
+
* With `from`, the component is made OF an element already in the document: the subtree leaves its tree for the
|
|
48
|
+
* component's, and an instance named `from.instanceId` takes its place, so the page renders what it rendered. Without
|
|
49
|
+
* it, the component brings its own tree. Both sides of a live document apply the same call and arrive at the same
|
|
50
|
+
* document, which is why the instance's name is the caller's to give. Returns false, changing nothing, when the
|
|
51
|
+
* component cannot be declared — a name taken, a tree that is not one, a declaration its tree does not bear out.
|
|
52
|
+
*/
|
|
53
|
+
export declare const addComponent: (schema: ElementDocument, component: SpaceComponent, from?: {
|
|
54
|
+
elementId: Element["id"];
|
|
55
|
+
instanceId: Element["id"];
|
|
56
|
+
}) => boolean;
|
|
57
|
+
/** Changes what a component declares — never its tree, which is edited element by element like any other. */
|
|
58
|
+
export declare const updateComponent: (schema: Pick<Schema, "components">, componentId: SpaceComponent["id"], declaration: SpaceComponentDeclaration) => boolean;
|
|
59
|
+
/** Removes a component nothing places any more. One still placed is refused: its instances would render nothing. */
|
|
60
|
+
export declare const removeComponent: (schema: Pick<Schema, "flat" | "components">, componentId: SpaceComponent["id"]) => boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Replaces an instance with a copy of what its component renders, which is then the page's own to edit.
|
|
63
|
+
*
|
|
64
|
+
* The copy takes names derived from the component's (`card` → `card-2`), each the first one free in the document,
|
|
65
|
+
* so every side of a live document that applies it arrives at the same names. What filled the instance's slots goes
|
|
66
|
+
* into the copies of those slots. Refused when a child has no slot to go to. Returns the copy's root id.
|
|
67
|
+
*/
|
|
68
|
+
export declare const detachInstance: (schema: ElementDocument, instanceId: Element["id"]) => Element["id"] | false;
|