@fibery/prosemirror-schema 4.20.0 → 4.22.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,10 @@
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";
8
+ export { DEFAULT_CALLOUT_ICON, DEFAULT_CALLOUT_ICON_TYPE } from "./src/nodes/callout";
9
+ export { ColorToken } from "./src/marks/text-background-color";
6
10
  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.ColorToken = exports.DEFAULT_CALLOUT_ICON_TYPE = exports.DEFAULT_CALLOUT_ICON = 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; } });
@@ -20,3 +24,8 @@ Object.defineProperty(exports, "FIBERY_VIEW_TYPE", { enumerable: true, get: func
20
24
  var video_1 = require("./src/nodes/video");
21
25
  Object.defineProperty(exports, "DEFAULT_VIDEO_WIDTH", { enumerable: true, get: function () { return video_1.DEFAULT_VIDEO_WIDTH; } });
22
26
  Object.defineProperty(exports, "DEFAULT_VIDEO_HEIGHT", { enumerable: true, get: function () { return video_1.DEFAULT_VIDEO_HEIGHT; } });
27
+ var callout_1 = require("./src/nodes/callout");
28
+ Object.defineProperty(exports, "DEFAULT_CALLOUT_ICON", { enumerable: true, get: function () { return callout_1.DEFAULT_CALLOUT_ICON; } });
29
+ Object.defineProperty(exports, "DEFAULT_CALLOUT_ICON_TYPE", { enumerable: true, get: function () { return callout_1.DEFAULT_CALLOUT_ICON_TYPE; } });
30
+ var text_background_color_1 = require("./src/marks/text-background-color");
31
+ Object.defineProperty(exports, "ColorToken", { enumerable: true, get: function () { return text_background_color_1.ColorToken; } });
@@ -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;
@@ -60,7 +60,7 @@ const createSchema = ({ sharedColorGenerator = null, headingToDOM = null, mediaT
60
60
  video: video_1.video,
61
61
  file: file_1.file,
62
62
  emoji: (0, emoji_1.makeEmoji)(emojiOptions),
63
- callout: callout_1.callout,
63
+ callout: (0, callout_1.makeCallout)(emojiOptions),
64
64
  columns: columns_1.columns,
65
65
  column: column_1.column,
66
66
  math_display: math_display_1.math_display,
@@ -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
- ...(listItemSpec.attrs ?? {}),
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: { ...(spec.attrs ?? {}), id: { default: null } },
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
- .reduce((out, key) => ({
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
  },
@@ -1,2 +1,13 @@
1
1
  import type { MarkSpec } from "prosemirror-model";
2
+ export declare enum ColorToken {
3
+ Yellow = "YELLOW",
4
+ Green = "GREEN",
5
+ Aquamarine = "AQUAMARINE",
6
+ Aqua = "AQUA",
7
+ Blue = "BLUE",
8
+ Purple = "PURPLE",
9
+ Pink = "PINK",
10
+ Red = "RED",
11
+ Grey = "GREY"
12
+ }
2
13
  export declare const textBackgroundColor: MarkSpec;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.textBackgroundColor = void 0;
3
+ exports.textBackgroundColor = exports.ColorToken = void 0;
4
4
  var ColorToken;
5
5
  (function (ColorToken) {
6
6
  ColorToken["Yellow"] = "YELLOW";
@@ -12,7 +12,7 @@ var ColorToken;
12
12
  ColorToken["Pink"] = "PINK";
13
13
  ColorToken["Red"] = "RED";
14
14
  ColorToken["Grey"] = "GREY";
15
- })(ColorToken || (ColorToken = {}));
15
+ })(ColorToken || (exports.ColorToken = ColorToken = {}));
16
16
  const isColorToken = (text) => {
17
17
  return Object.values(ColorToken).includes(text);
18
18
  };
@@ -1,4 +1,7 @@
1
- export declare const callout: {
1
+ import type { EmojiOptions } from "./emoji";
2
+ export declare const DEFAULT_CALLOUT_ICON_TYPE = "icon";
3
+ export declare const DEFAULT_CALLOUT_ICON = "exclamation-circle";
4
+ export declare const makeCallout: ({ getEmoji }?: EmojiOptions) => {
2
5
  attrs: {
3
6
  color: {
4
7
  default: string;
@@ -21,10 +24,5 @@ export declare const callout: {
21
24
  icon: string | null;
22
25
  };
23
26
  }[];
24
- toDOM(node: import("prosemirror-model").Node): [string, {
25
- "data-callout-node": string;
26
- "data-color": any;
27
- "data-icon-type": any;
28
- "data-icon": any;
29
- }, number];
27
+ toDOM(node: import("prosemirror-model").Node): [string, Record<string, string>, number];
30
28
  };
@@ -1,12 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.callout = void 0;
3
+ exports.makeCallout = exports.DEFAULT_CALLOUT_ICON = exports.DEFAULT_CALLOUT_ICON_TYPE = void 0;
4
4
  const nodeType = "data-callout-node";
5
- exports.callout = {
5
+ exports.DEFAULT_CALLOUT_ICON_TYPE = "icon";
6
+ exports.DEFAULT_CALLOUT_ICON = "exclamation-circle";
7
+ const aliasAttr = "data-icon-emoji-alias";
8
+ const defaultOptions = {
9
+ getEmoji() {
10
+ return;
11
+ },
12
+ };
13
+ const makeCallout = ({ getEmoji } = defaultOptions) => ({
6
14
  attrs: {
7
15
  color: { default: "#199EE3" },
8
- iconType: { default: "icon" },
9
- icon: { default: "exclamation-circle" },
16
+ iconType: { default: exports.DEFAULT_CALLOUT_ICON_TYPE },
17
+ icon: { default: exports.DEFAULT_CALLOUT_ICON },
10
18
  },
11
19
  group: "block",
12
20
  content: "block+",
@@ -16,11 +24,19 @@ exports.callout = {
16
24
  tag: `div[${nodeType}]`,
17
25
  getAttrs(domNode) {
18
26
  const dom = domNode;
19
- return {
20
- color: dom.getAttribute("data-color"),
21
- iconType: dom.getAttribute("data-icon-type"),
22
- icon: dom.getAttribute("data-icon"),
23
- };
27
+ const color = dom.getAttribute("data-color");
28
+ const iconType = dom.getAttribute("data-icon-type");
29
+ const icon = dom.getAttribute("data-icon");
30
+ if (iconType === "emoji") {
31
+ // A custom emoji is stored in `icon` as a workspace-specific id (`:<uuid>:`).
32
+ // When pasted into another workspace that id doesn't resolve, so fall back to
33
+ // the human alias preserved in `data-icon-emoji-alias` (e.g. `:css:`). This
34
+ // mirrors the inline emoji node and handles copy-paste between workspaces.
35
+ const alias = dom.getAttribute(aliasAttr);
36
+ const emoji = (icon && getEmoji(icon)) || (alias && getEmoji(alias)) || null;
37
+ return { color, iconType, icon: emoji ? emoji.colons : icon };
38
+ }
39
+ return { color, iconType, icon };
24
40
  },
25
41
  },
26
42
  ],
@@ -32,6 +48,15 @@ exports.callout = {
32
48
  "data-icon-type": iconType,
33
49
  "data-icon": icon,
34
50
  };
51
+ if (iconType === "emoji") {
52
+ // Preserve the human-readable alias so a custom emoji icon can be re-resolved
53
+ // when pasted into another workspace, where its id won't match.
54
+ const emoji = getEmoji(icon);
55
+ if (emoji) {
56
+ attrs[aliasAttr] = emoji.native || `:${emoji.shortNames[0]}:`;
57
+ }
58
+ }
35
59
  return ["div", attrs, 0];
36
60
  },
37
- };
61
+ });
62
+ exports.makeCallout = makeCallout;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fibery/prosemirror-schema",
3
- "version": "4.20.0",
3
+ "version": "4.22.0",
4
4
  "private": false,
5
5
  "description": "Fibery prosemirror schema",
6
6
  "files": [