@fibery/prosemirror-schema 4.20.0 → 4.21.0
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/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export { defaultHeight, defaultWidth } from "./src/resizable-node-attrs";
|
|
2
2
|
export { Node, Schema } from "prosemirror-model";
|
|
3
3
|
export { createSchema, Basic } from "./src/create-schema";
|
|
4
|
+
export { NODE_ATTR_CONSTRAINTS, validateAttrValue, validateAttrs } from "./src/attr-constraints";
|
|
5
|
+
export type { AttrConstraint, AttrConstraintType, AttrValidationIssue } from "./src/attr-constraints";
|
|
4
6
|
export { getBlockId, getBlockIdAttr, hasLegacyGuidAttr, buildBlockIdAttrs, getFiberyViewRef, FIBERY_VIEW_TYPE, } from "./src/block-id-utils";
|
|
5
7
|
export { DEFAULT_VIDEO_WIDTH, DEFAULT_VIDEO_HEIGHT } from "./src/nodes/video";
|
|
6
8
|
export type { EmojiOptions } from "./src/nodes/emoji";
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DEFAULT_VIDEO_HEIGHT = exports.DEFAULT_VIDEO_WIDTH = exports.FIBERY_VIEW_TYPE = exports.getFiberyViewRef = exports.buildBlockIdAttrs = exports.hasLegacyGuidAttr = exports.getBlockIdAttr = exports.getBlockId = exports.Basic = exports.createSchema = exports.Schema = exports.Node = exports.defaultWidth = exports.defaultHeight = void 0;
|
|
3
|
+
exports.DEFAULT_VIDEO_HEIGHT = exports.DEFAULT_VIDEO_WIDTH = exports.FIBERY_VIEW_TYPE = exports.getFiberyViewRef = exports.buildBlockIdAttrs = exports.hasLegacyGuidAttr = exports.getBlockIdAttr = exports.getBlockId = exports.validateAttrs = exports.validateAttrValue = exports.NODE_ATTR_CONSTRAINTS = exports.Basic = exports.createSchema = exports.Schema = exports.Node = exports.defaultWidth = exports.defaultHeight = void 0;
|
|
4
4
|
var resizable_node_attrs_1 = require("./src/resizable-node-attrs");
|
|
5
5
|
Object.defineProperty(exports, "defaultHeight", { enumerable: true, get: function () { return resizable_node_attrs_1.defaultHeight; } });
|
|
6
6
|
Object.defineProperty(exports, "defaultWidth", { enumerable: true, get: function () { return resizable_node_attrs_1.defaultWidth; } });
|
|
@@ -10,6 +10,10 @@ Object.defineProperty(exports, "Schema", { enumerable: true, get: function () {
|
|
|
10
10
|
var create_schema_1 = require("./src/create-schema");
|
|
11
11
|
Object.defineProperty(exports, "createSchema", { enumerable: true, get: function () { return create_schema_1.createSchema; } });
|
|
12
12
|
Object.defineProperty(exports, "Basic", { enumerable: true, get: function () { return create_schema_1.Basic; } });
|
|
13
|
+
var attr_constraints_1 = require("./src/attr-constraints");
|
|
14
|
+
Object.defineProperty(exports, "NODE_ATTR_CONSTRAINTS", { enumerable: true, get: function () { return attr_constraints_1.NODE_ATTR_CONSTRAINTS; } });
|
|
15
|
+
Object.defineProperty(exports, "validateAttrValue", { enumerable: true, get: function () { return attr_constraints_1.validateAttrValue; } });
|
|
16
|
+
Object.defineProperty(exports, "validateAttrs", { enumerable: true, get: function () { return attr_constraints_1.validateAttrs; } });
|
|
13
17
|
var block_id_utils_1 = require("./src/block-id-utils");
|
|
14
18
|
Object.defineProperty(exports, "getBlockId", { enumerable: true, get: function () { return block_id_utils_1.getBlockId; } });
|
|
15
19
|
Object.defineProperty(exports, "getBlockIdAttr", { enumerable: true, get: function () { return block_id_utils_1.getBlockIdAttr; } });
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type AttrConstraintType = "string" | "number" | "integer" | "boolean" | "null";
|
|
2
|
+
export type AttrConstraint = {
|
|
3
|
+
type?: ReadonlyArray<AttrConstraintType>;
|
|
4
|
+
enum?: ReadonlyArray<string | number | boolean | null>;
|
|
5
|
+
minimum?: number;
|
|
6
|
+
maximum?: number;
|
|
7
|
+
};
|
|
8
|
+
export declare const NODE_ATTR_CONSTRAINTS: Record<string, Record<string, AttrConstraint>>;
|
|
9
|
+
export declare const validateAttrValue: (nodeType: string, attr: string, value: unknown) => string | null;
|
|
10
|
+
export type AttrValidationIssue = {
|
|
11
|
+
attr: string;
|
|
12
|
+
message: string;
|
|
13
|
+
};
|
|
14
|
+
export declare const validateAttrs: (nodeType: string, attrs: Record<string, unknown>) => AttrValidationIssue[];
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Value constraints for node attributes, kept as data (JSON-Schema-shaped)
|
|
3
|
+
// next to the specs instead of prosemirror-model's attr `validate`: legacy
|
|
4
|
+
// documents already contain values that violate these constraints, so they
|
|
5
|
+
// must not run when a stored document is parsed (see the validator-stripping
|
|
6
|
+
// in create-schema.ts). Consumers apply them only to *incoming* changes (e.g.
|
|
7
|
+
// the collaboration block API validates operation payloads). Once broken
|
|
8
|
+
// documents are patched, the same table can be compiled into real `validate`
|
|
9
|
+
// functions on the specs.
|
|
10
|
+
//
|
|
11
|
+
// This module is deliberately free of prosemirror-model imports so it can be
|
|
12
|
+
// consumed by services pinned to older prosemirror-model versions.
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.validateAttrs = exports.validateAttrValue = exports.NODE_ATTR_CONSTRAINTS = void 0;
|
|
15
|
+
// Keyed by schema node-type name; attrs not listed here are unconstrained.
|
|
16
|
+
// Identifier attrs (id, guid, viewId) are deliberately absent — they are
|
|
17
|
+
// managed by the block-id machinery, not by user-supplied values.
|
|
18
|
+
exports.NODE_ATTR_CONSTRAINTS = {
|
|
19
|
+
heading: {
|
|
20
|
+
level: { type: ["integer"], minimum: 1, maximum: 6 },
|
|
21
|
+
},
|
|
22
|
+
check_item: {
|
|
23
|
+
state: { enum: ["TODO", "DONE"] },
|
|
24
|
+
},
|
|
25
|
+
list_item: {
|
|
26
|
+
state: { enum: ["TODO", "DONE", null] },
|
|
27
|
+
},
|
|
28
|
+
code_block: {
|
|
29
|
+
language: { type: ["string", "null"] },
|
|
30
|
+
wrapCode: { type: ["boolean"] },
|
|
31
|
+
mode: { enum: ["code", "split", "preview"] },
|
|
32
|
+
...resizableConstraints(),
|
|
33
|
+
},
|
|
34
|
+
callout: {
|
|
35
|
+
color: { type: ["string"] },
|
|
36
|
+
iconType: { type: ["string"] },
|
|
37
|
+
icon: { type: ["string"] },
|
|
38
|
+
},
|
|
39
|
+
ordered_list: {
|
|
40
|
+
order: { type: ["integer"], minimum: 0 },
|
|
41
|
+
},
|
|
42
|
+
column: {
|
|
43
|
+
columnRatio: { type: ["number"], minimum: 0 },
|
|
44
|
+
},
|
|
45
|
+
columns: {
|
|
46
|
+
...resizableConstraints(),
|
|
47
|
+
},
|
|
48
|
+
table_cell: cellConstraints(),
|
|
49
|
+
table_header: cellConstraints(),
|
|
50
|
+
media: {
|
|
51
|
+
url: { type: ["string"] },
|
|
52
|
+
compatibleMode: { type: ["boolean"] },
|
|
53
|
+
dynamicContent: { type: ["boolean"] },
|
|
54
|
+
aspectRatio: { type: ["number", "null"] },
|
|
55
|
+
...resizableConstraints(),
|
|
56
|
+
},
|
|
57
|
+
video: {
|
|
58
|
+
src: { type: ["string"] },
|
|
59
|
+
type: { type: ["string"] },
|
|
60
|
+
width: { type: ["number"], minimum: 0 },
|
|
61
|
+
height: { type: ["number"], minimum: 0 },
|
|
62
|
+
title: { type: ["string"] },
|
|
63
|
+
alignment: { type: ["string"] },
|
|
64
|
+
...resizableConstraints(),
|
|
65
|
+
},
|
|
66
|
+
imageBlock: {
|
|
67
|
+
src: { type: ["string"] },
|
|
68
|
+
alt: { type: ["string", "null"] },
|
|
69
|
+
title: { type: ["string", "null"] },
|
|
70
|
+
...resizableConstraints(),
|
|
71
|
+
},
|
|
72
|
+
fiberyView: {
|
|
73
|
+
...resizableConstraints(),
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
// The resizable attr set (resizable-node-attrs.ts). parseDOM stores string or
|
|
77
|
+
// null values in legacy documents, but new writes must be numeric/boolean;
|
|
78
|
+
// null stays allowed as the editor's own "unset" value.
|
|
79
|
+
function resizableConstraints() {
|
|
80
|
+
return {
|
|
81
|
+
nodeHeight: { type: ["number", "null"] },
|
|
82
|
+
nodeWidth: { type: ["number", "null"] },
|
|
83
|
+
nodePageWidth: { type: ["boolean", "null"] },
|
|
84
|
+
nodeFullWidth: { type: ["boolean", "null"] },
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
// prosemirror-tables cell attrs; colwidth (number[] | null) is out of scope
|
|
88
|
+
// of the scalar descriptor format and stays unconstrained.
|
|
89
|
+
function cellConstraints() {
|
|
90
|
+
return {
|
|
91
|
+
colspan: { type: ["integer"], minimum: 1 },
|
|
92
|
+
rowspan: { type: ["integer"], minimum: 1 },
|
|
93
|
+
background: { type: ["string", "null"] },
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
const matchesType = (type, value) => {
|
|
97
|
+
switch (type) {
|
|
98
|
+
case "string":
|
|
99
|
+
return typeof value === "string";
|
|
100
|
+
case "boolean":
|
|
101
|
+
return typeof value === "boolean";
|
|
102
|
+
case "number":
|
|
103
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
104
|
+
case "integer":
|
|
105
|
+
return typeof value === "number" && Number.isInteger(value);
|
|
106
|
+
case "null":
|
|
107
|
+
return value === null;
|
|
108
|
+
default:
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
const describeType = (type) => {
|
|
113
|
+
switch (type) {
|
|
114
|
+
case "integer":
|
|
115
|
+
return "an integer";
|
|
116
|
+
case "number":
|
|
117
|
+
return "a number";
|
|
118
|
+
case "string":
|
|
119
|
+
return "a string";
|
|
120
|
+
case "boolean":
|
|
121
|
+
return "a boolean";
|
|
122
|
+
case "null":
|
|
123
|
+
return "null";
|
|
124
|
+
default:
|
|
125
|
+
return String(type);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
const describeExpectation = (constraint) => {
|
|
129
|
+
if (constraint.enum !== undefined) {
|
|
130
|
+
return `one of [${constraint.enum.map((v) => JSON.stringify(v)).join(", ")}]`;
|
|
131
|
+
}
|
|
132
|
+
const types = (constraint.type ?? []).map(describeType).join(" or ");
|
|
133
|
+
if (constraint.minimum !== undefined && constraint.maximum !== undefined) {
|
|
134
|
+
return `${types} between ${constraint.minimum} and ${constraint.maximum}`;
|
|
135
|
+
}
|
|
136
|
+
if (constraint.minimum !== undefined) {
|
|
137
|
+
return `${types} of at least ${constraint.minimum}`;
|
|
138
|
+
}
|
|
139
|
+
if (constraint.maximum !== undefined) {
|
|
140
|
+
return `${types} of at most ${constraint.maximum}`;
|
|
141
|
+
}
|
|
142
|
+
return types;
|
|
143
|
+
};
|
|
144
|
+
const checkConstraint = (constraint, value) => {
|
|
145
|
+
if (constraint.enum !== undefined) {
|
|
146
|
+
return constraint.enum.includes(value);
|
|
147
|
+
}
|
|
148
|
+
if (constraint.type !== undefined && !constraint.type.some((type) => matchesType(type, value))) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
if (typeof value === "number") {
|
|
152
|
+
if (constraint.minimum !== undefined && value < constraint.minimum) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
if (constraint.maximum !== undefined && value > constraint.maximum) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
160
|
+
};
|
|
161
|
+
// null = valid (or unconstrained); otherwise a message like
|
|
162
|
+
// "must be an integer between 1 and 6, got [\"banana\"]" — the caller
|
|
163
|
+
// prefixes it with the node type and attr name.
|
|
164
|
+
const validateAttrValue = (nodeType, attr, value) => {
|
|
165
|
+
const constraint = exports.NODE_ATTR_CONSTRAINTS[nodeType]?.[attr];
|
|
166
|
+
if (constraint === undefined || checkConstraint(constraint, value)) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
return `must be ${describeExpectation(constraint)}, got [${JSON.stringify(value)}]`;
|
|
170
|
+
};
|
|
171
|
+
exports.validateAttrValue = validateAttrValue;
|
|
172
|
+
// Validates exactly the keys present in `attrs` — defaults and values already
|
|
173
|
+
// stored in a document are never re-checked, so documents with legacy-invalid
|
|
174
|
+
// attrs keep loading and stay editable on other keys.
|
|
175
|
+
const validateAttrs = (nodeType, attrs) => {
|
|
176
|
+
const issues = [];
|
|
177
|
+
for (const [attr, value] of Object.entries(attrs)) {
|
|
178
|
+
const message = (0, exports.validateAttrValue)(nodeType, attr, value);
|
|
179
|
+
if (message !== null) {
|
|
180
|
+
issues.push({ attr, message });
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return issues;
|
|
184
|
+
};
|
|
185
|
+
exports.validateAttrs = validateAttrs;
|
package/lib/src/create-schema.js
CHANGED
|
@@ -108,7 +108,7 @@ const createSchema = ({ sharedColorGenerator = null, headingToDOM = null, mediaT
|
|
|
108
108
|
actualNodes = actualNodes.update("list_item", {
|
|
109
109
|
...listItemSpec,
|
|
110
110
|
attrs: {
|
|
111
|
-
...
|
|
111
|
+
...listItemSpec.attrs,
|
|
112
112
|
state: { default: null },
|
|
113
113
|
id: { default: null },
|
|
114
114
|
},
|
|
@@ -224,7 +224,7 @@ const createSchema = ({ sharedColorGenerator = null, headingToDOM = null, mediaT
|
|
|
224
224
|
const spec = actualNodes.get(name);
|
|
225
225
|
actualNodes = actualNodes.update(name, {
|
|
226
226
|
...spec,
|
|
227
|
-
attrs: { ...
|
|
227
|
+
attrs: { ...spec.attrs, id: { default: null } },
|
|
228
228
|
});
|
|
229
229
|
}
|
|
230
230
|
return new prosemirror_model_1.Schema({
|
|
@@ -37,12 +37,9 @@ const createMark = (markType, sharedColorGenerator) => {
|
|
|
37
37
|
class: markClassName,
|
|
38
38
|
style: color ? `--${markType}-background-color:${color}` : ``,
|
|
39
39
|
"data-id": id,
|
|
40
|
-
...Object.keys(meta)
|
|
40
|
+
...Object.fromEntries(Object.keys(meta)
|
|
41
41
|
.filter((key) => meta[key] !== false) // remove specials
|
|
42
|
-
.
|
|
43
|
-
...out,
|
|
44
|
-
[`data-${hyphenatePascal(key)}`]: JSON.stringify(meta[key]),
|
|
45
|
-
}), {}),
|
|
42
|
+
.map((key) => [`data-${hyphenatePascal(key)}`, JSON.stringify(meta[key])])),
|
|
46
43
|
},
|
|
47
44
|
];
|
|
48
45
|
},
|